xref: /freebsd/sys/dev/usb/net/if_cdce.c (revision 290512231ace1cf864d7094a029a8677af51ccb6)
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/linker_set.h>
60ed6d949aSAndrew Thompson #include <sys/module.h>
61ed6d949aSAndrew Thompson #include <sys/lock.h>
62ed6d949aSAndrew Thompson #include <sys/mutex.h>
63ed6d949aSAndrew Thompson #include <sys/condvar.h>
64ed6d949aSAndrew Thompson #include <sys/sysctl.h>
65ed6d949aSAndrew Thompson #include <sys/sx.h>
66ed6d949aSAndrew Thompson #include <sys/unistd.h>
67ed6d949aSAndrew Thompson #include <sys/callout.h>
68ed6d949aSAndrew Thompson #include <sys/malloc.h>
69ed6d949aSAndrew Thompson #include <sys/priv.h>
70ed6d949aSAndrew Thompson 
7102ac6454SAndrew Thompson #include <dev/usb/usb.h>
72ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
73ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
75ed6d949aSAndrew Thompson #include "usbdevs.h"
7602ac6454SAndrew Thompson 
7702ac6454SAndrew Thompson #define	USB_DEBUG_VAR cdce_debug
7802ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
79ed6d949aSAndrew Thompson #include <dev/usb/usb_process.h>
80ed6d949aSAndrew Thompson #include "usb_if.h"
8102ac6454SAndrew Thompson 
8202ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h>
8302ac6454SAndrew Thompson #include <dev/usb/net/if_cdcereg.h>
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson static device_probe_t cdce_probe;
8602ac6454SAndrew Thompson static device_attach_t cdce_attach;
8702ac6454SAndrew Thompson static device_detach_t cdce_detach;
8802ac6454SAndrew Thompson static device_suspend_t cdce_suspend;
8902ac6454SAndrew Thompson static device_resume_t cdce_resume;
9002ac6454SAndrew Thompson static usb_handle_request_t cdce_handle_request;
9102ac6454SAndrew Thompson 
92e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_write_callback;
93e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_read_callback;
94e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_read_callback;
95e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_write_callback;
9602ac6454SAndrew Thompson 
974076dd23SAndrew Thompson #if CDCE_HAVE_NCM
984076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_write_callback;
994076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_read_callback;
1004076dd23SAndrew Thompson #endif
1014076dd23SAndrew Thompson 
102e0a69b51SAndrew Thompson static uether_fn_t cdce_attach_post;
103e0a69b51SAndrew Thompson static uether_fn_t cdce_init;
104e0a69b51SAndrew Thompson static uether_fn_t cdce_stop;
105e0a69b51SAndrew Thompson static uether_fn_t cdce_start;
106e0a69b51SAndrew Thompson static uether_fn_t cdce_setmulti;
107e0a69b51SAndrew Thompson static uether_fn_t cdce_setpromisc;
10802ac6454SAndrew Thompson 
10902ac6454SAndrew Thompson static uint32_t	cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson #if USB_DEBUG
11202ac6454SAndrew Thompson static int cdce_debug = 0;
11302ac6454SAndrew Thompson 
1149360ae40SAndrew Thompson 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");
11702ac6454SAndrew Thompson #endif
11802ac6454SAndrew Thompson 
119760bc48eSAndrew Thompson static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
12002ac6454SAndrew Thompson 
1214eae601eSAndrew Thompson 	[CDCE_BULK_RX] = {
12202ac6454SAndrew Thompson 		.type = UE_BULK,
12302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1244eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
12502ac6454SAndrew Thompson 		.if_index = 0,
1264eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1274eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1284eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
1294eae601eSAndrew Thompson 		.callback = cdce_bulk_read_callback,
1304eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
131f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
13202ac6454SAndrew Thompson 	},
13302ac6454SAndrew Thompson 
1344eae601eSAndrew Thompson 	[CDCE_BULK_TX] = {
13502ac6454SAndrew Thompson 		.type = UE_BULK,
13602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1374eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
13802ac6454SAndrew Thompson 		.if_index = 0,
1394eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1404eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1414eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
1424eae601eSAndrew Thompson 		.callback = cdce_bulk_write_callback,
1434eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
144f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
14502ac6454SAndrew Thompson 	},
14602ac6454SAndrew Thompson 
1474eae601eSAndrew Thompson 	[CDCE_INTR_RX] = {
14802ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
14902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1504eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
15102ac6454SAndrew Thompson 		.if_index = 1,
1524eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1534eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
1544eae601eSAndrew Thompson 		.callback = cdce_intr_read_callback,
1554eae601eSAndrew Thompson 		.timeout = 0,
1564eae601eSAndrew Thompson 		.usb_mode = USB_MODE_HOST,
1574eae601eSAndrew Thompson 	},
1584eae601eSAndrew Thompson 
1594eae601eSAndrew Thompson 	[CDCE_INTR_TX] = {
1604eae601eSAndrew Thompson 		.type = UE_INTERRUPT,
1614eae601eSAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1624eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
1634eae601eSAndrew Thompson 		.if_index = 1,
1644eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1654eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
1664eae601eSAndrew Thompson 		.callback = cdce_intr_write_callback,
1674eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
1684eae601eSAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
16902ac6454SAndrew Thompson 	},
17002ac6454SAndrew Thompson };
17102ac6454SAndrew Thompson 
1724076dd23SAndrew Thompson #if CDCE_HAVE_NCM
1734076dd23SAndrew Thompson static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
1744076dd23SAndrew Thompson 
1754076dd23SAndrew Thompson 	[CDCE_BULK_RX] = {
1764076dd23SAndrew Thompson 		.type = UE_BULK,
1774076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1784076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
1794076dd23SAndrew Thompson 		.if_index = 0,
1804076dd23SAndrew Thompson 		.frames = CDCE_NCM_RX_FRAMES_MAX,
1814076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
1824076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
1834076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_read_callback,
1844076dd23SAndrew Thompson 		.timeout = 0,	/* no timeout */
1854076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
1864076dd23SAndrew Thompson 	},
1874076dd23SAndrew Thompson 
1884076dd23SAndrew Thompson 	[CDCE_BULK_TX] = {
1894076dd23SAndrew Thompson 		.type = UE_BULK,
1904076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1914076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
1924076dd23SAndrew Thompson 		.if_index = 0,
1934076dd23SAndrew Thompson 		.frames = CDCE_NCM_TX_FRAMES_MAX,
1944076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
1954076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1964076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_write_callback,
1974076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
1984076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
1994076dd23SAndrew Thompson 	},
2004076dd23SAndrew Thompson 
2014076dd23SAndrew Thompson 	[CDCE_INTR_RX] = {
2024076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2034076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2044076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
2054076dd23SAndrew Thompson 		.if_index = 1,
2064076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2074076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
2084076dd23SAndrew Thompson 		.callback = cdce_intr_read_callback,
2094076dd23SAndrew Thompson 		.timeout = 0,
2104076dd23SAndrew Thompson 		.usb_mode = USB_MODE_HOST,
2114076dd23SAndrew Thompson 	},
2124076dd23SAndrew Thompson 
2134076dd23SAndrew Thompson 	[CDCE_INTR_TX] = {
2144076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2154076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2164076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
2174076dd23SAndrew Thompson 		.if_index = 1,
2184076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2194076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
2204076dd23SAndrew Thompson 		.callback = cdce_intr_write_callback,
2214076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2224076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
2234076dd23SAndrew Thompson 	},
2244076dd23SAndrew Thompson };
2254076dd23SAndrew Thompson #endif
2264076dd23SAndrew Thompson 
22702ac6454SAndrew Thompson static device_method_t cdce_methods[] = {
22802ac6454SAndrew Thompson 	/* USB interface */
22902ac6454SAndrew Thompson 	DEVMETHOD(usb_handle_request, cdce_handle_request),
23002ac6454SAndrew Thompson 
23102ac6454SAndrew Thompson 	/* Device interface */
23202ac6454SAndrew Thompson 	DEVMETHOD(device_probe, cdce_probe),
23302ac6454SAndrew Thompson 	DEVMETHOD(device_attach, cdce_attach),
23402ac6454SAndrew Thompson 	DEVMETHOD(device_detach, cdce_detach),
23502ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, cdce_suspend),
23602ac6454SAndrew Thompson 	DEVMETHOD(device_resume, cdce_resume),
23702ac6454SAndrew Thompson 
23802ac6454SAndrew Thompson 	{0, 0}
23902ac6454SAndrew Thompson };
24002ac6454SAndrew Thompson 
24102ac6454SAndrew Thompson static driver_t cdce_driver = {
24202ac6454SAndrew Thompson 	.name = "cdce",
24302ac6454SAndrew Thompson 	.methods = cdce_methods,
24402ac6454SAndrew Thompson 	.size = sizeof(struct cdce_softc),
24502ac6454SAndrew Thompson };
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson static devclass_t cdce_devclass;
24802ac6454SAndrew Thompson 
2499aef556dSAndrew Thompson DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0);
25002ac6454SAndrew Thompson MODULE_VERSION(cdce, 1);
25102ac6454SAndrew Thompson MODULE_DEPEND(cdce, uether, 1, 1, 1);
25202ac6454SAndrew Thompson MODULE_DEPEND(cdce, usb, 1, 1, 1);
25302ac6454SAndrew Thompson MODULE_DEPEND(cdce, ether, 1, 1, 1);
25402ac6454SAndrew Thompson 
255760bc48eSAndrew Thompson static const struct usb_ether_methods cdce_ue_methods = {
25602ac6454SAndrew Thompson 	.ue_attach_post = cdce_attach_post,
25702ac6454SAndrew Thompson 	.ue_start = cdce_start,
25802ac6454SAndrew Thompson 	.ue_init = cdce_init,
25902ac6454SAndrew Thompson 	.ue_stop = cdce_stop,
26002ac6454SAndrew Thompson 	.ue_setmulti = cdce_setmulti,
26102ac6454SAndrew Thompson 	.ue_setpromisc = cdce_setpromisc,
26202ac6454SAndrew Thompson };
26302ac6454SAndrew Thompson 
264760bc48eSAndrew Thompson static const struct usb_device_id cdce_devs[] = {
26502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
26602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
26702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
26802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
26902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
27202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
27302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
27402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
2789739167cSAlfred Perlstein 
2799739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
2809739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
2814076dd23SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
28202ac6454SAndrew Thompson };
28302ac6454SAndrew Thompson 
2844076dd23SAndrew Thompson #if CDCE_HAVE_NCM
2854076dd23SAndrew Thompson /*------------------------------------------------------------------------*
2864076dd23SAndrew Thompson  *	cdce_ncm_init
2874076dd23SAndrew Thompson  *
2884076dd23SAndrew Thompson  * Return values:
2894076dd23SAndrew Thompson  * 0: Success
2904076dd23SAndrew Thompson  * Else: Failure
2914076dd23SAndrew Thompson  *------------------------------------------------------------------------*/
2924076dd23SAndrew Thompson static uint8_t
2934076dd23SAndrew Thompson cdce_ncm_init(struct cdce_softc *sc)
2944076dd23SAndrew Thompson {
2954076dd23SAndrew Thompson 	struct usb_ncm_parameters temp;
2964076dd23SAndrew Thompson 	struct usb_device_request req;
2974076dd23SAndrew Thompson 	uDWord value;
2984076dd23SAndrew Thompson 	int err;
2994076dd23SAndrew Thompson 
3004076dd23SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
3014076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
3024076dd23SAndrew Thompson 	USETW(req.wValue, 0);
3034076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
3044076dd23SAndrew Thompson 	req.wIndex[1] = 0;
3054076dd23SAndrew Thompson 	USETW(req.wLength, sizeof(temp));
3064076dd23SAndrew Thompson 
3074076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
3084076dd23SAndrew Thompson 	    &temp, 0, NULL, 1000 /* ms */);
3094076dd23SAndrew Thompson 	if (err)
3104076dd23SAndrew Thompson 		return (1);
3114076dd23SAndrew Thompson 
3124076dd23SAndrew Thompson 	/* Read correct set of parameters according to device mode */
3134076dd23SAndrew Thompson 
3144076dd23SAndrew Thompson 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
3154076dd23SAndrew Thompson 		sc->sc_ncm.rx_max = UGETW(temp.dwNtbInMaxSize);
3164076dd23SAndrew Thompson 		sc->sc_ncm.tx_max = UGETW(temp.dwNtbOutMaxSize);
3174076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
3184076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
3194076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
3204076dd23SAndrew Thompson 	} else {
3214076dd23SAndrew Thompson 		sc->sc_ncm.rx_max = UGETW(temp.dwNtbOutMaxSize);
3224076dd23SAndrew Thompson 		sc->sc_ncm.tx_max = UGETW(temp.dwNtbInMaxSize);
3234076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
3244076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
3254076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
3264076dd23SAndrew Thompson 	}
3274076dd23SAndrew Thompson 
3284076dd23SAndrew Thompson 	/* Verify maximum receive length */
3294076dd23SAndrew Thompson 
3304076dd23SAndrew Thompson 	if (err || (sc->sc_ncm.rx_max < 32) ||
3314076dd23SAndrew Thompson 	    (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
3324076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum receive length\n");
3334076dd23SAndrew Thompson 		sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
3344076dd23SAndrew Thompson 	}
3354076dd23SAndrew Thompson 
3364076dd23SAndrew Thompson 	/* Verify maximum transmit length */
3374076dd23SAndrew Thompson 
3384076dd23SAndrew Thompson 	if (err || (sc->sc_ncm.tx_max < 32) ||
3394076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
3404076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum transmit length\n");
3414076dd23SAndrew Thompson 		sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
3424076dd23SAndrew Thompson 	}
3434076dd23SAndrew Thompson 
3444076dd23SAndrew Thompson 	/*
3454076dd23SAndrew Thompson 	 * Verify that the structure alignment is:
3464076dd23SAndrew Thompson 	 * - power of two
3474076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
3484076dd23SAndrew Thompson 	 * - not less than four bytes
3494076dd23SAndrew Thompson 	 */
3504076dd23SAndrew Thompson 	if (err || (sc->sc_ncm.tx_struct_align < 4) ||
3514076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align !=
3524076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
3534076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
3544076dd23SAndrew Thompson 		DPRINTFN(1, "Using default other alignment: 4 bytes\n");
3554076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = 4;
3564076dd23SAndrew Thompson 	}
3574076dd23SAndrew Thompson 
3584076dd23SAndrew Thompson 	/*
3594076dd23SAndrew Thompson 	 * Verify that the payload alignment is:
3604076dd23SAndrew Thompson 	 * - power of two
3614076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
3624076dd23SAndrew Thompson 	 * - not less than four bytes
3634076dd23SAndrew Thompson 	 */
3644076dd23SAndrew Thompson 	if (err || (sc->sc_ncm.tx_modulus < 4) ||
3654076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus !=
3664076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
3674076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
3684076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
3694076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = 4;
3704076dd23SAndrew Thompson 	}
3714076dd23SAndrew Thompson 
3724076dd23SAndrew Thompson 	/* Verify that the payload remainder */
3734076dd23SAndrew Thompson 
3744076dd23SAndrew Thompson 	if (err || (sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
3754076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
3764076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = 0;
3774076dd23SAndrew Thompson 	}
3784076dd23SAndrew Thompson 
3794076dd23SAndrew Thompson 	/* Additional configuration, will fail in device side mode, which is OK. */
3804076dd23SAndrew Thompson 
3814076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
3824076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
3834076dd23SAndrew Thompson 	USETW(req.wValue, 0);
3844076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
3854076dd23SAndrew Thompson 	req.wIndex[1] = 0;
3864076dd23SAndrew Thompson 	USETW(req.wLength, 4);
3874076dd23SAndrew Thompson 	USETDW(value, sc->sc_ncm.rx_max);
3884076dd23SAndrew Thompson 
3894076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
3904076dd23SAndrew Thompson 	    &value, 0, NULL, 1000 /* ms */);
3914076dd23SAndrew Thompson 	if (err) {
3924076dd23SAndrew Thompson 		DPRINTFN(1, "Setting input size "
3934076dd23SAndrew Thompson 		    "to %u failed.\n", sc->sc_ncm.rx_max);
3944076dd23SAndrew Thompson 	}
3954076dd23SAndrew Thompson 
3964076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
3974076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_CRC_MODE;
3984076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* no CRC */
3994076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4004076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4014076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4024076dd23SAndrew Thompson 
4034076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4044076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4054076dd23SAndrew Thompson 	if (err) {
4064076dd23SAndrew Thompson 		DPRINTFN(1, "Setting CRC mode to off failed.\n");
4074076dd23SAndrew Thompson 	}
4084076dd23SAndrew Thompson 
4094076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4104076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
4114076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* NTB-16 */
4124076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4134076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4144076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4154076dd23SAndrew Thompson 
4164076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4174076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4184076dd23SAndrew Thompson 	if (err) {
4194076dd23SAndrew Thompson 		DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
4204076dd23SAndrew Thompson 	}
4214076dd23SAndrew Thompson 
4224076dd23SAndrew Thompson 	return (0);		/* success */
4234076dd23SAndrew Thompson }
4244076dd23SAndrew Thompson #endif
4254076dd23SAndrew Thompson 
42602ac6454SAndrew Thompson static int
42702ac6454SAndrew Thompson cdce_probe(device_t dev)
42802ac6454SAndrew Thompson {
429760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
43002ac6454SAndrew Thompson 
431a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(cdce_devs, sizeof(cdce_devs), uaa));
43202ac6454SAndrew Thompson }
43302ac6454SAndrew Thompson 
43402ac6454SAndrew Thompson static void
435760bc48eSAndrew Thompson cdce_attach_post(struct usb_ether *ue)
43602ac6454SAndrew Thompson {
43702ac6454SAndrew Thompson 	/* no-op */
43802ac6454SAndrew Thompson 	return;
43902ac6454SAndrew Thompson }
44002ac6454SAndrew Thompson 
44102ac6454SAndrew Thompson static int
44202ac6454SAndrew Thompson cdce_attach(device_t dev)
44302ac6454SAndrew Thompson {
44402ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
445760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
446760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
447760bc48eSAndrew Thompson 	struct usb_interface *iface;
448760bc48eSAndrew Thompson 	const struct usb_cdc_union_descriptor *ud;
449760bc48eSAndrew Thompson 	const struct usb_interface_descriptor *id;
450760bc48eSAndrew Thompson 	const struct usb_cdc_ethernet_descriptor *ued;
4514076dd23SAndrew Thompson 	const struct usb_config *pcfg;
45202ac6454SAndrew Thompson 	int error;
45302ac6454SAndrew Thompson 	uint8_t i;
4544076dd23SAndrew Thompson 	uint8_t data_iface_no;
45502ac6454SAndrew Thompson 	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
4584076dd23SAndrew Thompson 	sc->sc_ue.ue_udev = uaa->device;
45902ac6454SAndrew Thompson 
460a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
46302ac6454SAndrew Thompson 
4643c8d24f4SAndrew Thompson 	ud = usbd_find_descriptor
46502ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
46602ac6454SAndrew Thompson 	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
46702ac6454SAndrew Thompson 
4684076dd23SAndrew Thompson 	if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
4694076dd23SAndrew Thompson 	    (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
4704076dd23SAndrew Thompson 		DPRINTFN(1, "No union descriptor!\n");
4714076dd23SAndrew Thompson 		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
4724076dd23SAndrew Thompson 		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
4734076dd23SAndrew Thompson 		goto alloc_transfers;
47402ac6454SAndrew Thompson 	}
4754076dd23SAndrew Thompson 	data_iface_no = ud->bSlaveInterface[0];
47602ac6454SAndrew Thompson 
47702ac6454SAndrew Thompson 	for (i = 0;; i++) {
47802ac6454SAndrew Thompson 
479a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
48002ac6454SAndrew Thompson 
48102ac6454SAndrew Thompson 		if (iface) {
48202ac6454SAndrew Thompson 
483a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
48402ac6454SAndrew Thompson 
4854076dd23SAndrew Thompson 			if (id && (id->bInterfaceNumber == data_iface_no)) {
48602ac6454SAndrew Thompson 				sc->sc_ifaces_index[0] = i;
48702ac6454SAndrew Thompson 				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
488a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
48902ac6454SAndrew Thompson 				break;
49002ac6454SAndrew Thompson 			}
49102ac6454SAndrew Thompson 		} else {
49202ac6454SAndrew Thompson 			device_printf(dev, "no data interface found!\n");
49302ac6454SAndrew Thompson 			goto detach;
49402ac6454SAndrew Thompson 		}
49502ac6454SAndrew Thompson 	}
49602ac6454SAndrew Thompson 
49702ac6454SAndrew Thompson 	/*
49802ac6454SAndrew Thompson 	 * <quote>
49902ac6454SAndrew Thompson 	 *
50002ac6454SAndrew Thompson 	 *  The Data Class interface of a networking device shall have
50102ac6454SAndrew Thompson 	 *  a minimum of two interface settings. The first setting
50202ac6454SAndrew Thompson 	 *  (the default interface setting) includes no endpoints and
50302ac6454SAndrew Thompson 	 *  therefore no networking traffic is exchanged whenever the
50402ac6454SAndrew Thompson 	 *  default interface setting is selected. One or more
50502ac6454SAndrew Thompson 	 *  additional interface settings are used for normal
50602ac6454SAndrew Thompson 	 *  operation, and therefore each includes a pair of endpoints
50702ac6454SAndrew Thompson 	 *  (one IN, and one OUT) to exchange network traffic. Select
50802ac6454SAndrew Thompson 	 *  an alternate interface setting to initialize the network
50902ac6454SAndrew Thompson 	 *  aspects of the device and to enable the exchange of
51002ac6454SAndrew Thompson 	 *  network traffic.
51102ac6454SAndrew Thompson 	 *
51202ac6454SAndrew Thompson 	 * </quote>
51302ac6454SAndrew Thompson 	 *
51402ac6454SAndrew Thompson 	 * Some devices, most notably cable modems, include interface
51502ac6454SAndrew Thompson 	 * settings that have no IN or OUT endpoint, therefore loop
51602ac6454SAndrew Thompson 	 * through the list of all available interface settings
51702ac6454SAndrew Thompson 	 * looking for one with both IN and OUT endpoints.
51802ac6454SAndrew Thompson 	 */
51902ac6454SAndrew Thompson 
52002ac6454SAndrew Thompson alloc_transfers:
52102ac6454SAndrew Thompson 
5224076dd23SAndrew Thompson 	pcfg = cdce_config;	/* Default Configuration */
5234076dd23SAndrew Thompson 
52402ac6454SAndrew Thompson 	for (i = 0; i != 32; i++) {
52502ac6454SAndrew Thompson 
5264076dd23SAndrew Thompson 		error = usbd_set_alt_interface_index(uaa->device,
5274076dd23SAndrew Thompson 		    sc->sc_ifaces_index[0], i);
5284076dd23SAndrew Thompson 		if (error)
5294076dd23SAndrew Thompson 			break;
5304076dd23SAndrew Thompson #if CDCE_HAVE_NCM
5314076dd23SAndrew Thompson 		if ((i == 0) && (cdce_ncm_init(sc) == 0))
5324076dd23SAndrew Thompson 			pcfg = cdce_ncm_config;
5334076dd23SAndrew Thompson #endif
5344076dd23SAndrew Thompson 		error = usbd_transfer_setup(uaa->device,
5354076dd23SAndrew Thompson 		    sc->sc_ifaces_index, sc->sc_xfer,
5364076dd23SAndrew Thompson 		    pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
53702ac6454SAndrew Thompson 
5384076dd23SAndrew Thompson 		if (error == 0)
53902ac6454SAndrew Thompson 			break;
54002ac6454SAndrew Thompson 	}
5414076dd23SAndrew Thompson 
5424076dd23SAndrew Thompson 	if (error || (i == 32)) {
5434076dd23SAndrew Thompson 		device_printf(dev, "No valid alternate "
5444076dd23SAndrew Thompson 		    "setting found!\n");
5454076dd23SAndrew Thompson 		goto detach;
54602ac6454SAndrew Thompson 	}
54702ac6454SAndrew Thompson 
5483c8d24f4SAndrew Thompson 	ued = usbd_find_descriptor
54902ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
55002ac6454SAndrew Thompson 	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1);
55102ac6454SAndrew Thompson 
55202ac6454SAndrew Thompson 	if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
55302ac6454SAndrew Thompson 		error = USB_ERR_INVAL;
55402ac6454SAndrew Thompson 	} else {
555a593f6b8SAndrew Thompson 		error = usbd_req_get_string_any(uaa->device, NULL,
55602ac6454SAndrew Thompson 		    eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
55702ac6454SAndrew Thompson 	}
55802ac6454SAndrew Thompson 
55902ac6454SAndrew Thompson 	if (error) {
56002ac6454SAndrew Thompson 
56102ac6454SAndrew Thompson 		/* fake MAC address */
56202ac6454SAndrew Thompson 
56302ac6454SAndrew Thompson 		device_printf(dev, "faking MAC address\n");
56402ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[0] = 0x2a;
56502ac6454SAndrew Thompson 		memcpy(&sc->sc_ue.ue_eaddr[1], &ticks, sizeof(uint32_t));
56602ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
56702ac6454SAndrew Thompson 
56802ac6454SAndrew Thompson 	} else {
56902ac6454SAndrew Thompson 
57002ac6454SAndrew Thompson 		bzero(sc->sc_ue.ue_eaddr, sizeof(sc->sc_ue.ue_eaddr));
57102ac6454SAndrew Thompson 
57202ac6454SAndrew Thompson 		for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
57302ac6454SAndrew Thompson 
57402ac6454SAndrew Thompson 			char c = eaddr_str[i];
57502ac6454SAndrew Thompson 
57602ac6454SAndrew Thompson 			if ('0' <= c && c <= '9')
57702ac6454SAndrew Thompson 				c -= '0';
57802ac6454SAndrew Thompson 			else if (c != 0)
57902ac6454SAndrew Thompson 				c -= 'A' - 10;
58002ac6454SAndrew Thompson 			else
58102ac6454SAndrew Thompson 				break;
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson 			c &= 0xf;
58402ac6454SAndrew Thompson 
58502ac6454SAndrew Thompson 			if ((i & 1) == 0)
58602ac6454SAndrew Thompson 				c <<= 4;
58702ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[i / 2] |= c;
58802ac6454SAndrew Thompson 		}
58902ac6454SAndrew Thompson 
590f29a0724SAndrew Thompson 		if (uaa->usb_mode == USB_MODE_DEVICE) {
59102ac6454SAndrew Thompson 			/*
59202ac6454SAndrew Thompson 			 * Do not use the same MAC address like the peer !
59302ac6454SAndrew Thompson 			 */
59402ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[5] ^= 0xFF;
59502ac6454SAndrew Thompson 		}
59602ac6454SAndrew Thompson 	}
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson 	ue->ue_sc = sc;
59902ac6454SAndrew Thompson 	ue->ue_dev = dev;
60002ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
60102ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
60202ac6454SAndrew Thompson 	ue->ue_methods = &cdce_ue_methods;
60302ac6454SAndrew Thompson 
604a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
60502ac6454SAndrew Thompson 	if (error) {
60602ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
60702ac6454SAndrew Thompson 		goto detach;
60802ac6454SAndrew Thompson 	}
60902ac6454SAndrew Thompson 	return (0);			/* success */
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson detach:
61202ac6454SAndrew Thompson 	cdce_detach(dev);
61302ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
61402ac6454SAndrew Thompson }
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson static int
61702ac6454SAndrew Thompson cdce_detach(device_t dev)
61802ac6454SAndrew Thompson {
61902ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
620760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 	/* stop all USB transfers first */
623a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
624a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
62502ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
62602ac6454SAndrew Thompson 
62702ac6454SAndrew Thompson 	return (0);
62802ac6454SAndrew Thompson }
62902ac6454SAndrew Thompson 
63002ac6454SAndrew Thompson static void
631760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue)
63202ac6454SAndrew Thompson {
633a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson 	/*
63602ac6454SAndrew Thompson 	 * Start the USB transfers, if not already started:
63702ac6454SAndrew Thompson 	 */
638a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
639a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
64002ac6454SAndrew Thompson }
64102ac6454SAndrew Thompson 
64202ac6454SAndrew Thompson static void
64302ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n)
64402ac6454SAndrew Thompson {
64502ac6454SAndrew Thompson 	uint8_t x;
64602ac6454SAndrew Thompson 	for (x = 0; x != n; x++) {
64702ac6454SAndrew Thompson 		if (ppm[x] != NULL) {
64802ac6454SAndrew Thompson 			m_freem(ppm[x]);
64902ac6454SAndrew Thompson 			ppm[x] = NULL;
65002ac6454SAndrew Thompson 		}
65102ac6454SAndrew Thompson 	}
65202ac6454SAndrew Thompson }
65302ac6454SAndrew Thompson 
65402ac6454SAndrew Thompson static void
655ed6d949aSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
65602ac6454SAndrew Thompson {
657ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
658a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
65902ac6454SAndrew Thompson 	struct mbuf *m;
66002ac6454SAndrew Thompson 	struct mbuf *mt;
66102ac6454SAndrew Thompson 	uint32_t crc;
66202ac6454SAndrew Thompson 	uint8_t x;
663ed6d949aSAndrew Thompson 	int actlen, aframes;
664ed6d949aSAndrew Thompson 
665ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson 	DPRINTFN(1, "\n");
66802ac6454SAndrew Thompson 
66902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
67002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
671ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
672ed6d949aSAndrew Thompson 		    actlen, aframes);
67302ac6454SAndrew Thompson 
67402ac6454SAndrew Thompson 		ifp->if_opackets++;
67502ac6454SAndrew Thompson 
67602ac6454SAndrew Thompson 		/* free all previous TX buffers */
67702ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
67802ac6454SAndrew Thompson 
67902ac6454SAndrew Thompson 		/* FALLTHROUGH */
68002ac6454SAndrew Thompson 	case USB_ST_SETUP:
68102ac6454SAndrew Thompson tr_setup:
68202ac6454SAndrew Thompson 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
68302ac6454SAndrew Thompson 
68402ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
68502ac6454SAndrew Thompson 
68602ac6454SAndrew Thompson 			if (m == NULL)
68702ac6454SAndrew Thompson 				break;
68802ac6454SAndrew Thompson 
68902ac6454SAndrew Thompson 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
69002ac6454SAndrew Thompson 				/*
69102ac6454SAndrew Thompson 				 * Zaurus wants a 32-bit CRC appended
69202ac6454SAndrew Thompson 				 * to every frame
69302ac6454SAndrew Thompson 				 */
69402ac6454SAndrew Thompson 
69502ac6454SAndrew Thompson 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
69602ac6454SAndrew Thompson 				crc = htole32(crc);
69702ac6454SAndrew Thompson 
69802ac6454SAndrew Thompson 				if (!m_append(m, 4, (void *)&crc)) {
69902ac6454SAndrew Thompson 					m_freem(m);
70002ac6454SAndrew Thompson 					ifp->if_oerrors++;
70102ac6454SAndrew Thompson 					continue;
70202ac6454SAndrew Thompson 				}
70302ac6454SAndrew Thompson 			}
70402ac6454SAndrew Thompson 			if (m->m_len != m->m_pkthdr.len) {
70502ac6454SAndrew Thompson 				mt = m_defrag(m, M_DONTWAIT);
70602ac6454SAndrew Thompson 				if (mt == NULL) {
70702ac6454SAndrew Thompson 					m_freem(m);
70802ac6454SAndrew Thompson 					ifp->if_oerrors++;
70902ac6454SAndrew Thompson 					continue;
71002ac6454SAndrew Thompson 				}
71102ac6454SAndrew Thompson 				m = mt;
71202ac6454SAndrew Thompson 			}
71302ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
71402ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
71502ac6454SAndrew Thompson 			}
71602ac6454SAndrew Thompson 			sc->sc_tx_buf[x] = m;
717ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
71802ac6454SAndrew Thompson 
71902ac6454SAndrew Thompson 			/*
72002ac6454SAndrew Thompson 			 * If there's a BPF listener, bounce a copy of
72102ac6454SAndrew Thompson 			 * this frame to him:
72202ac6454SAndrew Thompson 			 */
72302ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
72402ac6454SAndrew Thompson 		}
72502ac6454SAndrew Thompson 		if (x != 0) {
726ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
727ed6d949aSAndrew Thompson 
728a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
72902ac6454SAndrew Thompson 		}
73002ac6454SAndrew Thompson 		break;
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson 	default:			/* Error */
73302ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
734ed6d949aSAndrew Thompson 		    usbd_errstr(error));
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson 		/* free all previous TX buffers */
73702ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 		/* count output errors */
74002ac6454SAndrew Thompson 		ifp->if_oerrors++;
74102ac6454SAndrew Thompson 
742ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
74302ac6454SAndrew Thompson 			/* try to clear stall first */
744ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
74502ac6454SAndrew Thompson 			goto tr_setup;
74602ac6454SAndrew Thompson 		}
74702ac6454SAndrew Thompson 		break;
74802ac6454SAndrew Thompson 	}
74902ac6454SAndrew Thompson }
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson static int32_t
75202ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
75302ac6454SAndrew Thompson {
75402ac6454SAndrew Thompson 	uint32_t *p_crc = arg;
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson 	*p_crc = crc32_raw(src, count, *p_crc);
75702ac6454SAndrew Thompson 	return (0);
75802ac6454SAndrew Thompson }
75902ac6454SAndrew Thompson 
76002ac6454SAndrew Thompson static uint32_t
76102ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
76202ac6454SAndrew Thompson {
76302ac6454SAndrew Thompson 	uint32_t crc = 0xFFFFFFFF;
76402ac6454SAndrew Thompson 	int error;
76502ac6454SAndrew Thompson 
76602ac6454SAndrew Thompson 	error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
76702ac6454SAndrew Thompson 	return (crc ^ 0xFFFFFFFF);
76802ac6454SAndrew Thompson }
76902ac6454SAndrew Thompson 
77002ac6454SAndrew Thompson static void
771760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue)
77202ac6454SAndrew Thompson {
773a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
774a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
77502ac6454SAndrew Thompson 
77602ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
77702ac6454SAndrew Thompson 
77802ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson 	/* start interrupt transfer */
781a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
782a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
78302ac6454SAndrew Thompson 
78402ac6454SAndrew Thompson 	/* stall data write direction, which depends on USB mode */
785ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
78602ac6454SAndrew Thompson 
78702ac6454SAndrew Thompson 	/* start data transfers */
78802ac6454SAndrew Thompson 	cdce_start(ue);
78902ac6454SAndrew Thompson }
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson static void
792760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue)
79302ac6454SAndrew Thompson {
794a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
795a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
79802ac6454SAndrew Thompson 
79902ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson 	/*
80202ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
80302ac6454SAndrew Thompson 	 */
804a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
805a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
806a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
807a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
80802ac6454SAndrew Thompson }
80902ac6454SAndrew Thompson 
81002ac6454SAndrew Thompson static void
811760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue)
81202ac6454SAndrew Thompson {
81302ac6454SAndrew Thompson 	/* no-op */
81402ac6454SAndrew Thompson 	return;
81502ac6454SAndrew Thompson }
81602ac6454SAndrew Thompson 
81702ac6454SAndrew Thompson static void
818760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue)
81902ac6454SAndrew Thompson {
82002ac6454SAndrew Thompson 	/* no-op */
82102ac6454SAndrew Thompson 	return;
82202ac6454SAndrew Thompson }
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson static int
82502ac6454SAndrew Thompson cdce_suspend(device_t dev)
82602ac6454SAndrew Thompson {
82702ac6454SAndrew Thompson 	device_printf(dev, "Suspending\n");
82802ac6454SAndrew Thompson 	return (0);
82902ac6454SAndrew Thompson }
83002ac6454SAndrew Thompson 
83102ac6454SAndrew Thompson static int
83202ac6454SAndrew Thompson cdce_resume(device_t dev)
83302ac6454SAndrew Thompson {
83402ac6454SAndrew Thompson 	device_printf(dev, "Resuming\n");
83502ac6454SAndrew Thompson 	return (0);
83602ac6454SAndrew Thompson }
83702ac6454SAndrew Thompson 
83802ac6454SAndrew Thompson static void
839ed6d949aSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
84002ac6454SAndrew Thompson {
841ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
84202ac6454SAndrew Thompson 	struct mbuf *m;
84302ac6454SAndrew Thompson 	uint8_t x;
844ed6d949aSAndrew Thompson 	int actlen, aframes, len;
845ed6d949aSAndrew Thompson 
846ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
84902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
85002ac6454SAndrew Thompson 
851ed6d949aSAndrew Thompson 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
85202ac6454SAndrew Thompson 
853ed6d949aSAndrew Thompson 		for (x = 0; x != aframes; x++) {
85402ac6454SAndrew Thompson 
85502ac6454SAndrew Thompson 			m = sc->sc_rx_buf[x];
85602ac6454SAndrew Thompson 			sc->sc_rx_buf[x] = NULL;
8578f9e0ef9SAndrew Thompson 			len = usbd_xfer_frame_len(xfer, x);
85802ac6454SAndrew Thompson 
85902ac6454SAndrew Thompson 			/* Strip off CRC added by Zaurus, if any */
860ed6d949aSAndrew Thompson 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
861ed6d949aSAndrew Thompson 				len -= 4;
86202ac6454SAndrew Thompson 
863ed6d949aSAndrew Thompson 			if (len < sizeof(struct ether_header)) {
86402ac6454SAndrew Thompson 				m_freem(m);
86502ac6454SAndrew Thompson 				continue;
86602ac6454SAndrew Thompson 			}
86702ac6454SAndrew Thompson 			/* queue up mbuf */
868ed6d949aSAndrew Thompson 			uether_rxmbuf(&sc->sc_ue, m, len);
86902ac6454SAndrew Thompson 		}
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson 		/* FALLTHROUGH */
87202ac6454SAndrew Thompson 	case USB_ST_SETUP:
87302ac6454SAndrew Thompson 		/*
87402ac6454SAndrew Thompson 		 * TODO: Implement support for multi frame transfers,
87502ac6454SAndrew Thompson 		 * when the USB hardware supports it.
87602ac6454SAndrew Thompson 		 */
87702ac6454SAndrew Thompson 		for (x = 0; x != 1; x++) {
87802ac6454SAndrew Thompson 			if (sc->sc_rx_buf[x] == NULL) {
879a593f6b8SAndrew Thompson 				m = uether_newbuf();
88002ac6454SAndrew Thompson 				if (m == NULL)
88102ac6454SAndrew Thompson 					goto tr_stall;
88202ac6454SAndrew Thompson 				sc->sc_rx_buf[x] = m;
88302ac6454SAndrew Thompson 			} else {
88402ac6454SAndrew Thompson 				m = sc->sc_rx_buf[x];
88502ac6454SAndrew Thompson 			}
88602ac6454SAndrew Thompson 
887ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
88802ac6454SAndrew Thompson 		}
88902ac6454SAndrew Thompson 		/* set number of frames and start hardware */
890ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, x);
891a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
89202ac6454SAndrew Thompson 		/* flush any received frames */
893a593f6b8SAndrew Thompson 		uether_rxflush(&sc->sc_ue);
89402ac6454SAndrew Thompson 		break;
89502ac6454SAndrew Thompson 
89602ac6454SAndrew Thompson 	default:			/* Error */
89702ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
898ed6d949aSAndrew Thompson 		    usbd_errstr(error));
89902ac6454SAndrew Thompson 
900ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
90102ac6454SAndrew Thompson tr_stall:
90202ac6454SAndrew Thompson 			/* try to clear stall first */
903ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
904ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
905a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
90602ac6454SAndrew Thompson 			break;
90702ac6454SAndrew Thompson 		}
90802ac6454SAndrew Thompson 
90902ac6454SAndrew Thompson 		/* need to free the RX-mbufs when we are cancelled */
91002ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
91102ac6454SAndrew Thompson 		break;
91202ac6454SAndrew Thompson 	}
91302ac6454SAndrew Thompson }
91402ac6454SAndrew Thompson 
91502ac6454SAndrew Thompson static void
916ed6d949aSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
91702ac6454SAndrew Thompson {
918ed6d949aSAndrew Thompson 	int actlen;
919ed6d949aSAndrew Thompson 
920ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
921ed6d949aSAndrew Thompson 
92202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
92302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
92402ac6454SAndrew Thompson 
925ed6d949aSAndrew Thompson 		DPRINTF("Received %d bytes\n", actlen);
92602ac6454SAndrew Thompson 
92702ac6454SAndrew Thompson 		/* TODO: decode some indications */
92802ac6454SAndrew Thompson 
92902ac6454SAndrew Thompson 		/* FALLTHROUGH */
93002ac6454SAndrew Thompson 	case USB_ST_SETUP:
93102ac6454SAndrew Thompson tr_setup:
932ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
933a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
93402ac6454SAndrew Thompson 		break;
93502ac6454SAndrew Thompson 
93602ac6454SAndrew Thompson 	default:			/* Error */
937ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
93802ac6454SAndrew Thompson 			/* start clear stall */
939ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
94002ac6454SAndrew Thompson 			goto tr_setup;
94102ac6454SAndrew Thompson 		}
94202ac6454SAndrew Thompson 		break;
94302ac6454SAndrew Thompson 	}
94402ac6454SAndrew Thompson }
94502ac6454SAndrew Thompson 
94602ac6454SAndrew Thompson static void
947ed6d949aSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
94802ac6454SAndrew Thompson {
949ed6d949aSAndrew Thompson 	int actlen;
950ed6d949aSAndrew Thompson 
951ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
952ed6d949aSAndrew Thompson 
95302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
95402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
95502ac6454SAndrew Thompson 
956ed6d949aSAndrew Thompson 		DPRINTF("Transferred %d bytes\n", actlen);
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson 		/* FALLTHROUGH */
95902ac6454SAndrew Thompson 	case USB_ST_SETUP:
96002ac6454SAndrew Thompson tr_setup:
96102ac6454SAndrew Thompson #if 0
962ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, XXX);
963a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
96402ac6454SAndrew Thompson #endif
96502ac6454SAndrew Thompson 		break;
96602ac6454SAndrew Thompson 
96702ac6454SAndrew Thompson 	default:			/* Error */
968ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
96902ac6454SAndrew Thompson 			/* start clear stall */
970ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
97102ac6454SAndrew Thompson 			goto tr_setup;
97202ac6454SAndrew Thompson 		}
97302ac6454SAndrew Thompson 		break;
97402ac6454SAndrew Thompson 	}
97502ac6454SAndrew Thompson }
97602ac6454SAndrew Thompson 
97702ac6454SAndrew Thompson static int
97802ac6454SAndrew Thompson cdce_handle_request(device_t dev,
97902ac6454SAndrew Thompson     const void *req, void **pptr, uint16_t *plen,
98029bd7d7eSAndrew Thompson     uint16_t offset, uint8_t *pstate)
98102ac6454SAndrew Thompson {
98202ac6454SAndrew Thompson 	return (ENXIO);			/* use builtin handler */
98302ac6454SAndrew Thompson }
9844076dd23SAndrew Thompson 
9854076dd23SAndrew Thompson #if CDCE_HAVE_NCM
9864076dd23SAndrew Thompson static uint8_t
9874076dd23SAndrew Thompson cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
9884076dd23SAndrew Thompson {
9894076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
9904076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
9914076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
9924076dd23SAndrew Thompson 	struct mbuf *m;
9934076dd23SAndrew Thompson 	uint32_t rem;
9944076dd23SAndrew Thompson 	uint32_t offset;
9954076dd23SAndrew Thompson 	uint32_t last_offset;
9964076dd23SAndrew Thompson 	uint32_t n;
9974076dd23SAndrew Thompson 
9984076dd23SAndrew Thompson 	usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
9994076dd23SAndrew Thompson 
10004076dd23SAndrew Thompson 	offset = sizeof(sc->sc_ncm.hdr) +
10014076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
10024076dd23SAndrew Thompson 
10034076dd23SAndrew Thompson 	/* Store last valid offset before alignment */
10044076dd23SAndrew Thompson 	last_offset = offset;
10054076dd23SAndrew Thompson 
10064076dd23SAndrew Thompson 	/* Align offset correctly */
10074076dd23SAndrew Thompson 	offset = sc->sc_ncm.tx_remainder -
10084076dd23SAndrew Thompson 	    ((0UL - offset) & (0UL - sc->sc_ncm.tx_modulus));
10094076dd23SAndrew Thompson 
10104076dd23SAndrew Thompson 	for (n = 0; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
10114076dd23SAndrew Thompson 
10124076dd23SAndrew Thompson 		/* check if end of transmit buffer is reached */
10134076dd23SAndrew Thompson 
10144076dd23SAndrew Thompson 		if (offset >= sc->sc_ncm.tx_max)
10154076dd23SAndrew Thompson 			break;
10164076dd23SAndrew Thompson 
10174076dd23SAndrew Thompson 		/* compute maximum buffer size */
10184076dd23SAndrew Thompson 
10194076dd23SAndrew Thompson 		rem = sc->sc_ncm.tx_max - offset;
10204076dd23SAndrew Thompson 
10214076dd23SAndrew Thompson 		IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
10224076dd23SAndrew Thompson 
10234076dd23SAndrew Thompson 		if (m == NULL)
10244076dd23SAndrew Thompson 			break;
10254076dd23SAndrew Thompson 
10264076dd23SAndrew Thompson 		if (m->m_pkthdr.len > rem) {
10274076dd23SAndrew Thompson 			if (n == 0) {
10284076dd23SAndrew Thompson 				/* The frame won't fit in our buffer */
10294076dd23SAndrew Thompson 				DPRINTFN(1, "Frame too big to be transmitted!\n");
10304076dd23SAndrew Thompson 				m_freem(m);
10314076dd23SAndrew Thompson 				ifp->if_oerrors++;
10324076dd23SAndrew Thompson 				n--;
10334076dd23SAndrew Thompson 				continue;
10344076dd23SAndrew Thompson 			}
10354076dd23SAndrew Thompson 			/* Wait till next buffer becomes ready */
10364076dd23SAndrew Thompson 			IFQ_DRV_PREPEND(&(ifp->if_snd), m);
10374076dd23SAndrew Thompson 			break;
10384076dd23SAndrew Thompson 		}
10394076dd23SAndrew Thompson 		usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
10404076dd23SAndrew Thompson 
10414076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
10424076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
10434076dd23SAndrew Thompson 
10444076dd23SAndrew Thompson 		/* Update offset */
10454076dd23SAndrew Thompson 		offset += m->m_pkthdr.len;
10464076dd23SAndrew Thompson 
10474076dd23SAndrew Thompson 		/* Store last valid offset before alignment */
10484076dd23SAndrew Thompson 		last_offset = offset;
10494076dd23SAndrew Thompson 
10504076dd23SAndrew Thompson 		/* Align offset correctly */
10514076dd23SAndrew Thompson 		offset = sc->sc_ncm.tx_remainder -
10524076dd23SAndrew Thompson 		    ((0UL - offset) & (0UL - sc->sc_ncm.tx_modulus));
10534076dd23SAndrew Thompson 
10544076dd23SAndrew Thompson 		/*
10554076dd23SAndrew Thompson 		 * If there's a BPF listener, bounce a copy
10564076dd23SAndrew Thompson 		 * of this frame to him:
10574076dd23SAndrew Thompson 		 */
10584076dd23SAndrew Thompson 		BPF_MTAP(ifp, m);
10594076dd23SAndrew Thompson 
10604076dd23SAndrew Thompson 		/* Free mbuf */
10614076dd23SAndrew Thompson 
10624076dd23SAndrew Thompson 		m_freem(m);
10634076dd23SAndrew Thompson 
10644076dd23SAndrew Thompson 		/* Pre-increment interface counter */
10654076dd23SAndrew Thompson 
10664076dd23SAndrew Thompson 		ifp->if_opackets++;
10674076dd23SAndrew Thompson 	}
10684076dd23SAndrew Thompson 
10694076dd23SAndrew Thompson 	if (n == 0)
10704076dd23SAndrew Thompson 		return (1);
10714076dd23SAndrew Thompson 
10724076dd23SAndrew Thompson 	rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
10734076dd23SAndrew Thompson 
10744076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wLength, rem);
10754076dd23SAndrew Thompson 
10764076dd23SAndrew Thompson 	/* zero the rest of the data pointer entries */
10774076dd23SAndrew Thompson 	for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
10784076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
10794076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
10804076dd23SAndrew Thompson 	}
10814076dd23SAndrew Thompson 
10824076dd23SAndrew Thompson 	/* set frame length */
10834076dd23SAndrew Thompson 	usbd_xfer_set_frame_len(xfer, index, last_offset);
10844076dd23SAndrew Thompson 
10854076dd23SAndrew Thompson 	/* Fill out 16-bit header */
10864076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[0] = 'N';
10874076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[1] = 'C';
10884076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[2] = 'M';
10894076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[3] = 'H';
10904076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
109129051223SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wBlockLength, last_offset);
10924076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
10934076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
10944076dd23SAndrew Thompson 
10954076dd23SAndrew Thompson 	sc->sc_ncm.tx_seq++;
10964076dd23SAndrew Thompson 
10974076dd23SAndrew Thompson 	/* Fill out 16-bit frame table header */
10984076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[0] = 'N';
10994076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[1] = 'C';
11004076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[2] = 'M';
11014076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[3] = 'x';
11024076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0);		/* reserved */
11034076dd23SAndrew Thompson 
11044076dd23SAndrew Thompson 	usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
11054076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
11064076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt));
11074076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
11084076dd23SAndrew Thompson 	    &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
11094076dd23SAndrew Thompson 	return (0);
11104076dd23SAndrew Thompson }
11114076dd23SAndrew Thompson 
11124076dd23SAndrew Thompson static void
11134076dd23SAndrew Thompson cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
11144076dd23SAndrew Thompson {
11154076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
11164076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
11174076dd23SAndrew Thompson 	uint16_t x;
11184076dd23SAndrew Thompson 	int actlen;
11194076dd23SAndrew Thompson 	int aframes;
11204076dd23SAndrew Thompson 
11214076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
11224076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
11234076dd23SAndrew Thompson 
11244076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
11254076dd23SAndrew Thompson 
11264076dd23SAndrew Thompson 		DPRINTFN(10, "transfer complete: "
11274076dd23SAndrew Thompson 		    "%u bytes in %u frames\n", actlen, aframes);
11284076dd23SAndrew Thompson 
11294076dd23SAndrew Thompson 	case USB_ST_SETUP:
11304076dd23SAndrew Thompson 		for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
11314076dd23SAndrew Thompson 			if (cdce_ncm_fill_tx_frames(xfer, x))
11324076dd23SAndrew Thompson 				break;
11334076dd23SAndrew Thompson 		}
11344076dd23SAndrew Thompson 
11354076dd23SAndrew Thompson 		if (x != 0) {
11364076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
11374076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
11384076dd23SAndrew Thompson 		}
11394076dd23SAndrew Thompson 		break;
11404076dd23SAndrew Thompson 
11414076dd23SAndrew Thompson 	default:			/* Error */
11424076dd23SAndrew Thompson 		DPRINTFN(10, "Transfer error: %s\n",
11434076dd23SAndrew Thompson 		    usbd_errstr(error));
11444076dd23SAndrew Thompson 
11454076dd23SAndrew Thompson 		/* update error counter */
11464076dd23SAndrew Thompson 		ifp->if_oerrors += 1;
11474076dd23SAndrew Thompson 
11484076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
11494076dd23SAndrew Thompson 			/* try to clear stall first */
11504076dd23SAndrew Thompson 			usbd_xfer_set_stall(xfer);
11514076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
11524076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
11534076dd23SAndrew Thompson 		}
11544076dd23SAndrew Thompson 		break;
11554076dd23SAndrew Thompson 	}
11564076dd23SAndrew Thompson }
11574076dd23SAndrew Thompson 
11584076dd23SAndrew Thompson static void
11594076dd23SAndrew Thompson cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
11604076dd23SAndrew Thompson {
11614076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
11624076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
11634076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
11644076dd23SAndrew Thompson 	struct mbuf *m;
11654076dd23SAndrew Thompson 	int sumdata;
11664076dd23SAndrew Thompson 	int sumlen;
11674076dd23SAndrew Thompson 	int actlen;
11684076dd23SAndrew Thompson 	int aframes;
11694076dd23SAndrew Thompson 	int temp;
11704076dd23SAndrew Thompson 	int nframes;
11714076dd23SAndrew Thompson 	int x;
11724076dd23SAndrew Thompson 	int offset;
11734076dd23SAndrew Thompson 
11744076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
11754076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
11764076dd23SAndrew Thompson 
11774076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
11784076dd23SAndrew Thompson 
11794076dd23SAndrew Thompson 		DPRINTFN(1, "received %u bytes in %u frames\n",
11804076dd23SAndrew Thompson 		    actlen, aframes);
11814076dd23SAndrew Thompson 
11824076dd23SAndrew Thompson 		if (actlen < (sizeof(sc->sc_ncm.hdr) +
11834076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt))) {
11844076dd23SAndrew Thompson 			DPRINTFN(1, "frame too short\n");
11854076dd23SAndrew Thompson 			goto tr_stall;
11864076dd23SAndrew Thompson 		}
11874076dd23SAndrew Thompson 		usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
11884076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.hdr));
11894076dd23SAndrew Thompson 
11904076dd23SAndrew Thompson 		if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
11914076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
11924076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
11934076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
11944076dd23SAndrew Thompson 			DPRINTFN(1, "invalid HDR signature\n");
11954076dd23SAndrew Thompson 			goto tr_stall;
11964076dd23SAndrew Thompson 		}
11974076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
11984076dd23SAndrew Thompson 		if (temp > sumlen) {
11994076dd23SAndrew Thompson 			DPRINTFN(1, "unsupported block length %u/%u\n",
12004076dd23SAndrew Thompson 			    temp, sumlen);
12014076dd23SAndrew Thompson 			goto tr_stall;
12024076dd23SAndrew Thompson 		}
12034076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
12044076dd23SAndrew Thompson 		if ((temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
12054076dd23SAndrew Thompson 			DPRINTFN(1, "invalid DPT index\n");
12064076dd23SAndrew Thompson 			goto tr_stall;
12074076dd23SAndrew Thompson 		}
12084076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
12094076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt));
12104076dd23SAndrew Thompson 
12114076dd23SAndrew Thompson 		if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
12124076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
12134076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
12144076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[3] != 'x')) {
12154076dd23SAndrew Thompson 			DPRINTFN(1, "invalid DPT signature\n");
12164076dd23SAndrew Thompson 			goto tr_stall;
12174076dd23SAndrew Thompson 		}
12184076dd23SAndrew Thompson 		nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
12194076dd23SAndrew Thompson 
12204076dd23SAndrew Thompson 		/* Subtract size of header and last zero padded entry */
12214076dd23SAndrew Thompson 		if (nframes >= (2 + 1))
12224076dd23SAndrew Thompson 			nframes -= (2 + 1);
12234076dd23SAndrew Thompson 		else
12244076dd23SAndrew Thompson 			nframes = 0;
12254076dd23SAndrew Thompson 
12264076dd23SAndrew Thompson 		DPRINTFN(1, "nframes = %u\n", nframes);
12274076dd23SAndrew Thompson 
12284076dd23SAndrew Thompson 		temp += sizeof(sc->sc_ncm.dpt);
12294076dd23SAndrew Thompson 
12304076dd23SAndrew Thompson 		if ((temp + (4 * nframes)) > actlen)
12314076dd23SAndrew Thompson 			goto tr_stall;
12324076dd23SAndrew Thompson 
12334076dd23SAndrew Thompson 		if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
12344076dd23SAndrew Thompson 			DPRINTFN(1, "Truncating number of frames from %u to %u\n",
12354076dd23SAndrew Thompson 			    nframes, CDCE_NCM_SUBFRAMES_MAX);
12364076dd23SAndrew Thompson 			nframes = CDCE_NCM_SUBFRAMES_MAX;
12374076dd23SAndrew Thompson 		}
12384076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
12394076dd23SAndrew Thompson 
12404076dd23SAndrew Thompson 		sumdata = 0;
12414076dd23SAndrew Thompson 
12424076dd23SAndrew Thompson 		for (x = 0; x != nframes; x++) {
12434076dd23SAndrew Thompson 
12444076dd23SAndrew Thompson 			offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
12454076dd23SAndrew Thompson 			temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
12464076dd23SAndrew Thompson 
124729051223SAndrew Thompson 			if ((offset == 0) ||
124829051223SAndrew Thompson 			    (temp < sizeof(struct ether_header)) ||
124929051223SAndrew Thompson 			    (temp > (MCLBYTES - ETHER_ALIGN))) {
125029051223SAndrew Thompson 				DPRINTFN(1, "NULL frame detected at %d\n", x);
12514076dd23SAndrew Thompson 				m = NULL;
125229051223SAndrew Thompson 				/* silently ignore this frame */
12534076dd23SAndrew Thompson 				continue;
125429051223SAndrew Thompson 			} else if ((offset + temp) > actlen) {
125529051223SAndrew Thompson 				DPRINTFN(1, "invalid frame "
125629051223SAndrew Thompson 				    "detected at %d\n", x);
125729051223SAndrew Thompson 				m = NULL;
125829051223SAndrew Thompson 				/* silently ignore this frame */
125929051223SAndrew Thompson 				continue;
126029051223SAndrew Thompson 			} else if (temp > (MHLEN - ETHER_ALIGN)) {
12614076dd23SAndrew Thompson 				m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
12624076dd23SAndrew Thompson 			} else {
12634076dd23SAndrew Thompson 				m = m_gethdr(M_DONTWAIT, MT_DATA);
12644076dd23SAndrew Thompson 			}
12654076dd23SAndrew Thompson 
12664076dd23SAndrew Thompson 			DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
12674076dd23SAndrew Thompson 			    x, offset, temp);
12684076dd23SAndrew Thompson 
12694076dd23SAndrew Thompson 			/* check if we have a buffer */
12704076dd23SAndrew Thompson 			if (m) {
12714076dd23SAndrew Thompson 				m_adj(m, ETHER_ALIGN);
12724076dd23SAndrew Thompson 
12734076dd23SAndrew Thompson 				usbd_copy_out(pc, offset, m->m_data, temp);
12744076dd23SAndrew Thompson 
12754076dd23SAndrew Thompson 				/* enqueue */
12764076dd23SAndrew Thompson 				uether_rxmbuf(&sc->sc_ue, m, temp);
12774076dd23SAndrew Thompson 
12784076dd23SAndrew Thompson 				sumdata += temp;
12794076dd23SAndrew Thompson 			} else {
12804076dd23SAndrew Thompson 				ifp->if_ierrors++;
12814076dd23SAndrew Thompson 			}
12824076dd23SAndrew Thompson 		}
12834076dd23SAndrew Thompson 
12844076dd23SAndrew Thompson 		DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
12854076dd23SAndrew Thompson 
12864076dd23SAndrew Thompson 	case USB_ST_SETUP:
12874076dd23SAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
12884076dd23SAndrew Thompson 		usbd_xfer_set_frames(xfer, 1);
12894076dd23SAndrew Thompson 		usbd_transfer_submit(xfer);
12904076dd23SAndrew Thompson 		uether_rxflush(&sc->sc_ue);	/* must be last */
12914076dd23SAndrew Thompson 		break;
12924076dd23SAndrew Thompson 
12934076dd23SAndrew Thompson 	default:			/* Error */
12944076dd23SAndrew Thompson 		DPRINTFN(1, "error = %s\n",
12954076dd23SAndrew Thompson 		    usbd_errstr(error));
12964076dd23SAndrew Thompson 
12974076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
12984076dd23SAndrew Thompson tr_stall:
12994076dd23SAndrew Thompson 			/* try to clear stall first */
13004076dd23SAndrew Thompson 			usbd_xfer_set_stall(xfer);
13014076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
13024076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
13034076dd23SAndrew Thompson 		}
13044076dd23SAndrew Thompson 		break;
13054076dd23SAndrew Thompson 	}
13064076dd23SAndrew Thompson }
13074076dd23SAndrew Thompson #endif
1308