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/linker_set.h>
60 #include <sys/module.h>
62 #include <sys/mutex.h>
63 #include <sys/condvar.h>
64 #include <sys/sysctl.h>
66 #include <sys/unistd.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include <dev/usb/usb_cdc.h>
77 #define USB_DEBUG_VAR cdce_debug
78 #include <dev/usb/usb_debug.h>
79 #include <dev/usb/usb_process.h>
82 #include <dev/usb/net/usb_ethernet.h>
83 #include <dev/usb/net/if_cdcereg.h>
85 static device_probe_t cdce_probe;
86 static device_attach_t cdce_attach;
87 static device_detach_t cdce_detach;
88 static device_suspend_t cdce_suspend;
89 static device_resume_t cdce_resume;
90 static usb_handle_request_t cdce_handle_request;
92 static usb_callback_t cdce_bulk_write_callback;
93 static usb_callback_t cdce_bulk_read_callback;
94 static usb_callback_t cdce_intr_read_callback;
95 static usb_callback_t cdce_intr_write_callback;
98 static usb_callback_t cdce_ncm_bulk_write_callback;
99 static usb_callback_t cdce_ncm_bulk_read_callback;
102 static uether_fn_t cdce_attach_post;
103 static uether_fn_t cdce_init;
104 static uether_fn_t cdce_stop;
105 static uether_fn_t cdce_start;
106 static uether_fn_t cdce_setmulti;
107 static uether_fn_t cdce_setpromisc;
109 static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
112 static int cdce_debug = 0;
114 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,
119 static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
123 .endpoint = UE_ADDR_ANY,
124 .direction = UE_DIR_RX,
126 .frames = CDCE_FRAMES_MAX,
127 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
128 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
129 .callback = cdce_bulk_read_callback,
130 .timeout = 0, /* no timeout */
131 .usb_mode = USB_MODE_DUAL, /* both modes */
136 .endpoint = UE_ADDR_ANY,
137 .direction = UE_DIR_TX,
139 .frames = CDCE_FRAMES_MAX,
140 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
141 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
142 .callback = cdce_bulk_write_callback,
143 .timeout = 10000, /* 10 seconds */
144 .usb_mode = USB_MODE_DUAL, /* both modes */
148 .type = UE_INTERRUPT,
149 .endpoint = UE_ADDR_ANY,
150 .direction = UE_DIR_RX,
152 .bufsize = CDCE_IND_SIZE_MAX,
153 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
154 .callback = cdce_intr_read_callback,
156 .usb_mode = USB_MODE_HOST,
160 .type = UE_INTERRUPT,
161 .endpoint = UE_ADDR_ANY,
162 .direction = UE_DIR_TX,
164 .bufsize = CDCE_IND_SIZE_MAX,
165 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
166 .callback = cdce_intr_write_callback,
167 .timeout = 10000, /* 10 seconds */
168 .usb_mode = USB_MODE_DEVICE,
173 static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
177 .endpoint = UE_ADDR_ANY,
178 .direction = UE_DIR_RX,
180 .frames = CDCE_NCM_RX_FRAMES_MAX,
181 .bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
182 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
183 .callback = cdce_ncm_bulk_read_callback,
184 .timeout = 0, /* no timeout */
185 .usb_mode = USB_MODE_DUAL, /* both modes */
190 .endpoint = UE_ADDR_ANY,
191 .direction = UE_DIR_TX,
193 .frames = CDCE_NCM_TX_FRAMES_MAX,
194 .bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
195 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
196 .callback = cdce_ncm_bulk_write_callback,
197 .timeout = 10000, /* 10 seconds */
198 .usb_mode = USB_MODE_DUAL, /* both modes */
202 .type = UE_INTERRUPT,
203 .endpoint = UE_ADDR_ANY,
204 .direction = UE_DIR_RX,
206 .bufsize = CDCE_IND_SIZE_MAX,
207 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
208 .callback = cdce_intr_read_callback,
210 .usb_mode = USB_MODE_HOST,
214 .type = UE_INTERRUPT,
215 .endpoint = UE_ADDR_ANY,
216 .direction = UE_DIR_TX,
218 .bufsize = CDCE_IND_SIZE_MAX,
219 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
220 .callback = cdce_intr_write_callback,
221 .timeout = 10000, /* 10 seconds */
222 .usb_mode = USB_MODE_DEVICE,
227 static device_method_t cdce_methods[] = {
229 DEVMETHOD(usb_handle_request, cdce_handle_request),
231 /* Device interface */
232 DEVMETHOD(device_probe, cdce_probe),
233 DEVMETHOD(device_attach, cdce_attach),
234 DEVMETHOD(device_detach, cdce_detach),
235 DEVMETHOD(device_suspend, cdce_suspend),
236 DEVMETHOD(device_resume, cdce_resume),
241 static driver_t cdce_driver = {
243 .methods = cdce_methods,
244 .size = sizeof(struct cdce_softc),
247 static devclass_t cdce_devclass;
249 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0);
250 MODULE_VERSION(cdce, 1);
251 MODULE_DEPEND(cdce, uether, 1, 1, 1);
252 MODULE_DEPEND(cdce, usb, 1, 1, 1);
253 MODULE_DEPEND(cdce, ether, 1, 1, 1);
255 static const struct usb_ether_methods cdce_ue_methods = {
256 .ue_attach_post = cdce_attach_post,
257 .ue_start = cdce_start,
258 .ue_init = cdce_init,
259 .ue_stop = cdce_stop,
260 .ue_setmulti = cdce_setmulti,
261 .ue_setpromisc = cdce_setpromisc,
264 static const struct usb_device_id cdce_devs[] = {
265 {USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
266 {USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
267 {USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
268 {USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
269 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
270 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
271 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
272 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
273 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
274 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
275 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
276 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
277 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
279 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
280 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
281 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
285 /*------------------------------------------------------------------------*
291 *------------------------------------------------------------------------*/
293 cdce_ncm_init(struct cdce_softc *sc)
295 struct usb_ncm_parameters temp;
296 struct usb_device_request req;
300 req.bmRequestType = UT_READ_CLASS_INTERFACE;
301 req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
302 USETW(req.wValue, 0);
303 req.wIndex[0] = sc->sc_ifaces_index[1];
305 USETW(req.wLength, sizeof(temp));
307 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
308 &temp, 0, NULL, 1000 /* ms */);
312 /* Read correct set of parameters according to device mode */
314 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
315 sc->sc_ncm.rx_max = UGETW(temp.dwNtbInMaxSize);
316 sc->sc_ncm.tx_max = UGETW(temp.dwNtbOutMaxSize);
317 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
318 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
319 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
321 sc->sc_ncm.rx_max = UGETW(temp.dwNtbOutMaxSize);
322 sc->sc_ncm.tx_max = UGETW(temp.dwNtbInMaxSize);
323 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
324 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
325 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
328 /* Verify maximum receive length */
330 if (err || (sc->sc_ncm.rx_max < 32) ||
331 (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
332 DPRINTFN(1, "Using default maximum receive length\n");
333 sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
336 /* Verify maximum transmit length */
338 if (err || (sc->sc_ncm.tx_max < 32) ||
339 (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
340 DPRINTFN(1, "Using default maximum transmit length\n");
341 sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
345 * Verify that the structure alignment is:
347 * - not greater than the maximum transmit length
348 * - not less than four bytes
350 if (err || (sc->sc_ncm.tx_struct_align < 4) ||
351 (sc->sc_ncm.tx_struct_align !=
352 ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
353 (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
354 DPRINTFN(1, "Using default other alignment: 4 bytes\n");
355 sc->sc_ncm.tx_struct_align = 4;
359 * Verify that the payload alignment is:
361 * - not greater than the maximum transmit length
362 * - not less than four bytes
364 if (err || (sc->sc_ncm.tx_modulus < 4) ||
365 (sc->sc_ncm.tx_modulus !=
366 ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
367 (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
368 DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
369 sc->sc_ncm.tx_modulus = 4;
372 /* Verify that the payload remainder */
374 if (err || (sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
375 DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
376 sc->sc_ncm.tx_remainder = 0;
379 /* Additional configuration, will fail in device side mode, which is OK. */
381 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
382 req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
383 USETW(req.wValue, 0);
384 req.wIndex[0] = sc->sc_ifaces_index[1];
386 USETW(req.wLength, 4);
387 USETDW(value, sc->sc_ncm.rx_max);
389 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
390 &value, 0, NULL, 1000 /* ms */);
392 DPRINTFN(1, "Setting input size "
393 "to %u failed.\n", sc->sc_ncm.rx_max);
396 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
397 req.bRequest = UCDC_NCM_SET_CRC_MODE;
398 USETW(req.wValue, 0); /* no CRC */
399 req.wIndex[0] = sc->sc_ifaces_index[1];
401 USETW(req.wLength, 0);
403 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
404 NULL, 0, NULL, 1000 /* ms */);
406 DPRINTFN(1, "Setting CRC mode to off failed.\n");
409 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
410 req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
411 USETW(req.wValue, 0); /* NTB-16 */
412 req.wIndex[0] = sc->sc_ifaces_index[1];
414 USETW(req.wLength, 0);
416 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
417 NULL, 0, NULL, 1000 /* ms */);
419 DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
422 return (0); /* success */
427 cdce_probe(device_t dev)
429 struct usb_attach_arg *uaa = device_get_ivars(dev);
431 return (usbd_lookup_id_by_uaa(cdce_devs, sizeof(cdce_devs), uaa));
435 cdce_attach_post(struct usb_ether *ue)
442 cdce_attach(device_t dev)
444 struct cdce_softc *sc = device_get_softc(dev);
445 struct usb_ether *ue = &sc->sc_ue;
446 struct usb_attach_arg *uaa = device_get_ivars(dev);
447 struct usb_interface *iface;
448 const struct usb_cdc_union_descriptor *ud;
449 const struct usb_interface_descriptor *id;
450 const struct usb_cdc_ethernet_descriptor *ued;
451 const struct usb_config *pcfg;
454 uint8_t data_iface_no;
455 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */
457 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
458 sc->sc_ue.ue_udev = uaa->device;
460 device_set_usb_desc(dev);
462 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
464 ud = usbd_find_descriptor
465 (uaa->device, NULL, uaa->info.bIfaceIndex,
466 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
468 if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
469 (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
470 DPRINTFN(1, "No union descriptor!\n");
471 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
472 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
473 goto alloc_transfers;
475 data_iface_no = ud->bSlaveInterface[0];
479 iface = usbd_get_iface(uaa->device, i);
483 id = usbd_get_interface_descriptor(iface);
485 if (id && (id->bInterfaceNumber == data_iface_no)) {
486 sc->sc_ifaces_index[0] = i;
487 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
488 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
492 device_printf(dev, "no data interface found!\n");
500 * The Data Class interface of a networking device shall have
501 * a minimum of two interface settings. The first setting
502 * (the default interface setting) includes no endpoints and
503 * therefore no networking traffic is exchanged whenever the
504 * default interface setting is selected. One or more
505 * additional interface settings are used for normal
506 * operation, and therefore each includes a pair of endpoints
507 * (one IN, and one OUT) to exchange network traffic. Select
508 * an alternate interface setting to initialize the network
509 * aspects of the device and to enable the exchange of
514 * Some devices, most notably cable modems, include interface
515 * settings that have no IN or OUT endpoint, therefore loop
516 * through the list of all available interface settings
517 * looking for one with both IN and OUT endpoints.
522 pcfg = cdce_config; /* Default Configuration */
524 for (i = 0; i != 32; i++) {
526 error = usbd_set_alt_interface_index(uaa->device,
527 sc->sc_ifaces_index[0], i);
531 if ((i == 0) && (cdce_ncm_init(sc) == 0))
532 pcfg = cdce_ncm_config;
534 error = usbd_transfer_setup(uaa->device,
535 sc->sc_ifaces_index, sc->sc_xfer,
536 pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
542 if (error || (i == 32)) {
543 device_printf(dev, "No valid alternate "
548 ued = usbd_find_descriptor
549 (uaa->device, NULL, uaa->info.bIfaceIndex,
550 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1);
552 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
553 error = USB_ERR_INVAL;
555 error = usbd_req_get_string_any(uaa->device, NULL,
556 eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
561 /* fake MAC address */
563 device_printf(dev, "faking MAC address\n");
564 sc->sc_ue.ue_eaddr[0] = 0x2a;
565 memcpy(&sc->sc_ue.ue_eaddr[1], &ticks, sizeof(uint32_t));
566 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
570 bzero(sc->sc_ue.ue_eaddr, sizeof(sc->sc_ue.ue_eaddr));
572 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
574 char c = eaddr_str[i];
576 if ('0' <= c && c <= '9')
587 sc->sc_ue.ue_eaddr[i / 2] |= c;
590 if (uaa->usb_mode == USB_MODE_DEVICE) {
592 * Do not use the same MAC address like the peer !
594 sc->sc_ue.ue_eaddr[5] ^= 0xFF;
600 ue->ue_udev = uaa->device;
601 ue->ue_mtx = &sc->sc_mtx;
602 ue->ue_methods = &cdce_ue_methods;
604 error = uether_ifattach(ue);
606 device_printf(dev, "could not attach interface\n");
609 return (0); /* success */
613 return (ENXIO); /* failure */
617 cdce_detach(device_t dev)
619 struct cdce_softc *sc = device_get_softc(dev);
620 struct usb_ether *ue = &sc->sc_ue;
622 /* stop all USB transfers first */
623 usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
625 mtx_destroy(&sc->sc_mtx);
631 cdce_start(struct usb_ether *ue)
633 struct cdce_softc *sc = uether_getsc(ue);
636 * Start the USB transfers, if not already started:
638 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
639 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
643 cdce_free_queue(struct mbuf **ppm, uint8_t n)
646 for (x = 0; x != n; x++) {
647 if (ppm[x] != NULL) {
655 cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
657 struct cdce_softc *sc = usbd_xfer_softc(xfer);
658 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
665 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
669 switch (USB_GET_STATE(xfer)) {
670 case USB_ST_TRANSFERRED:
671 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
676 /* free all previous TX buffers */
677 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
682 for (x = 0; x != CDCE_FRAMES_MAX; x++) {
684 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
689 if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
691 * Zaurus wants a 32-bit CRC appended
695 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
698 if (!m_append(m, 4, (void *)&crc)) {
704 if (m->m_len != m->m_pkthdr.len) {
705 mt = m_defrag(m, M_DONTWAIT);
713 if (m->m_pkthdr.len > MCLBYTES) {
714 m->m_pkthdr.len = MCLBYTES;
716 sc->sc_tx_buf[x] = m;
717 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
720 * If there's a BPF listener, bounce a copy of
726 usbd_xfer_set_frames(xfer, x);
728 usbd_transfer_submit(xfer);
733 DPRINTFN(11, "transfer error, %s\n",
736 /* free all previous TX buffers */
737 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
739 /* count output errors */
742 if (error != USB_ERR_CANCELLED) {
743 /* try to clear stall first */
744 usbd_xfer_set_stall(xfer);
752 cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
754 uint32_t *p_crc = arg;
756 *p_crc = crc32_raw(src, count, *p_crc);
761 cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
763 uint32_t crc = 0xFFFFFFFF;
766 error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
767 return (crc ^ 0xFFFFFFFF);
771 cdce_init(struct usb_ether *ue)
773 struct cdce_softc *sc = uether_getsc(ue);
774 struct ifnet *ifp = uether_getifp(ue);
776 CDCE_LOCK_ASSERT(sc, MA_OWNED);
778 ifp->if_drv_flags |= IFF_DRV_RUNNING;
780 /* start interrupt transfer */
781 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
782 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
784 /* stall data write direction, which depends on USB mode */
785 usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
787 /* start data transfers */
792 cdce_stop(struct usb_ether *ue)
794 struct cdce_softc *sc = uether_getsc(ue);
795 struct ifnet *ifp = uether_getifp(ue);
797 CDCE_LOCK_ASSERT(sc, MA_OWNED);
799 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
802 * stop all the transfers, if not already stopped:
804 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
805 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
806 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
807 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
811 cdce_setmulti(struct usb_ether *ue)
818 cdce_setpromisc(struct usb_ether *ue)
825 cdce_suspend(device_t dev)
827 device_printf(dev, "Suspending\n");
832 cdce_resume(device_t dev)
834 device_printf(dev, "Resuming\n");
839 cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
841 struct cdce_softc *sc = usbd_xfer_softc(xfer);
844 int actlen, aframes, len;
846 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
848 switch (USB_GET_STATE(xfer)) {
849 case USB_ST_TRANSFERRED:
851 DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
853 for (x = 0; x != aframes; x++) {
855 m = sc->sc_rx_buf[x];
856 sc->sc_rx_buf[x] = NULL;
857 len = usbd_xfer_frame_len(xfer, x);
859 /* Strip off CRC added by Zaurus, if any */
860 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
863 if (len < sizeof(struct ether_header)) {
868 uether_rxmbuf(&sc->sc_ue, m, len);
874 * TODO: Implement support for multi frame transfers,
875 * when the USB hardware supports it.
877 for (x = 0; x != 1; x++) {
878 if (sc->sc_rx_buf[x] == NULL) {
882 sc->sc_rx_buf[x] = m;
884 m = sc->sc_rx_buf[x];
887 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
889 /* set number of frames and start hardware */
890 usbd_xfer_set_frames(xfer, x);
891 usbd_transfer_submit(xfer);
892 /* flush any received frames */
893 uether_rxflush(&sc->sc_ue);
897 DPRINTF("error = %s\n",
900 if (error != USB_ERR_CANCELLED) {
902 /* try to clear stall first */
903 usbd_xfer_set_stall(xfer);
904 usbd_xfer_set_frames(xfer, 0);
905 usbd_transfer_submit(xfer);
909 /* need to free the RX-mbufs when we are cancelled */
910 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
916 cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
920 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
922 switch (USB_GET_STATE(xfer)) {
923 case USB_ST_TRANSFERRED:
925 DPRINTF("Received %d bytes\n", actlen);
927 /* TODO: decode some indications */
932 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
933 usbd_transfer_submit(xfer);
937 if (error != USB_ERR_CANCELLED) {
938 /* start clear stall */
939 usbd_xfer_set_stall(xfer);
947 cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
951 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
953 switch (USB_GET_STATE(xfer)) {
954 case USB_ST_TRANSFERRED:
956 DPRINTF("Transferred %d bytes\n", actlen);
962 usbd_xfer_set_frame_len(xfer, 0, XXX);
963 usbd_transfer_submit(xfer);
968 if (error != USB_ERR_CANCELLED) {
969 /* start clear stall */
970 usbd_xfer_set_stall(xfer);
978 cdce_handle_request(device_t dev,
979 const void *req, void **pptr, uint16_t *plen,
980 uint16_t offset, uint8_t *pstate)
982 return (ENXIO); /* use builtin handler */
987 cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
989 struct cdce_softc *sc = usbd_xfer_softc(xfer);
990 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
991 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
995 uint32_t last_offset;
998 usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
1000 offset = sizeof(sc->sc_ncm.hdr) +
1001 sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
1003 /* Store last valid offset before alignment */
1004 last_offset = offset;
1006 /* Align offset correctly */
1007 offset = sc->sc_ncm.tx_remainder -
1008 ((0UL - offset) & (0UL - sc->sc_ncm.tx_modulus));
1010 for (n = 0; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
1012 /* check if end of transmit buffer is reached */
1014 if (offset >= sc->sc_ncm.tx_max)
1017 /* compute maximum buffer size */
1019 rem = sc->sc_ncm.tx_max - offset;
1021 IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
1026 if (m->m_pkthdr.len > rem) {
1028 /* The frame won't fit in our buffer */
1029 DPRINTFN(1, "Frame too big to be transmitted!\n");
1035 /* Wait till next buffer becomes ready */
1036 IFQ_DRV_PREPEND(&(ifp->if_snd), m);
1039 usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
1041 USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
1042 USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
1045 offset += m->m_pkthdr.len;
1047 /* Store last valid offset before alignment */
1048 last_offset = offset;
1050 /* Align offset correctly */
1051 offset = sc->sc_ncm.tx_remainder -
1052 ((0UL - offset) & (0UL - sc->sc_ncm.tx_modulus));
1055 * If there's a BPF listener, bounce a copy
1056 * of this frame to him:
1064 /* Pre-increment interface counter */
1072 rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
1074 USETW(sc->sc_ncm.dpt.wLength, rem);
1076 /* zero the rest of the data pointer entries */
1077 for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
1078 USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
1079 USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
1082 /* set frame length */
1083 usbd_xfer_set_frame_len(xfer, index, last_offset);
1085 /* Fill out 16-bit header */
1086 sc->sc_ncm.hdr.dwSignature[0] = 'N';
1087 sc->sc_ncm.hdr.dwSignature[1] = 'C';
1088 sc->sc_ncm.hdr.dwSignature[2] = 'M';
1089 sc->sc_ncm.hdr.dwSignature[3] = 'H';
1090 USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
1091 USETW(sc->sc_ncm.hdr.wBlockLength, last_offset);
1092 USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
1093 USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
1095 sc->sc_ncm.tx_seq++;
1097 /* Fill out 16-bit frame table header */
1098 sc->sc_ncm.dpt.dwSignature[0] = 'N';
1099 sc->sc_ncm.dpt.dwSignature[1] = 'C';
1100 sc->sc_ncm.dpt.dwSignature[2] = 'M';
1101 sc->sc_ncm.dpt.dwSignature[3] = 'x';
1102 USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0); /* reserved */
1104 usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
1105 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
1106 sizeof(sc->sc_ncm.dpt));
1107 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
1108 &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
1113 cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1115 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1116 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1121 switch (USB_GET_STATE(xfer)) {
1122 case USB_ST_TRANSFERRED:
1124 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
1126 DPRINTFN(10, "transfer complete: "
1127 "%u bytes in %u frames\n", actlen, aframes);
1130 for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
1131 if (cdce_ncm_fill_tx_frames(xfer, x))
1136 usbd_xfer_set_frames(xfer, x);
1137 usbd_transfer_submit(xfer);
1141 default: /* Error */
1142 DPRINTFN(10, "Transfer error: %s\n",
1143 usbd_errstr(error));
1145 /* update error counter */
1146 ifp->if_oerrors += 1;
1148 if (error != USB_ERR_CANCELLED) {
1149 /* try to clear stall first */
1150 usbd_xfer_set_stall(xfer);
1151 usbd_xfer_set_frames(xfer, 0);
1152 usbd_transfer_submit(xfer);
1159 cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1161 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1162 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
1163 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1174 switch (USB_GET_STATE(xfer)) {
1175 case USB_ST_TRANSFERRED:
1177 usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
1179 DPRINTFN(1, "received %u bytes in %u frames\n",
1182 if (actlen < (sizeof(sc->sc_ncm.hdr) +
1183 sizeof(sc->sc_ncm.dpt))) {
1184 DPRINTFN(1, "frame too short\n");
1187 usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
1188 sizeof(sc->sc_ncm.hdr));
1190 if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
1191 (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
1192 (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
1193 (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
1194 DPRINTFN(1, "invalid HDR signature\n");
1197 temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
1198 if (temp > sumlen) {
1199 DPRINTFN(1, "unsupported block length %u/%u\n",
1203 temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
1204 if ((temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
1205 DPRINTFN(1, "invalid DPT index\n");
1208 usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
1209 sizeof(sc->sc_ncm.dpt));
1211 if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
1212 (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
1213 (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
1214 (sc->sc_ncm.dpt.dwSignature[3] != 'x')) {
1215 DPRINTFN(1, "invalid DPT signature\n");
1218 nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
1220 /* Subtract size of header and last zero padded entry */
1221 if (nframes >= (2 + 1))
1226 DPRINTFN(1, "nframes = %u\n", nframes);
1228 temp += sizeof(sc->sc_ncm.dpt);
1230 if ((temp + (4 * nframes)) > actlen)
1233 if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
1234 DPRINTFN(1, "Truncating number of frames from %u to %u\n",
1235 nframes, CDCE_NCM_SUBFRAMES_MAX);
1236 nframes = CDCE_NCM_SUBFRAMES_MAX;
1238 usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
1242 for (x = 0; x != nframes; x++) {
1244 offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
1245 temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
1247 if ((offset == 0) ||
1248 (temp < sizeof(struct ether_header)) ||
1249 (temp > (MCLBYTES - ETHER_ALIGN))) {
1250 DPRINTFN(1, "NULL frame detected at %d\n", x);
1252 /* silently ignore this frame */
1254 } else if ((offset + temp) > actlen) {
1255 DPRINTFN(1, "invalid frame "
1256 "detected at %d\n", x);
1258 /* silently ignore this frame */
1260 } else if (temp > (MHLEN - ETHER_ALIGN)) {
1261 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1263 m = m_gethdr(M_DONTWAIT, MT_DATA);
1266 DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
1269 /* check if we have a buffer */
1271 m_adj(m, ETHER_ALIGN);
1273 usbd_copy_out(pc, offset, m->m_data, temp);
1276 uether_rxmbuf(&sc->sc_ue, m, temp);
1284 DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
1287 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
1288 usbd_xfer_set_frames(xfer, 1);
1289 usbd_transfer_submit(xfer);
1290 uether_rxflush(&sc->sc_ue); /* must be last */
1293 default: /* Error */
1294 DPRINTFN(1, "error = %s\n",
1295 usbd_errstr(error));
1297 if (error != USB_ERR_CANCELLED) {
1299 /* try to clear stall first */
1300 usbd_xfer_set_stall(xfer);
1301 usbd_xfer_set_frames(xfer, 0);
1302 usbd_transfer_submit(xfer);