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),
291 static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
292 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
293 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
294 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
297 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, cdce_driver_loaded, 0);
298 MODULE_VERSION(cdce, 1);
299 MODULE_DEPEND(cdce, uether, 1, 1, 1);
300 MODULE_DEPEND(cdce, usb, 1, 1, 1);
301 MODULE_DEPEND(cdce, ether, 1, 1, 1);
302 USB_PNP_DEVICE_INFO(cdce_switch_devs);
303 USB_PNP_HOST_INFO(cdce_host_devs);
304 USB_PNP_DUAL_INFO(cdce_dual_devs);
306 static const struct usb_ether_methods cdce_ue_methods = {
307 .ue_attach_post = cdce_attach_post,
308 .ue_start = cdce_start,
309 .ue_init = cdce_init,
310 .ue_stop = cdce_stop,
311 .ue_setmulti = cdce_setmulti,
312 .ue_setpromisc = cdce_setpromisc,
316 /*------------------------------------------------------------------------*
322 *------------------------------------------------------------------------*/
324 cdce_ncm_init(struct cdce_softc *sc)
326 struct usb_ncm_parameters temp;
327 struct usb_device_request req;
328 struct usb_ncm_func_descriptor *ufd;
332 ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
333 sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF,
334 UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF);
336 /* verify length of NCM functional descriptor */
338 if (ufd->bLength < sizeof(*ufd))
341 DPRINTFN(1, "Found NCM functional descriptor.\n");
344 req.bmRequestType = UT_READ_CLASS_INTERFACE;
345 req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
346 USETW(req.wValue, 0);
347 req.wIndex[0] = sc->sc_ifaces_index[1];
349 USETW(req.wLength, sizeof(temp));
351 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
352 &temp, 0, NULL, 1000 /* ms */);
356 /* Read correct set of parameters according to device mode */
358 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
359 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
360 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
361 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
362 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
363 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
364 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
366 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
367 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
368 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
369 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
370 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
371 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
374 /* Verify maximum receive length */
376 if ((sc->sc_ncm.rx_max < 32) ||
377 (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
378 DPRINTFN(1, "Using default maximum receive length\n");
379 sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
382 /* Verify maximum transmit length */
384 if ((sc->sc_ncm.tx_max < 32) ||
385 (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
386 DPRINTFN(1, "Using default maximum transmit length\n");
387 sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
391 * Verify that the structure alignment is:
393 * - not greater than the maximum transmit length
394 * - not less than four bytes
396 if ((sc->sc_ncm.tx_struct_align < 4) ||
397 (sc->sc_ncm.tx_struct_align !=
398 ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
399 (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
400 DPRINTFN(1, "Using default other alignment: 4 bytes\n");
401 sc->sc_ncm.tx_struct_align = 4;
405 * Verify that the payload alignment is:
407 * - not greater than the maximum transmit length
408 * - not less than four bytes
410 if ((sc->sc_ncm.tx_modulus < 4) ||
411 (sc->sc_ncm.tx_modulus !=
412 ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
413 (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
414 DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
415 sc->sc_ncm.tx_modulus = 4;
418 /* Verify that the payload remainder */
420 if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
421 DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
422 sc->sc_ncm.tx_remainder = 0;
426 * Offset the TX remainder so that IP packet payload starts at
427 * the tx_modulus. This is not too clear in the specification.
430 sc->sc_ncm.tx_remainder =
431 (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
432 (sc->sc_ncm.tx_modulus - 1);
434 /* Verify max datagrams */
436 if (sc->sc_ncm.tx_nframe == 0 ||
437 sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
438 DPRINTFN(1, "Using default max "
439 "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
440 /* need to reserve one entry for zero padding */
441 sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
444 /* Additional configuration, will fail in device side mode, which is OK. */
446 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
447 req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
448 USETW(req.wValue, 0);
449 req.wIndex[0] = sc->sc_ifaces_index[1];
453 (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
454 USETW(req.wLength, 8);
455 USETDW(value, sc->sc_ncm.rx_max);
456 USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
459 USETW(req.wLength, 4);
460 USETDW(value, sc->sc_ncm.rx_max);
463 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
464 &value, 0, NULL, 1000 /* ms */);
466 DPRINTFN(1, "Setting input size "
467 "to %u failed.\n", sc->sc_ncm.rx_max);
470 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
471 req.bRequest = UCDC_NCM_SET_CRC_MODE;
472 USETW(req.wValue, 0); /* no CRC */
473 req.wIndex[0] = sc->sc_ifaces_index[1];
475 USETW(req.wLength, 0);
477 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
478 NULL, 0, NULL, 1000 /* ms */);
480 DPRINTFN(1, "Setting CRC mode to off failed.\n");
483 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
484 req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
485 USETW(req.wValue, 0); /* NTB-16 */
486 req.wIndex[0] = sc->sc_ifaces_index[1];
488 USETW(req.wLength, 0);
490 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
491 NULL, 0, NULL, 1000 /* ms */);
493 DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
496 return (0); /* success */
501 cdce_test_autoinst(void *arg, struct usb_device *udev,
502 struct usb_attach_arg *uaa)
504 struct usb_interface *iface;
505 struct usb_interface_descriptor *id;
507 if (uaa->dev_state != UAA_DEV_READY)
510 iface = usbd_get_iface(udev, 0);
514 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
516 if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
517 return; /* no device match */
519 if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
520 /* success, mark the udev as disappearing */
521 uaa->dev_state = UAA_DEV_EJECTING;
526 cdce_driver_loaded(struct module *mod, int what, void *arg)
530 /* register our autoinstall handler */
531 cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
532 cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
535 EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
543 cdce_probe(device_t dev)
545 struct usb_attach_arg *uaa = device_get_ivars(dev);
548 error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
550 error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
555 cdce_attach_post(struct usb_ether *ue)
562 cdce_attach(device_t dev)
564 struct cdce_softc *sc = device_get_softc(dev);
565 struct usb_ether *ue = &sc->sc_ue;
566 struct usb_attach_arg *uaa = device_get_ivars(dev);
567 struct usb_interface *iface;
568 const struct usb_cdc_union_descriptor *ud;
569 const struct usb_interface_descriptor *id;
570 const struct usb_cdc_ethernet_descriptor *ued;
571 const struct usb_config *pcfg;
575 uint8_t data_iface_no;
576 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */
578 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
579 sc->sc_ue.ue_udev = uaa->device;
581 device_set_usb_desc(dev);
583 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
585 ud = usbd_find_descriptor
586 (uaa->device, NULL, uaa->info.bIfaceIndex,
587 UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF);
589 if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
590 (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
591 DPRINTFN(1, "No union descriptor!\n");
592 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
593 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
594 goto alloc_transfers;
596 data_iface_no = ud->bSlaveInterface[0];
600 iface = usbd_get_iface(uaa->device, i);
604 id = usbd_get_interface_descriptor(iface);
606 if (id && (id->bInterfaceNumber == data_iface_no)) {
607 sc->sc_ifaces_index[0] = i;
608 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
609 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
613 device_printf(dev, "no data interface found\n");
621 * The Data Class interface of a networking device shall have
622 * a minimum of two interface settings. The first setting
623 * (the default interface setting) includes no endpoints and
624 * therefore no networking traffic is exchanged whenever the
625 * default interface setting is selected. One or more
626 * additional interface settings are used for normal
627 * operation, and therefore each includes a pair of endpoints
628 * (one IN, and one OUT) to exchange network traffic. Select
629 * an alternate interface setting to initialize the network
630 * aspects of the device and to enable the exchange of
635 * Some devices, most notably cable modems, include interface
636 * settings that have no IN or OUT endpoint, therefore loop
637 * through the list of all available interface settings
638 * looking for one with both IN and OUT endpoints.
643 pcfg = cdce_config; /* Default Configuration */
645 for (i = 0; i != 32; i++) {
647 error = usbd_set_alt_interface_index(uaa->device,
648 sc->sc_ifaces_index[0], i);
652 if ((i == 0) && (cdce_ncm_init(sc) == 0))
653 pcfg = cdce_ncm_config;
655 error = usbd_transfer_setup(uaa->device,
656 sc->sc_ifaces_index, sc->sc_xfer,
657 pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
663 if (error || (i == 32)) {
664 device_printf(dev, "No valid alternate "
669 ued = usbd_find_descriptor
670 (uaa->device, NULL, uaa->info.bIfaceIndex,
671 UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF);
673 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
674 error = USB_ERR_INVAL;
676 error = usbd_req_get_string_any(uaa->device, NULL,
677 eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
682 /* fake MAC address */
684 device_printf(dev, "faking MAC address\n");
686 sc->sc_ue.ue_eaddr[0] = 0x2a;
687 memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
688 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
692 memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
694 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
696 char c = eaddr_str[i];
698 if ('0' <= c && c <= '9')
709 sc->sc_ue.ue_eaddr[i / 2] |= c;
712 if (uaa->usb_mode == USB_MODE_DEVICE) {
714 * Do not use the same MAC address like the peer !
716 sc->sc_ue.ue_eaddr[5] ^= 0xFF;
722 ue->ue_udev = uaa->device;
723 ue->ue_mtx = &sc->sc_mtx;
724 ue->ue_methods = &cdce_ue_methods;
726 error = uether_ifattach(ue);
728 device_printf(dev, "could not attach interface\n");
731 return (0); /* success */
735 return (ENXIO); /* failure */
739 cdce_detach(device_t dev)
741 struct cdce_softc *sc = device_get_softc(dev);
742 struct usb_ether *ue = &sc->sc_ue;
744 /* stop all USB transfers first */
745 usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
747 mtx_destroy(&sc->sc_mtx);
753 cdce_start(struct usb_ether *ue)
755 struct cdce_softc *sc = uether_getsc(ue);
758 * Start the USB transfers, if not already started:
760 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
761 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
765 cdce_free_queue(struct mbuf **ppm, uint8_t n)
768 for (x = 0; x != n; x++) {
769 if (ppm[x] != NULL) {
777 cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
779 struct cdce_softc *sc = usbd_xfer_softc(xfer);
780 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
787 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
791 switch (USB_GET_STATE(xfer)) {
792 case USB_ST_TRANSFERRED:
793 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
796 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
798 /* free all previous TX buffers */
799 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
804 for (x = 0; x != CDCE_FRAMES_MAX; x++) {
806 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
811 if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
813 * Zaurus wants a 32-bit CRC appended
817 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
820 if (!m_append(m, 4, (void *)&crc)) {
822 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
826 if (m->m_len != m->m_pkthdr.len) {
827 mt = m_defrag(m, M_NOWAIT);
830 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
835 if (m->m_pkthdr.len > MCLBYTES) {
836 m->m_pkthdr.len = MCLBYTES;
838 sc->sc_tx_buf[x] = m;
839 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
842 * If there's a BPF listener, bounce a copy of
848 usbd_xfer_set_frames(xfer, x);
850 usbd_transfer_submit(xfer);
855 DPRINTFN(11, "transfer error, %s\n",
858 /* free all previous TX buffers */
859 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
861 /* count output errors */
862 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
864 if (error != USB_ERR_CANCELLED) {
865 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
866 /* try to clear stall first */
867 usbd_xfer_set_stall(xfer);
876 cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
878 uint32_t *p_crc = arg;
880 *p_crc = crc32_raw(src, count, *p_crc);
885 cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
887 uint32_t crc = 0xFFFFFFFF;
890 error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
891 return (crc ^ 0xFFFFFFFF);
895 cdce_init(struct usb_ether *ue)
897 struct cdce_softc *sc = uether_getsc(ue);
898 struct ifnet *ifp = uether_getifp(ue);
900 CDCE_LOCK_ASSERT(sc, MA_OWNED);
902 ifp->if_drv_flags |= IFF_DRV_RUNNING;
904 /* start interrupt transfer */
905 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
906 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
909 * Stall data write direction, which depends on USB mode.
911 * Some USB host stacks (e.g. Mac OS X) don't clears stall
912 * bit as it should, so set it in our host mode only.
914 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
915 usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
917 /* start data transfers */
922 cdce_stop(struct usb_ether *ue)
924 struct cdce_softc *sc = uether_getsc(ue);
925 struct ifnet *ifp = uether_getifp(ue);
927 CDCE_LOCK_ASSERT(sc, MA_OWNED);
929 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
932 * stop all the transfers, if not already stopped:
934 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
935 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
936 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
937 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
941 cdce_setmulti(struct usb_ether *ue)
948 cdce_setpromisc(struct usb_ether *ue)
955 cdce_suspend(device_t dev)
957 device_printf(dev, "Suspending\n");
962 cdce_resume(device_t dev)
964 device_printf(dev, "Resuming\n");
969 cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
971 struct cdce_softc *sc = usbd_xfer_softc(xfer);
978 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
980 switch (USB_GET_STATE(xfer)) {
981 case USB_ST_TRANSFERRED:
983 DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
985 for (x = 0; x != aframes; x++) {
987 m = sc->sc_rx_buf[x];
988 sc->sc_rx_buf[x] = NULL;
989 len = usbd_xfer_frame_len(xfer, x);
991 /* Strip off CRC added by Zaurus, if any */
992 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
995 if (len < (int)sizeof(struct ether_header)) {
1000 uether_rxmbuf(&sc->sc_ue, m, len);
1006 * TODO: Implement support for multi frame transfers,
1007 * when the USB hardware supports it.
1009 for (x = 0; x != 1; x++) {
1010 if (sc->sc_rx_buf[x] == NULL) {
1011 m = uether_newbuf();
1014 sc->sc_rx_buf[x] = m;
1016 m = sc->sc_rx_buf[x];
1019 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
1021 /* set number of frames and start hardware */
1022 usbd_xfer_set_frames(xfer, x);
1023 usbd_transfer_submit(xfer);
1024 /* flush any received frames */
1025 uether_rxflush(&sc->sc_ue);
1028 default: /* Error */
1029 DPRINTF("error = %s\n",
1030 usbd_errstr(error));
1032 if (error != USB_ERR_CANCELLED) {
1034 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1035 /* try to clear stall first */
1036 usbd_xfer_set_stall(xfer);
1037 usbd_xfer_set_frames(xfer, 0);
1038 usbd_transfer_submit(xfer);
1043 /* need to free the RX-mbufs when we are cancelled */
1044 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
1050 cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
1052 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1055 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1057 switch (USB_GET_STATE(xfer)) {
1058 case USB_ST_TRANSFERRED:
1060 DPRINTF("Received %d bytes\n", actlen);
1062 /* TODO: decode some indications */
1067 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1068 usbd_transfer_submit(xfer);
1071 default: /* Error */
1072 if (error != USB_ERR_CANCELLED) {
1073 /* start clear stall */
1074 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1075 usbd_xfer_set_stall(xfer);
1083 cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
1085 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1086 struct usb_cdc_notification req;
1087 struct usb_page_cache *pc;
1091 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1093 switch (USB_GET_STATE(xfer)) {
1094 case USB_ST_TRANSFERRED:
1096 DPRINTF("Transferred %d bytes\n", actlen);
1098 switch (sc->sc_notify_state) {
1099 case CDCE_NOTIFY_NETWORK_CONNECTION:
1100 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1102 case CDCE_NOTIFY_SPEED_CHANGE:
1103 sc->sc_notify_state = CDCE_NOTIFY_DONE;
1113 * Inform host about connection. Required according to USB CDC
1114 * specification and communicating to Mac OS X USB host stack.
1115 * Some of the values seems ignored by Mac OS X though.
1117 if (sc->sc_notify_state == CDCE_NOTIFY_NETWORK_CONNECTION) {
1118 req.bmRequestType = UCDC_NOTIFICATION;
1119 req.bNotification = UCDC_N_NETWORK_CONNECTION;
1120 req.wIndex[0] = sc->sc_ifaces_index[1];
1122 USETW(req.wValue, 1); /* Connected */
1123 USETW(req.wLength, 0);
1125 pc = usbd_xfer_get_frame(xfer, 0);
1126 usbd_copy_in(pc, 0, &req, sizeof(req));
1127 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1128 usbd_xfer_set_frames(xfer, 1);
1129 usbd_transfer_submit(xfer);
1131 } else if (sc->sc_notify_state == CDCE_NOTIFY_SPEED_CHANGE) {
1132 req.bmRequestType = UCDC_NOTIFICATION;
1133 req.bNotification = UCDC_N_CONNECTION_SPEED_CHANGE;
1134 req.wIndex[0] = sc->sc_ifaces_index[1];
1136 USETW(req.wValue, 0);
1137 USETW(req.wLength, 8);
1139 /* Peak theoretical bulk trasfer rate in bits/s */
1140 if (usbd_get_speed(sc->sc_ue.ue_udev) != USB_SPEED_FULL)
1141 speed = (13 * 512 * 8 * 1000 * 8);
1143 speed = (19 * 64 * 1 * 1000 * 8);
1145 USETDW(req.data + 0, speed); /* Upstream bit rate */
1146 USETDW(req.data + 4, speed); /* Downstream bit rate */
1148 pc = usbd_xfer_get_frame(xfer, 0);
1149 usbd_copy_in(pc, 0, &req, sizeof(req));
1150 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1151 usbd_xfer_set_frames(xfer, 1);
1152 usbd_transfer_submit(xfer);
1156 default: /* Error */
1157 if (error != USB_ERR_CANCELLED) {
1158 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1159 /* start clear stall */
1160 usbd_xfer_set_stall(xfer);
1169 cdce_handle_request(device_t dev,
1170 const void *preq, void **pptr, uint16_t *plen,
1171 uint16_t offset, uint8_t *pstate)
1173 struct cdce_softc *sc = device_get_softc(dev);
1174 const struct usb_device_request *req = preq;
1175 uint8_t is_complete = *pstate;
1178 * When Mac OS X resumes after suspending it expects
1179 * to be notified again after this request.
1181 if (req->bmRequestType == UT_WRITE_CLASS_INTERFACE && \
1182 req->bRequest == UCDC_NCM_SET_ETHERNET_PACKET_FILTER) {
1184 if (is_complete == 1) {
1185 mtx_lock(&sc->sc_mtx);
1186 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1187 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
1188 mtx_unlock(&sc->sc_mtx);
1194 return (ENXIO); /* use builtin handler */
1199 cdce_ncm_tx_zero(struct usb_page_cache *pc,
1200 uint32_t start, uint32_t end)
1202 if (start >= CDCE_NCM_TX_MAXLEN)
1204 if (end > CDCE_NCM_TX_MAXLEN)
1205 end = CDCE_NCM_TX_MAXLEN;
1207 usbd_frame_zero(pc, start, end - start);
1211 cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
1213 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1214 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1215 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
1219 uint32_t last_offset;
1223 usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
1225 offset = sizeof(sc->sc_ncm.hdr) +
1226 sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
1228 /* Store last valid offset before alignment */
1229 last_offset = offset;
1232 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1233 offset, sc->sc_ncm.tx_modulus);
1236 cdce_ncm_tx_zero(pc, last_offset, offset);
1241 for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
1243 /* check if end of transmit buffer is reached */
1245 if (offset >= sc->sc_ncm.tx_max)
1248 /* compute maximum buffer size */
1250 rem = sc->sc_ncm.tx_max - offset;
1252 IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
1255 /* buffer not full */
1260 if (m->m_pkthdr.len > (int)rem) {
1262 /* The frame won't fit in our buffer */
1263 DPRINTFN(1, "Frame too big to be transmitted!\n");
1265 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1269 /* Wait till next buffer becomes ready */
1270 IFQ_DRV_PREPEND(&(ifp->if_snd), m);
1273 usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
1275 USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
1276 USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
1279 offset += m->m_pkthdr.len;
1281 /* Store last valid offset before alignment */
1282 last_offset = offset;
1285 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1286 offset, sc->sc_ncm.tx_modulus);
1289 cdce_ncm_tx_zero(pc, last_offset, offset);
1292 * If there's a BPF listener, bounce a copy
1293 * of this frame to him:
1301 /* Pre-increment interface counter */
1303 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1309 rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
1311 USETW(sc->sc_ncm.dpt.wLength, rem);
1313 /* zero the rest of the data pointer entries */
1314 for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
1315 USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
1316 USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
1319 offset = last_offset;
1322 offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
1324 /* Optimise, save bandwidth and force short termination */
1325 if (offset >= sc->sc_ncm.tx_max)
1326 offset = sc->sc_ncm.tx_max;
1331 cdce_ncm_tx_zero(pc, last_offset, offset);
1333 /* set frame length */
1334 usbd_xfer_set_frame_len(xfer, index, offset);
1336 /* Fill out 16-bit header */
1337 sc->sc_ncm.hdr.dwSignature[0] = 'N';
1338 sc->sc_ncm.hdr.dwSignature[1] = 'C';
1339 sc->sc_ncm.hdr.dwSignature[2] = 'M';
1340 sc->sc_ncm.hdr.dwSignature[3] = 'H';
1341 USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
1342 USETW(sc->sc_ncm.hdr.wBlockLength, offset);
1343 USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
1344 USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
1346 sc->sc_ncm.tx_seq++;
1348 /* Fill out 16-bit frame table header */
1349 sc->sc_ncm.dpt.dwSignature[0] = 'N';
1350 sc->sc_ncm.dpt.dwSignature[1] = 'C';
1351 sc->sc_ncm.dpt.dwSignature[2] = 'M';
1352 sc->sc_ncm.dpt.dwSignature[3] = '0';
1353 USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0); /* reserved */
1355 usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
1356 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
1357 sizeof(sc->sc_ncm.dpt));
1358 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
1359 &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
1364 cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1366 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1367 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1373 switch (USB_GET_STATE(xfer)) {
1374 case USB_ST_TRANSFERRED:
1376 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
1378 DPRINTFN(10, "transfer complete: "
1379 "%u bytes in %u frames\n", actlen, aframes);
1382 for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
1383 temp = cdce_ncm_fill_tx_frames(xfer, x);
1394 usbd_xfer_set_interval(xfer, cdce_tx_interval);
1396 usbd_xfer_set_frames(xfer, x);
1397 usbd_transfer_submit(xfer);
1401 default: /* Error */
1402 DPRINTFN(10, "Transfer error: %s\n",
1403 usbd_errstr(error));
1405 /* update error counter */
1406 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1408 if (error != USB_ERR_CANCELLED) {
1409 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1410 /* try to clear stall first */
1411 usbd_xfer_set_stall(xfer);
1412 usbd_xfer_set_frames(xfer, 0);
1413 usbd_transfer_submit(xfer);
1421 cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1423 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1424 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
1425 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1436 switch (USB_GET_STATE(xfer)) {
1437 case USB_ST_TRANSFERRED:
1439 usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
1441 DPRINTFN(1, "received %u bytes in %u frames\n",
1444 if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
1445 sizeof(sc->sc_ncm.dpt))) {
1446 DPRINTFN(1, "frame too short\n");
1449 usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
1450 sizeof(sc->sc_ncm.hdr));
1452 if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
1453 (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
1454 (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
1455 (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
1456 DPRINTFN(1, "invalid HDR signature: "
1457 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1458 sc->sc_ncm.hdr.dwSignature[0],
1459 sc->sc_ncm.hdr.dwSignature[1],
1460 sc->sc_ncm.hdr.dwSignature[2],
1461 sc->sc_ncm.hdr.dwSignature[3]);
1464 temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
1465 if (temp > sumlen) {
1466 DPRINTFN(1, "unsupported block length %u/%u\n",
1470 temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
1471 if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
1472 DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
1475 usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
1476 sizeof(sc->sc_ncm.dpt));
1478 if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
1479 (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
1480 (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
1481 (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
1482 DPRINTFN(1, "invalid DPT signature"
1483 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1484 sc->sc_ncm.dpt.dwSignature[0],
1485 sc->sc_ncm.dpt.dwSignature[1],
1486 sc->sc_ncm.dpt.dwSignature[2],
1487 sc->sc_ncm.dpt.dwSignature[3]);
1490 nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
1492 /* Subtract size of header and last zero padded entry */
1493 if (nframes >= (2 + 1))
1498 DPRINTFN(1, "nframes = %u\n", nframes);
1500 temp += sizeof(sc->sc_ncm.dpt);
1502 if ((temp + (4 * nframes)) > actlen)
1505 if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
1506 DPRINTFN(1, "Truncating number of frames from %u to %u\n",
1507 nframes, CDCE_NCM_SUBFRAMES_MAX);
1508 nframes = CDCE_NCM_SUBFRAMES_MAX;
1510 usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
1514 for (x = 0; x != nframes; x++) {
1516 offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
1517 temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
1519 if ((offset == 0) ||
1520 (temp < (int)sizeof(struct ether_header)) ||
1521 (temp > (MCLBYTES - ETHER_ALIGN))) {
1522 DPRINTFN(1, "NULL frame detected at %d\n", x);
1524 /* silently ignore this frame */
1526 } else if ((offset + temp) > actlen) {
1527 DPRINTFN(1, "invalid frame "
1528 "detected at %d\n", x);
1530 /* silently ignore this frame */
1532 } else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1533 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1535 m = m_gethdr(M_NOWAIT, MT_DATA);
1538 DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
1541 /* check if we have a buffer */
1543 m->m_len = m->m_pkthdr.len = temp + ETHER_ALIGN;
1544 m_adj(m, ETHER_ALIGN);
1546 usbd_copy_out(pc, offset, m->m_data, temp);
1549 uether_rxmbuf(&sc->sc_ue, m, temp);
1553 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1557 DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
1561 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
1562 usbd_xfer_set_frames(xfer, 1);
1563 usbd_transfer_submit(xfer);
1564 uether_rxflush(&sc->sc_ue); /* must be last */
1567 default: /* Error */
1568 DPRINTFN(1, "error = %s\n",
1569 usbd_errstr(error));
1571 if (error != USB_ERR_CANCELLED) {
1573 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1574 /* try to clear stall first */
1575 usbd_xfer_set_stall(xfer);
1576 usbd_xfer_set_frames(xfer, 0);
1577 usbd_transfer_submit(xfer);