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