xref: /freebsd/sys/dev/usb/net/if_cdce.c (revision c376f4397d82bd41fe914a4785b8cb346eca99b7)
102ac6454SAndrew Thompson /*	$NetBSD: if_cdce.c,v 1.4 2004/10/24 12:50:54 augustss Exp $ */
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /*-
402ac6454SAndrew Thompson  * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com>
502ac6454SAndrew Thompson  * Copyright (c) 2003-2005 Craig Boston
602ac6454SAndrew Thompson  * Copyright (c) 2004 Daniel Hartmeier
702ac6454SAndrew Thompson  * Copyright (c) 2009 Hans Petter Selasky
802ac6454SAndrew Thompson  * All rights reserved.
902ac6454SAndrew Thompson  *
1002ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1102ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1202ac6454SAndrew Thompson  * are met:
1302ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1502ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1602ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1702ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1802ac6454SAndrew Thompson  * 3. All advertising materials mentioning features or use of this software
1902ac6454SAndrew Thompson  *    must display the following acknowledgement:
2002ac6454SAndrew Thompson  *	This product includes software developed by Bill Paul.
2102ac6454SAndrew Thompson  * 4. Neither the name of the author nor the names of any co-contributors
2202ac6454SAndrew Thompson  *    may be used to endorse or promote products derived from this software
2302ac6454SAndrew Thompson  *    without specific prior written permission.
2402ac6454SAndrew Thompson  *
2502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2602ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2702ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2802ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR
2902ac6454SAndrew Thompson  * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3002ac6454SAndrew Thompson  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3102ac6454SAndrew Thompson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3202ac6454SAndrew Thompson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3302ac6454SAndrew Thompson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3402ac6454SAndrew Thompson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3502ac6454SAndrew Thompson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3602ac6454SAndrew Thompson  */
3702ac6454SAndrew Thompson 
3802ac6454SAndrew Thompson /*
3902ac6454SAndrew Thompson  * USB Communication Device Class (Ethernet Networking Control Model)
4002ac6454SAndrew Thompson  * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
4102ac6454SAndrew Thompson  */
4202ac6454SAndrew Thompson 
434076dd23SAndrew Thompson /*
444076dd23SAndrew Thompson  * USB Network Control Model (NCM)
454076dd23SAndrew Thompson  * http://www.usb.org/developers/devclass_docs/NCM10.zip
464076dd23SAndrew Thompson  */
474076dd23SAndrew Thompson 
4802ac6454SAndrew Thompson #include <sys/cdefs.h>
4902ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
5002ac6454SAndrew Thompson 
51ed6d949aSAndrew Thompson #include <sys/stdint.h>
52ed6d949aSAndrew Thompson #include <sys/stddef.h>
53ed6d949aSAndrew Thompson #include <sys/param.h>
54ed6d949aSAndrew Thompson #include <sys/queue.h>
55ed6d949aSAndrew Thompson #include <sys/types.h>
56ed6d949aSAndrew Thompson #include <sys/systm.h>
5776039bc8SGleb Smirnoff #include <sys/socket.h>
58ed6d949aSAndrew Thompson #include <sys/kernel.h>
59ed6d949aSAndrew Thompson #include <sys/bus.h>
60ed6d949aSAndrew Thompson #include <sys/module.h>
61ed6d949aSAndrew Thompson #include <sys/lock.h>
62ed6d949aSAndrew Thompson #include <sys/mutex.h>
63ed6d949aSAndrew Thompson #include <sys/condvar.h>
64ed6d949aSAndrew Thompson #include <sys/sysctl.h>
65ed6d949aSAndrew Thompson #include <sys/sx.h>
66ed6d949aSAndrew Thompson #include <sys/unistd.h>
67ed6d949aSAndrew Thompson #include <sys/callout.h>
68ed6d949aSAndrew Thompson #include <sys/malloc.h>
69ed6d949aSAndrew Thompson #include <sys/priv.h>
70ed6d949aSAndrew Thompson 
7176039bc8SGleb Smirnoff #include <net/if.h>
7276039bc8SGleb Smirnoff #include <net/if_var.h>
7376039bc8SGleb Smirnoff 
7402ac6454SAndrew Thompson #include <dev/usb/usb.h>
75ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
76ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
78ed6d949aSAndrew Thompson #include "usbdevs.h"
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson #define	USB_DEBUG_VAR cdce_debug
8102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
82ed6d949aSAndrew Thompson #include <dev/usb/usb_process.h>
83*c376f439SNick Hibma #include <dev/usb/usb_msctest.h>
84ed6d949aSAndrew Thompson #include "usb_if.h"
8502ac6454SAndrew Thompson 
8602ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h>
8702ac6454SAndrew Thompson #include <dev/usb/net/if_cdcereg.h>
8802ac6454SAndrew Thompson 
8902ac6454SAndrew Thompson static device_probe_t cdce_probe;
9002ac6454SAndrew Thompson static device_attach_t cdce_attach;
9102ac6454SAndrew Thompson static device_detach_t cdce_detach;
9202ac6454SAndrew Thompson static device_suspend_t cdce_suspend;
9302ac6454SAndrew Thompson static device_resume_t cdce_resume;
9402ac6454SAndrew Thompson static usb_handle_request_t cdce_handle_request;
9502ac6454SAndrew Thompson 
96e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_write_callback;
97e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_read_callback;
98e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_read_callback;
99e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_write_callback;
10002ac6454SAndrew Thompson 
1014076dd23SAndrew Thompson #if CDCE_HAVE_NCM
1024076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_write_callback;
1034076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_read_callback;
1044076dd23SAndrew Thompson #endif
1054076dd23SAndrew Thompson 
106e0a69b51SAndrew Thompson static uether_fn_t cdce_attach_post;
107e0a69b51SAndrew Thompson static uether_fn_t cdce_init;
108e0a69b51SAndrew Thompson static uether_fn_t cdce_stop;
109e0a69b51SAndrew Thompson static uether_fn_t cdce_start;
110e0a69b51SAndrew Thompson static uether_fn_t cdce_setmulti;
111e0a69b51SAndrew Thompson static uether_fn_t cdce_setpromisc;
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson static uint32_t	cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
11402ac6454SAndrew Thompson 
115b850ecc1SAndrew Thompson #ifdef USB_DEBUG
11602ac6454SAndrew Thompson static int cdce_debug = 0;
1177e05daaeSHans Petter Selasky static int cdce_tx_interval = 0;
11802ac6454SAndrew Thompson 
1196472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
1209360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0,
12102ac6454SAndrew Thompson     "Debug level");
1227e05daaeSHans Petter Selasky SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RW, &cdce_tx_interval, 0,
1237e05daaeSHans Petter Selasky     "NCM transmit interval in ms");
12402ac6454SAndrew Thompson #endif
12502ac6454SAndrew Thompson 
126760bc48eSAndrew Thompson static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
12702ac6454SAndrew Thompson 
1284eae601eSAndrew Thompson 	[CDCE_BULK_RX] = {
12902ac6454SAndrew Thompson 		.type = UE_BULK,
13002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1314eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
13202ac6454SAndrew Thompson 		.if_index = 0,
1334eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1344eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1354eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
1364eae601eSAndrew Thompson 		.callback = cdce_bulk_read_callback,
1374eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
138f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
13902ac6454SAndrew Thompson 	},
14002ac6454SAndrew Thompson 
1414eae601eSAndrew Thompson 	[CDCE_BULK_TX] = {
14202ac6454SAndrew Thompson 		.type = UE_BULK,
14302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1444eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
14502ac6454SAndrew Thompson 		.if_index = 0,
1464eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1474eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1484eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
1494eae601eSAndrew Thompson 		.callback = cdce_bulk_write_callback,
1504eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
151f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
15202ac6454SAndrew Thompson 	},
15302ac6454SAndrew Thompson 
1544eae601eSAndrew Thompson 	[CDCE_INTR_RX] = {
15502ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
15602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1574eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
15802ac6454SAndrew Thompson 		.if_index = 1,
1594eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1604eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
1614eae601eSAndrew Thompson 		.callback = cdce_intr_read_callback,
1624eae601eSAndrew Thompson 		.timeout = 0,
1634eae601eSAndrew Thompson 		.usb_mode = USB_MODE_HOST,
1644eae601eSAndrew Thompson 	},
1654eae601eSAndrew Thompson 
1664eae601eSAndrew Thompson 	[CDCE_INTR_TX] = {
1674eae601eSAndrew Thompson 		.type = UE_INTERRUPT,
1684eae601eSAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1694eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
1704eae601eSAndrew Thompson 		.if_index = 1,
1714eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1724eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
1734eae601eSAndrew Thompson 		.callback = cdce_intr_write_callback,
1744eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
1754eae601eSAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
17602ac6454SAndrew Thompson 	},
17702ac6454SAndrew Thompson };
17802ac6454SAndrew Thompson 
1794076dd23SAndrew Thompson #if CDCE_HAVE_NCM
1804076dd23SAndrew Thompson static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
1814076dd23SAndrew Thompson 
1824076dd23SAndrew Thompson 	[CDCE_BULK_RX] = {
1834076dd23SAndrew Thompson 		.type = UE_BULK,
1844076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1854076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
1864076dd23SAndrew Thompson 		.if_index = 0,
1874076dd23SAndrew Thompson 		.frames = CDCE_NCM_RX_FRAMES_MAX,
1884076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
1894076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
1904076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_read_callback,
1914076dd23SAndrew Thompson 		.timeout = 0,	/* no timeout */
1924076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
1934076dd23SAndrew Thompson 	},
1944076dd23SAndrew Thompson 
1954076dd23SAndrew Thompson 	[CDCE_BULK_TX] = {
1964076dd23SAndrew Thompson 		.type = UE_BULK,
1974076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1984076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
1994076dd23SAndrew Thompson 		.if_index = 0,
2004076dd23SAndrew Thompson 		.frames = CDCE_NCM_TX_FRAMES_MAX,
2014076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
2027e05daaeSHans Petter Selasky 		.flags = {.pipe_bof = 1,},
2034076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_write_callback,
2044076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2054076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
2064076dd23SAndrew Thompson 	},
2074076dd23SAndrew Thompson 
2084076dd23SAndrew Thompson 	[CDCE_INTR_RX] = {
2094076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2104076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2114076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
2124076dd23SAndrew Thompson 		.if_index = 1,
2134076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2144076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
2154076dd23SAndrew Thompson 		.callback = cdce_intr_read_callback,
2164076dd23SAndrew Thompson 		.timeout = 0,
2174076dd23SAndrew Thompson 		.usb_mode = USB_MODE_HOST,
2184076dd23SAndrew Thompson 	},
2194076dd23SAndrew Thompson 
2204076dd23SAndrew Thompson 	[CDCE_INTR_TX] = {
2214076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2224076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2234076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
2244076dd23SAndrew Thompson 		.if_index = 1,
2254076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2264076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
2274076dd23SAndrew Thompson 		.callback = cdce_intr_write_callback,
2284076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2294076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
2304076dd23SAndrew Thompson 	},
2314076dd23SAndrew Thompson };
2324076dd23SAndrew Thompson #endif
2334076dd23SAndrew Thompson 
23402ac6454SAndrew Thompson static device_method_t cdce_methods[] = {
23502ac6454SAndrew Thompson 	/* USB interface */
23602ac6454SAndrew Thompson 	DEVMETHOD(usb_handle_request, cdce_handle_request),
23702ac6454SAndrew Thompson 
23802ac6454SAndrew Thompson 	/* Device interface */
23902ac6454SAndrew Thompson 	DEVMETHOD(device_probe, cdce_probe),
24002ac6454SAndrew Thompson 	DEVMETHOD(device_attach, cdce_attach),
24102ac6454SAndrew Thompson 	DEVMETHOD(device_detach, cdce_detach),
24202ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, cdce_suspend),
24302ac6454SAndrew Thompson 	DEVMETHOD(device_resume, cdce_resume),
24402ac6454SAndrew Thompson 
24561bfd867SSofian Brabez 	DEVMETHOD_END
24602ac6454SAndrew Thompson };
24702ac6454SAndrew Thompson 
24802ac6454SAndrew Thompson static driver_t cdce_driver = {
24902ac6454SAndrew Thompson 	.name = "cdce",
25002ac6454SAndrew Thompson 	.methods = cdce_methods,
25102ac6454SAndrew Thompson 	.size = sizeof(struct cdce_softc),
25202ac6454SAndrew Thompson };
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson static devclass_t cdce_devclass;
255*c376f439SNick Hibma static eventhandler_tag cdce_etag;
25602ac6454SAndrew Thompson 
257*c376f439SNick Hibma static int  cdce_driver_loaded(struct module *, int, void *);
258*c376f439SNick Hibma 
259*c376f439SNick Hibma DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, cdce_driver_loaded, 0);
26002ac6454SAndrew Thompson MODULE_VERSION(cdce, 1);
26102ac6454SAndrew Thompson MODULE_DEPEND(cdce, uether, 1, 1, 1);
26202ac6454SAndrew Thompson MODULE_DEPEND(cdce, usb, 1, 1, 1);
26302ac6454SAndrew Thompson MODULE_DEPEND(cdce, ether, 1, 1, 1);
26402ac6454SAndrew Thompson 
265760bc48eSAndrew Thompson static const struct usb_ether_methods cdce_ue_methods = {
26602ac6454SAndrew Thompson 	.ue_attach_post = cdce_attach_post,
26702ac6454SAndrew Thompson 	.ue_start = cdce_start,
26802ac6454SAndrew Thompson 	.ue_init = cdce_init,
26902ac6454SAndrew Thompson 	.ue_stop = cdce_stop,
27002ac6454SAndrew Thompson 	.ue_setmulti = cdce_setmulti,
27102ac6454SAndrew Thompson 	.ue_setpromisc = cdce_setpromisc,
27202ac6454SAndrew Thompson };
27302ac6454SAndrew Thompson 
274*c376f439SNick Hibma static const STRUCT_USB_HOST_ID cdce_switch_devs[] = {
275*c376f439SNick Hibma 	{USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3272_INIT, MSC_EJECT_HUAWEI2)},
276*c376f439SNick Hibma };
277*c376f439SNick Hibma 
278f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
27902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
28002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
28102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
28202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
28302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
28602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
28702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
28802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
29002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
29102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
292*c376f439SNick Hibma 
293*c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
294*c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x16),
295*c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
296*c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
297*c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x46),
298*c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
299*c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
300*c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x76),
301*c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
302f1a16106SHans Petter Selasky };
3039739167cSAlfred Perlstein 
304f1a16106SHans Petter Selasky static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
3059739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
3069739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
3074076dd23SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
30802ac6454SAndrew Thompson };
30902ac6454SAndrew Thompson 
3104076dd23SAndrew Thompson #if CDCE_HAVE_NCM
3114076dd23SAndrew Thompson /*------------------------------------------------------------------------*
3124076dd23SAndrew Thompson  *	cdce_ncm_init
3134076dd23SAndrew Thompson  *
3144076dd23SAndrew Thompson  * Return values:
3154076dd23SAndrew Thompson  * 0: Success
3164076dd23SAndrew Thompson  * Else: Failure
3174076dd23SAndrew Thompson  *------------------------------------------------------------------------*/
3184076dd23SAndrew Thompson static uint8_t
3194076dd23SAndrew Thompson cdce_ncm_init(struct cdce_softc *sc)
3204076dd23SAndrew Thompson {
3214076dd23SAndrew Thompson 	struct usb_ncm_parameters temp;
3224076dd23SAndrew Thompson 	struct usb_device_request req;
3237e05daaeSHans Petter Selasky 	struct usb_ncm_func_descriptor *ufd;
3247e05daaeSHans Petter Selasky 	uint8_t value[8];
3254076dd23SAndrew Thompson 	int err;
3264076dd23SAndrew Thompson 
3277e05daaeSHans Petter Selasky 	ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
3286d917491SHans Petter Selasky 	    sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF,
3296d917491SHans Petter Selasky 	    UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF);
3307e05daaeSHans Petter Selasky 
3317e05daaeSHans Petter Selasky 	/* verify length of NCM functional descriptor */
3327e05daaeSHans Petter Selasky 	if (ufd != NULL) {
3337e05daaeSHans Petter Selasky 		if (ufd->bLength < sizeof(*ufd))
3347e05daaeSHans Petter Selasky 			ufd = NULL;
3357e05daaeSHans Petter Selasky 		else
3367e05daaeSHans Petter Selasky 			DPRINTFN(1, "Found NCM functional descriptor.\n");
3377e05daaeSHans Petter Selasky 	}
3387e05daaeSHans Petter Selasky 
3394076dd23SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
3404076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
3414076dd23SAndrew Thompson 	USETW(req.wValue, 0);
3424076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
3434076dd23SAndrew Thompson 	req.wIndex[1] = 0;
3444076dd23SAndrew Thompson 	USETW(req.wLength, sizeof(temp));
3454076dd23SAndrew Thompson 
3464076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
3474076dd23SAndrew Thompson 	    &temp, 0, NULL, 1000 /* ms */);
3484076dd23SAndrew Thompson 	if (err)
3494076dd23SAndrew Thompson 		return (1);
3504076dd23SAndrew Thompson 
3514076dd23SAndrew Thompson 	/* Read correct set of parameters according to device mode */
3524076dd23SAndrew Thompson 
3534076dd23SAndrew Thompson 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
354dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
355dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
3564076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
3574076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
3584076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
3597e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3604076dd23SAndrew Thompson 	} else {
361dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
362dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
3634076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
3644076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
3654076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
3667e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3674076dd23SAndrew Thompson 	}
3684076dd23SAndrew Thompson 
3694076dd23SAndrew Thompson 	/* Verify maximum receive length */
3704076dd23SAndrew Thompson 
3717e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.rx_max < 32) ||
3724076dd23SAndrew Thompson 	    (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
3734076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum receive length\n");
3744076dd23SAndrew Thompson 		sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
3754076dd23SAndrew Thompson 	}
3764076dd23SAndrew Thompson 
3774076dd23SAndrew Thompson 	/* Verify maximum transmit length */
3784076dd23SAndrew Thompson 
3797e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_max < 32) ||
3804076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
3814076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum transmit length\n");
3824076dd23SAndrew Thompson 		sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
3834076dd23SAndrew Thompson 	}
3844076dd23SAndrew Thompson 
3854076dd23SAndrew Thompson 	/*
3864076dd23SAndrew Thompson 	 * Verify that the structure alignment is:
3874076dd23SAndrew Thompson 	 * - power of two
3884076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
3894076dd23SAndrew Thompson 	 * - not less than four bytes
3904076dd23SAndrew Thompson 	 */
3917e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_struct_align < 4) ||
3924076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align !=
3934076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
3944076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
3954076dd23SAndrew Thompson 		DPRINTFN(1, "Using default other alignment: 4 bytes\n");
3964076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = 4;
3974076dd23SAndrew Thompson 	}
3984076dd23SAndrew Thompson 
3994076dd23SAndrew Thompson 	/*
4004076dd23SAndrew Thompson 	 * Verify that the payload alignment is:
4014076dd23SAndrew Thompson 	 * - power of two
4024076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
4034076dd23SAndrew Thompson 	 * - not less than four bytes
4044076dd23SAndrew Thompson 	 */
4057e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_modulus < 4) ||
4064076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus !=
4074076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
4084076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
4094076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
4104076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = 4;
4114076dd23SAndrew Thompson 	}
4124076dd23SAndrew Thompson 
4134076dd23SAndrew Thompson 	/* Verify that the payload remainder */
4144076dd23SAndrew Thompson 
4157e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
4164076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
4174076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = 0;
4184076dd23SAndrew Thompson 	}
4194076dd23SAndrew Thompson 
4207e05daaeSHans Petter Selasky 	/*
4217e05daaeSHans Petter Selasky 	 * Offset the TX remainder so that IP packet payload starts at
4227e05daaeSHans Petter Selasky 	 * the tx_modulus. This is not too clear in the specification.
4237e05daaeSHans Petter Selasky 	 */
4247e05daaeSHans Petter Selasky 
4257e05daaeSHans Petter Selasky 	sc->sc_ncm.tx_remainder =
4267e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
4277e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_modulus - 1);
4287e05daaeSHans Petter Selasky 
4297e05daaeSHans Petter Selasky 	/* Verify max datagrams */
4307e05daaeSHans Petter Selasky 
4317e05daaeSHans Petter Selasky 	if (sc->sc_ncm.tx_nframe == 0 ||
4327e05daaeSHans Petter Selasky 	    sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
4337e05daaeSHans Petter Selasky 		DPRINTFN(1, "Using default max "
4347e05daaeSHans Petter Selasky 		    "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
4357e05daaeSHans Petter Selasky 		/* need to reserve one entry for zero padding */
4367e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
4377e05daaeSHans Petter Selasky 	}
4387e05daaeSHans Petter Selasky 
4394076dd23SAndrew Thompson 	/* Additional configuration, will fail in device side mode, which is OK. */
4404076dd23SAndrew Thompson 
4414076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4424076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
4434076dd23SAndrew Thompson 	USETW(req.wValue, 0);
4444076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4454076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4467e05daaeSHans Petter Selasky 
4477e05daaeSHans Petter Selasky 	if (ufd != NULL &&
4487e05daaeSHans Petter Selasky 	    (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
4497e05daaeSHans Petter Selasky 		USETW(req.wLength, 8);
4507e05daaeSHans Petter Selasky 		USETDW(value, sc->sc_ncm.rx_max);
4517e05daaeSHans Petter Selasky 		USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
4527e05daaeSHans Petter Selasky 		USETW(value + 6, 0);
4537e05daaeSHans Petter Selasky 	} else {
4544076dd23SAndrew Thompson 		USETW(req.wLength, 4);
4554076dd23SAndrew Thompson 		USETDW(value, sc->sc_ncm.rx_max);
4567e05daaeSHans Petter Selasky  	}
4574076dd23SAndrew Thompson 
4584076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4594076dd23SAndrew Thompson 	    &value, 0, NULL, 1000 /* ms */);
4604076dd23SAndrew Thompson 	if (err) {
4614076dd23SAndrew Thompson 		DPRINTFN(1, "Setting input size "
4624076dd23SAndrew Thompson 		    "to %u failed.\n", sc->sc_ncm.rx_max);
4634076dd23SAndrew Thompson 	}
4644076dd23SAndrew Thompson 
4654076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4664076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_CRC_MODE;
4674076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* no CRC */
4684076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4694076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4704076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4714076dd23SAndrew Thompson 
4724076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4734076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4744076dd23SAndrew Thompson 	if (err) {
4754076dd23SAndrew Thompson 		DPRINTFN(1, "Setting CRC mode to off failed.\n");
4764076dd23SAndrew Thompson 	}
4774076dd23SAndrew Thompson 
4784076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4794076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
4804076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* NTB-16 */
4814076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4824076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4834076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4844076dd23SAndrew Thompson 
4854076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4864076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4874076dd23SAndrew Thompson 	if (err) {
4884076dd23SAndrew Thompson 		DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
4894076dd23SAndrew Thompson 	}
4904076dd23SAndrew Thompson 
4914076dd23SAndrew Thompson 	return (0);		/* success */
4924076dd23SAndrew Thompson }
4934076dd23SAndrew Thompson #endif
4944076dd23SAndrew Thompson 
495*c376f439SNick Hibma static void
496*c376f439SNick Hibma cdce_test_autoinst(void *arg, struct usb_device *udev,
497*c376f439SNick Hibma     struct usb_attach_arg *uaa)
498*c376f439SNick Hibma {
499*c376f439SNick Hibma 	struct usb_interface *iface;
500*c376f439SNick Hibma 	struct usb_interface_descriptor *id;
501*c376f439SNick Hibma 
502*c376f439SNick Hibma 	if (uaa->dev_state != UAA_DEV_READY)
503*c376f439SNick Hibma 		return;
504*c376f439SNick Hibma 
505*c376f439SNick Hibma 	iface = usbd_get_iface(udev, 0);
506*c376f439SNick Hibma 	if (iface == NULL)
507*c376f439SNick Hibma 		return;
508*c376f439SNick Hibma 	id = iface->idesc;
509*c376f439SNick Hibma 	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
510*c376f439SNick Hibma 		return;
511*c376f439SNick Hibma 	if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
512*c376f439SNick Hibma 		return;		/* no device match */
513*c376f439SNick Hibma 
514*c376f439SNick Hibma 	if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
515*c376f439SNick Hibma 		/* success, mark the udev as disappearing */
516*c376f439SNick Hibma 		uaa->dev_state = UAA_DEV_EJECTING;
517*c376f439SNick Hibma 	}
518*c376f439SNick Hibma }
519*c376f439SNick Hibma 
520*c376f439SNick Hibma static int
521*c376f439SNick Hibma cdce_driver_loaded(struct module *mod, int what, void *arg)
522*c376f439SNick Hibma {
523*c376f439SNick Hibma 	switch (what) {
524*c376f439SNick Hibma 	case MOD_LOAD:
525*c376f439SNick Hibma 		/* register our autoinstall handler */
526*c376f439SNick Hibma 		cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
527*c376f439SNick Hibma 		    cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
528*c376f439SNick Hibma 		return (0);
529*c376f439SNick Hibma 	case MOD_UNLOAD:
530*c376f439SNick Hibma 		EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
531*c376f439SNick Hibma 		return (0);
532*c376f439SNick Hibma 	default:
533*c376f439SNick Hibma 		return (EOPNOTSUPP);
534*c376f439SNick Hibma 	}
535*c376f439SNick Hibma }
536*c376f439SNick Hibma 
53702ac6454SAndrew Thompson static int
53802ac6454SAndrew Thompson cdce_probe(device_t dev)
53902ac6454SAndrew Thompson {
540760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
541f1a16106SHans Petter Selasky 	int error;
54202ac6454SAndrew Thompson 
543f1a16106SHans Petter Selasky 	error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
544f1a16106SHans Petter Selasky 	if (error)
545f1a16106SHans Petter Selasky 		error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
546f1a16106SHans Petter Selasky 	return (error);
54702ac6454SAndrew Thompson }
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson static void
550760bc48eSAndrew Thompson cdce_attach_post(struct usb_ether *ue)
55102ac6454SAndrew Thompson {
55202ac6454SAndrew Thompson 	/* no-op */
55302ac6454SAndrew Thompson 	return;
55402ac6454SAndrew Thompson }
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson static int
55702ac6454SAndrew Thompson cdce_attach(device_t dev)
55802ac6454SAndrew Thompson {
55902ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
560760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
561760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
562760bc48eSAndrew Thompson 	struct usb_interface *iface;
563760bc48eSAndrew Thompson 	const struct usb_cdc_union_descriptor *ud;
564760bc48eSAndrew Thompson 	const struct usb_interface_descriptor *id;
565760bc48eSAndrew Thompson 	const struct usb_cdc_ethernet_descriptor *ued;
5664076dd23SAndrew Thompson 	const struct usb_config *pcfg;
567a8df530dSJohn Baldwin 	uint32_t seed;
56802ac6454SAndrew Thompson 	int error;
56902ac6454SAndrew Thompson 	uint8_t i;
5704076dd23SAndrew Thompson 	uint8_t data_iface_no;
57102ac6454SAndrew Thompson 	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
5744076dd23SAndrew Thompson 	sc->sc_ue.ue_udev = uaa->device;
57502ac6454SAndrew Thompson 
576a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
57702ac6454SAndrew Thompson 
57802ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
57902ac6454SAndrew Thompson 
5803c8d24f4SAndrew Thompson 	ud = usbd_find_descriptor
58102ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
5826d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF);
58302ac6454SAndrew Thompson 
5844076dd23SAndrew Thompson 	if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
5854076dd23SAndrew Thompson 	    (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
5864076dd23SAndrew Thompson 		DPRINTFN(1, "No union descriptor!\n");
5874076dd23SAndrew Thompson 		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
5884076dd23SAndrew Thompson 		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
5894076dd23SAndrew Thompson 		goto alloc_transfers;
59002ac6454SAndrew Thompson 	}
5914076dd23SAndrew Thompson 	data_iface_no = ud->bSlaveInterface[0];
59202ac6454SAndrew Thompson 
59302ac6454SAndrew Thompson 	for (i = 0;; i++) {
59402ac6454SAndrew Thompson 
595a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
59602ac6454SAndrew Thompson 
59702ac6454SAndrew Thompson 		if (iface) {
59802ac6454SAndrew Thompson 
599a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
60002ac6454SAndrew Thompson 
6014076dd23SAndrew Thompson 			if (id && (id->bInterfaceNumber == data_iface_no)) {
60202ac6454SAndrew Thompson 				sc->sc_ifaces_index[0] = i;
60302ac6454SAndrew Thompson 				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
604a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
60502ac6454SAndrew Thompson 				break;
60602ac6454SAndrew Thompson 			}
60702ac6454SAndrew Thompson 		} else {
608767cb2e2SAndrew Thompson 			device_printf(dev, "no data interface found\n");
60902ac6454SAndrew Thompson 			goto detach;
61002ac6454SAndrew Thompson 		}
61102ac6454SAndrew Thompson 	}
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson 	/*
61402ac6454SAndrew Thompson 	 * <quote>
61502ac6454SAndrew Thompson 	 *
61602ac6454SAndrew Thompson 	 *  The Data Class interface of a networking device shall have
61702ac6454SAndrew Thompson 	 *  a minimum of two interface settings. The first setting
61802ac6454SAndrew Thompson 	 *  (the default interface setting) includes no endpoints and
61902ac6454SAndrew Thompson 	 *  therefore no networking traffic is exchanged whenever the
62002ac6454SAndrew Thompson 	 *  default interface setting is selected. One or more
62102ac6454SAndrew Thompson 	 *  additional interface settings are used for normal
62202ac6454SAndrew Thompson 	 *  operation, and therefore each includes a pair of endpoints
62302ac6454SAndrew Thompson 	 *  (one IN, and one OUT) to exchange network traffic. Select
62402ac6454SAndrew Thompson 	 *  an alternate interface setting to initialize the network
62502ac6454SAndrew Thompson 	 *  aspects of the device and to enable the exchange of
62602ac6454SAndrew Thompson 	 *  network traffic.
62702ac6454SAndrew Thompson 	 *
62802ac6454SAndrew Thompson 	 * </quote>
62902ac6454SAndrew Thompson 	 *
63002ac6454SAndrew Thompson 	 * Some devices, most notably cable modems, include interface
63102ac6454SAndrew Thompson 	 * settings that have no IN or OUT endpoint, therefore loop
63202ac6454SAndrew Thompson 	 * through the list of all available interface settings
63302ac6454SAndrew Thompson 	 * looking for one with both IN and OUT endpoints.
63402ac6454SAndrew Thompson 	 */
63502ac6454SAndrew Thompson 
63602ac6454SAndrew Thompson alloc_transfers:
63702ac6454SAndrew Thompson 
6384076dd23SAndrew Thompson 	pcfg = cdce_config;	/* Default Configuration */
6394076dd23SAndrew Thompson 
64002ac6454SAndrew Thompson 	for (i = 0; i != 32; i++) {
64102ac6454SAndrew Thompson 
6424076dd23SAndrew Thompson 		error = usbd_set_alt_interface_index(uaa->device,
6434076dd23SAndrew Thompson 		    sc->sc_ifaces_index[0], i);
6444076dd23SAndrew Thompson 		if (error)
6454076dd23SAndrew Thompson 			break;
6464076dd23SAndrew Thompson #if CDCE_HAVE_NCM
6474076dd23SAndrew Thompson 		if ((i == 0) && (cdce_ncm_init(sc) == 0))
6484076dd23SAndrew Thompson 			pcfg = cdce_ncm_config;
6494076dd23SAndrew Thompson #endif
6504076dd23SAndrew Thompson 		error = usbd_transfer_setup(uaa->device,
6514076dd23SAndrew Thompson 		    sc->sc_ifaces_index, sc->sc_xfer,
6524076dd23SAndrew Thompson 		    pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
65302ac6454SAndrew Thompson 
6544076dd23SAndrew Thompson 		if (error == 0)
65502ac6454SAndrew Thompson 			break;
65602ac6454SAndrew Thompson 	}
6574076dd23SAndrew Thompson 
6584076dd23SAndrew Thompson 	if (error || (i == 32)) {
6594076dd23SAndrew Thompson 		device_printf(dev, "No valid alternate "
660767cb2e2SAndrew Thompson 		    "setting found\n");
6614076dd23SAndrew Thompson 		goto detach;
66202ac6454SAndrew Thompson 	}
66302ac6454SAndrew Thompson 
6643c8d24f4SAndrew Thompson 	ued = usbd_find_descriptor
66502ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
6666d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF);
66702ac6454SAndrew Thompson 
66802ac6454SAndrew Thompson 	if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
66902ac6454SAndrew Thompson 		error = USB_ERR_INVAL;
67002ac6454SAndrew Thompson 	} else {
671a593f6b8SAndrew Thompson 		error = usbd_req_get_string_any(uaa->device, NULL,
67202ac6454SAndrew Thompson 		    eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
67302ac6454SAndrew Thompson 	}
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson 	if (error) {
67602ac6454SAndrew Thompson 
67702ac6454SAndrew Thompson 		/* fake MAC address */
67802ac6454SAndrew Thompson 
67902ac6454SAndrew Thompson 		device_printf(dev, "faking MAC address\n");
680a8df530dSJohn Baldwin 		seed = ticks;
68102ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[0] = 0x2a;
682a8df530dSJohn Baldwin 		memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
68302ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
68402ac6454SAndrew Thompson 
68502ac6454SAndrew Thompson 	} else {
68602ac6454SAndrew Thompson 
6877e05daaeSHans Petter Selasky 		memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
68802ac6454SAndrew Thompson 
68902ac6454SAndrew Thompson 		for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson 			char c = eaddr_str[i];
69202ac6454SAndrew Thompson 
69302ac6454SAndrew Thompson 			if ('0' <= c && c <= '9')
69402ac6454SAndrew Thompson 				c -= '0';
69502ac6454SAndrew Thompson 			else if (c != 0)
69602ac6454SAndrew Thompson 				c -= 'A' - 10;
69702ac6454SAndrew Thompson 			else
69802ac6454SAndrew Thompson 				break;
69902ac6454SAndrew Thompson 
70002ac6454SAndrew Thompson 			c &= 0xf;
70102ac6454SAndrew Thompson 
70202ac6454SAndrew Thompson 			if ((i & 1) == 0)
70302ac6454SAndrew Thompson 				c <<= 4;
70402ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[i / 2] |= c;
70502ac6454SAndrew Thompson 		}
70602ac6454SAndrew Thompson 
707f29a0724SAndrew Thompson 		if (uaa->usb_mode == USB_MODE_DEVICE) {
70802ac6454SAndrew Thompson 			/*
70902ac6454SAndrew Thompson 			 * Do not use the same MAC address like the peer !
71002ac6454SAndrew Thompson 			 */
71102ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[5] ^= 0xFF;
71202ac6454SAndrew Thompson 		}
71302ac6454SAndrew Thompson 	}
71402ac6454SAndrew Thompson 
71502ac6454SAndrew Thompson 	ue->ue_sc = sc;
71602ac6454SAndrew Thompson 	ue->ue_dev = dev;
71702ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
71802ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
71902ac6454SAndrew Thompson 	ue->ue_methods = &cdce_ue_methods;
72002ac6454SAndrew Thompson 
721a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
72202ac6454SAndrew Thompson 	if (error) {
72302ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
72402ac6454SAndrew Thompson 		goto detach;
72502ac6454SAndrew Thompson 	}
72602ac6454SAndrew Thompson 	return (0);			/* success */
72702ac6454SAndrew Thompson 
72802ac6454SAndrew Thompson detach:
72902ac6454SAndrew Thompson 	cdce_detach(dev);
73002ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
73102ac6454SAndrew Thompson }
73202ac6454SAndrew Thompson 
73302ac6454SAndrew Thompson static int
73402ac6454SAndrew Thompson cdce_detach(device_t dev)
73502ac6454SAndrew Thompson {
73602ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
737760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 	/* stop all USB transfers first */
740a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
741a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
74202ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
74302ac6454SAndrew Thompson 
74402ac6454SAndrew Thompson 	return (0);
74502ac6454SAndrew Thompson }
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson static void
748760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue)
74902ac6454SAndrew Thompson {
750a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
75102ac6454SAndrew Thompson 
75202ac6454SAndrew Thompson 	/*
75302ac6454SAndrew Thompson 	 * Start the USB transfers, if not already started:
75402ac6454SAndrew Thompson 	 */
755a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
756a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
75702ac6454SAndrew Thompson }
75802ac6454SAndrew Thompson 
75902ac6454SAndrew Thompson static void
76002ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n)
76102ac6454SAndrew Thompson {
76202ac6454SAndrew Thompson 	uint8_t x;
76302ac6454SAndrew Thompson 	for (x = 0; x != n; x++) {
76402ac6454SAndrew Thompson 		if (ppm[x] != NULL) {
76502ac6454SAndrew Thompson 			m_freem(ppm[x]);
76602ac6454SAndrew Thompson 			ppm[x] = NULL;
76702ac6454SAndrew Thompson 		}
76802ac6454SAndrew Thompson 	}
76902ac6454SAndrew Thompson }
77002ac6454SAndrew Thompson 
77102ac6454SAndrew Thompson static void
772ed6d949aSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
77302ac6454SAndrew Thompson {
774ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
775a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
77602ac6454SAndrew Thompson 	struct mbuf *m;
77702ac6454SAndrew Thompson 	struct mbuf *mt;
77802ac6454SAndrew Thompson 	uint32_t crc;
77902ac6454SAndrew Thompson 	uint8_t x;
780ed6d949aSAndrew Thompson 	int actlen, aframes;
781ed6d949aSAndrew Thompson 
782ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
78302ac6454SAndrew Thompson 
78402ac6454SAndrew Thompson 	DPRINTFN(1, "\n");
78502ac6454SAndrew Thompson 
78602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
78702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
788ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
789ed6d949aSAndrew Thompson 		    actlen, aframes);
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson 		ifp->if_opackets++;
79202ac6454SAndrew Thompson 
79302ac6454SAndrew Thompson 		/* free all previous TX buffers */
79402ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
79502ac6454SAndrew Thompson 
79602ac6454SAndrew Thompson 		/* FALLTHROUGH */
79702ac6454SAndrew Thompson 	case USB_ST_SETUP:
79802ac6454SAndrew Thompson tr_setup:
79902ac6454SAndrew Thompson 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
80202ac6454SAndrew Thompson 
80302ac6454SAndrew Thompson 			if (m == NULL)
80402ac6454SAndrew Thompson 				break;
80502ac6454SAndrew Thompson 
80602ac6454SAndrew Thompson 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
80702ac6454SAndrew Thompson 				/*
80802ac6454SAndrew Thompson 				 * Zaurus wants a 32-bit CRC appended
80902ac6454SAndrew Thompson 				 * to every frame
81002ac6454SAndrew Thompson 				 */
81102ac6454SAndrew Thompson 
81202ac6454SAndrew Thompson 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
81302ac6454SAndrew Thompson 				crc = htole32(crc);
81402ac6454SAndrew Thompson 
81502ac6454SAndrew Thompson 				if (!m_append(m, 4, (void *)&crc)) {
81602ac6454SAndrew Thompson 					m_freem(m);
81702ac6454SAndrew Thompson 					ifp->if_oerrors++;
81802ac6454SAndrew Thompson 					continue;
81902ac6454SAndrew Thompson 				}
82002ac6454SAndrew Thompson 			}
82102ac6454SAndrew Thompson 			if (m->m_len != m->m_pkthdr.len) {
822c6499eccSGleb Smirnoff 				mt = m_defrag(m, M_NOWAIT);
82302ac6454SAndrew Thompson 				if (mt == NULL) {
82402ac6454SAndrew Thompson 					m_freem(m);
82502ac6454SAndrew Thompson 					ifp->if_oerrors++;
82602ac6454SAndrew Thompson 					continue;
82702ac6454SAndrew Thompson 				}
82802ac6454SAndrew Thompson 				m = mt;
82902ac6454SAndrew Thompson 			}
83002ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
83102ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
83202ac6454SAndrew Thompson 			}
83302ac6454SAndrew Thompson 			sc->sc_tx_buf[x] = m;
834ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
83502ac6454SAndrew Thompson 
83602ac6454SAndrew Thompson 			/*
83702ac6454SAndrew Thompson 			 * If there's a BPF listener, bounce a copy of
83802ac6454SAndrew Thompson 			 * this frame to him:
83902ac6454SAndrew Thompson 			 */
84002ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
84102ac6454SAndrew Thompson 		}
84202ac6454SAndrew Thompson 		if (x != 0) {
843ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
844ed6d949aSAndrew Thompson 
845a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
84602ac6454SAndrew Thompson 		}
84702ac6454SAndrew Thompson 		break;
84802ac6454SAndrew Thompson 
84902ac6454SAndrew Thompson 	default:			/* Error */
85002ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
851ed6d949aSAndrew Thompson 		    usbd_errstr(error));
85202ac6454SAndrew Thompson 
85302ac6454SAndrew Thompson 		/* free all previous TX buffers */
85402ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
85502ac6454SAndrew Thompson 
85602ac6454SAndrew Thompson 		/* count output errors */
85702ac6454SAndrew Thompson 		ifp->if_oerrors++;
85802ac6454SAndrew Thompson 
859ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
86002ac6454SAndrew Thompson 			/* try to clear stall first */
861ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
86202ac6454SAndrew Thompson 			goto tr_setup;
86302ac6454SAndrew Thompson 		}
86402ac6454SAndrew Thompson 		break;
86502ac6454SAndrew Thompson 	}
86602ac6454SAndrew Thompson }
86702ac6454SAndrew Thompson 
86802ac6454SAndrew Thompson static int32_t
86902ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
87002ac6454SAndrew Thompson {
87102ac6454SAndrew Thompson 	uint32_t *p_crc = arg;
87202ac6454SAndrew Thompson 
87302ac6454SAndrew Thompson 	*p_crc = crc32_raw(src, count, *p_crc);
87402ac6454SAndrew Thompson 	return (0);
87502ac6454SAndrew Thompson }
87602ac6454SAndrew Thompson 
87702ac6454SAndrew Thompson static uint32_t
87802ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
87902ac6454SAndrew Thompson {
88002ac6454SAndrew Thompson 	uint32_t crc = 0xFFFFFFFF;
88102ac6454SAndrew Thompson 	int error;
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 	error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
88402ac6454SAndrew Thompson 	return (crc ^ 0xFFFFFFFF);
88502ac6454SAndrew Thompson }
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson static void
888760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue)
88902ac6454SAndrew Thompson {
890a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
891a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
89202ac6454SAndrew Thompson 
89302ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
89402ac6454SAndrew Thompson 
89502ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
89602ac6454SAndrew Thompson 
89702ac6454SAndrew Thompson 	/* start interrupt transfer */
898a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
899a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
90002ac6454SAndrew Thompson 
90102ac6454SAndrew Thompson 	/* stall data write direction, which depends on USB mode */
902ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
90302ac6454SAndrew Thompson 
90402ac6454SAndrew Thompson 	/* start data transfers */
90502ac6454SAndrew Thompson 	cdce_start(ue);
90602ac6454SAndrew Thompson }
90702ac6454SAndrew Thompson 
90802ac6454SAndrew Thompson static void
909760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue)
91002ac6454SAndrew Thompson {
911a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
912a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
91302ac6454SAndrew Thompson 
91402ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
91502ac6454SAndrew Thompson 
91602ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
91702ac6454SAndrew Thompson 
91802ac6454SAndrew Thompson 	/*
91902ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
92002ac6454SAndrew Thompson 	 */
921a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
922a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
923a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
924a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
92502ac6454SAndrew Thompson }
92602ac6454SAndrew Thompson 
92702ac6454SAndrew Thompson static void
928760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue)
92902ac6454SAndrew Thompson {
93002ac6454SAndrew Thompson 	/* no-op */
93102ac6454SAndrew Thompson 	return;
93202ac6454SAndrew Thompson }
93302ac6454SAndrew Thompson 
93402ac6454SAndrew Thompson static void
935760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue)
93602ac6454SAndrew Thompson {
93702ac6454SAndrew Thompson 	/* no-op */
93802ac6454SAndrew Thompson 	return;
93902ac6454SAndrew Thompson }
94002ac6454SAndrew Thompson 
94102ac6454SAndrew Thompson static int
94202ac6454SAndrew Thompson cdce_suspend(device_t dev)
94302ac6454SAndrew Thompson {
94402ac6454SAndrew Thompson 	device_printf(dev, "Suspending\n");
94502ac6454SAndrew Thompson 	return (0);
94602ac6454SAndrew Thompson }
94702ac6454SAndrew Thompson 
94802ac6454SAndrew Thompson static int
94902ac6454SAndrew Thompson cdce_resume(device_t dev)
95002ac6454SAndrew Thompson {
95102ac6454SAndrew Thompson 	device_printf(dev, "Resuming\n");
95202ac6454SAndrew Thompson 	return (0);
95302ac6454SAndrew Thompson }
95402ac6454SAndrew Thompson 
95502ac6454SAndrew Thompson static void
956ed6d949aSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
95702ac6454SAndrew Thompson {
958ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
95902ac6454SAndrew Thompson 	struct mbuf *m;
96002ac6454SAndrew Thompson 	uint8_t x;
9616d917491SHans Petter Selasky 	int actlen;
9626d917491SHans Petter Selasky 	int aframes;
9636d917491SHans Petter Selasky 	int len;
964ed6d949aSAndrew Thompson 
965ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
96602ac6454SAndrew Thompson 
96702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
96802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
96902ac6454SAndrew Thompson 
970ed6d949aSAndrew Thompson 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
97102ac6454SAndrew Thompson 
972ed6d949aSAndrew Thompson 		for (x = 0; x != aframes; x++) {
97302ac6454SAndrew Thompson 
97402ac6454SAndrew Thompson 			m = sc->sc_rx_buf[x];
97502ac6454SAndrew Thompson 			sc->sc_rx_buf[x] = NULL;
9768f9e0ef9SAndrew Thompson 			len = usbd_xfer_frame_len(xfer, x);
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson 			/* Strip off CRC added by Zaurus, if any */
979ed6d949aSAndrew Thompson 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
980ed6d949aSAndrew Thompson 				len -= 4;
98102ac6454SAndrew Thompson 
9826d917491SHans Petter Selasky 			if (len < (int)sizeof(struct ether_header)) {
98302ac6454SAndrew Thompson 				m_freem(m);
98402ac6454SAndrew Thompson 				continue;
98502ac6454SAndrew Thompson 			}
98602ac6454SAndrew Thompson 			/* queue up mbuf */
987ed6d949aSAndrew Thompson 			uether_rxmbuf(&sc->sc_ue, m, len);
98802ac6454SAndrew Thompson 		}
98902ac6454SAndrew Thompson 
99002ac6454SAndrew Thompson 		/* FALLTHROUGH */
99102ac6454SAndrew Thompson 	case USB_ST_SETUP:
99202ac6454SAndrew Thompson 		/*
99302ac6454SAndrew Thompson 		 * TODO: Implement support for multi frame transfers,
99402ac6454SAndrew Thompson 		 * when the USB hardware supports it.
99502ac6454SAndrew Thompson 		 */
99602ac6454SAndrew Thompson 		for (x = 0; x != 1; x++) {
99702ac6454SAndrew Thompson 			if (sc->sc_rx_buf[x] == NULL) {
998a593f6b8SAndrew Thompson 				m = uether_newbuf();
99902ac6454SAndrew Thompson 				if (m == NULL)
100002ac6454SAndrew Thompson 					goto tr_stall;
100102ac6454SAndrew Thompson 				sc->sc_rx_buf[x] = m;
100202ac6454SAndrew Thompson 			} else {
100302ac6454SAndrew Thompson 				m = sc->sc_rx_buf[x];
100402ac6454SAndrew Thompson 			}
100502ac6454SAndrew Thompson 
1006ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
100702ac6454SAndrew Thompson 		}
100802ac6454SAndrew Thompson 		/* set number of frames and start hardware */
1009ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, x);
1010a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
101102ac6454SAndrew Thompson 		/* flush any received frames */
1012a593f6b8SAndrew Thompson 		uether_rxflush(&sc->sc_ue);
101302ac6454SAndrew Thompson 		break;
101402ac6454SAndrew Thompson 
101502ac6454SAndrew Thompson 	default:			/* Error */
101602ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
1017ed6d949aSAndrew Thompson 		    usbd_errstr(error));
101802ac6454SAndrew Thompson 
1019ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
102002ac6454SAndrew Thompson tr_stall:
102102ac6454SAndrew Thompson 			/* try to clear stall first */
1022ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
1023ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
1024a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
102502ac6454SAndrew Thompson 			break;
102602ac6454SAndrew Thompson 		}
102702ac6454SAndrew Thompson 
102802ac6454SAndrew Thompson 		/* need to free the RX-mbufs when we are cancelled */
102902ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
103002ac6454SAndrew Thompson 		break;
103102ac6454SAndrew Thompson 	}
103202ac6454SAndrew Thompson }
103302ac6454SAndrew Thompson 
103402ac6454SAndrew Thompson static void
1035ed6d949aSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
103602ac6454SAndrew Thompson {
1037ed6d949aSAndrew Thompson 	int actlen;
1038ed6d949aSAndrew Thompson 
1039ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1040ed6d949aSAndrew Thompson 
104102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
104202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
104302ac6454SAndrew Thompson 
1044ed6d949aSAndrew Thompson 		DPRINTF("Received %d bytes\n", actlen);
104502ac6454SAndrew Thompson 
104602ac6454SAndrew Thompson 		/* TODO: decode some indications */
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson 		/* FALLTHROUGH */
104902ac6454SAndrew Thompson 	case USB_ST_SETUP:
105002ac6454SAndrew Thompson tr_setup:
1051ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1052a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
105302ac6454SAndrew Thompson 		break;
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson 	default:			/* Error */
1056ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
105702ac6454SAndrew Thompson 			/* start clear stall */
1058ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
105902ac6454SAndrew Thompson 			goto tr_setup;
106002ac6454SAndrew Thompson 		}
106102ac6454SAndrew Thompson 		break;
106202ac6454SAndrew Thompson 	}
106302ac6454SAndrew Thompson }
106402ac6454SAndrew Thompson 
106502ac6454SAndrew Thompson static void
1066ed6d949aSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
106702ac6454SAndrew Thompson {
1068ed6d949aSAndrew Thompson 	int actlen;
1069ed6d949aSAndrew Thompson 
1070ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1071ed6d949aSAndrew Thompson 
107202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
107302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
107402ac6454SAndrew Thompson 
1075ed6d949aSAndrew Thompson 		DPRINTF("Transferred %d bytes\n", actlen);
107602ac6454SAndrew Thompson 
107702ac6454SAndrew Thompson 		/* FALLTHROUGH */
107802ac6454SAndrew Thompson 	case USB_ST_SETUP:
107902ac6454SAndrew Thompson tr_setup:
108002ac6454SAndrew Thompson #if 0
1081ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, XXX);
1082a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
108302ac6454SAndrew Thompson #endif
108402ac6454SAndrew Thompson 		break;
108502ac6454SAndrew Thompson 
108602ac6454SAndrew Thompson 	default:			/* Error */
1087ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
108802ac6454SAndrew Thompson 			/* start clear stall */
1089ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
109002ac6454SAndrew Thompson 			goto tr_setup;
109102ac6454SAndrew Thompson 		}
109202ac6454SAndrew Thompson 		break;
109302ac6454SAndrew Thompson 	}
109402ac6454SAndrew Thompson }
109502ac6454SAndrew Thompson 
109602ac6454SAndrew Thompson static int
109702ac6454SAndrew Thompson cdce_handle_request(device_t dev,
109802ac6454SAndrew Thompson     const void *req, void **pptr, uint16_t *plen,
109929bd7d7eSAndrew Thompson     uint16_t offset, uint8_t *pstate)
110002ac6454SAndrew Thompson {
110102ac6454SAndrew Thompson 	return (ENXIO);			/* use builtin handler */
110202ac6454SAndrew Thompson }
11034076dd23SAndrew Thompson 
11044076dd23SAndrew Thompson #if CDCE_HAVE_NCM
11057e05daaeSHans Petter Selasky static void
11067e05daaeSHans Petter Selasky cdce_ncm_tx_zero(struct usb_page_cache *pc,
11077e05daaeSHans Petter Selasky     uint32_t start, uint32_t end)
11087e05daaeSHans Petter Selasky {
11097e05daaeSHans Petter Selasky 	if (start >= CDCE_NCM_TX_MAXLEN)
11107e05daaeSHans Petter Selasky 		return;
11117e05daaeSHans Petter Selasky 	if (end > CDCE_NCM_TX_MAXLEN)
11127e05daaeSHans Petter Selasky 		end = CDCE_NCM_TX_MAXLEN;
11137e05daaeSHans Petter Selasky 
11147e05daaeSHans Petter Selasky 	usbd_frame_zero(pc, start, end - start);
11157e05daaeSHans Petter Selasky }
11167e05daaeSHans Petter Selasky 
11174076dd23SAndrew Thompson static uint8_t
11184076dd23SAndrew Thompson cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
11194076dd23SAndrew Thompson {
11204076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
11214076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
11224076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
11234076dd23SAndrew Thompson 	struct mbuf *m;
11244076dd23SAndrew Thompson 	uint32_t rem;
11254076dd23SAndrew Thompson 	uint32_t offset;
11264076dd23SAndrew Thompson 	uint32_t last_offset;
11277e05daaeSHans Petter Selasky 	uint16_t n;
11287e05daaeSHans Petter Selasky 	uint8_t retval;
11294076dd23SAndrew Thompson 
11304076dd23SAndrew Thompson 	usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
11314076dd23SAndrew Thompson 
11324076dd23SAndrew Thompson 	offset = sizeof(sc->sc_ncm.hdr) +
11334076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
11344076dd23SAndrew Thompson 
11354076dd23SAndrew Thompson 	/* Store last valid offset before alignment */
11364076dd23SAndrew Thompson 	last_offset = offset;
11374076dd23SAndrew Thompson 
11387e05daaeSHans Petter Selasky 	/* Align offset */
11397e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
11407e05daaeSHans Petter Selasky 	    offset, sc->sc_ncm.tx_modulus);
11414076dd23SAndrew Thompson 
11427e05daaeSHans Petter Selasky 	/* Zero pad */
11437e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
11447e05daaeSHans Petter Selasky 
11457e05daaeSHans Petter Selasky 	/* buffer full */
11467e05daaeSHans Petter Selasky 	retval = 2;
11477e05daaeSHans Petter Selasky 
11487e05daaeSHans Petter Selasky 	for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
11494076dd23SAndrew Thompson 
11504076dd23SAndrew Thompson 		/* check if end of transmit buffer is reached */
11514076dd23SAndrew Thompson 
11524076dd23SAndrew Thompson 		if (offset >= sc->sc_ncm.tx_max)
11534076dd23SAndrew Thompson 			break;
11544076dd23SAndrew Thompson 
11554076dd23SAndrew Thompson 		/* compute maximum buffer size */
11564076dd23SAndrew Thompson 
11574076dd23SAndrew Thompson 		rem = sc->sc_ncm.tx_max - offset;
11584076dd23SAndrew Thompson 
11594076dd23SAndrew Thompson 		IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
11604076dd23SAndrew Thompson 
11617e05daaeSHans Petter Selasky 		if (m == NULL) {
11627e05daaeSHans Petter Selasky 			/* buffer not full */
11637e05daaeSHans Petter Selasky 			retval = 1;
11644076dd23SAndrew Thompson 			break;
11657e05daaeSHans Petter Selasky 		}
11664076dd23SAndrew Thompson 
11676d917491SHans Petter Selasky 		if (m->m_pkthdr.len > (int)rem) {
11684076dd23SAndrew Thompson 			if (n == 0) {
11694076dd23SAndrew Thompson 				/* The frame won't fit in our buffer */
11704076dd23SAndrew Thompson 				DPRINTFN(1, "Frame too big to be transmitted!\n");
11714076dd23SAndrew Thompson 				m_freem(m);
11724076dd23SAndrew Thompson 				ifp->if_oerrors++;
11734076dd23SAndrew Thompson 				n--;
11744076dd23SAndrew Thompson 				continue;
11754076dd23SAndrew Thompson 			}
11764076dd23SAndrew Thompson 			/* Wait till next buffer becomes ready */
11774076dd23SAndrew Thompson 			IFQ_DRV_PREPEND(&(ifp->if_snd), m);
11784076dd23SAndrew Thompson 			break;
11794076dd23SAndrew Thompson 		}
11804076dd23SAndrew Thompson 		usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
11814076dd23SAndrew Thompson 
11824076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
11834076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
11844076dd23SAndrew Thompson 
11854076dd23SAndrew Thompson 		/* Update offset */
11864076dd23SAndrew Thompson 		offset += m->m_pkthdr.len;
11874076dd23SAndrew Thompson 
11884076dd23SAndrew Thompson 		/* Store last valid offset before alignment */
11894076dd23SAndrew Thompson 		last_offset = offset;
11904076dd23SAndrew Thompson 
11917e05daaeSHans Petter Selasky 		/* Align offset */
11927e05daaeSHans Petter Selasky 		offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
11937e05daaeSHans Petter Selasky 		    offset, sc->sc_ncm.tx_modulus);
11947e05daaeSHans Petter Selasky 
11957e05daaeSHans Petter Selasky 		/* Zero pad */
11967e05daaeSHans Petter Selasky 		cdce_ncm_tx_zero(pc, last_offset, offset);
11974076dd23SAndrew Thompson 
11984076dd23SAndrew Thompson 		/*
11994076dd23SAndrew Thompson 		 * If there's a BPF listener, bounce a copy
12004076dd23SAndrew Thompson 		 * of this frame to him:
12014076dd23SAndrew Thompson 		 */
12024076dd23SAndrew Thompson 		BPF_MTAP(ifp, m);
12034076dd23SAndrew Thompson 
12044076dd23SAndrew Thompson 		/* Free mbuf */
12054076dd23SAndrew Thompson 
12064076dd23SAndrew Thompson 		m_freem(m);
12074076dd23SAndrew Thompson 
12084076dd23SAndrew Thompson 		/* Pre-increment interface counter */
12094076dd23SAndrew Thompson 
12104076dd23SAndrew Thompson 		ifp->if_opackets++;
12114076dd23SAndrew Thompson 	}
12124076dd23SAndrew Thompson 
12134076dd23SAndrew Thompson 	if (n == 0)
12147e05daaeSHans Petter Selasky 		return (0);
12154076dd23SAndrew Thompson 
12164076dd23SAndrew Thompson 	rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
12174076dd23SAndrew Thompson 
12184076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wLength, rem);
12194076dd23SAndrew Thompson 
12204076dd23SAndrew Thompson 	/* zero the rest of the data pointer entries */
12214076dd23SAndrew Thompson 	for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
12224076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
12234076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
12244076dd23SAndrew Thompson 	}
12254076dd23SAndrew Thompson 
12267e05daaeSHans Petter Selasky 	offset = last_offset;
12277e05daaeSHans Petter Selasky 
12287e05daaeSHans Petter Selasky 	/* Align offset */
12297e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
12307e05daaeSHans Petter Selasky 
12317e05daaeSHans Petter Selasky 	/* Optimise, save bandwidth and force short termination */
12327e05daaeSHans Petter Selasky 	if (offset >= sc->sc_ncm.tx_max)
12337e05daaeSHans Petter Selasky 		offset = sc->sc_ncm.tx_max;
12347e05daaeSHans Petter Selasky 	else
12357e05daaeSHans Petter Selasky 		offset ++;
12367e05daaeSHans Petter Selasky 
12377e05daaeSHans Petter Selasky 	/* Zero pad */
12387e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
12397e05daaeSHans Petter Selasky 
12404076dd23SAndrew Thompson 	/* set frame length */
12417e05daaeSHans Petter Selasky 	usbd_xfer_set_frame_len(xfer, index, offset);
12424076dd23SAndrew Thompson 
12434076dd23SAndrew Thompson 	/* Fill out 16-bit header */
12444076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[0] = 'N';
12454076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[1] = 'C';
12464076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[2] = 'M';
12474076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[3] = 'H';
12484076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
12497e05daaeSHans Petter Selasky 	USETW(sc->sc_ncm.hdr.wBlockLength, offset);
12504076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
12514076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
12524076dd23SAndrew Thompson 
12534076dd23SAndrew Thompson 	sc->sc_ncm.tx_seq++;
12544076dd23SAndrew Thompson 
12554076dd23SAndrew Thompson 	/* Fill out 16-bit frame table header */
12564076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[0] = 'N';
12574076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[1] = 'C';
12584076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[2] = 'M';
125985e3d588SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[3] = '0';
12604076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0);		/* reserved */
12614076dd23SAndrew Thompson 
12624076dd23SAndrew Thompson 	usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
12634076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
12644076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt));
12654076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
12664076dd23SAndrew Thompson 	    &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
12677e05daaeSHans Petter Selasky 	return (retval);
12684076dd23SAndrew Thompson }
12694076dd23SAndrew Thompson 
12704076dd23SAndrew Thompson static void
12714076dd23SAndrew Thompson cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
12724076dd23SAndrew Thompson {
12734076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
12744076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
12754076dd23SAndrew Thompson 	uint16_t x;
12767e05daaeSHans Petter Selasky 	uint8_t temp;
12774076dd23SAndrew Thompson 	int actlen;
12784076dd23SAndrew Thompson 	int aframes;
12794076dd23SAndrew Thompson 
12804076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
12814076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
12824076dd23SAndrew Thompson 
12834076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
12844076dd23SAndrew Thompson 
12854076dd23SAndrew Thompson 		DPRINTFN(10, "transfer complete: "
12864076dd23SAndrew Thompson 		    "%u bytes in %u frames\n", actlen, aframes);
12874076dd23SAndrew Thompson 
12884076dd23SAndrew Thompson 	case USB_ST_SETUP:
12894076dd23SAndrew Thompson 		for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
12907e05daaeSHans Petter Selasky 			temp = cdce_ncm_fill_tx_frames(xfer, x);
12917e05daaeSHans Petter Selasky 			if (temp == 0)
12924076dd23SAndrew Thompson 				break;
12937e05daaeSHans Petter Selasky 			if (temp == 1) {
12947e05daaeSHans Petter Selasky 				x++;
12957e05daaeSHans Petter Selasky 				break;
12967e05daaeSHans Petter Selasky 			}
12974076dd23SAndrew Thompson 		}
12984076dd23SAndrew Thompson 
12994076dd23SAndrew Thompson 		if (x != 0) {
13007e05daaeSHans Petter Selasky #ifdef USB_DEBUG
13017e05daaeSHans Petter Selasky 			usbd_xfer_set_interval(xfer, cdce_tx_interval);
13027e05daaeSHans Petter Selasky #endif
13034076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
13044076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
13054076dd23SAndrew Thompson 		}
13064076dd23SAndrew Thompson 		break;
13074076dd23SAndrew Thompson 
13084076dd23SAndrew Thompson 	default:			/* Error */
13094076dd23SAndrew Thompson 		DPRINTFN(10, "Transfer error: %s\n",
13104076dd23SAndrew Thompson 		    usbd_errstr(error));
13114076dd23SAndrew Thompson 
13124076dd23SAndrew Thompson 		/* update error counter */
13134076dd23SAndrew Thompson 		ifp->if_oerrors += 1;
13144076dd23SAndrew Thompson 
13154076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
13164076dd23SAndrew Thompson 			/* try to clear stall first */
13174076dd23SAndrew Thompson 			usbd_xfer_set_stall(xfer);
13184076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
13194076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
13204076dd23SAndrew Thompson 		}
13214076dd23SAndrew Thompson 		break;
13224076dd23SAndrew Thompson 	}
13234076dd23SAndrew Thompson }
13244076dd23SAndrew Thompson 
13254076dd23SAndrew Thompson static void
13264076dd23SAndrew Thompson cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
13274076dd23SAndrew Thompson {
13284076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
13294076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
13304076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
13314076dd23SAndrew Thompson 	struct mbuf *m;
13324076dd23SAndrew Thompson 	int sumdata;
13334076dd23SAndrew Thompson 	int sumlen;
13344076dd23SAndrew Thompson 	int actlen;
13354076dd23SAndrew Thompson 	int aframes;
13364076dd23SAndrew Thompson 	int temp;
13374076dd23SAndrew Thompson 	int nframes;
13384076dd23SAndrew Thompson 	int x;
13394076dd23SAndrew Thompson 	int offset;
13404076dd23SAndrew Thompson 
13414076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
13424076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
13434076dd23SAndrew Thompson 
13444076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
13454076dd23SAndrew Thompson 
13464076dd23SAndrew Thompson 		DPRINTFN(1, "received %u bytes in %u frames\n",
13474076dd23SAndrew Thompson 		    actlen, aframes);
13484076dd23SAndrew Thompson 
13496d917491SHans Petter Selasky 		if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
13504076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt))) {
13514076dd23SAndrew Thompson 			DPRINTFN(1, "frame too short\n");
135285e3d588SAndrew Thompson 			goto tr_setup;
13534076dd23SAndrew Thompson 		}
13544076dd23SAndrew Thompson 		usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
13554076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.hdr));
13564076dd23SAndrew Thompson 
13574076dd23SAndrew Thompson 		if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
13584076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
13594076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
13604076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
136185e3d588SAndrew Thompson 			DPRINTFN(1, "invalid HDR signature: "
136285e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
136385e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[0],
136485e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[1],
136585e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[2],
136685e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[3]);
13674076dd23SAndrew Thompson 			goto tr_stall;
13684076dd23SAndrew Thompson 		}
13694076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
13704076dd23SAndrew Thompson 		if (temp > sumlen) {
13714076dd23SAndrew Thompson 			DPRINTFN(1, "unsupported block length %u/%u\n",
13724076dd23SAndrew Thompson 			    temp, sumlen);
13734076dd23SAndrew Thompson 			goto tr_stall;
13744076dd23SAndrew Thompson 		}
13754076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
13766d917491SHans Petter Selasky 		if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
137785e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
13784076dd23SAndrew Thompson 			goto tr_stall;
13794076dd23SAndrew Thompson 		}
13804076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
13814076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt));
13824076dd23SAndrew Thompson 
13834076dd23SAndrew Thompson 		if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
13844076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
13854076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
138685e3d588SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
138785e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT signature"
138885e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
138985e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[0],
139085e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[1],
139185e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[2],
139285e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[3]);
13934076dd23SAndrew Thompson 			goto tr_stall;
13944076dd23SAndrew Thompson 		}
13954076dd23SAndrew Thompson 		nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
13964076dd23SAndrew Thompson 
13974076dd23SAndrew Thompson 		/* Subtract size of header and last zero padded entry */
13984076dd23SAndrew Thompson 		if (nframes >= (2 + 1))
13994076dd23SAndrew Thompson 			nframes -= (2 + 1);
14004076dd23SAndrew Thompson 		else
14014076dd23SAndrew Thompson 			nframes = 0;
14024076dd23SAndrew Thompson 
14034076dd23SAndrew Thompson 		DPRINTFN(1, "nframes = %u\n", nframes);
14044076dd23SAndrew Thompson 
14054076dd23SAndrew Thompson 		temp += sizeof(sc->sc_ncm.dpt);
14064076dd23SAndrew Thompson 
14074076dd23SAndrew Thompson 		if ((temp + (4 * nframes)) > actlen)
14084076dd23SAndrew Thompson 			goto tr_stall;
14094076dd23SAndrew Thompson 
14104076dd23SAndrew Thompson 		if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
14114076dd23SAndrew Thompson 			DPRINTFN(1, "Truncating number of frames from %u to %u\n",
14124076dd23SAndrew Thompson 			    nframes, CDCE_NCM_SUBFRAMES_MAX);
14134076dd23SAndrew Thompson 			nframes = CDCE_NCM_SUBFRAMES_MAX;
14144076dd23SAndrew Thompson 		}
14154076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
14164076dd23SAndrew Thompson 
14174076dd23SAndrew Thompson 		sumdata = 0;
14184076dd23SAndrew Thompson 
14194076dd23SAndrew Thompson 		for (x = 0; x != nframes; x++) {
14204076dd23SAndrew Thompson 
14214076dd23SAndrew Thompson 			offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
14224076dd23SAndrew Thompson 			temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
14234076dd23SAndrew Thompson 
142429051223SAndrew Thompson 			if ((offset == 0) ||
14256d917491SHans Petter Selasky 			    (temp < (int)sizeof(struct ether_header)) ||
142629051223SAndrew Thompson 			    (temp > (MCLBYTES - ETHER_ALIGN))) {
142729051223SAndrew Thompson 				DPRINTFN(1, "NULL frame detected at %d\n", x);
14284076dd23SAndrew Thompson 				m = NULL;
142929051223SAndrew Thompson 				/* silently ignore this frame */
14304076dd23SAndrew Thompson 				continue;
143129051223SAndrew Thompson 			} else if ((offset + temp) > actlen) {
143229051223SAndrew Thompson 				DPRINTFN(1, "invalid frame "
143329051223SAndrew Thompson 				    "detected at %d\n", x);
143429051223SAndrew Thompson 				m = NULL;
143529051223SAndrew Thompson 				/* silently ignore this frame */
143629051223SAndrew Thompson 				continue;
14376d917491SHans Petter Selasky 			} else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1438c6499eccSGleb Smirnoff 				m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
14394076dd23SAndrew Thompson 			} else {
1440c6499eccSGleb Smirnoff 				m = m_gethdr(M_NOWAIT, MT_DATA);
14414076dd23SAndrew Thompson 			}
14424076dd23SAndrew Thompson 
14434076dd23SAndrew Thompson 			DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
14444076dd23SAndrew Thompson 			    x, offset, temp);
14454076dd23SAndrew Thompson 
14464076dd23SAndrew Thompson 			/* check if we have a buffer */
14474076dd23SAndrew Thompson 			if (m) {
14484076dd23SAndrew Thompson 				m_adj(m, ETHER_ALIGN);
14494076dd23SAndrew Thompson 
14504076dd23SAndrew Thompson 				usbd_copy_out(pc, offset, m->m_data, temp);
14514076dd23SAndrew Thompson 
14524076dd23SAndrew Thompson 				/* enqueue */
14534076dd23SAndrew Thompson 				uether_rxmbuf(&sc->sc_ue, m, temp);
14544076dd23SAndrew Thompson 
14554076dd23SAndrew Thompson 				sumdata += temp;
14564076dd23SAndrew Thompson 			} else {
14574076dd23SAndrew Thompson 				ifp->if_ierrors++;
14584076dd23SAndrew Thompson 			}
14594076dd23SAndrew Thompson 		}
14604076dd23SAndrew Thompson 
14614076dd23SAndrew Thompson 		DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
14624076dd23SAndrew Thompson 
14634076dd23SAndrew Thompson 	case USB_ST_SETUP:
146485e3d588SAndrew Thompson tr_setup:
14654076dd23SAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
14664076dd23SAndrew Thompson 		usbd_xfer_set_frames(xfer, 1);
14674076dd23SAndrew Thompson 		usbd_transfer_submit(xfer);
14684076dd23SAndrew Thompson 		uether_rxflush(&sc->sc_ue);	/* must be last */
14694076dd23SAndrew Thompson 		break;
14704076dd23SAndrew Thompson 
14714076dd23SAndrew Thompson 	default:			/* Error */
14724076dd23SAndrew Thompson 		DPRINTFN(1, "error = %s\n",
14734076dd23SAndrew Thompson 		    usbd_errstr(error));
14744076dd23SAndrew Thompson 
14754076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
14764076dd23SAndrew Thompson tr_stall:
14774076dd23SAndrew Thompson 			/* try to clear stall first */
14784076dd23SAndrew Thompson 			usbd_xfer_set_stall(xfer);
14794076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
14804076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
14814076dd23SAndrew Thompson 		}
14824076dd23SAndrew Thompson 		break;
14834076dd23SAndrew Thompson 	}
14844076dd23SAndrew Thompson }
14854076dd23SAndrew Thompson #endif
1486