1 /* $NetBSD: if_cdce.c,v 1.72 2020/05/15 19:28:10 maxv Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com> 5 * Copyright (c) 2003 Craig Boston 6 * Copyright (c) 2004 Daniel Hartmeier 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Bill Paul. 20 * 4. Neither the name of the author nor the names of any co-contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR 28 * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 /* 38 * USB Communication Device Class (Ethernet Networking Control Model) 39 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.72 2020/05/15 19:28:10 maxv Exp $"); 44 45 #include <sys/param.h> 46 47 #include <dev/usb/usbnet.h> 48 #include <dev/usb/usbcdc.h> 49 50 #include <dev/usb/if_cdcereg.h> 51 52 struct cdce_type { 53 struct usb_devno cdce_dev; 54 uint16_t cdce_flags; 55 #define CDCE_ZAURUS 1 56 #define CDCE_NO_UNION 2 57 }; 58 59 static const struct cdce_type cdce_devs[] = { 60 {{ USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632 }, CDCE_NO_UNION }, 61 {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX }, CDCE_NO_UNION }, 62 {{ USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00 }, CDCE_NO_UNION }, 63 {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN }, CDCE_ZAURUS | CDCE_NO_UNION }, 64 {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2 }, CDCE_ZAURUS | CDCE_NO_UNION }, 65 {{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501 }, CDCE_NO_UNION }, 66 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500 }, CDCE_ZAURUS }, 67 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_A300 }, CDCE_ZAURUS | CDCE_NO_UNION }, 68 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600 }, CDCE_ZAURUS | CDCE_NO_UNION }, 69 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C700 }, CDCE_ZAURUS | CDCE_NO_UNION }, 70 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C750 }, CDCE_ZAURUS | CDCE_NO_UNION }, 71 }; 72 #define cdce_lookup(v, p) \ 73 ((const struct cdce_type *)usb_lookup(cdce_devs, v, p)) 74 75 static int cdce_match(device_t, cfdata_t, void *); 76 static void cdce_attach(device_t, device_t, void *); 77 78 CFATTACH_DECL_NEW(cdce, sizeof(struct usbnet), cdce_match, cdce_attach, 79 usbnet_detach, usbnet_activate); 80 81 static void cdce_uno_rx_loop(struct usbnet *, struct usbnet_chain *, 82 uint32_t); 83 static unsigned cdce_uno_tx_prepare(struct usbnet *, struct mbuf *, 84 struct usbnet_chain *); 85 static int cdce_uno_init(struct ifnet *); 86 87 static const struct usbnet_ops cdce_ops = { 88 .uno_tx_prepare = cdce_uno_tx_prepare, 89 .uno_rx_loop = cdce_uno_rx_loop, 90 .uno_init = cdce_uno_init, 91 }; 92 93 static int 94 cdce_match(device_t parent, cfdata_t match, void *aux) 95 { 96 struct usbif_attach_arg *uiaa = aux; 97 98 if (cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL) 99 return UMATCH_VENDOR_PRODUCT; 100 101 if (uiaa->uiaa_class == UICLASS_CDC && uiaa->uiaa_subclass == 102 UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL) 103 return UMATCH_IFACECLASS_GENERIC; 104 105 return UMATCH_NONE; 106 } 107 108 static void 109 cdce_attach(device_t parent, device_t self, void *aux) 110 { 111 struct usbnet * const un = device_private(self); 112 struct usbif_attach_arg *uiaa = aux; 113 char *devinfop; 114 struct usbd_device *dev = uiaa->uiaa_device; 115 const struct cdce_type *t; 116 usb_interface_descriptor_t *id; 117 usb_endpoint_descriptor_t *ed; 118 const usb_cdc_union_descriptor_t *ud; 119 usb_config_descriptor_t *cd; 120 int data_ifcno; 121 int i, j, numalts; 122 const usb_cdc_ethernet_descriptor_t *ue; 123 char eaddr_str[USB_MAX_ENCODED_STRING_LEN]; 124 125 aprint_naive("\n"); 126 aprint_normal("\n"); 127 devinfop = usbd_devinfo_alloc(dev, 0); 128 aprint_normal_dev(self, "%s\n", devinfop); 129 usbd_devinfo_free(devinfop); 130 131 un->un_dev = self; 132 un->un_udev = dev; 133 un->un_sc = un; 134 un->un_ops = &cdce_ops; 135 un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; 136 un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; 137 un->un_rx_list_cnt = CDCE_RX_LIST_CNT; 138 un->un_tx_list_cnt = CDCE_TX_LIST_CNT; 139 un->un_rx_bufsz = CDCE_BUFSZ; 140 un->un_tx_bufsz = CDCE_BUFSZ; 141 142 t = cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product); 143 if (t) 144 un->un_flags = t->cdce_flags; 145 146 if (un->un_flags & CDCE_NO_UNION) 147 un->un_iface = uiaa->uiaa_iface; 148 else { 149 ud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(un->un_udev, 150 UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION, 151 usbd_get_interface_descriptor(uiaa->uiaa_iface)); 152 if (ud == NULL) { 153 aprint_error_dev(self, "no union descriptor\n"); 154 return; 155 } 156 data_ifcno = ud->bSlaveInterface[0]; 157 158 for (i = 0; i < uiaa->uiaa_nifaces; i++) { 159 if (uiaa->uiaa_ifaces[i] != NULL) { 160 id = usbd_get_interface_descriptor( 161 uiaa->uiaa_ifaces[i]); 162 if (id != NULL && id->bInterfaceNumber == 163 data_ifcno) { 164 un->un_iface = uiaa->uiaa_ifaces[i]; 165 uiaa->uiaa_ifaces[i] = NULL; 166 } 167 } 168 } 169 } 170 if (un->un_iface == NULL) { 171 aprint_error_dev(self, "no data interface\n"); 172 return; 173 } 174 175 /* 176 * <quote> 177 * The Data Class interface of a networking device shall have a minimum 178 * of two interface settings. The first setting (the default interface 179 * setting) includes no endpoints and therefore no networking traffic is 180 * exchanged whenever the default interface setting is selected. One or 181 * more additional interface settings are used for normal operation, and 182 * therefore each includes a pair of endpoints (one IN, and one OUT) to 183 * exchange network traffic. Select an alternate interface setting to 184 * initialize the network aspects of the device and to enable the 185 * exchange of network traffic. 186 * </quote> 187 * 188 * Some devices, most notably cable modems, include interface settings 189 * that have no IN or OUT endpoint, therefore loop through the list of all 190 * available interface settings looking for one with both IN and OUT 191 * endpoints. 192 */ 193 id = usbd_get_interface_descriptor(un->un_iface); 194 cd = usbd_get_config_descriptor(un->un_udev); 195 numalts = usbd_get_no_alts(cd, id->bInterfaceNumber); 196 197 for (j = 0; j < numalts; j++) { 198 if (usbd_set_interface(un->un_iface, j)) { 199 aprint_error_dev(un->un_dev, 200 "setting alternate interface failed\n"); 201 return; 202 } 203 /* Find endpoints. */ 204 id = usbd_get_interface_descriptor(un->un_iface); 205 un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = 0; 206 for (i = 0; i < id->bNumEndpoints; i++) { 207 ed = usbd_interface2endpoint_descriptor(un->un_iface, i); 208 if (!ed) { 209 aprint_error_dev(self, 210 "could not read endpoint descriptor\n"); 211 return; 212 } 213 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 214 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 215 un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; 216 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 217 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 218 un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; 219 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 220 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 221 /* XXX: CDC spec defines an interrupt pipe, but it is not 222 * needed for simple host-to-host applications. */ 223 } else { 224 aprint_error_dev(self, "unexpected endpoint\n"); 225 } 226 } 227 /* If we found something, try and use it... */ 228 if (un->un_ed[USBNET_ENDPT_RX] != 0 && un->un_ed[USBNET_ENDPT_TX] != 0) 229 break; 230 } 231 232 if (un->un_ed[USBNET_ENDPT_RX] == 0) { 233 aprint_error_dev(self, "could not find data bulk in\n"); 234 return; 235 } 236 if (un->un_ed[USBNET_ENDPT_TX] == 0) { 237 aprint_error_dev(self, "could not find data bulk out\n"); 238 return; 239 } 240 241 ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc_if(dev, 242 UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF, 243 usbd_get_interface_descriptor(uiaa->uiaa_iface)); 244 if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) || 245 ether_aton_r(un->un_eaddr, sizeof(un->un_eaddr), eaddr_str)) { 246 aprint_normal_dev(self, "faking address\n"); 247 un->un_eaddr[0] = 0x2a; 248 uint32_t ticks = getticks(); 249 memcpy(&un->un_eaddr[1], &ticks, sizeof(ticks)); 250 un->un_eaddr[5] = (uint8_t)(device_unit(un->un_dev)); 251 } 252 253 usbnet_attach(un, "cdcedet"); 254 usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, 255 0, NULL); 256 } 257 258 static int 259 cdce_uno_init(struct ifnet *ifp) 260 { 261 struct usbnet *un = ifp->if_softc; 262 int rv; 263 264 usbnet_lock_core(un); 265 if (usbnet_isdying(un)) 266 rv = EIO; 267 else { 268 usbnet_stop(un, ifp, 1); 269 rv = usbnet_init_rx_tx(un); 270 usbnet_set_link(un, rv == 0); 271 } 272 usbnet_unlock_core(un); 273 274 return rv; 275 } 276 277 static void 278 cdce_uno_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len) 279 { 280 struct ifnet *ifp = usbnet_ifp(un); 281 282 usbnet_isowned_rx(un); 283 284 /* Strip off CRC added by Zaurus, if present */ 285 if (un->un_flags & CDCE_ZAURUS && total_len > 4) 286 total_len -= 4; 287 288 if (total_len < sizeof(struct ether_header)) { 289 if_statinc(ifp, if_ierrors); 290 return; 291 } 292 293 usbnet_enqueue(un, c->unc_buf, total_len, 0, 0, 0); 294 } 295 296 static unsigned 297 cdce_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) 298 { 299 /* Zaurus wants a 32-bit CRC appended to every frame */ 300 uint32_t crc; 301 unsigned extra = 0; 302 unsigned length; 303 304 if (un->un_flags & CDCE_ZAURUS) 305 extra = sizeof(crc); 306 307 if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - extra) 308 return 0; 309 length = m->m_pkthdr.len + extra; 310 311 m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf); 312 if (un->un_flags & CDCE_ZAURUS) { 313 crc = htole32(~ether_crc32_le(c->unc_buf, m->m_pkthdr.len)); 314 memcpy(c->unc_buf + m->m_pkthdr.len, &crc, sizeof(crc)); 315 } 316 317 return length; 318 } 319 320 #ifdef _MODULE 321 #include "ioconf.c" 322 #endif 323 324 USBNET_MODULE(cdce) 325