xref: /freebsd/sys/dev/usb/net/if_cdce.c (revision a8df530ddc1c97a009f26d18219c6e84d976c2aa)
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>
57ed6d949aSAndrew Thompson #include <sys/kernel.h>
58ed6d949aSAndrew Thompson #include <sys/bus.h>
59ed6d949aSAndrew Thompson #include <sys/module.h>
60ed6d949aSAndrew Thompson #include <sys/lock.h>
61ed6d949aSAndrew Thompson #include <sys/mutex.h>
62ed6d949aSAndrew Thompson #include <sys/condvar.h>
63ed6d949aSAndrew Thompson #include <sys/sysctl.h>
64ed6d949aSAndrew Thompson #include <sys/sx.h>
65ed6d949aSAndrew Thompson #include <sys/unistd.h>
66ed6d949aSAndrew Thompson #include <sys/callout.h>
67ed6d949aSAndrew Thompson #include <sys/malloc.h>
68ed6d949aSAndrew Thompson #include <sys/priv.h>
69ed6d949aSAndrew Thompson 
7002ac6454SAndrew Thompson #include <dev/usb/usb.h>
71ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
72ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
7302ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
74ed6d949aSAndrew Thompson #include "usbdevs.h"
7502ac6454SAndrew Thompson 
7602ac6454SAndrew Thompson #define	USB_DEBUG_VAR cdce_debug
7702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
78ed6d949aSAndrew Thompson #include <dev/usb/usb_process.h>
79ed6d949aSAndrew Thompson #include "usb_if.h"
8002ac6454SAndrew Thompson 
8102ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h>
8202ac6454SAndrew Thompson #include <dev/usb/net/if_cdcereg.h>
8302ac6454SAndrew Thompson 
8402ac6454SAndrew Thompson static device_probe_t cdce_probe;
8502ac6454SAndrew Thompson static device_attach_t cdce_attach;
8602ac6454SAndrew Thompson static device_detach_t cdce_detach;
8702ac6454SAndrew Thompson static device_suspend_t cdce_suspend;
8802ac6454SAndrew Thompson static device_resume_t cdce_resume;
8902ac6454SAndrew Thompson static usb_handle_request_t cdce_handle_request;
9002ac6454SAndrew Thompson 
91e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_write_callback;
92e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_read_callback;
93e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_read_callback;
94e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_write_callback;
9502ac6454SAndrew Thompson 
964076dd23SAndrew Thompson #if CDCE_HAVE_NCM
974076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_write_callback;
984076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_read_callback;
994076dd23SAndrew Thompson #endif
1004076dd23SAndrew Thompson 
101e0a69b51SAndrew Thompson static uether_fn_t cdce_attach_post;
102e0a69b51SAndrew Thompson static uether_fn_t cdce_init;
103e0a69b51SAndrew Thompson static uether_fn_t cdce_stop;
104e0a69b51SAndrew Thompson static uether_fn_t cdce_start;
105e0a69b51SAndrew Thompson static uether_fn_t cdce_setmulti;
106e0a69b51SAndrew Thompson static uether_fn_t cdce_setpromisc;
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson static uint32_t	cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
10902ac6454SAndrew Thompson 
110b850ecc1SAndrew Thompson #ifdef USB_DEBUG
11102ac6454SAndrew Thompson static int cdce_debug = 0;
1127e05daaeSHans Petter Selasky static int cdce_tx_interval = 0;
11302ac6454SAndrew Thompson 
1146472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
1159360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0,
11602ac6454SAndrew Thompson     "Debug level");
1177e05daaeSHans Petter Selasky SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RW, &cdce_tx_interval, 0,
1187e05daaeSHans Petter Selasky     "NCM transmit interval in ms");
11902ac6454SAndrew Thompson #endif
12002ac6454SAndrew Thompson 
121760bc48eSAndrew Thompson static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
12202ac6454SAndrew Thompson 
1234eae601eSAndrew Thompson 	[CDCE_BULK_RX] = {
12402ac6454SAndrew Thompson 		.type = UE_BULK,
12502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1264eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
12702ac6454SAndrew Thompson 		.if_index = 0,
1284eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1294eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1304eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
1314eae601eSAndrew Thompson 		.callback = cdce_bulk_read_callback,
1324eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
133f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
13402ac6454SAndrew Thompson 	},
13502ac6454SAndrew Thompson 
1364eae601eSAndrew Thompson 	[CDCE_BULK_TX] = {
13702ac6454SAndrew Thompson 		.type = UE_BULK,
13802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1394eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
14002ac6454SAndrew Thompson 		.if_index = 0,
1414eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1424eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1434eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
1444eae601eSAndrew Thompson 		.callback = cdce_bulk_write_callback,
1454eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
146f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
14702ac6454SAndrew Thompson 	},
14802ac6454SAndrew Thompson 
1494eae601eSAndrew Thompson 	[CDCE_INTR_RX] = {
15002ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
15102ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1524eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
15302ac6454SAndrew Thompson 		.if_index = 1,
1544eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1554eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
1564eae601eSAndrew Thompson 		.callback = cdce_intr_read_callback,
1574eae601eSAndrew Thompson 		.timeout = 0,
1584eae601eSAndrew Thompson 		.usb_mode = USB_MODE_HOST,
1594eae601eSAndrew Thompson 	},
1604eae601eSAndrew Thompson 
1614eae601eSAndrew Thompson 	[CDCE_INTR_TX] = {
1624eae601eSAndrew Thompson 		.type = UE_INTERRUPT,
1634eae601eSAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1644eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
1654eae601eSAndrew Thompson 		.if_index = 1,
1664eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1674eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
1684eae601eSAndrew Thompson 		.callback = cdce_intr_write_callback,
1694eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
1704eae601eSAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
17102ac6454SAndrew Thompson 	},
17202ac6454SAndrew Thompson };
17302ac6454SAndrew Thompson 
1744076dd23SAndrew Thompson #if CDCE_HAVE_NCM
1754076dd23SAndrew Thompson static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
1764076dd23SAndrew Thompson 
1774076dd23SAndrew Thompson 	[CDCE_BULK_RX] = {
1784076dd23SAndrew Thompson 		.type = UE_BULK,
1794076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1804076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
1814076dd23SAndrew Thompson 		.if_index = 0,
1824076dd23SAndrew Thompson 		.frames = CDCE_NCM_RX_FRAMES_MAX,
1834076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
1844076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
1854076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_read_callback,
1864076dd23SAndrew Thompson 		.timeout = 0,	/* no timeout */
1874076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
1884076dd23SAndrew Thompson 	},
1894076dd23SAndrew Thompson 
1904076dd23SAndrew Thompson 	[CDCE_BULK_TX] = {
1914076dd23SAndrew Thompson 		.type = UE_BULK,
1924076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1934076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
1944076dd23SAndrew Thompson 		.if_index = 0,
1954076dd23SAndrew Thompson 		.frames = CDCE_NCM_TX_FRAMES_MAX,
1964076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
1977e05daaeSHans Petter Selasky 		.flags = {.pipe_bof = 1,},
1984076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_write_callback,
1994076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2004076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
2014076dd23SAndrew Thompson 	},
2024076dd23SAndrew Thompson 
2034076dd23SAndrew Thompson 	[CDCE_INTR_RX] = {
2044076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2054076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2064076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
2074076dd23SAndrew Thompson 		.if_index = 1,
2084076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2094076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
2104076dd23SAndrew Thompson 		.callback = cdce_intr_read_callback,
2114076dd23SAndrew Thompson 		.timeout = 0,
2124076dd23SAndrew Thompson 		.usb_mode = USB_MODE_HOST,
2134076dd23SAndrew Thompson 	},
2144076dd23SAndrew Thompson 
2154076dd23SAndrew Thompson 	[CDCE_INTR_TX] = {
2164076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2174076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2184076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
2194076dd23SAndrew Thompson 		.if_index = 1,
2204076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2214076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
2224076dd23SAndrew Thompson 		.callback = cdce_intr_write_callback,
2234076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2244076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
2254076dd23SAndrew Thompson 	},
2264076dd23SAndrew Thompson };
2274076dd23SAndrew Thompson #endif
2284076dd23SAndrew Thompson 
22902ac6454SAndrew Thompson static device_method_t cdce_methods[] = {
23002ac6454SAndrew Thompson 	/* USB interface */
23102ac6454SAndrew Thompson 	DEVMETHOD(usb_handle_request, cdce_handle_request),
23202ac6454SAndrew Thompson 
23302ac6454SAndrew Thompson 	/* Device interface */
23402ac6454SAndrew Thompson 	DEVMETHOD(device_probe, cdce_probe),
23502ac6454SAndrew Thompson 	DEVMETHOD(device_attach, cdce_attach),
23602ac6454SAndrew Thompson 	DEVMETHOD(device_detach, cdce_detach),
23702ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, cdce_suspend),
23802ac6454SAndrew Thompson 	DEVMETHOD(device_resume, cdce_resume),
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 	{0, 0}
24102ac6454SAndrew Thompson };
24202ac6454SAndrew Thompson 
24302ac6454SAndrew Thompson static driver_t cdce_driver = {
24402ac6454SAndrew Thompson 	.name = "cdce",
24502ac6454SAndrew Thompson 	.methods = cdce_methods,
24602ac6454SAndrew Thompson 	.size = sizeof(struct cdce_softc),
24702ac6454SAndrew Thompson };
24802ac6454SAndrew Thompson 
24902ac6454SAndrew Thompson static devclass_t cdce_devclass;
25002ac6454SAndrew Thompson 
2519aef556dSAndrew Thompson DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0);
25202ac6454SAndrew Thompson MODULE_VERSION(cdce, 1);
25302ac6454SAndrew Thompson MODULE_DEPEND(cdce, uether, 1, 1, 1);
25402ac6454SAndrew Thompson MODULE_DEPEND(cdce, usb, 1, 1, 1);
25502ac6454SAndrew Thompson MODULE_DEPEND(cdce, ether, 1, 1, 1);
25602ac6454SAndrew Thompson 
257760bc48eSAndrew Thompson static const struct usb_ether_methods cdce_ue_methods = {
25802ac6454SAndrew Thompson 	.ue_attach_post = cdce_attach_post,
25902ac6454SAndrew Thompson 	.ue_start = cdce_start,
26002ac6454SAndrew Thompson 	.ue_init = cdce_init,
26102ac6454SAndrew Thompson 	.ue_stop = cdce_stop,
26202ac6454SAndrew Thompson 	.ue_setmulti = cdce_setmulti,
26302ac6454SAndrew Thompson 	.ue_setpromisc = cdce_setpromisc,
26402ac6454SAndrew Thompson };
26502ac6454SAndrew Thompson 
266f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
26702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
26802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
26902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
27002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
27102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
27402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
27502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
27602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
280f1a16106SHans Petter Selasky };
2819739167cSAlfred Perlstein 
282f1a16106SHans Petter Selasky static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
2839739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
2849739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
2854076dd23SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
28602ac6454SAndrew Thompson };
28702ac6454SAndrew Thompson 
2884076dd23SAndrew Thompson #if CDCE_HAVE_NCM
2894076dd23SAndrew Thompson /*------------------------------------------------------------------------*
2904076dd23SAndrew Thompson  *	cdce_ncm_init
2914076dd23SAndrew Thompson  *
2924076dd23SAndrew Thompson  * Return values:
2934076dd23SAndrew Thompson  * 0: Success
2944076dd23SAndrew Thompson  * Else: Failure
2954076dd23SAndrew Thompson  *------------------------------------------------------------------------*/
2964076dd23SAndrew Thompson static uint8_t
2974076dd23SAndrew Thompson cdce_ncm_init(struct cdce_softc *sc)
2984076dd23SAndrew Thompson {
2994076dd23SAndrew Thompson 	struct usb_ncm_parameters temp;
3004076dd23SAndrew Thompson 	struct usb_device_request req;
3017e05daaeSHans Petter Selasky 	struct usb_ncm_func_descriptor *ufd;
3027e05daaeSHans Petter Selasky 	uint8_t value[8];
3034076dd23SAndrew Thompson 	int err;
3044076dd23SAndrew Thompson 
3057e05daaeSHans Petter Selasky 	ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
3066d917491SHans Petter Selasky 	    sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF,
3076d917491SHans Petter Selasky 	    UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF);
3087e05daaeSHans Petter Selasky 
3097e05daaeSHans Petter Selasky 	/* verify length of NCM functional descriptor */
3107e05daaeSHans Petter Selasky 	if (ufd != NULL) {
3117e05daaeSHans Petter Selasky 		if (ufd->bLength < sizeof(*ufd))
3127e05daaeSHans Petter Selasky 			ufd = NULL;
3137e05daaeSHans Petter Selasky 		else
3147e05daaeSHans Petter Selasky 			DPRINTFN(1, "Found NCM functional descriptor.\n");
3157e05daaeSHans Petter Selasky 	}
3167e05daaeSHans Petter Selasky 
3174076dd23SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
3184076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
3194076dd23SAndrew Thompson 	USETW(req.wValue, 0);
3204076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
3214076dd23SAndrew Thompson 	req.wIndex[1] = 0;
3224076dd23SAndrew Thompson 	USETW(req.wLength, sizeof(temp));
3234076dd23SAndrew Thompson 
3244076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
3254076dd23SAndrew Thompson 	    &temp, 0, NULL, 1000 /* ms */);
3264076dd23SAndrew Thompson 	if (err)
3274076dd23SAndrew Thompson 		return (1);
3284076dd23SAndrew Thompson 
3294076dd23SAndrew Thompson 	/* Read correct set of parameters according to device mode */
3304076dd23SAndrew Thompson 
3314076dd23SAndrew Thompson 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
332dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
333dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
3344076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
3354076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
3364076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
3377e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3384076dd23SAndrew Thompson 	} else {
339dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
340dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
3414076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
3424076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
3434076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
3447e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3454076dd23SAndrew Thompson 	}
3464076dd23SAndrew Thompson 
3474076dd23SAndrew Thompson 	/* Verify maximum receive length */
3484076dd23SAndrew Thompson 
3497e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.rx_max < 32) ||
3504076dd23SAndrew Thompson 	    (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
3514076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum receive length\n");
3524076dd23SAndrew Thompson 		sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
3534076dd23SAndrew Thompson 	}
3544076dd23SAndrew Thompson 
3554076dd23SAndrew Thompson 	/* Verify maximum transmit length */
3564076dd23SAndrew Thompson 
3577e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_max < 32) ||
3584076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
3594076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum transmit length\n");
3604076dd23SAndrew Thompson 		sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
3614076dd23SAndrew Thompson 	}
3624076dd23SAndrew Thompson 
3634076dd23SAndrew Thompson 	/*
3644076dd23SAndrew Thompson 	 * Verify that the structure alignment is:
3654076dd23SAndrew Thompson 	 * - power of two
3664076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
3674076dd23SAndrew Thompson 	 * - not less than four bytes
3684076dd23SAndrew Thompson 	 */
3697e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_struct_align < 4) ||
3704076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align !=
3714076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
3724076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
3734076dd23SAndrew Thompson 		DPRINTFN(1, "Using default other alignment: 4 bytes\n");
3744076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = 4;
3754076dd23SAndrew Thompson 	}
3764076dd23SAndrew Thompson 
3774076dd23SAndrew Thompson 	/*
3784076dd23SAndrew Thompson 	 * Verify that the payload alignment is:
3794076dd23SAndrew Thompson 	 * - power of two
3804076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
3814076dd23SAndrew Thompson 	 * - not less than four bytes
3824076dd23SAndrew Thompson 	 */
3837e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_modulus < 4) ||
3844076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus !=
3854076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
3864076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
3874076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
3884076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = 4;
3894076dd23SAndrew Thompson 	}
3904076dd23SAndrew Thompson 
3914076dd23SAndrew Thompson 	/* Verify that the payload remainder */
3924076dd23SAndrew Thompson 
3937e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
3944076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
3954076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = 0;
3964076dd23SAndrew Thompson 	}
3974076dd23SAndrew Thompson 
3987e05daaeSHans Petter Selasky 	/*
3997e05daaeSHans Petter Selasky 	 * Offset the TX remainder so that IP packet payload starts at
4007e05daaeSHans Petter Selasky 	 * the tx_modulus. This is not too clear in the specification.
4017e05daaeSHans Petter Selasky 	 */
4027e05daaeSHans Petter Selasky 
4037e05daaeSHans Petter Selasky 	sc->sc_ncm.tx_remainder =
4047e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
4057e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_modulus - 1);
4067e05daaeSHans Petter Selasky 
4077e05daaeSHans Petter Selasky 	/* Verify max datagrams */
4087e05daaeSHans Petter Selasky 
4097e05daaeSHans Petter Selasky 	if (sc->sc_ncm.tx_nframe == 0 ||
4107e05daaeSHans Petter Selasky 	    sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
4117e05daaeSHans Petter Selasky 		DPRINTFN(1, "Using default max "
4127e05daaeSHans Petter Selasky 		    "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
4137e05daaeSHans Petter Selasky 		/* need to reserve one entry for zero padding */
4147e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
4157e05daaeSHans Petter Selasky 	}
4167e05daaeSHans Petter Selasky 
4174076dd23SAndrew Thompson 	/* Additional configuration, will fail in device side mode, which is OK. */
4184076dd23SAndrew Thompson 
4194076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4204076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
4214076dd23SAndrew Thompson 	USETW(req.wValue, 0);
4224076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4234076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4247e05daaeSHans Petter Selasky 
4257e05daaeSHans Petter Selasky 	if (ufd != NULL &&
4267e05daaeSHans Petter Selasky 	    (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
4277e05daaeSHans Petter Selasky 		USETW(req.wLength, 8);
4287e05daaeSHans Petter Selasky 		USETDW(value, sc->sc_ncm.rx_max);
4297e05daaeSHans Petter Selasky 		USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
4307e05daaeSHans Petter Selasky 		USETW(value + 6, 0);
4317e05daaeSHans Petter Selasky 	} else {
4324076dd23SAndrew Thompson 		USETW(req.wLength, 4);
4334076dd23SAndrew Thompson 		USETDW(value, sc->sc_ncm.rx_max);
4347e05daaeSHans Petter Selasky  	}
4354076dd23SAndrew Thompson 
4364076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4374076dd23SAndrew Thompson 	    &value, 0, NULL, 1000 /* ms */);
4384076dd23SAndrew Thompson 	if (err) {
4394076dd23SAndrew Thompson 		DPRINTFN(1, "Setting input size "
4404076dd23SAndrew Thompson 		    "to %u failed.\n", sc->sc_ncm.rx_max);
4414076dd23SAndrew Thompson 	}
4424076dd23SAndrew Thompson 
4434076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4444076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_CRC_MODE;
4454076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* no CRC */
4464076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4474076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4484076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4494076dd23SAndrew Thompson 
4504076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4514076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4524076dd23SAndrew Thompson 	if (err) {
4534076dd23SAndrew Thompson 		DPRINTFN(1, "Setting CRC mode to off failed.\n");
4544076dd23SAndrew Thompson 	}
4554076dd23SAndrew Thompson 
4564076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4574076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
4584076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* NTB-16 */
4594076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4604076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4614076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4624076dd23SAndrew Thompson 
4634076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4644076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4654076dd23SAndrew Thompson 	if (err) {
4664076dd23SAndrew Thompson 		DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
4674076dd23SAndrew Thompson 	}
4684076dd23SAndrew Thompson 
4694076dd23SAndrew Thompson 	return (0);		/* success */
4704076dd23SAndrew Thompson }
4714076dd23SAndrew Thompson #endif
4724076dd23SAndrew Thompson 
47302ac6454SAndrew Thompson static int
47402ac6454SAndrew Thompson cdce_probe(device_t dev)
47502ac6454SAndrew Thompson {
476760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
477f1a16106SHans Petter Selasky 	int error;
47802ac6454SAndrew Thompson 
479f1a16106SHans Petter Selasky 	error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
480f1a16106SHans Petter Selasky 	if (error)
481f1a16106SHans Petter Selasky 		error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
482f1a16106SHans Petter Selasky 	return (error);
48302ac6454SAndrew Thompson }
48402ac6454SAndrew Thompson 
48502ac6454SAndrew Thompson static void
486760bc48eSAndrew Thompson cdce_attach_post(struct usb_ether *ue)
48702ac6454SAndrew Thompson {
48802ac6454SAndrew Thompson 	/* no-op */
48902ac6454SAndrew Thompson 	return;
49002ac6454SAndrew Thompson }
49102ac6454SAndrew Thompson 
49202ac6454SAndrew Thompson static int
49302ac6454SAndrew Thompson cdce_attach(device_t dev)
49402ac6454SAndrew Thompson {
49502ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
496760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
497760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
498760bc48eSAndrew Thompson 	struct usb_interface *iface;
499760bc48eSAndrew Thompson 	const struct usb_cdc_union_descriptor *ud;
500760bc48eSAndrew Thompson 	const struct usb_interface_descriptor *id;
501760bc48eSAndrew Thompson 	const struct usb_cdc_ethernet_descriptor *ued;
5024076dd23SAndrew Thompson 	const struct usb_config *pcfg;
503*a8df530dSJohn Baldwin 	uint32_t seed;
50402ac6454SAndrew Thompson 	int error;
50502ac6454SAndrew Thompson 	uint8_t i;
5064076dd23SAndrew Thompson 	uint8_t data_iface_no;
50702ac6454SAndrew Thompson 	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */
50802ac6454SAndrew Thompson 
50902ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
5104076dd23SAndrew Thompson 	sc->sc_ue.ue_udev = uaa->device;
51102ac6454SAndrew Thompson 
512a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
51502ac6454SAndrew Thompson 
5163c8d24f4SAndrew Thompson 	ud = usbd_find_descriptor
51702ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
5186d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF);
51902ac6454SAndrew Thompson 
5204076dd23SAndrew Thompson 	if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
5214076dd23SAndrew Thompson 	    (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
5224076dd23SAndrew Thompson 		DPRINTFN(1, "No union descriptor!\n");
5234076dd23SAndrew Thompson 		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
5244076dd23SAndrew Thompson 		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
5254076dd23SAndrew Thompson 		goto alloc_transfers;
52602ac6454SAndrew Thompson 	}
5274076dd23SAndrew Thompson 	data_iface_no = ud->bSlaveInterface[0];
52802ac6454SAndrew Thompson 
52902ac6454SAndrew Thompson 	for (i = 0;; i++) {
53002ac6454SAndrew Thompson 
531a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
53202ac6454SAndrew Thompson 
53302ac6454SAndrew Thompson 		if (iface) {
53402ac6454SAndrew Thompson 
535a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
53602ac6454SAndrew Thompson 
5374076dd23SAndrew Thompson 			if (id && (id->bInterfaceNumber == data_iface_no)) {
53802ac6454SAndrew Thompson 				sc->sc_ifaces_index[0] = i;
53902ac6454SAndrew Thompson 				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
540a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
54102ac6454SAndrew Thompson 				break;
54202ac6454SAndrew Thompson 			}
54302ac6454SAndrew Thompson 		} else {
544767cb2e2SAndrew Thompson 			device_printf(dev, "no data interface found\n");
54502ac6454SAndrew Thompson 			goto detach;
54602ac6454SAndrew Thompson 		}
54702ac6454SAndrew Thompson 	}
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson 	/*
55002ac6454SAndrew Thompson 	 * <quote>
55102ac6454SAndrew Thompson 	 *
55202ac6454SAndrew Thompson 	 *  The Data Class interface of a networking device shall have
55302ac6454SAndrew Thompson 	 *  a minimum of two interface settings. The first setting
55402ac6454SAndrew Thompson 	 *  (the default interface setting) includes no endpoints and
55502ac6454SAndrew Thompson 	 *  therefore no networking traffic is exchanged whenever the
55602ac6454SAndrew Thompson 	 *  default interface setting is selected. One or more
55702ac6454SAndrew Thompson 	 *  additional interface settings are used for normal
55802ac6454SAndrew Thompson 	 *  operation, and therefore each includes a pair of endpoints
55902ac6454SAndrew Thompson 	 *  (one IN, and one OUT) to exchange network traffic. Select
56002ac6454SAndrew Thompson 	 *  an alternate interface setting to initialize the network
56102ac6454SAndrew Thompson 	 *  aspects of the device and to enable the exchange of
56202ac6454SAndrew Thompson 	 *  network traffic.
56302ac6454SAndrew Thompson 	 *
56402ac6454SAndrew Thompson 	 * </quote>
56502ac6454SAndrew Thompson 	 *
56602ac6454SAndrew Thompson 	 * Some devices, most notably cable modems, include interface
56702ac6454SAndrew Thompson 	 * settings that have no IN or OUT endpoint, therefore loop
56802ac6454SAndrew Thompson 	 * through the list of all available interface settings
56902ac6454SAndrew Thompson 	 * looking for one with both IN and OUT endpoints.
57002ac6454SAndrew Thompson 	 */
57102ac6454SAndrew Thompson 
57202ac6454SAndrew Thompson alloc_transfers:
57302ac6454SAndrew Thompson 
5744076dd23SAndrew Thompson 	pcfg = cdce_config;	/* Default Configuration */
5754076dd23SAndrew Thompson 
57602ac6454SAndrew Thompson 	for (i = 0; i != 32; i++) {
57702ac6454SAndrew Thompson 
5784076dd23SAndrew Thompson 		error = usbd_set_alt_interface_index(uaa->device,
5794076dd23SAndrew Thompson 		    sc->sc_ifaces_index[0], i);
5804076dd23SAndrew Thompson 		if (error)
5814076dd23SAndrew Thompson 			break;
5824076dd23SAndrew Thompson #if CDCE_HAVE_NCM
5834076dd23SAndrew Thompson 		if ((i == 0) && (cdce_ncm_init(sc) == 0))
5844076dd23SAndrew Thompson 			pcfg = cdce_ncm_config;
5854076dd23SAndrew Thompson #endif
5864076dd23SAndrew Thompson 		error = usbd_transfer_setup(uaa->device,
5874076dd23SAndrew Thompson 		    sc->sc_ifaces_index, sc->sc_xfer,
5884076dd23SAndrew Thompson 		    pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
58902ac6454SAndrew Thompson 
5904076dd23SAndrew Thompson 		if (error == 0)
59102ac6454SAndrew Thompson 			break;
59202ac6454SAndrew Thompson 	}
5934076dd23SAndrew Thompson 
5944076dd23SAndrew Thompson 	if (error || (i == 32)) {
5954076dd23SAndrew Thompson 		device_printf(dev, "No valid alternate "
596767cb2e2SAndrew Thompson 		    "setting found\n");
5974076dd23SAndrew Thompson 		goto detach;
59802ac6454SAndrew Thompson 	}
59902ac6454SAndrew Thompson 
6003c8d24f4SAndrew Thompson 	ued = usbd_find_descriptor
60102ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
6026d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF);
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 	if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
60502ac6454SAndrew Thompson 		error = USB_ERR_INVAL;
60602ac6454SAndrew Thompson 	} else {
607a593f6b8SAndrew Thompson 		error = usbd_req_get_string_any(uaa->device, NULL,
60802ac6454SAndrew Thompson 		    eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
60902ac6454SAndrew Thompson 	}
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson 	if (error) {
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson 		/* fake MAC address */
61402ac6454SAndrew Thompson 
61502ac6454SAndrew Thompson 		device_printf(dev, "faking MAC address\n");
616*a8df530dSJohn Baldwin 		seed = ticks;
61702ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[0] = 0x2a;
618*a8df530dSJohn Baldwin 		memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
61902ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson 	} else {
62202ac6454SAndrew Thompson 
6237e05daaeSHans Petter Selasky 		memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson 		for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
62602ac6454SAndrew Thompson 
62702ac6454SAndrew Thompson 			char c = eaddr_str[i];
62802ac6454SAndrew Thompson 
62902ac6454SAndrew Thompson 			if ('0' <= c && c <= '9')
63002ac6454SAndrew Thompson 				c -= '0';
63102ac6454SAndrew Thompson 			else if (c != 0)
63202ac6454SAndrew Thompson 				c -= 'A' - 10;
63302ac6454SAndrew Thompson 			else
63402ac6454SAndrew Thompson 				break;
63502ac6454SAndrew Thompson 
63602ac6454SAndrew Thompson 			c &= 0xf;
63702ac6454SAndrew Thompson 
63802ac6454SAndrew Thompson 			if ((i & 1) == 0)
63902ac6454SAndrew Thompson 				c <<= 4;
64002ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[i / 2] |= c;
64102ac6454SAndrew Thompson 		}
64202ac6454SAndrew Thompson 
643f29a0724SAndrew Thompson 		if (uaa->usb_mode == USB_MODE_DEVICE) {
64402ac6454SAndrew Thompson 			/*
64502ac6454SAndrew Thompson 			 * Do not use the same MAC address like the peer !
64602ac6454SAndrew Thompson 			 */
64702ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[5] ^= 0xFF;
64802ac6454SAndrew Thompson 		}
64902ac6454SAndrew Thompson 	}
65002ac6454SAndrew Thompson 
65102ac6454SAndrew Thompson 	ue->ue_sc = sc;
65202ac6454SAndrew Thompson 	ue->ue_dev = dev;
65302ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
65402ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
65502ac6454SAndrew Thompson 	ue->ue_methods = &cdce_ue_methods;
65602ac6454SAndrew Thompson 
657a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
65802ac6454SAndrew Thompson 	if (error) {
65902ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
66002ac6454SAndrew Thompson 		goto detach;
66102ac6454SAndrew Thompson 	}
66202ac6454SAndrew Thompson 	return (0);			/* success */
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson detach:
66502ac6454SAndrew Thompson 	cdce_detach(dev);
66602ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
66702ac6454SAndrew Thompson }
66802ac6454SAndrew Thompson 
66902ac6454SAndrew Thompson static int
67002ac6454SAndrew Thompson cdce_detach(device_t dev)
67102ac6454SAndrew Thompson {
67202ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
673760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson 	/* stop all USB transfers first */
676a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
677a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
67802ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
67902ac6454SAndrew Thompson 
68002ac6454SAndrew Thompson 	return (0);
68102ac6454SAndrew Thompson }
68202ac6454SAndrew Thompson 
68302ac6454SAndrew Thompson static void
684760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue)
68502ac6454SAndrew Thompson {
686a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 	/*
68902ac6454SAndrew Thompson 	 * Start the USB transfers, if not already started:
69002ac6454SAndrew Thompson 	 */
691a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
692a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
69302ac6454SAndrew Thompson }
69402ac6454SAndrew Thompson 
69502ac6454SAndrew Thompson static void
69602ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n)
69702ac6454SAndrew Thompson {
69802ac6454SAndrew Thompson 	uint8_t x;
69902ac6454SAndrew Thompson 	for (x = 0; x != n; x++) {
70002ac6454SAndrew Thompson 		if (ppm[x] != NULL) {
70102ac6454SAndrew Thompson 			m_freem(ppm[x]);
70202ac6454SAndrew Thompson 			ppm[x] = NULL;
70302ac6454SAndrew Thompson 		}
70402ac6454SAndrew Thompson 	}
70502ac6454SAndrew Thompson }
70602ac6454SAndrew Thompson 
70702ac6454SAndrew Thompson static void
708ed6d949aSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
70902ac6454SAndrew Thompson {
710ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
711a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
71202ac6454SAndrew Thompson 	struct mbuf *m;
71302ac6454SAndrew Thompson 	struct mbuf *mt;
71402ac6454SAndrew Thompson 	uint32_t crc;
71502ac6454SAndrew Thompson 	uint8_t x;
716ed6d949aSAndrew Thompson 	int actlen, aframes;
717ed6d949aSAndrew Thompson 
718ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
71902ac6454SAndrew Thompson 
72002ac6454SAndrew Thompson 	DPRINTFN(1, "\n");
72102ac6454SAndrew Thompson 
72202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
72302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
724ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
725ed6d949aSAndrew Thompson 		    actlen, aframes);
72602ac6454SAndrew Thompson 
72702ac6454SAndrew Thompson 		ifp->if_opackets++;
72802ac6454SAndrew Thompson 
72902ac6454SAndrew Thompson 		/* free all previous TX buffers */
73002ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson 		/* FALLTHROUGH */
73302ac6454SAndrew Thompson 	case USB_ST_SETUP:
73402ac6454SAndrew Thompson tr_setup:
73502ac6454SAndrew Thompson 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 			if (m == NULL)
74002ac6454SAndrew Thompson 				break;
74102ac6454SAndrew Thompson 
74202ac6454SAndrew Thompson 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
74302ac6454SAndrew Thompson 				/*
74402ac6454SAndrew Thompson 				 * Zaurus wants a 32-bit CRC appended
74502ac6454SAndrew Thompson 				 * to every frame
74602ac6454SAndrew Thompson 				 */
74702ac6454SAndrew Thompson 
74802ac6454SAndrew Thompson 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
74902ac6454SAndrew Thompson 				crc = htole32(crc);
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson 				if (!m_append(m, 4, (void *)&crc)) {
75202ac6454SAndrew Thompson 					m_freem(m);
75302ac6454SAndrew Thompson 					ifp->if_oerrors++;
75402ac6454SAndrew Thompson 					continue;
75502ac6454SAndrew Thompson 				}
75602ac6454SAndrew Thompson 			}
75702ac6454SAndrew Thompson 			if (m->m_len != m->m_pkthdr.len) {
758c6499eccSGleb Smirnoff 				mt = m_defrag(m, M_NOWAIT);
75902ac6454SAndrew Thompson 				if (mt == NULL) {
76002ac6454SAndrew Thompson 					m_freem(m);
76102ac6454SAndrew Thompson 					ifp->if_oerrors++;
76202ac6454SAndrew Thompson 					continue;
76302ac6454SAndrew Thompson 				}
76402ac6454SAndrew Thompson 				m = mt;
76502ac6454SAndrew Thompson 			}
76602ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
76702ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
76802ac6454SAndrew Thompson 			}
76902ac6454SAndrew Thompson 			sc->sc_tx_buf[x] = m;
770ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
77102ac6454SAndrew Thompson 
77202ac6454SAndrew Thompson 			/*
77302ac6454SAndrew Thompson 			 * If there's a BPF listener, bounce a copy of
77402ac6454SAndrew Thompson 			 * this frame to him:
77502ac6454SAndrew Thompson 			 */
77602ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
77702ac6454SAndrew Thompson 		}
77802ac6454SAndrew Thompson 		if (x != 0) {
779ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
780ed6d949aSAndrew Thompson 
781a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
78202ac6454SAndrew Thompson 		}
78302ac6454SAndrew Thompson 		break;
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson 	default:			/* Error */
78602ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
787ed6d949aSAndrew Thompson 		    usbd_errstr(error));
78802ac6454SAndrew Thompson 
78902ac6454SAndrew Thompson 		/* free all previous TX buffers */
79002ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 		/* count output errors */
79302ac6454SAndrew Thompson 		ifp->if_oerrors++;
79402ac6454SAndrew Thompson 
795ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
79602ac6454SAndrew Thompson 			/* try to clear stall first */
797ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
79802ac6454SAndrew Thompson 			goto tr_setup;
79902ac6454SAndrew Thompson 		}
80002ac6454SAndrew Thompson 		break;
80102ac6454SAndrew Thompson 	}
80202ac6454SAndrew Thompson }
80302ac6454SAndrew Thompson 
80402ac6454SAndrew Thompson static int32_t
80502ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
80602ac6454SAndrew Thompson {
80702ac6454SAndrew Thompson 	uint32_t *p_crc = arg;
80802ac6454SAndrew Thompson 
80902ac6454SAndrew Thompson 	*p_crc = crc32_raw(src, count, *p_crc);
81002ac6454SAndrew Thompson 	return (0);
81102ac6454SAndrew Thompson }
81202ac6454SAndrew Thompson 
81302ac6454SAndrew Thompson static uint32_t
81402ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
81502ac6454SAndrew Thompson {
81602ac6454SAndrew Thompson 	uint32_t crc = 0xFFFFFFFF;
81702ac6454SAndrew Thompson 	int error;
81802ac6454SAndrew Thompson 
81902ac6454SAndrew Thompson 	error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
82002ac6454SAndrew Thompson 	return (crc ^ 0xFFFFFFFF);
82102ac6454SAndrew Thompson }
82202ac6454SAndrew Thompson 
82302ac6454SAndrew Thompson static void
824760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue)
82502ac6454SAndrew Thompson {
826a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
827a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
82802ac6454SAndrew Thompson 
82902ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
83002ac6454SAndrew Thompson 
83102ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
83202ac6454SAndrew Thompson 
83302ac6454SAndrew Thompson 	/* start interrupt transfer */
834a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
835a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson 	/* stall data write direction, which depends on USB mode */
838ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson 	/* start data transfers */
84102ac6454SAndrew Thompson 	cdce_start(ue);
84202ac6454SAndrew Thompson }
84302ac6454SAndrew Thompson 
84402ac6454SAndrew Thompson static void
845760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue)
84602ac6454SAndrew Thompson {
847a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
848a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
84902ac6454SAndrew Thompson 
85002ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
85102ac6454SAndrew Thompson 
85202ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
85302ac6454SAndrew Thompson 
85402ac6454SAndrew Thompson 	/*
85502ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
85602ac6454SAndrew Thompson 	 */
857a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
858a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
859a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
860a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
86102ac6454SAndrew Thompson }
86202ac6454SAndrew Thompson 
86302ac6454SAndrew Thompson static void
864760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue)
86502ac6454SAndrew Thompson {
86602ac6454SAndrew Thompson 	/* no-op */
86702ac6454SAndrew Thompson 	return;
86802ac6454SAndrew Thompson }
86902ac6454SAndrew Thompson 
87002ac6454SAndrew Thompson static void
871760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue)
87202ac6454SAndrew Thompson {
87302ac6454SAndrew Thompson 	/* no-op */
87402ac6454SAndrew Thompson 	return;
87502ac6454SAndrew Thompson }
87602ac6454SAndrew Thompson 
87702ac6454SAndrew Thompson static int
87802ac6454SAndrew Thompson cdce_suspend(device_t dev)
87902ac6454SAndrew Thompson {
88002ac6454SAndrew Thompson 	device_printf(dev, "Suspending\n");
88102ac6454SAndrew Thompson 	return (0);
88202ac6454SAndrew Thompson }
88302ac6454SAndrew Thompson 
88402ac6454SAndrew Thompson static int
88502ac6454SAndrew Thompson cdce_resume(device_t dev)
88602ac6454SAndrew Thompson {
88702ac6454SAndrew Thompson 	device_printf(dev, "Resuming\n");
88802ac6454SAndrew Thompson 	return (0);
88902ac6454SAndrew Thompson }
89002ac6454SAndrew Thompson 
89102ac6454SAndrew Thompson static void
892ed6d949aSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
89302ac6454SAndrew Thompson {
894ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
89502ac6454SAndrew Thompson 	struct mbuf *m;
89602ac6454SAndrew Thompson 	uint8_t x;
8976d917491SHans Petter Selasky 	int actlen;
8986d917491SHans Petter Selasky 	int aframes;
8996d917491SHans Petter Selasky 	int len;
900ed6d949aSAndrew Thompson 
901ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
90202ac6454SAndrew Thompson 
90302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
90402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
90502ac6454SAndrew Thompson 
906ed6d949aSAndrew Thompson 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
90702ac6454SAndrew Thompson 
908ed6d949aSAndrew Thompson 		for (x = 0; x != aframes; x++) {
90902ac6454SAndrew Thompson 
91002ac6454SAndrew Thompson 			m = sc->sc_rx_buf[x];
91102ac6454SAndrew Thompson 			sc->sc_rx_buf[x] = NULL;
9128f9e0ef9SAndrew Thompson 			len = usbd_xfer_frame_len(xfer, x);
91302ac6454SAndrew Thompson 
91402ac6454SAndrew Thompson 			/* Strip off CRC added by Zaurus, if any */
915ed6d949aSAndrew Thompson 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
916ed6d949aSAndrew Thompson 				len -= 4;
91702ac6454SAndrew Thompson 
9186d917491SHans Petter Selasky 			if (len < (int)sizeof(struct ether_header)) {
91902ac6454SAndrew Thompson 				m_freem(m);
92002ac6454SAndrew Thompson 				continue;
92102ac6454SAndrew Thompson 			}
92202ac6454SAndrew Thompson 			/* queue up mbuf */
923ed6d949aSAndrew Thompson 			uether_rxmbuf(&sc->sc_ue, m, len);
92402ac6454SAndrew Thompson 		}
92502ac6454SAndrew Thompson 
92602ac6454SAndrew Thompson 		/* FALLTHROUGH */
92702ac6454SAndrew Thompson 	case USB_ST_SETUP:
92802ac6454SAndrew Thompson 		/*
92902ac6454SAndrew Thompson 		 * TODO: Implement support for multi frame transfers,
93002ac6454SAndrew Thompson 		 * when the USB hardware supports it.
93102ac6454SAndrew Thompson 		 */
93202ac6454SAndrew Thompson 		for (x = 0; x != 1; x++) {
93302ac6454SAndrew Thompson 			if (sc->sc_rx_buf[x] == NULL) {
934a593f6b8SAndrew Thompson 				m = uether_newbuf();
93502ac6454SAndrew Thompson 				if (m == NULL)
93602ac6454SAndrew Thompson 					goto tr_stall;
93702ac6454SAndrew Thompson 				sc->sc_rx_buf[x] = m;
93802ac6454SAndrew Thompson 			} else {
93902ac6454SAndrew Thompson 				m = sc->sc_rx_buf[x];
94002ac6454SAndrew Thompson 			}
94102ac6454SAndrew Thompson 
942ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
94302ac6454SAndrew Thompson 		}
94402ac6454SAndrew Thompson 		/* set number of frames and start hardware */
945ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, x);
946a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
94702ac6454SAndrew Thompson 		/* flush any received frames */
948a593f6b8SAndrew Thompson 		uether_rxflush(&sc->sc_ue);
94902ac6454SAndrew Thompson 		break;
95002ac6454SAndrew Thompson 
95102ac6454SAndrew Thompson 	default:			/* Error */
95202ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
953ed6d949aSAndrew Thompson 		    usbd_errstr(error));
95402ac6454SAndrew Thompson 
955ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
95602ac6454SAndrew Thompson tr_stall:
95702ac6454SAndrew Thompson 			/* try to clear stall first */
958ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
959ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
960a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
96102ac6454SAndrew Thompson 			break;
96202ac6454SAndrew Thompson 		}
96302ac6454SAndrew Thompson 
96402ac6454SAndrew Thompson 		/* need to free the RX-mbufs when we are cancelled */
96502ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
96602ac6454SAndrew Thompson 		break;
96702ac6454SAndrew Thompson 	}
96802ac6454SAndrew Thompson }
96902ac6454SAndrew Thompson 
97002ac6454SAndrew Thompson static void
971ed6d949aSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
97202ac6454SAndrew Thompson {
973ed6d949aSAndrew Thompson 	int actlen;
974ed6d949aSAndrew Thompson 
975ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
976ed6d949aSAndrew Thompson 
97702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
97802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
97902ac6454SAndrew Thompson 
980ed6d949aSAndrew Thompson 		DPRINTF("Received %d bytes\n", actlen);
98102ac6454SAndrew Thompson 
98202ac6454SAndrew Thompson 		/* TODO: decode some indications */
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 		/* FALLTHROUGH */
98502ac6454SAndrew Thompson 	case USB_ST_SETUP:
98602ac6454SAndrew Thompson tr_setup:
987ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
988a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
98902ac6454SAndrew Thompson 		break;
99002ac6454SAndrew Thompson 
99102ac6454SAndrew Thompson 	default:			/* Error */
992ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
99302ac6454SAndrew Thompson 			/* start clear stall */
994ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
99502ac6454SAndrew Thompson 			goto tr_setup;
99602ac6454SAndrew Thompson 		}
99702ac6454SAndrew Thompson 		break;
99802ac6454SAndrew Thompson 	}
99902ac6454SAndrew Thompson }
100002ac6454SAndrew Thompson 
100102ac6454SAndrew Thompson static void
1002ed6d949aSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
100302ac6454SAndrew Thompson {
1004ed6d949aSAndrew Thompson 	int actlen;
1005ed6d949aSAndrew Thompson 
1006ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1007ed6d949aSAndrew Thompson 
100802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
100902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
101002ac6454SAndrew Thompson 
1011ed6d949aSAndrew Thompson 		DPRINTF("Transferred %d bytes\n", actlen);
101202ac6454SAndrew Thompson 
101302ac6454SAndrew Thompson 		/* FALLTHROUGH */
101402ac6454SAndrew Thompson 	case USB_ST_SETUP:
101502ac6454SAndrew Thompson tr_setup:
101602ac6454SAndrew Thompson #if 0
1017ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, XXX);
1018a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
101902ac6454SAndrew Thompson #endif
102002ac6454SAndrew Thompson 		break;
102102ac6454SAndrew Thompson 
102202ac6454SAndrew Thompson 	default:			/* Error */
1023ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
102402ac6454SAndrew Thompson 			/* start clear stall */
1025ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
102602ac6454SAndrew Thompson 			goto tr_setup;
102702ac6454SAndrew Thompson 		}
102802ac6454SAndrew Thompson 		break;
102902ac6454SAndrew Thompson 	}
103002ac6454SAndrew Thompson }
103102ac6454SAndrew Thompson 
103202ac6454SAndrew Thompson static int
103302ac6454SAndrew Thompson cdce_handle_request(device_t dev,
103402ac6454SAndrew Thompson     const void *req, void **pptr, uint16_t *plen,
103529bd7d7eSAndrew Thompson     uint16_t offset, uint8_t *pstate)
103602ac6454SAndrew Thompson {
103702ac6454SAndrew Thompson 	return (ENXIO);			/* use builtin handler */
103802ac6454SAndrew Thompson }
10394076dd23SAndrew Thompson 
10404076dd23SAndrew Thompson #if CDCE_HAVE_NCM
10417e05daaeSHans Petter Selasky static void
10427e05daaeSHans Petter Selasky cdce_ncm_tx_zero(struct usb_page_cache *pc,
10437e05daaeSHans Petter Selasky     uint32_t start, uint32_t end)
10447e05daaeSHans Petter Selasky {
10457e05daaeSHans Petter Selasky 	if (start >= CDCE_NCM_TX_MAXLEN)
10467e05daaeSHans Petter Selasky 		return;
10477e05daaeSHans Petter Selasky 	if (end > CDCE_NCM_TX_MAXLEN)
10487e05daaeSHans Petter Selasky 		end = CDCE_NCM_TX_MAXLEN;
10497e05daaeSHans Petter Selasky 
10507e05daaeSHans Petter Selasky 	usbd_frame_zero(pc, start, end - start);
10517e05daaeSHans Petter Selasky }
10527e05daaeSHans Petter Selasky 
10534076dd23SAndrew Thompson static uint8_t
10544076dd23SAndrew Thompson cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
10554076dd23SAndrew Thompson {
10564076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
10574076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
10584076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
10594076dd23SAndrew Thompson 	struct mbuf *m;
10604076dd23SAndrew Thompson 	uint32_t rem;
10614076dd23SAndrew Thompson 	uint32_t offset;
10624076dd23SAndrew Thompson 	uint32_t last_offset;
10637e05daaeSHans Petter Selasky 	uint16_t n;
10647e05daaeSHans Petter Selasky 	uint8_t retval;
10654076dd23SAndrew Thompson 
10664076dd23SAndrew Thompson 	usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
10674076dd23SAndrew Thompson 
10684076dd23SAndrew Thompson 	offset = sizeof(sc->sc_ncm.hdr) +
10694076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
10704076dd23SAndrew Thompson 
10714076dd23SAndrew Thompson 	/* Store last valid offset before alignment */
10724076dd23SAndrew Thompson 	last_offset = offset;
10734076dd23SAndrew Thompson 
10747e05daaeSHans Petter Selasky 	/* Align offset */
10757e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
10767e05daaeSHans Petter Selasky 	    offset, sc->sc_ncm.tx_modulus);
10774076dd23SAndrew Thompson 
10787e05daaeSHans Petter Selasky 	/* Zero pad */
10797e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
10807e05daaeSHans Petter Selasky 
10817e05daaeSHans Petter Selasky 	/* buffer full */
10827e05daaeSHans Petter Selasky 	retval = 2;
10837e05daaeSHans Petter Selasky 
10847e05daaeSHans Petter Selasky 	for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
10854076dd23SAndrew Thompson 
10864076dd23SAndrew Thompson 		/* check if end of transmit buffer is reached */
10874076dd23SAndrew Thompson 
10884076dd23SAndrew Thompson 		if (offset >= sc->sc_ncm.tx_max)
10894076dd23SAndrew Thompson 			break;
10904076dd23SAndrew Thompson 
10914076dd23SAndrew Thompson 		/* compute maximum buffer size */
10924076dd23SAndrew Thompson 
10934076dd23SAndrew Thompson 		rem = sc->sc_ncm.tx_max - offset;
10944076dd23SAndrew Thompson 
10954076dd23SAndrew Thompson 		IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
10964076dd23SAndrew Thompson 
10977e05daaeSHans Petter Selasky 		if (m == NULL) {
10987e05daaeSHans Petter Selasky 			/* buffer not full */
10997e05daaeSHans Petter Selasky 			retval = 1;
11004076dd23SAndrew Thompson 			break;
11017e05daaeSHans Petter Selasky 		}
11024076dd23SAndrew Thompson 
11036d917491SHans Petter Selasky 		if (m->m_pkthdr.len > (int)rem) {
11044076dd23SAndrew Thompson 			if (n == 0) {
11054076dd23SAndrew Thompson 				/* The frame won't fit in our buffer */
11064076dd23SAndrew Thompson 				DPRINTFN(1, "Frame too big to be transmitted!\n");
11074076dd23SAndrew Thompson 				m_freem(m);
11084076dd23SAndrew Thompson 				ifp->if_oerrors++;
11094076dd23SAndrew Thompson 				n--;
11104076dd23SAndrew Thompson 				continue;
11114076dd23SAndrew Thompson 			}
11124076dd23SAndrew Thompson 			/* Wait till next buffer becomes ready */
11134076dd23SAndrew Thompson 			IFQ_DRV_PREPEND(&(ifp->if_snd), m);
11144076dd23SAndrew Thompson 			break;
11154076dd23SAndrew Thompson 		}
11164076dd23SAndrew Thompson 		usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
11174076dd23SAndrew Thompson 
11184076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
11194076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
11204076dd23SAndrew Thompson 
11214076dd23SAndrew Thompson 		/* Update offset */
11224076dd23SAndrew Thompson 		offset += m->m_pkthdr.len;
11234076dd23SAndrew Thompson 
11244076dd23SAndrew Thompson 		/* Store last valid offset before alignment */
11254076dd23SAndrew Thompson 		last_offset = offset;
11264076dd23SAndrew Thompson 
11277e05daaeSHans Petter Selasky 		/* Align offset */
11287e05daaeSHans Petter Selasky 		offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
11297e05daaeSHans Petter Selasky 		    offset, sc->sc_ncm.tx_modulus);
11307e05daaeSHans Petter Selasky 
11317e05daaeSHans Petter Selasky 		/* Zero pad */
11327e05daaeSHans Petter Selasky 		cdce_ncm_tx_zero(pc, last_offset, offset);
11334076dd23SAndrew Thompson 
11344076dd23SAndrew Thompson 		/*
11354076dd23SAndrew Thompson 		 * If there's a BPF listener, bounce a copy
11364076dd23SAndrew Thompson 		 * of this frame to him:
11374076dd23SAndrew Thompson 		 */
11384076dd23SAndrew Thompson 		BPF_MTAP(ifp, m);
11394076dd23SAndrew Thompson 
11404076dd23SAndrew Thompson 		/* Free mbuf */
11414076dd23SAndrew Thompson 
11424076dd23SAndrew Thompson 		m_freem(m);
11434076dd23SAndrew Thompson 
11444076dd23SAndrew Thompson 		/* Pre-increment interface counter */
11454076dd23SAndrew Thompson 
11464076dd23SAndrew Thompson 		ifp->if_opackets++;
11474076dd23SAndrew Thompson 	}
11484076dd23SAndrew Thompson 
11494076dd23SAndrew Thompson 	if (n == 0)
11507e05daaeSHans Petter Selasky 		return (0);
11514076dd23SAndrew Thompson 
11524076dd23SAndrew Thompson 	rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
11534076dd23SAndrew Thompson 
11544076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wLength, rem);
11554076dd23SAndrew Thompson 
11564076dd23SAndrew Thompson 	/* zero the rest of the data pointer entries */
11574076dd23SAndrew Thompson 	for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
11584076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
11594076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
11604076dd23SAndrew Thompson 	}
11614076dd23SAndrew Thompson 
11627e05daaeSHans Petter Selasky 	offset = last_offset;
11637e05daaeSHans Petter Selasky 
11647e05daaeSHans Petter Selasky 	/* Align offset */
11657e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
11667e05daaeSHans Petter Selasky 
11677e05daaeSHans Petter Selasky 	/* Optimise, save bandwidth and force short termination */
11687e05daaeSHans Petter Selasky 	if (offset >= sc->sc_ncm.tx_max)
11697e05daaeSHans Petter Selasky 		offset = sc->sc_ncm.tx_max;
11707e05daaeSHans Petter Selasky 	else
11717e05daaeSHans Petter Selasky 		offset ++;
11727e05daaeSHans Petter Selasky 
11737e05daaeSHans Petter Selasky 	/* Zero pad */
11747e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
11757e05daaeSHans Petter Selasky 
11764076dd23SAndrew Thompson 	/* set frame length */
11777e05daaeSHans Petter Selasky 	usbd_xfer_set_frame_len(xfer, index, offset);
11784076dd23SAndrew Thompson 
11794076dd23SAndrew Thompson 	/* Fill out 16-bit header */
11804076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[0] = 'N';
11814076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[1] = 'C';
11824076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[2] = 'M';
11834076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[3] = 'H';
11844076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
11857e05daaeSHans Petter Selasky 	USETW(sc->sc_ncm.hdr.wBlockLength, offset);
11864076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
11874076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
11884076dd23SAndrew Thompson 
11894076dd23SAndrew Thompson 	sc->sc_ncm.tx_seq++;
11904076dd23SAndrew Thompson 
11914076dd23SAndrew Thompson 	/* Fill out 16-bit frame table header */
11924076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[0] = 'N';
11934076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[1] = 'C';
11944076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[2] = 'M';
119585e3d588SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[3] = '0';
11964076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0);		/* reserved */
11974076dd23SAndrew Thompson 
11984076dd23SAndrew Thompson 	usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
11994076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
12004076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt));
12014076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
12024076dd23SAndrew Thompson 	    &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
12037e05daaeSHans Petter Selasky 	return (retval);
12044076dd23SAndrew Thompson }
12054076dd23SAndrew Thompson 
12064076dd23SAndrew Thompson static void
12074076dd23SAndrew Thompson cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
12084076dd23SAndrew Thompson {
12094076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
12104076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
12114076dd23SAndrew Thompson 	uint16_t x;
12127e05daaeSHans Petter Selasky 	uint8_t temp;
12134076dd23SAndrew Thompson 	int actlen;
12144076dd23SAndrew Thompson 	int aframes;
12154076dd23SAndrew Thompson 
12164076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
12174076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
12184076dd23SAndrew Thompson 
12194076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
12204076dd23SAndrew Thompson 
12214076dd23SAndrew Thompson 		DPRINTFN(10, "transfer complete: "
12224076dd23SAndrew Thompson 		    "%u bytes in %u frames\n", actlen, aframes);
12234076dd23SAndrew Thompson 
12244076dd23SAndrew Thompson 	case USB_ST_SETUP:
12254076dd23SAndrew Thompson 		for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
12267e05daaeSHans Petter Selasky 			temp = cdce_ncm_fill_tx_frames(xfer, x);
12277e05daaeSHans Petter Selasky 			if (temp == 0)
12284076dd23SAndrew Thompson 				break;
12297e05daaeSHans Petter Selasky 			if (temp == 1) {
12307e05daaeSHans Petter Selasky 				x++;
12317e05daaeSHans Petter Selasky 				break;
12327e05daaeSHans Petter Selasky 			}
12334076dd23SAndrew Thompson 		}
12344076dd23SAndrew Thompson 
12354076dd23SAndrew Thompson 		if (x != 0) {
12367e05daaeSHans Petter Selasky #ifdef USB_DEBUG
12377e05daaeSHans Petter Selasky 			usbd_xfer_set_interval(xfer, cdce_tx_interval);
12387e05daaeSHans Petter Selasky #endif
12394076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
12404076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
12414076dd23SAndrew Thompson 		}
12424076dd23SAndrew Thompson 		break;
12434076dd23SAndrew Thompson 
12444076dd23SAndrew Thompson 	default:			/* Error */
12454076dd23SAndrew Thompson 		DPRINTFN(10, "Transfer error: %s\n",
12464076dd23SAndrew Thompson 		    usbd_errstr(error));
12474076dd23SAndrew Thompson 
12484076dd23SAndrew Thompson 		/* update error counter */
12494076dd23SAndrew Thompson 		ifp->if_oerrors += 1;
12504076dd23SAndrew Thompson 
12514076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
12524076dd23SAndrew Thompson 			/* try to clear stall first */
12534076dd23SAndrew Thompson 			usbd_xfer_set_stall(xfer);
12544076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
12554076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
12564076dd23SAndrew Thompson 		}
12574076dd23SAndrew Thompson 		break;
12584076dd23SAndrew Thompson 	}
12594076dd23SAndrew Thompson }
12604076dd23SAndrew Thompson 
12614076dd23SAndrew Thompson static void
12624076dd23SAndrew Thompson cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
12634076dd23SAndrew Thompson {
12644076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
12654076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
12664076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
12674076dd23SAndrew Thompson 	struct mbuf *m;
12684076dd23SAndrew Thompson 	int sumdata;
12694076dd23SAndrew Thompson 	int sumlen;
12704076dd23SAndrew Thompson 	int actlen;
12714076dd23SAndrew Thompson 	int aframes;
12724076dd23SAndrew Thompson 	int temp;
12734076dd23SAndrew Thompson 	int nframes;
12744076dd23SAndrew Thompson 	int x;
12754076dd23SAndrew Thompson 	int offset;
12764076dd23SAndrew Thompson 
12774076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
12784076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
12794076dd23SAndrew Thompson 
12804076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
12814076dd23SAndrew Thompson 
12824076dd23SAndrew Thompson 		DPRINTFN(1, "received %u bytes in %u frames\n",
12834076dd23SAndrew Thompson 		    actlen, aframes);
12844076dd23SAndrew Thompson 
12856d917491SHans Petter Selasky 		if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
12864076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt))) {
12874076dd23SAndrew Thompson 			DPRINTFN(1, "frame too short\n");
128885e3d588SAndrew Thompson 			goto tr_setup;
12894076dd23SAndrew Thompson 		}
12904076dd23SAndrew Thompson 		usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
12914076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.hdr));
12924076dd23SAndrew Thompson 
12934076dd23SAndrew Thompson 		if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
12944076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
12954076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
12964076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
129785e3d588SAndrew Thompson 			DPRINTFN(1, "invalid HDR signature: "
129885e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
129985e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[0],
130085e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[1],
130185e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[2],
130285e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[3]);
13034076dd23SAndrew Thompson 			goto tr_stall;
13044076dd23SAndrew Thompson 		}
13054076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
13064076dd23SAndrew Thompson 		if (temp > sumlen) {
13074076dd23SAndrew Thompson 			DPRINTFN(1, "unsupported block length %u/%u\n",
13084076dd23SAndrew Thompson 			    temp, sumlen);
13094076dd23SAndrew Thompson 			goto tr_stall;
13104076dd23SAndrew Thompson 		}
13114076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
13126d917491SHans Petter Selasky 		if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
131385e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
13144076dd23SAndrew Thompson 			goto tr_stall;
13154076dd23SAndrew Thompson 		}
13164076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
13174076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt));
13184076dd23SAndrew Thompson 
13194076dd23SAndrew Thompson 		if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
13204076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
13214076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
132285e3d588SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
132385e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT signature"
132485e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
132585e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[0],
132685e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[1],
132785e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[2],
132885e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[3]);
13294076dd23SAndrew Thompson 			goto tr_stall;
13304076dd23SAndrew Thompson 		}
13314076dd23SAndrew Thompson 		nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
13324076dd23SAndrew Thompson 
13334076dd23SAndrew Thompson 		/* Subtract size of header and last zero padded entry */
13344076dd23SAndrew Thompson 		if (nframes >= (2 + 1))
13354076dd23SAndrew Thompson 			nframes -= (2 + 1);
13364076dd23SAndrew Thompson 		else
13374076dd23SAndrew Thompson 			nframes = 0;
13384076dd23SAndrew Thompson 
13394076dd23SAndrew Thompson 		DPRINTFN(1, "nframes = %u\n", nframes);
13404076dd23SAndrew Thompson 
13414076dd23SAndrew Thompson 		temp += sizeof(sc->sc_ncm.dpt);
13424076dd23SAndrew Thompson 
13434076dd23SAndrew Thompson 		if ((temp + (4 * nframes)) > actlen)
13444076dd23SAndrew Thompson 			goto tr_stall;
13454076dd23SAndrew Thompson 
13464076dd23SAndrew Thompson 		if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
13474076dd23SAndrew Thompson 			DPRINTFN(1, "Truncating number of frames from %u to %u\n",
13484076dd23SAndrew Thompson 			    nframes, CDCE_NCM_SUBFRAMES_MAX);
13494076dd23SAndrew Thompson 			nframes = CDCE_NCM_SUBFRAMES_MAX;
13504076dd23SAndrew Thompson 		}
13514076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
13524076dd23SAndrew Thompson 
13534076dd23SAndrew Thompson 		sumdata = 0;
13544076dd23SAndrew Thompson 
13554076dd23SAndrew Thompson 		for (x = 0; x != nframes; x++) {
13564076dd23SAndrew Thompson 
13574076dd23SAndrew Thompson 			offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
13584076dd23SAndrew Thompson 			temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
13594076dd23SAndrew Thompson 
136029051223SAndrew Thompson 			if ((offset == 0) ||
13616d917491SHans Petter Selasky 			    (temp < (int)sizeof(struct ether_header)) ||
136229051223SAndrew Thompson 			    (temp > (MCLBYTES - ETHER_ALIGN))) {
136329051223SAndrew Thompson 				DPRINTFN(1, "NULL frame detected at %d\n", x);
13644076dd23SAndrew Thompson 				m = NULL;
136529051223SAndrew Thompson 				/* silently ignore this frame */
13664076dd23SAndrew Thompson 				continue;
136729051223SAndrew Thompson 			} else if ((offset + temp) > actlen) {
136829051223SAndrew Thompson 				DPRINTFN(1, "invalid frame "
136929051223SAndrew Thompson 				    "detected at %d\n", x);
137029051223SAndrew Thompson 				m = NULL;
137129051223SAndrew Thompson 				/* silently ignore this frame */
137229051223SAndrew Thompson 				continue;
13736d917491SHans Petter Selasky 			} else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1374c6499eccSGleb Smirnoff 				m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13754076dd23SAndrew Thompson 			} else {
1376c6499eccSGleb Smirnoff 				m = m_gethdr(M_NOWAIT, MT_DATA);
13774076dd23SAndrew Thompson 			}
13784076dd23SAndrew Thompson 
13794076dd23SAndrew Thompson 			DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
13804076dd23SAndrew Thompson 			    x, offset, temp);
13814076dd23SAndrew Thompson 
13824076dd23SAndrew Thompson 			/* check if we have a buffer */
13834076dd23SAndrew Thompson 			if (m) {
13844076dd23SAndrew Thompson 				m_adj(m, ETHER_ALIGN);
13854076dd23SAndrew Thompson 
13864076dd23SAndrew Thompson 				usbd_copy_out(pc, offset, m->m_data, temp);
13874076dd23SAndrew Thompson 
13884076dd23SAndrew Thompson 				/* enqueue */
13894076dd23SAndrew Thompson 				uether_rxmbuf(&sc->sc_ue, m, temp);
13904076dd23SAndrew Thompson 
13914076dd23SAndrew Thompson 				sumdata += temp;
13924076dd23SAndrew Thompson 			} else {
13934076dd23SAndrew Thompson 				ifp->if_ierrors++;
13944076dd23SAndrew Thompson 			}
13954076dd23SAndrew Thompson 		}
13964076dd23SAndrew Thompson 
13974076dd23SAndrew Thompson 		DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
13984076dd23SAndrew Thompson 
13994076dd23SAndrew Thompson 	case USB_ST_SETUP:
140085e3d588SAndrew Thompson tr_setup:
14014076dd23SAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
14024076dd23SAndrew Thompson 		usbd_xfer_set_frames(xfer, 1);
14034076dd23SAndrew Thompson 		usbd_transfer_submit(xfer);
14044076dd23SAndrew Thompson 		uether_rxflush(&sc->sc_ue);	/* must be last */
14054076dd23SAndrew Thompson 		break;
14064076dd23SAndrew Thompson 
14074076dd23SAndrew Thompson 	default:			/* Error */
14084076dd23SAndrew Thompson 		DPRINTFN(1, "error = %s\n",
14094076dd23SAndrew Thompson 		    usbd_errstr(error));
14104076dd23SAndrew Thompson 
14114076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
14124076dd23SAndrew Thompson tr_stall:
14134076dd23SAndrew Thompson 			/* try to clear stall first */
14144076dd23SAndrew Thompson 			usbd_xfer_set_stall(xfer);
14154076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
14164076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
14174076dd23SAndrew Thompson 		}
14184076dd23SAndrew Thompson 		break;
14194076dd23SAndrew Thompson 	}
14204076dd23SAndrew Thompson }
14214076dd23SAndrew Thompson #endif
1422