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