1 /* $NetBSD: if_cdce.c,v 1.4 2004/10/24 12:50:54 augustss Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <[email protected]>
7 * Copyright (c) 2003-2005 Craig Boston
8 * Copyright (c) 2004 Daniel Hartmeier
9 * Copyright (c) 2009 Hans Petter Selasky
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
26 *
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.
38 */
39
40 /*
41 * USB Communication Device Class (Ethernet Networking Control Model)
42 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
43 */
44
45 /*
46 * USB Network Control Model (NCM)
47 * http://www.usb.org/developers/devclass_docs/NCM10.zip
48 */
49
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52
53 #include <sys/gsb_crc32.h>
54 #include <sys/eventhandler.h>
55 #include <sys/stdint.h>
56 #include <sys/stddef.h>
57 #include <sys/queue.h>
58 #include <sys/systm.h>
59 #include <sys/socket.h>
60 #include <sys/kernel.h>
61 #include <sys/bus.h>
62 #include <sys/module.h>
63 #include <sys/lock.h>
64 #include <sys/mutex.h>
65 #include <sys/condvar.h>
66 #include <sys/sysctl.h>
67 #include <sys/sx.h>
68 #include <sys/unistd.h>
69 #include <sys/callout.h>
70 #include <sys/malloc.h>
71 #include <sys/priv.h>
72
73 #include <net/if.h>
74 #include <net/if_var.h>
75
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>
80 #include "usbdevs.h"
81
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>
86 #include "usb_if.h"
87
88 #include <dev/usb/net/usb_ethernet.h>
89 #include <dev/usb/net/if_cdcereg.h>
90
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;
97
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;
102
103 #if CDCE_HAVE_NCM
104 static usb_callback_t cdce_ncm_bulk_write_callback;
105 static usb_callback_t cdce_ncm_bulk_read_callback;
106 #endif
107
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;
114 static int cdce_attach_post_sub(struct usb_ether *);
115 static int cdce_ioctl(struct ifnet *, u_long, caddr_t);
116 static int cdce_media_change_cb(struct ifnet *);
117 static void cdce_media_status_cb(struct ifnet *, struct ifmediareq *);
118
119 static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
120 static void cdce_set_filter(struct usb_ether *);
121
122 #ifdef USB_DEBUG
123 static int cdce_debug = 0;
124 static int cdce_tx_interval = 0;
125
126 static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
127 "USB CDC-Ethernet");
128 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RWTUN, &cdce_debug, 0,
129 "Debug level");
130 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RWTUN, &cdce_tx_interval, 0,
131 "NCM transmit interval in ms");
132 #else
133 #define cdce_debug 0
134 #endif
135
136 static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
137 [CDCE_BULK_RX] = {
138 .type = UE_BULK,
139 .endpoint = UE_ADDR_ANY,
140 .direction = UE_DIR_RX,
141 .if_index = 0,
142 .frames = CDCE_FRAMES_MAX,
143 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
144 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
145 .callback = cdce_bulk_read_callback,
146 .timeout = 0, /* no timeout */
147 .usb_mode = USB_MODE_DUAL, /* both modes */
148 },
149
150 [CDCE_BULK_TX] = {
151 .type = UE_BULK,
152 .endpoint = UE_ADDR_ANY,
153 .direction = UE_DIR_TX,
154 .if_index = 0,
155 .frames = CDCE_FRAMES_MAX,
156 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
157 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
158 .callback = cdce_bulk_write_callback,
159 .timeout = 10000, /* 10 seconds */
160 .usb_mode = USB_MODE_DUAL, /* both modes */
161 },
162
163 [CDCE_INTR_RX] = {
164 .type = UE_INTERRUPT,
165 .endpoint = UE_ADDR_ANY,
166 .direction = UE_DIR_RX,
167 .if_index = 1,
168 .bufsize = CDCE_IND_SIZE_MAX,
169 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
170 .callback = cdce_intr_read_callback,
171 .timeout = 0,
172 .usb_mode = USB_MODE_HOST,
173 },
174
175 [CDCE_INTR_TX] = {
176 .type = UE_INTERRUPT,
177 .endpoint = UE_ADDR_ANY,
178 .direction = UE_DIR_TX,
179 .if_index = 1,
180 .bufsize = CDCE_IND_SIZE_MAX,
181 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
182 .callback = cdce_intr_write_callback,
183 .timeout = 10000, /* 10 seconds */
184 .usb_mode = USB_MODE_DEVICE,
185 },
186 };
187
188 #if CDCE_HAVE_NCM
189 static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
190 [CDCE_BULK_RX] = {
191 .type = UE_BULK,
192 .endpoint = UE_ADDR_ANY,
193 .direction = UE_DIR_RX,
194 .if_index = 0,
195 .frames = CDCE_NCM_RX_FRAMES_MAX,
196 .bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
197 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
198 .callback = cdce_ncm_bulk_read_callback,
199 .timeout = 0, /* no timeout */
200 .usb_mode = USB_MODE_DUAL, /* both modes */
201 },
202
203 [CDCE_BULK_TX] = {
204 .type = UE_BULK,
205 .endpoint = UE_ADDR_ANY,
206 .direction = UE_DIR_TX,
207 .if_index = 0,
208 .frames = CDCE_NCM_TX_FRAMES_MAX,
209 .bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
210 .flags = {.pipe_bof = 1,},
211 .callback = cdce_ncm_bulk_write_callback,
212 .timeout = 10000, /* 10 seconds */
213 .usb_mode = USB_MODE_DUAL, /* both modes */
214 },
215
216 [CDCE_INTR_RX] = {
217 .type = UE_INTERRUPT,
218 .endpoint = UE_ADDR_ANY,
219 .direction = UE_DIR_RX,
220 .if_index = 1,
221 .bufsize = CDCE_IND_SIZE_MAX,
222 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
223 .callback = cdce_intr_read_callback,
224 .timeout = 0,
225 .usb_mode = USB_MODE_HOST,
226 },
227
228 [CDCE_INTR_TX] = {
229 .type = UE_INTERRUPT,
230 .endpoint = UE_ADDR_ANY,
231 .direction = UE_DIR_TX,
232 .if_index = 1,
233 .bufsize = CDCE_IND_SIZE_MAX,
234 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
235 .callback = cdce_intr_write_callback,
236 .timeout = 10000, /* 10 seconds */
237 .usb_mode = USB_MODE_DEVICE,
238 },
239 };
240 #endif
241
242 static device_method_t cdce_methods[] = {
243 /* USB interface */
244 DEVMETHOD(usb_handle_request, cdce_handle_request),
245
246 /* Device interface */
247 DEVMETHOD(device_probe, cdce_probe),
248 DEVMETHOD(device_attach, cdce_attach),
249 DEVMETHOD(device_detach, cdce_detach),
250 DEVMETHOD(device_suspend, cdce_suspend),
251 DEVMETHOD(device_resume, cdce_resume),
252
253 DEVMETHOD_END
254 };
255
256 static driver_t cdce_driver = {
257 .name = "cdce",
258 .methods = cdce_methods,
259 .size = sizeof(struct cdce_softc),
260 };
261
262 static devclass_t cdce_devclass;
263 static eventhandler_tag cdce_etag;
264
265 static int cdce_driver_loaded(struct module *, int, void *);
266
267 static const STRUCT_USB_HOST_ID cdce_switch_devs[] = {
268 {USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3272_INIT, MSC_EJECT_HUAWEI2)},
269 };
270
271 static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
272 {USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
273 {USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
274 {USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
275 {USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
276 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
277 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
278 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
279 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
280 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
281 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
282 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
283 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
284 {USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8156, 0)},
285
286 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
287 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x16),
288 USB_DRIVER_INFO(0)},
289 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
290 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x46),
291 USB_DRIVER_INFO(0)},
292 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
293 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x76),
294 USB_DRIVER_INFO(0)},
295 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
296 USB_IFACE_SUBCLASS(0x03), USB_IFACE_PROTOCOL(0x16),
297 USB_DRIVER_INFO(0)},
298 };
299
300 static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
301 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
302 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
303 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
304 };
305
306 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, cdce_driver_loaded, 0);
307 MODULE_VERSION(cdce, 1);
308 MODULE_DEPEND(cdce, uether, 1, 1, 1);
309 MODULE_DEPEND(cdce, usb, 1, 1, 1);
310 MODULE_DEPEND(cdce, ether, 1, 1, 1);
311 USB_PNP_DEVICE_INFO(cdce_switch_devs);
312 USB_PNP_HOST_INFO(cdce_host_devs);
313 USB_PNP_DUAL_INFO(cdce_dual_devs);
314
315 static const struct usb_ether_methods cdce_ue_methods = {
316 .ue_attach_post = cdce_attach_post,
317 .ue_attach_post_sub = cdce_attach_post_sub,
318 .ue_start = cdce_start,
319 .ue_init = cdce_init,
320 .ue_stop = cdce_stop,
321 .ue_setmulti = cdce_setmulti,
322 .ue_setpromisc = cdce_setpromisc,
323 };
324
325 #if CDCE_HAVE_NCM
326 /*------------------------------------------------------------------------*
327 * cdce_ncm_init
328 *
329 * Return values:
330 * 0: Success
331 * Else: Failure
332 *------------------------------------------------------------------------*/
333 static uint8_t
cdce_ncm_init(struct cdce_softc * sc)334 cdce_ncm_init(struct cdce_softc *sc)
335 {
336 struct usb_ncm_parameters temp;
337 struct usb_device_request req;
338 struct usb_ncm_func_descriptor *ufd;
339 uint8_t value[8];
340 int err;
341
342 ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
343 sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF,
344 UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF);
345
346 /* verify length of NCM functional descriptor */
347 if (ufd != NULL) {
348 if (ufd->bLength < sizeof(*ufd))
349 ufd = NULL;
350 else
351 DPRINTFN(1, "Found NCM functional descriptor.\n");
352 }
353
354 req.bmRequestType = UT_READ_CLASS_INTERFACE;
355 req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
356 USETW(req.wValue, 0);
357 req.wIndex[0] = sc->sc_ifaces_index[1];
358 req.wIndex[1] = 0;
359 USETW(req.wLength, sizeof(temp));
360
361 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
362 &temp, 0, NULL, 1000 /* ms */);
363 if (err)
364 return (1);
365
366 /* Read correct set of parameters according to device mode */
367
368 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
369 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
370 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
371 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
372 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
373 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
374 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
375 } else {
376 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
377 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
378 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
379 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
380 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
381 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
382 }
383
384 /* Verify maximum receive length */
385
386 if ((sc->sc_ncm.rx_max < 32) ||
387 (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
388 DPRINTFN(1, "Using default maximum receive length\n");
389 sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
390 }
391
392 /* Verify maximum transmit length */
393
394 if ((sc->sc_ncm.tx_max < 32) ||
395 (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
396 DPRINTFN(1, "Using default maximum transmit length\n");
397 sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
398 }
399
400 /*
401 * Verify that the structure alignment is:
402 * - power of two
403 * - not greater than the maximum transmit length
404 * - not less than four bytes
405 */
406 if ((sc->sc_ncm.tx_struct_align < 4) ||
407 (sc->sc_ncm.tx_struct_align !=
408 ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
409 (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
410 DPRINTFN(1, "Using default other alignment: 4 bytes\n");
411 sc->sc_ncm.tx_struct_align = 4;
412 }
413
414 /*
415 * Verify that the payload alignment is:
416 * - power of two
417 * - not greater than the maximum transmit length
418 * - not less than four bytes
419 */
420 if ((sc->sc_ncm.tx_modulus < 4) ||
421 (sc->sc_ncm.tx_modulus !=
422 ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
423 (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
424 DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
425 sc->sc_ncm.tx_modulus = 4;
426 }
427
428 /* Verify that the payload remainder */
429
430 if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
431 DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
432 sc->sc_ncm.tx_remainder = 0;
433 }
434
435 /*
436 * Offset the TX remainder so that IP packet payload starts at
437 * the tx_modulus. This is not too clear in the specification.
438 */
439
440 sc->sc_ncm.tx_remainder =
441 (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
442 (sc->sc_ncm.tx_modulus - 1);
443
444 /* Verify max datagrams */
445
446 if (sc->sc_ncm.tx_nframe == 0 ||
447 sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
448 DPRINTFN(1, "Using default max "
449 "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
450 /* need to reserve one entry for zero padding */
451 sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
452 }
453
454 /* Additional configuration, will fail in device side mode, which is OK. */
455
456 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
457 req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
458 USETW(req.wValue, 0);
459 req.wIndex[0] = sc->sc_ifaces_index[1];
460 req.wIndex[1] = 0;
461
462 if (ufd != NULL &&
463 (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
464 USETW(req.wLength, 8);
465 USETDW(value, sc->sc_ncm.rx_max);
466 USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
467 USETW(value + 6, 0);
468 } else {
469 USETW(req.wLength, 4);
470 USETDW(value, sc->sc_ncm.rx_max);
471 }
472
473 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
474 &value, 0, NULL, 1000 /* ms */);
475 if (err) {
476 DPRINTFN(1, "Setting input size "
477 "to %u failed.\n", sc->sc_ncm.rx_max);
478 }
479
480 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
481 req.bRequest = UCDC_NCM_SET_CRC_MODE;
482 USETW(req.wValue, 0); /* no CRC */
483 req.wIndex[0] = sc->sc_ifaces_index[1];
484 req.wIndex[1] = 0;
485 USETW(req.wLength, 0);
486
487 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
488 NULL, 0, NULL, 1000 /* ms */);
489 if (err) {
490 DPRINTFN(1, "Setting CRC mode to off failed.\n");
491 }
492
493 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
494 req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
495 USETW(req.wValue, 0); /* NTB-16 */
496 req.wIndex[0] = sc->sc_ifaces_index[1];
497 req.wIndex[1] = 0;
498 USETW(req.wLength, 0);
499
500 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
501 NULL, 0, NULL, 1000 /* ms */);
502 if (err) {
503 DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
504 }
505
506 return (0); /* success */
507 }
508 #endif
509
510 static void
cdce_test_autoinst(void * arg,struct usb_device * udev,struct usb_attach_arg * uaa)511 cdce_test_autoinst(void *arg, struct usb_device *udev,
512 struct usb_attach_arg *uaa)
513 {
514 struct usb_interface *iface;
515 struct usb_interface_descriptor *id;
516
517 if (uaa->dev_state != UAA_DEV_READY)
518 return;
519
520 iface = usbd_get_iface(udev, 0);
521 if (iface == NULL)
522 return;
523 id = iface->idesc;
524 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
525 return;
526 if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
527 return; /* no device match */
528
529 if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
530 /* success, mark the udev as disappearing */
531 uaa->dev_state = UAA_DEV_EJECTING;
532 }
533 }
534
535 static int
cdce_driver_loaded(struct module * mod,int what,void * arg)536 cdce_driver_loaded(struct module *mod, int what, void *arg)
537 {
538 switch (what) {
539 case MOD_LOAD:
540 /* register our autoinstall handler */
541 cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
542 cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
543 return (0);
544 case MOD_UNLOAD:
545 EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
546 return (0);
547 default:
548 return (EOPNOTSUPP);
549 }
550 }
551
552 static int
cdce_probe(device_t dev)553 cdce_probe(device_t dev)
554 {
555 struct usb_attach_arg *uaa = device_get_ivars(dev);
556 int error;
557
558 error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
559 if (error)
560 error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
561 return (error);
562 }
563
564 static void
cdce_attach_post(struct usb_ether * ue)565 cdce_attach_post(struct usb_ether *ue)
566 {
567 /* no-op */
568 return;
569 }
570
571 static int
cdce_attach_post_sub(struct usb_ether * ue)572 cdce_attach_post_sub(struct usb_ether *ue)
573 {
574 struct cdce_softc *sc = uether_getsc(ue);
575 struct ifnet *ifp = uether_getifp(ue);
576
577 /* mostly copied from usb_ethernet.c */
578 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
579 ifp->if_start = uether_start;
580 ifp->if_ioctl = cdce_ioctl;
581 ifp->if_init = uether_init;
582 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
583 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
584 IFQ_SET_READY(&ifp->if_snd);
585
586 if ((sc->sc_flags & CDCE_FLAG_VLAN) == CDCE_FLAG_VLAN)
587 if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
588
589 if_setcapabilitiesbit(ifp, IFCAP_LINKSTATE, 0);
590 if_setcapenable(ifp, if_getcapabilities(ifp));
591
592 ifmedia_init(&sc->sc_media, IFM_IMASK, cdce_media_change_cb,
593 cdce_media_status_cb);
594 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
595 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
596 sc->sc_media.ifm_media = sc->sc_media.ifm_cur->ifm_media;
597 CDCE_LOCK(sc);
598 cdce_set_filter(ue);
599 CDCE_UNLOCK(sc);
600
601 return 0;
602 }
603
604 static int
cdce_attach(device_t dev)605 cdce_attach(device_t dev)
606 {
607 struct cdce_softc *sc = device_get_softc(dev);
608 struct usb_ether *ue = &sc->sc_ue;
609 struct usb_attach_arg *uaa = device_get_ivars(dev);
610 struct usb_interface *iface;
611 const struct usb_cdc_union_descriptor *ud;
612 const struct usb_interface_descriptor *id;
613 const struct usb_cdc_ethernet_descriptor *ued;
614 const struct usb_config *pcfg;
615 uint32_t seed;
616 int error;
617 uint8_t i;
618 uint8_t data_iface_no;
619 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */
620
621 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
622 sc->sc_ue.ue_udev = uaa->device;
623
624 device_set_usb_desc(dev);
625
626 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
627
628 ud = usbd_find_descriptor
629 (uaa->device, NULL, uaa->info.bIfaceIndex,
630 UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF);
631
632 if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
633 (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
634 DPRINTFN(1, "No union descriptor!\n");
635 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
636 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
637 goto alloc_transfers;
638 }
639 data_iface_no = ud->bSlaveInterface[0];
640
641 for (i = 0;; i++) {
642 iface = usbd_get_iface(uaa->device, i);
643
644 if (iface) {
645 id = usbd_get_interface_descriptor(iface);
646
647 if (id && (id->bInterfaceNumber == data_iface_no)) {
648 sc->sc_ifaces_index[0] = i;
649 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
650 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
651 break;
652 }
653 } else {
654 device_printf(dev, "no data interface found\n");
655 goto detach;
656 }
657 }
658
659 /*
660 * <quote>
661 *
662 * The Data Class interface of a networking device shall have
663 * a minimum of two interface settings. The first setting
664 * (the default interface setting) includes no endpoints and
665 * therefore no networking traffic is exchanged whenever the
666 * default interface setting is selected. One or more
667 * additional interface settings are used for normal
668 * operation, and therefore each includes a pair of endpoints
669 * (one IN, and one OUT) to exchange network traffic. Select
670 * an alternate interface setting to initialize the network
671 * aspects of the device and to enable the exchange of
672 * network traffic.
673 *
674 * </quote>
675 *
676 * Some devices, most notably cable modems, include interface
677 * settings that have no IN or OUT endpoint, therefore loop
678 * through the list of all available interface settings
679 * looking for one with both IN and OUT endpoints.
680 */
681
682 alloc_transfers:
683
684 pcfg = cdce_config; /* Default Configuration */
685
686 for (i = 0; i != 32; i++) {
687 error = usbd_set_alt_interface_index(uaa->device,
688 sc->sc_ifaces_index[0], i);
689 if (error)
690 break;
691 #if CDCE_HAVE_NCM
692 if ((i == 0) && (cdce_ncm_init(sc) == 0))
693 pcfg = cdce_ncm_config;
694 #endif
695 error = usbd_transfer_setup(uaa->device,
696 sc->sc_ifaces_index, sc->sc_xfer,
697 pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
698
699 if (error == 0)
700 break;
701 }
702
703 if (error || (i == 32)) {
704 device_printf(dev, "No valid alternate "
705 "setting found\n");
706 goto detach;
707 }
708
709 ued = usbd_find_descriptor
710 (uaa->device, NULL, uaa->info.bIfaceIndex,
711 UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF);
712
713 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
714 error = USB_ERR_INVAL;
715 } else {
716 /*
717 * ECM 1.2 doesn't say it excludes the CRC, but states that it's
718 * normally 1514, which excludes the CRC.
719 */
720 DPRINTF("max segsize: %d\n", UGETW(ued->wMaxSegmentSize));
721 if (UGETW(ued->wMaxSegmentSize) >= (ETHER_MAX_LEN - ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN))
722 sc->sc_flags |= CDCE_FLAG_VLAN;
723
724 error = usbd_req_get_string_any(uaa->device, NULL,
725 eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
726 }
727
728 if (error) {
729 /* fake MAC address */
730
731 device_printf(dev, "faking MAC address\n");
732 seed = ticks;
733 sc->sc_ue.ue_eaddr[0] = 0x2a;
734 memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
735 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
736
737 } else {
738 memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
739
740 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
741 char c = eaddr_str[i];
742
743 if ('0' <= c && c <= '9')
744 c -= '0';
745 else if (c != 0)
746 c -= 'A' - 10;
747 else
748 break;
749
750 c &= 0xf;
751
752 if ((i & 1) == 0)
753 c <<= 4;
754 sc->sc_ue.ue_eaddr[i / 2] |= c;
755 }
756
757 if (uaa->usb_mode == USB_MODE_DEVICE) {
758 /*
759 * Do not use the same MAC address like the peer !
760 */
761 sc->sc_ue.ue_eaddr[5] ^= 0xFF;
762 }
763 }
764
765 ue->ue_sc = sc;
766 ue->ue_dev = dev;
767 ue->ue_udev = uaa->device;
768 ue->ue_mtx = &sc->sc_mtx;
769 ue->ue_methods = &cdce_ue_methods;
770
771 error = uether_ifattach(ue);
772 if (error) {
773 device_printf(dev, "could not attach interface\n");
774 goto detach;
775 }
776 return (0); /* success */
777
778 detach:
779 cdce_detach(dev);
780 return (ENXIO); /* failure */
781 }
782
783 static int
cdce_detach(device_t dev)784 cdce_detach(device_t dev)
785 {
786 struct cdce_softc *sc = device_get_softc(dev);
787 struct usb_ether *ue = &sc->sc_ue;
788
789 /* stop all USB transfers first */
790 usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
791 uether_ifdetach(ue);
792 mtx_destroy(&sc->sc_mtx);
793
794 ifmedia_removeall(&sc->sc_media);
795
796 return (0);
797 }
798
799 static void
cdce_start(struct usb_ether * ue)800 cdce_start(struct usb_ether *ue)
801 {
802 struct cdce_softc *sc = uether_getsc(ue);
803
804 /*
805 * Start the USB transfers, if not already started:
806 */
807 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
808 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
809 }
810
811 static int
cdce_ioctl(struct ifnet * ifp,u_long command,caddr_t data)812 cdce_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
813 {
814 struct usb_ether *ue = ifp->if_softc;
815 struct cdce_softc *sc = uether_getsc(ue);
816 struct ifreq *ifr = (struct ifreq *)data;
817 int error;
818
819 error = 0;
820
821 switch(command) {
822 case SIOCGIFMEDIA:
823 case SIOCSIFMEDIA:
824 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
825 break;
826 default:
827 error = uether_ioctl(ifp, command, data);
828 break;
829 }
830
831 return (error);
832 }
833
834 static void
cdce_free_queue(struct mbuf ** ppm,uint8_t n)835 cdce_free_queue(struct mbuf **ppm, uint8_t n)
836 {
837 uint8_t x;
838 for (x = 0; x != n; x++) {
839 if (ppm[x] != NULL) {
840 m_freem(ppm[x]);
841 ppm[x] = NULL;
842 }
843 }
844 }
845
846 static int
cdce_media_change_cb(struct ifnet * ifp)847 cdce_media_change_cb(struct ifnet *ifp)
848 {
849
850 return (EOPNOTSUPP);
851 }
852
853 static void
cdce_media_status_cb(struct ifnet * ifp,struct ifmediareq * ifmr)854 cdce_media_status_cb(struct ifnet *ifp, struct ifmediareq *ifmr)
855 {
856
857 if ((if_getflags(ifp) & IFF_UP) == 0)
858 return;
859
860 ifmr->ifm_active = IFM_ETHER;
861 ifmr->ifm_status = IFM_AVALID;
862 ifmr->ifm_status |=
863 ifp->if_link_state == LINK_STATE_UP ? IFM_ACTIVE : 0;
864 }
865
866 static void
cdce_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)867 cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
868 {
869 struct cdce_softc *sc = usbd_xfer_softc(xfer);
870 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
871 struct mbuf *m;
872 struct mbuf *mt;
873 uint32_t crc;
874 uint8_t x;
875 int actlen, aframes;
876
877 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
878
879 DPRINTFN(1, "\n");
880
881 switch (USB_GET_STATE(xfer)) {
882 case USB_ST_TRANSFERRED:
883 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
884 actlen, aframes);
885
886 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
887
888 /* free all previous TX buffers */
889 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
890
891 /* FALLTHROUGH */
892 case USB_ST_SETUP:
893 tr_setup:
894 for (x = 0; x != CDCE_FRAMES_MAX; x++) {
895 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
896
897 if (m == NULL)
898 break;
899
900 if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
901 /*
902 * Zaurus wants a 32-bit CRC appended
903 * to every frame
904 */
905
906 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
907 crc = htole32(crc);
908
909 if (!m_append(m, 4, (void *)&crc)) {
910 m_freem(m);
911 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
912 continue;
913 }
914 }
915 if (m->m_len != m->m_pkthdr.len) {
916 mt = m_defrag(m, M_NOWAIT);
917 if (mt == NULL) {
918 m_freem(m);
919 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
920 continue;
921 }
922 m = mt;
923 }
924 if (m->m_pkthdr.len > MCLBYTES) {
925 m->m_pkthdr.len = MCLBYTES;
926 }
927 sc->sc_tx_buf[x] = m;
928 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
929
930 /*
931 * If there's a BPF listener, bounce a copy of
932 * this frame to him:
933 */
934 BPF_MTAP(ifp, m);
935 }
936 if (x != 0) {
937 usbd_xfer_set_frames(xfer, x);
938
939 usbd_transfer_submit(xfer);
940 }
941 break;
942
943 default: /* Error */
944 DPRINTFN(11, "transfer error, %s\n",
945 usbd_errstr(error));
946
947 /* free all previous TX buffers */
948 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
949
950 /* count output errors */
951 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
952
953 if (error != USB_ERR_CANCELLED) {
954 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
955 /* try to clear stall first */
956 usbd_xfer_set_stall(xfer);
957 }
958 goto tr_setup;
959 }
960 break;
961 }
962 }
963
964 static int32_t
cdce_m_crc32_cb(void * arg,void * src,uint32_t count)965 cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
966 {
967 uint32_t *p_crc = arg;
968
969 *p_crc = crc32_raw(src, count, *p_crc);
970 return (0);
971 }
972
973 static uint32_t
cdce_m_crc32(struct mbuf * m,uint32_t src_offset,uint32_t src_len)974 cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
975 {
976 uint32_t crc = 0xFFFFFFFF;
977
978 (void)m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
979 return (crc ^ 0xFFFFFFFF);
980 }
981
982 static void
cdce_init(struct usb_ether * ue)983 cdce_init(struct usb_ether *ue)
984 {
985 struct cdce_softc *sc = uether_getsc(ue);
986 struct ifnet *ifp = uether_getifp(ue);
987
988 CDCE_LOCK_ASSERT(sc, MA_OWNED);
989
990 ifp->if_drv_flags |= IFF_DRV_RUNNING;
991
992 /* start interrupt transfer */
993 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
994 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
995
996 /*
997 * Stall data write direction, which depends on USB mode.
998 *
999 * Some USB host stacks (e.g. Mac OS X) don't clears stall
1000 * bit as it should, so set it in our host mode only.
1001 */
1002 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1003 usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
1004
1005 /* start data transfers */
1006 cdce_start(ue);
1007 }
1008
1009 static void
cdce_stop(struct usb_ether * ue)1010 cdce_stop(struct usb_ether *ue)
1011 {
1012 struct cdce_softc *sc = uether_getsc(ue);
1013 struct ifnet *ifp = uether_getifp(ue);
1014
1015 CDCE_LOCK_ASSERT(sc, MA_OWNED);
1016
1017 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1018
1019 /*
1020 * stop all the transfers, if not already stopped:
1021 */
1022 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
1023 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
1024 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
1025 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
1026 }
1027
1028 static void
cdce_setmulti(struct usb_ether * ue)1029 cdce_setmulti(struct usb_ether *ue)
1030 {
1031
1032 cdce_set_filter(ue);
1033 }
1034
1035 static void
cdce_setpromisc(struct usb_ether * ue)1036 cdce_setpromisc(struct usb_ether *ue)
1037 {
1038
1039 cdce_set_filter(ue);
1040 }
1041
1042 static void
cdce_set_filter(struct usb_ether * ue)1043 cdce_set_filter(struct usb_ether *ue)
1044 {
1045 struct cdce_softc *sc = uether_getsc(ue);
1046 struct ifnet *ifp = uether_getifp(ue);
1047 struct usb_device_request req;
1048 uint16_t value;
1049
1050 value = CDC_PACKET_TYPE_DIRECTED | CDC_PACKET_TYPE_BROADCAST;
1051 if (if_getflags(ifp) & IFF_PROMISC)
1052 value |= CDC_PACKET_TYPE_PROMISC;
1053 if (if_getflags(ifp) & IFF_ALLMULTI)
1054 value |= CDC_PACKET_TYPE_ALL_MULTICAST;
1055
1056 req.bmRequestType = UT_CLASS | UT_INTERFACE;
1057 req.bRequest = CDC_SET_ETHERNET_PACKET_FILTER;
1058 USETW(req.wValue, value);
1059 req.wIndex[0] = sc->sc_ifaces_index[1];
1060 req.wIndex[1] = 0;
1061 USETW(req.wLength, 0);
1062
1063 /*
1064 * Function below will drop the sc mutex.
1065 * We can do that since we're called from a separate task,
1066 * that simply wraps the setpromisc/setmulti methods.
1067 */
1068 usbd_do_request(sc->sc_ue.ue_udev, &sc->sc_mtx, &req, NULL);
1069 }
1070
1071 static int
cdce_suspend(device_t dev)1072 cdce_suspend(device_t dev)
1073 {
1074 device_printf(dev, "Suspending\n");
1075 return (0);
1076 }
1077
1078 static int
cdce_resume(device_t dev)1079 cdce_resume(device_t dev)
1080 {
1081 device_printf(dev, "Resuming\n");
1082 return (0);
1083 }
1084
1085 static void
cdce_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)1086 cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1087 {
1088 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1089 struct mbuf *m;
1090 uint8_t x;
1091 int actlen;
1092 int aframes;
1093 int len;
1094
1095 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
1096
1097 switch (USB_GET_STATE(xfer)) {
1098 case USB_ST_TRANSFERRED:
1099
1100 DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
1101
1102 for (x = 0; x != aframes; x++) {
1103 m = sc->sc_rx_buf[x];
1104 sc->sc_rx_buf[x] = NULL;
1105 len = usbd_xfer_frame_len(xfer, x);
1106
1107 /* Strip off CRC added by Zaurus, if any */
1108 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
1109 len -= 4;
1110
1111 if (len < (int)sizeof(struct ether_header)) {
1112 m_freem(m);
1113 continue;
1114 }
1115 /* queue up mbuf */
1116 uether_rxmbuf(&sc->sc_ue, m, len);
1117 }
1118
1119 /* FALLTHROUGH */
1120 case USB_ST_SETUP:
1121 /*
1122 * TODO: Implement support for multi frame transfers,
1123 * when the USB hardware supports it.
1124 */
1125 for (x = 0; x != 1; x++) {
1126 if (sc->sc_rx_buf[x] == NULL) {
1127 m = uether_newbuf();
1128 if (m == NULL)
1129 goto tr_stall;
1130 sc->sc_rx_buf[x] = m;
1131 } else {
1132 m = sc->sc_rx_buf[x];
1133 }
1134
1135 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
1136 }
1137 /* set number of frames and start hardware */
1138 usbd_xfer_set_frames(xfer, x);
1139 usbd_transfer_submit(xfer);
1140 /* flush any received frames */
1141 uether_rxflush(&sc->sc_ue);
1142 break;
1143
1144 default: /* Error */
1145 DPRINTF("error = %s\n",
1146 usbd_errstr(error));
1147
1148 if (error != USB_ERR_CANCELLED) {
1149 tr_stall:
1150 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1151 /* try to clear stall first */
1152 usbd_xfer_set_stall(xfer);
1153 usbd_xfer_set_frames(xfer, 0);
1154 usbd_transfer_submit(xfer);
1155 }
1156 break;
1157 }
1158
1159 /* need to free the RX-mbufs when we are cancelled */
1160 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
1161 break;
1162 }
1163 }
1164
1165 static void
cdce_intr_read_callback(struct usb_xfer * xfer,usb_error_t error)1166 cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
1167 {
1168 u_char buf[CDCE_IND_SIZE_MAX];
1169 struct usb_cdc_notification ucn;
1170 struct cdce_softc *sc;
1171 struct ifnet *ifp;
1172 struct usb_page_cache *pc;
1173 int off, actlen;
1174 uint32_t downrate, uprate;
1175
1176 sc = usbd_xfer_softc(xfer);
1177 ifp = uether_getifp(&sc->sc_ue);
1178 pc = usbd_xfer_get_frame(xfer, 0);
1179
1180 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1181
1182 switch (USB_GET_STATE(xfer)) {
1183 case USB_ST_TRANSFERRED:
1184 if (USB_DEBUG_VAR)
1185 usbd_copy_out(pc, 0, buf, MIN(actlen, sizeof buf));
1186 DPRINTF("Received %d bytes: %*D\n", actlen,
1187 (int)MIN(actlen, sizeof buf), buf, "");
1188
1189 off = 0;
1190 while (actlen - off >= UCDC_NOTIFICATION_LENGTH) {
1191 usbd_copy_out(pc, off, &ucn, UCDC_NOTIFICATION_LENGTH);
1192
1193 do {
1194 if (ucn.bmRequestType != 0xa1)
1195 break;
1196
1197 switch (ucn.bNotification) {
1198 case UCDC_N_NETWORK_CONNECTION:
1199 DPRINTF("changing link state: %d\n",
1200 UGETW(ucn.wValue));
1201 if_link_state_change(ifp,
1202 UGETW(ucn.wValue) ? LINK_STATE_UP :
1203 LINK_STATE_DOWN);
1204 break;
1205
1206 case UCDC_N_CONNECTION_SPEED_CHANGE:
1207 if (UGETW(ucn.wLength) != 8)
1208 break;
1209
1210 usbd_copy_out(pc, off +
1211 UCDC_NOTIFICATION_LENGTH,
1212 &ucn.data, UGETW(ucn.wLength));
1213 downrate = UGETDW(ucn.data);
1214 uprate = UGETDW(ucn.data);
1215 if (downrate != uprate)
1216 break;
1217
1218 /* set rate */
1219 DPRINTF("changing baudrate: %u\n",
1220 downrate);
1221 if_setbaudrate(ifp, downrate);
1222 break;
1223
1224 default:
1225 break;
1226 }
1227 } while (0);
1228
1229 off += UCDC_NOTIFICATION_LENGTH + UGETW(ucn.wLength);
1230 }
1231
1232 /* FALLTHROUGH */
1233 case USB_ST_SETUP:
1234 tr_setup:
1235 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1236 usbd_transfer_submit(xfer);
1237 break;
1238
1239 default: /* Error */
1240 if (error != USB_ERR_CANCELLED) {
1241 /* start clear stall */
1242 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1243 usbd_xfer_set_stall(xfer);
1244 goto tr_setup;
1245 }
1246 break;
1247 }
1248 }
1249
1250 static void
cdce_intr_write_callback(struct usb_xfer * xfer,usb_error_t error)1251 cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
1252 {
1253 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1254 struct usb_cdc_notification req;
1255 struct usb_page_cache *pc;
1256 uint32_t speed;
1257 int actlen;
1258
1259 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1260
1261 switch (USB_GET_STATE(xfer)) {
1262 case USB_ST_TRANSFERRED:
1263
1264 DPRINTF("Transferred %d bytes\n", actlen);
1265
1266 switch (sc->sc_notify_state) {
1267 case CDCE_NOTIFY_NETWORK_CONNECTION:
1268 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1269 break;
1270 case CDCE_NOTIFY_SPEED_CHANGE:
1271 sc->sc_notify_state = CDCE_NOTIFY_DONE;
1272 break;
1273 default:
1274 break;
1275 }
1276
1277 /* FALLTHROUGH */
1278 case USB_ST_SETUP:
1279 tr_setup:
1280 /*
1281 * Inform host about connection. Required according to USB CDC
1282 * specification and communicating to Mac OS X USB host stack.
1283 * Some of the values seems ignored by Mac OS X though.
1284 */
1285 if (sc->sc_notify_state == CDCE_NOTIFY_NETWORK_CONNECTION) {
1286 req.bmRequestType = UCDC_NOTIFICATION;
1287 req.bNotification = UCDC_N_NETWORK_CONNECTION;
1288 req.wIndex[0] = sc->sc_ifaces_index[1];
1289 req.wIndex[1] = 0;
1290 USETW(req.wValue, 1); /* Connected */
1291 USETW(req.wLength, 0);
1292
1293 pc = usbd_xfer_get_frame(xfer, 0);
1294 usbd_copy_in(pc, 0, &req, sizeof(req));
1295 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1296 usbd_xfer_set_frames(xfer, 1);
1297 usbd_transfer_submit(xfer);
1298
1299 } else if (sc->sc_notify_state == CDCE_NOTIFY_SPEED_CHANGE) {
1300 req.bmRequestType = UCDC_NOTIFICATION;
1301 req.bNotification = UCDC_N_CONNECTION_SPEED_CHANGE;
1302 req.wIndex[0] = sc->sc_ifaces_index[1];
1303 req.wIndex[1] = 0;
1304 USETW(req.wValue, 0);
1305 USETW(req.wLength, 8);
1306
1307 /* Peak theoretical bulk trasfer rate in bits/s */
1308 if (usbd_get_speed(sc->sc_ue.ue_udev) != USB_SPEED_FULL)
1309 speed = (13 * 512 * 8 * 1000 * 8);
1310 else
1311 speed = (19 * 64 * 1 * 1000 * 8);
1312
1313 USETDW(req.data + 0, speed); /* Upstream bit rate */
1314 USETDW(req.data + 4, speed); /* Downstream bit rate */
1315
1316 pc = usbd_xfer_get_frame(xfer, 0);
1317 usbd_copy_in(pc, 0, &req, sizeof(req));
1318 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1319 usbd_xfer_set_frames(xfer, 1);
1320 usbd_transfer_submit(xfer);
1321 }
1322 break;
1323
1324 default: /* Error */
1325 if (error != USB_ERR_CANCELLED) {
1326 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1327 /* start clear stall */
1328 usbd_xfer_set_stall(xfer);
1329 }
1330 goto tr_setup;
1331 }
1332 break;
1333 }
1334 }
1335
1336 static int
cdce_handle_request(device_t dev,const void * preq,void ** pptr,uint16_t * plen,uint16_t offset,uint8_t * pstate)1337 cdce_handle_request(device_t dev,
1338 const void *preq, void **pptr, uint16_t *plen,
1339 uint16_t offset, uint8_t *pstate)
1340 {
1341 struct cdce_softc *sc = device_get_softc(dev);
1342 const struct usb_device_request *req = preq;
1343 uint8_t is_complete = *pstate;
1344
1345 /*
1346 * When Mac OS X resumes after suspending it expects
1347 * to be notified again after this request.
1348 */
1349 if (req->bmRequestType == UT_WRITE_CLASS_INTERFACE && \
1350 req->bRequest == UCDC_NCM_SET_ETHERNET_PACKET_FILTER) {
1351 if (is_complete == 1) {
1352 mtx_lock(&sc->sc_mtx);
1353 sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1354 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
1355 mtx_unlock(&sc->sc_mtx);
1356 }
1357
1358 return (0);
1359 }
1360
1361 return (ENXIO); /* use builtin handler */
1362 }
1363
1364 #if CDCE_HAVE_NCM
1365 static void
cdce_ncm_tx_zero(struct usb_page_cache * pc,uint32_t start,uint32_t end)1366 cdce_ncm_tx_zero(struct usb_page_cache *pc,
1367 uint32_t start, uint32_t end)
1368 {
1369 if (start >= CDCE_NCM_TX_MAXLEN)
1370 return;
1371 if (end > CDCE_NCM_TX_MAXLEN)
1372 end = CDCE_NCM_TX_MAXLEN;
1373
1374 usbd_frame_zero(pc, start, end - start);
1375 }
1376
1377 static uint8_t
cdce_ncm_fill_tx_frames(struct usb_xfer * xfer,uint8_t index)1378 cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
1379 {
1380 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1381 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1382 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
1383 struct mbuf *m;
1384 uint32_t rem;
1385 uint32_t offset;
1386 uint32_t last_offset;
1387 uint16_t n;
1388 uint8_t retval;
1389
1390 usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
1391
1392 offset = sizeof(sc->sc_ncm.hdr) +
1393 sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
1394
1395 /* Store last valid offset before alignment */
1396 last_offset = offset;
1397
1398 /* Align offset */
1399 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1400 offset, sc->sc_ncm.tx_modulus);
1401
1402 /* Zero pad */
1403 cdce_ncm_tx_zero(pc, last_offset, offset);
1404
1405 /* buffer full */
1406 retval = 2;
1407
1408 for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
1409 /* check if end of transmit buffer is reached */
1410
1411 if (offset >= sc->sc_ncm.tx_max)
1412 break;
1413
1414 /* compute maximum buffer size */
1415
1416 rem = sc->sc_ncm.tx_max - offset;
1417
1418 IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
1419
1420 if (m == NULL) {
1421 /* buffer not full */
1422 retval = 1;
1423 break;
1424 }
1425
1426 if (m->m_pkthdr.len > (int)rem) {
1427 if (n == 0) {
1428 /* The frame won't fit in our buffer */
1429 DPRINTFN(1, "Frame too big to be transmitted!\n");
1430 m_freem(m);
1431 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1432 n--;
1433 continue;
1434 }
1435 /* Wait till next buffer becomes ready */
1436 IFQ_DRV_PREPEND(&(ifp->if_snd), m);
1437 break;
1438 }
1439 usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
1440
1441 USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
1442 USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
1443
1444 /* Update offset */
1445 offset += m->m_pkthdr.len;
1446
1447 /* Store last valid offset before alignment */
1448 last_offset = offset;
1449
1450 /* Align offset */
1451 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1452 offset, sc->sc_ncm.tx_modulus);
1453
1454 /* Zero pad */
1455 cdce_ncm_tx_zero(pc, last_offset, offset);
1456
1457 /*
1458 * If there's a BPF listener, bounce a copy
1459 * of this frame to him:
1460 */
1461 BPF_MTAP(ifp, m);
1462
1463 /* Free mbuf */
1464
1465 m_freem(m);
1466
1467 /* Pre-increment interface counter */
1468
1469 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1470 }
1471
1472 if (n == 0)
1473 return (0);
1474
1475 rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
1476
1477 USETW(sc->sc_ncm.dpt.wLength, rem);
1478
1479 /* zero the rest of the data pointer entries */
1480 for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
1481 USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
1482 USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
1483 }
1484
1485 offset = last_offset;
1486
1487 /* Align offset */
1488 offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
1489
1490 /* Optimise, save bandwidth and force short termination */
1491 if (offset >= sc->sc_ncm.tx_max)
1492 offset = sc->sc_ncm.tx_max;
1493 else
1494 offset ++;
1495
1496 /* Zero pad */
1497 cdce_ncm_tx_zero(pc, last_offset, offset);
1498
1499 /* set frame length */
1500 usbd_xfer_set_frame_len(xfer, index, offset);
1501
1502 /* Fill out 16-bit header */
1503 sc->sc_ncm.hdr.dwSignature[0] = 'N';
1504 sc->sc_ncm.hdr.dwSignature[1] = 'C';
1505 sc->sc_ncm.hdr.dwSignature[2] = 'M';
1506 sc->sc_ncm.hdr.dwSignature[3] = 'H';
1507 USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
1508 USETW(sc->sc_ncm.hdr.wBlockLength, offset);
1509 USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
1510 USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
1511
1512 sc->sc_ncm.tx_seq++;
1513
1514 /* Fill out 16-bit frame table header */
1515 sc->sc_ncm.dpt.dwSignature[0] = 'N';
1516 sc->sc_ncm.dpt.dwSignature[1] = 'C';
1517 sc->sc_ncm.dpt.dwSignature[2] = 'M';
1518 sc->sc_ncm.dpt.dwSignature[3] = '0';
1519 USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0); /* reserved */
1520
1521 usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
1522 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
1523 sizeof(sc->sc_ncm.dpt));
1524 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
1525 &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
1526 return (retval);
1527 }
1528
1529 static void
cdce_ncm_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)1530 cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1531 {
1532 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1533 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1534 uint16_t x;
1535 uint8_t temp;
1536 int actlen;
1537 int aframes;
1538
1539 switch (USB_GET_STATE(xfer)) {
1540 case USB_ST_TRANSFERRED:
1541
1542 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
1543
1544 DPRINTFN(10, "transfer complete: "
1545 "%u bytes in %u frames\n", actlen, aframes);
1546
1547 case USB_ST_SETUP:
1548 for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
1549 temp = cdce_ncm_fill_tx_frames(xfer, x);
1550 if (temp == 0)
1551 break;
1552 if (temp == 1) {
1553 x++;
1554 break;
1555 }
1556 }
1557
1558 if (x != 0) {
1559 #ifdef USB_DEBUG
1560 usbd_xfer_set_interval(xfer, cdce_tx_interval);
1561 #endif
1562 usbd_xfer_set_frames(xfer, x);
1563 usbd_transfer_submit(xfer);
1564 }
1565 break;
1566
1567 default: /* Error */
1568 DPRINTFN(10, "Transfer error: %s\n",
1569 usbd_errstr(error));
1570
1571 /* update error counter */
1572 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1573
1574 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);
1580 }
1581 }
1582 break;
1583 }
1584 }
1585
1586 static void
cdce_ncm_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)1587 cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1588 {
1589 struct cdce_softc *sc = usbd_xfer_softc(xfer);
1590 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
1591 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1592 struct mbuf *m;
1593 int sumdata;
1594 int sumlen;
1595 int actlen;
1596 int aframes;
1597 int temp;
1598 int nframes;
1599 int x;
1600 int offset;
1601
1602 switch (USB_GET_STATE(xfer)) {
1603 case USB_ST_TRANSFERRED:
1604
1605 usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
1606
1607 DPRINTFN(1, "received %u bytes in %u frames\n",
1608 actlen, aframes);
1609
1610 if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
1611 sizeof(sc->sc_ncm.dpt))) {
1612 DPRINTFN(1, "frame too short\n");
1613 goto tr_setup;
1614 }
1615 usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
1616 sizeof(sc->sc_ncm.hdr));
1617
1618 if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
1619 (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
1620 (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
1621 (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
1622 DPRINTFN(1, "invalid HDR signature: "
1623 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1624 sc->sc_ncm.hdr.dwSignature[0],
1625 sc->sc_ncm.hdr.dwSignature[1],
1626 sc->sc_ncm.hdr.dwSignature[2],
1627 sc->sc_ncm.hdr.dwSignature[3]);
1628 goto tr_stall;
1629 }
1630 temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
1631 if (temp > sumlen) {
1632 DPRINTFN(1, "unsupported block length %u/%u\n",
1633 temp, sumlen);
1634 goto tr_stall;
1635 }
1636 temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
1637 if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
1638 DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
1639 goto tr_stall;
1640 }
1641 usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
1642 sizeof(sc->sc_ncm.dpt));
1643
1644 if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
1645 (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
1646 (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
1647 (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
1648 DPRINTFN(1, "invalid DPT signature"
1649 "0x%02x:0x%02x:0x%02x:0x%02x\n",
1650 sc->sc_ncm.dpt.dwSignature[0],
1651 sc->sc_ncm.dpt.dwSignature[1],
1652 sc->sc_ncm.dpt.dwSignature[2],
1653 sc->sc_ncm.dpt.dwSignature[3]);
1654 goto tr_stall;
1655 }
1656 nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
1657
1658 /* Subtract size of header and last zero padded entry */
1659 if (nframes >= (2 + 1))
1660 nframes -= (2 + 1);
1661 else
1662 nframes = 0;
1663
1664 DPRINTFN(1, "nframes = %u\n", nframes);
1665
1666 temp += sizeof(sc->sc_ncm.dpt);
1667
1668 if ((temp + (4 * nframes)) > actlen)
1669 goto tr_stall;
1670
1671 if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
1672 DPRINTFN(1, "Truncating number of frames from %u to %u\n",
1673 nframes, CDCE_NCM_SUBFRAMES_MAX);
1674 nframes = CDCE_NCM_SUBFRAMES_MAX;
1675 }
1676 usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
1677
1678 sumdata = 0;
1679
1680 for (x = 0; x != nframes; x++) {
1681 offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
1682 temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
1683
1684 if ((offset == 0) ||
1685 (temp < (int)sizeof(struct ether_header)) ||
1686 (temp > (MCLBYTES - ETHER_ALIGN))) {
1687 DPRINTFN(1, "NULL frame detected at %d\n", x);
1688 m = NULL;
1689 /* silently ignore this frame */
1690 continue;
1691 } else if ((offset + temp) > actlen) {
1692 DPRINTFN(1, "invalid frame "
1693 "detected at %d\n", x);
1694 m = NULL;
1695 /* silently ignore this frame */
1696 continue;
1697 } else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1698 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1699 } else {
1700 m = m_gethdr(M_NOWAIT, MT_DATA);
1701 }
1702
1703 DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
1704 x, offset, temp);
1705
1706 /* check if we have a buffer */
1707 if (m) {
1708 m->m_len = m->m_pkthdr.len = temp + ETHER_ALIGN;
1709 m_adj(m, ETHER_ALIGN);
1710
1711 usbd_copy_out(pc, offset, m->m_data, temp);
1712
1713 /* enqueue */
1714 uether_rxmbuf(&sc->sc_ue, m, temp);
1715
1716 sumdata += temp;
1717 } else {
1718 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1719 }
1720 }
1721
1722 DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
1723
1724 case USB_ST_SETUP:
1725 tr_setup:
1726 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
1727 usbd_xfer_set_frames(xfer, 1);
1728 usbd_transfer_submit(xfer);
1729 uether_rxflush(&sc->sc_ue); /* must be last */
1730 break;
1731
1732 default: /* Error */
1733 DPRINTFN(1, "error = %s\n",
1734 usbd_errstr(error));
1735
1736 if (error != USB_ERR_CANCELLED) {
1737 tr_stall:
1738 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1739 /* try to clear stall first */
1740 usbd_xfer_set_stall(xfer);
1741 usbd_xfer_set_frames(xfer, 0);
1742 usbd_transfer_submit(xfer);
1743 }
1744 }
1745 break;
1746 }
1747 }
1748 #endif
1749