xref: /freebsd/sys/dev/usb/net/if_cdce.c (revision 760bc48e7ee4471fe04fa5fee89d00bf7d698ddb)
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 
4302ac6454SAndrew Thompson #include <sys/cdefs.h>
4402ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson #include "usbdevs.h"
4702ac6454SAndrew Thompson #include <dev/usb/usb.h>
4802ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
4902ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
5002ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
5102ac6454SAndrew Thompson 
5202ac6454SAndrew Thompson #define	USB_DEBUG_VAR cdce_debug
5302ac6454SAndrew Thompson 
5402ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5502ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
5602ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5802ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6002ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
6102ac6454SAndrew Thompson #include <dev/usb/usb_parse.h>
6202ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
6302ac6454SAndrew Thompson 
6402ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h>
6502ac6454SAndrew Thompson #include <dev/usb/net/if_cdcereg.h>
6602ac6454SAndrew Thompson 
6702ac6454SAndrew Thompson static device_probe_t cdce_probe;
6802ac6454SAndrew Thompson static device_attach_t cdce_attach;
6902ac6454SAndrew Thompson static device_detach_t cdce_detach;
7002ac6454SAndrew Thompson static device_suspend_t cdce_suspend;
7102ac6454SAndrew Thompson static device_resume_t cdce_resume;
7202ac6454SAndrew Thompson static usb_handle_request_t cdce_handle_request;
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson static usb2_callback_t cdce_bulk_write_callback;
7502ac6454SAndrew Thompson static usb2_callback_t cdce_bulk_read_callback;
7602ac6454SAndrew Thompson static usb2_callback_t cdce_intr_read_callback;
7702ac6454SAndrew Thompson static usb2_callback_t cdce_intr_write_callback;
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson static usb2_ether_fn_t cdce_attach_post;
8002ac6454SAndrew Thompson static usb2_ether_fn_t cdce_init;
8102ac6454SAndrew Thompson static usb2_ether_fn_t cdce_stop;
8202ac6454SAndrew Thompson static usb2_ether_fn_t cdce_start;
8302ac6454SAndrew Thompson static usb2_ether_fn_t cdce_setmulti;
8402ac6454SAndrew Thompson static usb2_ether_fn_t cdce_setpromisc;
8502ac6454SAndrew Thompson 
8602ac6454SAndrew Thompson static uint32_t	cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson #if USB_DEBUG
8902ac6454SAndrew Thompson static int cdce_debug = 0;
9002ac6454SAndrew Thompson 
919360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
929360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0,
9302ac6454SAndrew Thompson     "Debug level");
9402ac6454SAndrew Thompson #endif
9502ac6454SAndrew Thompson 
96760bc48eSAndrew Thompson static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
9702ac6454SAndrew Thompson 
984eae601eSAndrew Thompson 	[CDCE_BULK_RX] = {
9902ac6454SAndrew Thompson 		.type = UE_BULK,
10002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1014eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
10202ac6454SAndrew Thompson 		.if_index = 0,
1034eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1044eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1054eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
1064eae601eSAndrew Thompson 		.callback = cdce_bulk_read_callback,
1074eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
108f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
10902ac6454SAndrew Thompson 	},
11002ac6454SAndrew Thompson 
1114eae601eSAndrew Thompson 	[CDCE_BULK_TX] = {
11202ac6454SAndrew Thompson 		.type = UE_BULK,
11302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1144eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
11502ac6454SAndrew Thompson 		.if_index = 0,
1164eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1174eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1184eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
1194eae601eSAndrew Thompson 		.callback = cdce_bulk_write_callback,
1204eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
121f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
12202ac6454SAndrew Thompson 	},
12302ac6454SAndrew Thompson 
1244eae601eSAndrew Thompson 	[CDCE_INTR_RX] = {
12502ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
12602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1274eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
12802ac6454SAndrew Thompson 		.if_index = 1,
1294eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1304eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
1314eae601eSAndrew Thompson 		.callback = cdce_intr_read_callback,
1324eae601eSAndrew Thompson 		.timeout = 0,
1334eae601eSAndrew Thompson 		.usb_mode = USB_MODE_HOST,
1344eae601eSAndrew Thompson 	},
1354eae601eSAndrew Thompson 
1364eae601eSAndrew Thompson 	[CDCE_INTR_TX] = {
1374eae601eSAndrew Thompson 		.type = UE_INTERRUPT,
1384eae601eSAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1394eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
1404eae601eSAndrew Thompson 		.if_index = 1,
1414eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1424eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
1434eae601eSAndrew Thompson 		.callback = cdce_intr_write_callback,
1444eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
1454eae601eSAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
14602ac6454SAndrew Thompson 	},
14702ac6454SAndrew Thompson };
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson static device_method_t cdce_methods[] = {
15002ac6454SAndrew Thompson 	/* USB interface */
15102ac6454SAndrew Thompson 	DEVMETHOD(usb_handle_request, cdce_handle_request),
15202ac6454SAndrew Thompson 
15302ac6454SAndrew Thompson 	/* Device interface */
15402ac6454SAndrew Thompson 	DEVMETHOD(device_probe, cdce_probe),
15502ac6454SAndrew Thompson 	DEVMETHOD(device_attach, cdce_attach),
15602ac6454SAndrew Thompson 	DEVMETHOD(device_detach, cdce_detach),
15702ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, cdce_suspend),
15802ac6454SAndrew Thompson 	DEVMETHOD(device_resume, cdce_resume),
15902ac6454SAndrew Thompson 
16002ac6454SAndrew Thompson 	{0, 0}
16102ac6454SAndrew Thompson };
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson static driver_t cdce_driver = {
16402ac6454SAndrew Thompson 	.name = "cdce",
16502ac6454SAndrew Thompson 	.methods = cdce_methods,
16602ac6454SAndrew Thompson 	.size = sizeof(struct cdce_softc),
16702ac6454SAndrew Thompson };
16802ac6454SAndrew Thompson 
16902ac6454SAndrew Thompson static devclass_t cdce_devclass;
17002ac6454SAndrew Thompson 
1719aef556dSAndrew Thompson DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0);
17202ac6454SAndrew Thompson MODULE_VERSION(cdce, 1);
17302ac6454SAndrew Thompson MODULE_DEPEND(cdce, uether, 1, 1, 1);
17402ac6454SAndrew Thompson MODULE_DEPEND(cdce, usb, 1, 1, 1);
17502ac6454SAndrew Thompson MODULE_DEPEND(cdce, ether, 1, 1, 1);
17602ac6454SAndrew Thompson 
177760bc48eSAndrew Thompson static const struct usb_ether_methods cdce_ue_methods = {
17802ac6454SAndrew Thompson 	.ue_attach_post = cdce_attach_post,
17902ac6454SAndrew Thompson 	.ue_start = cdce_start,
18002ac6454SAndrew Thompson 	.ue_init = cdce_init,
18102ac6454SAndrew Thompson 	.ue_stop = cdce_stop,
18202ac6454SAndrew Thompson 	.ue_setmulti = cdce_setmulti,
18302ac6454SAndrew Thompson 	.ue_setpromisc = cdce_setpromisc,
18402ac6454SAndrew Thompson };
18502ac6454SAndrew Thompson 
186760bc48eSAndrew Thompson static const struct usb_device_id cdce_devs[] = {
18702ac6454SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
18802ac6454SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
18902ac6454SAndrew Thompson 
19002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
19102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
19202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
19302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
19402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
19502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
19602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
19702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
19802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
19902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
20002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
20102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
20202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
20302ac6454SAndrew Thompson };
20402ac6454SAndrew Thompson 
20502ac6454SAndrew Thompson static int
20602ac6454SAndrew Thompson cdce_probe(device_t dev)
20702ac6454SAndrew Thompson {
208760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
20902ac6454SAndrew Thompson 
21002ac6454SAndrew Thompson 	return (usb2_lookup_id_by_uaa(cdce_devs, sizeof(cdce_devs), uaa));
21102ac6454SAndrew Thompson }
21202ac6454SAndrew Thompson 
21302ac6454SAndrew Thompson static void
214760bc48eSAndrew Thompson cdce_attach_post(struct usb_ether *ue)
21502ac6454SAndrew Thompson {
21602ac6454SAndrew Thompson 	/* no-op */
21702ac6454SAndrew Thompson 	return;
21802ac6454SAndrew Thompson }
21902ac6454SAndrew Thompson 
22002ac6454SAndrew Thompson static int
22102ac6454SAndrew Thompson cdce_attach(device_t dev)
22202ac6454SAndrew Thompson {
22302ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
224760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
225760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
226760bc48eSAndrew Thompson 	struct usb_interface *iface;
227760bc48eSAndrew Thompson 	const struct usb_cdc_union_descriptor *ud;
228760bc48eSAndrew Thompson 	const struct usb_interface_descriptor *id;
229760bc48eSAndrew Thompson 	const struct usb_cdc_ethernet_descriptor *ued;
23002ac6454SAndrew Thompson 	int error;
23102ac6454SAndrew Thompson 	uint8_t i;
23202ac6454SAndrew Thompson 	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */
23302ac6454SAndrew Thompson 
23402ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
23502ac6454SAndrew Thompson 
23602ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
23702ac6454SAndrew Thompson 
23802ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 	if (sc->sc_flags & CDCE_FLAG_NO_UNION) {
24102ac6454SAndrew Thompson 		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
24202ac6454SAndrew Thompson 		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
24302ac6454SAndrew Thompson 		sc->sc_data_iface_no = 0;	/* not used */
24402ac6454SAndrew Thompson 		goto alloc_transfers;
24502ac6454SAndrew Thompson 	}
24602ac6454SAndrew Thompson 	ud = usb2_find_descriptor
24702ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
24802ac6454SAndrew Thompson 	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson 	if ((ud == NULL) || (ud->bLength < sizeof(*ud))) {
25102ac6454SAndrew Thompson 		device_printf(dev, "no union descriptor!\n");
25202ac6454SAndrew Thompson 		goto detach;
25302ac6454SAndrew Thompson 	}
25402ac6454SAndrew Thompson 	sc->sc_data_iface_no = ud->bSlaveInterface[0];
25502ac6454SAndrew Thompson 
25602ac6454SAndrew Thompson 	for (i = 0;; i++) {
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson 		iface = usb2_get_iface(uaa->device, i);
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson 		if (iface) {
26102ac6454SAndrew Thompson 
26202ac6454SAndrew Thompson 			id = usb2_get_interface_descriptor(iface);
26302ac6454SAndrew Thompson 
26402ac6454SAndrew Thompson 			if (id && (id->bInterfaceNumber ==
26502ac6454SAndrew Thompson 			    sc->sc_data_iface_no)) {
26602ac6454SAndrew Thompson 				sc->sc_ifaces_index[0] = i;
26702ac6454SAndrew Thompson 				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
26802ac6454SAndrew Thompson 				usb2_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
26902ac6454SAndrew Thompson 				break;
27002ac6454SAndrew Thompson 			}
27102ac6454SAndrew Thompson 		} else {
27202ac6454SAndrew Thompson 			device_printf(dev, "no data interface found!\n");
27302ac6454SAndrew Thompson 			goto detach;
27402ac6454SAndrew Thompson 		}
27502ac6454SAndrew Thompson 	}
27602ac6454SAndrew Thompson 
27702ac6454SAndrew Thompson 	/*
27802ac6454SAndrew Thompson 	 * <quote>
27902ac6454SAndrew Thompson 	 *
28002ac6454SAndrew Thompson 	 *  The Data Class interface of a networking device shall have
28102ac6454SAndrew Thompson 	 *  a minimum of two interface settings. The first setting
28202ac6454SAndrew Thompson 	 *  (the default interface setting) includes no endpoints and
28302ac6454SAndrew Thompson 	 *  therefore no networking traffic is exchanged whenever the
28402ac6454SAndrew Thompson 	 *  default interface setting is selected. One or more
28502ac6454SAndrew Thompson 	 *  additional interface settings are used for normal
28602ac6454SAndrew Thompson 	 *  operation, and therefore each includes a pair of endpoints
28702ac6454SAndrew Thompson 	 *  (one IN, and one OUT) to exchange network traffic. Select
28802ac6454SAndrew Thompson 	 *  an alternate interface setting to initialize the network
28902ac6454SAndrew Thompson 	 *  aspects of the device and to enable the exchange of
29002ac6454SAndrew Thompson 	 *  network traffic.
29102ac6454SAndrew Thompson 	 *
29202ac6454SAndrew Thompson 	 * </quote>
29302ac6454SAndrew Thompson 	 *
29402ac6454SAndrew Thompson 	 * Some devices, most notably cable modems, include interface
29502ac6454SAndrew Thompson 	 * settings that have no IN or OUT endpoint, therefore loop
29602ac6454SAndrew Thompson 	 * through the list of all available interface settings
29702ac6454SAndrew Thompson 	 * looking for one with both IN and OUT endpoints.
29802ac6454SAndrew Thompson 	 */
29902ac6454SAndrew Thompson 
30002ac6454SAndrew Thompson alloc_transfers:
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson 	for (i = 0; i != 32; i++) {
30302ac6454SAndrew Thompson 
30402ac6454SAndrew Thompson 		error = usb2_set_alt_interface_index
30502ac6454SAndrew Thompson 		    (uaa->device, sc->sc_ifaces_index[0], i);
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson 		if (error) {
30802ac6454SAndrew Thompson 			device_printf(dev, "no valid alternate "
30902ac6454SAndrew Thompson 			    "setting found!\n");
31002ac6454SAndrew Thompson 			goto detach;
31102ac6454SAndrew Thompson 		}
31202ac6454SAndrew Thompson 		error = usb2_transfer_setup
31302ac6454SAndrew Thompson 		    (uaa->device, sc->sc_ifaces_index,
31402ac6454SAndrew Thompson 		    sc->sc_xfer, cdce_config, CDCE_N_TRANSFER,
31502ac6454SAndrew Thompson 		    sc, &sc->sc_mtx);
31602ac6454SAndrew Thompson 
31702ac6454SAndrew Thompson 		if (error == 0) {
31802ac6454SAndrew Thompson 			break;
31902ac6454SAndrew Thompson 		}
32002ac6454SAndrew Thompson 	}
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson 	ued = usb2_find_descriptor
32302ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
32402ac6454SAndrew Thompson 	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1);
32502ac6454SAndrew Thompson 
32602ac6454SAndrew Thompson 	if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
32702ac6454SAndrew Thompson 		error = USB_ERR_INVAL;
32802ac6454SAndrew Thompson 	} else {
32902ac6454SAndrew Thompson 		error = usb2_req_get_string_any(uaa->device, NULL,
33002ac6454SAndrew Thompson 		    eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
33102ac6454SAndrew Thompson 	}
33202ac6454SAndrew Thompson 
33302ac6454SAndrew Thompson 	if (error) {
33402ac6454SAndrew Thompson 
33502ac6454SAndrew Thompson 		/* fake MAC address */
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson 		device_printf(dev, "faking MAC address\n");
33802ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[0] = 0x2a;
33902ac6454SAndrew Thompson 		memcpy(&sc->sc_ue.ue_eaddr[1], &ticks, sizeof(uint32_t));
34002ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
34102ac6454SAndrew Thompson 
34202ac6454SAndrew Thompson 	} else {
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 		bzero(sc->sc_ue.ue_eaddr, sizeof(sc->sc_ue.ue_eaddr));
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 		for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
34702ac6454SAndrew Thompson 
34802ac6454SAndrew Thompson 			char c = eaddr_str[i];
34902ac6454SAndrew Thompson 
35002ac6454SAndrew Thompson 			if ('0' <= c && c <= '9')
35102ac6454SAndrew Thompson 				c -= '0';
35202ac6454SAndrew Thompson 			else if (c != 0)
35302ac6454SAndrew Thompson 				c -= 'A' - 10;
35402ac6454SAndrew Thompson 			else
35502ac6454SAndrew Thompson 				break;
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 			c &= 0xf;
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson 			if ((i & 1) == 0)
36002ac6454SAndrew Thompson 				c <<= 4;
36102ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[i / 2] |= c;
36202ac6454SAndrew Thompson 		}
36302ac6454SAndrew Thompson 
364f29a0724SAndrew Thompson 		if (uaa->usb_mode == USB_MODE_DEVICE) {
36502ac6454SAndrew Thompson 			/*
36602ac6454SAndrew Thompson 			 * Do not use the same MAC address like the peer !
36702ac6454SAndrew Thompson 			 */
36802ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[5] ^= 0xFF;
36902ac6454SAndrew Thompson 		}
37002ac6454SAndrew Thompson 	}
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson 	ue->ue_sc = sc;
37302ac6454SAndrew Thompson 	ue->ue_dev = dev;
37402ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
37502ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
37602ac6454SAndrew Thompson 	ue->ue_methods = &cdce_ue_methods;
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson 	error = usb2_ether_ifattach(ue);
37902ac6454SAndrew Thompson 	if (error) {
38002ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
38102ac6454SAndrew Thompson 		goto detach;
38202ac6454SAndrew Thompson 	}
38302ac6454SAndrew Thompson 	return (0);			/* success */
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson detach:
38602ac6454SAndrew Thompson 	cdce_detach(dev);
38702ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
38802ac6454SAndrew Thompson }
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson static int
39102ac6454SAndrew Thompson cdce_detach(device_t dev)
39202ac6454SAndrew Thompson {
39302ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
394760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 	/* stop all USB transfers first */
39702ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
39802ac6454SAndrew Thompson 	usb2_ether_ifdetach(ue);
39902ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson 	return (0);
40202ac6454SAndrew Thompson }
40302ac6454SAndrew Thompson 
40402ac6454SAndrew Thompson static void
405760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue)
40602ac6454SAndrew Thompson {
40702ac6454SAndrew Thompson 	struct cdce_softc *sc = usb2_ether_getsc(ue);
40802ac6454SAndrew Thompson 
40902ac6454SAndrew Thompson 	/*
41002ac6454SAndrew Thompson 	 * Start the USB transfers, if not already started:
41102ac6454SAndrew Thompson 	 */
4124eae601eSAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
4134eae601eSAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
41402ac6454SAndrew Thompson }
41502ac6454SAndrew Thompson 
41602ac6454SAndrew Thompson static void
41702ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n)
41802ac6454SAndrew Thompson {
41902ac6454SAndrew Thompson 	uint8_t x;
42002ac6454SAndrew Thompson 	for (x = 0; x != n; x++) {
42102ac6454SAndrew Thompson 		if (ppm[x] != NULL) {
42202ac6454SAndrew Thompson 			m_freem(ppm[x]);
42302ac6454SAndrew Thompson 			ppm[x] = NULL;
42402ac6454SAndrew Thompson 		}
42502ac6454SAndrew Thompson 	}
42602ac6454SAndrew Thompson }
42702ac6454SAndrew Thompson 
42802ac6454SAndrew Thompson static void
429760bc48eSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer)
43002ac6454SAndrew Thompson {
43102ac6454SAndrew Thompson 	struct cdce_softc *sc = xfer->priv_sc;
43202ac6454SAndrew Thompson 	struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue);
43302ac6454SAndrew Thompson 	struct mbuf *m;
43402ac6454SAndrew Thompson 	struct mbuf *mt;
43502ac6454SAndrew Thompson 	uint32_t crc;
43602ac6454SAndrew Thompson 	uint8_t x;
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson 	DPRINTFN(1, "\n");
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
44102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
44202ac6454SAndrew Thompson 		DPRINTFN(11, "transfer complete: "
44302ac6454SAndrew Thompson 		    "%u bytes in %u frames\n", xfer->actlen,
44402ac6454SAndrew Thompson 		    xfer->aframes);
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson 		ifp->if_opackets++;
44702ac6454SAndrew Thompson 
44802ac6454SAndrew Thompson 		/* free all previous TX buffers */
44902ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 		/* FALLTHROUGH */
45202ac6454SAndrew Thompson 	case USB_ST_SETUP:
45302ac6454SAndrew Thompson tr_setup:
45402ac6454SAndrew Thompson 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
45702ac6454SAndrew Thompson 
45802ac6454SAndrew Thompson 			if (m == NULL)
45902ac6454SAndrew Thompson 				break;
46002ac6454SAndrew Thompson 
46102ac6454SAndrew Thompson 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
46202ac6454SAndrew Thompson 				/*
46302ac6454SAndrew Thompson 				 * Zaurus wants a 32-bit CRC appended
46402ac6454SAndrew Thompson 				 * to every frame
46502ac6454SAndrew Thompson 				 */
46602ac6454SAndrew Thompson 
46702ac6454SAndrew Thompson 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
46802ac6454SAndrew Thompson 				crc = htole32(crc);
46902ac6454SAndrew Thompson 
47002ac6454SAndrew Thompson 				if (!m_append(m, 4, (void *)&crc)) {
47102ac6454SAndrew Thompson 					m_freem(m);
47202ac6454SAndrew Thompson 					ifp->if_oerrors++;
47302ac6454SAndrew Thompson 					continue;
47402ac6454SAndrew Thompson 				}
47502ac6454SAndrew Thompson 			}
47602ac6454SAndrew Thompson 			if (m->m_len != m->m_pkthdr.len) {
47702ac6454SAndrew Thompson 				mt = m_defrag(m, M_DONTWAIT);
47802ac6454SAndrew Thompson 				if (mt == NULL) {
47902ac6454SAndrew Thompson 					m_freem(m);
48002ac6454SAndrew Thompson 					ifp->if_oerrors++;
48102ac6454SAndrew Thompson 					continue;
48202ac6454SAndrew Thompson 				}
48302ac6454SAndrew Thompson 				m = mt;
48402ac6454SAndrew Thompson 			}
48502ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
48602ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
48702ac6454SAndrew Thompson 			}
48802ac6454SAndrew Thompson 			sc->sc_tx_buf[x] = m;
48902ac6454SAndrew Thompson 			xfer->frlengths[x] = m->m_len;
49002ac6454SAndrew Thompson 			usb2_set_frame_data(xfer, m->m_data, x);
49102ac6454SAndrew Thompson 
49202ac6454SAndrew Thompson 			/*
49302ac6454SAndrew Thompson 			 * If there's a BPF listener, bounce a copy of
49402ac6454SAndrew Thompson 			 * this frame to him:
49502ac6454SAndrew Thompson 			 */
49602ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
49702ac6454SAndrew Thompson 		}
49802ac6454SAndrew Thompson 		if (x != 0) {
49902ac6454SAndrew Thompson 			xfer->nframes = x;
50002ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
50102ac6454SAndrew Thompson 		}
50202ac6454SAndrew Thompson 		break;
50302ac6454SAndrew Thompson 
50402ac6454SAndrew Thompson 	default:			/* Error */
50502ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
50602ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 		/* free all previous TX buffers */
50902ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson 		/* count output errors */
51202ac6454SAndrew Thompson 		ifp->if_oerrors++;
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
51502ac6454SAndrew Thompson 			/* try to clear stall first */
51602ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
51702ac6454SAndrew Thompson 			goto tr_setup;
51802ac6454SAndrew Thompson 		}
51902ac6454SAndrew Thompson 		break;
52002ac6454SAndrew Thompson 	}
52102ac6454SAndrew Thompson }
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson static int32_t
52402ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
52502ac6454SAndrew Thompson {
52602ac6454SAndrew Thompson 	uint32_t *p_crc = arg;
52702ac6454SAndrew Thompson 
52802ac6454SAndrew Thompson 	*p_crc = crc32_raw(src, count, *p_crc);
52902ac6454SAndrew Thompson 	return (0);
53002ac6454SAndrew Thompson }
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson static uint32_t
53302ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
53402ac6454SAndrew Thompson {
53502ac6454SAndrew Thompson 	uint32_t crc = 0xFFFFFFFF;
53602ac6454SAndrew Thompson 	int error;
53702ac6454SAndrew Thompson 
53802ac6454SAndrew Thompson 	error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
53902ac6454SAndrew Thompson 	return (crc ^ 0xFFFFFFFF);
54002ac6454SAndrew Thompson }
54102ac6454SAndrew Thompson 
54202ac6454SAndrew Thompson static void
543760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue)
54402ac6454SAndrew Thompson {
54502ac6454SAndrew Thompson 	struct cdce_softc *sc = usb2_ether_getsc(ue);
54602ac6454SAndrew Thompson 	struct ifnet *ifp = usb2_ether_getifp(ue);
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
54902ac6454SAndrew Thompson 
55002ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
55102ac6454SAndrew Thompson 
55202ac6454SAndrew Thompson 	/* start interrupt transfer */
5534eae601eSAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
5544eae601eSAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson 	/* stall data write direction, which depends on USB mode */
5574eae601eSAndrew Thompson 	usb2_transfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
55802ac6454SAndrew Thompson 
55902ac6454SAndrew Thompson 	/* start data transfers */
56002ac6454SAndrew Thompson 	cdce_start(ue);
56102ac6454SAndrew Thompson }
56202ac6454SAndrew Thompson 
56302ac6454SAndrew Thompson static void
564760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue)
56502ac6454SAndrew Thompson {
56602ac6454SAndrew Thompson 	struct cdce_softc *sc = usb2_ether_getsc(ue);
56702ac6454SAndrew Thompson 	struct ifnet *ifp = usb2_ether_getifp(ue);
56802ac6454SAndrew Thompson 
56902ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson 	/*
57402ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
57502ac6454SAndrew Thompson 	 */
5764eae601eSAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
5774eae601eSAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
5784eae601eSAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
5794eae601eSAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
58002ac6454SAndrew Thompson }
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson static void
583760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue)
58402ac6454SAndrew Thompson {
58502ac6454SAndrew Thompson 	/* no-op */
58602ac6454SAndrew Thompson 	return;
58702ac6454SAndrew Thompson }
58802ac6454SAndrew Thompson 
58902ac6454SAndrew Thompson static void
590760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue)
59102ac6454SAndrew Thompson {
59202ac6454SAndrew Thompson 	/* no-op */
59302ac6454SAndrew Thompson 	return;
59402ac6454SAndrew Thompson }
59502ac6454SAndrew Thompson 
59602ac6454SAndrew Thompson static int
59702ac6454SAndrew Thompson cdce_suspend(device_t dev)
59802ac6454SAndrew Thompson {
59902ac6454SAndrew Thompson 	device_printf(dev, "Suspending\n");
60002ac6454SAndrew Thompson 	return (0);
60102ac6454SAndrew Thompson }
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson static int
60402ac6454SAndrew Thompson cdce_resume(device_t dev)
60502ac6454SAndrew Thompson {
60602ac6454SAndrew Thompson 	device_printf(dev, "Resuming\n");
60702ac6454SAndrew Thompson 	return (0);
60802ac6454SAndrew Thompson }
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson static void
611760bc48eSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer)
61202ac6454SAndrew Thompson {
61302ac6454SAndrew Thompson 	struct cdce_softc *sc = xfer->priv_sc;
61402ac6454SAndrew Thompson 	struct mbuf *m;
61502ac6454SAndrew Thompson 	uint8_t x;
61602ac6454SAndrew Thompson 
61702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
61802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
61902ac6454SAndrew Thompson 
62002ac6454SAndrew Thompson 		DPRINTF("received %u bytes in %u frames\n",
62102ac6454SAndrew Thompson 		    xfer->actlen, xfer->aframes);
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 		for (x = 0; x != xfer->aframes; x++) {
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson 			m = sc->sc_rx_buf[x];
62602ac6454SAndrew Thompson 			sc->sc_rx_buf[x] = NULL;
62702ac6454SAndrew Thompson 
62802ac6454SAndrew Thompson 			/* Strip off CRC added by Zaurus, if any */
62902ac6454SAndrew Thompson 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) &&
63002ac6454SAndrew Thompson 			    (xfer->frlengths[x] >= 14))
63102ac6454SAndrew Thompson 				xfer->frlengths[x] -= 4;
63202ac6454SAndrew Thompson 
63302ac6454SAndrew Thompson 			if (xfer->frlengths[x] < sizeof(struct ether_header)) {
63402ac6454SAndrew Thompson 				m_freem(m);
63502ac6454SAndrew Thompson 				continue;
63602ac6454SAndrew Thompson 			}
63702ac6454SAndrew Thompson 			/* queue up mbuf */
63802ac6454SAndrew Thompson 			usb2_ether_rxmbuf(&sc->sc_ue, m, xfer->frlengths[x]);
63902ac6454SAndrew Thompson 		}
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson 		/* FALLTHROUGH */
64202ac6454SAndrew Thompson 	case USB_ST_SETUP:
64302ac6454SAndrew Thompson 		/*
64402ac6454SAndrew Thompson 		 * TODO: Implement support for multi frame transfers,
64502ac6454SAndrew Thompson 		 * when the USB hardware supports it.
64602ac6454SAndrew Thompson 		 */
64702ac6454SAndrew Thompson 		for (x = 0; x != 1; x++) {
64802ac6454SAndrew Thompson 			if (sc->sc_rx_buf[x] == NULL) {
64975fd0939SAndrew Thompson 				m = usb2_ether_newbuf();
65002ac6454SAndrew Thompson 				if (m == NULL)
65102ac6454SAndrew Thompson 					goto tr_stall;
65202ac6454SAndrew Thompson 				sc->sc_rx_buf[x] = m;
65302ac6454SAndrew Thompson 			} else {
65402ac6454SAndrew Thompson 				m = sc->sc_rx_buf[x];
65502ac6454SAndrew Thompson 			}
65602ac6454SAndrew Thompson 
65702ac6454SAndrew Thompson 			usb2_set_frame_data(xfer, m->m_data, x);
65802ac6454SAndrew Thompson 			xfer->frlengths[x] = m->m_len;
65902ac6454SAndrew Thompson 		}
66002ac6454SAndrew Thompson 		/* set number of frames and start hardware */
66102ac6454SAndrew Thompson 		xfer->nframes = x;
66202ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
66302ac6454SAndrew Thompson 		/* flush any received frames */
66402ac6454SAndrew Thompson 		usb2_ether_rxflush(&sc->sc_ue);
66502ac6454SAndrew Thompson 		break;
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson 	default:			/* Error */
66802ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
66902ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
67002ac6454SAndrew Thompson 
67102ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
67202ac6454SAndrew Thompson tr_stall:
67302ac6454SAndrew Thompson 			/* try to clear stall first */
67402ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
67502ac6454SAndrew Thompson 			xfer->nframes = 0;
67602ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
67702ac6454SAndrew Thompson 			break;
67802ac6454SAndrew Thompson 		}
67902ac6454SAndrew Thompson 
68002ac6454SAndrew Thompson 		/* need to free the RX-mbufs when we are cancelled */
68102ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
68202ac6454SAndrew Thompson 		break;
68302ac6454SAndrew Thompson 	}
68402ac6454SAndrew Thompson }
68502ac6454SAndrew Thompson 
68602ac6454SAndrew Thompson static void
687760bc48eSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer)
68802ac6454SAndrew Thompson {
68902ac6454SAndrew Thompson 	;				/* style fix */
69002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
69102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
69202ac6454SAndrew Thompson 
69302ac6454SAndrew Thompson 		DPRINTF("Received %d bytes\n",
69402ac6454SAndrew Thompson 		    xfer->actlen);
69502ac6454SAndrew Thompson 
69602ac6454SAndrew Thompson 		/* TODO: decode some indications */
69702ac6454SAndrew Thompson 
69802ac6454SAndrew Thompson 		/* FALLTHROUGH */
69902ac6454SAndrew Thompson 	case USB_ST_SETUP:
70002ac6454SAndrew Thompson tr_setup:
70102ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
70202ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
70302ac6454SAndrew Thompson 		break;
70402ac6454SAndrew Thompson 
70502ac6454SAndrew Thompson 	default:			/* Error */
70602ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
70702ac6454SAndrew Thompson 			/* start clear stall */
70802ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
70902ac6454SAndrew Thompson 			goto tr_setup;
71002ac6454SAndrew Thompson 		}
71102ac6454SAndrew Thompson 		break;
71202ac6454SAndrew Thompson 	}
71302ac6454SAndrew Thompson }
71402ac6454SAndrew Thompson 
71502ac6454SAndrew Thompson static void
716760bc48eSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer)
71702ac6454SAndrew Thompson {
71802ac6454SAndrew Thompson 	;				/* style fix */
71902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
72002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
72102ac6454SAndrew Thompson 
72202ac6454SAndrew Thompson 		DPRINTF("Transferred %d bytes\n", xfer->actlen);
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson 		/* FALLTHROUGH */
72502ac6454SAndrew Thompson 	case USB_ST_SETUP:
72602ac6454SAndrew Thompson tr_setup:
72702ac6454SAndrew Thompson #if 0
72802ac6454SAndrew Thompson 		xfer->frlengths[0] = XXX;
72902ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
73002ac6454SAndrew Thompson #endif
73102ac6454SAndrew Thompson 		break;
73202ac6454SAndrew Thompson 
73302ac6454SAndrew Thompson 	default:			/* Error */
73402ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
73502ac6454SAndrew Thompson 			/* start clear stall */
73602ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
73702ac6454SAndrew Thompson 			goto tr_setup;
73802ac6454SAndrew Thompson 		}
73902ac6454SAndrew Thompson 		break;
74002ac6454SAndrew Thompson 	}
74102ac6454SAndrew Thompson }
74202ac6454SAndrew Thompson 
74302ac6454SAndrew Thompson static int
74402ac6454SAndrew Thompson cdce_handle_request(device_t dev,
74502ac6454SAndrew Thompson     const void *req, void **pptr, uint16_t *plen,
74602ac6454SAndrew Thompson     uint16_t offset, uint8_t is_complete)
74702ac6454SAndrew Thompson {
74802ac6454SAndrew Thompson 	return (ENXIO);			/* use builtin handler */
74902ac6454SAndrew Thompson }
750