xref: /freebsd/sys/dev/usb/net/if_cdce.c (revision 6472ac3d8a86336899b6cfb789a4cd9897e3fab5)
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 
114*6472ac3dSEd 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,
3067e05daaeSHans Petter Selasky 	    sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0 - 1,
3077e05daaeSHans Petter Selasky 	    UCDC_NCM_FUNC_DESC_SUBTYPE, 0 - 1);
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;
50302ac6454SAndrew Thompson 	int error;
50402ac6454SAndrew Thompson 	uint8_t i;
5054076dd23SAndrew Thompson 	uint8_t data_iface_no;
50602ac6454SAndrew Thompson 	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
5094076dd23SAndrew Thompson 	sc->sc_ue.ue_udev = uaa->device;
51002ac6454SAndrew Thompson 
511a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
51402ac6454SAndrew Thompson 
5153c8d24f4SAndrew Thompson 	ud = usbd_find_descriptor
51602ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
51702ac6454SAndrew Thompson 	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
51802ac6454SAndrew Thompson 
5194076dd23SAndrew Thompson 	if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
5204076dd23SAndrew Thompson 	    (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
5214076dd23SAndrew Thompson 		DPRINTFN(1, "No union descriptor!\n");
5224076dd23SAndrew Thompson 		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
5234076dd23SAndrew Thompson 		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
5244076dd23SAndrew Thompson 		goto alloc_transfers;
52502ac6454SAndrew Thompson 	}
5264076dd23SAndrew Thompson 	data_iface_no = ud->bSlaveInterface[0];
52702ac6454SAndrew Thompson 
52802ac6454SAndrew Thompson 	for (i = 0;; i++) {
52902ac6454SAndrew Thompson 
530a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 		if (iface) {
53302ac6454SAndrew Thompson 
534a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
53502ac6454SAndrew Thompson 
5364076dd23SAndrew Thompson 			if (id && (id->bInterfaceNumber == data_iface_no)) {
53702ac6454SAndrew Thompson 				sc->sc_ifaces_index[0] = i;
53802ac6454SAndrew Thompson 				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
539a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
54002ac6454SAndrew Thompson 				break;
54102ac6454SAndrew Thompson 			}
54202ac6454SAndrew Thompson 		} else {
543767cb2e2SAndrew Thompson 			device_printf(dev, "no data interface found\n");
54402ac6454SAndrew Thompson 			goto detach;
54502ac6454SAndrew Thompson 		}
54602ac6454SAndrew Thompson 	}
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	/*
54902ac6454SAndrew Thompson 	 * <quote>
55002ac6454SAndrew Thompson 	 *
55102ac6454SAndrew Thompson 	 *  The Data Class interface of a networking device shall have
55202ac6454SAndrew Thompson 	 *  a minimum of two interface settings. The first setting
55302ac6454SAndrew Thompson 	 *  (the default interface setting) includes no endpoints and
55402ac6454SAndrew Thompson 	 *  therefore no networking traffic is exchanged whenever the
55502ac6454SAndrew Thompson 	 *  default interface setting is selected. One or more
55602ac6454SAndrew Thompson 	 *  additional interface settings are used for normal
55702ac6454SAndrew Thompson 	 *  operation, and therefore each includes a pair of endpoints
55802ac6454SAndrew Thompson 	 *  (one IN, and one OUT) to exchange network traffic. Select
55902ac6454SAndrew Thompson 	 *  an alternate interface setting to initialize the network
56002ac6454SAndrew Thompson 	 *  aspects of the device and to enable the exchange of
56102ac6454SAndrew Thompson 	 *  network traffic.
56202ac6454SAndrew Thompson 	 *
56302ac6454SAndrew Thompson 	 * </quote>
56402ac6454SAndrew Thompson 	 *
56502ac6454SAndrew Thompson 	 * Some devices, most notably cable modems, include interface
56602ac6454SAndrew Thompson 	 * settings that have no IN or OUT endpoint, therefore loop
56702ac6454SAndrew Thompson 	 * through the list of all available interface settings
56802ac6454SAndrew Thompson 	 * looking for one with both IN and OUT endpoints.
56902ac6454SAndrew Thompson 	 */
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson alloc_transfers:
57202ac6454SAndrew Thompson 
5734076dd23SAndrew Thompson 	pcfg = cdce_config;	/* Default Configuration */
5744076dd23SAndrew Thompson 
57502ac6454SAndrew Thompson 	for (i = 0; i != 32; i++) {
57602ac6454SAndrew Thompson 
5774076dd23SAndrew Thompson 		error = usbd_set_alt_interface_index(uaa->device,
5784076dd23SAndrew Thompson 		    sc->sc_ifaces_index[0], i);
5794076dd23SAndrew Thompson 		if (error)
5804076dd23SAndrew Thompson 			break;
5814076dd23SAndrew Thompson #if CDCE_HAVE_NCM
5824076dd23SAndrew Thompson 		if ((i == 0) && (cdce_ncm_init(sc) == 0))
5834076dd23SAndrew Thompson 			pcfg = cdce_ncm_config;
5844076dd23SAndrew Thompson #endif
5854076dd23SAndrew Thompson 		error = usbd_transfer_setup(uaa->device,
5864076dd23SAndrew Thompson 		    sc->sc_ifaces_index, sc->sc_xfer,
5874076dd23SAndrew Thompson 		    pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
58802ac6454SAndrew Thompson 
5894076dd23SAndrew Thompson 		if (error == 0)
59002ac6454SAndrew Thompson 			break;
59102ac6454SAndrew Thompson 	}
5924076dd23SAndrew Thompson 
5934076dd23SAndrew Thompson 	if (error || (i == 32)) {
5944076dd23SAndrew Thompson 		device_printf(dev, "No valid alternate "
595767cb2e2SAndrew Thompson 		    "setting found\n");
5964076dd23SAndrew Thompson 		goto detach;
59702ac6454SAndrew Thompson 	}
59802ac6454SAndrew Thompson 
5993c8d24f4SAndrew Thompson 	ued = usbd_find_descriptor
60002ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
60102ac6454SAndrew Thompson 	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1);
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson 	if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
60402ac6454SAndrew Thompson 		error = USB_ERR_INVAL;
60502ac6454SAndrew Thompson 	} else {
606a593f6b8SAndrew Thompson 		error = usbd_req_get_string_any(uaa->device, NULL,
60702ac6454SAndrew Thompson 		    eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
60802ac6454SAndrew Thompson 	}
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson 	if (error) {
61102ac6454SAndrew Thompson 
61202ac6454SAndrew Thompson 		/* fake MAC address */
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 		device_printf(dev, "faking MAC address\n");
61502ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[0] = 0x2a;
61602ac6454SAndrew Thompson 		memcpy(&sc->sc_ue.ue_eaddr[1], &ticks, sizeof(uint32_t));
61702ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
61802ac6454SAndrew Thompson 
61902ac6454SAndrew Thompson 	} else {
62002ac6454SAndrew Thompson 
6217e05daaeSHans Petter Selasky 		memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 		for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson 			char c = eaddr_str[i];
62602ac6454SAndrew Thompson 
62702ac6454SAndrew Thompson 			if ('0' <= c && c <= '9')
62802ac6454SAndrew Thompson 				c -= '0';
62902ac6454SAndrew Thompson 			else if (c != 0)
63002ac6454SAndrew Thompson 				c -= 'A' - 10;
63102ac6454SAndrew Thompson 			else
63202ac6454SAndrew Thompson 				break;
63302ac6454SAndrew Thompson 
63402ac6454SAndrew Thompson 			c &= 0xf;
63502ac6454SAndrew Thompson 
63602ac6454SAndrew Thompson 			if ((i & 1) == 0)
63702ac6454SAndrew Thompson 				c <<= 4;
63802ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[i / 2] |= c;
63902ac6454SAndrew Thompson 		}
64002ac6454SAndrew Thompson 
641f29a0724SAndrew Thompson 		if (uaa->usb_mode == USB_MODE_DEVICE) {
64202ac6454SAndrew Thompson 			/*
64302ac6454SAndrew Thompson 			 * Do not use the same MAC address like the peer !
64402ac6454SAndrew Thompson 			 */
64502ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[5] ^= 0xFF;
64602ac6454SAndrew Thompson 		}
64702ac6454SAndrew Thompson 	}
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	ue->ue_sc = sc;
65002ac6454SAndrew Thompson 	ue->ue_dev = dev;
65102ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
65202ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
65302ac6454SAndrew Thompson 	ue->ue_methods = &cdce_ue_methods;
65402ac6454SAndrew Thompson 
655a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
65602ac6454SAndrew Thompson 	if (error) {
65702ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
65802ac6454SAndrew Thompson 		goto detach;
65902ac6454SAndrew Thompson 	}
66002ac6454SAndrew Thompson 	return (0);			/* success */
66102ac6454SAndrew Thompson 
66202ac6454SAndrew Thompson detach:
66302ac6454SAndrew Thompson 	cdce_detach(dev);
66402ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
66502ac6454SAndrew Thompson }
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson static int
66802ac6454SAndrew Thompson cdce_detach(device_t dev)
66902ac6454SAndrew Thompson {
67002ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
671760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
67202ac6454SAndrew Thompson 
67302ac6454SAndrew Thompson 	/* stop all USB transfers first */
674a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
675a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
67602ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
67702ac6454SAndrew Thompson 
67802ac6454SAndrew Thompson 	return (0);
67902ac6454SAndrew Thompson }
68002ac6454SAndrew Thompson 
68102ac6454SAndrew Thompson static void
682760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue)
68302ac6454SAndrew Thompson {
684a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
68502ac6454SAndrew Thompson 
68602ac6454SAndrew Thompson 	/*
68702ac6454SAndrew Thompson 	 * Start the USB transfers, if not already started:
68802ac6454SAndrew Thompson 	 */
689a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
690a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
69102ac6454SAndrew Thompson }
69202ac6454SAndrew Thompson 
69302ac6454SAndrew Thompson static void
69402ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n)
69502ac6454SAndrew Thompson {
69602ac6454SAndrew Thompson 	uint8_t x;
69702ac6454SAndrew Thompson 	for (x = 0; x != n; x++) {
69802ac6454SAndrew Thompson 		if (ppm[x] != NULL) {
69902ac6454SAndrew Thompson 			m_freem(ppm[x]);
70002ac6454SAndrew Thompson 			ppm[x] = NULL;
70102ac6454SAndrew Thompson 		}
70202ac6454SAndrew Thompson 	}
70302ac6454SAndrew Thompson }
70402ac6454SAndrew Thompson 
70502ac6454SAndrew Thompson static void
706ed6d949aSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
70702ac6454SAndrew Thompson {
708ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
709a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
71002ac6454SAndrew Thompson 	struct mbuf *m;
71102ac6454SAndrew Thompson 	struct mbuf *mt;
71202ac6454SAndrew Thompson 	uint32_t crc;
71302ac6454SAndrew Thompson 	uint8_t x;
714ed6d949aSAndrew Thompson 	int actlen, aframes;
715ed6d949aSAndrew Thompson 
716ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
71702ac6454SAndrew Thompson 
71802ac6454SAndrew Thompson 	DPRINTFN(1, "\n");
71902ac6454SAndrew Thompson 
72002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
72102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
722ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
723ed6d949aSAndrew Thompson 		    actlen, aframes);
72402ac6454SAndrew Thompson 
72502ac6454SAndrew Thompson 		ifp->if_opackets++;
72602ac6454SAndrew Thompson 
72702ac6454SAndrew Thompson 		/* free all previous TX buffers */
72802ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
72902ac6454SAndrew Thompson 
73002ac6454SAndrew Thompson 		/* FALLTHROUGH */
73102ac6454SAndrew Thompson 	case USB_ST_SETUP:
73202ac6454SAndrew Thompson tr_setup:
73302ac6454SAndrew Thompson 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
73402ac6454SAndrew Thompson 
73502ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 			if (m == NULL)
73802ac6454SAndrew Thompson 				break;
73902ac6454SAndrew Thompson 
74002ac6454SAndrew Thompson 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
74102ac6454SAndrew Thompson 				/*
74202ac6454SAndrew Thompson 				 * Zaurus wants a 32-bit CRC appended
74302ac6454SAndrew Thompson 				 * to every frame
74402ac6454SAndrew Thompson 				 */
74502ac6454SAndrew Thompson 
74602ac6454SAndrew Thompson 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
74702ac6454SAndrew Thompson 				crc = htole32(crc);
74802ac6454SAndrew Thompson 
74902ac6454SAndrew Thompson 				if (!m_append(m, 4, (void *)&crc)) {
75002ac6454SAndrew Thompson 					m_freem(m);
75102ac6454SAndrew Thompson 					ifp->if_oerrors++;
75202ac6454SAndrew Thompson 					continue;
75302ac6454SAndrew Thompson 				}
75402ac6454SAndrew Thompson 			}
75502ac6454SAndrew Thompson 			if (m->m_len != m->m_pkthdr.len) {
75602ac6454SAndrew Thompson 				mt = m_defrag(m, M_DONTWAIT);
75702ac6454SAndrew Thompson 				if (mt == NULL) {
75802ac6454SAndrew Thompson 					m_freem(m);
75902ac6454SAndrew Thompson 					ifp->if_oerrors++;
76002ac6454SAndrew Thompson 					continue;
76102ac6454SAndrew Thompson 				}
76202ac6454SAndrew Thompson 				m = mt;
76302ac6454SAndrew Thompson 			}
76402ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
76502ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
76602ac6454SAndrew Thompson 			}
76702ac6454SAndrew Thompson 			sc->sc_tx_buf[x] = m;
768ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
76902ac6454SAndrew Thompson 
77002ac6454SAndrew Thompson 			/*
77102ac6454SAndrew Thompson 			 * If there's a BPF listener, bounce a copy of
77202ac6454SAndrew Thompson 			 * this frame to him:
77302ac6454SAndrew Thompson 			 */
77402ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
77502ac6454SAndrew Thompson 		}
77602ac6454SAndrew Thompson 		if (x != 0) {
777ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
778ed6d949aSAndrew Thompson 
779a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
78002ac6454SAndrew Thompson 		}
78102ac6454SAndrew Thompson 		break;
78202ac6454SAndrew Thompson 
78302ac6454SAndrew Thompson 	default:			/* Error */
78402ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
785ed6d949aSAndrew Thompson 		    usbd_errstr(error));
78602ac6454SAndrew Thompson 
78702ac6454SAndrew Thompson 		/* free all previous TX buffers */
78802ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
78902ac6454SAndrew Thompson 
79002ac6454SAndrew Thompson 		/* count output errors */
79102ac6454SAndrew Thompson 		ifp->if_oerrors++;
79202ac6454SAndrew Thompson 
793ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
79402ac6454SAndrew Thompson 			/* try to clear stall first */
795ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
79602ac6454SAndrew Thompson 			goto tr_setup;
79702ac6454SAndrew Thompson 		}
79802ac6454SAndrew Thompson 		break;
79902ac6454SAndrew Thompson 	}
80002ac6454SAndrew Thompson }
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson static int32_t
80302ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
80402ac6454SAndrew Thompson {
80502ac6454SAndrew Thompson 	uint32_t *p_crc = arg;
80602ac6454SAndrew Thompson 
80702ac6454SAndrew Thompson 	*p_crc = crc32_raw(src, count, *p_crc);
80802ac6454SAndrew Thompson 	return (0);
80902ac6454SAndrew Thompson }
81002ac6454SAndrew Thompson 
81102ac6454SAndrew Thompson static uint32_t
81202ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
81302ac6454SAndrew Thompson {
81402ac6454SAndrew Thompson 	uint32_t crc = 0xFFFFFFFF;
81502ac6454SAndrew Thompson 	int error;
81602ac6454SAndrew Thompson 
81702ac6454SAndrew Thompson 	error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
81802ac6454SAndrew Thompson 	return (crc ^ 0xFFFFFFFF);
81902ac6454SAndrew Thompson }
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson static void
822760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue)
82302ac6454SAndrew Thompson {
824a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
825a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
82602ac6454SAndrew Thompson 
82702ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
82802ac6454SAndrew Thompson 
82902ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
83002ac6454SAndrew Thompson 
83102ac6454SAndrew Thompson 	/* start interrupt transfer */
832a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
833a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
83402ac6454SAndrew Thompson 
83502ac6454SAndrew Thompson 	/* stall data write direction, which depends on USB mode */
836ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
83702ac6454SAndrew Thompson 
83802ac6454SAndrew Thompson 	/* start data transfers */
83902ac6454SAndrew Thompson 	cdce_start(ue);
84002ac6454SAndrew Thompson }
84102ac6454SAndrew Thompson 
84202ac6454SAndrew Thompson static void
843760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue)
84402ac6454SAndrew Thompson {
845a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
846a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
84902ac6454SAndrew Thompson 
85002ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
85102ac6454SAndrew Thompson 
85202ac6454SAndrew Thompson 	/*
85302ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
85402ac6454SAndrew Thompson 	 */
855a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
856a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
857a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
858a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
85902ac6454SAndrew Thompson }
86002ac6454SAndrew Thompson 
86102ac6454SAndrew Thompson static void
862760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue)
86302ac6454SAndrew Thompson {
86402ac6454SAndrew Thompson 	/* no-op */
86502ac6454SAndrew Thompson 	return;
86602ac6454SAndrew Thompson }
86702ac6454SAndrew Thompson 
86802ac6454SAndrew Thompson static void
869760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue)
87002ac6454SAndrew Thompson {
87102ac6454SAndrew Thompson 	/* no-op */
87202ac6454SAndrew Thompson 	return;
87302ac6454SAndrew Thompson }
87402ac6454SAndrew Thompson 
87502ac6454SAndrew Thompson static int
87602ac6454SAndrew Thompson cdce_suspend(device_t dev)
87702ac6454SAndrew Thompson {
87802ac6454SAndrew Thompson 	device_printf(dev, "Suspending\n");
87902ac6454SAndrew Thompson 	return (0);
88002ac6454SAndrew Thompson }
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson static int
88302ac6454SAndrew Thompson cdce_resume(device_t dev)
88402ac6454SAndrew Thompson {
88502ac6454SAndrew Thompson 	device_printf(dev, "Resuming\n");
88602ac6454SAndrew Thompson 	return (0);
88702ac6454SAndrew Thompson }
88802ac6454SAndrew Thompson 
88902ac6454SAndrew Thompson static void
890ed6d949aSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
89102ac6454SAndrew Thompson {
892ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
89302ac6454SAndrew Thompson 	struct mbuf *m;
89402ac6454SAndrew Thompson 	uint8_t x;
895ed6d949aSAndrew Thompson 	int actlen, aframes, len;
896ed6d949aSAndrew Thompson 
897ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
89802ac6454SAndrew Thompson 
89902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
90002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
90102ac6454SAndrew Thompson 
902ed6d949aSAndrew Thompson 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
90302ac6454SAndrew Thompson 
904ed6d949aSAndrew Thompson 		for (x = 0; x != aframes; x++) {
90502ac6454SAndrew Thompson 
90602ac6454SAndrew Thompson 			m = sc->sc_rx_buf[x];
90702ac6454SAndrew Thompson 			sc->sc_rx_buf[x] = NULL;
9088f9e0ef9SAndrew Thompson 			len = usbd_xfer_frame_len(xfer, x);
90902ac6454SAndrew Thompson 
91002ac6454SAndrew Thompson 			/* Strip off CRC added by Zaurus, if any */
911ed6d949aSAndrew Thompson 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
912ed6d949aSAndrew Thompson 				len -= 4;
91302ac6454SAndrew Thompson 
914ed6d949aSAndrew Thompson 			if (len < sizeof(struct ether_header)) {
91502ac6454SAndrew Thompson 				m_freem(m);
91602ac6454SAndrew Thompson 				continue;
91702ac6454SAndrew Thompson 			}
91802ac6454SAndrew Thompson 			/* queue up mbuf */
919ed6d949aSAndrew Thompson 			uether_rxmbuf(&sc->sc_ue, m, len);
92002ac6454SAndrew Thompson 		}
92102ac6454SAndrew Thompson 
92202ac6454SAndrew Thompson 		/* FALLTHROUGH */
92302ac6454SAndrew Thompson 	case USB_ST_SETUP:
92402ac6454SAndrew Thompson 		/*
92502ac6454SAndrew Thompson 		 * TODO: Implement support for multi frame transfers,
92602ac6454SAndrew Thompson 		 * when the USB hardware supports it.
92702ac6454SAndrew Thompson 		 */
92802ac6454SAndrew Thompson 		for (x = 0; x != 1; x++) {
92902ac6454SAndrew Thompson 			if (sc->sc_rx_buf[x] == NULL) {
930a593f6b8SAndrew Thompson 				m = uether_newbuf();
93102ac6454SAndrew Thompson 				if (m == NULL)
93202ac6454SAndrew Thompson 					goto tr_stall;
93302ac6454SAndrew Thompson 				sc->sc_rx_buf[x] = m;
93402ac6454SAndrew Thompson 			} else {
93502ac6454SAndrew Thompson 				m = sc->sc_rx_buf[x];
93602ac6454SAndrew Thompson 			}
93702ac6454SAndrew Thompson 
938ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
93902ac6454SAndrew Thompson 		}
94002ac6454SAndrew Thompson 		/* set number of frames and start hardware */
941ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, x);
942a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
94302ac6454SAndrew Thompson 		/* flush any received frames */
944a593f6b8SAndrew Thompson 		uether_rxflush(&sc->sc_ue);
94502ac6454SAndrew Thompson 		break;
94602ac6454SAndrew Thompson 
94702ac6454SAndrew Thompson 	default:			/* Error */
94802ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
949ed6d949aSAndrew Thompson 		    usbd_errstr(error));
95002ac6454SAndrew Thompson 
951ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
95202ac6454SAndrew Thompson tr_stall:
95302ac6454SAndrew Thompson 			/* try to clear stall first */
954ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
955ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
956a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
95702ac6454SAndrew Thompson 			break;
95802ac6454SAndrew Thompson 		}
95902ac6454SAndrew Thompson 
96002ac6454SAndrew Thompson 		/* need to free the RX-mbufs when we are cancelled */
96102ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
96202ac6454SAndrew Thompson 		break;
96302ac6454SAndrew Thompson 	}
96402ac6454SAndrew Thompson }
96502ac6454SAndrew Thompson 
96602ac6454SAndrew Thompson static void
967ed6d949aSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
96802ac6454SAndrew Thompson {
969ed6d949aSAndrew Thompson 	int actlen;
970ed6d949aSAndrew Thompson 
971ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
972ed6d949aSAndrew Thompson 
97302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
97402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
97502ac6454SAndrew Thompson 
976ed6d949aSAndrew Thompson 		DPRINTF("Received %d bytes\n", actlen);
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson 		/* TODO: decode some indications */
97902ac6454SAndrew Thompson 
98002ac6454SAndrew Thompson 		/* FALLTHROUGH */
98102ac6454SAndrew Thompson 	case USB_ST_SETUP:
98202ac6454SAndrew Thompson tr_setup:
983ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
984a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
98502ac6454SAndrew Thompson 		break;
98602ac6454SAndrew Thompson 
98702ac6454SAndrew Thompson 	default:			/* Error */
988ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
98902ac6454SAndrew Thompson 			/* start clear stall */
990ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
99102ac6454SAndrew Thompson 			goto tr_setup;
99202ac6454SAndrew Thompson 		}
99302ac6454SAndrew Thompson 		break;
99402ac6454SAndrew Thompson 	}
99502ac6454SAndrew Thompson }
99602ac6454SAndrew Thompson 
99702ac6454SAndrew Thompson static void
998ed6d949aSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
99902ac6454SAndrew Thompson {
1000ed6d949aSAndrew Thompson 	int actlen;
1001ed6d949aSAndrew Thompson 
1002ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1003ed6d949aSAndrew Thompson 
100402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
100502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
100602ac6454SAndrew Thompson 
1007ed6d949aSAndrew Thompson 		DPRINTF("Transferred %d bytes\n", actlen);
100802ac6454SAndrew Thompson 
100902ac6454SAndrew Thompson 		/* FALLTHROUGH */
101002ac6454SAndrew Thompson 	case USB_ST_SETUP:
101102ac6454SAndrew Thompson tr_setup:
101202ac6454SAndrew Thompson #if 0
1013ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, XXX);
1014a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
101502ac6454SAndrew Thompson #endif
101602ac6454SAndrew Thompson 		break;
101702ac6454SAndrew Thompson 
101802ac6454SAndrew Thompson 	default:			/* Error */
1019ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
102002ac6454SAndrew Thompson 			/* start clear stall */
1021ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
102202ac6454SAndrew Thompson 			goto tr_setup;
102302ac6454SAndrew Thompson 		}
102402ac6454SAndrew Thompson 		break;
102502ac6454SAndrew Thompson 	}
102602ac6454SAndrew Thompson }
102702ac6454SAndrew Thompson 
102802ac6454SAndrew Thompson static int
102902ac6454SAndrew Thompson cdce_handle_request(device_t dev,
103002ac6454SAndrew Thompson     const void *req, void **pptr, uint16_t *plen,
103129bd7d7eSAndrew Thompson     uint16_t offset, uint8_t *pstate)
103202ac6454SAndrew Thompson {
103302ac6454SAndrew Thompson 	return (ENXIO);			/* use builtin handler */
103402ac6454SAndrew Thompson }
10354076dd23SAndrew Thompson 
10364076dd23SAndrew Thompson #if CDCE_HAVE_NCM
10377e05daaeSHans Petter Selasky static void
10387e05daaeSHans Petter Selasky cdce_ncm_tx_zero(struct usb_page_cache *pc,
10397e05daaeSHans Petter Selasky     uint32_t start, uint32_t end)
10407e05daaeSHans Petter Selasky {
10417e05daaeSHans Petter Selasky 	if (start >= CDCE_NCM_TX_MAXLEN)
10427e05daaeSHans Petter Selasky 		return;
10437e05daaeSHans Petter Selasky 	if (end > CDCE_NCM_TX_MAXLEN)
10447e05daaeSHans Petter Selasky 		end = CDCE_NCM_TX_MAXLEN;
10457e05daaeSHans Petter Selasky 
10467e05daaeSHans Petter Selasky 	usbd_frame_zero(pc, start, end - start);
10477e05daaeSHans Petter Selasky }
10487e05daaeSHans Petter Selasky 
10494076dd23SAndrew Thompson static uint8_t
10504076dd23SAndrew Thompson cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
10514076dd23SAndrew Thompson {
10524076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
10534076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
10544076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
10554076dd23SAndrew Thompson 	struct mbuf *m;
10564076dd23SAndrew Thompson 	uint32_t rem;
10574076dd23SAndrew Thompson 	uint32_t offset;
10584076dd23SAndrew Thompson 	uint32_t last_offset;
10597e05daaeSHans Petter Selasky 	uint16_t n;
10607e05daaeSHans Petter Selasky 	uint8_t retval;
10614076dd23SAndrew Thompson 
10624076dd23SAndrew Thompson 	usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
10634076dd23SAndrew Thompson 
10644076dd23SAndrew Thompson 	offset = sizeof(sc->sc_ncm.hdr) +
10654076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
10664076dd23SAndrew Thompson 
10674076dd23SAndrew Thompson 	/* Store last valid offset before alignment */
10684076dd23SAndrew Thompson 	last_offset = offset;
10694076dd23SAndrew Thompson 
10707e05daaeSHans Petter Selasky 	/* Align offset */
10717e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
10727e05daaeSHans Petter Selasky 	    offset, sc->sc_ncm.tx_modulus);
10734076dd23SAndrew Thompson 
10747e05daaeSHans Petter Selasky 	/* Zero pad */
10757e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
10767e05daaeSHans Petter Selasky 
10777e05daaeSHans Petter Selasky 	/* buffer full */
10787e05daaeSHans Petter Selasky 	retval = 2;
10797e05daaeSHans Petter Selasky 
10807e05daaeSHans Petter Selasky 	for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
10814076dd23SAndrew Thompson 
10824076dd23SAndrew Thompson 		/* check if end of transmit buffer is reached */
10834076dd23SAndrew Thompson 
10844076dd23SAndrew Thompson 		if (offset >= sc->sc_ncm.tx_max)
10854076dd23SAndrew Thompson 			break;
10864076dd23SAndrew Thompson 
10874076dd23SAndrew Thompson 		/* compute maximum buffer size */
10884076dd23SAndrew Thompson 
10894076dd23SAndrew Thompson 		rem = sc->sc_ncm.tx_max - offset;
10904076dd23SAndrew Thompson 
10914076dd23SAndrew Thompson 		IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
10924076dd23SAndrew Thompson 
10937e05daaeSHans Petter Selasky 		if (m == NULL) {
10947e05daaeSHans Petter Selasky 			/* buffer not full */
10957e05daaeSHans Petter Selasky 			retval = 1;
10964076dd23SAndrew Thompson 			break;
10977e05daaeSHans Petter Selasky 		}
10984076dd23SAndrew Thompson 
10994076dd23SAndrew Thompson 		if (m->m_pkthdr.len > rem) {
11004076dd23SAndrew Thompson 			if (n == 0) {
11014076dd23SAndrew Thompson 				/* The frame won't fit in our buffer */
11024076dd23SAndrew Thompson 				DPRINTFN(1, "Frame too big to be transmitted!\n");
11034076dd23SAndrew Thompson 				m_freem(m);
11044076dd23SAndrew Thompson 				ifp->if_oerrors++;
11054076dd23SAndrew Thompson 				n--;
11064076dd23SAndrew Thompson 				continue;
11074076dd23SAndrew Thompson 			}
11084076dd23SAndrew Thompson 			/* Wait till next buffer becomes ready */
11094076dd23SAndrew Thompson 			IFQ_DRV_PREPEND(&(ifp->if_snd), m);
11104076dd23SAndrew Thompson 			break;
11114076dd23SAndrew Thompson 		}
11124076dd23SAndrew Thompson 		usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
11134076dd23SAndrew Thompson 
11144076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
11154076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
11164076dd23SAndrew Thompson 
11174076dd23SAndrew Thompson 		/* Update offset */
11184076dd23SAndrew Thompson 		offset += m->m_pkthdr.len;
11194076dd23SAndrew Thompson 
11204076dd23SAndrew Thompson 		/* Store last valid offset before alignment */
11214076dd23SAndrew Thompson 		last_offset = offset;
11224076dd23SAndrew Thompson 
11237e05daaeSHans Petter Selasky 		/* Align offset */
11247e05daaeSHans Petter Selasky 		offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
11257e05daaeSHans Petter Selasky 		    offset, sc->sc_ncm.tx_modulus);
11267e05daaeSHans Petter Selasky 
11277e05daaeSHans Petter Selasky 		/* Zero pad */
11287e05daaeSHans Petter Selasky 		cdce_ncm_tx_zero(pc, last_offset, offset);
11294076dd23SAndrew Thompson 
11304076dd23SAndrew Thompson 		/*
11314076dd23SAndrew Thompson 		 * If there's a BPF listener, bounce a copy
11324076dd23SAndrew Thompson 		 * of this frame to him:
11334076dd23SAndrew Thompson 		 */
11344076dd23SAndrew Thompson 		BPF_MTAP(ifp, m);
11354076dd23SAndrew Thompson 
11364076dd23SAndrew Thompson 		/* Free mbuf */
11374076dd23SAndrew Thompson 
11384076dd23SAndrew Thompson 		m_freem(m);
11394076dd23SAndrew Thompson 
11404076dd23SAndrew Thompson 		/* Pre-increment interface counter */
11414076dd23SAndrew Thompson 
11424076dd23SAndrew Thompson 		ifp->if_opackets++;
11434076dd23SAndrew Thompson 	}
11444076dd23SAndrew Thompson 
11454076dd23SAndrew Thompson 	if (n == 0)
11467e05daaeSHans Petter Selasky 		return (0);
11474076dd23SAndrew Thompson 
11484076dd23SAndrew Thompson 	rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
11494076dd23SAndrew Thompson 
11504076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wLength, rem);
11514076dd23SAndrew Thompson 
11524076dd23SAndrew Thompson 	/* zero the rest of the data pointer entries */
11534076dd23SAndrew Thompson 	for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
11544076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
11554076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
11564076dd23SAndrew Thompson 	}
11574076dd23SAndrew Thompson 
11587e05daaeSHans Petter Selasky 	offset = last_offset;
11597e05daaeSHans Petter Selasky 
11607e05daaeSHans Petter Selasky 	/* Align offset */
11617e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
11627e05daaeSHans Petter Selasky 
11637e05daaeSHans Petter Selasky 	/* Optimise, save bandwidth and force short termination */
11647e05daaeSHans Petter Selasky 	if (offset >= sc->sc_ncm.tx_max)
11657e05daaeSHans Petter Selasky 		offset = sc->sc_ncm.tx_max;
11667e05daaeSHans Petter Selasky 	else
11677e05daaeSHans Petter Selasky 		offset ++;
11687e05daaeSHans Petter Selasky 
11697e05daaeSHans Petter Selasky 	/* Zero pad */
11707e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
11717e05daaeSHans Petter Selasky 
11724076dd23SAndrew Thompson 	/* set frame length */
11737e05daaeSHans Petter Selasky 	usbd_xfer_set_frame_len(xfer, index, offset);
11744076dd23SAndrew Thompson 
11754076dd23SAndrew Thompson 	/* Fill out 16-bit header */
11764076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[0] = 'N';
11774076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[1] = 'C';
11784076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[2] = 'M';
11794076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[3] = 'H';
11804076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
11817e05daaeSHans Petter Selasky 	USETW(sc->sc_ncm.hdr.wBlockLength, offset);
11824076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
11834076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
11844076dd23SAndrew Thompson 
11854076dd23SAndrew Thompson 	sc->sc_ncm.tx_seq++;
11864076dd23SAndrew Thompson 
11874076dd23SAndrew Thompson 	/* Fill out 16-bit frame table header */
11884076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[0] = 'N';
11894076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[1] = 'C';
11904076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[2] = 'M';
119185e3d588SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[3] = '0';
11924076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0);		/* reserved */
11934076dd23SAndrew Thompson 
11944076dd23SAndrew Thompson 	usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
11954076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
11964076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt));
11974076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
11984076dd23SAndrew Thompson 	    &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
11997e05daaeSHans Petter Selasky 	return (retval);
12004076dd23SAndrew Thompson }
12014076dd23SAndrew Thompson 
12024076dd23SAndrew Thompson static void
12034076dd23SAndrew Thompson cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
12044076dd23SAndrew Thompson {
12054076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
12064076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
12074076dd23SAndrew Thompson 	uint16_t x;
12087e05daaeSHans Petter Selasky 	uint8_t temp;
12094076dd23SAndrew Thompson 	int actlen;
12104076dd23SAndrew Thompson 	int aframes;
12114076dd23SAndrew Thompson 
12124076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
12134076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
12144076dd23SAndrew Thompson 
12154076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
12164076dd23SAndrew Thompson 
12174076dd23SAndrew Thompson 		DPRINTFN(10, "transfer complete: "
12184076dd23SAndrew Thompson 		    "%u bytes in %u frames\n", actlen, aframes);
12194076dd23SAndrew Thompson 
12204076dd23SAndrew Thompson 	case USB_ST_SETUP:
12214076dd23SAndrew Thompson 		for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
12227e05daaeSHans Petter Selasky 			temp = cdce_ncm_fill_tx_frames(xfer, x);
12237e05daaeSHans Petter Selasky 			if (temp == 0)
12244076dd23SAndrew Thompson 				break;
12257e05daaeSHans Petter Selasky 			if (temp == 1) {
12267e05daaeSHans Petter Selasky 				x++;
12277e05daaeSHans Petter Selasky 				break;
12287e05daaeSHans Petter Selasky 			}
12294076dd23SAndrew Thompson 		}
12304076dd23SAndrew Thompson 
12314076dd23SAndrew Thompson 		if (x != 0) {
12327e05daaeSHans Petter Selasky #ifdef USB_DEBUG
12337e05daaeSHans Petter Selasky 			usbd_xfer_set_interval(xfer, cdce_tx_interval);
12347e05daaeSHans Petter Selasky #endif
12354076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
12364076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
12374076dd23SAndrew Thompson 		}
12384076dd23SAndrew Thompson 		break;
12394076dd23SAndrew Thompson 
12404076dd23SAndrew Thompson 	default:			/* Error */
12414076dd23SAndrew Thompson 		DPRINTFN(10, "Transfer error: %s\n",
12424076dd23SAndrew Thompson 		    usbd_errstr(error));
12434076dd23SAndrew Thompson 
12444076dd23SAndrew Thompson 		/* update error counter */
12454076dd23SAndrew Thompson 		ifp->if_oerrors += 1;
12464076dd23SAndrew Thompson 
12474076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
12484076dd23SAndrew Thompson 			/* try to clear stall first */
12494076dd23SAndrew Thompson 			usbd_xfer_set_stall(xfer);
12504076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
12514076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
12524076dd23SAndrew Thompson 		}
12534076dd23SAndrew Thompson 		break;
12544076dd23SAndrew Thompson 	}
12554076dd23SAndrew Thompson }
12564076dd23SAndrew Thompson 
12574076dd23SAndrew Thompson static void
12584076dd23SAndrew Thompson cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
12594076dd23SAndrew Thompson {
12604076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
12614076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
12624076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
12634076dd23SAndrew Thompson 	struct mbuf *m;
12644076dd23SAndrew Thompson 	int sumdata;
12654076dd23SAndrew Thompson 	int sumlen;
12664076dd23SAndrew Thompson 	int actlen;
12674076dd23SAndrew Thompson 	int aframes;
12684076dd23SAndrew Thompson 	int temp;
12694076dd23SAndrew Thompson 	int nframes;
12704076dd23SAndrew Thompson 	int x;
12714076dd23SAndrew Thompson 	int offset;
12724076dd23SAndrew Thompson 
12734076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
12744076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
12754076dd23SAndrew Thompson 
12764076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
12774076dd23SAndrew Thompson 
12784076dd23SAndrew Thompson 		DPRINTFN(1, "received %u bytes in %u frames\n",
12794076dd23SAndrew Thompson 		    actlen, aframes);
12804076dd23SAndrew Thompson 
12814076dd23SAndrew Thompson 		if (actlen < (sizeof(sc->sc_ncm.hdr) +
12824076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt))) {
12834076dd23SAndrew Thompson 			DPRINTFN(1, "frame too short\n");
128485e3d588SAndrew Thompson 			goto tr_setup;
12854076dd23SAndrew Thompson 		}
12864076dd23SAndrew Thompson 		usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
12874076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.hdr));
12884076dd23SAndrew Thompson 
12894076dd23SAndrew Thompson 		if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
12904076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
12914076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
12924076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
129385e3d588SAndrew Thompson 			DPRINTFN(1, "invalid HDR signature: "
129485e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
129585e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[0],
129685e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[1],
129785e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[2],
129885e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[3]);
12994076dd23SAndrew Thompson 			goto tr_stall;
13004076dd23SAndrew Thompson 		}
13014076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
13024076dd23SAndrew Thompson 		if (temp > sumlen) {
13034076dd23SAndrew Thompson 			DPRINTFN(1, "unsupported block length %u/%u\n",
13044076dd23SAndrew Thompson 			    temp, sumlen);
13054076dd23SAndrew Thompson 			goto tr_stall;
13064076dd23SAndrew Thompson 		}
13074076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
13084076dd23SAndrew Thompson 		if ((temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
130985e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
13104076dd23SAndrew Thompson 			goto tr_stall;
13114076dd23SAndrew Thompson 		}
13124076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
13134076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt));
13144076dd23SAndrew Thompson 
13154076dd23SAndrew Thompson 		if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
13164076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
13174076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
131885e3d588SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
131985e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT signature"
132085e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
132185e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[0],
132285e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[1],
132385e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[2],
132485e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[3]);
13254076dd23SAndrew Thompson 			goto tr_stall;
13264076dd23SAndrew Thompson 		}
13274076dd23SAndrew Thompson 		nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
13284076dd23SAndrew Thompson 
13294076dd23SAndrew Thompson 		/* Subtract size of header and last zero padded entry */
13304076dd23SAndrew Thompson 		if (nframes >= (2 + 1))
13314076dd23SAndrew Thompson 			nframes -= (2 + 1);
13324076dd23SAndrew Thompson 		else
13334076dd23SAndrew Thompson 			nframes = 0;
13344076dd23SAndrew Thompson 
13354076dd23SAndrew Thompson 		DPRINTFN(1, "nframes = %u\n", nframes);
13364076dd23SAndrew Thompson 
13374076dd23SAndrew Thompson 		temp += sizeof(sc->sc_ncm.dpt);
13384076dd23SAndrew Thompson 
13394076dd23SAndrew Thompson 		if ((temp + (4 * nframes)) > actlen)
13404076dd23SAndrew Thompson 			goto tr_stall;
13414076dd23SAndrew Thompson 
13424076dd23SAndrew Thompson 		if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
13434076dd23SAndrew Thompson 			DPRINTFN(1, "Truncating number of frames from %u to %u\n",
13444076dd23SAndrew Thompson 			    nframes, CDCE_NCM_SUBFRAMES_MAX);
13454076dd23SAndrew Thompson 			nframes = CDCE_NCM_SUBFRAMES_MAX;
13464076dd23SAndrew Thompson 		}
13474076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
13484076dd23SAndrew Thompson 
13494076dd23SAndrew Thompson 		sumdata = 0;
13504076dd23SAndrew Thompson 
13514076dd23SAndrew Thompson 		for (x = 0; x != nframes; x++) {
13524076dd23SAndrew Thompson 
13534076dd23SAndrew Thompson 			offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
13544076dd23SAndrew Thompson 			temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
13554076dd23SAndrew Thompson 
135629051223SAndrew Thompson 			if ((offset == 0) ||
135729051223SAndrew Thompson 			    (temp < sizeof(struct ether_header)) ||
135829051223SAndrew Thompson 			    (temp > (MCLBYTES - ETHER_ALIGN))) {
135929051223SAndrew Thompson 				DPRINTFN(1, "NULL frame detected at %d\n", x);
13604076dd23SAndrew Thompson 				m = NULL;
136129051223SAndrew Thompson 				/* silently ignore this frame */
13624076dd23SAndrew Thompson 				continue;
136329051223SAndrew Thompson 			} else if ((offset + temp) > actlen) {
136429051223SAndrew Thompson 				DPRINTFN(1, "invalid frame "
136529051223SAndrew Thompson 				    "detected at %d\n", x);
136629051223SAndrew Thompson 				m = NULL;
136729051223SAndrew Thompson 				/* silently ignore this frame */
136829051223SAndrew Thompson 				continue;
136929051223SAndrew Thompson 			} else if (temp > (MHLEN - ETHER_ALIGN)) {
13704076dd23SAndrew Thompson 				m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
13714076dd23SAndrew Thompson 			} else {
13724076dd23SAndrew Thompson 				m = m_gethdr(M_DONTWAIT, MT_DATA);
13734076dd23SAndrew Thompson 			}
13744076dd23SAndrew Thompson 
13754076dd23SAndrew Thompson 			DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
13764076dd23SAndrew Thompson 			    x, offset, temp);
13774076dd23SAndrew Thompson 
13784076dd23SAndrew Thompson 			/* check if we have a buffer */
13794076dd23SAndrew Thompson 			if (m) {
13804076dd23SAndrew Thompson 				m_adj(m, ETHER_ALIGN);
13814076dd23SAndrew Thompson 
13824076dd23SAndrew Thompson 				usbd_copy_out(pc, offset, m->m_data, temp);
13834076dd23SAndrew Thompson 
13844076dd23SAndrew Thompson 				/* enqueue */
13854076dd23SAndrew Thompson 				uether_rxmbuf(&sc->sc_ue, m, temp);
13864076dd23SAndrew Thompson 
13874076dd23SAndrew Thompson 				sumdata += temp;
13884076dd23SAndrew Thompson 			} else {
13894076dd23SAndrew Thompson 				ifp->if_ierrors++;
13904076dd23SAndrew Thompson 			}
13914076dd23SAndrew Thompson 		}
13924076dd23SAndrew Thompson 
13934076dd23SAndrew Thompson 		DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
13944076dd23SAndrew Thompson 
13954076dd23SAndrew Thompson 	case USB_ST_SETUP:
139685e3d588SAndrew Thompson tr_setup:
13974076dd23SAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
13984076dd23SAndrew Thompson 		usbd_xfer_set_frames(xfer, 1);
13994076dd23SAndrew Thompson 		usbd_transfer_submit(xfer);
14004076dd23SAndrew Thompson 		uether_rxflush(&sc->sc_ue);	/* must be last */
14014076dd23SAndrew Thompson 		break;
14024076dd23SAndrew Thompson 
14034076dd23SAndrew Thompson 	default:			/* Error */
14044076dd23SAndrew Thompson 		DPRINTFN(1, "error = %s\n",
14054076dd23SAndrew Thompson 		    usbd_errstr(error));
14064076dd23SAndrew Thompson 
14074076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
14084076dd23SAndrew Thompson tr_stall:
14094076dd23SAndrew Thompson 			/* try to clear stall first */
14104076dd23SAndrew Thompson 			usbd_xfer_set_stall(xfer);
14114076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
14124076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
14134076dd23SAndrew Thompson 		}
14144076dd23SAndrew Thompson 		break;
14154076dd23SAndrew Thompson 	}
14164076dd23SAndrew Thompson }
14174076dd23SAndrew Thompson #endif
1418