xref: /dragonfly/sys/bus/u4b/net/if_cdce.c (revision 6e5c5008)
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 
50 #include <sys/stdint.h>
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/bus.h>
57 #include <sys/module.h>
58 #include <sys/lock.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
65 #include <sys/priv.h>
66 
67 #include <net/if.h>
68 #include <net/if_var.h>
69 #include <net/ifq_var.h>
70 
71 #include <bus/u4b/usb.h>
72 #include <bus/u4b/usbdi.h>
73 #include <bus/u4b/usbdi_util.h>
74 #include <bus/u4b/usb_cdc.h>
75 #include "usbdevs.h"
76 
77 #define	USB_DEBUG_VAR cdce_debug
78 #include <bus/u4b/usb_debug.h>
79 #include <bus/u4b/usb_process.h>
80 #include <bus/u4b/usb_msctest.h>
81 #include "usb_if.h"
82 
83 #include <bus/u4b/net/usb_ethernet.h>
84 #include <bus/u4b/net/if_cdcereg.h>
85 
86 static device_probe_t cdce_probe;
87 static device_attach_t cdce_attach;
88 static device_detach_t cdce_detach;
89 static device_suspend_t cdce_suspend;
90 static device_resume_t cdce_resume;
91 static usb_handle_request_t cdce_handle_request;
92 
93 static usb_callback_t cdce_bulk_write_callback;
94 static usb_callback_t cdce_bulk_read_callback;
95 static usb_callback_t cdce_intr_read_callback;
96 static usb_callback_t cdce_intr_write_callback;
97 
98 #if CDCE_HAVE_NCM
99 static usb_callback_t cdce_ncm_bulk_write_callback;
100 static usb_callback_t cdce_ncm_bulk_read_callback;
101 #endif
102 
103 static uether_fn_t cdce_attach_post;
104 static uether_fn_t cdce_init;
105 static uether_fn_t cdce_stop;
106 static uether_fn_t cdce_start;
107 static uether_fn_t cdce_setmulti;
108 static uether_fn_t cdce_setpromisc;
109 
110 static uint32_t	cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
111 
112 #ifdef USB_DEBUG
113 static int cdce_debug = 0;
114 static int cdce_tx_interval = 0;
115 
116 static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
117 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0,
118     "Debug level");
119 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RW, &cdce_tx_interval, 0,
120     "NCM transmit interval in ms");
121 #endif
122 
123 static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
124 
125 	[CDCE_BULK_RX] = {
126 		.type = UE_BULK,
127 		.endpoint = UE_ADDR_ANY,
128 		.direction = UE_DIR_RX,
129 		.if_index = 0,
130 		.frames = CDCE_FRAMES_MAX,
131 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
132 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
133 		.callback = cdce_bulk_read_callback,
134 		.timeout = 0,	/* no timeout */
135 		.usb_mode = USB_MODE_DUAL,	/* both modes */
136 	},
137 
138 	[CDCE_BULK_TX] = {
139 		.type = UE_BULK,
140 		.endpoint = UE_ADDR_ANY,
141 		.direction = UE_DIR_TX,
142 		.if_index = 0,
143 		.frames = CDCE_FRAMES_MAX,
144 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
145 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
146 		.callback = cdce_bulk_write_callback,
147 		.timeout = 10000,	/* 10 seconds */
148 		.usb_mode = USB_MODE_DUAL,	/* both modes */
149 	},
150 
151 	[CDCE_INTR_RX] = {
152 		.type = UE_INTERRUPT,
153 		.endpoint = UE_ADDR_ANY,
154 		.direction = UE_DIR_RX,
155 		.if_index = 1,
156 		.bufsize = CDCE_IND_SIZE_MAX,
157 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
158 		.callback = cdce_intr_read_callback,
159 		.timeout = 0,
160 		.usb_mode = USB_MODE_HOST,
161 	},
162 
163 	[CDCE_INTR_TX] = {
164 		.type = UE_INTERRUPT,
165 		.endpoint = UE_ADDR_ANY,
166 		.direction = UE_DIR_TX,
167 		.if_index = 1,
168 		.bufsize = CDCE_IND_SIZE_MAX,
169 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
170 		.callback = cdce_intr_write_callback,
171 		.timeout = 10000,	/* 10 seconds */
172 		.usb_mode = USB_MODE_DEVICE,
173 	},
174 };
175 
176 #if CDCE_HAVE_NCM
177 static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
178 
179 	[CDCE_BULK_RX] = {
180 		.type = UE_BULK,
181 		.endpoint = UE_ADDR_ANY,
182 		.direction = UE_DIR_RX,
183 		.if_index = 0,
184 		.frames = CDCE_NCM_RX_FRAMES_MAX,
185 		.bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
186 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
187 		.callback = cdce_ncm_bulk_read_callback,
188 		.timeout = 0,	/* no timeout */
189 		.usb_mode = USB_MODE_DUAL,	/* both modes */
190 	},
191 
192 	[CDCE_BULK_TX] = {
193 		.type = UE_BULK,
194 		.endpoint = UE_ADDR_ANY,
195 		.direction = UE_DIR_TX,
196 		.if_index = 0,
197 		.frames = CDCE_NCM_TX_FRAMES_MAX,
198 		.bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
199 		.flags = {.pipe_bof = 1,},
200 		.callback = cdce_ncm_bulk_write_callback,
201 		.timeout = 10000,	/* 10 seconds */
202 		.usb_mode = USB_MODE_DUAL,	/* both modes */
203 	},
204 
205 	[CDCE_INTR_RX] = {
206 		.type = UE_INTERRUPT,
207 		.endpoint = UE_ADDR_ANY,
208 		.direction = UE_DIR_RX,
209 		.if_index = 1,
210 		.bufsize = CDCE_IND_SIZE_MAX,
211 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
212 		.callback = cdce_intr_read_callback,
213 		.timeout = 0,
214 		.usb_mode = USB_MODE_HOST,
215 	},
216 
217 	[CDCE_INTR_TX] = {
218 		.type = UE_INTERRUPT,
219 		.endpoint = UE_ADDR_ANY,
220 		.direction = UE_DIR_TX,
221 		.if_index = 1,
222 		.bufsize = CDCE_IND_SIZE_MAX,
223 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
224 		.callback = cdce_intr_write_callback,
225 		.timeout = 10000,	/* 10 seconds */
226 		.usb_mode = USB_MODE_DEVICE,
227 	},
228 };
229 #endif
230 
231 static device_method_t cdce_methods[] = {
232 	/* USB interface */
233 	DEVMETHOD(usb_handle_request, cdce_handle_request),
234 
235 	/* Device interface */
236 	DEVMETHOD(device_probe, cdce_probe),
237 	DEVMETHOD(device_attach, cdce_attach),
238 	DEVMETHOD(device_detach, cdce_detach),
239 	DEVMETHOD(device_suspend, cdce_suspend),
240 	DEVMETHOD(device_resume, cdce_resume),
241 
242 	DEVMETHOD_END
243 };
244 
245 static driver_t cdce_driver = {
246 	.name = "cdce",
247 	.methods = cdce_methods,
248 	.size = sizeof(struct cdce_softc),
249 };
250 
251 static devclass_t cdce_devclass;
252 static eventhandler_tag cdce_etag;
253 
254 static int cdce_driver_loaded(struct module *, int, void *);
255 
256 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, cdce_driver_loaded, NULL);
257 MODULE_VERSION(cdce, 1);
258 MODULE_DEPEND(cdce, uether, 1, 1, 1);
259 MODULE_DEPEND(cdce, usb, 1, 1, 1);
260 MODULE_DEPEND(cdce, ether, 1, 1, 1);
261 
262 static const struct usb_ether_methods cdce_ue_methods = {
263 	.ue_attach_post = cdce_attach_post,
264 	.ue_start = cdce_start,
265 	.ue_init = cdce_init,
266 	.ue_stop = cdce_stop,
267 	.ue_setmulti = cdce_setmulti,
268 	.ue_setpromisc = cdce_setpromisc,
269 };
270 
271 
272 static const STRUCT_USB_HOST_ID cdce_switch_devs[] = {
273     {USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3272_INIT, MSC_EJECT_HUAWEI2)},
274 };
275 
276 static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
277 	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
278 	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
279 	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
280 	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
281 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
282 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
283 	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
284 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
285 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
286 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
287 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
288 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
289 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
290 
291 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
292    USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x16),
293    USB_DRIVER_INFO(0)},
294 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
295    USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x46),
296    USB_DRIVER_INFO(0)},
297 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
298    USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x76),
299    USB_DRIVER_INFO(0)},
300 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
301    USB_IFACE_SUBCLASS(0x03), USB_IFACE_PROTOCOL(0x16),
302    USB_DRIVER_INFO(0)},
303 
304 };
305 
306 static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
307 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
308 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
309 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
310 };
311 
312 #if CDCE_HAVE_NCM
313 /*------------------------------------------------------------------------*
314  *	cdce_ncm_init
315  *
316  * Return values:
317  * 0: Success
318  * Else: Failure
319  *------------------------------------------------------------------------*/
320 static uint8_t
321 cdce_ncm_init(struct cdce_softc *sc)
322 {
323 	struct usb_ncm_parameters temp;
324 	struct usb_device_request req;
325 	struct usb_ncm_func_descriptor *ufd;
326 	uint8_t value[8];
327 	int err;
328 
329 	ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
330 	    sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0 - 1,
331 	    UCDC_NCM_FUNC_DESC_SUBTYPE, 0 - 1);
332 
333 	/* verify length of NCM functional descriptor */
334 	if (ufd != NULL) {
335 		if (ufd->bLength < sizeof(*ufd))
336 			ufd = NULL;
337 		else
338 			DPRINTFN(1, "Found NCM functional descriptor.\n");
339 	}
340 
341 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
342 	req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
343 	USETW(req.wValue, 0);
344 	req.wIndex[0] = sc->sc_ifaces_index[1];
345 	req.wIndex[1] = 0;
346 	USETW(req.wLength, sizeof(temp));
347 
348 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
349 	    &temp, 0, NULL, 1000 /* ms */);
350 	if (err)
351 		return (1);
352 
353 	/* Read correct set of parameters according to device mode */
354 
355 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
356 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
357 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
358 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
359 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
360 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
361 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
362 	} else {
363 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
364 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
365 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
366 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
367 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
368 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
369 	}
370 
371 	/* Verify maximum receive length */
372 
373 	if ((sc->sc_ncm.rx_max < 32) ||
374 	    (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
375 		DPRINTFN(1, "Using default maximum receive length\n");
376 		sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
377 	}
378 
379 	/* Verify maximum transmit length */
380 
381 	if ((sc->sc_ncm.tx_max < 32) ||
382 	    (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
383 		DPRINTFN(1, "Using default maximum transmit length\n");
384 		sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
385 	}
386 
387 	/*
388 	 * Verify that the structure alignment is:
389 	 * - power of two
390 	 * - not greater than the maximum transmit length
391 	 * - not less than four bytes
392 	 */
393 	if ((sc->sc_ncm.tx_struct_align < 4) ||
394 	    (sc->sc_ncm.tx_struct_align !=
395 	     ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
396 	    (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
397 		DPRINTFN(1, "Using default other alignment: 4 bytes\n");
398 		sc->sc_ncm.tx_struct_align = 4;
399 	}
400 
401 	/*
402 	 * Verify that the payload alignment is:
403 	 * - power of two
404 	 * - not greater than the maximum transmit length
405 	 * - not less than four bytes
406 	 */
407 	if ((sc->sc_ncm.tx_modulus < 4) ||
408 	    (sc->sc_ncm.tx_modulus !=
409 	     ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
410 	    (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
411 		DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
412 		sc->sc_ncm.tx_modulus = 4;
413 	}
414 
415 	/* Verify that the payload remainder */
416 
417 	if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
418 		DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
419 		sc->sc_ncm.tx_remainder = 0;
420 	}
421 
422 	/*
423 	 * Offset the TX remainder so that IP packet payload starts at
424 	 * the tx_modulus. This is not too clear in the specification.
425 	 */
426 
427 	sc->sc_ncm.tx_remainder =
428 	    (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
429 	    (sc->sc_ncm.tx_modulus - 1);
430 
431 	/* Verify max datagrams */
432 
433 	if (sc->sc_ncm.tx_nframe == 0 ||
434 	    sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
435 		DPRINTFN(1, "Using default max "
436 		    "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
437 		/* need to reserve one entry for zero padding */
438 		sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
439 	}
440 
441 	/* Additional configuration, will fail in device side mode, which is OK. */
442 
443 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
444 	req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
445 	USETW(req.wValue, 0);
446 	req.wIndex[0] = sc->sc_ifaces_index[1];
447 	req.wIndex[1] = 0;
448 
449 	if (ufd != NULL &&
450 	    (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
451 		USETW(req.wLength, 8);
452 		USETDW(value, sc->sc_ncm.rx_max);
453 		USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
454 		USETW(value + 6, 0);
455 	} else {
456 		USETW(req.wLength, 4);
457 		USETDW(value, sc->sc_ncm.rx_max);
458  	}
459 
460 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
461 	    &value, 0, NULL, 1000 /* ms */);
462 	if (err) {
463 		DPRINTFN(1, "Setting input size "
464 		    "to %u failed.\n", sc->sc_ncm.rx_max);
465 	}
466 
467 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
468 	req.bRequest = UCDC_NCM_SET_CRC_MODE;
469 	USETW(req.wValue, 0);	/* no CRC */
470 	req.wIndex[0] = sc->sc_ifaces_index[1];
471 	req.wIndex[1] = 0;
472 	USETW(req.wLength, 0);
473 
474 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
475 	    NULL, 0, NULL, 1000 /* ms */);
476 	if (err) {
477 		DPRINTFN(1, "Setting CRC mode to off failed.\n");
478 	}
479 
480 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
481 	req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
482 	USETW(req.wValue, 0);	/* NTB-16 */
483 	req.wIndex[0] = sc->sc_ifaces_index[1];
484 	req.wIndex[1] = 0;
485 	USETW(req.wLength, 0);
486 
487 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
488 	    NULL, 0, NULL, 1000 /* ms */);
489 	if (err) {
490 		DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
491 	}
492 
493 	return (0);		/* success */
494 }
495 #endif
496 
497 static void
498 cdce_test_autoinst(void *arg, struct usb_device *udev,
499     struct usb_attach_arg *uaa)
500 {
501 	struct usb_interface *iface;
502 	struct usb_interface_descriptor *id;
503 
504 	if (uaa->dev_state != UAA_DEV_READY)
505 		return;
506 
507 	iface = usbd_get_iface(udev, 0);
508 	if (iface == NULL)
509 		return;
510 	id = iface->idesc;
511 	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
512 		return;
513 	if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
514 		return;		/* no device match */
515 
516 	if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
517 		/* success, mark the udev as disappearing */
518 		uaa->dev_state = UAA_DEV_EJECTING;
519 	}
520 }
521 
522 static int
523 cdce_driver_loaded(struct module *mod, int what, void *arg)
524 {
525 	switch (what) {
526 	case MOD_LOAD:
527 		/* register our autoinstall handler */
528 		cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
529 		    cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
530 		return (0);
531 	case MOD_UNLOAD:
532 		EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
533 		return (0);
534 	default:
535 		return (EOPNOTSUPP);
536 	}
537 }
538 
539 static int
540 cdce_probe(device_t dev)
541 {
542 	struct usb_attach_arg *uaa = device_get_ivars(dev);
543 	int error;
544 
545 	error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
546 	if (error)
547 		error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
548 	return (error);
549 }
550 
551 static void
552 cdce_attach_post(struct usb_ether *ue)
553 {
554 	struct ifnet *ifp = uether_getifp(ue);
555 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
556 	ifq_set_ready(&ifp->if_snd);
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 	lockinit(&sc->sc_lock, device_get_nameunit(dev), 0, 0);
582 
583 	ud = usbd_find_descriptor
584 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
585 	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
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_lock);
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, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1);
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_lock = &sc->sc_lock;
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 	lockuninit(&sc->sc_lock);
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 		ifq_clr_oactive(&ifp->if_snd);
794 		IFNET_STAT_INC(ifp, 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 		DPRINTFN(11, "transfer setup\n");
803 		if(ifq_is_oactive(&ifp->if_snd))
804 			return;
805 
806 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
807 
808 			m = ifq_dequeue(&ifp->if_snd);
809 
810 			if (m == NULL)
811 				break;
812 
813 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
814 				/*
815 				 * Zaurus wants a 32-bit CRC appended
816 				 * to every frame
817 				 */
818 
819 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
820 				crc = htole32(crc);
821 
822 				if (!m_append(m, 4, (void *)&crc)) {
823 					m_freem(m);
824           IFNET_STAT_INC(ifp, oerrors, 1);
825 					continue;
826 				}
827 			}
828 			if (m->m_len != m->m_pkthdr.len) {
829 				mt = m_defrag(m, M_NOWAIT);
830 				if (mt == NULL) {
831 					m_freem(m);
832 					ifp->if_oerrors++;
833 					continue;
834 				}
835 				m = mt;
836 			}
837 			if (m->m_pkthdr.len > MCLBYTES) {
838 				m->m_pkthdr.len = MCLBYTES;
839 			}
840 			sc->sc_tx_buf[x] = m;
841 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
842 
843 			/*
844 			 * If there's a BPF listener, bounce a copy of
845 			 * this frame to him:
846 			 */
847 			BPF_MTAP(ifp, m);
848 		}
849 		if (x != 0) {
850 			usbd_xfer_set_frames(xfer, x);
851 			usbd_transfer_submit(xfer);
852 			ifq_set_oactive(&ifp->if_snd);
853 		}
854 		break;
855 
856 	default:			/* Error */
857 		DPRINTFN(11, "transfer error, %s\n",
858 		    usbd_errstr(error));
859 
860 		/* free all previous TX buffers */
861 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
862 
863 		/* count output errors */
864     IFNET_STAT_INC(ifp, oerrors, 1);
865 		ifq_clr_oactive(&ifp->if_snd);
866 		if (error != USB_ERR_CANCELLED) {
867 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
868 				/* try to clear stall first */
869 				usbd_xfer_set_stall(xfer);
870 			}
871 			goto tr_setup;
872 		}
873 		break;
874 	}
875 }
876 
877 static int32_t
878 cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
879 {
880 	uint32_t *p_crc = arg;
881 
882 	*p_crc = crc32_ext(src, count, *p_crc);
883 
884 	return (0);
885 }
886 
887 static uint32_t
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
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_flags |= IFF_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
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_flags &= ~IFF_RUNNING;
933 	ifq_clr_oactive(&ifp->if_snd);
934 
935 	/*
936 	 * stop all the transfers, if not already stopped:
937 	 */
938 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
939 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
940 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
941 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
942 }
943 
944 static void
945 cdce_setmulti(struct usb_ether *ue)
946 {
947 	/* no-op */
948 	return;
949 }
950 
951 static void
952 cdce_setpromisc(struct usb_ether *ue)
953 {
954 	/* no-op */
955 	return;
956 }
957 
958 static int
959 cdce_suspend(device_t dev)
960 {
961 	device_printf(dev, "Suspending\n");
962 	return (0);
963 }
964 
965 static int
966 cdce_resume(device_t dev)
967 {
968 	device_printf(dev, "Resuming\n");
969 	return (0);
970 }
971 
972 static void
973 cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
974 {
975 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
976 	struct mbuf *m;
977 	uint8_t x;
978 	int actlen, aframes, len;
979 
980 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
981 
982 	switch (USB_GET_STATE(xfer)) {
983 	case USB_ST_TRANSFERRED:
984 
985 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
986 
987 		for (x = 0; x != aframes; x++) {
988 
989 			m = sc->sc_rx_buf[x];
990 			sc->sc_rx_buf[x] = NULL;
991 			len = usbd_xfer_frame_len(xfer, x);
992 
993 			/* Strip off CRC added by Zaurus, if any */
994 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
995 				len -= 4;
996 
997 			if (len < (int)sizeof(struct ether_header)) {
998 				m_freem(m);
999 				continue;
1000 			}
1001 			/* queue up mbuf */
1002 			uether_rxmbuf(&sc->sc_ue, m, len);
1003 		}
1004 
1005 		/* FALLTHROUGH */
1006 	case USB_ST_SETUP:
1007 		/*
1008 		 * TODO: Implement support for multi frame transfers,
1009 		 * when the USB hardware supports it.
1010 		 */
1011 		for (x = 0; x != 1; x++) {
1012 			if (sc->sc_rx_buf[x] == NULL) {
1013 				m = uether_newbuf();
1014 				if (m == NULL)
1015 					goto tr_stall;
1016 				sc->sc_rx_buf[x] = m;
1017 			} else {
1018 				m = sc->sc_rx_buf[x];
1019 			}
1020 
1021 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
1022 		}
1023 		/* set number of frames and start hardware */
1024 		usbd_xfer_set_frames(xfer, x);
1025 		usbd_transfer_submit(xfer);
1026 		/* flush any received frames */
1027 		uether_rxflush(&sc->sc_ue);
1028 		break;
1029 
1030 	default:			/* Error */
1031 		DPRINTF("error = %s\n",
1032 		    usbd_errstr(error));
1033 
1034 		if (error != USB_ERR_CANCELLED) {
1035 tr_stall:
1036 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1037 				/* try to clear stall first */
1038 				usbd_xfer_set_stall(xfer);
1039 				usbd_xfer_set_frames(xfer, 0);
1040 				usbd_transfer_submit(xfer);
1041 			}
1042 			break;
1043 		}
1044 
1045 		/* need to free the RX-mbufs when we are cancelled */
1046 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
1047 		break;
1048 	}
1049 }
1050 
1051 static void
1052 cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
1053 {
1054 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
1055 	int actlen;
1056 
1057 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1058 
1059 	switch (USB_GET_STATE(xfer)) {
1060 	case USB_ST_TRANSFERRED:
1061 
1062 		DPRINTF("Received %d bytes\n", actlen);
1063 
1064 		/* TODO: decode some indications */
1065 
1066 		/* FALLTHROUGH */
1067 	case USB_ST_SETUP:
1068 tr_setup:
1069 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1070 		usbd_transfer_submit(xfer);
1071 		break;
1072 
1073 	default:			/* Error */
1074 		if (error != USB_ERR_CANCELLED) {
1075 			/* start clear stall */
1076 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1077 				usbd_xfer_set_stall(xfer);
1078 			goto tr_setup;
1079 		}
1080 		break;
1081 	}
1082 }
1083 
1084 static void
1085 cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
1086 {
1087 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
1088 	struct usb_cdc_notification req;
1089 	struct usb_page_cache *pc;
1090 	uint32_t speed;
1091 	int actlen;
1092 
1093 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1094 
1095 	switch (USB_GET_STATE(xfer)) {
1096 	case USB_ST_TRANSFERRED:
1097 
1098 		DPRINTF("Transferred %d bytes\n", actlen);
1099 
1100 		switch (sc->sc_notify_state) {
1101 		case CDCE_NOTIFY_NETWORK_CONNECTION:
1102 			sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1103 			break;
1104 		case CDCE_NOTIFY_SPEED_CHANGE:
1105 			sc->sc_notify_state = CDCE_NOTIFY_DONE;
1106 			break;
1107 		default:
1108 			break;
1109 		}
1110 
1111 		/* FALLTHROUGH */
1112 	case USB_ST_SETUP:
1113 tr_setup:
1114 		/*
1115 		 * Inform host about connection. Required according to USB CDC
1116 		 * specification and communicating to Mac OS X USB host stack.
1117 		 * Some of the values seems ignored by Mac OS X though.
1118 		 */
1119 		if (sc->sc_notify_state == CDCE_NOTIFY_NETWORK_CONNECTION) {
1120 			req.bmRequestType = UCDC_NOTIFICATION;
1121 			req.bNotification = UCDC_N_NETWORK_CONNECTION;
1122 			req.wIndex[0] = sc->sc_ifaces_index[1];
1123 			req.wIndex[1] = 0;
1124 			USETW(req.wValue, 1); /* Connected */
1125 			USETW(req.wLength, 0);
1126 
1127 			pc = usbd_xfer_get_frame(xfer, 0);
1128 			usbd_copy_in(pc, 0, &req, sizeof(req));
1129 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1130 			usbd_xfer_set_frames(xfer, 1);
1131 			usbd_transfer_submit(xfer);
1132 
1133 		} else if (sc->sc_notify_state == CDCE_NOTIFY_SPEED_CHANGE) {
1134 			req.bmRequestType = UCDC_NOTIFICATION;
1135 			req.bNotification = UCDC_N_CONNECTION_SPEED_CHANGE;
1136 			req.wIndex[0] = sc->sc_ifaces_index[1];
1137 			req.wIndex[1] = 0;
1138 			USETW(req.wValue, 0);
1139 			USETW(req.wLength, 8);
1140 
1141 			/* Peak theoretical bulk trasfer rate in bits/s */
1142 			if (usbd_get_speed(sc->sc_ue.ue_udev) != USB_SPEED_FULL)
1143 				speed = (13 * 512 * 8 * 1000 * 8);
1144 			else
1145 				speed = (19 * 64 * 1 * 1000 * 8);
1146 
1147 			USETDW(req.data + 0, speed); /* Upstream bit rate */
1148 			USETDW(req.data + 4, speed); /* Downstream bit rate */
1149 
1150 			pc = usbd_xfer_get_frame(xfer, 0);
1151 			usbd_copy_in(pc, 0, &req, sizeof(req));
1152 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1153 			usbd_xfer_set_frames(xfer, 1);
1154 			usbd_transfer_submit(xfer);
1155 		}
1156 		break;
1157 
1158 	default:			/* Error */
1159 		if (error != USB_ERR_CANCELLED) {
1160 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1161 				/* start clear stall */
1162 				usbd_xfer_set_stall(xfer);
1163 			}
1164 			goto tr_setup;
1165 		}
1166 		break;
1167 	}
1168 }
1169 
1170 static int
1171 cdce_handle_request(device_t dev,
1172     const void *preq, void **pptr, uint16_t *plen,
1173     uint16_t offset, uint8_t *pstate)
1174 {
1175 	struct cdce_softc *sc = device_get_softc(dev);
1176 	const struct usb_device_request *req = preq;
1177 	uint8_t is_complete = *pstate;
1178 
1179 	/*
1180 	 * When Mac OS X resumes after suspending it expects
1181 	 * to be notified again after this request.
1182 	 */
1183 	if (req->bmRequestType == UT_WRITE_CLASS_INTERFACE && \
1184 	    req->bRequest == UCDC_NCM_SET_ETHERNET_PACKET_FILTER) {
1185 
1186 		if (is_complete == 1) {
1187 			CDCE_LOCK(sc);
1188 			sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
1189 			usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
1190 			CDCE_UNLOCK(sc);
1191 		}
1192 
1193 		return (0);
1194 	}
1195 
1196 	return (ENXIO);			/* use builtin handler */
1197 }
1198 
1199 #if CDCE_HAVE_NCM
1200 static void
1201 cdce_ncm_tx_zero(struct usb_page_cache *pc,
1202     uint32_t start, uint32_t end)
1203 {
1204 	if (start >= CDCE_NCM_TX_MAXLEN)
1205 		return;
1206 	if (end > CDCE_NCM_TX_MAXLEN)
1207 		end = CDCE_NCM_TX_MAXLEN;
1208 
1209 	usbd_frame_zero(pc, start, end - start);
1210 }
1211 
1212 static uint8_t
1213 cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
1214 {
1215 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
1216 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1217 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
1218 	struct mbuf *m;
1219 	uint32_t rem;
1220 	uint32_t offset;
1221 	uint32_t last_offset;
1222 	uint16_t n;
1223 	uint8_t retval;
1224 
1225 	usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
1226 
1227 	offset = sizeof(sc->sc_ncm.hdr) +
1228 	    sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
1229 
1230 	/* Store last valid offset before alignment */
1231 	last_offset = offset;
1232 
1233 	/* Align offset */
1234 	offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1235 	    offset, sc->sc_ncm.tx_modulus);
1236 
1237 	/* Zero pad */
1238 	cdce_ncm_tx_zero(pc, last_offset, offset);
1239 
1240 	/* buffer full */
1241 	retval = 2;
1242 
1243 	for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
1244 
1245 		/* check if end of transmit buffer is reached */
1246 
1247 		if (offset >= sc->sc_ncm.tx_max)
1248 			break;
1249 
1250 		/* compute maximum buffer size */
1251 
1252 		rem = sc->sc_ncm.tx_max - offset;
1253 
1254 		m = ifq_dequeue(&ifp->if_snd);
1255 
1256 		if (m == NULL) {
1257 			/* buffer not full */
1258 			retval = 1;
1259 			break;
1260 		}
1261 
1262 		if (m->m_pkthdr.len > (int)rem) {
1263 			if (n == 0) {
1264 				/* The frame won't fit in our buffer */
1265 				DPRINTFN(1, "Frame too big to be transmitted!\n");
1266 				m_freem(m);
1267 				ifp->if_oerrors++;
1268 				n--;
1269 				continue;
1270 			}
1271 			/* Wait till next buffer becomes ready */
1272 			ifq_prepend(&ifp->if_snd, m);
1273 			break;
1274 		}
1275 		usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
1276 
1277 		USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
1278 		USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
1279 
1280 		/* Update offset */
1281 		offset += m->m_pkthdr.len;
1282 
1283 		/* Store last valid offset before alignment */
1284 		last_offset = offset;
1285 
1286 		/* Align offset */
1287 		offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
1288 		    offset, sc->sc_ncm.tx_modulus);
1289 
1290 		/* Zero pad */
1291 		cdce_ncm_tx_zero(pc, last_offset, offset);
1292 
1293 		/*
1294 		 * If there's a BPF listener, bounce a copy
1295 		 * of this frame to him:
1296 		 */
1297 		BPF_MTAP(ifp, m);
1298 
1299 		/* Free mbuf */
1300 
1301 		m_freem(m);
1302 
1303 		/* Pre-increment interface counter */
1304 
1305 		IFNET_STAT_INC(ifp, opackets, 1);
1306 	}
1307 
1308 	if (n == 0)
1309 		return (0);
1310 
1311 	rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
1312 
1313 	USETW(sc->sc_ncm.dpt.wLength, rem);
1314 
1315 	/* zero the rest of the data pointer entries */
1316 	for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
1317 		USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
1318 		USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
1319 	}
1320 
1321 	offset = last_offset;
1322 
1323 	/* Align offset */
1324 	offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
1325 
1326 	/* Optimise, save bandwidth and force short termination */
1327 	if (offset >= sc->sc_ncm.tx_max)
1328 		offset = sc->sc_ncm.tx_max;
1329 	else
1330 		offset ++;
1331 
1332 	/* Zero pad */
1333 	cdce_ncm_tx_zero(pc, last_offset, offset);
1334 
1335 	/* set frame length */
1336 	usbd_xfer_set_frame_len(xfer, index, offset);
1337 
1338 	/* Fill out 16-bit header */
1339 	sc->sc_ncm.hdr.dwSignature[0] = 'N';
1340 	sc->sc_ncm.hdr.dwSignature[1] = 'C';
1341 	sc->sc_ncm.hdr.dwSignature[2] = 'M';
1342 	sc->sc_ncm.hdr.dwSignature[3] = 'H';
1343 	USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
1344 	USETW(sc->sc_ncm.hdr.wBlockLength, offset);
1345 	USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
1346 	USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
1347 
1348 	sc->sc_ncm.tx_seq++;
1349 
1350 	/* Fill out 16-bit frame table header */
1351 	sc->sc_ncm.dpt.dwSignature[0] = 'N';
1352 	sc->sc_ncm.dpt.dwSignature[1] = 'C';
1353 	sc->sc_ncm.dpt.dwSignature[2] = 'M';
1354 	sc->sc_ncm.dpt.dwSignature[3] = '0';
1355 	USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0);		/* reserved */
1356 
1357 	usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
1358 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
1359 	    sizeof(sc->sc_ncm.dpt));
1360 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
1361 	    &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
1362 	return (retval);
1363 }
1364 
1365 static void
1366 cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1367 {
1368 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
1369 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1370 	uint16_t x;
1371 	uint8_t temp;
1372 	int actlen;
1373 	int aframes;
1374 
1375 	switch (USB_GET_STATE(xfer)) {
1376 	case USB_ST_TRANSFERRED:
1377 
1378 		usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
1379 
1380 		DPRINTFN(10, "transfer complete: "
1381 		    "%u bytes in %u frames\n", actlen, aframes);
1382 
1383 	case USB_ST_SETUP:
1384 		for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
1385 			temp = cdce_ncm_fill_tx_frames(xfer, x);
1386 			if (temp == 0)
1387 				break;
1388 			if (temp == 1) {
1389 				x++;
1390 				break;
1391 			}
1392 		}
1393 
1394 		if (x != 0) {
1395 #ifdef USB_DEBUG
1396 			usbd_xfer_set_interval(xfer, cdce_tx_interval);
1397 #endif
1398 			usbd_xfer_set_frames(xfer, x);
1399 			usbd_transfer_submit(xfer);
1400 		}
1401 		break;
1402 
1403 	default:			/* Error */
1404 		DPRINTFN(10, "Transfer error: %s\n",
1405 		    usbd_errstr(error));
1406 
1407 		/* update error counter */
1408 		ifp->if_oerrors += 1;
1409 
1410 		if (error != USB_ERR_CANCELLED) {
1411 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1412 				/* try to clear stall first */
1413 				usbd_xfer_set_stall(xfer);
1414 				usbd_xfer_set_frames(xfer, 0);
1415 				usbd_transfer_submit(xfer);
1416 			}
1417 		}
1418 		break;
1419 	}
1420 }
1421 
1422 static void
1423 cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1424 {
1425 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
1426 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
1427 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1428 	struct mbuf *m;
1429 	int sumdata;
1430 	int sumlen;
1431 	int actlen;
1432 	int aframes;
1433 	int temp;
1434 	int nframes;
1435 	int x;
1436 	int offset;
1437 
1438 	switch (USB_GET_STATE(xfer)) {
1439 	case USB_ST_TRANSFERRED:
1440 
1441 		usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
1442 
1443 		DPRINTFN(1, "received %u bytes in %u frames\n",
1444 		    actlen, aframes);
1445 
1446 		if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
1447 		    sizeof(sc->sc_ncm.dpt))) {
1448 			DPRINTFN(1, "frame too short\n");
1449 			goto tr_setup;
1450 		}
1451 		usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
1452 		    sizeof(sc->sc_ncm.hdr));
1453 
1454 		if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
1455 		    (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
1456 		    (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
1457 		    (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
1458 			DPRINTFN(1, "invalid HDR signature: "
1459 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
1460 			    sc->sc_ncm.hdr.dwSignature[0],
1461 			    sc->sc_ncm.hdr.dwSignature[1],
1462 			    sc->sc_ncm.hdr.dwSignature[2],
1463 			    sc->sc_ncm.hdr.dwSignature[3]);
1464 			goto tr_stall;
1465 		}
1466 		temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
1467 		if (temp > sumlen) {
1468 			DPRINTFN(1, "unsupported block length %u/%u\n",
1469 			    temp, sumlen);
1470 			goto tr_stall;
1471 		}
1472 		temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
1473 		if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
1474 			DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
1475 			goto tr_stall;
1476 		}
1477 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
1478 		    sizeof(sc->sc_ncm.dpt));
1479 
1480 		if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
1481 		    (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
1482 		    (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
1483 		    (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
1484 			DPRINTFN(1, "invalid DPT signature"
1485 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
1486 			    sc->sc_ncm.dpt.dwSignature[0],
1487 			    sc->sc_ncm.dpt.dwSignature[1],
1488 			    sc->sc_ncm.dpt.dwSignature[2],
1489 			    sc->sc_ncm.dpt.dwSignature[3]);
1490 			goto tr_stall;
1491 		}
1492 		nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
1493 
1494 		/* Subtract size of header and last zero padded entry */
1495 		if (nframes >= (2 + 1))
1496 			nframes -= (2 + 1);
1497 		else
1498 			nframes = 0;
1499 
1500 		DPRINTFN(1, "nframes = %u\n", nframes);
1501 
1502 		temp += sizeof(sc->sc_ncm.dpt);
1503 
1504 		if ((temp + (4 * nframes)) > actlen)
1505 			goto tr_stall;
1506 
1507 		if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
1508 			DPRINTFN(1, "Truncating number of frames from %u to %u\n",
1509 			    nframes, CDCE_NCM_SUBFRAMES_MAX);
1510 			nframes = CDCE_NCM_SUBFRAMES_MAX;
1511 		}
1512 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
1513 
1514 		sumdata = 0;
1515 
1516 		for (x = 0; x != nframes; x++) {
1517 
1518 			offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
1519 			temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
1520 
1521 			if ((offset == 0) ||
1522 			    (temp < (int)sizeof(struct ether_header)) ||
1523 			    (temp > (MCLBYTES - ETHER_ALIGN))) {
1524 				DPRINTFN(1, "NULL frame detected at %d\n", x);
1525 				m = NULL;
1526 				/* silently ignore this frame */
1527 				continue;
1528 			} else if ((offset + temp) > actlen) {
1529 				DPRINTFN(1, "invalid frame "
1530 				    "detected at %d\n", x);
1531 				m = NULL;
1532 				/* silently ignore this frame */
1533 				continue;
1534 			} else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1535 				m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1536 			} else {
1537 				m = m_gethdr(M_NOWAIT, MT_DATA);
1538 			}
1539 
1540 			DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
1541 			    x, offset, temp);
1542 
1543 			/* check if we have a buffer */
1544 			if (m) {
1545 				m->m_len = m->m_pkthdr.len = temp + ETHER_ALIGN;
1546 				m_adj(m, ETHER_ALIGN);
1547 
1548 				usbd_copy_out(pc, offset, m->m_data, temp);
1549 
1550 				/* enqueue */
1551 				uether_rxmbuf(&sc->sc_ue, m, temp);
1552 
1553 				sumdata += temp;
1554 			} else {
1555 				ifp->if_ierrors++;
1556 			}
1557 		}
1558 
1559 		DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
1560 
1561 	case USB_ST_SETUP:
1562 tr_setup:
1563 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
1564 		usbd_xfer_set_frames(xfer, 1);
1565 		usbd_transfer_submit(xfer);
1566 		uether_rxflush(&sc->sc_ue);	/* must be last */
1567 		break;
1568 
1569 	default:			/* Error */
1570 		DPRINTFN(1, "error = %s\n",
1571 		    usbd_errstr(error));
1572 
1573 		if (error != USB_ERR_CANCELLED) {
1574 tr_stall:
1575 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
1576 				/* try to clear stall first */
1577 				usbd_xfer_set_stall(xfer);
1578 				usbd_xfer_set_frames(xfer, 0);
1579 				usbd_transfer_submit(xfer);
1580 			}
1581 		}
1582 		break;
1583 	}
1584 }
1585 #endif
1586