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