xref: /freebsd/sys/dev/usb/net/if_cdce.c (revision e66352013a67183dfeec14b2a22913305c26e838)
102ac6454SAndrew Thompson /*	$NetBSD: if_cdce.c,v 1.4 2004/10/24 12:50:54 augustss Exp $ */
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /*-
4df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
5df57947fSPedro F. Giffuni  *
602ac6454SAndrew Thompson  * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com>
702ac6454SAndrew Thompson  * Copyright (c) 2003-2005 Craig Boston
802ac6454SAndrew Thompson  * Copyright (c) 2004 Daniel Hartmeier
902ac6454SAndrew Thompson  * Copyright (c) 2009 Hans Petter Selasky
1002ac6454SAndrew Thompson  * All rights reserved.
1102ac6454SAndrew Thompson  *
1202ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1302ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1402ac6454SAndrew Thompson  * are met:
1502ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1602ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1702ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1802ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1902ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
2002ac6454SAndrew Thompson  * 3. All advertising materials mentioning features or use of this software
2102ac6454SAndrew Thompson  *    must display the following acknowledgement:
2202ac6454SAndrew Thompson  *	This product includes software developed by Bill Paul.
2302ac6454SAndrew Thompson  * 4. Neither the name of the author nor the names of any co-contributors
2402ac6454SAndrew Thompson  *    may be used to endorse or promote products derived from this software
2502ac6454SAndrew Thompson  *    without specific prior written permission.
2602ac6454SAndrew Thompson  *
2702ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2802ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2902ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3002ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR
3102ac6454SAndrew Thompson  * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3202ac6454SAndrew Thompson  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3302ac6454SAndrew Thompson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3402ac6454SAndrew Thompson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3502ac6454SAndrew Thompson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3602ac6454SAndrew Thompson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3702ac6454SAndrew Thompson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3802ac6454SAndrew Thompson  */
3902ac6454SAndrew Thompson 
4002ac6454SAndrew Thompson /*
4102ac6454SAndrew Thompson  * USB Communication Device Class (Ethernet Networking Control Model)
4202ac6454SAndrew Thompson  * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
4302ac6454SAndrew Thompson  */
4402ac6454SAndrew Thompson 
454076dd23SAndrew Thompson /*
464076dd23SAndrew Thompson  * USB Network Control Model (NCM)
474076dd23SAndrew Thompson  * http://www.usb.org/developers/devclass_docs/NCM10.zip
484076dd23SAndrew Thompson  */
494076dd23SAndrew Thompson 
5002ac6454SAndrew Thompson #include <sys/cdefs.h>
5102ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
5202ac6454SAndrew Thompson 
53f89d2072SXin LI #include <sys/gsb_crc32.h>
54e2e050c8SConrad Meyer #include <sys/eventhandler.h>
55ed6d949aSAndrew Thompson #include <sys/stdint.h>
56ed6d949aSAndrew Thompson #include <sys/stddef.h>
57ed6d949aSAndrew Thompson #include <sys/queue.h>
58ed6d949aSAndrew Thompson #include <sys/systm.h>
5976039bc8SGleb Smirnoff #include <sys/socket.h>
60ed6d949aSAndrew Thompson #include <sys/kernel.h>
61ed6d949aSAndrew Thompson #include <sys/bus.h>
62ed6d949aSAndrew Thompson #include <sys/module.h>
63ed6d949aSAndrew Thompson #include <sys/lock.h>
64ed6d949aSAndrew Thompson #include <sys/mutex.h>
65ed6d949aSAndrew Thompson #include <sys/condvar.h>
66ed6d949aSAndrew Thompson #include <sys/sysctl.h>
67ed6d949aSAndrew Thompson #include <sys/sx.h>
68ed6d949aSAndrew Thompson #include <sys/unistd.h>
69ed6d949aSAndrew Thompson #include <sys/callout.h>
70ed6d949aSAndrew Thompson #include <sys/malloc.h>
71ed6d949aSAndrew Thompson #include <sys/priv.h>
72ed6d949aSAndrew Thompson 
7376039bc8SGleb Smirnoff #include <net/if.h>
7476039bc8SGleb Smirnoff #include <net/if_var.h>
7576039bc8SGleb Smirnoff 
7602ac6454SAndrew Thompson #include <dev/usb/usb.h>
77ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
78ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
7902ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
80ed6d949aSAndrew Thompson #include "usbdevs.h"
8102ac6454SAndrew Thompson 
8202ac6454SAndrew Thompson #define	USB_DEBUG_VAR cdce_debug
8302ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
84ed6d949aSAndrew Thompson #include <dev/usb/usb_process.h>
85c376f439SNick Hibma #include <dev/usb/usb_msctest.h>
86ed6d949aSAndrew Thompson #include "usb_if.h"
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h>
8902ac6454SAndrew Thompson #include <dev/usb/net/if_cdcereg.h>
9002ac6454SAndrew Thompson 
9102ac6454SAndrew Thompson static device_probe_t cdce_probe;
9202ac6454SAndrew Thompson static device_attach_t cdce_attach;
9302ac6454SAndrew Thompson static device_detach_t cdce_detach;
9402ac6454SAndrew Thompson static device_suspend_t cdce_suspend;
9502ac6454SAndrew Thompson static device_resume_t cdce_resume;
9602ac6454SAndrew Thompson static usb_handle_request_t cdce_handle_request;
9702ac6454SAndrew Thompson 
98e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_write_callback;
99e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_read_callback;
100e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_read_callback;
101e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_write_callback;
10202ac6454SAndrew Thompson 
1034076dd23SAndrew Thompson #if CDCE_HAVE_NCM
1044076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_write_callback;
1054076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_read_callback;
1064076dd23SAndrew Thompson #endif
1074076dd23SAndrew Thompson 
108e0a69b51SAndrew Thompson static uether_fn_t cdce_attach_post;
109e0a69b51SAndrew Thompson static uether_fn_t cdce_init;
110e0a69b51SAndrew Thompson static uether_fn_t cdce_stop;
111e0a69b51SAndrew Thompson static uether_fn_t cdce_start;
112e0a69b51SAndrew Thompson static uether_fn_t cdce_setmulti;
113e0a69b51SAndrew Thompson static uether_fn_t cdce_setpromisc;
114b43d600cSJohn-Mark Gurney static int cdce_attach_post_sub(struct usb_ether *);
115b43d600cSJohn-Mark Gurney static int cdce_ioctl(struct ifnet *, u_long, caddr_t);
116b43d600cSJohn-Mark Gurney static int cdce_media_change_cb(struct ifnet *);
117b43d600cSJohn-Mark Gurney static void cdce_media_status_cb(struct ifnet *, struct ifmediareq *);
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson static uint32_t	cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
120f0c393f7SKornel Duleba static void	cdce_set_filter(struct usb_ether *);
12102ac6454SAndrew Thompson 
122b850ecc1SAndrew Thompson #ifdef USB_DEBUG
12302ac6454SAndrew Thompson static int cdce_debug = 0;
1247e05daaeSHans Petter Selasky static int cdce_tx_interval = 0;
12502ac6454SAndrew Thompson 
126f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
127f8d2b1f3SPawel Biernacki     "USB CDC-Ethernet");
128ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RWTUN, &cdce_debug, 0,
12902ac6454SAndrew Thompson     "Debug level");
130ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RWTUN, &cdce_tx_interval, 0,
1317e05daaeSHans Petter Selasky     "NCM transmit interval in ms");
132b43d600cSJohn-Mark Gurney #else
133b43d600cSJohn-Mark Gurney #define cdce_debug 0
13402ac6454SAndrew Thompson #endif
13502ac6454SAndrew Thompson 
136760bc48eSAndrew Thompson static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
1374eae601eSAndrew Thompson 	[CDCE_BULK_RX] = {
13802ac6454SAndrew Thompson 		.type = UE_BULK,
13902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1404eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
14102ac6454SAndrew Thompson 		.if_index = 0,
1424eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1434eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1444eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
1454eae601eSAndrew Thompson 		.callback = cdce_bulk_read_callback,
1464eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
147f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
14802ac6454SAndrew Thompson 	},
14902ac6454SAndrew Thompson 
1504eae601eSAndrew Thompson 	[CDCE_BULK_TX] = {
15102ac6454SAndrew Thompson 		.type = UE_BULK,
15202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1534eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
15402ac6454SAndrew Thompson 		.if_index = 0,
1554eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1564eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1574eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
1584eae601eSAndrew Thompson 		.callback = cdce_bulk_write_callback,
1594eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
160f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
16102ac6454SAndrew Thompson 	},
16202ac6454SAndrew Thompson 
1634eae601eSAndrew Thompson 	[CDCE_INTR_RX] = {
16402ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
16502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1664eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
16702ac6454SAndrew Thompson 		.if_index = 1,
1684eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1694eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
1704eae601eSAndrew Thompson 		.callback = cdce_intr_read_callback,
1714eae601eSAndrew Thompson 		.timeout = 0,
1724eae601eSAndrew Thompson 		.usb_mode = USB_MODE_HOST,
1734eae601eSAndrew Thompson 	},
1744eae601eSAndrew Thompson 
1754eae601eSAndrew Thompson 	[CDCE_INTR_TX] = {
1764eae601eSAndrew Thompson 		.type = UE_INTERRUPT,
1774eae601eSAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1784eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
1794eae601eSAndrew Thompson 		.if_index = 1,
1804eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1814eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
1824eae601eSAndrew Thompson 		.callback = cdce_intr_write_callback,
1834eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
1844eae601eSAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
18502ac6454SAndrew Thompson 	},
18602ac6454SAndrew Thompson };
18702ac6454SAndrew Thompson 
1884076dd23SAndrew Thompson #if CDCE_HAVE_NCM
1894076dd23SAndrew Thompson static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
1904076dd23SAndrew Thompson 	[CDCE_BULK_RX] = {
1914076dd23SAndrew Thompson 		.type = UE_BULK,
1924076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1934076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
1944076dd23SAndrew Thompson 		.if_index = 0,
1954076dd23SAndrew Thompson 		.frames = CDCE_NCM_RX_FRAMES_MAX,
1964076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
1974076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
1984076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_read_callback,
1994076dd23SAndrew Thompson 		.timeout = 0,	/* no timeout */
2004076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
2014076dd23SAndrew Thompson 	},
2024076dd23SAndrew Thompson 
2034076dd23SAndrew Thompson 	[CDCE_BULK_TX] = {
2044076dd23SAndrew Thompson 		.type = UE_BULK,
2054076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2064076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
2074076dd23SAndrew Thompson 		.if_index = 0,
2084076dd23SAndrew Thompson 		.frames = CDCE_NCM_TX_FRAMES_MAX,
2094076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
2107e05daaeSHans Petter Selasky 		.flags = {.pipe_bof = 1,},
2114076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_write_callback,
2124076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2134076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
2144076dd23SAndrew Thompson 	},
2154076dd23SAndrew Thompson 
2164076dd23SAndrew Thompson 	[CDCE_INTR_RX] = {
2174076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2184076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2194076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
2204076dd23SAndrew Thompson 		.if_index = 1,
2214076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2224076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
2234076dd23SAndrew Thompson 		.callback = cdce_intr_read_callback,
2244076dd23SAndrew Thompson 		.timeout = 0,
2254076dd23SAndrew Thompson 		.usb_mode = USB_MODE_HOST,
2264076dd23SAndrew Thompson 	},
2274076dd23SAndrew Thompson 
2284076dd23SAndrew Thompson 	[CDCE_INTR_TX] = {
2294076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2304076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2314076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
2324076dd23SAndrew Thompson 		.if_index = 1,
2334076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2344076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
2354076dd23SAndrew Thompson 		.callback = cdce_intr_write_callback,
2364076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2374076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
2384076dd23SAndrew Thompson 	},
2394076dd23SAndrew Thompson };
2404076dd23SAndrew Thompson #endif
2414076dd23SAndrew Thompson 
24202ac6454SAndrew Thompson static device_method_t cdce_methods[] = {
24302ac6454SAndrew Thompson 	/* USB interface */
24402ac6454SAndrew Thompson 	DEVMETHOD(usb_handle_request, cdce_handle_request),
24502ac6454SAndrew Thompson 
24602ac6454SAndrew Thompson 	/* Device interface */
24702ac6454SAndrew Thompson 	DEVMETHOD(device_probe, cdce_probe),
24802ac6454SAndrew Thompson 	DEVMETHOD(device_attach, cdce_attach),
24902ac6454SAndrew Thompson 	DEVMETHOD(device_detach, cdce_detach),
25002ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, cdce_suspend),
25102ac6454SAndrew Thompson 	DEVMETHOD(device_resume, cdce_resume),
25202ac6454SAndrew Thompson 
25361bfd867SSofian Brabez 	DEVMETHOD_END
25402ac6454SAndrew Thompson };
25502ac6454SAndrew Thompson 
25602ac6454SAndrew Thompson static driver_t cdce_driver = {
25702ac6454SAndrew Thompson 	.name = "cdce",
25802ac6454SAndrew Thompson 	.methods = cdce_methods,
25902ac6454SAndrew Thompson 	.size = sizeof(struct cdce_softc),
26002ac6454SAndrew Thompson };
26102ac6454SAndrew Thompson 
262c376f439SNick Hibma static eventhandler_tag cdce_etag;
26302ac6454SAndrew Thompson 
264c376f439SNick Hibma static int  cdce_driver_loaded(struct module *, int, void *);
265c376f439SNick Hibma 
266c376f439SNick Hibma static const STRUCT_USB_HOST_ID cdce_switch_devs[] = {
267c376f439SNick Hibma 	{USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3272_INIT, MSC_EJECT_HUAWEI2)},
2681997d3a4SMichael Paepcke 	{USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3372v153_INIT, MSC_EJECT_HUAWEI2)},
269*e6635201SMichael Paepcke 	{USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3372_INIT, MSC_EJECT_HUAWEI4)},
270*e6635201SMichael Paepcke 	{USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E5573Cs322_ECM, MSC_EJECT_HUAWEI3)},
271c376f439SNick Hibma };
272c376f439SNick Hibma 
273f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
27402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
27502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
27602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
27702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
27802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
28102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
28202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
2862625e519SHiroki Sato 	{USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8156, 0)},
287c376f439SNick Hibma 
288c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
289c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x16),
290c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
291c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
292c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x46),
293c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
294c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
295c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x76),
296c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
297cdadbbb0SHans Petter Selasky 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
298cdadbbb0SHans Petter Selasky 		USB_IFACE_SUBCLASS(0x03), USB_IFACE_PROTOCOL(0x16),
299cdadbbb0SHans Petter Selasky 		USB_DRIVER_INFO(0)},
300f1a16106SHans Petter Selasky };
3019739167cSAlfred Perlstein 
302f1a16106SHans Petter Selasky static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
3039739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
3049739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
3054076dd23SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
30602ac6454SAndrew Thompson };
30702ac6454SAndrew Thompson 
308bc9372d7SJohn Baldwin DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_driver_loaded, NULL);
309f809f280SWarner Losh MODULE_VERSION(cdce, 1);
310f809f280SWarner Losh MODULE_DEPEND(cdce, uether, 1, 1, 1);
311f809f280SWarner Losh MODULE_DEPEND(cdce, usb, 1, 1, 1);
312f809f280SWarner Losh MODULE_DEPEND(cdce, ether, 1, 1, 1);
313f809f280SWarner Losh USB_PNP_DEVICE_INFO(cdce_switch_devs);
314f809f280SWarner Losh USB_PNP_HOST_INFO(cdce_host_devs);
315f809f280SWarner Losh USB_PNP_DUAL_INFO(cdce_dual_devs);
316f809f280SWarner Losh 
317f809f280SWarner Losh static const struct usb_ether_methods cdce_ue_methods = {
318f809f280SWarner Losh 	.ue_attach_post = cdce_attach_post,
319b43d600cSJohn-Mark Gurney 	.ue_attach_post_sub = cdce_attach_post_sub,
320f809f280SWarner Losh 	.ue_start = cdce_start,
321f809f280SWarner Losh 	.ue_init = cdce_init,
322f809f280SWarner Losh 	.ue_stop = cdce_stop,
323f809f280SWarner Losh 	.ue_setmulti = cdce_setmulti,
324f809f280SWarner Losh 	.ue_setpromisc = cdce_setpromisc,
325f809f280SWarner Losh };
326f809f280SWarner Losh 
3274076dd23SAndrew Thompson #if CDCE_HAVE_NCM
3284076dd23SAndrew Thompson /*------------------------------------------------------------------------*
3294076dd23SAndrew Thompson  *	cdce_ncm_init
3304076dd23SAndrew Thompson  *
3314076dd23SAndrew Thompson  * Return values:
3324076dd23SAndrew Thompson  * 0: Success
3334076dd23SAndrew Thompson  * Else: Failure
3344076dd23SAndrew Thompson  *------------------------------------------------------------------------*/
3354076dd23SAndrew Thompson static uint8_t
3364076dd23SAndrew Thompson cdce_ncm_init(struct cdce_softc *sc)
3374076dd23SAndrew Thompson {
3384076dd23SAndrew Thompson 	struct usb_ncm_parameters temp;
3394076dd23SAndrew Thompson 	struct usb_device_request req;
3407e05daaeSHans Petter Selasky 	struct usb_ncm_func_descriptor *ufd;
3417e05daaeSHans Petter Selasky 	uint8_t value[8];
3424076dd23SAndrew Thompson 	int err;
3434076dd23SAndrew Thompson 
3447e05daaeSHans Petter Selasky 	ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
3456d917491SHans Petter Selasky 	    sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF,
3466d917491SHans Petter Selasky 	    UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF);
3477e05daaeSHans Petter Selasky 
3487e05daaeSHans Petter Selasky 	/* verify length of NCM functional descriptor */
3497e05daaeSHans Petter Selasky 	if (ufd != NULL) {
3507e05daaeSHans Petter Selasky 		if (ufd->bLength < sizeof(*ufd))
3517e05daaeSHans Petter Selasky 			ufd = NULL;
3527e05daaeSHans Petter Selasky 		else
3537e05daaeSHans Petter Selasky 			DPRINTFN(1, "Found NCM functional descriptor.\n");
3547e05daaeSHans Petter Selasky 	}
3557e05daaeSHans Petter Selasky 
3564076dd23SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
3574076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
3584076dd23SAndrew Thompson 	USETW(req.wValue, 0);
3594076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
3604076dd23SAndrew Thompson 	req.wIndex[1] = 0;
3614076dd23SAndrew Thompson 	USETW(req.wLength, sizeof(temp));
3624076dd23SAndrew Thompson 
3634076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
3644076dd23SAndrew Thompson 	    &temp, 0, NULL, 1000 /* ms */);
3654076dd23SAndrew Thompson 	if (err)
3664076dd23SAndrew Thompson 		return (1);
3674076dd23SAndrew Thompson 
3684076dd23SAndrew Thompson 	/* Read correct set of parameters according to device mode */
3694076dd23SAndrew Thompson 
3704076dd23SAndrew Thompson 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
371dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
372dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
3734076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
3744076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
3754076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
3767e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3774076dd23SAndrew Thompson 	} else {
378dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
379dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
3804076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
3814076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
3824076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
3837e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3844076dd23SAndrew Thompson 	}
3854076dd23SAndrew Thompson 
3864076dd23SAndrew Thompson 	/* Verify maximum receive length */
3874076dd23SAndrew Thompson 
3887e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.rx_max < 32) ||
3894076dd23SAndrew Thompson 	    (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
3904076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum receive length\n");
3914076dd23SAndrew Thompson 		sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
3924076dd23SAndrew Thompson 	}
3934076dd23SAndrew Thompson 
3944076dd23SAndrew Thompson 	/* Verify maximum transmit length */
3954076dd23SAndrew Thompson 
3967e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_max < 32) ||
3974076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
3984076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum transmit length\n");
3994076dd23SAndrew Thompson 		sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
4004076dd23SAndrew Thompson 	}
4014076dd23SAndrew Thompson 
4024076dd23SAndrew Thompson 	/*
4034076dd23SAndrew Thompson 	 * Verify that the structure alignment is:
4044076dd23SAndrew Thompson 	 * - power of two
4054076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
4064076dd23SAndrew Thompson 	 * - not less than four bytes
4074076dd23SAndrew Thompson 	 */
4087e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_struct_align < 4) ||
4094076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align !=
4104076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
4114076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
4124076dd23SAndrew Thompson 		DPRINTFN(1, "Using default other alignment: 4 bytes\n");
4134076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = 4;
4144076dd23SAndrew Thompson 	}
4154076dd23SAndrew Thompson 
4164076dd23SAndrew Thompson 	/*
4174076dd23SAndrew Thompson 	 * Verify that the payload alignment is:
4184076dd23SAndrew Thompson 	 * - power of two
4194076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
4204076dd23SAndrew Thompson 	 * - not less than four bytes
4214076dd23SAndrew Thompson 	 */
4227e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_modulus < 4) ||
4234076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus !=
4244076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
4254076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
4264076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
4274076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = 4;
4284076dd23SAndrew Thompson 	}
4294076dd23SAndrew Thompson 
4304076dd23SAndrew Thompson 	/* Verify that the payload remainder */
4314076dd23SAndrew Thompson 
4327e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
4334076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
4344076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = 0;
4354076dd23SAndrew Thompson 	}
4364076dd23SAndrew Thompson 
4377e05daaeSHans Petter Selasky 	/*
4387e05daaeSHans Petter Selasky 	 * Offset the TX remainder so that IP packet payload starts at
4397e05daaeSHans Petter Selasky 	 * the tx_modulus. This is not too clear in the specification.
4407e05daaeSHans Petter Selasky 	 */
4417e05daaeSHans Petter Selasky 
4427e05daaeSHans Petter Selasky 	sc->sc_ncm.tx_remainder =
4437e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
4447e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_modulus - 1);
4457e05daaeSHans Petter Selasky 
4467e05daaeSHans Petter Selasky 	/* Verify max datagrams */
4477e05daaeSHans Petter Selasky 
4487e05daaeSHans Petter Selasky 	if (sc->sc_ncm.tx_nframe == 0 ||
4497e05daaeSHans Petter Selasky 	    sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
4507e05daaeSHans Petter Selasky 		DPRINTFN(1, "Using default max "
4517e05daaeSHans Petter Selasky 		    "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
4527e05daaeSHans Petter Selasky 		/* need to reserve one entry for zero padding */
4537e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
4547e05daaeSHans Petter Selasky 	}
4557e05daaeSHans Petter Selasky 
4564076dd23SAndrew Thompson 	/* Additional configuration, will fail in device side mode, which is OK. */
4574076dd23SAndrew Thompson 
4584076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4594076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
4604076dd23SAndrew Thompson 	USETW(req.wValue, 0);
4614076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4624076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4637e05daaeSHans Petter Selasky 
4647e05daaeSHans Petter Selasky 	if (ufd != NULL &&
4657e05daaeSHans Petter Selasky 	    (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
4667e05daaeSHans Petter Selasky 		USETW(req.wLength, 8);
4677e05daaeSHans Petter Selasky 		USETDW(value, sc->sc_ncm.rx_max);
4687e05daaeSHans Petter Selasky 		USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
4697e05daaeSHans Petter Selasky 		USETW(value + 6, 0);
4707e05daaeSHans Petter Selasky 	} else {
4714076dd23SAndrew Thompson 		USETW(req.wLength, 4);
4724076dd23SAndrew Thompson 		USETDW(value, sc->sc_ncm.rx_max);
4737e05daaeSHans Petter Selasky  	}
4744076dd23SAndrew Thompson 
4754076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4764076dd23SAndrew Thompson 	    &value, 0, NULL, 1000 /* ms */);
4774076dd23SAndrew Thompson 	if (err) {
4784076dd23SAndrew Thompson 		DPRINTFN(1, "Setting input size "
4794076dd23SAndrew Thompson 		    "to %u failed.\n", sc->sc_ncm.rx_max);
4804076dd23SAndrew Thompson 	}
4814076dd23SAndrew Thompson 
4824076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4834076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_CRC_MODE;
4844076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* no CRC */
4854076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4864076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4874076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4884076dd23SAndrew Thompson 
4894076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4904076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4914076dd23SAndrew Thompson 	if (err) {
4924076dd23SAndrew Thompson 		DPRINTFN(1, "Setting CRC mode to off failed.\n");
4934076dd23SAndrew Thompson 	}
4944076dd23SAndrew Thompson 
4954076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4964076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
4974076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* NTB-16 */
4984076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4994076dd23SAndrew Thompson 	req.wIndex[1] = 0;
5004076dd23SAndrew Thompson 	USETW(req.wLength, 0);
5014076dd23SAndrew Thompson 
5024076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
5034076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
5044076dd23SAndrew Thompson 	if (err) {
5054076dd23SAndrew Thompson 		DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
5064076dd23SAndrew Thompson 	}
5074076dd23SAndrew Thompson 
5084076dd23SAndrew Thompson 	return (0);		/* success */
5094076dd23SAndrew Thompson }
5104076dd23SAndrew Thompson #endif
5114076dd23SAndrew Thompson 
512c376f439SNick Hibma static void
513c376f439SNick Hibma cdce_test_autoinst(void *arg, struct usb_device *udev,
514c376f439SNick Hibma     struct usb_attach_arg *uaa)
515c376f439SNick Hibma {
516c376f439SNick Hibma 	struct usb_interface *iface;
517c376f439SNick Hibma 	struct usb_interface_descriptor *id;
518c376f439SNick Hibma 
519c376f439SNick Hibma 	if (uaa->dev_state != UAA_DEV_READY)
520c376f439SNick Hibma 		return;
521c376f439SNick Hibma 
522c376f439SNick Hibma 	iface = usbd_get_iface(udev, 0);
523c376f439SNick Hibma 	if (iface == NULL)
524c376f439SNick Hibma 		return;
525c376f439SNick Hibma 	id = iface->idesc;
526c376f439SNick Hibma 	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
527c376f439SNick Hibma 		return;
528c376f439SNick Hibma 	if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
529c376f439SNick Hibma 		return;		/* no device match */
530c376f439SNick Hibma 
531c376f439SNick Hibma 	if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
532c376f439SNick Hibma 		/* success, mark the udev as disappearing */
533c376f439SNick Hibma 		uaa->dev_state = UAA_DEV_EJECTING;
534c376f439SNick Hibma 	}
535c376f439SNick Hibma }
536c376f439SNick Hibma 
537c376f439SNick Hibma static int
538c376f439SNick Hibma cdce_driver_loaded(struct module *mod, int what, void *arg)
539c376f439SNick Hibma {
540c376f439SNick Hibma 	switch (what) {
541c376f439SNick Hibma 	case MOD_LOAD:
542c376f439SNick Hibma 		/* register our autoinstall handler */
543c376f439SNick Hibma 		cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
544c376f439SNick Hibma 		    cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
545c376f439SNick Hibma 		return (0);
546c376f439SNick Hibma 	case MOD_UNLOAD:
547c376f439SNick Hibma 		EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
548c376f439SNick Hibma 		return (0);
549c376f439SNick Hibma 	default:
550c376f439SNick Hibma 		return (EOPNOTSUPP);
551c376f439SNick Hibma 	}
552c376f439SNick Hibma }
553c376f439SNick Hibma 
55402ac6454SAndrew Thompson static int
55502ac6454SAndrew Thompson cdce_probe(device_t dev)
55602ac6454SAndrew Thompson {
557760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
558f1a16106SHans Petter Selasky 	int error;
55902ac6454SAndrew Thompson 
560f1a16106SHans Petter Selasky 	error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
561f1a16106SHans Petter Selasky 	if (error)
562f1a16106SHans Petter Selasky 		error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
563f1a16106SHans Petter Selasky 	return (error);
56402ac6454SAndrew Thompson }
56502ac6454SAndrew Thompson 
56602ac6454SAndrew Thompson static void
567760bc48eSAndrew Thompson cdce_attach_post(struct usb_ether *ue)
56802ac6454SAndrew Thompson {
56902ac6454SAndrew Thompson 	/* no-op */
57002ac6454SAndrew Thompson 	return;
57102ac6454SAndrew Thompson }
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson static int
574b43d600cSJohn-Mark Gurney cdce_attach_post_sub(struct usb_ether *ue)
575b43d600cSJohn-Mark Gurney {
576b43d600cSJohn-Mark Gurney 	struct cdce_softc *sc = uether_getsc(ue);
577b43d600cSJohn-Mark Gurney 	struct ifnet *ifp = uether_getifp(ue);
578b43d600cSJohn-Mark Gurney 
579b43d600cSJohn-Mark Gurney 	/* mostly copied from usb_ethernet.c */
580b43d600cSJohn-Mark Gurney 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
581b43d600cSJohn-Mark Gurney 	ifp->if_start = uether_start;
582b43d600cSJohn-Mark Gurney 	ifp->if_ioctl = cdce_ioctl;
583b43d600cSJohn-Mark Gurney 	ifp->if_init = uether_init;
584b43d600cSJohn-Mark Gurney 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
585b43d600cSJohn-Mark Gurney 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
586b43d600cSJohn-Mark Gurney 	IFQ_SET_READY(&ifp->if_snd);
587b43d600cSJohn-Mark Gurney 
588b43d600cSJohn-Mark Gurney 	if ((sc->sc_flags & CDCE_FLAG_VLAN) == CDCE_FLAG_VLAN)
589b43d600cSJohn-Mark Gurney 		if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
590b43d600cSJohn-Mark Gurney 
591b43d600cSJohn-Mark Gurney 	if_setcapabilitiesbit(ifp, IFCAP_LINKSTATE, 0);
592b43d600cSJohn-Mark Gurney 	if_setcapenable(ifp, if_getcapabilities(ifp));
593b43d600cSJohn-Mark Gurney 
594b43d600cSJohn-Mark Gurney 	ifmedia_init(&sc->sc_media, IFM_IMASK, cdce_media_change_cb,
595b43d600cSJohn-Mark Gurney 	    cdce_media_status_cb);
596b43d600cSJohn-Mark Gurney 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
597b43d600cSJohn-Mark Gurney 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
598b43d600cSJohn-Mark Gurney 	sc->sc_media.ifm_media = sc->sc_media.ifm_cur->ifm_media;
599f0c393f7SKornel Duleba 	CDCE_LOCK(sc);
600f0c393f7SKornel Duleba 	cdce_set_filter(ue);
601f0c393f7SKornel Duleba 	CDCE_UNLOCK(sc);
602b43d600cSJohn-Mark Gurney 
603b43d600cSJohn-Mark Gurney 	return 0;
604b43d600cSJohn-Mark Gurney }
605b43d600cSJohn-Mark Gurney 
606b43d600cSJohn-Mark Gurney static int
60702ac6454SAndrew Thompson cdce_attach(device_t dev)
60802ac6454SAndrew Thompson {
60902ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
610760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
611760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
612760bc48eSAndrew Thompson 	struct usb_interface *iface;
613760bc48eSAndrew Thompson 	const struct usb_cdc_union_descriptor *ud;
614760bc48eSAndrew Thompson 	const struct usb_interface_descriptor *id;
615760bc48eSAndrew Thompson 	const struct usb_cdc_ethernet_descriptor *ued;
6164076dd23SAndrew Thompson 	const struct usb_config *pcfg;
617a8df530dSJohn Baldwin 	uint32_t seed;
61802ac6454SAndrew Thompson 	int error;
61902ac6454SAndrew Thompson 	uint8_t i;
6204076dd23SAndrew Thompson 	uint8_t data_iface_no;
62102ac6454SAndrew Thompson 	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
6244076dd23SAndrew Thompson 	sc->sc_ue.ue_udev = uaa->device;
62502ac6454SAndrew Thompson 
626a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
62702ac6454SAndrew Thompson 
62802ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
62902ac6454SAndrew Thompson 
6303c8d24f4SAndrew Thompson 	ud = usbd_find_descriptor
63102ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
6326d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF);
63302ac6454SAndrew Thompson 
6344076dd23SAndrew Thompson 	if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
6354076dd23SAndrew Thompson 	    (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
6364076dd23SAndrew Thompson 		DPRINTFN(1, "No union descriptor!\n");
6374076dd23SAndrew Thompson 		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
6384076dd23SAndrew Thompson 		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
6394076dd23SAndrew Thompson 		goto alloc_transfers;
64002ac6454SAndrew Thompson 	}
6414076dd23SAndrew Thompson 	data_iface_no = ud->bSlaveInterface[0];
64202ac6454SAndrew Thompson 
64302ac6454SAndrew Thompson 	for (i = 0;; i++) {
644a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
64502ac6454SAndrew Thompson 
64602ac6454SAndrew Thompson 		if (iface) {
647a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
64802ac6454SAndrew Thompson 
6494076dd23SAndrew Thompson 			if (id && (id->bInterfaceNumber == data_iface_no)) {
65002ac6454SAndrew Thompson 				sc->sc_ifaces_index[0] = i;
65102ac6454SAndrew Thompson 				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
652a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
65302ac6454SAndrew Thompson 				break;
65402ac6454SAndrew Thompson 			}
65502ac6454SAndrew Thompson 		} else {
656767cb2e2SAndrew Thompson 			device_printf(dev, "no data interface found\n");
65702ac6454SAndrew Thompson 			goto detach;
65802ac6454SAndrew Thompson 		}
65902ac6454SAndrew Thompson 	}
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson 	/*
66202ac6454SAndrew Thompson 	 * <quote>
66302ac6454SAndrew Thompson 	 *
66402ac6454SAndrew Thompson 	 *  The Data Class interface of a networking device shall have
66502ac6454SAndrew Thompson 	 *  a minimum of two interface settings. The first setting
66602ac6454SAndrew Thompson 	 *  (the default interface setting) includes no endpoints and
66702ac6454SAndrew Thompson 	 *  therefore no networking traffic is exchanged whenever the
66802ac6454SAndrew Thompson 	 *  default interface setting is selected. One or more
66902ac6454SAndrew Thompson 	 *  additional interface settings are used for normal
67002ac6454SAndrew Thompson 	 *  operation, and therefore each includes a pair of endpoints
67102ac6454SAndrew Thompson 	 *  (one IN, and one OUT) to exchange network traffic. Select
67202ac6454SAndrew Thompson 	 *  an alternate interface setting to initialize the network
67302ac6454SAndrew Thompson 	 *  aspects of the device and to enable the exchange of
67402ac6454SAndrew Thompson 	 *  network traffic.
67502ac6454SAndrew Thompson 	 *
67602ac6454SAndrew Thompson 	 * </quote>
67702ac6454SAndrew Thompson 	 *
67802ac6454SAndrew Thompson 	 * Some devices, most notably cable modems, include interface
67902ac6454SAndrew Thompson 	 * settings that have no IN or OUT endpoint, therefore loop
68002ac6454SAndrew Thompson 	 * through the list of all available interface settings
68102ac6454SAndrew Thompson 	 * looking for one with both IN and OUT endpoints.
68202ac6454SAndrew Thompson 	 */
68302ac6454SAndrew Thompson 
68402ac6454SAndrew Thompson alloc_transfers:
68502ac6454SAndrew Thompson 
6864076dd23SAndrew Thompson 	pcfg = cdce_config;	/* Default Configuration */
6874076dd23SAndrew Thompson 
68802ac6454SAndrew Thompson 	for (i = 0; i != 32; i++) {
6894076dd23SAndrew Thompson 		error = usbd_set_alt_interface_index(uaa->device,
6904076dd23SAndrew Thompson 		    sc->sc_ifaces_index[0], i);
6914076dd23SAndrew Thompson 		if (error)
6924076dd23SAndrew Thompson 			break;
6934076dd23SAndrew Thompson #if CDCE_HAVE_NCM
6944076dd23SAndrew Thompson 		if ((i == 0) && (cdce_ncm_init(sc) == 0))
6954076dd23SAndrew Thompson 			pcfg = cdce_ncm_config;
6964076dd23SAndrew Thompson #endif
6974076dd23SAndrew Thompson 		error = usbd_transfer_setup(uaa->device,
6984076dd23SAndrew Thompson 		    sc->sc_ifaces_index, sc->sc_xfer,
6994076dd23SAndrew Thompson 		    pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
70002ac6454SAndrew Thompson 
7014076dd23SAndrew Thompson 		if (error == 0)
70202ac6454SAndrew Thompson 			break;
70302ac6454SAndrew Thompson 	}
7044076dd23SAndrew Thompson 
7054076dd23SAndrew Thompson 	if (error || (i == 32)) {
7064076dd23SAndrew Thompson 		device_printf(dev, "No valid alternate "
707767cb2e2SAndrew Thompson 		    "setting found\n");
7084076dd23SAndrew Thompson 		goto detach;
70902ac6454SAndrew Thompson 	}
71002ac6454SAndrew Thompson 
7113c8d24f4SAndrew Thompson 	ued = usbd_find_descriptor
71202ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
7136d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF);
71402ac6454SAndrew Thompson 
71502ac6454SAndrew Thompson 	if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
71602ac6454SAndrew Thompson 		error = USB_ERR_INVAL;
71702ac6454SAndrew Thompson 	} else {
718b43d600cSJohn-Mark Gurney 		/*
719b43d600cSJohn-Mark Gurney 		 * ECM 1.2 doesn't say it excludes the CRC, but states that it's
720b43d600cSJohn-Mark Gurney 		 * normally 1514, which excludes the CRC.
721b43d600cSJohn-Mark Gurney 		 */
722b43d600cSJohn-Mark Gurney 		DPRINTF("max segsize: %d\n", UGETW(ued->wMaxSegmentSize));
723b43d600cSJohn-Mark Gurney 		if (UGETW(ued->wMaxSegmentSize) >= (ETHER_MAX_LEN - ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN))
724b43d600cSJohn-Mark Gurney 			sc->sc_flags |= CDCE_FLAG_VLAN;
725b43d600cSJohn-Mark Gurney 
7263d510418SJohn-Mark Gurney 		error = usbd_req_get_string_any(uaa->device, NULL,
7273d510418SJohn-Mark Gurney 		    eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
7283d510418SJohn-Mark Gurney 	}
7293d510418SJohn-Mark Gurney 
73002ac6454SAndrew Thompson 	if (error) {
73102ac6454SAndrew Thompson 		/* fake MAC address */
73202ac6454SAndrew Thompson 
73302ac6454SAndrew Thompson 		device_printf(dev, "faking MAC address\n");
734a8df530dSJohn Baldwin 		seed = ticks;
73502ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[0] = 0x2a;
736a8df530dSJohn Baldwin 		memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
73702ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 	} else {
7407e05daaeSHans Petter Selasky 		memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
74102ac6454SAndrew Thompson 
74202ac6454SAndrew Thompson 		for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
74302ac6454SAndrew Thompson 			char c = eaddr_str[i];
74402ac6454SAndrew Thompson 
74502ac6454SAndrew Thompson 			if ('0' <= c && c <= '9')
74602ac6454SAndrew Thompson 				c -= '0';
74702ac6454SAndrew Thompson 			else if (c != 0)
74802ac6454SAndrew Thompson 				c -= 'A' - 10;
74902ac6454SAndrew Thompson 			else
75002ac6454SAndrew Thompson 				break;
75102ac6454SAndrew Thompson 
75202ac6454SAndrew Thompson 			c &= 0xf;
75302ac6454SAndrew Thompson 
75402ac6454SAndrew Thompson 			if ((i & 1) == 0)
75502ac6454SAndrew Thompson 				c <<= 4;
75602ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[i / 2] |= c;
75702ac6454SAndrew Thompson 		}
75802ac6454SAndrew Thompson 
759f29a0724SAndrew Thompson 		if (uaa->usb_mode == USB_MODE_DEVICE) {
76002ac6454SAndrew Thompson 			/*
76102ac6454SAndrew Thompson 			 * Do not use the same MAC address like the peer !
76202ac6454SAndrew Thompson 			 */
76302ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[5] ^= 0xFF;
76402ac6454SAndrew Thompson 		}
76502ac6454SAndrew Thompson 	}
76602ac6454SAndrew Thompson 
76702ac6454SAndrew Thompson 	ue->ue_sc = sc;
76802ac6454SAndrew Thompson 	ue->ue_dev = dev;
76902ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
77002ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
77102ac6454SAndrew Thompson 	ue->ue_methods = &cdce_ue_methods;
77202ac6454SAndrew Thompson 
773a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
77402ac6454SAndrew Thompson 	if (error) {
77502ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
77602ac6454SAndrew Thompson 		goto detach;
77702ac6454SAndrew Thompson 	}
77802ac6454SAndrew Thompson 	return (0);			/* success */
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson detach:
78102ac6454SAndrew Thompson 	cdce_detach(dev);
78202ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
78302ac6454SAndrew Thompson }
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson static int
78602ac6454SAndrew Thompson cdce_detach(device_t dev)
78702ac6454SAndrew Thompson {
78802ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
789760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson 	/* stop all USB transfers first */
792a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
793a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
79402ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
79502ac6454SAndrew Thompson 
796b43d600cSJohn-Mark Gurney 	ifmedia_removeall(&sc->sc_media);
797b43d600cSJohn-Mark Gurney 
79802ac6454SAndrew Thompson 	return (0);
79902ac6454SAndrew Thompson }
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson static void
802760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue)
80302ac6454SAndrew Thompson {
804a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
80502ac6454SAndrew Thompson 
80602ac6454SAndrew Thompson 	/*
80702ac6454SAndrew Thompson 	 * Start the USB transfers, if not already started:
80802ac6454SAndrew Thompson 	 */
809a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
810a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
81102ac6454SAndrew Thompson }
81202ac6454SAndrew Thompson 
813b43d600cSJohn-Mark Gurney static int
814b43d600cSJohn-Mark Gurney cdce_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
815b43d600cSJohn-Mark Gurney {
816b43d600cSJohn-Mark Gurney 	struct usb_ether *ue = ifp->if_softc;
817b43d600cSJohn-Mark Gurney 	struct cdce_softc *sc = uether_getsc(ue);
818b43d600cSJohn-Mark Gurney 	struct ifreq *ifr = (struct ifreq *)data;
819b43d600cSJohn-Mark Gurney 	int error;
820b43d600cSJohn-Mark Gurney 
821b43d600cSJohn-Mark Gurney 	error = 0;
822b43d600cSJohn-Mark Gurney 
823b43d600cSJohn-Mark Gurney 	switch(command) {
824b43d600cSJohn-Mark Gurney 	case SIOCGIFMEDIA:
825b43d600cSJohn-Mark Gurney 	case SIOCSIFMEDIA:
826b43d600cSJohn-Mark Gurney 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
827b43d600cSJohn-Mark Gurney 		break;
828b43d600cSJohn-Mark Gurney 	default:
829b43d600cSJohn-Mark Gurney 		error = uether_ioctl(ifp, command, data);
830b43d600cSJohn-Mark Gurney 		break;
831b43d600cSJohn-Mark Gurney 	}
832b43d600cSJohn-Mark Gurney 
833b43d600cSJohn-Mark Gurney 	return (error);
834b43d600cSJohn-Mark Gurney }
835b43d600cSJohn-Mark Gurney 
83602ac6454SAndrew Thompson static void
83702ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n)
83802ac6454SAndrew Thompson {
83902ac6454SAndrew Thompson 	uint8_t x;
84002ac6454SAndrew Thompson 	for (x = 0; x != n; x++) {
84102ac6454SAndrew Thompson 		if (ppm[x] != NULL) {
84202ac6454SAndrew Thompson 			m_freem(ppm[x]);
84302ac6454SAndrew Thompson 			ppm[x] = NULL;
84402ac6454SAndrew Thompson 		}
84502ac6454SAndrew Thompson 	}
84602ac6454SAndrew Thompson }
84702ac6454SAndrew Thompson 
848b43d600cSJohn-Mark Gurney static int
849b43d600cSJohn-Mark Gurney cdce_media_change_cb(struct ifnet *ifp)
850b43d600cSJohn-Mark Gurney {
851b43d600cSJohn-Mark Gurney 
852b43d600cSJohn-Mark Gurney 	return (EOPNOTSUPP);
853b43d600cSJohn-Mark Gurney }
854b43d600cSJohn-Mark Gurney 
855b43d600cSJohn-Mark Gurney static void
856b43d600cSJohn-Mark Gurney cdce_media_status_cb(struct ifnet *ifp, struct ifmediareq *ifmr)
857b43d600cSJohn-Mark Gurney {
858b43d600cSJohn-Mark Gurney 
859b43d600cSJohn-Mark Gurney 	if ((if_getflags(ifp) & IFF_UP) == 0)
860b43d600cSJohn-Mark Gurney 		return;
861b43d600cSJohn-Mark Gurney 
862b43d600cSJohn-Mark Gurney 	ifmr->ifm_active = IFM_ETHER;
863b43d600cSJohn-Mark Gurney 	ifmr->ifm_status = IFM_AVALID;
864b43d600cSJohn-Mark Gurney 	ifmr->ifm_status |=
865b43d600cSJohn-Mark Gurney 	    ifp->if_link_state == LINK_STATE_UP ? IFM_ACTIVE : 0;
866b43d600cSJohn-Mark Gurney }
867b43d600cSJohn-Mark Gurney 
86802ac6454SAndrew Thompson static void
869ed6d949aSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
87002ac6454SAndrew Thompson {
871ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
872a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
87302ac6454SAndrew Thompson 	struct mbuf *m;
87402ac6454SAndrew Thompson 	struct mbuf *mt;
87502ac6454SAndrew Thompson 	uint32_t crc;
87602ac6454SAndrew Thompson 	uint8_t x;
877ed6d949aSAndrew Thompson 	int actlen, aframes;
878ed6d949aSAndrew Thompson 
879ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
88002ac6454SAndrew Thompson 
88102ac6454SAndrew Thompson 	DPRINTFN(1, "\n");
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
88402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
885ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
886ed6d949aSAndrew Thompson 		    actlen, aframes);
88702ac6454SAndrew Thompson 
888ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
88902ac6454SAndrew Thompson 
89002ac6454SAndrew Thompson 		/* free all previous TX buffers */
89102ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
89202ac6454SAndrew Thompson 
89302ac6454SAndrew Thompson 		/* FALLTHROUGH */
89402ac6454SAndrew Thompson 	case USB_ST_SETUP:
89502ac6454SAndrew Thompson tr_setup:
89602ac6454SAndrew Thompson 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
89702ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
89802ac6454SAndrew Thompson 
89902ac6454SAndrew Thompson 			if (m == NULL)
90002ac6454SAndrew Thompson 				break;
90102ac6454SAndrew Thompson 
90202ac6454SAndrew Thompson 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
90302ac6454SAndrew Thompson 				/*
90402ac6454SAndrew Thompson 				 * Zaurus wants a 32-bit CRC appended
90502ac6454SAndrew Thompson 				 * to every frame
90602ac6454SAndrew Thompson 				 */
90702ac6454SAndrew Thompson 
90802ac6454SAndrew Thompson 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
90902ac6454SAndrew Thompson 				crc = htole32(crc);
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson 				if (!m_append(m, 4, (void *)&crc)) {
91202ac6454SAndrew Thompson 					m_freem(m);
913ecc70d3fSGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
91402ac6454SAndrew Thompson 					continue;
91502ac6454SAndrew Thompson 				}
91602ac6454SAndrew Thompson 			}
91702ac6454SAndrew Thompson 			if (m->m_len != m->m_pkthdr.len) {
918c6499eccSGleb Smirnoff 				mt = m_defrag(m, M_NOWAIT);
91902ac6454SAndrew Thompson 				if (mt == NULL) {
92002ac6454SAndrew Thompson 					m_freem(m);
921ecc70d3fSGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
92202ac6454SAndrew Thompson 					continue;
92302ac6454SAndrew Thompson 				}
92402ac6454SAndrew Thompson 				m = mt;
92502ac6454SAndrew Thompson 			}
92602ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
92702ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
92802ac6454SAndrew Thompson 			}
92902ac6454SAndrew Thompson 			sc->sc_tx_buf[x] = m;
930ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 			/*
93302ac6454SAndrew Thompson 			 * If there's a BPF listener, bounce a copy of
93402ac6454SAndrew Thompson 			 * this frame to him:
93502ac6454SAndrew Thompson 			 */
93602ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
93702ac6454SAndrew Thompson 		}
93802ac6454SAndrew Thompson 		if (x != 0) {
939ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
940ed6d949aSAndrew Thompson 
941a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
94202ac6454SAndrew Thompson 		}
94302ac6454SAndrew Thompson 		break;
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson 	default:			/* Error */
94602ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
947ed6d949aSAndrew Thompson 		    usbd_errstr(error));
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson 		/* free all previous TX buffers */
95002ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
95102ac6454SAndrew Thompson 
95202ac6454SAndrew Thompson 		/* count output errors */
953ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
95402ac6454SAndrew Thompson 
955ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
9567d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
95702ac6454SAndrew Thompson 				/* try to clear stall first */
958ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
95902ac6454SAndrew Thompson 			}
960d69eefebSRuslan Bukin 			goto tr_setup;
9617d502f32SRuslan Bukin 		}
96202ac6454SAndrew Thompson 		break;
96302ac6454SAndrew Thompson 	}
96402ac6454SAndrew Thompson }
96502ac6454SAndrew Thompson 
96602ac6454SAndrew Thompson static int32_t
96702ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
96802ac6454SAndrew Thompson {
96902ac6454SAndrew Thompson 	uint32_t *p_crc = arg;
97002ac6454SAndrew Thompson 
97102ac6454SAndrew Thompson 	*p_crc = crc32_raw(src, count, *p_crc);
97202ac6454SAndrew Thompson 	return (0);
97302ac6454SAndrew Thompson }
97402ac6454SAndrew Thompson 
97502ac6454SAndrew Thompson static uint32_t
97602ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
97702ac6454SAndrew Thompson {
97802ac6454SAndrew Thompson 	uint32_t crc = 0xFFFFFFFF;
97902ac6454SAndrew Thompson 
980ceb246c7SMark Johnston 	(void)m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
98102ac6454SAndrew Thompson 	return (crc ^ 0xFFFFFFFF);
98202ac6454SAndrew Thompson }
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson static void
985760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue)
98602ac6454SAndrew Thompson {
987a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
988a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
98902ac6454SAndrew Thompson 
99002ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
99102ac6454SAndrew Thompson 
99202ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
99302ac6454SAndrew Thompson 
99402ac6454SAndrew Thompson 	/* start interrupt transfer */
995a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
996a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
99702ac6454SAndrew Thompson 
998b0b74fb3SRuslan Bukin 	/*
999b0b74fb3SRuslan Bukin 	 * Stall data write direction, which depends on USB mode.
1000b0b74fb3SRuslan Bukin 	 *
1001b0b74fb3SRuslan Bukin 	 * Some USB host stacks (e.g. Mac OS X) don't clears stall
1002b0b74fb3SRuslan Bukin 	 * bit as it should, so set it in our host mode only.
1003b0b74fb3SRuslan Bukin 	 */
1004b0b74fb3SRuslan Bukin 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1005ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
100602ac6454SAndrew Thompson 
100702ac6454SAndrew Thompson 	/* start data transfers */
100802ac6454SAndrew Thompson 	cdce_start(ue);
100902ac6454SAndrew Thompson }
101002ac6454SAndrew Thompson 
101102ac6454SAndrew Thompson static void
1012760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue)
101302ac6454SAndrew Thompson {
1014a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
1015a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
101602ac6454SAndrew Thompson 
101702ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
102002ac6454SAndrew Thompson 
102102ac6454SAndrew Thompson 	/*
102202ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
102302ac6454SAndrew Thompson 	 */
1024a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
1025a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
1026a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
1027a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
102802ac6454SAndrew Thompson }
102902ac6454SAndrew Thompson 
103002ac6454SAndrew Thompson static void
1031760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue)
103202ac6454SAndrew Thompson {
1033f0c393f7SKornel Duleba 
1034f0c393f7SKornel Duleba 	cdce_set_filter(ue);
103502ac6454SAndrew Thompson }
103602ac6454SAndrew Thompson 
103702ac6454SAndrew Thompson static void
1038760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue)
103902ac6454SAndrew Thompson {
1040f0c393f7SKornel Duleba 
1041f0c393f7SKornel Duleba 	cdce_set_filter(ue);
1042f0c393f7SKornel Duleba }
1043f0c393f7SKornel Duleba 
1044f0c393f7SKornel Duleba static void
1045f0c393f7SKornel Duleba cdce_set_filter(struct usb_ether *ue)
1046f0c393f7SKornel Duleba {
1047f0c393f7SKornel Duleba 	struct cdce_softc *sc = uether_getsc(ue);
1048f0c393f7SKornel Duleba 	struct ifnet *ifp = uether_getifp(ue);
1049f0c393f7SKornel Duleba 	struct usb_device_request req;
1050f0c393f7SKornel Duleba 	uint16_t value;
1051f0c393f7SKornel Duleba 
1052f0c393f7SKornel Duleba 	value = CDC_PACKET_TYPE_DIRECTED | CDC_PACKET_TYPE_BROADCAST;
1053f0c393f7SKornel Duleba 	if (if_getflags(ifp) & IFF_PROMISC)
1054f0c393f7SKornel Duleba 		value |= CDC_PACKET_TYPE_PROMISC;
1055f0c393f7SKornel Duleba 	if (if_getflags(ifp) & IFF_ALLMULTI)
1056f0c393f7SKornel Duleba 		value |= CDC_PACKET_TYPE_ALL_MULTICAST;
1057f0c393f7SKornel Duleba 
1058f0c393f7SKornel Duleba 	req.bmRequestType = UT_CLASS | UT_INTERFACE;
1059f0c393f7SKornel Duleba 	req.bRequest = CDC_SET_ETHERNET_PACKET_FILTER;
1060f0c393f7SKornel Duleba 	USETW(req.wValue, value);
1061f0c393f7SKornel Duleba 	req.wIndex[0] = sc->sc_ifaces_index[1];
1062f0c393f7SKornel Duleba 	req.wIndex[1] = 0;
1063f0c393f7SKornel Duleba 	USETW(req.wLength, 0);
1064f0c393f7SKornel Duleba 
1065f0c393f7SKornel Duleba 	/*
1066f0c393f7SKornel Duleba 	 * Function below will drop the sc mutex.
1067f0c393f7SKornel Duleba 	 * We can do that since we're called from a separate task,
1068f0c393f7SKornel Duleba 	 * that simply wraps the setpromisc/setmulti methods.
1069f0c393f7SKornel Duleba 	 */
1070f0c393f7SKornel Duleba 	usbd_do_request(sc->sc_ue.ue_udev, &sc->sc_mtx, &req, NULL);
107102ac6454SAndrew Thompson }
107202ac6454SAndrew Thompson 
107302ac6454SAndrew Thompson static int
107402ac6454SAndrew Thompson cdce_suspend(device_t dev)
107502ac6454SAndrew Thompson {
107602ac6454SAndrew Thompson 	device_printf(dev, "Suspending\n");
107702ac6454SAndrew Thompson 	return (0);
107802ac6454SAndrew Thompson }
107902ac6454SAndrew Thompson 
108002ac6454SAndrew Thompson static int
108102ac6454SAndrew Thompson cdce_resume(device_t dev)
108202ac6454SAndrew Thompson {
108302ac6454SAndrew Thompson 	device_printf(dev, "Resuming\n");
108402ac6454SAndrew Thompson 	return (0);
108502ac6454SAndrew Thompson }
108602ac6454SAndrew Thompson 
108702ac6454SAndrew Thompson static void
1088ed6d949aSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
108902ac6454SAndrew Thompson {
1090ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
109102ac6454SAndrew Thompson 	struct mbuf *m;
109202ac6454SAndrew Thompson 	uint8_t x;
10936d917491SHans Petter Selasky 	int actlen;
10946d917491SHans Petter Selasky 	int aframes;
10956d917491SHans Petter Selasky 	int len;
1096ed6d949aSAndrew Thompson 
1097ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
109802ac6454SAndrew Thompson 
109902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
110002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
110102ac6454SAndrew Thompson 
1102ed6d949aSAndrew Thompson 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
110302ac6454SAndrew Thompson 
1104ed6d949aSAndrew Thompson 		for (x = 0; x != aframes; x++) {
110502ac6454SAndrew Thompson 			m = sc->sc_rx_buf[x];
110602ac6454SAndrew Thompson 			sc->sc_rx_buf[x] = NULL;
11078f9e0ef9SAndrew Thompson 			len = usbd_xfer_frame_len(xfer, x);
110802ac6454SAndrew Thompson 
110902ac6454SAndrew Thompson 			/* Strip off CRC added by Zaurus, if any */
1110ed6d949aSAndrew Thompson 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
1111ed6d949aSAndrew Thompson 				len -= 4;
111202ac6454SAndrew Thompson 
11136d917491SHans Petter Selasky 			if (len < (int)sizeof(struct ether_header)) {
111402ac6454SAndrew Thompson 				m_freem(m);
111502ac6454SAndrew Thompson 				continue;
111602ac6454SAndrew Thompson 			}
111702ac6454SAndrew Thompson 			/* queue up mbuf */
1118ed6d949aSAndrew Thompson 			uether_rxmbuf(&sc->sc_ue, m, len);
111902ac6454SAndrew Thompson 		}
112002ac6454SAndrew Thompson 
112102ac6454SAndrew Thompson 		/* FALLTHROUGH */
112202ac6454SAndrew Thompson 	case USB_ST_SETUP:
112302ac6454SAndrew Thompson 		/*
112402ac6454SAndrew Thompson 		 * TODO: Implement support for multi frame transfers,
112502ac6454SAndrew Thompson 		 * when the USB hardware supports it.
112602ac6454SAndrew Thompson 		 */
112702ac6454SAndrew Thompson 		for (x = 0; x != 1; x++) {
112802ac6454SAndrew Thompson 			if (sc->sc_rx_buf[x] == NULL) {
1129a593f6b8SAndrew Thompson 				m = uether_newbuf();
113002ac6454SAndrew Thompson 				if (m == NULL)
113102ac6454SAndrew Thompson 					goto tr_stall;
113202ac6454SAndrew Thompson 				sc->sc_rx_buf[x] = m;
113302ac6454SAndrew Thompson 			} else {
113402ac6454SAndrew Thompson 				m = sc->sc_rx_buf[x];
113502ac6454SAndrew Thompson 			}
113602ac6454SAndrew Thompson 
1137ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
113802ac6454SAndrew Thompson 		}
113902ac6454SAndrew Thompson 		/* set number of frames and start hardware */
1140ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, x);
1141a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
114202ac6454SAndrew Thompson 		/* flush any received frames */
1143a593f6b8SAndrew Thompson 		uether_rxflush(&sc->sc_ue);
114402ac6454SAndrew Thompson 		break;
114502ac6454SAndrew Thompson 
114602ac6454SAndrew Thompson 	default:			/* Error */
114702ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
1148ed6d949aSAndrew Thompson 		    usbd_errstr(error));
114902ac6454SAndrew Thompson 
1150ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
115102ac6454SAndrew Thompson tr_stall:
11527d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
115302ac6454SAndrew Thompson 				/* try to clear stall first */
1154ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
1155ed6d949aSAndrew Thompson 				usbd_xfer_set_frames(xfer, 0);
1156a593f6b8SAndrew Thompson 				usbd_transfer_submit(xfer);
11577d502f32SRuslan Bukin 			}
115802ac6454SAndrew Thompson 			break;
115902ac6454SAndrew Thompson 		}
116002ac6454SAndrew Thompson 
116102ac6454SAndrew Thompson 		/* need to free the RX-mbufs when we are cancelled */
116202ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
116302ac6454SAndrew Thompson 		break;
116402ac6454SAndrew Thompson 	}
116502ac6454SAndrew Thompson }
116602ac6454SAndrew Thompson 
116702ac6454SAndrew Thompson static void
1168ed6d949aSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
116902ac6454SAndrew Thompson {
1170b43d600cSJohn-Mark Gurney 	u_char buf[CDCE_IND_SIZE_MAX];
1171b43d600cSJohn-Mark Gurney 	struct usb_cdc_notification ucn;
1172b43d600cSJohn-Mark Gurney 	struct cdce_softc *sc;
1173b43d600cSJohn-Mark Gurney 	struct ifnet *ifp;
1174b43d600cSJohn-Mark Gurney 	struct usb_page_cache *pc;
1175b43d600cSJohn-Mark Gurney 	int off, actlen;
1176b43d600cSJohn-Mark Gurney 	uint32_t downrate, uprate;
1177b43d600cSJohn-Mark Gurney 
1178b43d600cSJohn-Mark Gurney 	sc = usbd_xfer_softc(xfer);
1179b43d600cSJohn-Mark Gurney 	ifp = uether_getifp(&sc->sc_ue);
1180b43d600cSJohn-Mark Gurney 	pc = usbd_xfer_get_frame(xfer, 0);
1181ed6d949aSAndrew Thompson 
1182ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1183ed6d949aSAndrew Thompson 
118402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
118502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
1186b43d600cSJohn-Mark Gurney 		if (USB_DEBUG_VAR)
1187b43d600cSJohn-Mark Gurney 			usbd_copy_out(pc, 0, buf, MIN(actlen, sizeof buf));
1188b43d600cSJohn-Mark Gurney 		DPRINTF("Received %d bytes: %*D\n", actlen,
1189b43d600cSJohn-Mark Gurney 		    (int)MIN(actlen, sizeof buf), buf, "");
119002ac6454SAndrew Thompson 
1191b43d600cSJohn-Mark Gurney 		off = 0;
1192b43d600cSJohn-Mark Gurney 		while (actlen - off >= UCDC_NOTIFICATION_LENGTH) {
1193b43d600cSJohn-Mark Gurney 			usbd_copy_out(pc, off, &ucn, UCDC_NOTIFICATION_LENGTH);
119402ac6454SAndrew Thompson 
1195b43d600cSJohn-Mark Gurney 			do {
1196b43d600cSJohn-Mark Gurney 				if (ucn.bmRequestType != 0xa1)
1197b43d600cSJohn-Mark Gurney 					break;
1198b43d600cSJohn-Mark Gurney 
1199b43d600cSJohn-Mark Gurney 				switch (ucn.bNotification) {
1200b43d600cSJohn-Mark Gurney 				case UCDC_N_NETWORK_CONNECTION:
1201b43d600cSJohn-Mark Gurney 					DPRINTF("changing link state: %d\n",
1202b43d600cSJohn-Mark Gurney 					    UGETW(ucn.wValue));
1203b43d600cSJohn-Mark Gurney 					if_link_state_change(ifp,
1204b43d600cSJohn-Mark Gurney 					    UGETW(ucn.wValue) ? LINK_STATE_UP :
1205b43d600cSJohn-Mark Gurney 					    LINK_STATE_DOWN);
1206b43d600cSJohn-Mark Gurney 					break;
1207b43d600cSJohn-Mark Gurney 
1208b43d600cSJohn-Mark Gurney 				case UCDC_N_CONNECTION_SPEED_CHANGE:
1209b43d600cSJohn-Mark Gurney 					if (UGETW(ucn.wLength) != 8)
1210b43d600cSJohn-Mark Gurney 						break;
1211b43d600cSJohn-Mark Gurney 
1212b43d600cSJohn-Mark Gurney 					usbd_copy_out(pc, off +
1213b43d600cSJohn-Mark Gurney 					    UCDC_NOTIFICATION_LENGTH,
1214b43d600cSJohn-Mark Gurney 					    &ucn.data, UGETW(ucn.wLength));
1215b43d600cSJohn-Mark Gurney 					downrate = UGETDW(ucn.data);
1216b43d600cSJohn-Mark Gurney 					uprate = UGETDW(ucn.data);
1217b43d600cSJohn-Mark Gurney 					if (downrate != uprate)
1218b43d600cSJohn-Mark Gurney 						break;
1219b43d600cSJohn-Mark Gurney 
1220b43d600cSJohn-Mark Gurney 					/* set rate */
1221b43d600cSJohn-Mark Gurney 					DPRINTF("changing baudrate: %u\n",
1222b43d600cSJohn-Mark Gurney 					    downrate);
1223b43d600cSJohn-Mark Gurney 					if_setbaudrate(ifp, downrate);
1224b43d600cSJohn-Mark Gurney 					break;
1225b43d600cSJohn-Mark Gurney 
1226b43d600cSJohn-Mark Gurney 				default:
1227b43d600cSJohn-Mark Gurney 					break;
1228b43d600cSJohn-Mark Gurney 				}
1229b43d600cSJohn-Mark Gurney 			} while (0);
1230b43d600cSJohn-Mark Gurney 
1231b43d600cSJohn-Mark Gurney 			off += UCDC_NOTIFICATION_LENGTH + UGETW(ucn.wLength);
1232b43d600cSJohn-Mark Gurney 		}
123302ac6454SAndrew Thompson 
123402ac6454SAndrew Thompson 		/* FALLTHROUGH */
123502ac6454SAndrew Thompson 	case USB_ST_SETUP:
123602ac6454SAndrew Thompson tr_setup:
1237ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1238a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
123902ac6454SAndrew Thompson 		break;
124002ac6454SAndrew Thompson 
124102ac6454SAndrew Thompson 	default:			/* Error */
1242ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
124302ac6454SAndrew Thompson 			/* start clear stall */
12447d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1245ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
124602ac6454SAndrew Thompson 			goto tr_setup;
124702ac6454SAndrew Thompson 		}
124802ac6454SAndrew Thompson 		break;
124902ac6454SAndrew Thompson 	}
125002ac6454SAndrew Thompson }
125102ac6454SAndrew Thompson 
125202ac6454SAndrew Thompson static void
1253ed6d949aSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
125402ac6454SAndrew Thompson {
1255b0b74fb3SRuslan Bukin 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
1256b0b74fb3SRuslan Bukin 	struct usb_cdc_notification req;
1257b0b74fb3SRuslan Bukin 	struct usb_page_cache *pc;
1258b0b74fb3SRuslan Bukin 	uint32_t speed;
1259ed6d949aSAndrew Thompson 	int actlen;
1260ed6d949aSAndrew Thompson 
1261ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1262ed6d949aSAndrew Thompson 
126302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
126402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
126502ac6454SAndrew Thompson 
1266ed6d949aSAndrew Thompson 		DPRINTF("Transferred %d bytes\n", actlen);
126702ac6454SAndrew Thompson 
12687d502f32SRuslan Bukin 		switch (sc->sc_notify_state) {
1269d69eefebSRuslan Bukin 		case CDCE_NOTIFY_NETWORK_CONNECTION:
12707d502f32SRuslan Bukin 			sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
12717d502f32SRuslan Bukin 			break;
1272d69eefebSRuslan Bukin 		case CDCE_NOTIFY_SPEED_CHANGE:
12737d502f32SRuslan Bukin 			sc->sc_notify_state = CDCE_NOTIFY_DONE;
12747d502f32SRuslan Bukin 			break;
12757d502f32SRuslan Bukin 		default:
12767d502f32SRuslan Bukin 			break;
12777d502f32SRuslan Bukin 		}
12787d502f32SRuslan Bukin 
127902ac6454SAndrew Thompson 		/* FALLTHROUGH */
128002ac6454SAndrew Thompson 	case USB_ST_SETUP:
128102ac6454SAndrew Thompson tr_setup:
1282b0b74fb3SRuslan Bukin 		/*
1283b0b74fb3SRuslan Bukin 		 * Inform host about connection. Required according to USB CDC
1284b0b74fb3SRuslan Bukin 		 * specification and communicating to Mac OS X USB host stack.
1285b0b74fb3SRuslan Bukin 		 * Some of the values seems ignored by Mac OS X though.
1286b0b74fb3SRuslan Bukin 		 */
1287b0b74fb3SRuslan Bukin 		if (sc->sc_notify_state == CDCE_NOTIFY_NETWORK_CONNECTION) {
1288b0b74fb3SRuslan Bukin 			req.bmRequestType = UCDC_NOTIFICATION;
1289b0b74fb3SRuslan Bukin 			req.bNotification = UCDC_N_NETWORK_CONNECTION;
1290b0b74fb3SRuslan Bukin 			req.wIndex[0] = sc->sc_ifaces_index[1];
1291b0b74fb3SRuslan Bukin 			req.wIndex[1] = 0;
1292b0b74fb3SRuslan Bukin 			USETW(req.wValue, 1); /* Connected */
1293b0b74fb3SRuslan Bukin 			USETW(req.wLength, 0);
1294b0b74fb3SRuslan Bukin 
1295b0b74fb3SRuslan Bukin 			pc = usbd_xfer_get_frame(xfer, 0);
1296b0b74fb3SRuslan Bukin 			usbd_copy_in(pc, 0, &req, sizeof(req));
1297b0b74fb3SRuslan Bukin 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1298b0b74fb3SRuslan Bukin 			usbd_xfer_set_frames(xfer, 1);
1299a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
1300b0b74fb3SRuslan Bukin 
1301b0b74fb3SRuslan Bukin 		} else if (sc->sc_notify_state == CDCE_NOTIFY_SPEED_CHANGE) {
1302b0b74fb3SRuslan Bukin 			req.bmRequestType = UCDC_NOTIFICATION;
1303b0b74fb3SRuslan Bukin 			req.bNotification = UCDC_N_CONNECTION_SPEED_CHANGE;
1304b0b74fb3SRuslan Bukin 			req.wIndex[0] = sc->sc_ifaces_index[1];
1305b0b74fb3SRuslan Bukin 			req.wIndex[1] = 0;
1306b0b74fb3SRuslan Bukin 			USETW(req.wValue, 0);
1307b0b74fb3SRuslan Bukin 			USETW(req.wLength, 8);
1308b0b74fb3SRuslan Bukin 
1309b0b74fb3SRuslan Bukin 			/* Peak theoretical bulk trasfer rate in bits/s */
13107d502f32SRuslan Bukin 			if (usbd_get_speed(sc->sc_ue.ue_udev) != USB_SPEED_FULL)
1311b0b74fb3SRuslan Bukin 				speed = (13 * 512 * 8 * 1000 * 8);
1312b0b74fb3SRuslan Bukin 			else
1313b0b74fb3SRuslan Bukin 				speed = (19 * 64 * 1 * 1000 * 8);
1314b0b74fb3SRuslan Bukin 
1315b0b74fb3SRuslan Bukin 			USETDW(req.data + 0, speed); /* Upstream bit rate */
1316b0b74fb3SRuslan Bukin 			USETDW(req.data + 4, speed); /* Downstream bit rate */
1317b0b74fb3SRuslan Bukin 
1318b0b74fb3SRuslan Bukin 			pc = usbd_xfer_get_frame(xfer, 0);
1319b0b74fb3SRuslan Bukin 			usbd_copy_in(pc, 0, &req, sizeof(req));
1320b0b74fb3SRuslan Bukin 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1321b0b74fb3SRuslan Bukin 			usbd_xfer_set_frames(xfer, 1);
1322b0b74fb3SRuslan Bukin 			usbd_transfer_submit(xfer);
1323b0b74fb3SRuslan Bukin 		}
132402ac6454SAndrew Thompson 		break;
132502ac6454SAndrew Thompson 
132602ac6454SAndrew Thompson 	default:			/* Error */
1327ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
13287d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
132902ac6454SAndrew Thompson 				/* start clear stall */
1330ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
133102ac6454SAndrew Thompson 			}
1332d69eefebSRuslan Bukin 			goto tr_setup;
13337d502f32SRuslan Bukin 		}
133402ac6454SAndrew Thompson 		break;
133502ac6454SAndrew Thompson 	}
133602ac6454SAndrew Thompson }
133702ac6454SAndrew Thompson 
133802ac6454SAndrew Thompson static int
133902ac6454SAndrew Thompson cdce_handle_request(device_t dev,
13407d502f32SRuslan Bukin     const void *preq, void **pptr, uint16_t *plen,
134129bd7d7eSAndrew Thompson     uint16_t offset, uint8_t *pstate)
134202ac6454SAndrew Thompson {
13437d502f32SRuslan Bukin 	struct cdce_softc *sc = device_get_softc(dev);
13447d502f32SRuslan Bukin 	const struct usb_device_request *req = preq;
13457d502f32SRuslan Bukin 	uint8_t is_complete = *pstate;
13467d502f32SRuslan Bukin 
13477d502f32SRuslan Bukin 	/*
13487d502f32SRuslan Bukin 	 * When Mac OS X resumes after suspending it expects
13497d502f32SRuslan Bukin 	 * to be notified again after this request.
13507d502f32SRuslan Bukin 	 */
13517d502f32SRuslan Bukin 	if (req->bmRequestType == UT_WRITE_CLASS_INTERFACE && \
13527d502f32SRuslan Bukin 	    req->bRequest == UCDC_NCM_SET_ETHERNET_PACKET_FILTER) {
13537d502f32SRuslan Bukin 		if (is_complete == 1) {
13547d502f32SRuslan Bukin 			mtx_lock(&sc->sc_mtx);
13557d502f32SRuslan Bukin 			sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
13567d502f32SRuslan Bukin 			usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
13577d502f32SRuslan Bukin 			mtx_unlock(&sc->sc_mtx);
13587d502f32SRuslan Bukin 		}
13597d502f32SRuslan Bukin 
13607d502f32SRuslan Bukin 		return (0);
13617d502f32SRuslan Bukin 	}
13627d502f32SRuslan Bukin 
136302ac6454SAndrew Thompson 	return (ENXIO);			/* use builtin handler */
136402ac6454SAndrew Thompson }
13654076dd23SAndrew Thompson 
13664076dd23SAndrew Thompson #if CDCE_HAVE_NCM
13677e05daaeSHans Petter Selasky static void
13687e05daaeSHans Petter Selasky cdce_ncm_tx_zero(struct usb_page_cache *pc,
13697e05daaeSHans Petter Selasky     uint32_t start, uint32_t end)
13707e05daaeSHans Petter Selasky {
13717e05daaeSHans Petter Selasky 	if (start >= CDCE_NCM_TX_MAXLEN)
13727e05daaeSHans Petter Selasky 		return;
13737e05daaeSHans Petter Selasky 	if (end > CDCE_NCM_TX_MAXLEN)
13747e05daaeSHans Petter Selasky 		end = CDCE_NCM_TX_MAXLEN;
13757e05daaeSHans Petter Selasky 
13767e05daaeSHans Petter Selasky 	usbd_frame_zero(pc, start, end - start);
13777e05daaeSHans Petter Selasky }
13787e05daaeSHans Petter Selasky 
13794076dd23SAndrew Thompson static uint8_t
13804076dd23SAndrew Thompson cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
13814076dd23SAndrew Thompson {
13824076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
13834076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
13844076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
13854076dd23SAndrew Thompson 	struct mbuf *m;
13864076dd23SAndrew Thompson 	uint32_t rem;
13874076dd23SAndrew Thompson 	uint32_t offset;
13884076dd23SAndrew Thompson 	uint32_t last_offset;
13897e05daaeSHans Petter Selasky 	uint16_t n;
13907e05daaeSHans Petter Selasky 	uint8_t retval;
13914076dd23SAndrew Thompson 
13924076dd23SAndrew Thompson 	usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
13934076dd23SAndrew Thompson 
13944076dd23SAndrew Thompson 	offset = sizeof(sc->sc_ncm.hdr) +
13954076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
13964076dd23SAndrew Thompson 
13974076dd23SAndrew Thompson 	/* Store last valid offset before alignment */
13984076dd23SAndrew Thompson 	last_offset = offset;
13994076dd23SAndrew Thompson 
14007e05daaeSHans Petter Selasky 	/* Align offset */
14017e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
14027e05daaeSHans Petter Selasky 	    offset, sc->sc_ncm.tx_modulus);
14034076dd23SAndrew Thompson 
14047e05daaeSHans Petter Selasky 	/* Zero pad */
14057e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
14067e05daaeSHans Petter Selasky 
14077e05daaeSHans Petter Selasky 	/* buffer full */
14087e05daaeSHans Petter Selasky 	retval = 2;
14097e05daaeSHans Petter Selasky 
14107e05daaeSHans Petter Selasky 	for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
14114076dd23SAndrew Thompson 		/* check if end of transmit buffer is reached */
14124076dd23SAndrew Thompson 
14134076dd23SAndrew Thompson 		if (offset >= sc->sc_ncm.tx_max)
14144076dd23SAndrew Thompson 			break;
14154076dd23SAndrew Thompson 
14164076dd23SAndrew Thompson 		/* compute maximum buffer size */
14174076dd23SAndrew Thompson 
14184076dd23SAndrew Thompson 		rem = sc->sc_ncm.tx_max - offset;
14194076dd23SAndrew Thompson 
14204076dd23SAndrew Thompson 		IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
14214076dd23SAndrew Thompson 
14227e05daaeSHans Petter Selasky 		if (m == NULL) {
14237e05daaeSHans Petter Selasky 			/* buffer not full */
14247e05daaeSHans Petter Selasky 			retval = 1;
14254076dd23SAndrew Thompson 			break;
14267e05daaeSHans Petter Selasky 		}
14274076dd23SAndrew Thompson 
14286d917491SHans Petter Selasky 		if (m->m_pkthdr.len > (int)rem) {
14294076dd23SAndrew Thompson 			if (n == 0) {
14304076dd23SAndrew Thompson 				/* The frame won't fit in our buffer */
14314076dd23SAndrew Thompson 				DPRINTFN(1, "Frame too big to be transmitted!\n");
14324076dd23SAndrew Thompson 				m_freem(m);
1433ecc70d3fSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
14344076dd23SAndrew Thompson 				n--;
14354076dd23SAndrew Thompson 				continue;
14364076dd23SAndrew Thompson 			}
14374076dd23SAndrew Thompson 			/* Wait till next buffer becomes ready */
14384076dd23SAndrew Thompson 			IFQ_DRV_PREPEND(&(ifp->if_snd), m);
14394076dd23SAndrew Thompson 			break;
14404076dd23SAndrew Thompson 		}
14414076dd23SAndrew Thompson 		usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
14424076dd23SAndrew Thompson 
14434076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
14444076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
14454076dd23SAndrew Thompson 
14464076dd23SAndrew Thompson 		/* Update offset */
14474076dd23SAndrew Thompson 		offset += m->m_pkthdr.len;
14484076dd23SAndrew Thompson 
14494076dd23SAndrew Thompson 		/* Store last valid offset before alignment */
14504076dd23SAndrew Thompson 		last_offset = offset;
14514076dd23SAndrew Thompson 
14527e05daaeSHans Petter Selasky 		/* Align offset */
14537e05daaeSHans Petter Selasky 		offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
14547e05daaeSHans Petter Selasky 		    offset, sc->sc_ncm.tx_modulus);
14557e05daaeSHans Petter Selasky 
14567e05daaeSHans Petter Selasky 		/* Zero pad */
14577e05daaeSHans Petter Selasky 		cdce_ncm_tx_zero(pc, last_offset, offset);
14584076dd23SAndrew Thompson 
14594076dd23SAndrew Thompson 		/*
14604076dd23SAndrew Thompson 		 * If there's a BPF listener, bounce a copy
14614076dd23SAndrew Thompson 		 * of this frame to him:
14624076dd23SAndrew Thompson 		 */
14634076dd23SAndrew Thompson 		BPF_MTAP(ifp, m);
14644076dd23SAndrew Thompson 
14654076dd23SAndrew Thompson 		/* Free mbuf */
14664076dd23SAndrew Thompson 
14674076dd23SAndrew Thompson 		m_freem(m);
14684076dd23SAndrew Thompson 
14694076dd23SAndrew Thompson 		/* Pre-increment interface counter */
14704076dd23SAndrew Thompson 
1471ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
14724076dd23SAndrew Thompson 	}
14734076dd23SAndrew Thompson 
14744076dd23SAndrew Thompson 	if (n == 0)
14757e05daaeSHans Petter Selasky 		return (0);
14764076dd23SAndrew Thompson 
14774076dd23SAndrew Thompson 	rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
14784076dd23SAndrew Thompson 
14794076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wLength, rem);
14804076dd23SAndrew Thompson 
14814076dd23SAndrew Thompson 	/* zero the rest of the data pointer entries */
14824076dd23SAndrew Thompson 	for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
14834076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
14844076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
14854076dd23SAndrew Thompson 	}
14864076dd23SAndrew Thompson 
14877e05daaeSHans Petter Selasky 	offset = last_offset;
14887e05daaeSHans Petter Selasky 
14897e05daaeSHans Petter Selasky 	/* Align offset */
14907e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
14917e05daaeSHans Petter Selasky 
14927e05daaeSHans Petter Selasky 	/* Optimise, save bandwidth and force short termination */
14937e05daaeSHans Petter Selasky 	if (offset >= sc->sc_ncm.tx_max)
14947e05daaeSHans Petter Selasky 		offset = sc->sc_ncm.tx_max;
14957e05daaeSHans Petter Selasky 	else
14967e05daaeSHans Petter Selasky 		offset ++;
14977e05daaeSHans Petter Selasky 
14987e05daaeSHans Petter Selasky 	/* Zero pad */
14997e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
15007e05daaeSHans Petter Selasky 
15014076dd23SAndrew Thompson 	/* set frame length */
15027e05daaeSHans Petter Selasky 	usbd_xfer_set_frame_len(xfer, index, offset);
15034076dd23SAndrew Thompson 
15044076dd23SAndrew Thompson 	/* Fill out 16-bit header */
15054076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[0] = 'N';
15064076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[1] = 'C';
15074076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[2] = 'M';
15084076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[3] = 'H';
15094076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
15107e05daaeSHans Petter Selasky 	USETW(sc->sc_ncm.hdr.wBlockLength, offset);
15114076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
15124076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
15134076dd23SAndrew Thompson 
15144076dd23SAndrew Thompson 	sc->sc_ncm.tx_seq++;
15154076dd23SAndrew Thompson 
15164076dd23SAndrew Thompson 	/* Fill out 16-bit frame table header */
15174076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[0] = 'N';
15184076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[1] = 'C';
15194076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[2] = 'M';
152085e3d588SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[3] = '0';
15214076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0);		/* reserved */
15224076dd23SAndrew Thompson 
15234076dd23SAndrew Thompson 	usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
15244076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
15254076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt));
15264076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
15274076dd23SAndrew Thompson 	    &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
15287e05daaeSHans Petter Selasky 	return (retval);
15294076dd23SAndrew Thompson }
15304076dd23SAndrew Thompson 
15314076dd23SAndrew Thompson static void
15324076dd23SAndrew Thompson cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
15334076dd23SAndrew Thompson {
15344076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
15354076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
15364076dd23SAndrew Thompson 	uint16_t x;
15377e05daaeSHans Petter Selasky 	uint8_t temp;
15384076dd23SAndrew Thompson 	int actlen;
15394076dd23SAndrew Thompson 	int aframes;
15404076dd23SAndrew Thompson 
15414076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
15424076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
15434076dd23SAndrew Thompson 
15444076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
15454076dd23SAndrew Thompson 
15464076dd23SAndrew Thompson 		DPRINTFN(10, "transfer complete: "
15474076dd23SAndrew Thompson 		    "%u bytes in %u frames\n", actlen, aframes);
15484076dd23SAndrew Thompson 
15494076dd23SAndrew Thompson 	case USB_ST_SETUP:
15504076dd23SAndrew Thompson 		for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
15517e05daaeSHans Petter Selasky 			temp = cdce_ncm_fill_tx_frames(xfer, x);
15527e05daaeSHans Petter Selasky 			if (temp == 0)
15534076dd23SAndrew Thompson 				break;
15547e05daaeSHans Petter Selasky 			if (temp == 1) {
15557e05daaeSHans Petter Selasky 				x++;
15567e05daaeSHans Petter Selasky 				break;
15577e05daaeSHans Petter Selasky 			}
15584076dd23SAndrew Thompson 		}
15594076dd23SAndrew Thompson 
15604076dd23SAndrew Thompson 		if (x != 0) {
15617e05daaeSHans Petter Selasky #ifdef USB_DEBUG
15627e05daaeSHans Petter Selasky 			usbd_xfer_set_interval(xfer, cdce_tx_interval);
15637e05daaeSHans Petter Selasky #endif
15644076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
15654076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
15664076dd23SAndrew Thompson 		}
15674076dd23SAndrew Thompson 		break;
15684076dd23SAndrew Thompson 
15694076dd23SAndrew Thompson 	default:			/* Error */
15704076dd23SAndrew Thompson 		DPRINTFN(10, "Transfer error: %s\n",
15714076dd23SAndrew Thompson 		    usbd_errstr(error));
15724076dd23SAndrew Thompson 
15734076dd23SAndrew Thompson 		/* update error counter */
1574ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
15754076dd23SAndrew Thompson 
15764076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
15777d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
15784076dd23SAndrew Thompson 				/* try to clear stall first */
15794076dd23SAndrew Thompson 				usbd_xfer_set_stall(xfer);
15804076dd23SAndrew Thompson 				usbd_xfer_set_frames(xfer, 0);
15814076dd23SAndrew Thompson 				usbd_transfer_submit(xfer);
15824076dd23SAndrew Thompson 			}
15837d502f32SRuslan Bukin 		}
15844076dd23SAndrew Thompson 		break;
15854076dd23SAndrew Thompson 	}
15864076dd23SAndrew Thompson }
15874076dd23SAndrew Thompson 
15884076dd23SAndrew Thompson static void
15894076dd23SAndrew Thompson cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
15904076dd23SAndrew Thompson {
15914076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
15924076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
15934076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
15944076dd23SAndrew Thompson 	struct mbuf *m;
1595ffd8101eSJohn Baldwin 	int sumdata __usbdebug_used;
15964076dd23SAndrew Thompson 	int sumlen;
15974076dd23SAndrew Thompson 	int actlen;
15984076dd23SAndrew Thompson 	int aframes;
15994076dd23SAndrew Thompson 	int temp;
16004076dd23SAndrew Thompson 	int nframes;
16014076dd23SAndrew Thompson 	int x;
16024076dd23SAndrew Thompson 	int offset;
16034076dd23SAndrew Thompson 
16044076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
16054076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
16064076dd23SAndrew Thompson 
16074076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
16084076dd23SAndrew Thompson 
16094076dd23SAndrew Thompson 		DPRINTFN(1, "received %u bytes in %u frames\n",
16104076dd23SAndrew Thompson 		    actlen, aframes);
16114076dd23SAndrew Thompson 
16126d917491SHans Petter Selasky 		if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
16134076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt))) {
16144076dd23SAndrew Thompson 			DPRINTFN(1, "frame too short\n");
161585e3d588SAndrew Thompson 			goto tr_setup;
16164076dd23SAndrew Thompson 		}
16174076dd23SAndrew Thompson 		usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
16184076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.hdr));
16194076dd23SAndrew Thompson 
16204076dd23SAndrew Thompson 		if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
16214076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
16224076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
16234076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
162485e3d588SAndrew Thompson 			DPRINTFN(1, "invalid HDR signature: "
162585e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
162685e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[0],
162785e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[1],
162885e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[2],
162985e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[3]);
16304076dd23SAndrew Thompson 			goto tr_stall;
16314076dd23SAndrew Thompson 		}
16324076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
16334076dd23SAndrew Thompson 		if (temp > sumlen) {
16344076dd23SAndrew Thompson 			DPRINTFN(1, "unsupported block length %u/%u\n",
16354076dd23SAndrew Thompson 			    temp, sumlen);
16364076dd23SAndrew Thompson 			goto tr_stall;
16374076dd23SAndrew Thompson 		}
16384076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
16396d917491SHans Petter Selasky 		if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
164085e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
16414076dd23SAndrew Thompson 			goto tr_stall;
16424076dd23SAndrew Thompson 		}
16434076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
16444076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt));
16454076dd23SAndrew Thompson 
16464076dd23SAndrew Thompson 		if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
16474076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
16484076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
164985e3d588SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
165085e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT signature"
165185e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
165285e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[0],
165385e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[1],
165485e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[2],
165585e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[3]);
16564076dd23SAndrew Thompson 			goto tr_stall;
16574076dd23SAndrew Thompson 		}
16584076dd23SAndrew Thompson 		nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
16594076dd23SAndrew Thompson 
16604076dd23SAndrew Thompson 		/* Subtract size of header and last zero padded entry */
16614076dd23SAndrew Thompson 		if (nframes >= (2 + 1))
16624076dd23SAndrew Thompson 			nframes -= (2 + 1);
16634076dd23SAndrew Thompson 		else
16644076dd23SAndrew Thompson 			nframes = 0;
16654076dd23SAndrew Thompson 
16664076dd23SAndrew Thompson 		DPRINTFN(1, "nframes = %u\n", nframes);
16674076dd23SAndrew Thompson 
16684076dd23SAndrew Thompson 		temp += sizeof(sc->sc_ncm.dpt);
16694076dd23SAndrew Thompson 
16704076dd23SAndrew Thompson 		if ((temp + (4 * nframes)) > actlen)
16714076dd23SAndrew Thompson 			goto tr_stall;
16724076dd23SAndrew Thompson 
16734076dd23SAndrew Thompson 		if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
16744076dd23SAndrew Thompson 			DPRINTFN(1, "Truncating number of frames from %u to %u\n",
16754076dd23SAndrew Thompson 			    nframes, CDCE_NCM_SUBFRAMES_MAX);
16764076dd23SAndrew Thompson 			nframes = CDCE_NCM_SUBFRAMES_MAX;
16774076dd23SAndrew Thompson 		}
16784076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
16794076dd23SAndrew Thompson 
16804076dd23SAndrew Thompson 		sumdata = 0;
16814076dd23SAndrew Thompson 
16824076dd23SAndrew Thompson 		for (x = 0; x != nframes; x++) {
16834076dd23SAndrew Thompson 			offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
16844076dd23SAndrew Thompson 			temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
16854076dd23SAndrew Thompson 
168629051223SAndrew Thompson 			if ((offset == 0) ||
16876d917491SHans Petter Selasky 			    (temp < (int)sizeof(struct ether_header)) ||
168829051223SAndrew Thompson 			    (temp > (MCLBYTES - ETHER_ALIGN))) {
168929051223SAndrew Thompson 				DPRINTFN(1, "NULL frame detected at %d\n", x);
16904076dd23SAndrew Thompson 				m = NULL;
169129051223SAndrew Thompson 				/* silently ignore this frame */
16924076dd23SAndrew Thompson 				continue;
169329051223SAndrew Thompson 			} else if ((offset + temp) > actlen) {
169429051223SAndrew Thompson 				DPRINTFN(1, "invalid frame "
169529051223SAndrew Thompson 				    "detected at %d\n", x);
169629051223SAndrew Thompson 				m = NULL;
169729051223SAndrew Thompson 				/* silently ignore this frame */
169829051223SAndrew Thompson 				continue;
16996d917491SHans Petter Selasky 			} else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1700c6499eccSGleb Smirnoff 				m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
17014076dd23SAndrew Thompson 			} else {
1702c6499eccSGleb Smirnoff 				m = m_gethdr(M_NOWAIT, MT_DATA);
17034076dd23SAndrew Thompson 			}
17044076dd23SAndrew Thompson 
17054076dd23SAndrew Thompson 			DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
17064076dd23SAndrew Thompson 			    x, offset, temp);
17074076dd23SAndrew Thompson 
17084076dd23SAndrew Thompson 			/* check if we have a buffer */
17094076dd23SAndrew Thompson 			if (m) {
1710a51f9801SHans Petter Selasky 				m->m_len = m->m_pkthdr.len = temp + ETHER_ALIGN;
17114076dd23SAndrew Thompson 				m_adj(m, ETHER_ALIGN);
17124076dd23SAndrew Thompson 
17134076dd23SAndrew Thompson 				usbd_copy_out(pc, offset, m->m_data, temp);
17144076dd23SAndrew Thompson 
17154076dd23SAndrew Thompson 				/* enqueue */
17164076dd23SAndrew Thompson 				uether_rxmbuf(&sc->sc_ue, m, temp);
17174076dd23SAndrew Thompson 
17184076dd23SAndrew Thompson 				sumdata += temp;
17194076dd23SAndrew Thompson 			} else {
1720ecc70d3fSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
17214076dd23SAndrew Thompson 			}
17224076dd23SAndrew Thompson 		}
17234076dd23SAndrew Thompson 
17244076dd23SAndrew Thompson 		DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
17254076dd23SAndrew Thompson 
17264076dd23SAndrew Thompson 	case USB_ST_SETUP:
172785e3d588SAndrew Thompson tr_setup:
17284076dd23SAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
17294076dd23SAndrew Thompson 		usbd_xfer_set_frames(xfer, 1);
17304076dd23SAndrew Thompson 		usbd_transfer_submit(xfer);
17314076dd23SAndrew Thompson 		uether_rxflush(&sc->sc_ue);	/* must be last */
17324076dd23SAndrew Thompson 		break;
17334076dd23SAndrew Thompson 
17344076dd23SAndrew Thompson 	default:			/* Error */
17354076dd23SAndrew Thompson 		DPRINTFN(1, "error = %s\n",
17364076dd23SAndrew Thompson 		    usbd_errstr(error));
17374076dd23SAndrew Thompson 
17384076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
17394076dd23SAndrew Thompson tr_stall:
17407d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
17414076dd23SAndrew Thompson 				/* try to clear stall first */
17424076dd23SAndrew Thompson 				usbd_xfer_set_stall(xfer);
17434076dd23SAndrew Thompson 				usbd_xfer_set_frames(xfer, 0);
17444076dd23SAndrew Thompson 				usbd_transfer_submit(xfer);
17454076dd23SAndrew Thompson 			}
17467d502f32SRuslan Bukin 		}
17474076dd23SAndrew Thompson 		break;
17484076dd23SAndrew Thompson 	}
17494076dd23SAndrew Thompson }
17504076dd23SAndrew Thompson #endif
1751