1 /*        $NetBSD: if_cdce.c,v 1.83 2025/01/18 18:35:47 riastradh 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  *
40  * Originally written for the 1.1 spec at:
41  * https://web.archive.org/web/20140824090418/http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
42  *
43  * Updated 1.2 spec at:
44  * https://web.archive.org/web/20241127002135/https://www.usb.org/document-library/class-definitions-communication-devices-12
45  * https://web.archive.org/web/20240510195654/https://www.usb.org/sites/default/files/CDC1.2_WMC1.1_012011.zip
46  */
47 
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.83 2025/01/18 18:35:47 riastradh Exp $");
50 
51 #include <sys/param.h>
52 
53 #include <dev/usb/usbnet.h>
54 #include <dev/usb/usbcdc.h>
55 
56 #include <dev/usb/if_cdcereg.h>
57 
58 struct cdce_type {
59           struct usb_devno     cdce_dev;
60           uint16_t             cdce_flags;
61 #define CDCE_ZAURUS 1
62 #define CDCE_NO_UNION         2
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 
84 CFATTACH_DECL_NEW(cdce, sizeof(struct usbnet), cdce_match, cdce_attach,
85     usbnet_detach, usbnet_activate);
86 
87 static void         cdce_uno_rx_loop(struct usbnet *, struct usbnet_chain *,
88                                          uint32_t);
89 static unsigned     cdce_uno_tx_prepare(struct usbnet *, struct mbuf *,
90                                             struct usbnet_chain *);
91 
92 static const struct usbnet_ops cdce_ops = {
93           .uno_tx_prepare = cdce_uno_tx_prepare,
94           .uno_rx_loop = cdce_uno_rx_loop,
95 };
96 
97 static int
cdce_match(device_t parent,cfdata_t match,void * aux)98 cdce_match(device_t parent, cfdata_t match, void *aux)
99 {
100           struct usbif_attach_arg *uiaa = aux;
101 
102           if (cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
103                     return UMATCH_VENDOR_PRODUCT;
104 
105           if (uiaa->uiaa_class == UICLASS_CDC && uiaa->uiaa_subclass ==
106               UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL)
107                     return UMATCH_IFACECLASS_GENERIC;
108 
109           return UMATCH_NONE;
110 }
111 
112 static void
cdce_attach(device_t parent,device_t self,void * aux)113 cdce_attach(device_t parent, device_t self, void *aux)
114 {
115           struct usbnet * const                    un = device_private(self);
116           struct usbif_attach_arg                 *uiaa = aux;
117           char                                    *devinfop;
118           struct usbd_device          *dev = uiaa->uiaa_device;
119           const struct cdce_type                  *t;
120           usb_interface_descriptor_t    *id;
121           usb_endpoint_descriptor_t     *ed;
122           const usb_cdc_union_descriptor_t *ud;
123           usb_config_descriptor_t                 *cd;
124           int                                      data_ifcno;
125           int                                      i, j, numalts;
126           const usb_cdc_ethernet_descriptor_t *ue;
127           char                                     eaddr_str[USB_MAX_ENCODED_STRING_LEN];
128 
129           aprint_naive("\n");
130           aprint_normal("\n");
131           devinfop = usbd_devinfo_alloc(dev, 0);
132           aprint_normal_dev(self, "%s\n", devinfop);
133           usbd_devinfo_free(devinfop);
134 
135           un->un_dev = self;
136           un->un_udev = dev;
137           un->un_sc = un;
138           un->un_ops = &cdce_ops;
139           un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
140           un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
141           un->un_rx_list_cnt = CDCE_RX_LIST_CNT;
142           un->un_tx_list_cnt = CDCE_TX_LIST_CNT;
143           un->un_rx_bufsz = CDCE_BUFSZ;
144           un->un_tx_bufsz = CDCE_BUFSZ;
145 
146           t = cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
147           if (t)
148                     un->un_flags = t->cdce_flags;
149 
150           if (un->un_flags & CDCE_NO_UNION)
151                     un->un_iface = uiaa->uiaa_iface;
152           else {
153                     ud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(un->un_udev,
154                         UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION,
155                         usbd_get_interface_descriptor(uiaa->uiaa_iface));
156                     if (ud == NULL) {
157                               aprint_error_dev(self, "no union descriptor\n");
158                               return;
159                     }
160                     data_ifcno = ud->bSlaveInterface[0];
161 
162                     for (i = 0; i < uiaa->uiaa_nifaces; i++) {
163                               if (uiaa->uiaa_ifaces[i] != NULL) {
164                                         id = usbd_get_interface_descriptor(
165                                             uiaa->uiaa_ifaces[i]);
166                                         if (id != NULL && id->bInterfaceNumber ==
167                                             data_ifcno) {
168                                                   un->un_iface = uiaa->uiaa_ifaces[i];
169                                                   uiaa->uiaa_ifaces[i] = NULL;
170                                         }
171                               }
172                     }
173           }
174           if (un->un_iface == NULL) {
175                     aprint_error_dev(self, "no data interface\n");
176                     return;
177           }
178 
179           /*
180            * <quote>
181            *  The Data Class interface of a networking device shall have a minimum
182            *  of two interface settings. The first setting (the default interface
183            *  setting) includes no endpoints and therefore no networking traffic is
184            *  exchanged whenever the default interface setting is selected. One or
185            *  more additional interface settings are used for normal operation, and
186            *  therefore each includes a pair of endpoints (one IN, and one OUT) to
187            *  exchange network traffic. Select an alternate interface setting to
188            *  initialize the network aspects of the device and to enable the
189            *  exchange of network traffic.
190            * </quote>
191            *
192            * Some devices, most notably cable modems, include interface settings
193            * that have no IN or OUT endpoint, therefore loop through the list of all
194            * available interface settings looking for one with both IN and OUT
195            * endpoints.
196            */
197           id = usbd_get_interface_descriptor(un->un_iface);
198           cd = usbd_get_config_descriptor(un->un_udev);
199           numalts = usbd_get_no_alts(cd, id->bInterfaceNumber);
200 
201           for (j = 0; j < numalts; j++) {
202                     if (usbd_set_interface(un->un_iface, j)) {
203                               aprint_error_dev(un->un_dev,
204                                                   "setting alternate interface failed\n");
205                               return;
206                     }
207                     /* Find endpoints. */
208                     id = usbd_get_interface_descriptor(un->un_iface);
209                     un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = 0;
210                     for (i = 0; i < id->bNumEndpoints; i++) {
211                               ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
212                               if (!ed) {
213                                         aprint_error_dev(self,
214                                                             "could not read endpoint descriptor\n");
215                                         return;
216                               }
217                               if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
218                                                   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
219                                         un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
220                               } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
221                                                   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
222                                         un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
223                               } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
224                                                   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
225                                         /* XXX: CDC spec defines an interrupt pipe, but it is not
226                                          * needed for simple host-to-host applications. */
227                               } else {
228                                         aprint_error_dev(self, "unexpected endpoint\n");
229                               }
230                     }
231                     /* If we found something, try and use it... */
232                     if (un->un_ed[USBNET_ENDPT_RX] != 0 && un->un_ed[USBNET_ENDPT_TX] != 0)
233                               break;
234           }
235 
236           if (un->un_ed[USBNET_ENDPT_RX] == 0) {
237                     aprint_error_dev(self, "could not find data bulk in\n");
238                     return;
239           }
240           if (un->un_ed[USBNET_ENDPT_TX] == 0) {
241                     aprint_error_dev(self, "could not find data bulk out\n");
242                     return;
243           }
244 
245           ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc_if(dev,
246               UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF,
247               usbd_get_interface_descriptor(uiaa->uiaa_iface));
248           if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) ||
249               ether_aton_r(un->un_eaddr, sizeof(un->un_eaddr), eaddr_str)) {
250                     aprint_normal_dev(self, "faking address\n");
251                     un->un_eaddr[0] = 0x2a;
252                     uint32_t ticks = getticks();
253                     memcpy(&un->un_eaddr[1], &ticks, sizeof(ticks));
254                     un->un_eaddr[5] = (uint8_t)(device_unit(un->un_dev));
255           }
256 
257           usbnet_attach(un);
258           usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
259             0, NULL);
260 
261           /* XXX There is no link state, pretend we are always on */
262           if_link_state_change(usbnet_ifp(un), LINK_STATE_UP);
263 }
264 
265 static void
cdce_uno_rx_loop(struct usbnet * un,struct usbnet_chain * c,uint32_t total_len)266 cdce_uno_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len)
267 {
268           struct ifnet                  *ifp = usbnet_ifp(un);
269 
270           /* Strip off CRC added by Zaurus, if present */
271           if (un->un_flags & CDCE_ZAURUS && total_len > 4)
272                     total_len -= 4;
273 
274           if (total_len < sizeof(struct ether_header)) {
275                     if_statinc(ifp, if_ierrors);
276                     return;
277           }
278 
279           usbnet_enqueue(un, c->unc_buf, total_len, 0, 0, 0);
280 }
281 
282 static unsigned
cdce_uno_tx_prepare(struct usbnet * un,struct mbuf * m,struct usbnet_chain * c)283 cdce_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
284 {
285           /* Zaurus wants a 32-bit CRC appended to every frame */
286           uint32_t             crc;
287           unsigned             extra = 0;
288           unsigned             length;
289 
290           if (un->un_flags & CDCE_ZAURUS)
291                     extra = sizeof(crc);
292 
293           if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - extra)
294                     return 0;
295           length = m->m_pkthdr.len + extra;
296 
297           m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
298           if (un->un_flags & CDCE_ZAURUS) {
299                     crc = htole32(~ether_crc32_le(c->unc_buf, m->m_pkthdr.len));
300                     memcpy(c->unc_buf + m->m_pkthdr.len, &crc, sizeof(crc));
301           }
302 
303           return length;
304 }
305 
306 #ifdef _MODULE
307 #include "ioconf.c"
308 #endif
309 
310 USBNET_MODULE(cdce)
311