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