xref: /freebsd/sys/dev/usb/net/if_cdce.c (revision 8f9e0ef947d29b8728c62a941c13e127120701fc)
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 
46ed6d949aSAndrew Thompson #include <sys/stdint.h>
47ed6d949aSAndrew Thompson #include <sys/stddef.h>
48ed6d949aSAndrew Thompson #include <sys/param.h>
49ed6d949aSAndrew Thompson #include <sys/queue.h>
50ed6d949aSAndrew Thompson #include <sys/types.h>
51ed6d949aSAndrew Thompson #include <sys/systm.h>
52ed6d949aSAndrew Thompson #include <sys/kernel.h>
53ed6d949aSAndrew Thompson #include <sys/bus.h>
54ed6d949aSAndrew Thompson #include <sys/linker_set.h>
55ed6d949aSAndrew Thompson #include <sys/module.h>
56ed6d949aSAndrew Thompson #include <sys/lock.h>
57ed6d949aSAndrew Thompson #include <sys/mutex.h>
58ed6d949aSAndrew Thompson #include <sys/condvar.h>
59ed6d949aSAndrew Thompson #include <sys/sysctl.h>
60ed6d949aSAndrew Thompson #include <sys/sx.h>
61ed6d949aSAndrew Thompson #include <sys/unistd.h>
62ed6d949aSAndrew Thompson #include <sys/callout.h>
63ed6d949aSAndrew Thompson #include <sys/malloc.h>
64ed6d949aSAndrew Thompson #include <sys/priv.h>
65ed6d949aSAndrew Thompson 
6602ac6454SAndrew Thompson #include <dev/usb/usb.h>
67ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
68ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
6902ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
70ed6d949aSAndrew Thompson #include "usbdevs.h"
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson #define	USB_DEBUG_VAR cdce_debug
7302ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
74ed6d949aSAndrew Thompson #include <dev/usb/usb_process.h>
75ed6d949aSAndrew Thompson #include "usb_if.h"
7602ac6454SAndrew Thompson 
7702ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h>
7802ac6454SAndrew Thompson #include <dev/usb/net/if_cdcereg.h>
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson static device_probe_t cdce_probe;
8102ac6454SAndrew Thompson static device_attach_t cdce_attach;
8202ac6454SAndrew Thompson static device_detach_t cdce_detach;
8302ac6454SAndrew Thompson static device_suspend_t cdce_suspend;
8402ac6454SAndrew Thompson static device_resume_t cdce_resume;
8502ac6454SAndrew Thompson static usb_handle_request_t cdce_handle_request;
8602ac6454SAndrew Thompson 
87e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_write_callback;
88e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_read_callback;
89e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_read_callback;
90e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_write_callback;
9102ac6454SAndrew Thompson 
92e0a69b51SAndrew Thompson static uether_fn_t cdce_attach_post;
93e0a69b51SAndrew Thompson static uether_fn_t cdce_init;
94e0a69b51SAndrew Thompson static uether_fn_t cdce_stop;
95e0a69b51SAndrew Thompson static uether_fn_t cdce_start;
96e0a69b51SAndrew Thompson static uether_fn_t cdce_setmulti;
97e0a69b51SAndrew Thompson static uether_fn_t cdce_setpromisc;
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson static uint32_t	cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
10002ac6454SAndrew Thompson 
10102ac6454SAndrew Thompson #if USB_DEBUG
10202ac6454SAndrew Thompson static int cdce_debug = 0;
10302ac6454SAndrew Thompson 
1049360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
1059360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0,
10602ac6454SAndrew Thompson     "Debug level");
10702ac6454SAndrew Thompson #endif
10802ac6454SAndrew Thompson 
109760bc48eSAndrew Thompson static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
11002ac6454SAndrew Thompson 
1114eae601eSAndrew Thompson 	[CDCE_BULK_RX] = {
11202ac6454SAndrew Thompson 		.type = UE_BULK,
11302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1144eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
11502ac6454SAndrew Thompson 		.if_index = 0,
1164eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1174eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1184eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
1194eae601eSAndrew Thompson 		.callback = cdce_bulk_read_callback,
1204eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
121f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
12202ac6454SAndrew Thompson 	},
12302ac6454SAndrew Thompson 
1244eae601eSAndrew Thompson 	[CDCE_BULK_TX] = {
12502ac6454SAndrew Thompson 		.type = UE_BULK,
12602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1274eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
12802ac6454SAndrew Thompson 		.if_index = 0,
1294eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1304eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1314eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
1324eae601eSAndrew Thompson 		.callback = cdce_bulk_write_callback,
1334eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
134f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
13502ac6454SAndrew Thompson 	},
13602ac6454SAndrew Thompson 
1374eae601eSAndrew Thompson 	[CDCE_INTR_RX] = {
13802ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
13902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1404eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
14102ac6454SAndrew Thompson 		.if_index = 1,
1424eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1434eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
1444eae601eSAndrew Thompson 		.callback = cdce_intr_read_callback,
1454eae601eSAndrew Thompson 		.timeout = 0,
1464eae601eSAndrew Thompson 		.usb_mode = USB_MODE_HOST,
1474eae601eSAndrew Thompson 	},
1484eae601eSAndrew Thompson 
1494eae601eSAndrew Thompson 	[CDCE_INTR_TX] = {
1504eae601eSAndrew Thompson 		.type = UE_INTERRUPT,
1514eae601eSAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1524eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
1534eae601eSAndrew Thompson 		.if_index = 1,
1544eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1554eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
1564eae601eSAndrew Thompson 		.callback = cdce_intr_write_callback,
1574eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
1584eae601eSAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
15902ac6454SAndrew Thompson 	},
16002ac6454SAndrew Thompson };
16102ac6454SAndrew Thompson 
16202ac6454SAndrew Thompson static device_method_t cdce_methods[] = {
16302ac6454SAndrew Thompson 	/* USB interface */
16402ac6454SAndrew Thompson 	DEVMETHOD(usb_handle_request, cdce_handle_request),
16502ac6454SAndrew Thompson 
16602ac6454SAndrew Thompson 	/* Device interface */
16702ac6454SAndrew Thompson 	DEVMETHOD(device_probe, cdce_probe),
16802ac6454SAndrew Thompson 	DEVMETHOD(device_attach, cdce_attach),
16902ac6454SAndrew Thompson 	DEVMETHOD(device_detach, cdce_detach),
17002ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, cdce_suspend),
17102ac6454SAndrew Thompson 	DEVMETHOD(device_resume, cdce_resume),
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson 	{0, 0}
17402ac6454SAndrew Thompson };
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson static driver_t cdce_driver = {
17702ac6454SAndrew Thompson 	.name = "cdce",
17802ac6454SAndrew Thompson 	.methods = cdce_methods,
17902ac6454SAndrew Thompson 	.size = sizeof(struct cdce_softc),
18002ac6454SAndrew Thompson };
18102ac6454SAndrew Thompson 
18202ac6454SAndrew Thompson static devclass_t cdce_devclass;
18302ac6454SAndrew Thompson 
1849aef556dSAndrew Thompson DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0);
18502ac6454SAndrew Thompson MODULE_VERSION(cdce, 1);
18602ac6454SAndrew Thompson MODULE_DEPEND(cdce, uether, 1, 1, 1);
18702ac6454SAndrew Thompson MODULE_DEPEND(cdce, usb, 1, 1, 1);
18802ac6454SAndrew Thompson MODULE_DEPEND(cdce, ether, 1, 1, 1);
18902ac6454SAndrew Thompson 
190760bc48eSAndrew Thompson static const struct usb_ether_methods cdce_ue_methods = {
19102ac6454SAndrew Thompson 	.ue_attach_post = cdce_attach_post,
19202ac6454SAndrew Thompson 	.ue_start = cdce_start,
19302ac6454SAndrew Thompson 	.ue_init = cdce_init,
19402ac6454SAndrew Thompson 	.ue_stop = cdce_stop,
19502ac6454SAndrew Thompson 	.ue_setmulti = cdce_setmulti,
19602ac6454SAndrew Thompson 	.ue_setpromisc = cdce_setpromisc,
19702ac6454SAndrew Thompson };
19802ac6454SAndrew Thompson 
199760bc48eSAndrew Thompson static const struct usb_device_id cdce_devs[] = {
20002ac6454SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
20102ac6454SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
20202ac6454SAndrew Thompson 
20302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
20402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
20502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
20602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
20702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
20802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
20902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
21002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
21102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
21202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
21302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
21402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
21502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
21602ac6454SAndrew Thompson };
21702ac6454SAndrew Thompson 
21802ac6454SAndrew Thompson static int
21902ac6454SAndrew Thompson cdce_probe(device_t dev)
22002ac6454SAndrew Thompson {
221760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
22202ac6454SAndrew Thompson 
223a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(cdce_devs, sizeof(cdce_devs), uaa));
22402ac6454SAndrew Thompson }
22502ac6454SAndrew Thompson 
22602ac6454SAndrew Thompson static void
227760bc48eSAndrew Thompson cdce_attach_post(struct usb_ether *ue)
22802ac6454SAndrew Thompson {
22902ac6454SAndrew Thompson 	/* no-op */
23002ac6454SAndrew Thompson 	return;
23102ac6454SAndrew Thompson }
23202ac6454SAndrew Thompson 
23302ac6454SAndrew Thompson static int
23402ac6454SAndrew Thompson cdce_attach(device_t dev)
23502ac6454SAndrew Thompson {
23602ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
237760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
238760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
239760bc48eSAndrew Thompson 	struct usb_interface *iface;
240760bc48eSAndrew Thompson 	const struct usb_cdc_union_descriptor *ud;
241760bc48eSAndrew Thompson 	const struct usb_interface_descriptor *id;
242760bc48eSAndrew Thompson 	const struct usb_cdc_ethernet_descriptor *ued;
24302ac6454SAndrew Thompson 	int error;
24402ac6454SAndrew Thompson 	uint8_t i;
24502ac6454SAndrew Thompson 	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
24802ac6454SAndrew Thompson 
249a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
25002ac6454SAndrew Thompson 
25102ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
25202ac6454SAndrew Thompson 
25302ac6454SAndrew Thompson 	if (sc->sc_flags & CDCE_FLAG_NO_UNION) {
25402ac6454SAndrew Thompson 		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
25502ac6454SAndrew Thompson 		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
25602ac6454SAndrew Thompson 		sc->sc_data_iface_no = 0;	/* not used */
25702ac6454SAndrew Thompson 		goto alloc_transfers;
25802ac6454SAndrew Thompson 	}
2593c8d24f4SAndrew Thompson 	ud = usbd_find_descriptor
26002ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
26102ac6454SAndrew Thompson 	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
26202ac6454SAndrew Thompson 
26302ac6454SAndrew Thompson 	if ((ud == NULL) || (ud->bLength < sizeof(*ud))) {
26402ac6454SAndrew Thompson 		device_printf(dev, "no union descriptor!\n");
26502ac6454SAndrew Thompson 		goto detach;
26602ac6454SAndrew Thompson 	}
26702ac6454SAndrew Thompson 	sc->sc_data_iface_no = ud->bSlaveInterface[0];
26802ac6454SAndrew Thompson 
26902ac6454SAndrew Thompson 	for (i = 0;; i++) {
27002ac6454SAndrew Thompson 
271a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
27202ac6454SAndrew Thompson 
27302ac6454SAndrew Thompson 		if (iface) {
27402ac6454SAndrew Thompson 
275a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
27602ac6454SAndrew Thompson 
27702ac6454SAndrew Thompson 			if (id && (id->bInterfaceNumber ==
27802ac6454SAndrew Thompson 			    sc->sc_data_iface_no)) {
27902ac6454SAndrew Thompson 				sc->sc_ifaces_index[0] = i;
28002ac6454SAndrew Thompson 				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
281a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
28202ac6454SAndrew Thompson 				break;
28302ac6454SAndrew Thompson 			}
28402ac6454SAndrew Thompson 		} else {
28502ac6454SAndrew Thompson 			device_printf(dev, "no data interface found!\n");
28602ac6454SAndrew Thompson 			goto detach;
28702ac6454SAndrew Thompson 		}
28802ac6454SAndrew Thompson 	}
28902ac6454SAndrew Thompson 
29002ac6454SAndrew Thompson 	/*
29102ac6454SAndrew Thompson 	 * <quote>
29202ac6454SAndrew Thompson 	 *
29302ac6454SAndrew Thompson 	 *  The Data Class interface of a networking device shall have
29402ac6454SAndrew Thompson 	 *  a minimum of two interface settings. The first setting
29502ac6454SAndrew Thompson 	 *  (the default interface setting) includes no endpoints and
29602ac6454SAndrew Thompson 	 *  therefore no networking traffic is exchanged whenever the
29702ac6454SAndrew Thompson 	 *  default interface setting is selected. One or more
29802ac6454SAndrew Thompson 	 *  additional interface settings are used for normal
29902ac6454SAndrew Thompson 	 *  operation, and therefore each includes a pair of endpoints
30002ac6454SAndrew Thompson 	 *  (one IN, and one OUT) to exchange network traffic. Select
30102ac6454SAndrew Thompson 	 *  an alternate interface setting to initialize the network
30202ac6454SAndrew Thompson 	 *  aspects of the device and to enable the exchange of
30302ac6454SAndrew Thompson 	 *  network traffic.
30402ac6454SAndrew Thompson 	 *
30502ac6454SAndrew Thompson 	 * </quote>
30602ac6454SAndrew Thompson 	 *
30702ac6454SAndrew Thompson 	 * Some devices, most notably cable modems, include interface
30802ac6454SAndrew Thompson 	 * settings that have no IN or OUT endpoint, therefore loop
30902ac6454SAndrew Thompson 	 * through the list of all available interface settings
31002ac6454SAndrew Thompson 	 * looking for one with both IN and OUT endpoints.
31102ac6454SAndrew Thompson 	 */
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson alloc_transfers:
31402ac6454SAndrew Thompson 
31502ac6454SAndrew Thompson 	for (i = 0; i != 32; i++) {
31602ac6454SAndrew Thompson 
317a593f6b8SAndrew Thompson 		error = usbd_set_alt_interface_index
31802ac6454SAndrew Thompson 		    (uaa->device, sc->sc_ifaces_index[0], i);
31902ac6454SAndrew Thompson 
32002ac6454SAndrew Thompson 		if (error) {
32102ac6454SAndrew Thompson 			device_printf(dev, "no valid alternate "
32202ac6454SAndrew Thompson 			    "setting found!\n");
32302ac6454SAndrew Thompson 			goto detach;
32402ac6454SAndrew Thompson 		}
325a593f6b8SAndrew Thompson 		error = usbd_transfer_setup
32602ac6454SAndrew Thompson 		    (uaa->device, sc->sc_ifaces_index,
32702ac6454SAndrew Thompson 		    sc->sc_xfer, cdce_config, CDCE_N_TRANSFER,
32802ac6454SAndrew Thompson 		    sc, &sc->sc_mtx);
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 		if (error == 0) {
33102ac6454SAndrew Thompson 			break;
33202ac6454SAndrew Thompson 		}
33302ac6454SAndrew Thompson 	}
33402ac6454SAndrew Thompson 
3353c8d24f4SAndrew Thompson 	ued = usbd_find_descriptor
33602ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
33702ac6454SAndrew Thompson 	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1);
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson 	if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
34002ac6454SAndrew Thompson 		error = USB_ERR_INVAL;
34102ac6454SAndrew Thompson 	} else {
342a593f6b8SAndrew Thompson 		error = usbd_req_get_string_any(uaa->device, NULL,
34302ac6454SAndrew Thompson 		    eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
34402ac6454SAndrew Thompson 	}
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 	if (error) {
34702ac6454SAndrew Thompson 
34802ac6454SAndrew Thompson 		/* fake MAC address */
34902ac6454SAndrew Thompson 
35002ac6454SAndrew Thompson 		device_printf(dev, "faking MAC address\n");
35102ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[0] = 0x2a;
35202ac6454SAndrew Thompson 		memcpy(&sc->sc_ue.ue_eaddr[1], &ticks, sizeof(uint32_t));
35302ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson 	} else {
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 		bzero(sc->sc_ue.ue_eaddr, sizeof(sc->sc_ue.ue_eaddr));
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson 		for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
36002ac6454SAndrew Thompson 
36102ac6454SAndrew Thompson 			char c = eaddr_str[i];
36202ac6454SAndrew Thompson 
36302ac6454SAndrew Thompson 			if ('0' <= c && c <= '9')
36402ac6454SAndrew Thompson 				c -= '0';
36502ac6454SAndrew Thompson 			else if (c != 0)
36602ac6454SAndrew Thompson 				c -= 'A' - 10;
36702ac6454SAndrew Thompson 			else
36802ac6454SAndrew Thompson 				break;
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 			c &= 0xf;
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson 			if ((i & 1) == 0)
37302ac6454SAndrew Thompson 				c <<= 4;
37402ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[i / 2] |= c;
37502ac6454SAndrew Thompson 		}
37602ac6454SAndrew Thompson 
377f29a0724SAndrew Thompson 		if (uaa->usb_mode == USB_MODE_DEVICE) {
37802ac6454SAndrew Thompson 			/*
37902ac6454SAndrew Thompson 			 * Do not use the same MAC address like the peer !
38002ac6454SAndrew Thompson 			 */
38102ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[5] ^= 0xFF;
38202ac6454SAndrew Thompson 		}
38302ac6454SAndrew Thompson 	}
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	ue->ue_sc = sc;
38602ac6454SAndrew Thompson 	ue->ue_dev = dev;
38702ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
38802ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
38902ac6454SAndrew Thompson 	ue->ue_methods = &cdce_ue_methods;
39002ac6454SAndrew Thompson 
391a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
39202ac6454SAndrew Thompson 	if (error) {
39302ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
39402ac6454SAndrew Thompson 		goto detach;
39502ac6454SAndrew Thompson 	}
39602ac6454SAndrew Thompson 	return (0);			/* success */
39702ac6454SAndrew Thompson 
39802ac6454SAndrew Thompson detach:
39902ac6454SAndrew Thompson 	cdce_detach(dev);
40002ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
40102ac6454SAndrew Thompson }
40202ac6454SAndrew Thompson 
40302ac6454SAndrew Thompson static int
40402ac6454SAndrew Thompson cdce_detach(device_t dev)
40502ac6454SAndrew Thompson {
40602ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
407760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
40802ac6454SAndrew Thompson 
40902ac6454SAndrew Thompson 	/* stop all USB transfers first */
410a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
411a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
41202ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
41302ac6454SAndrew Thompson 
41402ac6454SAndrew Thompson 	return (0);
41502ac6454SAndrew Thompson }
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson static void
418760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue)
41902ac6454SAndrew Thompson {
420a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
42102ac6454SAndrew Thompson 
42202ac6454SAndrew Thompson 	/*
42302ac6454SAndrew Thompson 	 * Start the USB transfers, if not already started:
42402ac6454SAndrew Thompson 	 */
425a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
426a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
42702ac6454SAndrew Thompson }
42802ac6454SAndrew Thompson 
42902ac6454SAndrew Thompson static void
43002ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n)
43102ac6454SAndrew Thompson {
43202ac6454SAndrew Thompson 	uint8_t x;
43302ac6454SAndrew Thompson 	for (x = 0; x != n; x++) {
43402ac6454SAndrew Thompson 		if (ppm[x] != NULL) {
43502ac6454SAndrew Thompson 			m_freem(ppm[x]);
43602ac6454SAndrew Thompson 			ppm[x] = NULL;
43702ac6454SAndrew Thompson 		}
43802ac6454SAndrew Thompson 	}
43902ac6454SAndrew Thompson }
44002ac6454SAndrew Thompson 
44102ac6454SAndrew Thompson static void
442ed6d949aSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
44302ac6454SAndrew Thompson {
444ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
445a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
44602ac6454SAndrew Thompson 	struct mbuf *m;
44702ac6454SAndrew Thompson 	struct mbuf *mt;
44802ac6454SAndrew Thompson 	uint32_t crc;
44902ac6454SAndrew Thompson 	uint8_t x;
450ed6d949aSAndrew Thompson 	int actlen, aframes;
451ed6d949aSAndrew Thompson 
452ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 	DPRINTFN(1, "\n");
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
45702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
458ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
459ed6d949aSAndrew Thompson 		    actlen, aframes);
46002ac6454SAndrew Thompson 
46102ac6454SAndrew Thompson 		ifp->if_opackets++;
46202ac6454SAndrew Thompson 
46302ac6454SAndrew Thompson 		/* free all previous TX buffers */
46402ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
46502ac6454SAndrew Thompson 
46602ac6454SAndrew Thompson 		/* FALLTHROUGH */
46702ac6454SAndrew Thompson 	case USB_ST_SETUP:
46802ac6454SAndrew Thompson tr_setup:
46902ac6454SAndrew Thompson 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson 			if (m == NULL)
47402ac6454SAndrew Thompson 				break;
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
47702ac6454SAndrew Thompson 				/*
47802ac6454SAndrew Thompson 				 * Zaurus wants a 32-bit CRC appended
47902ac6454SAndrew Thompson 				 * to every frame
48002ac6454SAndrew Thompson 				 */
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
48302ac6454SAndrew Thompson 				crc = htole32(crc);
48402ac6454SAndrew Thompson 
48502ac6454SAndrew Thompson 				if (!m_append(m, 4, (void *)&crc)) {
48602ac6454SAndrew Thompson 					m_freem(m);
48702ac6454SAndrew Thompson 					ifp->if_oerrors++;
48802ac6454SAndrew Thompson 					continue;
48902ac6454SAndrew Thompson 				}
49002ac6454SAndrew Thompson 			}
49102ac6454SAndrew Thompson 			if (m->m_len != m->m_pkthdr.len) {
49202ac6454SAndrew Thompson 				mt = m_defrag(m, M_DONTWAIT);
49302ac6454SAndrew Thompson 				if (mt == NULL) {
49402ac6454SAndrew Thompson 					m_freem(m);
49502ac6454SAndrew Thompson 					ifp->if_oerrors++;
49602ac6454SAndrew Thompson 					continue;
49702ac6454SAndrew Thompson 				}
49802ac6454SAndrew Thompson 				m = mt;
49902ac6454SAndrew Thompson 			}
50002ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
50102ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
50202ac6454SAndrew Thompson 			}
50302ac6454SAndrew Thompson 			sc->sc_tx_buf[x] = m;
504ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
50502ac6454SAndrew Thompson 
50602ac6454SAndrew Thompson 			/*
50702ac6454SAndrew Thompson 			 * If there's a BPF listener, bounce a copy of
50802ac6454SAndrew Thompson 			 * this frame to him:
50902ac6454SAndrew Thompson 			 */
51002ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
51102ac6454SAndrew Thompson 		}
51202ac6454SAndrew Thompson 		if (x != 0) {
513ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
514ed6d949aSAndrew Thompson 
515a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
51602ac6454SAndrew Thompson 		}
51702ac6454SAndrew Thompson 		break;
51802ac6454SAndrew Thompson 
51902ac6454SAndrew Thompson 	default:			/* Error */
52002ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
521ed6d949aSAndrew Thompson 		    usbd_errstr(error));
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson 		/* free all previous TX buffers */
52402ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
52502ac6454SAndrew Thompson 
52602ac6454SAndrew Thompson 		/* count output errors */
52702ac6454SAndrew Thompson 		ifp->if_oerrors++;
52802ac6454SAndrew Thompson 
529ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
53002ac6454SAndrew Thompson 			/* try to clear stall first */
531ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
53202ac6454SAndrew Thompson 			goto tr_setup;
53302ac6454SAndrew Thompson 		}
53402ac6454SAndrew Thompson 		break;
53502ac6454SAndrew Thompson 	}
53602ac6454SAndrew Thompson }
53702ac6454SAndrew Thompson 
53802ac6454SAndrew Thompson static int32_t
53902ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
54002ac6454SAndrew Thompson {
54102ac6454SAndrew Thompson 	uint32_t *p_crc = arg;
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson 	*p_crc = crc32_raw(src, count, *p_crc);
54402ac6454SAndrew Thompson 	return (0);
54502ac6454SAndrew Thompson }
54602ac6454SAndrew Thompson 
54702ac6454SAndrew Thompson static uint32_t
54802ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
54902ac6454SAndrew Thompson {
55002ac6454SAndrew Thompson 	uint32_t crc = 0xFFFFFFFF;
55102ac6454SAndrew Thompson 	int error;
55202ac6454SAndrew Thompson 
55302ac6454SAndrew Thompson 	error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
55402ac6454SAndrew Thompson 	return (crc ^ 0xFFFFFFFF);
55502ac6454SAndrew Thompson }
55602ac6454SAndrew Thompson 
55702ac6454SAndrew Thompson static void
558760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue)
55902ac6454SAndrew Thompson {
560a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
561a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
56202ac6454SAndrew Thompson 
56302ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
56602ac6454SAndrew Thompson 
56702ac6454SAndrew Thompson 	/* start interrupt transfer */
568a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
569a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson 	/* stall data write direction, which depends on USB mode */
572ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
57302ac6454SAndrew Thompson 
57402ac6454SAndrew Thompson 	/* start data transfers */
57502ac6454SAndrew Thompson 	cdce_start(ue);
57602ac6454SAndrew Thompson }
57702ac6454SAndrew Thompson 
57802ac6454SAndrew Thompson static void
579760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue)
58002ac6454SAndrew Thompson {
581a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
582a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
58302ac6454SAndrew Thompson 
58402ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
58502ac6454SAndrew Thompson 
58602ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
58702ac6454SAndrew Thompson 
58802ac6454SAndrew Thompson 	/*
58902ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
59002ac6454SAndrew Thompson 	 */
591a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
592a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
593a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
594a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
59502ac6454SAndrew Thompson }
59602ac6454SAndrew Thompson 
59702ac6454SAndrew Thompson static void
598760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue)
59902ac6454SAndrew Thompson {
60002ac6454SAndrew Thompson 	/* no-op */
60102ac6454SAndrew Thompson 	return;
60202ac6454SAndrew Thompson }
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson static void
605760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue)
60602ac6454SAndrew Thompson {
60702ac6454SAndrew Thompson 	/* no-op */
60802ac6454SAndrew Thompson 	return;
60902ac6454SAndrew Thompson }
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson static int
61202ac6454SAndrew Thompson cdce_suspend(device_t dev)
61302ac6454SAndrew Thompson {
61402ac6454SAndrew Thompson 	device_printf(dev, "Suspending\n");
61502ac6454SAndrew Thompson 	return (0);
61602ac6454SAndrew Thompson }
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson static int
61902ac6454SAndrew Thompson cdce_resume(device_t dev)
62002ac6454SAndrew Thompson {
62102ac6454SAndrew Thompson 	device_printf(dev, "Resuming\n");
62202ac6454SAndrew Thompson 	return (0);
62302ac6454SAndrew Thompson }
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson static void
626ed6d949aSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
62702ac6454SAndrew Thompson {
628ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
62902ac6454SAndrew Thompson 	struct mbuf *m;
63002ac6454SAndrew Thompson 	uint8_t x;
631ed6d949aSAndrew Thompson 	int actlen, aframes, len;
632ed6d949aSAndrew Thompson 
633ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
63602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
63702ac6454SAndrew Thompson 
638ed6d949aSAndrew Thompson 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
63902ac6454SAndrew Thompson 
640ed6d949aSAndrew Thompson 		for (x = 0; x != aframes; x++) {
64102ac6454SAndrew Thompson 
64202ac6454SAndrew Thompson 			m = sc->sc_rx_buf[x];
64302ac6454SAndrew Thompson 			sc->sc_rx_buf[x] = NULL;
6448f9e0ef9SAndrew Thompson 			len = usbd_xfer_frame_len(xfer, x);
64502ac6454SAndrew Thompson 
64602ac6454SAndrew Thompson 			/* Strip off CRC added by Zaurus, if any */
647ed6d949aSAndrew Thompson 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
648ed6d949aSAndrew Thompson 				len -= 4;
64902ac6454SAndrew Thompson 
650ed6d949aSAndrew Thompson 			if (len < sizeof(struct ether_header)) {
65102ac6454SAndrew Thompson 				m_freem(m);
65202ac6454SAndrew Thompson 				continue;
65302ac6454SAndrew Thompson 			}
65402ac6454SAndrew Thompson 			/* queue up mbuf */
655ed6d949aSAndrew Thompson 			uether_rxmbuf(&sc->sc_ue, m, len);
65602ac6454SAndrew Thompson 		}
65702ac6454SAndrew Thompson 
65802ac6454SAndrew Thompson 		/* FALLTHROUGH */
65902ac6454SAndrew Thompson 	case USB_ST_SETUP:
66002ac6454SAndrew Thompson 		/*
66102ac6454SAndrew Thompson 		 * TODO: Implement support for multi frame transfers,
66202ac6454SAndrew Thompson 		 * when the USB hardware supports it.
66302ac6454SAndrew Thompson 		 */
66402ac6454SAndrew Thompson 		for (x = 0; x != 1; x++) {
66502ac6454SAndrew Thompson 			if (sc->sc_rx_buf[x] == NULL) {
666a593f6b8SAndrew Thompson 				m = uether_newbuf();
66702ac6454SAndrew Thompson 				if (m == NULL)
66802ac6454SAndrew Thompson 					goto tr_stall;
66902ac6454SAndrew Thompson 				sc->sc_rx_buf[x] = m;
67002ac6454SAndrew Thompson 			} else {
67102ac6454SAndrew Thompson 				m = sc->sc_rx_buf[x];
67202ac6454SAndrew Thompson 			}
67302ac6454SAndrew Thompson 
674ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
67502ac6454SAndrew Thompson 		}
67602ac6454SAndrew Thompson 		/* set number of frames and start hardware */
677ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, x);
678a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
67902ac6454SAndrew Thompson 		/* flush any received frames */
680a593f6b8SAndrew Thompson 		uether_rxflush(&sc->sc_ue);
68102ac6454SAndrew Thompson 		break;
68202ac6454SAndrew Thompson 
68302ac6454SAndrew Thompson 	default:			/* Error */
68402ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
685ed6d949aSAndrew Thompson 		    usbd_errstr(error));
68602ac6454SAndrew Thompson 
687ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
68802ac6454SAndrew Thompson tr_stall:
68902ac6454SAndrew Thompson 			/* try to clear stall first */
690ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
691ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, 0);
692a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
69302ac6454SAndrew Thompson 			break;
69402ac6454SAndrew Thompson 		}
69502ac6454SAndrew Thompson 
69602ac6454SAndrew Thompson 		/* need to free the RX-mbufs when we are cancelled */
69702ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
69802ac6454SAndrew Thompson 		break;
69902ac6454SAndrew Thompson 	}
70002ac6454SAndrew Thompson }
70102ac6454SAndrew Thompson 
70202ac6454SAndrew Thompson static void
703ed6d949aSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
70402ac6454SAndrew Thompson {
705ed6d949aSAndrew Thompson 	int actlen;
706ed6d949aSAndrew Thompson 
707ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
708ed6d949aSAndrew Thompson 
70902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
71002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
71102ac6454SAndrew Thompson 
712ed6d949aSAndrew Thompson 		DPRINTF("Received %d bytes\n", actlen);
71302ac6454SAndrew Thompson 
71402ac6454SAndrew Thompson 		/* TODO: decode some indications */
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson 		/* FALLTHROUGH */
71702ac6454SAndrew Thompson 	case USB_ST_SETUP:
71802ac6454SAndrew Thompson tr_setup:
719ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
720a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
72102ac6454SAndrew Thompson 		break;
72202ac6454SAndrew Thompson 
72302ac6454SAndrew Thompson 	default:			/* Error */
724ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
72502ac6454SAndrew Thompson 			/* start clear stall */
726ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
72702ac6454SAndrew Thompson 			goto tr_setup;
72802ac6454SAndrew Thompson 		}
72902ac6454SAndrew Thompson 		break;
73002ac6454SAndrew Thompson 	}
73102ac6454SAndrew Thompson }
73202ac6454SAndrew Thompson 
73302ac6454SAndrew Thompson static void
734ed6d949aSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
73502ac6454SAndrew Thompson {
736ed6d949aSAndrew Thompson 	int actlen;
737ed6d949aSAndrew Thompson 
738ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
739ed6d949aSAndrew Thompson 
74002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
74102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
74202ac6454SAndrew Thompson 
743ed6d949aSAndrew Thompson 		DPRINTF("Transferred %d bytes\n", actlen);
74402ac6454SAndrew Thompson 
74502ac6454SAndrew Thompson 		/* FALLTHROUGH */
74602ac6454SAndrew Thompson 	case USB_ST_SETUP:
74702ac6454SAndrew Thompson tr_setup:
74802ac6454SAndrew Thompson #if 0
749ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, XXX);
750a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
75102ac6454SAndrew Thompson #endif
75202ac6454SAndrew Thompson 		break;
75302ac6454SAndrew Thompson 
75402ac6454SAndrew Thompson 	default:			/* Error */
755ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
75602ac6454SAndrew Thompson 			/* start clear stall */
757ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
75802ac6454SAndrew Thompson 			goto tr_setup;
75902ac6454SAndrew Thompson 		}
76002ac6454SAndrew Thompson 		break;
76102ac6454SAndrew Thompson 	}
76202ac6454SAndrew Thompson }
76302ac6454SAndrew Thompson 
76402ac6454SAndrew Thompson static int
76502ac6454SAndrew Thompson cdce_handle_request(device_t dev,
76602ac6454SAndrew Thompson     const void *req, void **pptr, uint16_t *plen,
76702ac6454SAndrew Thompson     uint16_t offset, uint8_t is_complete)
76802ac6454SAndrew Thompson {
76902ac6454SAndrew Thompson 	return (ENXIO);			/* use builtin handler */
77002ac6454SAndrew Thompson }
771