1 /* $NetBSD: if_cdce.c,v 1.4 2004/10/24 12:50:54 augustss Exp $ */
4 * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com>
5 * Copyright (c) 2003-2005 Craig Boston
6 * Copyright (c) 2004 Daniel Hartmeier
7 * Copyright (c) 2009 Hans Petter Selasky
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Bill Paul.
21 * 4. Neither the name of the author nor the names of any co-contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR
29 * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * USB Communication Device Class (Ethernet Networking Control Model)
40 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
44 * USB Network Control Model (NCM)
45 * http://www.usb.org/developers/devclass_docs/NCM10.zip
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/module.h>
61 #include <sys/mutex.h>
62 #include <sys/condvar.h>
63 #include <sys/sysctl.h>
65 #include <sys/unistd.h>
66 #include <sys/callout.h>
67 #include <sys/malloc.h>
70 #include <dev/usb/usb.h>
71 #include <dev/usb/usbdi.h>
72 #include <dev/usb/usbdi_util.h>
73 #include <dev/usb/usb_cdc.h>
76 #define USB_DEBUG_VAR cdce_debug
77 #include <dev/usb/usb_debug.h>
78 #include <dev/usb/usb_process.h>
81 #include <dev/usb/net/usb_ethernet.h>
82 #include <dev/usb/net/if_cdcereg.h>
84 static device_probe_t cdce_probe;
85 static device_attach_t cdce_attach;
86 static device_detach_t cdce_detach;
87 static device_suspend_t cdce_suspend;
88 static device_resume_t cdce_resume;
89 static usb_handle_request_t cdce_handle_request;
91 static usb_callback_t cdce_bulk_write_callback;
92 static usb_callback_t cdce_bulk_read_callback;
93 static usb_callback_t cdce_intr_read_callback;
94 static usb_callback_t cdce_intr_write_callback;
97 static usb_callback_t cdce_ncm_bulk_write_callback;
98 static usb_callback_t cdce_ncm_bulk_read_callback;
101 static uether_fn_t cdce_attach_post;
102 static uether_fn_t cdce_init;
103 static uether_fn_t cdce_stop;
104 static uether_fn_t cdce_start;
105 static uether_fn_t cdce_setmulti;
106 static uether_fn_t cdce_setpromisc;
108 static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
111 static int cdce_debug = 0;
112 static int cdce_tx_interval = 0;
114 static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
115 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0,
117 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RW, &cdce_tx_interval, 0,
118 "NCM transmit interval in ms");
121 static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
125 .endpoint = UE_ADDR_ANY,
126 .direction = UE_DIR_RX,
128 .frames = CDCE_FRAMES_MAX,
129 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
130 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
131 .callback = cdce_bulk_read_callback,
132 .timeout = 0, /* no timeout */
133 .usb_mode = USB_MODE_DUAL, /* both modes */
138 .endpoint = UE_ADDR_ANY,
139 .direction = UE_DIR_TX,
141 .frames = CDCE_FRAMES_MAX,
142 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
143 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
144 .callback = cdce_bulk_write_callback,
145 .timeout = 10000, /* 10 seconds */
146 .usb_mode = USB_MODE_DUAL, /* both modes */
150 .type = UE_INTERRUPT,
151 .endpoint = UE_ADDR_ANY,
152 .direction = UE_DIR_RX,
154 .bufsize = CDCE_IND_SIZE_MAX,
155 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
156 .callback = cdce_intr_read_callback,
158 .usb_mode = USB_MODE_HOST,
162 .type = UE_INTERRUPT,
163 .endpoint = UE_ADDR_ANY,
164 .direction = UE_DIR_TX,
166 .bufsize = CDCE_IND_SIZE_MAX,
167 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
168 .callback = cdce_intr_write_callback,
169 .timeout = 10000, /* 10 seconds */
170 .usb_mode = USB_MODE_DEVICE,
175 static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
179 .endpoint = UE_ADDR_ANY,
180 .direction = UE_DIR_RX,
182 .frames = CDCE_NCM_RX_FRAMES_MAX,
183 .bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
184 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
185 .callback = cdce_ncm_bulk_read_callback,
186 .timeout = 0, /* no timeout */
187 .usb_mode = USB_MODE_DUAL, /* both modes */
192 .endpoint = UE_ADDR_ANY,
193 .direction = UE_DIR_TX,
195 .frames = CDCE_NCM_TX_FRAMES_MAX,
196 .bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
197 .flags = {.pipe_bof = 1,},
198 .callback = cdce_ncm_bulk_write_callback,
199 .timeout = 10000, /* 10 seconds */
200 .usb_mode = USB_MODE_DUAL, /* both modes */
204 .type = UE_INTERRUPT,
205 .endpoint = UE_ADDR_ANY,
206 .direction = UE_DIR_RX,
208 .bufsize = CDCE_IND_SIZE_MAX,
209 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
210 .callback = cdce_intr_read_callback,
212 .usb_mode = USB_MODE_HOST,
216 .type = UE_INTERRUPT,
217 .endpoint = UE_ADDR_ANY,
218 .direction = UE_DIR_TX,
220 .bufsize = CDCE_IND_SIZE_MAX,
221 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
222 .callback = cdce_intr_write_callback,
223 .timeout = 10000, /* 10 seconds */
224 .usb_mode = USB_MODE_DEVICE,
229 static device_method_t cdce_methods[] = {
231 DEVMETHOD(usb_handle_request, cdce_handle_request),
233 /* Device interface */
234 DEVMETHOD(device_probe, cdce_probe),
235 DEVMETHOD(device_attach, cdce_attach),
236 DEVMETHOD(device_detach, cdce_detach),
237 DEVMETHOD(device_suspend, cdce_suspend),
238 DEVMETHOD(device_resume, cdce_resume),
243 static driver_t cdce_driver = {
245 .methods = cdce_methods,
246 .size = sizeof(struct cdce_softc),
249 static devclass_t cdce_devclass;
251 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0);
252 MODULE_VERSION(cdce, 1);
253 MODULE_DEPEND(cdce, uether, 1, 1, 1);
254 MODULE_DEPEND(cdce, usb, 1, 1, 1);
255 MODULE_DEPEND(cdce, ether, 1, 1, 1);
257 static const struct usb_ether_methods cdce_ue_methods = {
258 .ue_attach_post = cdce_attach_post,
259 .ue_start = cdce_start,
260 .ue_init = cdce_init,
261 .ue_stop = cdce_stop,
262 .ue_setmulti = cdce_setmulti,
263 .ue_setpromisc = cdce_setpromisc,
266 static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
267 {USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
268 {USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
269 {USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
270 {USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
271 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
272 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
273 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
274 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
275 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
276 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
277 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
278 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
279 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
282 static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
283 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
284 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
285 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
289 /*------------------------------------------------------------------------*
295 *------------------------------------------------------------------------*/
297 cdce_ncm_init(struct cdce_softc *sc)
299 struct usb_ncm_parameters temp;
300 struct usb_device_request req;
301 struct usb_ncm_func_descriptor *ufd;
305 ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
306 sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0 - 1,
307 UCDC_NCM_FUNC_DESC_SUBTYPE, 0 - 1);
309 /* verify length of NCM functional descriptor */
311 if (ufd->bLength < sizeof(*ufd))
314 DPRINTFN(1, "Found NCM functional descriptor.\n");
317 req.bmRequestType = UT_READ_CLASS_INTERFACE;
318 req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
319 USETW(req.wValue, 0);
320 req.wIndex[0] = sc->sc_ifaces_index[1];
322 USETW(req.wLength, sizeof(temp));
324 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
325 &temp, 0, NULL, 1000 /* ms */);
329 /* Read correct set of parameters according to device mode */
331 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
332 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
333 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
334 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
335 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
336 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
337 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
339 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
340 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
341 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
342 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
343 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
344 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
347 /* Verify maximum receive length */
349 if ((sc->sc_ncm.rx_max < 32) ||
350 (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
351 DPRINTFN(1, "Using default maximum receive length\n");
352 sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
355 /* Verify maximum transmit length */
357 if ((sc->sc_ncm.tx_max < 32) ||
358 (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
359 DPRINTFN(1, "Using default maximum transmit length\n");
360 sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
364 * Verify that the structure alignment is:
366 * - not greater than the maximum transmit length
367 * - not less than four bytes
369 if ((sc->sc_ncm.tx_struct_align < 4) ||
370 (sc->sc_ncm.tx_struct_align !=
371 ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
372 (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
373 DPRINTFN(1, "Using default other alignment: 4 bytes\n");
374 sc->sc_ncm.tx_struct_align = 4;
378 * Verify that the payload alignment is:
380 * - not greater than the maximum transmit length
381 * - not less than four bytes
383 if ((sc->sc_ncm.tx_modulus < 4) ||
384 (sc->sc_ncm.tx_modulus !=
385 ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
386 (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
387 DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
388 sc->sc_ncm.tx_modulus = 4;
391 /* Verify that the payload remainder */
393 if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
394 DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
395 sc->sc_ncm.tx_remainder = 0;
399 * Offset the TX remainder so that IP packet payload starts at
400 * the tx_modulus. This is not too clear in the specification.
403 sc->sc_ncm.tx_remainder =
404 (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
405 (sc->sc_ncm.tx_modulus - 1);
407 /* Verify max datagrams */
409 if (sc->sc_ncm.tx_nframe == 0 ||
410 sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
411 DPRINTFN(1, "Using default max "
412 "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
413 /* need to reserve one entry for zero padding */
414 sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
417 /* Additional configuration, will fail in device side mode, which is OK. */
419 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
420 req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
421 USETW(req.wValue, 0);
422 req.wIndex[0] = sc->sc_ifaces_index[1];
426 (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
427 USETW(req.wLength, 8);
428 USETDW(value, sc->sc_ncm.rx_max);
429 USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
432 USETW(req.wLength, 4);
433 USETDW(value, sc->sc_ncm.rx_max);
436 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
437 &value, 0, NULL, 1000 /* ms */);
439 DPRINTFN(1, "Setting input size "
440 "to %u failed.\n", sc->sc_ncm.rx_max);
443 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
444 req.bRequest = UCDC_NCM_SET_CRC_MODE;
445 USETW(req.wValue, 0); /* no CRC */
446 req.wIndex[0] = sc->sc_ifaces_index[1];
448 USETW(req.wLength, 0);
450 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
451 NULL, 0, NULL, 1000 /* ms */);
453 DPRINTFN(1, "Setting CRC mode to off failed.\n");
456 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
457 req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
458 USETW(req.wValue, 0); /* NTB-16 */
459 req.wIndex[0] = sc->sc_ifaces_index[1];
461 USETW(req.wLength, 0);
463 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
464 NULL, 0, NULL, 1000 /* ms */);
466 DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
469 return (0); /* success */
474 cdce_probe(device_t dev)
476 struct usb_attach_arg *uaa = device_get_ivars(dev);
479 error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
481 error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
486 cdce_attach_post(struct usb_ether *ue)
493 cdce_attach(device_t dev)
495 struct cdce_softc *sc = device_get_softc(dev);
496 struct usb_ether *ue = &sc->sc_ue;
497 struct usb_attach_arg *uaa = device_get_ivars(dev);
498 struct usb_interface *iface;
499 const struct usb_cdc_union_descriptor *ud;
500 const struct usb_interface_descriptor *id;
501 const struct usb_cdc_ethernet_descriptor *ued;
502 const struct usb_config *pcfg;
505 uint8_t data_iface_no;
506 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */
508 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
509 sc->sc_ue.ue_udev = uaa->device;
511 device_set_usb_desc(dev);
513 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
515 ud = usbd_find_descriptor
516 (uaa->device, NULL, uaa->info.bIfaceIndex,
517 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
519 if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
520 (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
521 DPRINTFN(1, "No union descriptor!\n");
522 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
523 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
524 goto alloc_transfers;
526 data_iface_no = ud->bSlaveInterface[0];
530 iface = usbd_get_iface(uaa->device, i);
534 id = usbd_get_interface_descriptor(iface);
536 if (id && (id->bInterfaceNumber == data_iface_no)) {
537 sc->sc_ifaces_index[0] = i;
538 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
539 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
543 device_printf(dev, "no data interface found\n");
551 * The Data Class interface of a networking device shall have
552 * a minimum of two interface settings. The first setting
553 * (the default interface setting) includes no endpoints and
554 * therefore no networking traffic is exchanged whenever the
555 * default interface setting is selected. One or more
556 * additional interface settings are used for normal
557 * operation, and therefore each includes a pair of endpoints
558 * (one IN, and one OUT) to exchange network traffic. Select
559 * an alternate interface setting to initialize the network
560 * aspects of the device and to enable the exchange of
565 * Some devices, most notably cable modems, include interface
566 * settings that have no IN or OUT endpoint, therefore loop
567 * through the list of all available interface settings
568 * looking for one with both IN and OUT endpoints.
573 pcfg = cdce_config; /* Default Configuration */
575 for (i = 0; i != 32; i++) {
577 error = usbd_set_alt_interface_index(uaa->device,
578 sc->sc_ifaces_index[0], i);
582 if ((i == 0) && (cdce_ncm_init(sc) == 0))
583 pcfg = cdce_ncm_config;
585 error = usbd_transfer_setup(uaa->device,
586 sc->sc_ifaces_index, sc->sc_xfer,
587 pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
593 if (error || (i == 32)) {
594 device_printf(dev, "No valid alternate "
599 ued = usbd_find_descriptor
600 (uaa->device, NULL, uaa->info.bIfaceIndex,
601 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1);
603 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
604 error = USB_ERR_INVAL;
606 error = usbd_req_get_string_any(uaa->device, NULL,
607 eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
612 /* fake MAC address */
614 device_printf(dev, "faking MAC address\n");
615 sc->sc_ue.ue_eaddr[0] = 0x2a;
616 memcpy(&sc->sc_ue.ue_eaddr[1], &ticks, sizeof(uint32_t));
617 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
621 memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
623 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
625 char c = eaddr_str[i];
627 if ('0' <= c && c <= '9')
638 sc->sc_ue.ue_eaddr[i / 2] |= c;
641 if (uaa->usb_mode == USB_MODE_DEVICE) {
643 * Do not use the same MAC address like the peer !
645 sc->sc_ue.ue_eaddr[5] ^= 0xFF;
651 ue->ue_udev = uaa->device;
652 ue->ue_mtx = &sc->sc_mtx;
653 ue->ue_methods = &cdce_ue_methods;
655 error = uether_ifattach(ue);
657 device_printf(dev, "could not attach interface\n");
660 return (0); /* success */
664 return (ENXIO); /* failure */
668 cdce_detach(device_t dev)
670 struct cdce_softc *sc = device_get_softc(dev);
671 struct usb_ether *ue = &sc->sc_ue;
673 /* stop all USB transfers first */
674 usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
676 mtx_destroy(&sc->sc_mtx);
682 cdce_start(struct usb_ether *ue)
684 struct cdce_softc *sc = uether_getsc(ue);
687 * Start the USB transfers, if not already started:
689 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
690 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
694 cdce_free_queue(struct mbuf **ppm, uint8_t n)
697 for (x = 0; x != n; x++) {
698 if (ppm[x] != NULL) {
706 cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
708 struct cdce_softc *sc = usbd_xfer_softc(xfer);
709 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
716 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
720 switch (USB_GET_STATE(xfer)) {
721 case USB_ST_TRANSFERRED:
722 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
727 /* free all previous TX buffers */
728 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
733 for (x = 0; x != CDCE_FRAMES_MAX; x++) {
735 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
740 if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
742 * Zaurus wants a 32-bit CRC appended
746 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
749 if (!m_append(m, 4, (void *)&crc)) {
755 if (m->m_len != m->m_pkthdr.len) {
756 mt = m_defrag(m, M_DONTWAIT);
764 if (m->m_pkthdr.len > MCLBYTES) {
765 m->m_pkthdr.len = MCLBYTES;
767 sc->sc_tx_buf[x] = m;
768 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
771 * If there's a BPF listener, bounce a copy of
777 usbd_xfer_set_frames(xfer, x);
779 usbd_transfer_submit(xfer);
784 DPRINTFN(11, "transfer error, %s\n",
787 /* free all previous TX buffers */
788 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
790 /* count output errors */
793 if (error != USB_ERR_CANCELLED) {
794 /* try to clear stall first */
795 usbd_xfer_set_stall(xfer);
803 cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
805 uint32_t *p_crc = arg;
807 *p_crc = crc32_raw(src, count, *p_crc);
812 cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
814 uint32_t crc = 0xFFFFFFFF;
817 error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
818 return (crc ^ 0xFFFFFFFF);
822 cdce_init(struct usb_ether *ue)
824 struct cdce_softc *sc = uether_getsc(ue);
825 struct ifnet *ifp = uether_getifp(ue);
827 CDCE_LOCK_ASSERT(sc, MA_OWNED);
829 ifp->if_drv_flags |= IFF_DRV_RUNNING;
831 /* start interrupt transfer */
832 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
833 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
835 /* stall data write direction, which depends on USB mode */
836 usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
838 /* start data transfers */
843 cdce_stop(struct usb_ether *ue)
845 struct cdce_softc *sc = uether_getsc(ue);
846 struct ifnet *ifp = uether_getifp(ue);
848 CDCE_LOCK_ASSERT(sc, MA_OWNED);
850 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
853 * stop all the transfers, if not already stopped:
855 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
856 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
857 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
858 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
862 cdce_setmulti(struct usb_ether *ue)
869 cdce_setpromisc(struct usb_ether *ue)
876 cdce_suspend(device_t dev)
878 device_printf(dev, "Suspending\n");
883 cdce_resume(device_t dev)
885 device_printf(dev, "Resuming\n");
890 cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
892 struct cdce_softc *sc = usbd_xfer_softc(xfer);
895 int actlen, aframes, len;
897 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
899 switch (USB_GET_STATE(xfer)) {
900 case USB_ST_TRANSFERRED:
902 DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
904 for (x = 0; x != aframes; x++) {
906 m = sc->sc_rx_buf[x];
907 sc->sc_rx_buf[x] = NULL;
908 len = usbd_xfer_frame_len(xfer, x);
910 /* Strip off CRC added by Zaurus, if any */
911 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
914 if (len < sizeof(struct ether_header)) {
919 uether_rxmbuf(&sc->sc_ue, m, len);
925 * TODO: Implement support for multi frame transfers,
926 * when the USB hardware supports it.
928 for (x = 0; x != 1; x++) {
929 if (sc->sc_rx_buf[x] == NULL) {
933 sc->sc_rx_buf[x] = m;
935 m = sc->sc_rx_buf[x];
938 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
940 /* set number of frames and start hardware */
941 usbd_xfer_set_frames(xfer, x);
942 usbd_transfer_submit(xfer);
943 /* flush any received frames */
944 uether_rxflush(&sc->sc_ue);
948 DPRINTF("error = %s\n",
951 if (error != USB_ERR_CANCELLED) {
953 /* try to clear stall first */
954 usbd_xfer_set_stall(xfer);
955 usbd_xfer_set_frames(xfer, 0);
956 usbd_transfer_submit(xfer);
960 /* need to free the RX-mbufs when we are cancelled */
961 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
967 cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
971 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
973 switch (USB_GET_STATE(xfer)) {
974 case USB_ST_TRANSFERRED:
976 DPRINTF("Received %d bytes\n", actlen);
978 /* TODO: decode some indications */
983 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
984 usbd_transfer_submit(xfer);
988 if (error != USB_ERR_CANCELLED) {
989 /* start clear stall */
990 usbd_xfer_set_stall(xfer);
998 cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
1002 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1004 switch (USB_GET_STATE(xfer)) {
1005 case USB_ST_TRANSFERRED:
1007 DPRINTF("Transferred %d bytes\n", actlen);
1013 usbd_xfer_set_frame_len(xfer, 0, XXX);
1014 usbd_transfer_submit(xfer);
1018 default: /* Error */
1019 if (error != USB_ERR_CANCELLED) {
1020 /* start clear stall */
1021 usbd_xfer_set_stall(xfer);
1029 cdce_handle_request(device_t dev,
1030 const void *req, void **pptr, uint16_t *plen,
1031 uint16_t offset, uint8_t *pstate)
1033 return (ENXIO); /* use builtin handler */
1038 cdce_ncm_tx_zero(struct usb_page_cache *pc,
1039 uint32_t start, uint32_t end)
1041 if (start >= CDCE_NCM_TX_MAXLEN)
1043 if (end > CDCE_NCM_TX_MAXLEN)
1044 end = CDCE_NCM_TX_MAXLEN;
1046 usbd_frame_zero(pc, start, end - start);
1050 cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
1052 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1053 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1054 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
1058 uint32_t last_offset;
1062 usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
1064 offset = sizeof(sc->sc_ncm.hdr) +
1065 sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
1067 /* Store last valid offset before alignment */
1068 last_offset = offset;
1071 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1072 offset, sc->sc_ncm.tx_modulus);
1075 cdce_ncm_tx_zero(pc, last_offset, offset);
1080 for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
1082 /* check if end of transmit buffer is reached */
1084 if (offset >= sc->sc_ncm.tx_max)
1087 /* compute maximum buffer size */
1089 rem = sc->sc_ncm.tx_max - offset;
1091 IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
1094 /* buffer not full */
1099 if (m->m_pkthdr.len > rem) {
1101 /* The frame won't fit in our buffer */
1102 DPRINTFN(1, "Frame too big to be transmitted!\n");
1108 /* Wait till next buffer becomes ready */
1109 IFQ_DRV_PREPEND(&(ifp->if_snd), m);
1112 usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
1114 USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
1115 USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
1118 offset += m->m_pkthdr.len;
1120 /* Store last valid offset before alignment */
1121 last_offset = offset;
1124 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1125 offset, sc->sc_ncm.tx_modulus);
1128 cdce_ncm_tx_zero(pc, last_offset, offset);
1131 * If there's a BPF listener, bounce a copy
1132 * of this frame to him:
1140 /* Pre-increment interface counter */
1148 rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
1150 USETW(sc->sc_ncm.dpt.wLength, rem);
1152 /* zero the rest of the data pointer entries */
1153 for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
1154 USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
1155 USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
1158 offset = last_offset;
1161 offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
1163 /* Optimise, save bandwidth and force short termination */
1164 if (offset >= sc->sc_ncm.tx_max)
1165 offset = sc->sc_ncm.tx_max;
1170 cdce_ncm_tx_zero(pc, last_offset, offset);
1172 /* set frame length */
1173 usbd_xfer_set_frame_len(xfer, index, offset);
1175 /* Fill out 16-bit header */
1176 sc->sc_ncm.hdr.dwSignature[0] = 'N';
1177 sc->sc_ncm.hdr.dwSignature[1] = 'C';
1178 sc->sc_ncm.hdr.dwSignature[2] = 'M';
1179 sc->sc_ncm.hdr.dwSignature[3] = 'H';
1180 USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
1181 USETW(sc->sc_ncm.hdr.wBlockLength, offset);
1182 USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
1183 USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
1185 sc->sc_ncm.tx_seq++;
1187 /* Fill out 16-bit frame table header */
1188 sc->sc_ncm.dpt.dwSignature[0] = 'N';
1189 sc->sc_ncm.dpt.dwSignature[1] = 'C';
1190 sc->sc_ncm.dpt.dwSignature[2] = 'M';
1191 sc->sc_ncm.dpt.dwSignature[3] = '0';
1192 USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0); /* reserved */
1194 usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
1195 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
1196 sizeof(sc->sc_ncm.dpt));
1197 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
1198 &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
1203 cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1205 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1206 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1212 switch (USB_GET_STATE(xfer)) {
1213 case USB_ST_TRANSFERRED:
1215 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
1217 DPRINTFN(10, "transfer complete: "
1218 "%u bytes in %u frames\n", actlen, aframes);
1221 for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
1222 temp = cdce_ncm_fill_tx_frames(xfer, x);
1233 usbd_xfer_set_interval(xfer, cdce_tx_interval);
1235 usbd_xfer_set_frames(xfer, x);
1236 usbd_transfer_submit(xfer);
1240 default: /* Error */
1241 DPRINTFN(10, "Transfer error: %s\n",
1242 usbd_errstr(error));
1244 /* update error counter */
1245 ifp->if_oerrors += 1;
1247 if (error != USB_ERR_CANCELLED) {
1248 /* try to clear stall first */
1249 usbd_xfer_set_stall(xfer);
1250 usbd_xfer_set_frames(xfer, 0);
1251 usbd_transfer_submit(xfer);
1258 cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1260 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1261 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
1262 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1273 switch (USB_GET_STATE(xfer)) {
1274 case USB_ST_TRANSFERRED:
1276 usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
1278 DPRINTFN(1, "received %u bytes in %u frames\n",
1281 if (actlen < (sizeof(sc->sc_ncm.hdr) +
1282 sizeof(sc->sc_ncm.dpt))) {
1283 DPRINTFN(1, "frame too short\n");
1286 usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
1287 sizeof(sc->sc_ncm.hdr));
1289 if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
1290 (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
1291 (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
1292 (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
1293 DPRINTFN(1, "invalid HDR signature: "
1294 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1295 sc->sc_ncm.hdr.dwSignature[0],
1296 sc->sc_ncm.hdr.dwSignature[1],
1297 sc->sc_ncm.hdr.dwSignature[2],
1298 sc->sc_ncm.hdr.dwSignature[3]);
1301 temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
1302 if (temp > sumlen) {
1303 DPRINTFN(1, "unsupported block length %u/%u\n",
1307 temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
1308 if ((temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
1309 DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
1312 usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
1313 sizeof(sc->sc_ncm.dpt));
1315 if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
1316 (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
1317 (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
1318 (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
1319 DPRINTFN(1, "invalid DPT signature"
1320 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1321 sc->sc_ncm.dpt.dwSignature[0],
1322 sc->sc_ncm.dpt.dwSignature[1],
1323 sc->sc_ncm.dpt.dwSignature[2],
1324 sc->sc_ncm.dpt.dwSignature[3]);
1327 nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
1329 /* Subtract size of header and last zero padded entry */
1330 if (nframes >= (2 + 1))
1335 DPRINTFN(1, "nframes = %u\n", nframes);
1337 temp += sizeof(sc->sc_ncm.dpt);
1339 if ((temp + (4 * nframes)) > actlen)
1342 if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
1343 DPRINTFN(1, "Truncating number of frames from %u to %u\n",
1344 nframes, CDCE_NCM_SUBFRAMES_MAX);
1345 nframes = CDCE_NCM_SUBFRAMES_MAX;
1347 usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
1351 for (x = 0; x != nframes; x++) {
1353 offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
1354 temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
1356 if ((offset == 0) ||
1357 (temp < sizeof(struct ether_header)) ||
1358 (temp > (MCLBYTES - ETHER_ALIGN))) {
1359 DPRINTFN(1, "NULL frame detected at %d\n", x);
1361 /* silently ignore this frame */
1363 } else if ((offset + temp) > actlen) {
1364 DPRINTFN(1, "invalid frame "
1365 "detected at %d\n", x);
1367 /* silently ignore this frame */
1369 } else if (temp > (MHLEN - ETHER_ALIGN)) {
1370 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1372 m = m_gethdr(M_DONTWAIT, MT_DATA);
1375 DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
1378 /* check if we have a buffer */
1380 m_adj(m, ETHER_ALIGN);
1382 usbd_copy_out(pc, offset, m->m_data, temp);
1385 uether_rxmbuf(&sc->sc_ue, m, temp);
1393 DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
1397 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
1398 usbd_xfer_set_frames(xfer, 1);
1399 usbd_transfer_submit(xfer);
1400 uether_rxflush(&sc->sc_ue); /* must be last */
1403 default: /* Error */
1404 DPRINTFN(1, "error = %s\n",
1405 usbd_errstr(error));
1407 if (error != USB_ERR_CANCELLED) {
1409 /* try to clear stall first */
1410 usbd_xfer_set_stall(xfer);
1411 usbd_xfer_set_frames(xfer, 0);
1412 usbd_transfer_submit(xfer);