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/socket.h>
58 #include <sys/kernel.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>
72 #include <net/if_var.h>
74 #include <dev/usb/usb.h>
75 #include <dev/usb/usbdi.h>
76 #include <dev/usb/usbdi_util.h>
77 #include <dev/usb/usb_cdc.h>
80 #define USB_DEBUG_VAR cdce_debug
81 #include <dev/usb/usb_debug.h>
82 #include <dev/usb/usb_process.h>
83 #include <dev/usb/usb_msctest.h>
86 #include <dev/usb/net/usb_ethernet.h>
87 #include <dev/usb/net/if_cdcereg.h>
89 static device_probe_t cdce_probe;
90 static device_attach_t cdce_attach;
91 static device_detach_t cdce_detach;
92 static device_suspend_t cdce_suspend;
93 static device_resume_t cdce_resume;
94 static usb_handle_request_t cdce_handle_request;
96 static usb_callback_t cdce_bulk_write_callback;
97 static usb_callback_t cdce_bulk_read_callback;
98 static usb_callback_t cdce_intr_read_callback;
99 static usb_callback_t cdce_intr_write_callback;
102 static usb_callback_t cdce_ncm_bulk_write_callback;
103 static usb_callback_t cdce_ncm_bulk_read_callback;
106 static uether_fn_t cdce_attach_post;
107 static uether_fn_t cdce_init;
108 static uether_fn_t cdce_stop;
109 static uether_fn_t cdce_start;
110 static uether_fn_t cdce_setmulti;
111 static uether_fn_t cdce_setpromisc;
113 static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
116 static int cdce_debug = 0;
117 static int cdce_tx_interval = 0;
119 static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
120 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RWTUN, &cdce_debug, 0,
122 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RWTUN, &cdce_tx_interval, 0,
123 "NCM transmit interval in ms");
126 static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
130 .endpoint = UE_ADDR_ANY,
131 .direction = UE_DIR_RX,
133 .frames = CDCE_FRAMES_MAX,
134 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
135 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
136 .callback = cdce_bulk_read_callback,
137 .timeout = 0, /* no timeout */
138 .usb_mode = USB_MODE_DUAL, /* both modes */
143 .endpoint = UE_ADDR_ANY,
144 .direction = UE_DIR_TX,
146 .frames = CDCE_FRAMES_MAX,
147 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
148 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
149 .callback = cdce_bulk_write_callback,
150 .timeout = 10000, /* 10 seconds */
151 .usb_mode = USB_MODE_DUAL, /* both modes */
155 .type = UE_INTERRUPT,
156 .endpoint = UE_ADDR_ANY,
157 .direction = UE_DIR_RX,
159 .bufsize = CDCE_IND_SIZE_MAX,
160 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
161 .callback = cdce_intr_read_callback,
163 .usb_mode = USB_MODE_HOST,
167 .type = UE_INTERRUPT,
168 .endpoint = UE_ADDR_ANY,
169 .direction = UE_DIR_TX,
171 .bufsize = CDCE_IND_SIZE_MAX,
172 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
173 .callback = cdce_intr_write_callback,
174 .timeout = 10000, /* 10 seconds */
175 .usb_mode = USB_MODE_DEVICE,
180 static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
184 .endpoint = UE_ADDR_ANY,
185 .direction = UE_DIR_RX,
187 .frames = CDCE_NCM_RX_FRAMES_MAX,
188 .bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
189 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
190 .callback = cdce_ncm_bulk_read_callback,
191 .timeout = 0, /* no timeout */
192 .usb_mode = USB_MODE_DUAL, /* both modes */
197 .endpoint = UE_ADDR_ANY,
198 .direction = UE_DIR_TX,
200 .frames = CDCE_NCM_TX_FRAMES_MAX,
201 .bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
202 .flags = {.pipe_bof = 1,},
203 .callback = cdce_ncm_bulk_write_callback,
204 .timeout = 10000, /* 10 seconds */
205 .usb_mode = USB_MODE_DUAL, /* both modes */
209 .type = UE_INTERRUPT,
210 .endpoint = UE_ADDR_ANY,
211 .direction = UE_DIR_RX,
213 .bufsize = CDCE_IND_SIZE_MAX,
214 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
215 .callback = cdce_intr_read_callback,
217 .usb_mode = USB_MODE_HOST,
221 .type = UE_INTERRUPT,
222 .endpoint = UE_ADDR_ANY,
223 .direction = UE_DIR_TX,
225 .bufsize = CDCE_IND_SIZE_MAX,
226 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
227 .callback = cdce_intr_write_callback,
228 .timeout = 10000, /* 10 seconds */
229 .usb_mode = USB_MODE_DEVICE,
234 static device_method_t cdce_methods[] = {
236 DEVMETHOD(usb_handle_request, cdce_handle_request),
238 /* Device interface */
239 DEVMETHOD(device_probe, cdce_probe),
240 DEVMETHOD(device_attach, cdce_attach),
241 DEVMETHOD(device_detach, cdce_detach),
242 DEVMETHOD(device_suspend, cdce_suspend),
243 DEVMETHOD(device_resume, cdce_resume),
248 static driver_t cdce_driver = {
250 .methods = cdce_methods,
251 .size = sizeof(struct cdce_softc),
254 static devclass_t cdce_devclass;
255 static eventhandler_tag cdce_etag;
257 static int cdce_driver_loaded(struct module *, int, void *);
259 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, cdce_driver_loaded, 0);
260 MODULE_VERSION(cdce, 1);
261 MODULE_DEPEND(cdce, uether, 1, 1, 1);
262 MODULE_DEPEND(cdce, usb, 1, 1, 1);
263 MODULE_DEPEND(cdce, ether, 1, 1, 1);
265 static const struct usb_ether_methods cdce_ue_methods = {
266 .ue_attach_post = cdce_attach_post,
267 .ue_start = cdce_start,
268 .ue_init = cdce_init,
269 .ue_stop = cdce_stop,
270 .ue_setmulti = cdce_setmulti,
271 .ue_setpromisc = cdce_setpromisc,
274 static const STRUCT_USB_HOST_ID cdce_switch_devs[] = {
275 {USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3272_INIT, MSC_EJECT_HUAWEI2)},
278 static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
279 {USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
280 {USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
281 {USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
282 {USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
283 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
284 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
285 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
286 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
287 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
288 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
289 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
290 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
291 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
293 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
294 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x16),
296 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
297 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x46),
299 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
300 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x76),
304 static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
305 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
306 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
307 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
311 /*------------------------------------------------------------------------*
317 *------------------------------------------------------------------------*/
319 cdce_ncm_init(struct cdce_softc *sc)
321 struct usb_ncm_parameters temp;
322 struct usb_device_request req;
323 struct usb_ncm_func_descriptor *ufd;
327 ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
328 sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF,
329 UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF);
331 /* verify length of NCM functional descriptor */
333 if (ufd->bLength < sizeof(*ufd))
336 DPRINTFN(1, "Found NCM functional descriptor.\n");
339 req.bmRequestType = UT_READ_CLASS_INTERFACE;
340 req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
341 USETW(req.wValue, 0);
342 req.wIndex[0] = sc->sc_ifaces_index[1];
344 USETW(req.wLength, sizeof(temp));
346 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
347 &temp, 0, NULL, 1000 /* ms */);
351 /* Read correct set of parameters according to device mode */
353 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
354 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
355 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
356 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
357 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
358 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
359 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
361 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
362 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
363 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
364 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
365 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
366 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
369 /* Verify maximum receive length */
371 if ((sc->sc_ncm.rx_max < 32) ||
372 (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
373 DPRINTFN(1, "Using default maximum receive length\n");
374 sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
377 /* Verify maximum transmit length */
379 if ((sc->sc_ncm.tx_max < 32) ||
380 (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
381 DPRINTFN(1, "Using default maximum transmit length\n");
382 sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
386 * Verify that the structure alignment is:
388 * - not greater than the maximum transmit length
389 * - not less than four bytes
391 if ((sc->sc_ncm.tx_struct_align < 4) ||
392 (sc->sc_ncm.tx_struct_align !=
393 ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
394 (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
395 DPRINTFN(1, "Using default other alignment: 4 bytes\n");
396 sc->sc_ncm.tx_struct_align = 4;
400 * Verify that the payload alignment is:
402 * - not greater than the maximum transmit length
403 * - not less than four bytes
405 if ((sc->sc_ncm.tx_modulus < 4) ||
406 (sc->sc_ncm.tx_modulus !=
407 ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
408 (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
409 DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
410 sc->sc_ncm.tx_modulus = 4;
413 /* Verify that the payload remainder */
415 if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
416 DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
417 sc->sc_ncm.tx_remainder = 0;
421 * Offset the TX remainder so that IP packet payload starts at
422 * the tx_modulus. This is not too clear in the specification.
425 sc->sc_ncm.tx_remainder =
426 (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
427 (sc->sc_ncm.tx_modulus - 1);
429 /* Verify max datagrams */
431 if (sc->sc_ncm.tx_nframe == 0 ||
432 sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
433 DPRINTFN(1, "Using default max "
434 "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
435 /* need to reserve one entry for zero padding */
436 sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
439 /* Additional configuration, will fail in device side mode, which is OK. */
441 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
442 req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
443 USETW(req.wValue, 0);
444 req.wIndex[0] = sc->sc_ifaces_index[1];
448 (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
449 USETW(req.wLength, 8);
450 USETDW(value, sc->sc_ncm.rx_max);
451 USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
454 USETW(req.wLength, 4);
455 USETDW(value, sc->sc_ncm.rx_max);
458 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
459 &value, 0, NULL, 1000 /* ms */);
461 DPRINTFN(1, "Setting input size "
462 "to %u failed.\n", sc->sc_ncm.rx_max);
465 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
466 req.bRequest = UCDC_NCM_SET_CRC_MODE;
467 USETW(req.wValue, 0); /* no CRC */
468 req.wIndex[0] = sc->sc_ifaces_index[1];
470 USETW(req.wLength, 0);
472 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
473 NULL, 0, NULL, 1000 /* ms */);
475 DPRINTFN(1, "Setting CRC mode to off failed.\n");
478 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
479 req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
480 USETW(req.wValue, 0); /* NTB-16 */
481 req.wIndex[0] = sc->sc_ifaces_index[1];
483 USETW(req.wLength, 0);
485 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
486 NULL, 0, NULL, 1000 /* ms */);
488 DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
491 return (0); /* success */
496 cdce_test_autoinst(void *arg, struct usb_device *udev,
497 struct usb_attach_arg *uaa)
499 struct usb_interface *iface;
500 struct usb_interface_descriptor *id;
502 if (uaa->dev_state != UAA_DEV_READY)
505 iface = usbd_get_iface(udev, 0);
509 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
511 if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
512 return; /* no device match */
514 if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
515 /* success, mark the udev as disappearing */
516 uaa->dev_state = UAA_DEV_EJECTING;
521 cdce_driver_loaded(struct module *mod, int what, void *arg)
525 /* register our autoinstall handler */
526 cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
527 cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
530 EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
538 cdce_probe(device_t dev)
540 struct usb_attach_arg *uaa = device_get_ivars(dev);
543 error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
545 error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
550 cdce_attach_post(struct usb_ether *ue)
557 cdce_attach(device_t dev)
559 struct cdce_softc *sc = device_get_softc(dev);
560 struct usb_ether *ue = &sc->sc_ue;
561 struct usb_attach_arg *uaa = device_get_ivars(dev);
562 struct usb_interface *iface;
563 const struct usb_cdc_union_descriptor *ud;
564 const struct usb_interface_descriptor *id;
565 const struct usb_cdc_ethernet_descriptor *ued;
566 const struct usb_config *pcfg;
570 uint8_t data_iface_no;
571 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */
573 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
574 sc->sc_ue.ue_udev = uaa->device;
576 device_set_usb_desc(dev);
578 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
580 ud = usbd_find_descriptor
581 (uaa->device, NULL, uaa->info.bIfaceIndex,
582 UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF);
584 if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
585 (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
586 DPRINTFN(1, "No union descriptor!\n");
587 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
588 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
589 goto alloc_transfers;
591 data_iface_no = ud->bSlaveInterface[0];
595 iface = usbd_get_iface(uaa->device, i);
599 id = usbd_get_interface_descriptor(iface);
601 if (id && (id->bInterfaceNumber == data_iface_no)) {
602 sc->sc_ifaces_index[0] = i;
603 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
604 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
608 device_printf(dev, "no data interface found\n");
616 * The Data Class interface of a networking device shall have
617 * a minimum of two interface settings. The first setting
618 * (the default interface setting) includes no endpoints and
619 * therefore no networking traffic is exchanged whenever the
620 * default interface setting is selected. One or more
621 * additional interface settings are used for normal
622 * operation, and therefore each includes a pair of endpoints
623 * (one IN, and one OUT) to exchange network traffic. Select
624 * an alternate interface setting to initialize the network
625 * aspects of the device and to enable the exchange of
630 * Some devices, most notably cable modems, include interface
631 * settings that have no IN or OUT endpoint, therefore loop
632 * through the list of all available interface settings
633 * looking for one with both IN and OUT endpoints.
638 pcfg = cdce_config; /* Default Configuration */
640 for (i = 0; i != 32; i++) {
642 error = usbd_set_alt_interface_index(uaa->device,
643 sc->sc_ifaces_index[0], i);
647 if ((i == 0) && (cdce_ncm_init(sc) == 0))
648 pcfg = cdce_ncm_config;
650 error = usbd_transfer_setup(uaa->device,
651 sc->sc_ifaces_index, sc->sc_xfer,
652 pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
658 if (error || (i == 32)) {
659 device_printf(dev, "No valid alternate "
664 ued = usbd_find_descriptor
665 (uaa->device, NULL, uaa->info.bIfaceIndex,
666 UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF);
668 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
669 error = USB_ERR_INVAL;
671 error = usbd_req_get_string_any(uaa->device, NULL,
672 eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
677 /* fake MAC address */
679 device_printf(dev, "faking MAC address\n");
681 sc->sc_ue.ue_eaddr[0] = 0x2a;
682 memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
683 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
687 memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
689 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
691 char c = eaddr_str[i];
693 if ('0' <= c && c <= '9')
704 sc->sc_ue.ue_eaddr[i / 2] |= c;
707 if (uaa->usb_mode == USB_MODE_DEVICE) {
709 * Do not use the same MAC address like the peer !
711 sc->sc_ue.ue_eaddr[5] ^= 0xFF;
717 ue->ue_udev = uaa->device;
718 ue->ue_mtx = &sc->sc_mtx;
719 ue->ue_methods = &cdce_ue_methods;
721 error = uether_ifattach(ue);
723 device_printf(dev, "could not attach interface\n");
726 return (0); /* success */
730 return (ENXIO); /* failure */
734 cdce_detach(device_t dev)
736 struct cdce_softc *sc = device_get_softc(dev);
737 struct usb_ether *ue = &sc->sc_ue;
739 /* stop all USB transfers first */
740 usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
742 mtx_destroy(&sc->sc_mtx);
748 cdce_start(struct usb_ether *ue)
750 struct cdce_softc *sc = uether_getsc(ue);
753 * Start the USB transfers, if not already started:
755 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
756 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
760 cdce_free_queue(struct mbuf **ppm, uint8_t n)
763 for (x = 0; x != n; x++) {
764 if (ppm[x] != NULL) {
772 cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
774 struct cdce_softc *sc = usbd_xfer_softc(xfer);
775 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
782 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
786 switch (USB_GET_STATE(xfer)) {
787 case USB_ST_TRANSFERRED:
788 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
791 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
793 /* free all previous TX buffers */
794 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
799 for (x = 0; x != CDCE_FRAMES_MAX; x++) {
801 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
806 if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
808 * Zaurus wants a 32-bit CRC appended
812 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
815 if (!m_append(m, 4, (void *)&crc)) {
817 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
821 if (m->m_len != m->m_pkthdr.len) {
822 mt = m_defrag(m, M_NOWAIT);
825 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
830 if (m->m_pkthdr.len > MCLBYTES) {
831 m->m_pkthdr.len = MCLBYTES;
833 sc->sc_tx_buf[x] = m;
834 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
837 * If there's a BPF listener, bounce a copy of
843 usbd_xfer_set_frames(xfer, x);
845 usbd_transfer_submit(xfer);
850 DPRINTFN(11, "transfer error, %s\n",
853 /* free all previous TX buffers */
854 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
856 /* count output errors */
857 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
859 if (error != USB_ERR_CANCELLED) {
860 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
861 /* try to clear stall first */
862 usbd_xfer_set_stall(xfer);
871 cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
873 uint32_t *p_crc = arg;
875 *p_crc = crc32_raw(src, count, *p_crc);
880 cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
882 uint32_t crc = 0xFFFFFFFF;
885 error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
886 return (crc ^ 0xFFFFFFFF);
890 cdce_init(struct usb_ether *ue)
892 struct cdce_softc *sc = uether_getsc(ue);
893 struct ifnet *ifp = uether_getifp(ue);
895 CDCE_LOCK_ASSERT(sc, MA_OWNED);
897 ifp->if_drv_flags |= IFF_DRV_RUNNING;
899 /* start interrupt transfer */
900 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
901 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
904 * Stall data write direction, which depends on USB mode.
906 * Some USB host stacks (e.g. Mac OS X) don't clears stall
907 * bit as it should, so set it in our host mode only.
909 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
910 usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
912 /* start data transfers */
917 cdce_stop(struct usb_ether *ue)
919 struct cdce_softc *sc = uether_getsc(ue);
920 struct ifnet *ifp = uether_getifp(ue);
922 CDCE_LOCK_ASSERT(sc, MA_OWNED);
924 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
927 * stop all the transfers, if not already stopped:
929 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
930 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
931 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
932 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
936 cdce_setmulti(struct usb_ether *ue)
943 cdce_setpromisc(struct usb_ether *ue)
950 cdce_suspend(device_t dev)
952 device_printf(dev, "Suspending\n");
957 cdce_resume(device_t dev)
959 device_printf(dev, "Resuming\n");
964 cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
966 struct cdce_softc *sc = usbd_xfer_softc(xfer);
973 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
975 switch (USB_GET_STATE(xfer)) {
976 case USB_ST_TRANSFERRED:
978 DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
980 for (x = 0; x != aframes; x++) {
982 m = sc->sc_rx_buf[x];
983 sc->sc_rx_buf[x] = NULL;
984 len = usbd_xfer_frame_len(xfer, x);
986 /* Strip off CRC added by Zaurus, if any */
987 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
990 if (len < (int)sizeof(struct ether_header)) {
995 uether_rxmbuf(&sc->sc_ue, m, len);
1001 * TODO: Implement support for multi frame transfers,
1002 * when the USB hardware supports it.
1004 for (x = 0; x != 1; x++) {
1005 if (sc->sc_rx_buf[x] == NULL) {
1006 m = uether_newbuf();
1009 sc->sc_rx_buf[x] = m;
1011 m = sc->sc_rx_buf[x];
1014 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
1016 /* set number of frames and start hardware */
1017 usbd_xfer_set_frames(xfer, x);
1018 usbd_transfer_submit(xfer);
1019 /* flush any received frames */
1020 uether_rxflush(&sc->sc_ue);
1023 default: /* Error */
1024 DPRINTF("error = %s\n",
1025 usbd_errstr(error));
1027 if (error != USB_ERR_CANCELLED) {
1029 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1030 /* try to clear stall first */
1031 usbd_xfer_set_stall(xfer);
1032 usbd_xfer_set_frames(xfer, 0);
1033 usbd_transfer_submit(xfer);
1038 /* need to free the RX-mbufs when we are cancelled */
1039 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
1045 cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
1047 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1050 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1052 switch (USB_GET_STATE(xfer)) {
1053 case USB_ST_TRANSFERRED:
1055 DPRINTF("Received %d bytes\n", actlen);
1057 /* TODO: decode some indications */
1062 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1063 usbd_transfer_submit(xfer);
1066 default: /* Error */
1067 if (error != USB_ERR_CANCELLED) {
1068 /* start clear stall */
1069 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1070 usbd_xfer_set_stall(xfer);
1078 cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
1080 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1081 struct usb_cdc_notification req;
1082 struct usb_page_cache *pc;
1086 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1088 switch (USB_GET_STATE(xfer)) {
1089 case USB_ST_TRANSFERRED:
1091 DPRINTF("Transferred %d bytes\n", actlen);
1093 switch (sc->sc_notify_state) {
1094 case CDCE_NOTIFY_NETWORK_CONNECTION:
1095 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1097 case CDCE_NOTIFY_SPEED_CHANGE:
1098 sc->sc_notify_state = CDCE_NOTIFY_DONE;
1108 * Inform host about connection. Required according to USB CDC
1109 * specification and communicating to Mac OS X USB host stack.
1110 * Some of the values seems ignored by Mac OS X though.
1112 if (sc->sc_notify_state == CDCE_NOTIFY_NETWORK_CONNECTION) {
1113 req.bmRequestType = UCDC_NOTIFICATION;
1114 req.bNotification = UCDC_N_NETWORK_CONNECTION;
1115 req.wIndex[0] = sc->sc_ifaces_index[1];
1117 USETW(req.wValue, 1); /* Connected */
1118 USETW(req.wLength, 0);
1120 pc = usbd_xfer_get_frame(xfer, 0);
1121 usbd_copy_in(pc, 0, &req, sizeof(req));
1122 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1123 usbd_xfer_set_frames(xfer, 1);
1124 usbd_transfer_submit(xfer);
1126 } else if (sc->sc_notify_state == CDCE_NOTIFY_SPEED_CHANGE) {
1127 req.bmRequestType = UCDC_NOTIFICATION;
1128 req.bNotification = UCDC_N_CONNECTION_SPEED_CHANGE;
1129 req.wIndex[0] = sc->sc_ifaces_index[1];
1131 USETW(req.wValue, 0);
1132 USETW(req.wLength, 8);
1134 /* Peak theoretical bulk trasfer rate in bits/s */
1135 if (usbd_get_speed(sc->sc_ue.ue_udev) != USB_SPEED_FULL)
1136 speed = (13 * 512 * 8 * 1000 * 8);
1138 speed = (19 * 64 * 1 * 1000 * 8);
1140 USETDW(req.data + 0, speed); /* Upstream bit rate */
1141 USETDW(req.data + 4, speed); /* Downstream bit rate */
1143 pc = usbd_xfer_get_frame(xfer, 0);
1144 usbd_copy_in(pc, 0, &req, sizeof(req));
1145 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1146 usbd_xfer_set_frames(xfer, 1);
1147 usbd_transfer_submit(xfer);
1151 default: /* Error */
1152 if (error != USB_ERR_CANCELLED) {
1153 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1154 /* start clear stall */
1155 usbd_xfer_set_stall(xfer);
1164 cdce_handle_request(device_t dev,
1165 const void *preq, void **pptr, uint16_t *plen,
1166 uint16_t offset, uint8_t *pstate)
1168 struct cdce_softc *sc = device_get_softc(dev);
1169 const struct usb_device_request *req = preq;
1170 uint8_t is_complete = *pstate;
1173 * When Mac OS X resumes after suspending it expects
1174 * to be notified again after this request.
1176 if (req->bmRequestType == UT_WRITE_CLASS_INTERFACE && \
1177 req->bRequest == UCDC_NCM_SET_ETHERNET_PACKET_FILTER) {
1179 if (is_complete == 1) {
1180 mtx_lock(&sc->sc_mtx);
1181 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1182 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
1183 mtx_unlock(&sc->sc_mtx);
1189 return (ENXIO); /* use builtin handler */
1194 cdce_ncm_tx_zero(struct usb_page_cache *pc,
1195 uint32_t start, uint32_t end)
1197 if (start >= CDCE_NCM_TX_MAXLEN)
1199 if (end > CDCE_NCM_TX_MAXLEN)
1200 end = CDCE_NCM_TX_MAXLEN;
1202 usbd_frame_zero(pc, start, end - start);
1206 cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
1208 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1209 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1210 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
1214 uint32_t last_offset;
1218 usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
1220 offset = sizeof(sc->sc_ncm.hdr) +
1221 sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
1223 /* Store last valid offset before alignment */
1224 last_offset = offset;
1227 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1228 offset, sc->sc_ncm.tx_modulus);
1231 cdce_ncm_tx_zero(pc, last_offset, offset);
1236 for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
1238 /* check if end of transmit buffer is reached */
1240 if (offset >= sc->sc_ncm.tx_max)
1243 /* compute maximum buffer size */
1245 rem = sc->sc_ncm.tx_max - offset;
1247 IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
1250 /* buffer not full */
1255 if (m->m_pkthdr.len > (int)rem) {
1257 /* The frame won't fit in our buffer */
1258 DPRINTFN(1, "Frame too big to be transmitted!\n");
1260 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1264 /* Wait till next buffer becomes ready */
1265 IFQ_DRV_PREPEND(&(ifp->if_snd), m);
1268 usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
1270 USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
1271 USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
1274 offset += m->m_pkthdr.len;
1276 /* Store last valid offset before alignment */
1277 last_offset = offset;
1280 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1281 offset, sc->sc_ncm.tx_modulus);
1284 cdce_ncm_tx_zero(pc, last_offset, offset);
1287 * If there's a BPF listener, bounce a copy
1288 * of this frame to him:
1296 /* Pre-increment interface counter */
1298 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1304 rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
1306 USETW(sc->sc_ncm.dpt.wLength, rem);
1308 /* zero the rest of the data pointer entries */
1309 for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
1310 USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
1311 USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
1314 offset = last_offset;
1317 offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
1319 /* Optimise, save bandwidth and force short termination */
1320 if (offset >= sc->sc_ncm.tx_max)
1321 offset = sc->sc_ncm.tx_max;
1326 cdce_ncm_tx_zero(pc, last_offset, offset);
1328 /* set frame length */
1329 usbd_xfer_set_frame_len(xfer, index, offset);
1331 /* Fill out 16-bit header */
1332 sc->sc_ncm.hdr.dwSignature[0] = 'N';
1333 sc->sc_ncm.hdr.dwSignature[1] = 'C';
1334 sc->sc_ncm.hdr.dwSignature[2] = 'M';
1335 sc->sc_ncm.hdr.dwSignature[3] = 'H';
1336 USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
1337 USETW(sc->sc_ncm.hdr.wBlockLength, offset);
1338 USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
1339 USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
1341 sc->sc_ncm.tx_seq++;
1343 /* Fill out 16-bit frame table header */
1344 sc->sc_ncm.dpt.dwSignature[0] = 'N';
1345 sc->sc_ncm.dpt.dwSignature[1] = 'C';
1346 sc->sc_ncm.dpt.dwSignature[2] = 'M';
1347 sc->sc_ncm.dpt.dwSignature[3] = '0';
1348 USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0); /* reserved */
1350 usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
1351 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
1352 sizeof(sc->sc_ncm.dpt));
1353 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
1354 &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
1359 cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1361 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1362 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1368 switch (USB_GET_STATE(xfer)) {
1369 case USB_ST_TRANSFERRED:
1371 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
1373 DPRINTFN(10, "transfer complete: "
1374 "%u bytes in %u frames\n", actlen, aframes);
1377 for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
1378 temp = cdce_ncm_fill_tx_frames(xfer, x);
1389 usbd_xfer_set_interval(xfer, cdce_tx_interval);
1391 usbd_xfer_set_frames(xfer, x);
1392 usbd_transfer_submit(xfer);
1396 default: /* Error */
1397 DPRINTFN(10, "Transfer error: %s\n",
1398 usbd_errstr(error));
1400 /* update error counter */
1401 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1403 if (error != USB_ERR_CANCELLED) {
1404 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1405 /* try to clear stall first */
1406 usbd_xfer_set_stall(xfer);
1407 usbd_xfer_set_frames(xfer, 0);
1408 usbd_transfer_submit(xfer);
1416 cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1418 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1419 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
1420 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1431 switch (USB_GET_STATE(xfer)) {
1432 case USB_ST_TRANSFERRED:
1434 usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
1436 DPRINTFN(1, "received %u bytes in %u frames\n",
1439 if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
1440 sizeof(sc->sc_ncm.dpt))) {
1441 DPRINTFN(1, "frame too short\n");
1444 usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
1445 sizeof(sc->sc_ncm.hdr));
1447 if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
1448 (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
1449 (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
1450 (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
1451 DPRINTFN(1, "invalid HDR signature: "
1452 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1453 sc->sc_ncm.hdr.dwSignature[0],
1454 sc->sc_ncm.hdr.dwSignature[1],
1455 sc->sc_ncm.hdr.dwSignature[2],
1456 sc->sc_ncm.hdr.dwSignature[3]);
1459 temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
1460 if (temp > sumlen) {
1461 DPRINTFN(1, "unsupported block length %u/%u\n",
1465 temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
1466 if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
1467 DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
1470 usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
1471 sizeof(sc->sc_ncm.dpt));
1473 if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
1474 (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
1475 (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
1476 (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
1477 DPRINTFN(1, "invalid DPT signature"
1478 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1479 sc->sc_ncm.dpt.dwSignature[0],
1480 sc->sc_ncm.dpt.dwSignature[1],
1481 sc->sc_ncm.dpt.dwSignature[2],
1482 sc->sc_ncm.dpt.dwSignature[3]);
1485 nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
1487 /* Subtract size of header and last zero padded entry */
1488 if (nframes >= (2 + 1))
1493 DPRINTFN(1, "nframes = %u\n", nframes);
1495 temp += sizeof(sc->sc_ncm.dpt);
1497 if ((temp + (4 * nframes)) > actlen)
1500 if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
1501 DPRINTFN(1, "Truncating number of frames from %u to %u\n",
1502 nframes, CDCE_NCM_SUBFRAMES_MAX);
1503 nframes = CDCE_NCM_SUBFRAMES_MAX;
1505 usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
1509 for (x = 0; x != nframes; x++) {
1511 offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
1512 temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
1514 if ((offset == 0) ||
1515 (temp < (int)sizeof(struct ether_header)) ||
1516 (temp > (MCLBYTES - ETHER_ALIGN))) {
1517 DPRINTFN(1, "NULL frame detected at %d\n", x);
1519 /* silently ignore this frame */
1521 } else if ((offset + temp) > actlen) {
1522 DPRINTFN(1, "invalid frame "
1523 "detected at %d\n", x);
1525 /* silently ignore this frame */
1527 } else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1528 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1530 m = m_gethdr(M_NOWAIT, MT_DATA);
1533 DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
1536 /* check if we have a buffer */
1538 m_adj(m, ETHER_ALIGN);
1540 usbd_copy_out(pc, offset, m->m_data, temp);
1543 uether_rxmbuf(&sc->sc_ue, m, temp);
1547 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1551 DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
1555 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
1556 usbd_xfer_set_frames(xfer, 1);
1557 usbd_transfer_submit(xfer);
1558 uether_rxflush(&sc->sc_ue); /* must be last */
1561 default: /* Error */
1562 DPRINTFN(1, "error = %s\n",
1563 usbd_errstr(error));
1565 if (error != USB_ERR_CANCELLED) {
1567 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1568 /* try to clear stall first */
1569 usbd_xfer_set_stall(xfer);
1570 usbd_xfer_set_frames(xfer, 0);
1571 usbd_transfer_submit(xfer);