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