1 /* $NetBSD: if_cdce.c,v 1.4 2004/10/24 12:50:54 augustss Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com>
7 * Copyright (c) 2003-2005 Craig Boston
8 * Copyright (c) 2004 Daniel Hartmeier
9 * Copyright (c) 2009 Hans Petter Selasky
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Bill Paul.
23 * 4. Neither the name of the author nor the names of any co-contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR
31 * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * USB Communication Device Class (Ethernet Networking Control Model)
42 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
46 * USB Network Control Model (NCM)
47 * http://www.usb.org/developers/devclass_docs/NCM10.zip
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/stdint.h>
54 #include <sys/stddef.h>
55 #include <sys/param.h>
56 #include <sys/queue.h>
57 #include <sys/types.h>
58 #include <sys/systm.h>
59 #include <sys/socket.h>
60 #include <sys/kernel.h>
62 #include <sys/module.h>
64 #include <sys/mutex.h>
65 #include <sys/condvar.h>
66 #include <sys/sysctl.h>
68 #include <sys/unistd.h>
69 #include <sys/callout.h>
70 #include <sys/malloc.h>
74 #include <net/if_var.h>
76 #include <dev/usb/usb.h>
77 #include <dev/usb/usbdi.h>
78 #include <dev/usb/usbdi_util.h>
79 #include <dev/usb/usb_cdc.h>
82 #define USB_DEBUG_VAR cdce_debug
83 #include <dev/usb/usb_debug.h>
84 #include <dev/usb/usb_process.h>
85 #include <dev/usb/usb_msctest.h>
88 #include <dev/usb/net/usb_ethernet.h>
89 #include <dev/usb/net/if_cdcereg.h>
91 static device_probe_t cdce_probe;
92 static device_attach_t cdce_attach;
93 static device_detach_t cdce_detach;
94 static device_suspend_t cdce_suspend;
95 static device_resume_t cdce_resume;
96 static usb_handle_request_t cdce_handle_request;
98 static usb_callback_t cdce_bulk_write_callback;
99 static usb_callback_t cdce_bulk_read_callback;
100 static usb_callback_t cdce_intr_read_callback;
101 static usb_callback_t cdce_intr_write_callback;
104 static usb_callback_t cdce_ncm_bulk_write_callback;
105 static usb_callback_t cdce_ncm_bulk_read_callback;
108 static uether_fn_t cdce_attach_post;
109 static uether_fn_t cdce_init;
110 static uether_fn_t cdce_stop;
111 static uether_fn_t cdce_start;
112 static uether_fn_t cdce_setmulti;
113 static uether_fn_t cdce_setpromisc;
115 static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
118 static int cdce_debug = 0;
119 static int cdce_tx_interval = 0;
121 static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
122 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RWTUN, &cdce_debug, 0,
124 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RWTUN, &cdce_tx_interval, 0,
125 "NCM transmit interval in ms");
128 static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
132 .endpoint = UE_ADDR_ANY,
133 .direction = UE_DIR_RX,
135 .frames = CDCE_FRAMES_MAX,
136 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
137 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
138 .callback = cdce_bulk_read_callback,
139 .timeout = 0, /* no timeout */
140 .usb_mode = USB_MODE_DUAL, /* both modes */
145 .endpoint = UE_ADDR_ANY,
146 .direction = UE_DIR_TX,
148 .frames = CDCE_FRAMES_MAX,
149 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
150 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
151 .callback = cdce_bulk_write_callback,
152 .timeout = 10000, /* 10 seconds */
153 .usb_mode = USB_MODE_DUAL, /* both modes */
157 .type = UE_INTERRUPT,
158 .endpoint = UE_ADDR_ANY,
159 .direction = UE_DIR_RX,
161 .bufsize = CDCE_IND_SIZE_MAX,
162 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
163 .callback = cdce_intr_read_callback,
165 .usb_mode = USB_MODE_HOST,
169 .type = UE_INTERRUPT,
170 .endpoint = UE_ADDR_ANY,
171 .direction = UE_DIR_TX,
173 .bufsize = CDCE_IND_SIZE_MAX,
174 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
175 .callback = cdce_intr_write_callback,
176 .timeout = 10000, /* 10 seconds */
177 .usb_mode = USB_MODE_DEVICE,
182 static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
186 .endpoint = UE_ADDR_ANY,
187 .direction = UE_DIR_RX,
189 .frames = CDCE_NCM_RX_FRAMES_MAX,
190 .bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
191 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
192 .callback = cdce_ncm_bulk_read_callback,
193 .timeout = 0, /* no timeout */
194 .usb_mode = USB_MODE_DUAL, /* both modes */
199 .endpoint = UE_ADDR_ANY,
200 .direction = UE_DIR_TX,
202 .frames = CDCE_NCM_TX_FRAMES_MAX,
203 .bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
204 .flags = {.pipe_bof = 1,},
205 .callback = cdce_ncm_bulk_write_callback,
206 .timeout = 10000, /* 10 seconds */
207 .usb_mode = USB_MODE_DUAL, /* both modes */
211 .type = UE_INTERRUPT,
212 .endpoint = UE_ADDR_ANY,
213 .direction = UE_DIR_RX,
215 .bufsize = CDCE_IND_SIZE_MAX,
216 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
217 .callback = cdce_intr_read_callback,
219 .usb_mode = USB_MODE_HOST,
223 .type = UE_INTERRUPT,
224 .endpoint = UE_ADDR_ANY,
225 .direction = UE_DIR_TX,
227 .bufsize = CDCE_IND_SIZE_MAX,
228 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
229 .callback = cdce_intr_write_callback,
230 .timeout = 10000, /* 10 seconds */
231 .usb_mode = USB_MODE_DEVICE,
236 static device_method_t cdce_methods[] = {
238 DEVMETHOD(usb_handle_request, cdce_handle_request),
240 /* Device interface */
241 DEVMETHOD(device_probe, cdce_probe),
242 DEVMETHOD(device_attach, cdce_attach),
243 DEVMETHOD(device_detach, cdce_detach),
244 DEVMETHOD(device_suspend, cdce_suspend),
245 DEVMETHOD(device_resume, cdce_resume),
250 static driver_t cdce_driver = {
252 .methods = cdce_methods,
253 .size = sizeof(struct cdce_softc),
256 static devclass_t cdce_devclass;
257 static eventhandler_tag cdce_etag;
259 static int cdce_driver_loaded(struct module *, int, void *);
261 static const STRUCT_USB_HOST_ID cdce_switch_devs[] = {
262 {USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3272_INIT, MSC_EJECT_HUAWEI2)},
265 static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
266 {USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
267 {USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
268 {USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
269 {USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
270 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
271 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
272 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
273 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
274 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
275 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
276 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
277 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
278 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
280 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
281 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x16),
283 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
284 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x46),
286 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
287 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x76),
289 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
290 USB_IFACE_SUBCLASS(0x03), USB_IFACE_PROTOCOL(0x16),
294 static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
295 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
296 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
297 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
300 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, cdce_driver_loaded, 0);
301 MODULE_VERSION(cdce, 1);
302 MODULE_DEPEND(cdce, uether, 1, 1, 1);
303 MODULE_DEPEND(cdce, usb, 1, 1, 1);
304 MODULE_DEPEND(cdce, ether, 1, 1, 1);
305 USB_PNP_DEVICE_INFO(cdce_switch_devs);
306 USB_PNP_HOST_INFO(cdce_host_devs);
307 USB_PNP_DUAL_INFO(cdce_dual_devs);
309 static const struct usb_ether_methods cdce_ue_methods = {
310 .ue_attach_post = cdce_attach_post,
311 .ue_start = cdce_start,
312 .ue_init = cdce_init,
313 .ue_stop = cdce_stop,
314 .ue_setmulti = cdce_setmulti,
315 .ue_setpromisc = cdce_setpromisc,
319 /*------------------------------------------------------------------------*
325 *------------------------------------------------------------------------*/
327 cdce_ncm_init(struct cdce_softc *sc)
329 struct usb_ncm_parameters temp;
330 struct usb_device_request req;
331 struct usb_ncm_func_descriptor *ufd;
335 ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
336 sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF,
337 UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF);
339 /* verify length of NCM functional descriptor */
341 if (ufd->bLength < sizeof(*ufd))
344 DPRINTFN(1, "Found NCM functional descriptor.\n");
347 req.bmRequestType = UT_READ_CLASS_INTERFACE;
348 req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
349 USETW(req.wValue, 0);
350 req.wIndex[0] = sc->sc_ifaces_index[1];
352 USETW(req.wLength, sizeof(temp));
354 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
355 &temp, 0, NULL, 1000 /* ms */);
359 /* Read correct set of parameters according to device mode */
361 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
362 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
363 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
364 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
365 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
366 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
367 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
369 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
370 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
371 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
372 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
373 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
374 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
377 /* Verify maximum receive length */
379 if ((sc->sc_ncm.rx_max < 32) ||
380 (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
381 DPRINTFN(1, "Using default maximum receive length\n");
382 sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
385 /* Verify maximum transmit length */
387 if ((sc->sc_ncm.tx_max < 32) ||
388 (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
389 DPRINTFN(1, "Using default maximum transmit length\n");
390 sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
394 * Verify that the structure alignment is:
396 * - not greater than the maximum transmit length
397 * - not less than four bytes
399 if ((sc->sc_ncm.tx_struct_align < 4) ||
400 (sc->sc_ncm.tx_struct_align !=
401 ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
402 (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
403 DPRINTFN(1, "Using default other alignment: 4 bytes\n");
404 sc->sc_ncm.tx_struct_align = 4;
408 * Verify that the payload alignment is:
410 * - not greater than the maximum transmit length
411 * - not less than four bytes
413 if ((sc->sc_ncm.tx_modulus < 4) ||
414 (sc->sc_ncm.tx_modulus !=
415 ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
416 (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
417 DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
418 sc->sc_ncm.tx_modulus = 4;
421 /* Verify that the payload remainder */
423 if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
424 DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
425 sc->sc_ncm.tx_remainder = 0;
429 * Offset the TX remainder so that IP packet payload starts at
430 * the tx_modulus. This is not too clear in the specification.
433 sc->sc_ncm.tx_remainder =
434 (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
435 (sc->sc_ncm.tx_modulus - 1);
437 /* Verify max datagrams */
439 if (sc->sc_ncm.tx_nframe == 0 ||
440 sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
441 DPRINTFN(1, "Using default max "
442 "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
443 /* need to reserve one entry for zero padding */
444 sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
447 /* Additional configuration, will fail in device side mode, which is OK. */
449 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
450 req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
451 USETW(req.wValue, 0);
452 req.wIndex[0] = sc->sc_ifaces_index[1];
456 (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
457 USETW(req.wLength, 8);
458 USETDW(value, sc->sc_ncm.rx_max);
459 USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
462 USETW(req.wLength, 4);
463 USETDW(value, sc->sc_ncm.rx_max);
466 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
467 &value, 0, NULL, 1000 /* ms */);
469 DPRINTFN(1, "Setting input size "
470 "to %u failed.\n", sc->sc_ncm.rx_max);
473 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
474 req.bRequest = UCDC_NCM_SET_CRC_MODE;
475 USETW(req.wValue, 0); /* no CRC */
476 req.wIndex[0] = sc->sc_ifaces_index[1];
478 USETW(req.wLength, 0);
480 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
481 NULL, 0, NULL, 1000 /* ms */);
483 DPRINTFN(1, "Setting CRC mode to off failed.\n");
486 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
487 req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
488 USETW(req.wValue, 0); /* NTB-16 */
489 req.wIndex[0] = sc->sc_ifaces_index[1];
491 USETW(req.wLength, 0);
493 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
494 NULL, 0, NULL, 1000 /* ms */);
496 DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
499 return (0); /* success */
504 cdce_test_autoinst(void *arg, struct usb_device *udev,
505 struct usb_attach_arg *uaa)
507 struct usb_interface *iface;
508 struct usb_interface_descriptor *id;
510 if (uaa->dev_state != UAA_DEV_READY)
513 iface = usbd_get_iface(udev, 0);
517 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
519 if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
520 return; /* no device match */
522 if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
523 /* success, mark the udev as disappearing */
524 uaa->dev_state = UAA_DEV_EJECTING;
529 cdce_driver_loaded(struct module *mod, int what, void *arg)
533 /* register our autoinstall handler */
534 cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
535 cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
538 EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
546 cdce_probe(device_t dev)
548 struct usb_attach_arg *uaa = device_get_ivars(dev);
551 error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
553 error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
558 cdce_attach_post(struct usb_ether *ue)
565 cdce_attach(device_t dev)
567 struct cdce_softc *sc = device_get_softc(dev);
568 struct usb_ether *ue = &sc->sc_ue;
569 struct usb_attach_arg *uaa = device_get_ivars(dev);
570 struct usb_interface *iface;
571 const struct usb_cdc_union_descriptor *ud;
572 const struct usb_interface_descriptor *id;
573 const struct usb_cdc_ethernet_descriptor *ued;
574 const struct usb_config *pcfg;
578 uint8_t data_iface_no;
579 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */
581 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
582 sc->sc_ue.ue_udev = uaa->device;
584 device_set_usb_desc(dev);
586 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
588 ud = usbd_find_descriptor
589 (uaa->device, NULL, uaa->info.bIfaceIndex,
590 UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF);
592 if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
593 (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
594 DPRINTFN(1, "No union descriptor!\n");
595 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
596 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
597 goto alloc_transfers;
599 data_iface_no = ud->bSlaveInterface[0];
603 iface = usbd_get_iface(uaa->device, i);
607 id = usbd_get_interface_descriptor(iface);
609 if (id && (id->bInterfaceNumber == data_iface_no)) {
610 sc->sc_ifaces_index[0] = i;
611 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
612 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
616 device_printf(dev, "no data interface found\n");
624 * The Data Class interface of a networking device shall have
625 * a minimum of two interface settings. The first setting
626 * (the default interface setting) includes no endpoints and
627 * therefore no networking traffic is exchanged whenever the
628 * default interface setting is selected. One or more
629 * additional interface settings are used for normal
630 * operation, and therefore each includes a pair of endpoints
631 * (one IN, and one OUT) to exchange network traffic. Select
632 * an alternate interface setting to initialize the network
633 * aspects of the device and to enable the exchange of
638 * Some devices, most notably cable modems, include interface
639 * settings that have no IN or OUT endpoint, therefore loop
640 * through the list of all available interface settings
641 * looking for one with both IN and OUT endpoints.
646 pcfg = cdce_config; /* Default Configuration */
648 for (i = 0; i != 32; i++) {
650 error = usbd_set_alt_interface_index(uaa->device,
651 sc->sc_ifaces_index[0], i);
655 if ((i == 0) && (cdce_ncm_init(sc) == 0))
656 pcfg = cdce_ncm_config;
658 error = usbd_transfer_setup(uaa->device,
659 sc->sc_ifaces_index, sc->sc_xfer,
660 pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
666 if (error || (i == 32)) {
667 device_printf(dev, "No valid alternate "
672 ued = usbd_find_descriptor
673 (uaa->device, NULL, uaa->info.bIfaceIndex,
674 UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF);
676 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
677 error = USB_ERR_INVAL;
679 error = usbd_req_get_string_any(uaa->device, NULL,
680 eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
685 /* fake MAC address */
687 device_printf(dev, "faking MAC address\n");
689 sc->sc_ue.ue_eaddr[0] = 0x2a;
690 memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
691 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
695 memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
697 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
699 char c = eaddr_str[i];
701 if ('0' <= c && c <= '9')
712 sc->sc_ue.ue_eaddr[i / 2] |= c;
715 if (uaa->usb_mode == USB_MODE_DEVICE) {
717 * Do not use the same MAC address like the peer !
719 sc->sc_ue.ue_eaddr[5] ^= 0xFF;
725 ue->ue_udev = uaa->device;
726 ue->ue_mtx = &sc->sc_mtx;
727 ue->ue_methods = &cdce_ue_methods;
729 error = uether_ifattach(ue);
731 device_printf(dev, "could not attach interface\n");
734 return (0); /* success */
738 return (ENXIO); /* failure */
742 cdce_detach(device_t dev)
744 struct cdce_softc *sc = device_get_softc(dev);
745 struct usb_ether *ue = &sc->sc_ue;
747 /* stop all USB transfers first */
748 usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
750 mtx_destroy(&sc->sc_mtx);
756 cdce_start(struct usb_ether *ue)
758 struct cdce_softc *sc = uether_getsc(ue);
761 * Start the USB transfers, if not already started:
763 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
764 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
768 cdce_free_queue(struct mbuf **ppm, uint8_t n)
771 for (x = 0; x != n; x++) {
772 if (ppm[x] != NULL) {
780 cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
782 struct cdce_softc *sc = usbd_xfer_softc(xfer);
783 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
790 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
794 switch (USB_GET_STATE(xfer)) {
795 case USB_ST_TRANSFERRED:
796 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
799 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
801 /* free all previous TX buffers */
802 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
807 for (x = 0; x != CDCE_FRAMES_MAX; x++) {
809 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
814 if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
816 * Zaurus wants a 32-bit CRC appended
820 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
823 if (!m_append(m, 4, (void *)&crc)) {
825 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
829 if (m->m_len != m->m_pkthdr.len) {
830 mt = m_defrag(m, M_NOWAIT);
833 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
838 if (m->m_pkthdr.len > MCLBYTES) {
839 m->m_pkthdr.len = MCLBYTES;
841 sc->sc_tx_buf[x] = m;
842 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
845 * If there's a BPF listener, bounce a copy of
851 usbd_xfer_set_frames(xfer, x);
853 usbd_transfer_submit(xfer);
858 DPRINTFN(11, "transfer error, %s\n",
861 /* free all previous TX buffers */
862 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
864 /* count output errors */
865 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
867 if (error != USB_ERR_CANCELLED) {
868 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
869 /* try to clear stall first */
870 usbd_xfer_set_stall(xfer);
879 cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
881 uint32_t *p_crc = arg;
883 *p_crc = crc32_raw(src, count, *p_crc);
888 cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
890 uint32_t crc = 0xFFFFFFFF;
893 error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
894 return (crc ^ 0xFFFFFFFF);
898 cdce_init(struct usb_ether *ue)
900 struct cdce_softc *sc = uether_getsc(ue);
901 struct ifnet *ifp = uether_getifp(ue);
903 CDCE_LOCK_ASSERT(sc, MA_OWNED);
905 ifp->if_drv_flags |= IFF_DRV_RUNNING;
907 /* start interrupt transfer */
908 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
909 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
912 * Stall data write direction, which depends on USB mode.
914 * Some USB host stacks (e.g. Mac OS X) don't clears stall
915 * bit as it should, so set it in our host mode only.
917 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
918 usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
920 /* start data transfers */
925 cdce_stop(struct usb_ether *ue)
927 struct cdce_softc *sc = uether_getsc(ue);
928 struct ifnet *ifp = uether_getifp(ue);
930 CDCE_LOCK_ASSERT(sc, MA_OWNED);
932 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
935 * stop all the transfers, if not already stopped:
937 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
938 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
939 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
940 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
944 cdce_setmulti(struct usb_ether *ue)
951 cdce_setpromisc(struct usb_ether *ue)
958 cdce_suspend(device_t dev)
960 device_printf(dev, "Suspending\n");
965 cdce_resume(device_t dev)
967 device_printf(dev, "Resuming\n");
972 cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
974 struct cdce_softc *sc = usbd_xfer_softc(xfer);
981 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
983 switch (USB_GET_STATE(xfer)) {
984 case USB_ST_TRANSFERRED:
986 DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
988 for (x = 0; x != aframes; x++) {
990 m = sc->sc_rx_buf[x];
991 sc->sc_rx_buf[x] = NULL;
992 len = usbd_xfer_frame_len(xfer, x);
994 /* Strip off CRC added by Zaurus, if any */
995 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
998 if (len < (int)sizeof(struct ether_header)) {
1003 uether_rxmbuf(&sc->sc_ue, m, len);
1009 * TODO: Implement support for multi frame transfers,
1010 * when the USB hardware supports it.
1012 for (x = 0; x != 1; x++) {
1013 if (sc->sc_rx_buf[x] == NULL) {
1014 m = uether_newbuf();
1017 sc->sc_rx_buf[x] = m;
1019 m = sc->sc_rx_buf[x];
1022 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
1024 /* set number of frames and start hardware */
1025 usbd_xfer_set_frames(xfer, x);
1026 usbd_transfer_submit(xfer);
1027 /* flush any received frames */
1028 uether_rxflush(&sc->sc_ue);
1031 default: /* Error */
1032 DPRINTF("error = %s\n",
1033 usbd_errstr(error));
1035 if (error != USB_ERR_CANCELLED) {
1037 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1038 /* try to clear stall first */
1039 usbd_xfer_set_stall(xfer);
1040 usbd_xfer_set_frames(xfer, 0);
1041 usbd_transfer_submit(xfer);
1046 /* need to free the RX-mbufs when we are cancelled */
1047 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
1053 cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
1055 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1058 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1060 switch (USB_GET_STATE(xfer)) {
1061 case USB_ST_TRANSFERRED:
1063 DPRINTF("Received %d bytes\n", actlen);
1065 /* TODO: decode some indications */
1070 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1071 usbd_transfer_submit(xfer);
1074 default: /* Error */
1075 if (error != USB_ERR_CANCELLED) {
1076 /* start clear stall */
1077 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1078 usbd_xfer_set_stall(xfer);
1086 cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
1088 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1089 struct usb_cdc_notification req;
1090 struct usb_page_cache *pc;
1094 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1096 switch (USB_GET_STATE(xfer)) {
1097 case USB_ST_TRANSFERRED:
1099 DPRINTF("Transferred %d bytes\n", actlen);
1101 switch (sc->sc_notify_state) {
1102 case CDCE_NOTIFY_NETWORK_CONNECTION:
1103 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1105 case CDCE_NOTIFY_SPEED_CHANGE:
1106 sc->sc_notify_state = CDCE_NOTIFY_DONE;
1116 * Inform host about connection. Required according to USB CDC
1117 * specification and communicating to Mac OS X USB host stack.
1118 * Some of the values seems ignored by Mac OS X though.
1120 if (sc->sc_notify_state == CDCE_NOTIFY_NETWORK_CONNECTION) {
1121 req.bmRequestType = UCDC_NOTIFICATION;
1122 req.bNotification = UCDC_N_NETWORK_CONNECTION;
1123 req.wIndex[0] = sc->sc_ifaces_index[1];
1125 USETW(req.wValue, 1); /* Connected */
1126 USETW(req.wLength, 0);
1128 pc = usbd_xfer_get_frame(xfer, 0);
1129 usbd_copy_in(pc, 0, &req, sizeof(req));
1130 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1131 usbd_xfer_set_frames(xfer, 1);
1132 usbd_transfer_submit(xfer);
1134 } else if (sc->sc_notify_state == CDCE_NOTIFY_SPEED_CHANGE) {
1135 req.bmRequestType = UCDC_NOTIFICATION;
1136 req.bNotification = UCDC_N_CONNECTION_SPEED_CHANGE;
1137 req.wIndex[0] = sc->sc_ifaces_index[1];
1139 USETW(req.wValue, 0);
1140 USETW(req.wLength, 8);
1142 /* Peak theoretical bulk trasfer rate in bits/s */
1143 if (usbd_get_speed(sc->sc_ue.ue_udev) != USB_SPEED_FULL)
1144 speed = (13 * 512 * 8 * 1000 * 8);
1146 speed = (19 * 64 * 1 * 1000 * 8);
1148 USETDW(req.data + 0, speed); /* Upstream bit rate */
1149 USETDW(req.data + 4, speed); /* Downstream bit rate */
1151 pc = usbd_xfer_get_frame(xfer, 0);
1152 usbd_copy_in(pc, 0, &req, sizeof(req));
1153 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1154 usbd_xfer_set_frames(xfer, 1);
1155 usbd_transfer_submit(xfer);
1159 default: /* Error */
1160 if (error != USB_ERR_CANCELLED) {
1161 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1162 /* start clear stall */
1163 usbd_xfer_set_stall(xfer);
1172 cdce_handle_request(device_t dev,
1173 const void *preq, void **pptr, uint16_t *plen,
1174 uint16_t offset, uint8_t *pstate)
1176 struct cdce_softc *sc = device_get_softc(dev);
1177 const struct usb_device_request *req = preq;
1178 uint8_t is_complete = *pstate;
1181 * When Mac OS X resumes after suspending it expects
1182 * to be notified again after this request.
1184 if (req->bmRequestType == UT_WRITE_CLASS_INTERFACE && \
1185 req->bRequest == UCDC_NCM_SET_ETHERNET_PACKET_FILTER) {
1187 if (is_complete == 1) {
1188 mtx_lock(&sc->sc_mtx);
1189 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1190 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
1191 mtx_unlock(&sc->sc_mtx);
1197 return (ENXIO); /* use builtin handler */
1202 cdce_ncm_tx_zero(struct usb_page_cache *pc,
1203 uint32_t start, uint32_t end)
1205 if (start >= CDCE_NCM_TX_MAXLEN)
1207 if (end > CDCE_NCM_TX_MAXLEN)
1208 end = CDCE_NCM_TX_MAXLEN;
1210 usbd_frame_zero(pc, start, end - start);
1214 cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
1216 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1217 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1218 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
1222 uint32_t last_offset;
1226 usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
1228 offset = sizeof(sc->sc_ncm.hdr) +
1229 sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
1231 /* Store last valid offset before alignment */
1232 last_offset = offset;
1235 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1236 offset, sc->sc_ncm.tx_modulus);
1239 cdce_ncm_tx_zero(pc, last_offset, offset);
1244 for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
1246 /* check if end of transmit buffer is reached */
1248 if (offset >= sc->sc_ncm.tx_max)
1251 /* compute maximum buffer size */
1253 rem = sc->sc_ncm.tx_max - offset;
1255 IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
1258 /* buffer not full */
1263 if (m->m_pkthdr.len > (int)rem) {
1265 /* The frame won't fit in our buffer */
1266 DPRINTFN(1, "Frame too big to be transmitted!\n");
1268 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1272 /* Wait till next buffer becomes ready */
1273 IFQ_DRV_PREPEND(&(ifp->if_snd), m);
1276 usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
1278 USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
1279 USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
1282 offset += m->m_pkthdr.len;
1284 /* Store last valid offset before alignment */
1285 last_offset = offset;
1288 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1289 offset, sc->sc_ncm.tx_modulus);
1292 cdce_ncm_tx_zero(pc, last_offset, offset);
1295 * If there's a BPF listener, bounce a copy
1296 * of this frame to him:
1304 /* Pre-increment interface counter */
1306 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1312 rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
1314 USETW(sc->sc_ncm.dpt.wLength, rem);
1316 /* zero the rest of the data pointer entries */
1317 for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
1318 USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
1319 USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
1322 offset = last_offset;
1325 offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
1327 /* Optimise, save bandwidth and force short termination */
1328 if (offset >= sc->sc_ncm.tx_max)
1329 offset = sc->sc_ncm.tx_max;
1334 cdce_ncm_tx_zero(pc, last_offset, offset);
1336 /* set frame length */
1337 usbd_xfer_set_frame_len(xfer, index, offset);
1339 /* Fill out 16-bit header */
1340 sc->sc_ncm.hdr.dwSignature[0] = 'N';
1341 sc->sc_ncm.hdr.dwSignature[1] = 'C';
1342 sc->sc_ncm.hdr.dwSignature[2] = 'M';
1343 sc->sc_ncm.hdr.dwSignature[3] = 'H';
1344 USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
1345 USETW(sc->sc_ncm.hdr.wBlockLength, offset);
1346 USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
1347 USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
1349 sc->sc_ncm.tx_seq++;
1351 /* Fill out 16-bit frame table header */
1352 sc->sc_ncm.dpt.dwSignature[0] = 'N';
1353 sc->sc_ncm.dpt.dwSignature[1] = 'C';
1354 sc->sc_ncm.dpt.dwSignature[2] = 'M';
1355 sc->sc_ncm.dpt.dwSignature[3] = '0';
1356 USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0); /* reserved */
1358 usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
1359 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
1360 sizeof(sc->sc_ncm.dpt));
1361 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
1362 &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
1367 cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1369 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1370 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1376 switch (USB_GET_STATE(xfer)) {
1377 case USB_ST_TRANSFERRED:
1379 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
1381 DPRINTFN(10, "transfer complete: "
1382 "%u bytes in %u frames\n", actlen, aframes);
1385 for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
1386 temp = cdce_ncm_fill_tx_frames(xfer, x);
1397 usbd_xfer_set_interval(xfer, cdce_tx_interval);
1399 usbd_xfer_set_frames(xfer, x);
1400 usbd_transfer_submit(xfer);
1404 default: /* Error */
1405 DPRINTFN(10, "Transfer error: %s\n",
1406 usbd_errstr(error));
1408 /* update error counter */
1409 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1411 if (error != USB_ERR_CANCELLED) {
1412 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1413 /* try to clear stall first */
1414 usbd_xfer_set_stall(xfer);
1415 usbd_xfer_set_frames(xfer, 0);
1416 usbd_transfer_submit(xfer);
1424 cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1426 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1427 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
1428 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1439 switch (USB_GET_STATE(xfer)) {
1440 case USB_ST_TRANSFERRED:
1442 usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
1444 DPRINTFN(1, "received %u bytes in %u frames\n",
1447 if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
1448 sizeof(sc->sc_ncm.dpt))) {
1449 DPRINTFN(1, "frame too short\n");
1452 usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
1453 sizeof(sc->sc_ncm.hdr));
1455 if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
1456 (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
1457 (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
1458 (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
1459 DPRINTFN(1, "invalid HDR signature: "
1460 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1461 sc->sc_ncm.hdr.dwSignature[0],
1462 sc->sc_ncm.hdr.dwSignature[1],
1463 sc->sc_ncm.hdr.dwSignature[2],
1464 sc->sc_ncm.hdr.dwSignature[3]);
1467 temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
1468 if (temp > sumlen) {
1469 DPRINTFN(1, "unsupported block length %u/%u\n",
1473 temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
1474 if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
1475 DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
1478 usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
1479 sizeof(sc->sc_ncm.dpt));
1481 if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
1482 (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
1483 (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
1484 (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
1485 DPRINTFN(1, "invalid DPT signature"
1486 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1487 sc->sc_ncm.dpt.dwSignature[0],
1488 sc->sc_ncm.dpt.dwSignature[1],
1489 sc->sc_ncm.dpt.dwSignature[2],
1490 sc->sc_ncm.dpt.dwSignature[3]);
1493 nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
1495 /* Subtract size of header and last zero padded entry */
1496 if (nframes >= (2 + 1))
1501 DPRINTFN(1, "nframes = %u\n", nframes);
1503 temp += sizeof(sc->sc_ncm.dpt);
1505 if ((temp + (4 * nframes)) > actlen)
1508 if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
1509 DPRINTFN(1, "Truncating number of frames from %u to %u\n",
1510 nframes, CDCE_NCM_SUBFRAMES_MAX);
1511 nframes = CDCE_NCM_SUBFRAMES_MAX;
1513 usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
1517 for (x = 0; x != nframes; x++) {
1519 offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
1520 temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
1522 if ((offset == 0) ||
1523 (temp < (int)sizeof(struct ether_header)) ||
1524 (temp > (MCLBYTES - ETHER_ALIGN))) {
1525 DPRINTFN(1, "NULL frame detected at %d\n", x);
1527 /* silently ignore this frame */
1529 } else if ((offset + temp) > actlen) {
1530 DPRINTFN(1, "invalid frame "
1531 "detected at %d\n", x);
1533 /* silently ignore this frame */
1535 } else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1536 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1538 m = m_gethdr(M_NOWAIT, MT_DATA);
1541 DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
1544 /* check if we have a buffer */
1546 m->m_len = m->m_pkthdr.len = temp + ETHER_ALIGN;
1547 m_adj(m, ETHER_ALIGN);
1549 usbd_copy_out(pc, offset, m->m_data, temp);
1552 uether_rxmbuf(&sc->sc_ue, m, temp);
1556 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1560 DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
1564 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
1565 usbd_xfer_set_frames(xfer, 1);
1566 usbd_transfer_submit(xfer);
1567 uether_rxflush(&sc->sc_ue); /* must be last */
1570 default: /* Error */
1571 DPRINTFN(1, "error = %s\n",
1572 usbd_errstr(error));
1574 if (error != USB_ERR_CANCELLED) {
1576 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1577 /* try to clear stall first */
1578 usbd_xfer_set_stall(xfer);
1579 usbd_xfer_set_frames(xfer, 0);
1580 usbd_transfer_submit(xfer);