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