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