Home | History | Annotate | Line # | Download | only in usb
if_cdce.c revision 1.55
      1 /*	$NetBSD: if_cdce.c,v 1.55 2019/07/31 23:47:16 mrg Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul (at) 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.55 2019/07/31 23:47:16 mrg Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/kernel.h>
     47 
     48 #include <dev/usb/usbnet.h>
     49 #include <dev/usb/usbcdc.h>
     50 
     51 #include <dev/usb/if_cdcereg.h>
     52 
     53 struct cdce_type {
     54 	struct usb_devno	 cdce_dev;
     55 	uint16_t		 cdce_flags;
     56 #define CDCE_ZAURUS	1
     57 #define CDCE_NO_UNION	2
     58 };
     59 
     60 struct cdce_softc {
     61 	struct usbnet		 cdce_un;
     62 	uint16_t		 cdce_flags;
     63 };
     64 
     65 static const struct cdce_type cdce_devs[] = {
     66   {{ USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632 }, CDCE_NO_UNION },
     67   {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX }, CDCE_NO_UNION },
     68   {{ USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00 }, CDCE_NO_UNION },
     69   {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN }, CDCE_ZAURUS | CDCE_NO_UNION },
     70   {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2 }, CDCE_ZAURUS | CDCE_NO_UNION },
     71   {{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501 }, CDCE_NO_UNION },
     72   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500 }, CDCE_ZAURUS },
     73   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_A300 }, CDCE_ZAURUS | CDCE_NO_UNION },
     74   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600 }, CDCE_ZAURUS | CDCE_NO_UNION },
     75   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C700 }, CDCE_ZAURUS | CDCE_NO_UNION },
     76   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C750 }, CDCE_ZAURUS | CDCE_NO_UNION },
     77 };
     78 #define cdce_lookup(v, p) \
     79 	((const struct cdce_type *)usb_lookup(cdce_devs, v, p))
     80 
     81 static int	cdce_match(device_t, cfdata_t, void *);
     82 static void	cdce_attach(device_t, device_t, void *);
     83 static int	cdce_init(struct ifnet *);
     84 static void	cdce_rxeof_loop(struct usbnet *, struct usbd_xfer *,
     85 				struct usbnet_chain *, uint32_t);
     86 static unsigned	cdce_tx_prepare(struct usbnet *, struct mbuf *,
     87 				struct usbnet_chain *);
     88 
     89 CFATTACH_DECL_NEW(cdce, sizeof(struct cdce_softc), cdce_match, cdce_attach,
     90     usbnet_detach, usbnet_activate);
     91 
     92 static int
     93 cdce_match(device_t parent, cfdata_t match, void *aux)
     94 {
     95 	struct usbif_attach_arg *uiaa = aux;
     96 
     97 	if (cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
     98 		return UMATCH_VENDOR_PRODUCT;
     99 
    100 	if (uiaa->uiaa_class == UICLASS_CDC && uiaa->uiaa_subclass ==
    101 	    UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL)
    102 		return UMATCH_IFACECLASS_GENERIC;
    103 
    104 	return UMATCH_NONE;
    105 }
    106 
    107 static void
    108 cdce_attach(device_t parent, device_t self, void *aux)
    109 {
    110 	struct cdce_softc 		*sc = device_private(self);
    111 	struct usbnet * const		 un = &sc->cdce_un;
    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 	/* Switch to usbnet for device_private() */
    126 	self->dv_private = un;
    127 
    128 	aprint_naive("\n");
    129 	aprint_normal("\n");
    130 	devinfop = usbd_devinfo_alloc(dev, 0);
    131 	aprint_normal_dev(self, "%s\n", devinfop);
    132 	usbd_devinfo_free(devinfop);
    133 
    134 	un->un_dev = self;
    135 	un->un_udev = dev;
    136 	un->un_sc = sc;
    137 	un->un_init_cb = cdce_init;
    138 	un->un_tx_prepare_cb = cdce_tx_prepare;
    139 	un->un_rx_loop_cb = cdce_rxeof_loop;
    140 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
    141 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
    142 	un->un_cdata.uncd_rx_bufsz = CDCE_BUFSZ;
    143 	un->un_cdata.uncd_tx_bufsz = CDCE_BUFSZ;
    144 
    145 	t = cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
    146 	if (t)
    147 		sc->cdce_flags = t->cdce_flags;
    148 
    149 	if (sc->cdce_flags & CDCE_NO_UNION)
    150 		un->un_iface = uiaa->uiaa_iface;
    151 	else {
    152 		ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(un->un_udev,
    153 		    UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION);
    154 		if (ud == NULL) {
    155 			aprint_error_dev(self, "no union descriptor\n");
    156 			return;
    157 		}
    158 		data_ifcno = ud->bSlaveInterface[0];
    159 
    160 		for (i = 0; i < uiaa->uiaa_nifaces; i++) {
    161 			if (uiaa->uiaa_ifaces[i] != NULL) {
    162 				id = usbd_get_interface_descriptor(
    163 				    uiaa->uiaa_ifaces[i]);
    164 				if (id != NULL && id->bInterfaceNumber ==
    165 				    data_ifcno) {
    166 					un->un_iface = uiaa->uiaa_ifaces[i];
    167 					uiaa->uiaa_ifaces[i] = NULL;
    168 				}
    169 			}
    170 		}
    171 	}
    172 	if (un->un_iface == NULL) {
    173 		aprint_error_dev(self, "no data interface\n");
    174 		return;
    175 	}
    176 
    177 	/*
    178 	 * <quote>
    179 	 *  The Data Class interface of a networking device shall have a minimum
    180 	 *  of two interface settings. The first setting (the default interface
    181 	 *  setting) includes no endpoints and therefore no networking traffic is
    182 	 *  exchanged whenever the default interface setting is selected. One or
    183 	 *  more additional interface settings are used for normal operation, and
    184 	 *  therefore each includes a pair of endpoints (one IN, and one OUT) to
    185 	 *  exchange network traffic. Select an alternate interface setting to
    186 	 *  initialize the network aspects of the device and to enable the
    187 	 *  exchange of network traffic.
    188 	 * </quote>
    189 	 *
    190 	 * Some devices, most notably cable modems, include interface settings
    191 	 * that have no IN or OUT endpoint, therefore loop through the list of all
    192 	 * available interface settings looking for one with both IN and OUT
    193 	 * endpoints.
    194 	 */
    195 	id = usbd_get_interface_descriptor(un->un_iface);
    196 	cd = usbd_get_config_descriptor(un->un_udev);
    197 	numalts = usbd_get_no_alts(cd, id->bInterfaceNumber);
    198 
    199 	for (j = 0; j < numalts; j++) {
    200 		if (usbd_set_interface(un->un_iface, j)) {
    201 			aprint_error_dev(un->un_dev,
    202 					"setting alternate interface failed\n");
    203 			return;
    204 		}
    205 		/* Find endpoints. */
    206 		id = usbd_get_interface_descriptor(un->un_iface);
    207 		un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = -1;
    208 		for (i = 0; i < id->bNumEndpoints; i++) {
    209 			ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
    210 			if (!ed) {
    211 				aprint_error_dev(self,
    212 						"could not read endpoint descriptor\n");
    213 				return;
    214 			}
    215 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    216 					UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    217 				un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
    218 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    219 					UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    220 				un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
    221 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    222 					UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    223 				/* XXX: CDC spec defines an interrupt pipe, but it is not
    224 				 * needed for simple host-to-host applications. */
    225 			} else {
    226 				aprint_error_dev(self, "unexpected endpoint\n");
    227 			}
    228 		}
    229 		/* If we found something, try and use it... */
    230 		if (un->un_ed[USBNET_ENDPT_RX] != -1 && un->un_ed[USBNET_ENDPT_TX] != -1)
    231 			break;
    232 	}
    233 
    234 	if (un->un_ed[USBNET_ENDPT_RX] == -1) {
    235 		aprint_error_dev(self, "could not find data bulk in\n");
    236 		return;
    237 	}
    238 	if (un->un_ed[USBNET_ENDPT_TX] == -1 ) {
    239 		aprint_error_dev(self, "could not find data bulk out\n");
    240 		return;
    241 	}
    242 
    243 	ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc(dev,
    244 	    UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF);
    245 	if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) ||
    246 	    ether_aton_r(un->un_eaddr, sizeof(un->un_eaddr), eaddr_str)) {
    247 		aprint_normal_dev(self, "faking address\n");
    248 		un->un_eaddr[0] = 0x2a;
    249 		memcpy(&un->un_eaddr[1], &hardclock_ticks, sizeof(uint32_t));
    250 		un->un_eaddr[5] = (uint8_t)(device_unit(un->un_dev));
    251 	}
    252 
    253 	aprint_normal_dev(self, "address %s\n", ether_sprintf(un->un_eaddr));
    254 
    255 	usbnet_attach(un, "cdcedet", CDCE_RX_LIST_CNT, CDCE_TX_LIST_CNT);
    256 	usbnet_attach_ifp(un, false, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
    257             0, 0);
    258 }
    259 
    260 static int
    261 cdce_init(struct ifnet *ifp)
    262 {
    263 	struct usbnet		*un = ifp->if_softc;
    264 	int rv;
    265 
    266 	mutex_enter(&un->un_lock);
    267 	if (un->un_dying)
    268 		rv = EIO;
    269 	else {
    270 		usbnet_stop(un, ifp, 1);
    271 		rv = usbnet_init_rx_tx(un, 0, 0);
    272 		if (rv == 0)
    273 			un->un_link = true;
    274 	}
    275 	mutex_exit(&un->un_lock);
    276 
    277 	return rv;
    278 }
    279 
    280 static void
    281 cdce_rxeof_loop(struct usbnet * un, struct usbd_xfer *xfer,
    282 		struct usbnet_chain *c, uint32_t total_len)
    283 {
    284 	struct ifnet		*ifp = usbnet_ifp(un);
    285 	struct cdce_softc	*sc = un->un_sc;
    286 
    287 	KASSERT(mutex_owned(&un->un_rxlock));
    288 
    289 	/* Strip off CRC added by Zaurus, if present */
    290 	if (sc->cdce_flags & CDCE_ZAURUS && total_len > 4)
    291 		total_len -= 4;
    292 
    293 	if (total_len < sizeof(struct ether_header)) {
    294 		ifp->if_ierrors++;
    295 		return;
    296 	}
    297 
    298 	usbnet_enqueue(un, c->unc_buf, total_len, 0);
    299 }
    300 
    301 static unsigned
    302 cdce_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
    303 {
    304 	struct cdce_softc	*sc = un->un_sc;
    305 	int			 extra = 0;
    306 
    307 	KASSERT(mutex_owned(&un->un_txlock));
    308 
    309 	m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
    310 	if (sc->cdce_flags & CDCE_ZAURUS) {
    311 		/* Zaurus wants a 32-bit CRC appended to every frame */
    312 		uint32_t crc;
    313 
    314 		crc = htole32(~ether_crc32_le(c->unc_buf, m->m_pkthdr.len));
    315 		memcpy(c->unc_buf + m->m_pkthdr.len, &crc, sizeof(crc));
    316 		extra = sizeof(crc);
    317 	}
    318 
    319 	return m->m_pkthdr.len + extra;
    320 }
    321