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