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>
50 #include <sys/stdint.h>
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/module.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
68 #include <net/if_var.h>
69 #include <net/ifq_var.h>
71 #include <bus/u4b/usb.h>
72 #include <bus/u4b/usbdi.h>
73 #include <bus/u4b/usbdi_util.h>
74 #include <bus/u4b/usb_cdc.h>
77 #define USB_DEBUG_VAR cdce_debug
78 #include <bus/u4b/usb_debug.h>
79 #include <bus/u4b/usb_process.h>
80 #include <bus/u4b/usb_msctest.h>
83 #include <bus/u4b/net/usb_ethernet.h>
84 #include <bus/u4b/net/if_cdcereg.h>
86 static device_probe_t cdce_probe;
87 static device_attach_t cdce_attach;
88 static device_detach_t cdce_detach;
89 static device_suspend_t cdce_suspend;
90 static device_resume_t cdce_resume;
91 static usb_handle_request_t cdce_handle_request;
93 static usb_callback_t cdce_bulk_write_callback;
94 static usb_callback_t cdce_bulk_read_callback;
95 static usb_callback_t cdce_intr_read_callback;
96 static usb_callback_t cdce_intr_write_callback;
99 static usb_callback_t cdce_ncm_bulk_write_callback;
100 static usb_callback_t cdce_ncm_bulk_read_callback;
103 static uether_fn_t cdce_attach_post;
104 static uether_fn_t cdce_init;
105 static uether_fn_t cdce_stop;
106 static uether_fn_t cdce_start;
107 static uether_fn_t cdce_setmulti;
108 static uether_fn_t cdce_setpromisc;
110 static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
113 static int cdce_debug = 0;
114 static int cdce_tx_interval = 0;
116 static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
117 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0,
119 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RW, &cdce_tx_interval, 0,
120 "NCM transmit interval in ms");
123 static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
127 .endpoint = UE_ADDR_ANY,
128 .direction = UE_DIR_RX,
130 .frames = CDCE_FRAMES_MAX,
131 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
132 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
133 .callback = cdce_bulk_read_callback,
134 .timeout = 0, /* no timeout */
135 .usb_mode = USB_MODE_DUAL, /* both modes */
140 .endpoint = UE_ADDR_ANY,
141 .direction = UE_DIR_TX,
143 .frames = CDCE_FRAMES_MAX,
144 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
145 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
146 .callback = cdce_bulk_write_callback,
147 .timeout = 10000, /* 10 seconds */
148 .usb_mode = USB_MODE_DUAL, /* both modes */
152 .type = UE_INTERRUPT,
153 .endpoint = UE_ADDR_ANY,
154 .direction = UE_DIR_RX,
156 .bufsize = CDCE_IND_SIZE_MAX,
157 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
158 .callback = cdce_intr_read_callback,
160 .usb_mode = USB_MODE_HOST,
164 .type = UE_INTERRUPT,
165 .endpoint = UE_ADDR_ANY,
166 .direction = UE_DIR_TX,
168 .bufsize = CDCE_IND_SIZE_MAX,
169 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
170 .callback = cdce_intr_write_callback,
171 .timeout = 10000, /* 10 seconds */
172 .usb_mode = USB_MODE_DEVICE,
177 static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
181 .endpoint = UE_ADDR_ANY,
182 .direction = UE_DIR_RX,
184 .frames = CDCE_NCM_RX_FRAMES_MAX,
185 .bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
186 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
187 .callback = cdce_ncm_bulk_read_callback,
188 .timeout = 0, /* no timeout */
189 .usb_mode = USB_MODE_DUAL, /* both modes */
194 .endpoint = UE_ADDR_ANY,
195 .direction = UE_DIR_TX,
197 .frames = CDCE_NCM_TX_FRAMES_MAX,
198 .bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
199 .flags = {.pipe_bof = 1,},
200 .callback = cdce_ncm_bulk_write_callback,
201 .timeout = 10000, /* 10 seconds */
202 .usb_mode = USB_MODE_DUAL, /* both modes */
206 .type = UE_INTERRUPT,
207 .endpoint = UE_ADDR_ANY,
208 .direction = UE_DIR_RX,
210 .bufsize = CDCE_IND_SIZE_MAX,
211 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
212 .callback = cdce_intr_read_callback,
214 .usb_mode = USB_MODE_HOST,
218 .type = UE_INTERRUPT,
219 .endpoint = UE_ADDR_ANY,
220 .direction = UE_DIR_TX,
222 .bufsize = CDCE_IND_SIZE_MAX,
223 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
224 .callback = cdce_intr_write_callback,
225 .timeout = 10000, /* 10 seconds */
226 .usb_mode = USB_MODE_DEVICE,
231 static device_method_t cdce_methods[] = {
233 DEVMETHOD(usb_handle_request, cdce_handle_request),
235 /* Device interface */
236 DEVMETHOD(device_probe, cdce_probe),
237 DEVMETHOD(device_attach, cdce_attach),
238 DEVMETHOD(device_detach, cdce_detach),
239 DEVMETHOD(device_suspend, cdce_suspend),
240 DEVMETHOD(device_resume, cdce_resume),
245 static driver_t cdce_driver = {
247 .methods = cdce_methods,
248 .size = sizeof(struct cdce_softc),
251 static devclass_t cdce_devclass;
252 static eventhandler_tag cdce_etag;
254 static int cdce_driver_loaded(struct module *, int, void *);
256 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, cdce_driver_loaded, NULL);
257 MODULE_VERSION(cdce, 1);
258 MODULE_DEPEND(cdce, uether, 1, 1, 1);
259 MODULE_DEPEND(cdce, usb, 1, 1, 1);
260 MODULE_DEPEND(cdce, ether, 1, 1, 1);
262 static const struct usb_ether_methods cdce_ue_methods = {
263 .ue_attach_post = cdce_attach_post,
264 .ue_start = cdce_start,
265 .ue_init = cdce_init,
266 .ue_stop = cdce_stop,
267 .ue_setmulti = cdce_setmulti,
268 .ue_setpromisc = cdce_setpromisc,
272 static const STRUCT_USB_HOST_ID cdce_switch_devs[] = {
273 {USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3272_INIT, MSC_EJECT_HUAWEI2)},
276 static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
277 {USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
278 {USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
279 {USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
280 {USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
281 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
282 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
283 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
284 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
285 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
286 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
287 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
288 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
289 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
291 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
292 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x16),
294 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
295 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x46),
297 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
298 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x76),
300 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
301 USB_IFACE_SUBCLASS(0x03), USB_IFACE_PROTOCOL(0x16),
306 static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
307 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
308 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
309 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
313 /*------------------------------------------------------------------------*
319 *------------------------------------------------------------------------*/
321 cdce_ncm_init(struct cdce_softc *sc)
323 struct usb_ncm_parameters temp;
324 struct usb_device_request req;
325 struct usb_ncm_func_descriptor *ufd;
329 ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
330 sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0 - 1,
331 UCDC_NCM_FUNC_DESC_SUBTYPE, 0 - 1);
333 /* verify length of NCM functional descriptor */
335 if (ufd->bLength < sizeof(*ufd))
338 DPRINTFN(1, "Found NCM functional descriptor.\n");
341 req.bmRequestType = UT_READ_CLASS_INTERFACE;
342 req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
343 USETW(req.wValue, 0);
344 req.wIndex[0] = sc->sc_ifaces_index[1];
346 USETW(req.wLength, sizeof(temp));
348 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
349 &temp, 0, NULL, 1000 /* ms */);
353 /* Read correct set of parameters according to device mode */
355 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
356 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
357 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
358 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
359 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
360 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
361 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
363 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
364 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
365 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
366 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
367 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
368 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
371 /* Verify maximum receive length */
373 if ((sc->sc_ncm.rx_max < 32) ||
374 (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
375 DPRINTFN(1, "Using default maximum receive length\n");
376 sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
379 /* Verify maximum transmit length */
381 if ((sc->sc_ncm.tx_max < 32) ||
382 (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
383 DPRINTFN(1, "Using default maximum transmit length\n");
384 sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
388 * Verify that the structure alignment is:
390 * - not greater than the maximum transmit length
391 * - not less than four bytes
393 if ((sc->sc_ncm.tx_struct_align < 4) ||
394 (sc->sc_ncm.tx_struct_align !=
395 ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
396 (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
397 DPRINTFN(1, "Using default other alignment: 4 bytes\n");
398 sc->sc_ncm.tx_struct_align = 4;
402 * Verify that the payload alignment is:
404 * - not greater than the maximum transmit length
405 * - not less than four bytes
407 if ((sc->sc_ncm.tx_modulus < 4) ||
408 (sc->sc_ncm.tx_modulus !=
409 ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
410 (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
411 DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
412 sc->sc_ncm.tx_modulus = 4;
415 /* Verify that the payload remainder */
417 if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
418 DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
419 sc->sc_ncm.tx_remainder = 0;
423 * Offset the TX remainder so that IP packet payload starts at
424 * the tx_modulus. This is not too clear in the specification.
427 sc->sc_ncm.tx_remainder =
428 (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
429 (sc->sc_ncm.tx_modulus - 1);
431 /* Verify max datagrams */
433 if (sc->sc_ncm.tx_nframe == 0 ||
434 sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
435 DPRINTFN(1, "Using default max "
436 "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
437 /* need to reserve one entry for zero padding */
438 sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
441 /* Additional configuration, will fail in device side mode, which is OK. */
443 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
444 req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
445 USETW(req.wValue, 0);
446 req.wIndex[0] = sc->sc_ifaces_index[1];
450 (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
451 USETW(req.wLength, 8);
452 USETDW(value, sc->sc_ncm.rx_max);
453 USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
456 USETW(req.wLength, 4);
457 USETDW(value, sc->sc_ncm.rx_max);
460 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
461 &value, 0, NULL, 1000 /* ms */);
463 DPRINTFN(1, "Setting input size "
464 "to %u failed.\n", sc->sc_ncm.rx_max);
467 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
468 req.bRequest = UCDC_NCM_SET_CRC_MODE;
469 USETW(req.wValue, 0); /* no CRC */
470 req.wIndex[0] = sc->sc_ifaces_index[1];
472 USETW(req.wLength, 0);
474 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
475 NULL, 0, NULL, 1000 /* ms */);
477 DPRINTFN(1, "Setting CRC mode to off failed.\n");
480 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
481 req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
482 USETW(req.wValue, 0); /* NTB-16 */
483 req.wIndex[0] = sc->sc_ifaces_index[1];
485 USETW(req.wLength, 0);
487 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
488 NULL, 0, NULL, 1000 /* ms */);
490 DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
493 return (0); /* success */
498 cdce_test_autoinst(void *arg, struct usb_device *udev,
499 struct usb_attach_arg *uaa)
501 struct usb_interface *iface;
502 struct usb_interface_descriptor *id;
504 if (uaa->dev_state != UAA_DEV_READY)
507 iface = usbd_get_iface(udev, 0);
511 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
513 if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
514 return; /* no device match */
516 if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
517 /* success, mark the udev as disappearing */
518 uaa->dev_state = UAA_DEV_EJECTING;
523 cdce_driver_loaded(struct module *mod, int what, void *arg)
527 /* register our autoinstall handler */
528 cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
529 cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
532 EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
540 cdce_probe(device_t dev)
542 struct usb_attach_arg *uaa = device_get_ivars(dev);
545 error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
547 error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
552 cdce_attach_post(struct usb_ether *ue)
554 struct ifnet *ifp = uether_getifp(ue);
555 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
556 ifq_set_ready(&ifp->if_snd);
560 cdce_attach(device_t dev)
562 struct cdce_softc *sc = device_get_softc(dev);
563 struct usb_ether *ue = &sc->sc_ue;
564 struct usb_attach_arg *uaa = device_get_ivars(dev);
565 struct usb_interface *iface;
566 const struct usb_cdc_union_descriptor *ud;
567 const struct usb_interface_descriptor *id;
568 const struct usb_cdc_ethernet_descriptor *ued;
569 const struct usb_config *pcfg;
573 uint8_t data_iface_no;
574 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */
576 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
577 sc->sc_ue.ue_udev = uaa->device;
579 device_set_usb_desc(dev);
581 lockinit(&sc->sc_lock, device_get_nameunit(dev), 0, 0);
583 ud = usbd_find_descriptor
584 (uaa->device, NULL, uaa->info.bIfaceIndex,
585 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
587 if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
588 (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
589 DPRINTFN(1, "No union descriptor!\n");
590 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
591 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
592 goto alloc_transfers;
594 data_iface_no = ud->bSlaveInterface[0];
598 iface = usbd_get_iface(uaa->device, i);
602 id = usbd_get_interface_descriptor(iface);
604 if (id && (id->bInterfaceNumber == data_iface_no)) {
605 sc->sc_ifaces_index[0] = i;
606 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
607 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
611 device_printf(dev, "no data interface found\n");
619 * The Data Class interface of a networking device shall have
620 * a minimum of two interface settings. The first setting
621 * (the default interface setting) includes no endpoints and
622 * therefore no networking traffic is exchanged whenever the
623 * default interface setting is selected. One or more
624 * additional interface settings are used for normal
625 * operation, and therefore each includes a pair of endpoints
626 * (one IN, and one OUT) to exchange network traffic. Select
627 * an alternate interface setting to initialize the network
628 * aspects of the device and to enable the exchange of
633 * Some devices, most notably cable modems, include interface
634 * settings that have no IN or OUT endpoint, therefore loop
635 * through the list of all available interface settings
636 * looking for one with both IN and OUT endpoints.
641 pcfg = cdce_config; /* Default Configuration */
643 for (i = 0; i != 32; i++) {
645 error = usbd_set_alt_interface_index(uaa->device,
646 sc->sc_ifaces_index[0], i);
650 if ((i == 0) && (cdce_ncm_init(sc) == 0))
651 pcfg = cdce_ncm_config;
653 error = usbd_transfer_setup(uaa->device,
654 sc->sc_ifaces_index, sc->sc_xfer,
655 pcfg, CDCE_N_TRANSFER, sc, &sc->sc_lock);
661 if (error || (i == 32)) {
662 device_printf(dev, "No valid alternate "
667 ued = usbd_find_descriptor
668 (uaa->device, NULL, uaa->info.bIfaceIndex,
669 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1);
671 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
672 error = USB_ERR_INVAL;
674 error = usbd_req_get_string_any(uaa->device, NULL,
675 eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
680 /* fake MAC address */
682 device_printf(dev, "faking MAC address\n");
684 sc->sc_ue.ue_eaddr[0] = 0x2a;
685 memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
686 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
690 memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
692 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
694 char c = eaddr_str[i];
696 if ('0' <= c && c <= '9')
707 sc->sc_ue.ue_eaddr[i / 2] |= c;
710 if (uaa->usb_mode == USB_MODE_DEVICE) {
712 * Do not use the same MAC address like the peer !
714 sc->sc_ue.ue_eaddr[5] ^= 0xFF;
720 ue->ue_udev = uaa->device;
721 ue->ue_lock = &sc->sc_lock;
722 ue->ue_methods = &cdce_ue_methods;
724 error = uether_ifattach(ue);
726 device_printf(dev, "could not attach interface\n");
729 return (0); /* success */
733 return (ENXIO); /* failure */
737 cdce_detach(device_t dev)
739 struct cdce_softc *sc = device_get_softc(dev);
740 struct usb_ether *ue = &sc->sc_ue;
742 /* stop all USB transfers first */
743 usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
745 lockuninit(&sc->sc_lock);
751 cdce_start(struct usb_ether *ue)
753 struct cdce_softc *sc = uether_getsc(ue);
756 * Start the USB transfers, if not already started:
758 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
759 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
763 cdce_free_queue(struct mbuf **ppm, uint8_t n)
766 for (x = 0; x != n; x++) {
767 if (ppm[x] != NULL) {
775 cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
777 struct cdce_softc *sc = usbd_xfer_softc(xfer);
778 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
785 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
789 switch (USB_GET_STATE(xfer)) {
790 case USB_ST_TRANSFERRED:
791 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
793 ifq_clr_oactive(&ifp->if_snd);
794 IFNET_STAT_INC(ifp, opackets, 1);
796 /* free all previous TX buffers */
797 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
802 DPRINTFN(11, "transfer setup\n");
803 if(ifq_is_oactive(&ifp->if_snd))
806 for (x = 0; x != CDCE_FRAMES_MAX; x++) {
808 m = ifq_dequeue(&ifp->if_snd);
813 if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
815 * Zaurus wants a 32-bit CRC appended
819 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
822 if (!m_append(m, 4, (void *)&crc)) {
824 IFNET_STAT_INC(ifp, oerrors, 1);
828 if (m->m_len != m->m_pkthdr.len) {
829 mt = m_defrag(m, M_NOWAIT);
837 if (m->m_pkthdr.len > MCLBYTES) {
838 m->m_pkthdr.len = MCLBYTES;
840 sc->sc_tx_buf[x] = m;
841 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
844 * If there's a BPF listener, bounce a copy of
850 usbd_xfer_set_frames(xfer, x);
851 usbd_transfer_submit(xfer);
852 ifq_set_oactive(&ifp->if_snd);
857 DPRINTFN(11, "transfer error, %s\n",
860 /* free all previous TX buffers */
861 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
863 /* count output errors */
864 IFNET_STAT_INC(ifp, oerrors, 1);
865 ifq_clr_oactive(&ifp->if_snd);
866 if (error != USB_ERR_CANCELLED) {
867 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
868 /* try to clear stall first */
869 usbd_xfer_set_stall(xfer);
878 cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
880 uint32_t *p_crc = arg;
882 *p_crc = crc32_ext(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_flags |= IFF_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_flags &= ~IFF_RUNNING;
933 ifq_clr_oactive(&ifp->if_snd);
936 * stop all the transfers, if not already stopped:
938 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
939 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
940 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
941 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
945 cdce_setmulti(struct usb_ether *ue)
952 cdce_setpromisc(struct usb_ether *ue)
959 cdce_suspend(device_t dev)
961 device_printf(dev, "Suspending\n");
966 cdce_resume(device_t dev)
968 device_printf(dev, "Resuming\n");
973 cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
975 struct cdce_softc *sc = usbd_xfer_softc(xfer);
978 int actlen, aframes, len;
980 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
982 switch (USB_GET_STATE(xfer)) {
983 case USB_ST_TRANSFERRED:
985 DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
987 for (x = 0; x != aframes; x++) {
989 m = sc->sc_rx_buf[x];
990 sc->sc_rx_buf[x] = NULL;
991 len = usbd_xfer_frame_len(xfer, x);
993 /* Strip off CRC added by Zaurus, if any */
994 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
997 if (len < (int)sizeof(struct ether_header)) {
1002 uether_rxmbuf(&sc->sc_ue, m, len);
1008 * TODO: Implement support for multi frame transfers,
1009 * when the USB hardware supports it.
1011 for (x = 0; x != 1; x++) {
1012 if (sc->sc_rx_buf[x] == NULL) {
1013 m = uether_newbuf();
1016 sc->sc_rx_buf[x] = m;
1018 m = sc->sc_rx_buf[x];
1021 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
1023 /* set number of frames and start hardware */
1024 usbd_xfer_set_frames(xfer, x);
1025 usbd_transfer_submit(xfer);
1026 /* flush any received frames */
1027 uether_rxflush(&sc->sc_ue);
1030 default: /* Error */
1031 DPRINTF("error = %s\n",
1032 usbd_errstr(error));
1034 if (error != USB_ERR_CANCELLED) {
1036 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1037 /* try to clear stall first */
1038 usbd_xfer_set_stall(xfer);
1039 usbd_xfer_set_frames(xfer, 0);
1040 usbd_transfer_submit(xfer);
1045 /* need to free the RX-mbufs when we are cancelled */
1046 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
1052 cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
1054 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1057 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1059 switch (USB_GET_STATE(xfer)) {
1060 case USB_ST_TRANSFERRED:
1062 DPRINTF("Received %d bytes\n", actlen);
1064 /* TODO: decode some indications */
1069 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1070 usbd_transfer_submit(xfer);
1073 default: /* Error */
1074 if (error != USB_ERR_CANCELLED) {
1075 /* start clear stall */
1076 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1077 usbd_xfer_set_stall(xfer);
1085 cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
1087 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1088 struct usb_cdc_notification req;
1089 struct usb_page_cache *pc;
1093 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1095 switch (USB_GET_STATE(xfer)) {
1096 case USB_ST_TRANSFERRED:
1098 DPRINTF("Transferred %d bytes\n", actlen);
1100 switch (sc->sc_notify_state) {
1101 case CDCE_NOTIFY_NETWORK_CONNECTION:
1102 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1104 case CDCE_NOTIFY_SPEED_CHANGE:
1105 sc->sc_notify_state = CDCE_NOTIFY_DONE;
1115 * Inform host about connection. Required according to USB CDC
1116 * specification and communicating to Mac OS X USB host stack.
1117 * Some of the values seems ignored by Mac OS X though.
1119 if (sc->sc_notify_state == CDCE_NOTIFY_NETWORK_CONNECTION) {
1120 req.bmRequestType = UCDC_NOTIFICATION;
1121 req.bNotification = UCDC_N_NETWORK_CONNECTION;
1122 req.wIndex[0] = sc->sc_ifaces_index[1];
1124 USETW(req.wValue, 1); /* Connected */
1125 USETW(req.wLength, 0);
1127 pc = usbd_xfer_get_frame(xfer, 0);
1128 usbd_copy_in(pc, 0, &req, sizeof(req));
1129 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1130 usbd_xfer_set_frames(xfer, 1);
1131 usbd_transfer_submit(xfer);
1133 } else if (sc->sc_notify_state == CDCE_NOTIFY_SPEED_CHANGE) {
1134 req.bmRequestType = UCDC_NOTIFICATION;
1135 req.bNotification = UCDC_N_CONNECTION_SPEED_CHANGE;
1136 req.wIndex[0] = sc->sc_ifaces_index[1];
1138 USETW(req.wValue, 0);
1139 USETW(req.wLength, 8);
1141 /* Peak theoretical bulk trasfer rate in bits/s */
1142 if (usbd_get_speed(sc->sc_ue.ue_udev) != USB_SPEED_FULL)
1143 speed = (13 * 512 * 8 * 1000 * 8);
1145 speed = (19 * 64 * 1 * 1000 * 8);
1147 USETDW(req.data + 0, speed); /* Upstream bit rate */
1148 USETDW(req.data + 4, speed); /* Downstream bit rate */
1150 pc = usbd_xfer_get_frame(xfer, 0);
1151 usbd_copy_in(pc, 0, &req, sizeof(req));
1152 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1153 usbd_xfer_set_frames(xfer, 1);
1154 usbd_transfer_submit(xfer);
1158 default: /* Error */
1159 if (error != USB_ERR_CANCELLED) {
1160 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1161 /* start clear stall */
1162 usbd_xfer_set_stall(xfer);
1171 cdce_handle_request(device_t dev,
1172 const void *preq, void **pptr, uint16_t *plen,
1173 uint16_t offset, uint8_t *pstate)
1175 struct cdce_softc *sc = device_get_softc(dev);
1176 const struct usb_device_request *req = preq;
1177 uint8_t is_complete = *pstate;
1180 * When Mac OS X resumes after suspending it expects
1181 * to be notified again after this request.
1183 if (req->bmRequestType == UT_WRITE_CLASS_INTERFACE && \
1184 req->bRequest == UCDC_NCM_SET_ETHERNET_PACKET_FILTER) {
1186 if (is_complete == 1) {
1188 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1189 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
1196 return (ENXIO); /* use builtin handler */
1201 cdce_ncm_tx_zero(struct usb_page_cache *pc,
1202 uint32_t start, uint32_t end)
1204 if (start >= CDCE_NCM_TX_MAXLEN)
1206 if (end > CDCE_NCM_TX_MAXLEN)
1207 end = CDCE_NCM_TX_MAXLEN;
1209 usbd_frame_zero(pc, start, end - start);
1213 cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
1215 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1216 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1217 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
1221 uint32_t last_offset;
1225 usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
1227 offset = sizeof(sc->sc_ncm.hdr) +
1228 sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
1230 /* Store last valid offset before alignment */
1231 last_offset = offset;
1234 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1235 offset, sc->sc_ncm.tx_modulus);
1238 cdce_ncm_tx_zero(pc, last_offset, offset);
1243 for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
1245 /* check if end of transmit buffer is reached */
1247 if (offset >= sc->sc_ncm.tx_max)
1250 /* compute maximum buffer size */
1252 rem = sc->sc_ncm.tx_max - offset;
1254 m = ifq_dequeue(&ifp->if_snd);
1257 /* buffer not full */
1262 if (m->m_pkthdr.len > (int)rem) {
1264 /* The frame won't fit in our buffer */
1265 DPRINTFN(1, "Frame too big to be transmitted!\n");
1271 /* Wait till next buffer becomes ready */
1272 ifq_prepend(&ifp->if_snd, m);
1275 usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
1277 USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
1278 USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
1281 offset += m->m_pkthdr.len;
1283 /* Store last valid offset before alignment */
1284 last_offset = offset;
1287 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1288 offset, sc->sc_ncm.tx_modulus);
1291 cdce_ncm_tx_zero(pc, last_offset, offset);
1294 * If there's a BPF listener, bounce a copy
1295 * of this frame to him:
1303 /* Pre-increment interface counter */
1305 IFNET_STAT_INC(ifp, opackets, 1);
1311 rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
1313 USETW(sc->sc_ncm.dpt.wLength, rem);
1315 /* zero the rest of the data pointer entries */
1316 for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
1317 USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
1318 USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
1321 offset = last_offset;
1324 offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
1326 /* Optimise, save bandwidth and force short termination */
1327 if (offset >= sc->sc_ncm.tx_max)
1328 offset = sc->sc_ncm.tx_max;
1333 cdce_ncm_tx_zero(pc, last_offset, offset);
1335 /* set frame length */
1336 usbd_xfer_set_frame_len(xfer, index, offset);
1338 /* Fill out 16-bit header */
1339 sc->sc_ncm.hdr.dwSignature[0] = 'N';
1340 sc->sc_ncm.hdr.dwSignature[1] = 'C';
1341 sc->sc_ncm.hdr.dwSignature[2] = 'M';
1342 sc->sc_ncm.hdr.dwSignature[3] = 'H';
1343 USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
1344 USETW(sc->sc_ncm.hdr.wBlockLength, offset);
1345 USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
1346 USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
1348 sc->sc_ncm.tx_seq++;
1350 /* Fill out 16-bit frame table header */
1351 sc->sc_ncm.dpt.dwSignature[0] = 'N';
1352 sc->sc_ncm.dpt.dwSignature[1] = 'C';
1353 sc->sc_ncm.dpt.dwSignature[2] = 'M';
1354 sc->sc_ncm.dpt.dwSignature[3] = '0';
1355 USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0); /* reserved */
1357 usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
1358 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
1359 sizeof(sc->sc_ncm.dpt));
1360 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
1361 &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
1366 cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1368 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1369 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1375 switch (USB_GET_STATE(xfer)) {
1376 case USB_ST_TRANSFERRED:
1378 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
1380 DPRINTFN(10, "transfer complete: "
1381 "%u bytes in %u frames\n", actlen, aframes);
1384 for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
1385 temp = cdce_ncm_fill_tx_frames(xfer, x);
1396 usbd_xfer_set_interval(xfer, cdce_tx_interval);
1398 usbd_xfer_set_frames(xfer, x);
1399 usbd_transfer_submit(xfer);
1403 default: /* Error */
1404 DPRINTFN(10, "Transfer error: %s\n",
1405 usbd_errstr(error));
1407 /* update error counter */
1408 ifp->if_oerrors += 1;
1410 if (error != USB_ERR_CANCELLED) {
1411 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1412 /* try to clear stall first */
1413 usbd_xfer_set_stall(xfer);
1414 usbd_xfer_set_frames(xfer, 0);
1415 usbd_transfer_submit(xfer);
1423 cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1425 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1426 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
1427 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1438 switch (USB_GET_STATE(xfer)) {
1439 case USB_ST_TRANSFERRED:
1441 usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
1443 DPRINTFN(1, "received %u bytes in %u frames\n",
1446 if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
1447 sizeof(sc->sc_ncm.dpt))) {
1448 DPRINTFN(1, "frame too short\n");
1451 usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
1452 sizeof(sc->sc_ncm.hdr));
1454 if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
1455 (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
1456 (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
1457 (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
1458 DPRINTFN(1, "invalid HDR signature: "
1459 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1460 sc->sc_ncm.hdr.dwSignature[0],
1461 sc->sc_ncm.hdr.dwSignature[1],
1462 sc->sc_ncm.hdr.dwSignature[2],
1463 sc->sc_ncm.hdr.dwSignature[3]);
1466 temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
1467 if (temp > sumlen) {
1468 DPRINTFN(1, "unsupported block length %u/%u\n",
1472 temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
1473 if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
1474 DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
1477 usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
1478 sizeof(sc->sc_ncm.dpt));
1480 if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
1481 (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
1482 (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
1483 (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
1484 DPRINTFN(1, "invalid DPT signature"
1485 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1486 sc->sc_ncm.dpt.dwSignature[0],
1487 sc->sc_ncm.dpt.dwSignature[1],
1488 sc->sc_ncm.dpt.dwSignature[2],
1489 sc->sc_ncm.dpt.dwSignature[3]);
1492 nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
1494 /* Subtract size of header and last zero padded entry */
1495 if (nframes >= (2 + 1))
1500 DPRINTFN(1, "nframes = %u\n", nframes);
1502 temp += sizeof(sc->sc_ncm.dpt);
1504 if ((temp + (4 * nframes)) > actlen)
1507 if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
1508 DPRINTFN(1, "Truncating number of frames from %u to %u\n",
1509 nframes, CDCE_NCM_SUBFRAMES_MAX);
1510 nframes = CDCE_NCM_SUBFRAMES_MAX;
1512 usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
1516 for (x = 0; x != nframes; x++) {
1518 offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
1519 temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
1521 if ((offset == 0) ||
1522 (temp < (int)sizeof(struct ether_header)) ||
1523 (temp > (MCLBYTES - ETHER_ALIGN))) {
1524 DPRINTFN(1, "NULL frame detected at %d\n", x);
1526 /* silently ignore this frame */
1528 } else if ((offset + temp) > actlen) {
1529 DPRINTFN(1, "invalid frame "
1530 "detected at %d\n", x);
1532 /* silently ignore this frame */
1534 } else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1535 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1537 m = m_gethdr(M_NOWAIT, MT_DATA);
1540 DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
1543 /* check if we have a buffer */
1545 m->m_len = m->m_pkthdr.len = temp + ETHER_ALIGN;
1546 m_adj(m, ETHER_ALIGN);
1548 usbd_copy_out(pc, offset, m->m_data, temp);
1551 uether_rxmbuf(&sc->sc_ue, m, temp);
1559 DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
1563 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
1564 usbd_xfer_set_frames(xfer, 1);
1565 usbd_transfer_submit(xfer);
1566 uether_rxflush(&sc->sc_ue); /* must be last */
1569 default: /* Error */
1570 DPRINTFN(1, "error = %s\n",
1571 usbd_errstr(error));
1573 if (error != USB_ERR_CANCELLED) {
1575 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1576 /* try to clear stall first */
1577 usbd_xfer_set_stall(xfer);
1578 usbd_xfer_set_frames(xfer, 0);
1579 usbd_transfer_submit(xfer);