xref: /freebsd/sys/dev/usb/net/if_cdce.c (revision 2625e51956881fed072864ab37c3b1d0f784e73a)
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;
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson static uint32_t	cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
11602ac6454SAndrew Thompson 
117b850ecc1SAndrew Thompson #ifdef USB_DEBUG
11802ac6454SAndrew Thompson static int cdce_debug = 0;
1197e05daaeSHans Petter Selasky static int cdce_tx_interval = 0;
12002ac6454SAndrew Thompson 
1216472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet");
122ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RWTUN, &cdce_debug, 0,
12302ac6454SAndrew Thompson     "Debug level");
124ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RWTUN, &cdce_tx_interval, 0,
1257e05daaeSHans Petter Selasky     "NCM transmit interval in ms");
12602ac6454SAndrew Thompson #endif
12702ac6454SAndrew Thompson 
128760bc48eSAndrew Thompson static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
12902ac6454SAndrew Thompson 
1304eae601eSAndrew Thompson 	[CDCE_BULK_RX] = {
13102ac6454SAndrew Thompson 		.type = UE_BULK,
13202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1334eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
13402ac6454SAndrew Thompson 		.if_index = 0,
1354eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1364eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1374eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
1384eae601eSAndrew Thompson 		.callback = cdce_bulk_read_callback,
1394eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
140f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
14102ac6454SAndrew Thompson 	},
14202ac6454SAndrew Thompson 
1434eae601eSAndrew Thompson 	[CDCE_BULK_TX] = {
14402ac6454SAndrew Thompson 		.type = UE_BULK,
14502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1464eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
14702ac6454SAndrew Thompson 		.if_index = 0,
1484eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1494eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1504eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
1514eae601eSAndrew Thompson 		.callback = cdce_bulk_write_callback,
1524eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
153f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
15402ac6454SAndrew Thompson 	},
15502ac6454SAndrew Thompson 
1564eae601eSAndrew Thompson 	[CDCE_INTR_RX] = {
15702ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
15802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1594eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
16002ac6454SAndrew Thompson 		.if_index = 1,
1614eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1624eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
1634eae601eSAndrew Thompson 		.callback = cdce_intr_read_callback,
1644eae601eSAndrew Thompson 		.timeout = 0,
1654eae601eSAndrew Thompson 		.usb_mode = USB_MODE_HOST,
1664eae601eSAndrew Thompson 	},
1674eae601eSAndrew Thompson 
1684eae601eSAndrew Thompson 	[CDCE_INTR_TX] = {
1694eae601eSAndrew Thompson 		.type = UE_INTERRUPT,
1704eae601eSAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1714eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
1724eae601eSAndrew Thompson 		.if_index = 1,
1734eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1744eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
1754eae601eSAndrew Thompson 		.callback = cdce_intr_write_callback,
1764eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
1774eae601eSAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
17802ac6454SAndrew Thompson 	},
17902ac6454SAndrew Thompson };
18002ac6454SAndrew Thompson 
1814076dd23SAndrew Thompson #if CDCE_HAVE_NCM
1824076dd23SAndrew Thompson static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
1834076dd23SAndrew Thompson 
1844076dd23SAndrew Thompson 	[CDCE_BULK_RX] = {
1854076dd23SAndrew Thompson 		.type = UE_BULK,
1864076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1874076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
1884076dd23SAndrew Thompson 		.if_index = 0,
1894076dd23SAndrew Thompson 		.frames = CDCE_NCM_RX_FRAMES_MAX,
1904076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
1914076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
1924076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_read_callback,
1934076dd23SAndrew Thompson 		.timeout = 0,	/* no timeout */
1944076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
1954076dd23SAndrew Thompson 	},
1964076dd23SAndrew Thompson 
1974076dd23SAndrew Thompson 	[CDCE_BULK_TX] = {
1984076dd23SAndrew Thompson 		.type = UE_BULK,
1994076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2004076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
2014076dd23SAndrew Thompson 		.if_index = 0,
2024076dd23SAndrew Thompson 		.frames = CDCE_NCM_TX_FRAMES_MAX,
2034076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
2047e05daaeSHans Petter Selasky 		.flags = {.pipe_bof = 1,},
2054076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_write_callback,
2064076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2074076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
2084076dd23SAndrew Thompson 	},
2094076dd23SAndrew Thompson 
2104076dd23SAndrew Thompson 	[CDCE_INTR_RX] = {
2114076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2124076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2134076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
2144076dd23SAndrew Thompson 		.if_index = 1,
2154076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2164076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
2174076dd23SAndrew Thompson 		.callback = cdce_intr_read_callback,
2184076dd23SAndrew Thompson 		.timeout = 0,
2194076dd23SAndrew Thompson 		.usb_mode = USB_MODE_HOST,
2204076dd23SAndrew Thompson 	},
2214076dd23SAndrew Thompson 
2224076dd23SAndrew Thompson 	[CDCE_INTR_TX] = {
2234076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2244076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2254076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
2264076dd23SAndrew Thompson 		.if_index = 1,
2274076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2284076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
2294076dd23SAndrew Thompson 		.callback = cdce_intr_write_callback,
2304076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2314076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
2324076dd23SAndrew Thompson 	},
2334076dd23SAndrew Thompson };
2344076dd23SAndrew Thompson #endif
2354076dd23SAndrew Thompson 
23602ac6454SAndrew Thompson static device_method_t cdce_methods[] = {
23702ac6454SAndrew Thompson 	/* USB interface */
23802ac6454SAndrew Thompson 	DEVMETHOD(usb_handle_request, cdce_handle_request),
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 	/* Device interface */
24102ac6454SAndrew Thompson 	DEVMETHOD(device_probe, cdce_probe),
24202ac6454SAndrew Thompson 	DEVMETHOD(device_attach, cdce_attach),
24302ac6454SAndrew Thompson 	DEVMETHOD(device_detach, cdce_detach),
24402ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, cdce_suspend),
24502ac6454SAndrew Thompson 	DEVMETHOD(device_resume, cdce_resume),
24602ac6454SAndrew Thompson 
24761bfd867SSofian Brabez 	DEVMETHOD_END
24802ac6454SAndrew Thompson };
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson static driver_t cdce_driver = {
25102ac6454SAndrew Thompson 	.name = "cdce",
25202ac6454SAndrew Thompson 	.methods = cdce_methods,
25302ac6454SAndrew Thompson 	.size = sizeof(struct cdce_softc),
25402ac6454SAndrew Thompson };
25502ac6454SAndrew Thompson 
25602ac6454SAndrew Thompson static devclass_t cdce_devclass;
257c376f439SNick Hibma static eventhandler_tag cdce_etag;
25802ac6454SAndrew Thompson 
259c376f439SNick Hibma static int  cdce_driver_loaded(struct module *, int, void *);
260c376f439SNick Hibma 
261c376f439SNick Hibma static const STRUCT_USB_HOST_ID cdce_switch_devs[] = {
262c376f439SNick Hibma 	{USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E3272_INIT, MSC_EJECT_HUAWEI2)},
263c376f439SNick Hibma };
264c376f439SNick Hibma 
265f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
26602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
26702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
26802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
26902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
27002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
27302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
27402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
27502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
279*2625e519SHiroki Sato 	{USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8156, 0)},
280c376f439SNick Hibma 
281c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
282c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x16),
283c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
284c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
285c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x46),
286c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
287c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
288c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x76),
289c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
290cdadbbb0SHans Petter Selasky 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
291cdadbbb0SHans Petter Selasky 		USB_IFACE_SUBCLASS(0x03), USB_IFACE_PROTOCOL(0x16),
292cdadbbb0SHans Petter Selasky 		USB_DRIVER_INFO(0)},
293f1a16106SHans Petter Selasky };
2949739167cSAlfred Perlstein 
295f1a16106SHans Petter Selasky static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
2969739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
2979739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
2984076dd23SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
29902ac6454SAndrew Thompson };
30002ac6454SAndrew Thompson 
301f809f280SWarner Losh DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, cdce_driver_loaded, 0);
302f809f280SWarner Losh MODULE_VERSION(cdce, 1);
303f809f280SWarner Losh MODULE_DEPEND(cdce, uether, 1, 1, 1);
304f809f280SWarner Losh MODULE_DEPEND(cdce, usb, 1, 1, 1);
305f809f280SWarner Losh MODULE_DEPEND(cdce, ether, 1, 1, 1);
306f809f280SWarner Losh USB_PNP_DEVICE_INFO(cdce_switch_devs);
307f809f280SWarner Losh USB_PNP_HOST_INFO(cdce_host_devs);
308f809f280SWarner Losh USB_PNP_DUAL_INFO(cdce_dual_devs);
309f809f280SWarner Losh 
310f809f280SWarner Losh static const struct usb_ether_methods cdce_ue_methods = {
311f809f280SWarner Losh 	.ue_attach_post = cdce_attach_post,
312f809f280SWarner Losh 	.ue_start = cdce_start,
313f809f280SWarner Losh 	.ue_init = cdce_init,
314f809f280SWarner Losh 	.ue_stop = cdce_stop,
315f809f280SWarner Losh 	.ue_setmulti = cdce_setmulti,
316f809f280SWarner Losh 	.ue_setpromisc = cdce_setpromisc,
317f809f280SWarner Losh };
318f809f280SWarner Losh 
3194076dd23SAndrew Thompson #if CDCE_HAVE_NCM
3204076dd23SAndrew Thompson /*------------------------------------------------------------------------*
3214076dd23SAndrew Thompson  *	cdce_ncm_init
3224076dd23SAndrew Thompson  *
3234076dd23SAndrew Thompson  * Return values:
3244076dd23SAndrew Thompson  * 0: Success
3254076dd23SAndrew Thompson  * Else: Failure
3264076dd23SAndrew Thompson  *------------------------------------------------------------------------*/
3274076dd23SAndrew Thompson static uint8_t
3284076dd23SAndrew Thompson cdce_ncm_init(struct cdce_softc *sc)
3294076dd23SAndrew Thompson {
3304076dd23SAndrew Thompson 	struct usb_ncm_parameters temp;
3314076dd23SAndrew Thompson 	struct usb_device_request req;
3327e05daaeSHans Petter Selasky 	struct usb_ncm_func_descriptor *ufd;
3337e05daaeSHans Petter Selasky 	uint8_t value[8];
3344076dd23SAndrew Thompson 	int err;
3354076dd23SAndrew Thompson 
3367e05daaeSHans Petter Selasky 	ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
3376d917491SHans Petter Selasky 	    sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF,
3386d917491SHans Petter Selasky 	    UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF);
3397e05daaeSHans Petter Selasky 
3407e05daaeSHans Petter Selasky 	/* verify length of NCM functional descriptor */
3417e05daaeSHans Petter Selasky 	if (ufd != NULL) {
3427e05daaeSHans Petter Selasky 		if (ufd->bLength < sizeof(*ufd))
3437e05daaeSHans Petter Selasky 			ufd = NULL;
3447e05daaeSHans Petter Selasky 		else
3457e05daaeSHans Petter Selasky 			DPRINTFN(1, "Found NCM functional descriptor.\n");
3467e05daaeSHans Petter Selasky 	}
3477e05daaeSHans Petter Selasky 
3484076dd23SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
3494076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
3504076dd23SAndrew Thompson 	USETW(req.wValue, 0);
3514076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
3524076dd23SAndrew Thompson 	req.wIndex[1] = 0;
3534076dd23SAndrew Thompson 	USETW(req.wLength, sizeof(temp));
3544076dd23SAndrew Thompson 
3554076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
3564076dd23SAndrew Thompson 	    &temp, 0, NULL, 1000 /* ms */);
3574076dd23SAndrew Thompson 	if (err)
3584076dd23SAndrew Thompson 		return (1);
3594076dd23SAndrew Thompson 
3604076dd23SAndrew Thompson 	/* Read correct set of parameters according to device mode */
3614076dd23SAndrew Thompson 
3624076dd23SAndrew Thompson 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
363dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
364dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
3654076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
3664076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
3674076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
3687e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3694076dd23SAndrew Thompson 	} else {
370dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
371dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
3724076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
3734076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
3744076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
3757e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3764076dd23SAndrew Thompson 	}
3774076dd23SAndrew Thompson 
3784076dd23SAndrew Thompson 	/* Verify maximum receive length */
3794076dd23SAndrew Thompson 
3807e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.rx_max < 32) ||
3814076dd23SAndrew Thompson 	    (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
3824076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum receive length\n");
3834076dd23SAndrew Thompson 		sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
3844076dd23SAndrew Thompson 	}
3854076dd23SAndrew Thompson 
3864076dd23SAndrew Thompson 	/* Verify maximum transmit length */
3874076dd23SAndrew Thompson 
3887e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_max < 32) ||
3894076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
3904076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum transmit length\n");
3914076dd23SAndrew Thompson 		sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
3924076dd23SAndrew Thompson 	}
3934076dd23SAndrew Thompson 
3944076dd23SAndrew Thompson 	/*
3954076dd23SAndrew Thompson 	 * Verify that the structure alignment is:
3964076dd23SAndrew Thompson 	 * - power of two
3974076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
3984076dd23SAndrew Thompson 	 * - not less than four bytes
3994076dd23SAndrew Thompson 	 */
4007e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_struct_align < 4) ||
4014076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align !=
4024076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
4034076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
4044076dd23SAndrew Thompson 		DPRINTFN(1, "Using default other alignment: 4 bytes\n");
4054076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = 4;
4064076dd23SAndrew Thompson 	}
4074076dd23SAndrew Thompson 
4084076dd23SAndrew Thompson 	/*
4094076dd23SAndrew Thompson 	 * Verify that the payload alignment is:
4104076dd23SAndrew Thompson 	 * - power of two
4114076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
4124076dd23SAndrew Thompson 	 * - not less than four bytes
4134076dd23SAndrew Thompson 	 */
4147e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_modulus < 4) ||
4154076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus !=
4164076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
4174076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
4184076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
4194076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = 4;
4204076dd23SAndrew Thompson 	}
4214076dd23SAndrew Thompson 
4224076dd23SAndrew Thompson 	/* Verify that the payload remainder */
4234076dd23SAndrew Thompson 
4247e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
4254076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
4264076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = 0;
4274076dd23SAndrew Thompson 	}
4284076dd23SAndrew Thompson 
4297e05daaeSHans Petter Selasky 	/*
4307e05daaeSHans Petter Selasky 	 * Offset the TX remainder so that IP packet payload starts at
4317e05daaeSHans Petter Selasky 	 * the tx_modulus. This is not too clear in the specification.
4327e05daaeSHans Petter Selasky 	 */
4337e05daaeSHans Petter Selasky 
4347e05daaeSHans Petter Selasky 	sc->sc_ncm.tx_remainder =
4357e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
4367e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_modulus - 1);
4377e05daaeSHans Petter Selasky 
4387e05daaeSHans Petter Selasky 	/* Verify max datagrams */
4397e05daaeSHans Petter Selasky 
4407e05daaeSHans Petter Selasky 	if (sc->sc_ncm.tx_nframe == 0 ||
4417e05daaeSHans Petter Selasky 	    sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
4427e05daaeSHans Petter Selasky 		DPRINTFN(1, "Using default max "
4437e05daaeSHans Petter Selasky 		    "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
4447e05daaeSHans Petter Selasky 		/* need to reserve one entry for zero padding */
4457e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
4467e05daaeSHans Petter Selasky 	}
4477e05daaeSHans Petter Selasky 
4484076dd23SAndrew Thompson 	/* Additional configuration, will fail in device side mode, which is OK. */
4494076dd23SAndrew Thompson 
4504076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4514076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
4524076dd23SAndrew Thompson 	USETW(req.wValue, 0);
4534076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4544076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4557e05daaeSHans Petter Selasky 
4567e05daaeSHans Petter Selasky 	if (ufd != NULL &&
4577e05daaeSHans Petter Selasky 	    (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
4587e05daaeSHans Petter Selasky 		USETW(req.wLength, 8);
4597e05daaeSHans Petter Selasky 		USETDW(value, sc->sc_ncm.rx_max);
4607e05daaeSHans Petter Selasky 		USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
4617e05daaeSHans Petter Selasky 		USETW(value + 6, 0);
4627e05daaeSHans Petter Selasky 	} else {
4634076dd23SAndrew Thompson 		USETW(req.wLength, 4);
4644076dd23SAndrew Thompson 		USETDW(value, sc->sc_ncm.rx_max);
4657e05daaeSHans Petter Selasky  	}
4664076dd23SAndrew Thompson 
4674076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4684076dd23SAndrew Thompson 	    &value, 0, NULL, 1000 /* ms */);
4694076dd23SAndrew Thompson 	if (err) {
4704076dd23SAndrew Thompson 		DPRINTFN(1, "Setting input size "
4714076dd23SAndrew Thompson 		    "to %u failed.\n", sc->sc_ncm.rx_max);
4724076dd23SAndrew Thompson 	}
4734076dd23SAndrew Thompson 
4744076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4754076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_CRC_MODE;
4764076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* no CRC */
4774076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4784076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4794076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4804076dd23SAndrew Thompson 
4814076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4824076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4834076dd23SAndrew Thompson 	if (err) {
4844076dd23SAndrew Thompson 		DPRINTFN(1, "Setting CRC mode to off failed.\n");
4854076dd23SAndrew Thompson 	}
4864076dd23SAndrew Thompson 
4874076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4884076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
4894076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* NTB-16 */
4904076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4914076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4924076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4934076dd23SAndrew Thompson 
4944076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4954076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4964076dd23SAndrew Thompson 	if (err) {
4974076dd23SAndrew Thompson 		DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
4984076dd23SAndrew Thompson 	}
4994076dd23SAndrew Thompson 
5004076dd23SAndrew Thompson 	return (0);		/* success */
5014076dd23SAndrew Thompson }
5024076dd23SAndrew Thompson #endif
5034076dd23SAndrew Thompson 
504c376f439SNick Hibma static void
505c376f439SNick Hibma cdce_test_autoinst(void *arg, struct usb_device *udev,
506c376f439SNick Hibma     struct usb_attach_arg *uaa)
507c376f439SNick Hibma {
508c376f439SNick Hibma 	struct usb_interface *iface;
509c376f439SNick Hibma 	struct usb_interface_descriptor *id;
510c376f439SNick Hibma 
511c376f439SNick Hibma 	if (uaa->dev_state != UAA_DEV_READY)
512c376f439SNick Hibma 		return;
513c376f439SNick Hibma 
514c376f439SNick Hibma 	iface = usbd_get_iface(udev, 0);
515c376f439SNick Hibma 	if (iface == NULL)
516c376f439SNick Hibma 		return;
517c376f439SNick Hibma 	id = iface->idesc;
518c376f439SNick Hibma 	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
519c376f439SNick Hibma 		return;
520c376f439SNick Hibma 	if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
521c376f439SNick Hibma 		return;		/* no device match */
522c376f439SNick Hibma 
523c376f439SNick Hibma 	if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
524c376f439SNick Hibma 		/* success, mark the udev as disappearing */
525c376f439SNick Hibma 		uaa->dev_state = UAA_DEV_EJECTING;
526c376f439SNick Hibma 	}
527c376f439SNick Hibma }
528c376f439SNick Hibma 
529c376f439SNick Hibma static int
530c376f439SNick Hibma cdce_driver_loaded(struct module *mod, int what, void *arg)
531c376f439SNick Hibma {
532c376f439SNick Hibma 	switch (what) {
533c376f439SNick Hibma 	case MOD_LOAD:
534c376f439SNick Hibma 		/* register our autoinstall handler */
535c376f439SNick Hibma 		cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
536c376f439SNick Hibma 		    cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
537c376f439SNick Hibma 		return (0);
538c376f439SNick Hibma 	case MOD_UNLOAD:
539c376f439SNick Hibma 		EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
540c376f439SNick Hibma 		return (0);
541c376f439SNick Hibma 	default:
542c376f439SNick Hibma 		return (EOPNOTSUPP);
543c376f439SNick Hibma 	}
544c376f439SNick Hibma }
545c376f439SNick Hibma 
54602ac6454SAndrew Thompson static int
54702ac6454SAndrew Thompson cdce_probe(device_t dev)
54802ac6454SAndrew Thompson {
549760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
550f1a16106SHans Petter Selasky 	int error;
55102ac6454SAndrew Thompson 
552f1a16106SHans Petter Selasky 	error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
553f1a16106SHans Petter Selasky 	if (error)
554f1a16106SHans Petter Selasky 		error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
555f1a16106SHans Petter Selasky 	return (error);
55602ac6454SAndrew Thompson }
55702ac6454SAndrew Thompson 
55802ac6454SAndrew Thompson static void
559760bc48eSAndrew Thompson cdce_attach_post(struct usb_ether *ue)
56002ac6454SAndrew Thompson {
56102ac6454SAndrew Thompson 	/* no-op */
56202ac6454SAndrew Thompson 	return;
56302ac6454SAndrew Thompson }
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson static int
56602ac6454SAndrew Thompson cdce_attach(device_t dev)
56702ac6454SAndrew Thompson {
56802ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
569760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
570760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
571760bc48eSAndrew Thompson 	struct usb_interface *iface;
572760bc48eSAndrew Thompson 	const struct usb_cdc_union_descriptor *ud;
573760bc48eSAndrew Thompson 	const struct usb_interface_descriptor *id;
574760bc48eSAndrew Thompson 	const struct usb_cdc_ethernet_descriptor *ued;
5754076dd23SAndrew Thompson 	const struct usb_config *pcfg;
576a8df530dSJohn Baldwin 	uint32_t seed;
57702ac6454SAndrew Thompson 	int error;
57802ac6454SAndrew Thompson 	uint8_t i;
5794076dd23SAndrew Thompson 	uint8_t data_iface_no;
58002ac6454SAndrew Thompson 	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
5834076dd23SAndrew Thompson 	sc->sc_ue.ue_udev = uaa->device;
58402ac6454SAndrew Thompson 
585a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
58602ac6454SAndrew Thompson 
58702ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
58802ac6454SAndrew Thompson 
5893c8d24f4SAndrew Thompson 	ud = usbd_find_descriptor
59002ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
5916d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF);
59202ac6454SAndrew Thompson 
5934076dd23SAndrew Thompson 	if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
5944076dd23SAndrew Thompson 	    (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
5954076dd23SAndrew Thompson 		DPRINTFN(1, "No union descriptor!\n");
5964076dd23SAndrew Thompson 		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
5974076dd23SAndrew Thompson 		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
5984076dd23SAndrew Thompson 		goto alloc_transfers;
59902ac6454SAndrew Thompson 	}
6004076dd23SAndrew Thompson 	data_iface_no = ud->bSlaveInterface[0];
60102ac6454SAndrew Thompson 
60202ac6454SAndrew Thompson 	for (i = 0;; i++) {
60302ac6454SAndrew Thompson 
604a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
60502ac6454SAndrew Thompson 
60602ac6454SAndrew Thompson 		if (iface) {
60702ac6454SAndrew Thompson 
608a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
60902ac6454SAndrew Thompson 
6104076dd23SAndrew Thompson 			if (id && (id->bInterfaceNumber == data_iface_no)) {
61102ac6454SAndrew Thompson 				sc->sc_ifaces_index[0] = i;
61202ac6454SAndrew Thompson 				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
613a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
61402ac6454SAndrew Thompson 				break;
61502ac6454SAndrew Thompson 			}
61602ac6454SAndrew Thompson 		} else {
617767cb2e2SAndrew Thompson 			device_printf(dev, "no data interface found\n");
61802ac6454SAndrew Thompson 			goto detach;
61902ac6454SAndrew Thompson 		}
62002ac6454SAndrew Thompson 	}
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 	/*
62302ac6454SAndrew Thompson 	 * <quote>
62402ac6454SAndrew Thompson 	 *
62502ac6454SAndrew Thompson 	 *  The Data Class interface of a networking device shall have
62602ac6454SAndrew Thompson 	 *  a minimum of two interface settings. The first setting
62702ac6454SAndrew Thompson 	 *  (the default interface setting) includes no endpoints and
62802ac6454SAndrew Thompson 	 *  therefore no networking traffic is exchanged whenever the
62902ac6454SAndrew Thompson 	 *  default interface setting is selected. One or more
63002ac6454SAndrew Thompson 	 *  additional interface settings are used for normal
63102ac6454SAndrew Thompson 	 *  operation, and therefore each includes a pair of endpoints
63202ac6454SAndrew Thompson 	 *  (one IN, and one OUT) to exchange network traffic. Select
63302ac6454SAndrew Thompson 	 *  an alternate interface setting to initialize the network
63402ac6454SAndrew Thompson 	 *  aspects of the device and to enable the exchange of
63502ac6454SAndrew Thompson 	 *  network traffic.
63602ac6454SAndrew Thompson 	 *
63702ac6454SAndrew Thompson 	 * </quote>
63802ac6454SAndrew Thompson 	 *
63902ac6454SAndrew Thompson 	 * Some devices, most notably cable modems, include interface
64002ac6454SAndrew Thompson 	 * settings that have no IN or OUT endpoint, therefore loop
64102ac6454SAndrew Thompson 	 * through the list of all available interface settings
64202ac6454SAndrew Thompson 	 * looking for one with both IN and OUT endpoints.
64302ac6454SAndrew Thompson 	 */
64402ac6454SAndrew Thompson 
64502ac6454SAndrew Thompson alloc_transfers:
64602ac6454SAndrew Thompson 
6474076dd23SAndrew Thompson 	pcfg = cdce_config;	/* Default Configuration */
6484076dd23SAndrew Thompson 
64902ac6454SAndrew Thompson 	for (i = 0; i != 32; i++) {
65002ac6454SAndrew Thompson 
6514076dd23SAndrew Thompson 		error = usbd_set_alt_interface_index(uaa->device,
6524076dd23SAndrew Thompson 		    sc->sc_ifaces_index[0], i);
6534076dd23SAndrew Thompson 		if (error)
6544076dd23SAndrew Thompson 			break;
6554076dd23SAndrew Thompson #if CDCE_HAVE_NCM
6564076dd23SAndrew Thompson 		if ((i == 0) && (cdce_ncm_init(sc) == 0))
6574076dd23SAndrew Thompson 			pcfg = cdce_ncm_config;
6584076dd23SAndrew Thompson #endif
6594076dd23SAndrew Thompson 		error = usbd_transfer_setup(uaa->device,
6604076dd23SAndrew Thompson 		    sc->sc_ifaces_index, sc->sc_xfer,
6614076dd23SAndrew Thompson 		    pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
66202ac6454SAndrew Thompson 
6634076dd23SAndrew Thompson 		if (error == 0)
66402ac6454SAndrew Thompson 			break;
66502ac6454SAndrew Thompson 	}
6664076dd23SAndrew Thompson 
6674076dd23SAndrew Thompson 	if (error || (i == 32)) {
6684076dd23SAndrew Thompson 		device_printf(dev, "No valid alternate "
669767cb2e2SAndrew Thompson 		    "setting found\n");
6704076dd23SAndrew Thompson 		goto detach;
67102ac6454SAndrew Thompson 	}
67202ac6454SAndrew Thompson 
6733c8d24f4SAndrew Thompson 	ued = usbd_find_descriptor
67402ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
6756d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF);
67602ac6454SAndrew Thompson 
67702ac6454SAndrew Thompson 	if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
67802ac6454SAndrew Thompson 		error = USB_ERR_INVAL;
67902ac6454SAndrew Thompson 	} else {
680a593f6b8SAndrew Thompson 		error = usbd_req_get_string_any(uaa->device, NULL,
68102ac6454SAndrew Thompson 		    eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
68202ac6454SAndrew Thompson 	}
68302ac6454SAndrew Thompson 
68402ac6454SAndrew Thompson 	if (error) {
68502ac6454SAndrew Thompson 
68602ac6454SAndrew Thompson 		/* fake MAC address */
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 		device_printf(dev, "faking MAC address\n");
689a8df530dSJohn Baldwin 		seed = ticks;
69002ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[0] = 0x2a;
691a8df530dSJohn Baldwin 		memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
69202ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 	} else {
69502ac6454SAndrew Thompson 
6967e05daaeSHans Petter Selasky 		memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
69702ac6454SAndrew Thompson 
69802ac6454SAndrew Thompson 		for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
69902ac6454SAndrew Thompson 
70002ac6454SAndrew Thompson 			char c = eaddr_str[i];
70102ac6454SAndrew Thompson 
70202ac6454SAndrew Thompson 			if ('0' <= c && c <= '9')
70302ac6454SAndrew Thompson 				c -= '0';
70402ac6454SAndrew Thompson 			else if (c != 0)
70502ac6454SAndrew Thompson 				c -= 'A' - 10;
70602ac6454SAndrew Thompson 			else
70702ac6454SAndrew Thompson 				break;
70802ac6454SAndrew Thompson 
70902ac6454SAndrew Thompson 			c &= 0xf;
71002ac6454SAndrew Thompson 
71102ac6454SAndrew Thompson 			if ((i & 1) == 0)
71202ac6454SAndrew Thompson 				c <<= 4;
71302ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[i / 2] |= c;
71402ac6454SAndrew Thompson 		}
71502ac6454SAndrew Thompson 
716f29a0724SAndrew Thompson 		if (uaa->usb_mode == USB_MODE_DEVICE) {
71702ac6454SAndrew Thompson 			/*
71802ac6454SAndrew Thompson 			 * Do not use the same MAC address like the peer !
71902ac6454SAndrew Thompson 			 */
72002ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[5] ^= 0xFF;
72102ac6454SAndrew Thompson 		}
72202ac6454SAndrew Thompson 	}
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson 	ue->ue_sc = sc;
72502ac6454SAndrew Thompson 	ue->ue_dev = dev;
72602ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
72702ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
72802ac6454SAndrew Thompson 	ue->ue_methods = &cdce_ue_methods;
72902ac6454SAndrew Thompson 
730a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
73102ac6454SAndrew Thompson 	if (error) {
73202ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
73302ac6454SAndrew Thompson 		goto detach;
73402ac6454SAndrew Thompson 	}
73502ac6454SAndrew Thompson 	return (0);			/* success */
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson detach:
73802ac6454SAndrew Thompson 	cdce_detach(dev);
73902ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
74002ac6454SAndrew Thompson }
74102ac6454SAndrew Thompson 
74202ac6454SAndrew Thompson static int
74302ac6454SAndrew Thompson cdce_detach(device_t dev)
74402ac6454SAndrew Thompson {
74502ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
746760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
74702ac6454SAndrew Thompson 
74802ac6454SAndrew Thompson 	/* stop all USB transfers first */
749a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
750a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
75102ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 	return (0);
75402ac6454SAndrew Thompson }
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson static void
757760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue)
75802ac6454SAndrew Thompson {
759a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
76002ac6454SAndrew Thompson 
76102ac6454SAndrew Thompson 	/*
76202ac6454SAndrew Thompson 	 * Start the USB transfers, if not already started:
76302ac6454SAndrew Thompson 	 */
764a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
765a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
76602ac6454SAndrew Thompson }
76702ac6454SAndrew Thompson 
76802ac6454SAndrew Thompson static void
76902ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n)
77002ac6454SAndrew Thompson {
77102ac6454SAndrew Thompson 	uint8_t x;
77202ac6454SAndrew Thompson 	for (x = 0; x != n; x++) {
77302ac6454SAndrew Thompson 		if (ppm[x] != NULL) {
77402ac6454SAndrew Thompson 			m_freem(ppm[x]);
77502ac6454SAndrew Thompson 			ppm[x] = NULL;
77602ac6454SAndrew Thompson 		}
77702ac6454SAndrew Thompson 	}
77802ac6454SAndrew Thompson }
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson static void
781ed6d949aSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
78202ac6454SAndrew Thompson {
783ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
784a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
78502ac6454SAndrew Thompson 	struct mbuf *m;
78602ac6454SAndrew Thompson 	struct mbuf *mt;
78702ac6454SAndrew Thompson 	uint32_t crc;
78802ac6454SAndrew Thompson 	uint8_t x;
789ed6d949aSAndrew Thompson 	int actlen, aframes;
790ed6d949aSAndrew Thompson 
791ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
79202ac6454SAndrew Thompson 
79302ac6454SAndrew Thompson 	DPRINTFN(1, "\n");
79402ac6454SAndrew Thompson 
79502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
79602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
797ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
798ed6d949aSAndrew Thompson 		    actlen, aframes);
79902ac6454SAndrew Thompson 
800ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 		/* free all previous TX buffers */
80302ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
80402ac6454SAndrew Thompson 
80502ac6454SAndrew Thompson 		/* FALLTHROUGH */
80602ac6454SAndrew Thompson 	case USB_ST_SETUP:
80702ac6454SAndrew Thompson tr_setup:
80802ac6454SAndrew Thompson 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
80902ac6454SAndrew Thompson 
81002ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
81102ac6454SAndrew Thompson 
81202ac6454SAndrew Thompson 			if (m == NULL)
81302ac6454SAndrew Thompson 				break;
81402ac6454SAndrew Thompson 
81502ac6454SAndrew Thompson 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
81602ac6454SAndrew Thompson 				/*
81702ac6454SAndrew Thompson 				 * Zaurus wants a 32-bit CRC appended
81802ac6454SAndrew Thompson 				 * to every frame
81902ac6454SAndrew Thompson 				 */
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
82202ac6454SAndrew Thompson 				crc = htole32(crc);
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson 				if (!m_append(m, 4, (void *)&crc)) {
82502ac6454SAndrew Thompson 					m_freem(m);
826ecc70d3fSGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
82702ac6454SAndrew Thompson 					continue;
82802ac6454SAndrew Thompson 				}
82902ac6454SAndrew Thompson 			}
83002ac6454SAndrew Thompson 			if (m->m_len != m->m_pkthdr.len) {
831c6499eccSGleb Smirnoff 				mt = m_defrag(m, M_NOWAIT);
83202ac6454SAndrew Thompson 				if (mt == NULL) {
83302ac6454SAndrew Thompson 					m_freem(m);
834ecc70d3fSGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
83502ac6454SAndrew Thompson 					continue;
83602ac6454SAndrew Thompson 				}
83702ac6454SAndrew Thompson 				m = mt;
83802ac6454SAndrew Thompson 			}
83902ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
84002ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
84102ac6454SAndrew Thompson 			}
84202ac6454SAndrew Thompson 			sc->sc_tx_buf[x] = m;
843ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
84402ac6454SAndrew Thompson 
84502ac6454SAndrew Thompson 			/*
84602ac6454SAndrew Thompson 			 * If there's a BPF listener, bounce a copy of
84702ac6454SAndrew Thompson 			 * this frame to him:
84802ac6454SAndrew Thompson 			 */
84902ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
85002ac6454SAndrew Thompson 		}
85102ac6454SAndrew Thompson 		if (x != 0) {
852ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
853ed6d949aSAndrew Thompson 
854a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
85502ac6454SAndrew Thompson 		}
85602ac6454SAndrew Thompson 		break;
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson 	default:			/* Error */
85902ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
860ed6d949aSAndrew Thompson 		    usbd_errstr(error));
86102ac6454SAndrew Thompson 
86202ac6454SAndrew Thompson 		/* free all previous TX buffers */
86302ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
86402ac6454SAndrew Thompson 
86502ac6454SAndrew Thompson 		/* count output errors */
866ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
86702ac6454SAndrew Thompson 
868ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
8697d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
87002ac6454SAndrew Thompson 				/* try to clear stall first */
871ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
87202ac6454SAndrew Thompson 			}
873d69eefebSRuslan Bukin 			goto tr_setup;
8747d502f32SRuslan Bukin 		}
87502ac6454SAndrew Thompson 		break;
87602ac6454SAndrew Thompson 	}
87702ac6454SAndrew Thompson }
87802ac6454SAndrew Thompson 
87902ac6454SAndrew Thompson static int32_t
88002ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
88102ac6454SAndrew Thompson {
88202ac6454SAndrew Thompson 	uint32_t *p_crc = arg;
88302ac6454SAndrew Thompson 
88402ac6454SAndrew Thompson 	*p_crc = crc32_raw(src, count, *p_crc);
88502ac6454SAndrew Thompson 	return (0);
88602ac6454SAndrew Thompson }
88702ac6454SAndrew Thompson 
88802ac6454SAndrew Thompson static uint32_t
88902ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
89002ac6454SAndrew Thompson {
89102ac6454SAndrew Thompson 	uint32_t crc = 0xFFFFFFFF;
89202ac6454SAndrew Thompson 	int error;
89302ac6454SAndrew Thompson 
89402ac6454SAndrew Thompson 	error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
89502ac6454SAndrew Thompson 	return (crc ^ 0xFFFFFFFF);
89602ac6454SAndrew Thompson }
89702ac6454SAndrew Thompson 
89802ac6454SAndrew Thompson static void
899760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue)
90002ac6454SAndrew Thompson {
901a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
902a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
90302ac6454SAndrew Thompson 
90402ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
90502ac6454SAndrew Thompson 
90602ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
90702ac6454SAndrew Thompson 
90802ac6454SAndrew Thompson 	/* start interrupt transfer */
909a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
910a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
91102ac6454SAndrew Thompson 
912b0b74fb3SRuslan Bukin 	/*
913b0b74fb3SRuslan Bukin 	 * Stall data write direction, which depends on USB mode.
914b0b74fb3SRuslan Bukin 	 *
915b0b74fb3SRuslan Bukin 	 * Some USB host stacks (e.g. Mac OS X) don't clears stall
916b0b74fb3SRuslan Bukin 	 * bit as it should, so set it in our host mode only.
917b0b74fb3SRuslan Bukin 	 */
918b0b74fb3SRuslan Bukin 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
919ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
92002ac6454SAndrew Thompson 
92102ac6454SAndrew Thompson 	/* start data transfers */
92202ac6454SAndrew Thompson 	cdce_start(ue);
92302ac6454SAndrew Thompson }
92402ac6454SAndrew Thompson 
92502ac6454SAndrew Thompson static void
926760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue)
92702ac6454SAndrew Thompson {
928a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
929a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
93002ac6454SAndrew Thompson 
93102ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
93202ac6454SAndrew Thompson 
93302ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
93402ac6454SAndrew Thompson 
93502ac6454SAndrew Thompson 	/*
93602ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
93702ac6454SAndrew Thompson 	 */
938a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
939a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
940a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
941a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
94202ac6454SAndrew Thompson }
94302ac6454SAndrew Thompson 
94402ac6454SAndrew Thompson static void
945760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue)
94602ac6454SAndrew Thompson {
94702ac6454SAndrew Thompson 	/* no-op */
94802ac6454SAndrew Thompson 	return;
94902ac6454SAndrew Thompson }
95002ac6454SAndrew Thompson 
95102ac6454SAndrew Thompson static void
952760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue)
95302ac6454SAndrew Thompson {
95402ac6454SAndrew Thompson 	/* no-op */
95502ac6454SAndrew Thompson 	return;
95602ac6454SAndrew Thompson }
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson static int
95902ac6454SAndrew Thompson cdce_suspend(device_t dev)
96002ac6454SAndrew Thompson {
96102ac6454SAndrew Thompson 	device_printf(dev, "Suspending\n");
96202ac6454SAndrew Thompson 	return (0);
96302ac6454SAndrew Thompson }
96402ac6454SAndrew Thompson 
96502ac6454SAndrew Thompson static int
96602ac6454SAndrew Thompson cdce_resume(device_t dev)
96702ac6454SAndrew Thompson {
96802ac6454SAndrew Thompson 	device_printf(dev, "Resuming\n");
96902ac6454SAndrew Thompson 	return (0);
97002ac6454SAndrew Thompson }
97102ac6454SAndrew Thompson 
97202ac6454SAndrew Thompson static void
973ed6d949aSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
97402ac6454SAndrew Thompson {
975ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
97602ac6454SAndrew Thompson 	struct mbuf *m;
97702ac6454SAndrew Thompson 	uint8_t x;
9786d917491SHans Petter Selasky 	int actlen;
9796d917491SHans Petter Selasky 	int aframes;
9806d917491SHans Petter Selasky 	int len;
981ed6d949aSAndrew Thompson 
982ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
98502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
98602ac6454SAndrew Thompson 
987ed6d949aSAndrew Thompson 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
98802ac6454SAndrew Thompson 
989ed6d949aSAndrew Thompson 		for (x = 0; x != aframes; x++) {
99002ac6454SAndrew Thompson 
99102ac6454SAndrew Thompson 			m = sc->sc_rx_buf[x];
99202ac6454SAndrew Thompson 			sc->sc_rx_buf[x] = NULL;
9938f9e0ef9SAndrew Thompson 			len = usbd_xfer_frame_len(xfer, x);
99402ac6454SAndrew Thompson 
99502ac6454SAndrew Thompson 			/* Strip off CRC added by Zaurus, if any */
996ed6d949aSAndrew Thompson 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
997ed6d949aSAndrew Thompson 				len -= 4;
99802ac6454SAndrew Thompson 
9996d917491SHans Petter Selasky 			if (len < (int)sizeof(struct ether_header)) {
100002ac6454SAndrew Thompson 				m_freem(m);
100102ac6454SAndrew Thompson 				continue;
100202ac6454SAndrew Thompson 			}
100302ac6454SAndrew Thompson 			/* queue up mbuf */
1004ed6d949aSAndrew Thompson 			uether_rxmbuf(&sc->sc_ue, m, len);
100502ac6454SAndrew Thompson 		}
100602ac6454SAndrew Thompson 
100702ac6454SAndrew Thompson 		/* FALLTHROUGH */
100802ac6454SAndrew Thompson 	case USB_ST_SETUP:
100902ac6454SAndrew Thompson 		/*
101002ac6454SAndrew Thompson 		 * TODO: Implement support for multi frame transfers,
101102ac6454SAndrew Thompson 		 * when the USB hardware supports it.
101202ac6454SAndrew Thompson 		 */
101302ac6454SAndrew Thompson 		for (x = 0; x != 1; x++) {
101402ac6454SAndrew Thompson 			if (sc->sc_rx_buf[x] == NULL) {
1015a593f6b8SAndrew Thompson 				m = uether_newbuf();
101602ac6454SAndrew Thompson 				if (m == NULL)
101702ac6454SAndrew Thompson 					goto tr_stall;
101802ac6454SAndrew Thompson 				sc->sc_rx_buf[x] = m;
101902ac6454SAndrew Thompson 			} else {
102002ac6454SAndrew Thompson 				m = sc->sc_rx_buf[x];
102102ac6454SAndrew Thompson 			}
102202ac6454SAndrew Thompson 
1023ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
102402ac6454SAndrew Thompson 		}
102502ac6454SAndrew Thompson 		/* set number of frames and start hardware */
1026ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, x);
1027a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
102802ac6454SAndrew Thompson 		/* flush any received frames */
1029a593f6b8SAndrew Thompson 		uether_rxflush(&sc->sc_ue);
103002ac6454SAndrew Thompson 		break;
103102ac6454SAndrew Thompson 
103202ac6454SAndrew Thompson 	default:			/* Error */
103302ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
1034ed6d949aSAndrew Thompson 		    usbd_errstr(error));
103502ac6454SAndrew Thompson 
1036ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
103702ac6454SAndrew Thompson tr_stall:
10387d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
103902ac6454SAndrew Thompson 				/* try to clear stall first */
1040ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
1041ed6d949aSAndrew Thompson 				usbd_xfer_set_frames(xfer, 0);
1042a593f6b8SAndrew Thompson 				usbd_transfer_submit(xfer);
10437d502f32SRuslan Bukin 			}
104402ac6454SAndrew Thompson 			break;
104502ac6454SAndrew Thompson 		}
104602ac6454SAndrew Thompson 
104702ac6454SAndrew Thompson 		/* need to free the RX-mbufs when we are cancelled */
104802ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
104902ac6454SAndrew Thompson 		break;
105002ac6454SAndrew Thompson 	}
105102ac6454SAndrew Thompson }
105202ac6454SAndrew Thompson 
105302ac6454SAndrew Thompson static void
1054ed6d949aSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
105502ac6454SAndrew Thompson {
10567d502f32SRuslan Bukin 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
1057ed6d949aSAndrew Thompson 	int actlen;
1058ed6d949aSAndrew Thompson 
1059ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1060ed6d949aSAndrew Thompson 
106102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
106202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
106302ac6454SAndrew Thompson 
1064ed6d949aSAndrew Thompson 		DPRINTF("Received %d bytes\n", actlen);
106502ac6454SAndrew Thompson 
106602ac6454SAndrew Thompson 		/* TODO: decode some indications */
106702ac6454SAndrew Thompson 
106802ac6454SAndrew Thompson 		/* FALLTHROUGH */
106902ac6454SAndrew Thompson 	case USB_ST_SETUP:
107002ac6454SAndrew Thompson tr_setup:
1071ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1072a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
107302ac6454SAndrew Thompson 		break;
107402ac6454SAndrew Thompson 
107502ac6454SAndrew Thompson 	default:			/* Error */
1076ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
107702ac6454SAndrew Thompson 			/* start clear stall */
10787d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1079ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
108002ac6454SAndrew Thompson 			goto tr_setup;
108102ac6454SAndrew Thompson 		}
108202ac6454SAndrew Thompson 		break;
108302ac6454SAndrew Thompson 	}
108402ac6454SAndrew Thompson }
108502ac6454SAndrew Thompson 
108602ac6454SAndrew Thompson static void
1087ed6d949aSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
108802ac6454SAndrew Thompson {
1089b0b74fb3SRuslan Bukin 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
1090b0b74fb3SRuslan Bukin 	struct usb_cdc_notification req;
1091b0b74fb3SRuslan Bukin 	struct usb_page_cache *pc;
1092b0b74fb3SRuslan Bukin 	uint32_t speed;
1093ed6d949aSAndrew Thompson 	int actlen;
1094ed6d949aSAndrew Thompson 
1095ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1096ed6d949aSAndrew Thompson 
109702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
109802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
109902ac6454SAndrew Thompson 
1100ed6d949aSAndrew Thompson 		DPRINTF("Transferred %d bytes\n", actlen);
110102ac6454SAndrew Thompson 
11027d502f32SRuslan Bukin 		switch (sc->sc_notify_state) {
1103d69eefebSRuslan Bukin 		case CDCE_NOTIFY_NETWORK_CONNECTION:
11047d502f32SRuslan Bukin 			sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
11057d502f32SRuslan Bukin 			break;
1106d69eefebSRuslan Bukin 		case CDCE_NOTIFY_SPEED_CHANGE:
11077d502f32SRuslan Bukin 			sc->sc_notify_state = CDCE_NOTIFY_DONE;
11087d502f32SRuslan Bukin 			break;
11097d502f32SRuslan Bukin 		default:
11107d502f32SRuslan Bukin 			break;
11117d502f32SRuslan Bukin 		}
11127d502f32SRuslan Bukin 
111302ac6454SAndrew Thompson 		/* FALLTHROUGH */
111402ac6454SAndrew Thompson 	case USB_ST_SETUP:
111502ac6454SAndrew Thompson tr_setup:
1116b0b74fb3SRuslan Bukin 		/*
1117b0b74fb3SRuslan Bukin 		 * Inform host about connection. Required according to USB CDC
1118b0b74fb3SRuslan Bukin 		 * specification and communicating to Mac OS X USB host stack.
1119b0b74fb3SRuslan Bukin 		 * Some of the values seems ignored by Mac OS X though.
1120b0b74fb3SRuslan Bukin 		 */
1121b0b74fb3SRuslan Bukin 		if (sc->sc_notify_state == CDCE_NOTIFY_NETWORK_CONNECTION) {
1122b0b74fb3SRuslan Bukin 			req.bmRequestType = UCDC_NOTIFICATION;
1123b0b74fb3SRuslan Bukin 			req.bNotification = UCDC_N_NETWORK_CONNECTION;
1124b0b74fb3SRuslan Bukin 			req.wIndex[0] = sc->sc_ifaces_index[1];
1125b0b74fb3SRuslan Bukin 			req.wIndex[1] = 0;
1126b0b74fb3SRuslan Bukin 			USETW(req.wValue, 1); /* Connected */
1127b0b74fb3SRuslan Bukin 			USETW(req.wLength, 0);
1128b0b74fb3SRuslan Bukin 
1129b0b74fb3SRuslan Bukin 			pc = usbd_xfer_get_frame(xfer, 0);
1130b0b74fb3SRuslan Bukin 			usbd_copy_in(pc, 0, &req, sizeof(req));
1131b0b74fb3SRuslan Bukin 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1132b0b74fb3SRuslan Bukin 			usbd_xfer_set_frames(xfer, 1);
1133a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
1134b0b74fb3SRuslan Bukin 
1135b0b74fb3SRuslan Bukin 		} else if (sc->sc_notify_state == CDCE_NOTIFY_SPEED_CHANGE) {
1136b0b74fb3SRuslan Bukin 			req.bmRequestType = UCDC_NOTIFICATION;
1137b0b74fb3SRuslan Bukin 			req.bNotification = UCDC_N_CONNECTION_SPEED_CHANGE;
1138b0b74fb3SRuslan Bukin 			req.wIndex[0] = sc->sc_ifaces_index[1];
1139b0b74fb3SRuslan Bukin 			req.wIndex[1] = 0;
1140b0b74fb3SRuslan Bukin 			USETW(req.wValue, 0);
1141b0b74fb3SRuslan Bukin 			USETW(req.wLength, 8);
1142b0b74fb3SRuslan Bukin 
1143b0b74fb3SRuslan Bukin 			/* Peak theoretical bulk trasfer rate in bits/s */
11447d502f32SRuslan Bukin 			if (usbd_get_speed(sc->sc_ue.ue_udev) != USB_SPEED_FULL)
1145b0b74fb3SRuslan Bukin 				speed = (13 * 512 * 8 * 1000 * 8);
1146b0b74fb3SRuslan Bukin 			else
1147b0b74fb3SRuslan Bukin 				speed = (19 * 64 * 1 * 1000 * 8);
1148b0b74fb3SRuslan Bukin 
1149b0b74fb3SRuslan Bukin 			USETDW(req.data + 0, speed); /* Upstream bit rate */
1150b0b74fb3SRuslan Bukin 			USETDW(req.data + 4, speed); /* Downstream bit rate */
1151b0b74fb3SRuslan Bukin 
1152b0b74fb3SRuslan Bukin 			pc = usbd_xfer_get_frame(xfer, 0);
1153b0b74fb3SRuslan Bukin 			usbd_copy_in(pc, 0, &req, sizeof(req));
1154b0b74fb3SRuslan Bukin 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1155b0b74fb3SRuslan Bukin 			usbd_xfer_set_frames(xfer, 1);
1156b0b74fb3SRuslan Bukin 			usbd_transfer_submit(xfer);
1157b0b74fb3SRuslan Bukin 		}
115802ac6454SAndrew Thompson 		break;
115902ac6454SAndrew Thompson 
116002ac6454SAndrew Thompson 	default:			/* Error */
1161ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
11627d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
116302ac6454SAndrew Thompson 				/* start clear stall */
1164ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
116502ac6454SAndrew Thompson 			}
1166d69eefebSRuslan Bukin 			goto tr_setup;
11677d502f32SRuslan Bukin 		}
116802ac6454SAndrew Thompson 		break;
116902ac6454SAndrew Thompson 	}
117002ac6454SAndrew Thompson }
117102ac6454SAndrew Thompson 
117202ac6454SAndrew Thompson static int
117302ac6454SAndrew Thompson cdce_handle_request(device_t dev,
11747d502f32SRuslan Bukin     const void *preq, void **pptr, uint16_t *plen,
117529bd7d7eSAndrew Thompson     uint16_t offset, uint8_t *pstate)
117602ac6454SAndrew Thompson {
11777d502f32SRuslan Bukin 	struct cdce_softc *sc = device_get_softc(dev);
11787d502f32SRuslan Bukin 	const struct usb_device_request *req = preq;
11797d502f32SRuslan Bukin 	uint8_t is_complete = *pstate;
11807d502f32SRuslan Bukin 
11817d502f32SRuslan Bukin 	/*
11827d502f32SRuslan Bukin 	 * When Mac OS X resumes after suspending it expects
11837d502f32SRuslan Bukin 	 * to be notified again after this request.
11847d502f32SRuslan Bukin 	 */
11857d502f32SRuslan Bukin 	if (req->bmRequestType == UT_WRITE_CLASS_INTERFACE && \
11867d502f32SRuslan Bukin 	    req->bRequest == UCDC_NCM_SET_ETHERNET_PACKET_FILTER) {
11877d502f32SRuslan Bukin 
11887d502f32SRuslan Bukin 		if (is_complete == 1) {
11897d502f32SRuslan Bukin 			mtx_lock(&sc->sc_mtx);
11907d502f32SRuslan Bukin 			sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
11917d502f32SRuslan Bukin 			usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
11927d502f32SRuslan Bukin 			mtx_unlock(&sc->sc_mtx);
11937d502f32SRuslan Bukin 		}
11947d502f32SRuslan Bukin 
11957d502f32SRuslan Bukin 		return (0);
11967d502f32SRuslan Bukin 	}
11977d502f32SRuslan Bukin 
119802ac6454SAndrew Thompson 	return (ENXIO);			/* use builtin handler */
119902ac6454SAndrew Thompson }
12004076dd23SAndrew Thompson 
12014076dd23SAndrew Thompson #if CDCE_HAVE_NCM
12027e05daaeSHans Petter Selasky static void
12037e05daaeSHans Petter Selasky cdce_ncm_tx_zero(struct usb_page_cache *pc,
12047e05daaeSHans Petter Selasky     uint32_t start, uint32_t end)
12057e05daaeSHans Petter Selasky {
12067e05daaeSHans Petter Selasky 	if (start >= CDCE_NCM_TX_MAXLEN)
12077e05daaeSHans Petter Selasky 		return;
12087e05daaeSHans Petter Selasky 	if (end > CDCE_NCM_TX_MAXLEN)
12097e05daaeSHans Petter Selasky 		end = CDCE_NCM_TX_MAXLEN;
12107e05daaeSHans Petter Selasky 
12117e05daaeSHans Petter Selasky 	usbd_frame_zero(pc, start, end - start);
12127e05daaeSHans Petter Selasky }
12137e05daaeSHans Petter Selasky 
12144076dd23SAndrew Thompson static uint8_t
12154076dd23SAndrew Thompson cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
12164076dd23SAndrew Thompson {
12174076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
12184076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
12194076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
12204076dd23SAndrew Thompson 	struct mbuf *m;
12214076dd23SAndrew Thompson 	uint32_t rem;
12224076dd23SAndrew Thompson 	uint32_t offset;
12234076dd23SAndrew Thompson 	uint32_t last_offset;
12247e05daaeSHans Petter Selasky 	uint16_t n;
12257e05daaeSHans Petter Selasky 	uint8_t retval;
12264076dd23SAndrew Thompson 
12274076dd23SAndrew Thompson 	usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
12284076dd23SAndrew Thompson 
12294076dd23SAndrew Thompson 	offset = sizeof(sc->sc_ncm.hdr) +
12304076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
12314076dd23SAndrew Thompson 
12324076dd23SAndrew Thompson 	/* Store last valid offset before alignment */
12334076dd23SAndrew Thompson 	last_offset = offset;
12344076dd23SAndrew Thompson 
12357e05daaeSHans Petter Selasky 	/* Align offset */
12367e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
12377e05daaeSHans Petter Selasky 	    offset, sc->sc_ncm.tx_modulus);
12384076dd23SAndrew Thompson 
12397e05daaeSHans Petter Selasky 	/* Zero pad */
12407e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
12417e05daaeSHans Petter Selasky 
12427e05daaeSHans Petter Selasky 	/* buffer full */
12437e05daaeSHans Petter Selasky 	retval = 2;
12447e05daaeSHans Petter Selasky 
12457e05daaeSHans Petter Selasky 	for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
12464076dd23SAndrew Thompson 
12474076dd23SAndrew Thompson 		/* check if end of transmit buffer is reached */
12484076dd23SAndrew Thompson 
12494076dd23SAndrew Thompson 		if (offset >= sc->sc_ncm.tx_max)
12504076dd23SAndrew Thompson 			break;
12514076dd23SAndrew Thompson 
12524076dd23SAndrew Thompson 		/* compute maximum buffer size */
12534076dd23SAndrew Thompson 
12544076dd23SAndrew Thompson 		rem = sc->sc_ncm.tx_max - offset;
12554076dd23SAndrew Thompson 
12564076dd23SAndrew Thompson 		IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
12574076dd23SAndrew Thompson 
12587e05daaeSHans Petter Selasky 		if (m == NULL) {
12597e05daaeSHans Petter Selasky 			/* buffer not full */
12607e05daaeSHans Petter Selasky 			retval = 1;
12614076dd23SAndrew Thompson 			break;
12627e05daaeSHans Petter Selasky 		}
12634076dd23SAndrew Thompson 
12646d917491SHans Petter Selasky 		if (m->m_pkthdr.len > (int)rem) {
12654076dd23SAndrew Thompson 			if (n == 0) {
12664076dd23SAndrew Thompson 				/* The frame won't fit in our buffer */
12674076dd23SAndrew Thompson 				DPRINTFN(1, "Frame too big to be transmitted!\n");
12684076dd23SAndrew Thompson 				m_freem(m);
1269ecc70d3fSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
12704076dd23SAndrew Thompson 				n--;
12714076dd23SAndrew Thompson 				continue;
12724076dd23SAndrew Thompson 			}
12734076dd23SAndrew Thompson 			/* Wait till next buffer becomes ready */
12744076dd23SAndrew Thompson 			IFQ_DRV_PREPEND(&(ifp->if_snd), m);
12754076dd23SAndrew Thompson 			break;
12764076dd23SAndrew Thompson 		}
12774076dd23SAndrew Thompson 		usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
12784076dd23SAndrew Thompson 
12794076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
12804076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
12814076dd23SAndrew Thompson 
12824076dd23SAndrew Thompson 		/* Update offset */
12834076dd23SAndrew Thompson 		offset += m->m_pkthdr.len;
12844076dd23SAndrew Thompson 
12854076dd23SAndrew Thompson 		/* Store last valid offset before alignment */
12864076dd23SAndrew Thompson 		last_offset = offset;
12874076dd23SAndrew Thompson 
12887e05daaeSHans Petter Selasky 		/* Align offset */
12897e05daaeSHans Petter Selasky 		offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
12907e05daaeSHans Petter Selasky 		    offset, sc->sc_ncm.tx_modulus);
12917e05daaeSHans Petter Selasky 
12927e05daaeSHans Petter Selasky 		/* Zero pad */
12937e05daaeSHans Petter Selasky 		cdce_ncm_tx_zero(pc, last_offset, offset);
12944076dd23SAndrew Thompson 
12954076dd23SAndrew Thompson 		/*
12964076dd23SAndrew Thompson 		 * If there's a BPF listener, bounce a copy
12974076dd23SAndrew Thompson 		 * of this frame to him:
12984076dd23SAndrew Thompson 		 */
12994076dd23SAndrew Thompson 		BPF_MTAP(ifp, m);
13004076dd23SAndrew Thompson 
13014076dd23SAndrew Thompson 		/* Free mbuf */
13024076dd23SAndrew Thompson 
13034076dd23SAndrew Thompson 		m_freem(m);
13044076dd23SAndrew Thompson 
13054076dd23SAndrew Thompson 		/* Pre-increment interface counter */
13064076dd23SAndrew Thompson 
1307ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
13084076dd23SAndrew Thompson 	}
13094076dd23SAndrew Thompson 
13104076dd23SAndrew Thompson 	if (n == 0)
13117e05daaeSHans Petter Selasky 		return (0);
13124076dd23SAndrew Thompson 
13134076dd23SAndrew Thompson 	rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
13144076dd23SAndrew Thompson 
13154076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wLength, rem);
13164076dd23SAndrew Thompson 
13174076dd23SAndrew Thompson 	/* zero the rest of the data pointer entries */
13184076dd23SAndrew Thompson 	for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
13194076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
13204076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
13214076dd23SAndrew Thompson 	}
13224076dd23SAndrew Thompson 
13237e05daaeSHans Petter Selasky 	offset = last_offset;
13247e05daaeSHans Petter Selasky 
13257e05daaeSHans Petter Selasky 	/* Align offset */
13267e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
13277e05daaeSHans Petter Selasky 
13287e05daaeSHans Petter Selasky 	/* Optimise, save bandwidth and force short termination */
13297e05daaeSHans Petter Selasky 	if (offset >= sc->sc_ncm.tx_max)
13307e05daaeSHans Petter Selasky 		offset = sc->sc_ncm.tx_max;
13317e05daaeSHans Petter Selasky 	else
13327e05daaeSHans Petter Selasky 		offset ++;
13337e05daaeSHans Petter Selasky 
13347e05daaeSHans Petter Selasky 	/* Zero pad */
13357e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
13367e05daaeSHans Petter Selasky 
13374076dd23SAndrew Thompson 	/* set frame length */
13387e05daaeSHans Petter Selasky 	usbd_xfer_set_frame_len(xfer, index, offset);
13394076dd23SAndrew Thompson 
13404076dd23SAndrew Thompson 	/* Fill out 16-bit header */
13414076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[0] = 'N';
13424076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[1] = 'C';
13434076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[2] = 'M';
13444076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[3] = 'H';
13454076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
13467e05daaeSHans Petter Selasky 	USETW(sc->sc_ncm.hdr.wBlockLength, offset);
13474076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
13484076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
13494076dd23SAndrew Thompson 
13504076dd23SAndrew Thompson 	sc->sc_ncm.tx_seq++;
13514076dd23SAndrew Thompson 
13524076dd23SAndrew Thompson 	/* Fill out 16-bit frame table header */
13534076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[0] = 'N';
13544076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[1] = 'C';
13554076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[2] = 'M';
135685e3d588SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[3] = '0';
13574076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0);		/* reserved */
13584076dd23SAndrew Thompson 
13594076dd23SAndrew Thompson 	usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
13604076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
13614076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt));
13624076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
13634076dd23SAndrew Thompson 	    &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
13647e05daaeSHans Petter Selasky 	return (retval);
13654076dd23SAndrew Thompson }
13664076dd23SAndrew Thompson 
13674076dd23SAndrew Thompson static void
13684076dd23SAndrew Thompson cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
13694076dd23SAndrew Thompson {
13704076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
13714076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
13724076dd23SAndrew Thompson 	uint16_t x;
13737e05daaeSHans Petter Selasky 	uint8_t temp;
13744076dd23SAndrew Thompson 	int actlen;
13754076dd23SAndrew Thompson 	int aframes;
13764076dd23SAndrew Thompson 
13774076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
13784076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
13794076dd23SAndrew Thompson 
13804076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
13814076dd23SAndrew Thompson 
13824076dd23SAndrew Thompson 		DPRINTFN(10, "transfer complete: "
13834076dd23SAndrew Thompson 		    "%u bytes in %u frames\n", actlen, aframes);
13844076dd23SAndrew Thompson 
13854076dd23SAndrew Thompson 	case USB_ST_SETUP:
13864076dd23SAndrew Thompson 		for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
13877e05daaeSHans Petter Selasky 			temp = cdce_ncm_fill_tx_frames(xfer, x);
13887e05daaeSHans Petter Selasky 			if (temp == 0)
13894076dd23SAndrew Thompson 				break;
13907e05daaeSHans Petter Selasky 			if (temp == 1) {
13917e05daaeSHans Petter Selasky 				x++;
13927e05daaeSHans Petter Selasky 				break;
13937e05daaeSHans Petter Selasky 			}
13944076dd23SAndrew Thompson 		}
13954076dd23SAndrew Thompson 
13964076dd23SAndrew Thompson 		if (x != 0) {
13977e05daaeSHans Petter Selasky #ifdef USB_DEBUG
13987e05daaeSHans Petter Selasky 			usbd_xfer_set_interval(xfer, cdce_tx_interval);
13997e05daaeSHans Petter Selasky #endif
14004076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
14014076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
14024076dd23SAndrew Thompson 		}
14034076dd23SAndrew Thompson 		break;
14044076dd23SAndrew Thompson 
14054076dd23SAndrew Thompson 	default:			/* Error */
14064076dd23SAndrew Thompson 		DPRINTFN(10, "Transfer error: %s\n",
14074076dd23SAndrew Thompson 		    usbd_errstr(error));
14084076dd23SAndrew Thompson 
14094076dd23SAndrew Thompson 		/* update error counter */
1410ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
14114076dd23SAndrew Thompson 
14124076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
14137d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
14144076dd23SAndrew Thompson 				/* try to clear stall first */
14154076dd23SAndrew Thompson 				usbd_xfer_set_stall(xfer);
14164076dd23SAndrew Thompson 				usbd_xfer_set_frames(xfer, 0);
14174076dd23SAndrew Thompson 				usbd_transfer_submit(xfer);
14184076dd23SAndrew Thompson 			}
14197d502f32SRuslan Bukin 		}
14204076dd23SAndrew Thompson 		break;
14214076dd23SAndrew Thompson 	}
14224076dd23SAndrew Thompson }
14234076dd23SAndrew Thompson 
14244076dd23SAndrew Thompson static void
14254076dd23SAndrew Thompson cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
14264076dd23SAndrew Thompson {
14274076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
14284076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
14294076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
14304076dd23SAndrew Thompson 	struct mbuf *m;
14314076dd23SAndrew Thompson 	int sumdata;
14324076dd23SAndrew Thompson 	int sumlen;
14334076dd23SAndrew Thompson 	int actlen;
14344076dd23SAndrew Thompson 	int aframes;
14354076dd23SAndrew Thompson 	int temp;
14364076dd23SAndrew Thompson 	int nframes;
14374076dd23SAndrew Thompson 	int x;
14384076dd23SAndrew Thompson 	int offset;
14394076dd23SAndrew Thompson 
14404076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
14414076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
14424076dd23SAndrew Thompson 
14434076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
14444076dd23SAndrew Thompson 
14454076dd23SAndrew Thompson 		DPRINTFN(1, "received %u bytes in %u frames\n",
14464076dd23SAndrew Thompson 		    actlen, aframes);
14474076dd23SAndrew Thompson 
14486d917491SHans Petter Selasky 		if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
14494076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt))) {
14504076dd23SAndrew Thompson 			DPRINTFN(1, "frame too short\n");
145185e3d588SAndrew Thompson 			goto tr_setup;
14524076dd23SAndrew Thompson 		}
14534076dd23SAndrew Thompson 		usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
14544076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.hdr));
14554076dd23SAndrew Thompson 
14564076dd23SAndrew Thompson 		if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
14574076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
14584076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
14594076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
146085e3d588SAndrew Thompson 			DPRINTFN(1, "invalid HDR signature: "
146185e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
146285e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[0],
146385e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[1],
146485e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[2],
146585e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[3]);
14664076dd23SAndrew Thompson 			goto tr_stall;
14674076dd23SAndrew Thompson 		}
14684076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
14694076dd23SAndrew Thompson 		if (temp > sumlen) {
14704076dd23SAndrew Thompson 			DPRINTFN(1, "unsupported block length %u/%u\n",
14714076dd23SAndrew Thompson 			    temp, sumlen);
14724076dd23SAndrew Thompson 			goto tr_stall;
14734076dd23SAndrew Thompson 		}
14744076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
14756d917491SHans Petter Selasky 		if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
147685e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
14774076dd23SAndrew Thompson 			goto tr_stall;
14784076dd23SAndrew Thompson 		}
14794076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
14804076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt));
14814076dd23SAndrew Thompson 
14824076dd23SAndrew Thompson 		if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
14834076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
14844076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
148585e3d588SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
148685e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT signature"
148785e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
148885e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[0],
148985e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[1],
149085e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[2],
149185e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[3]);
14924076dd23SAndrew Thompson 			goto tr_stall;
14934076dd23SAndrew Thompson 		}
14944076dd23SAndrew Thompson 		nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
14954076dd23SAndrew Thompson 
14964076dd23SAndrew Thompson 		/* Subtract size of header and last zero padded entry */
14974076dd23SAndrew Thompson 		if (nframes >= (2 + 1))
14984076dd23SAndrew Thompson 			nframes -= (2 + 1);
14994076dd23SAndrew Thompson 		else
15004076dd23SAndrew Thompson 			nframes = 0;
15014076dd23SAndrew Thompson 
15024076dd23SAndrew Thompson 		DPRINTFN(1, "nframes = %u\n", nframes);
15034076dd23SAndrew Thompson 
15044076dd23SAndrew Thompson 		temp += sizeof(sc->sc_ncm.dpt);
15054076dd23SAndrew Thompson 
15064076dd23SAndrew Thompson 		if ((temp + (4 * nframes)) > actlen)
15074076dd23SAndrew Thompson 			goto tr_stall;
15084076dd23SAndrew Thompson 
15094076dd23SAndrew Thompson 		if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
15104076dd23SAndrew Thompson 			DPRINTFN(1, "Truncating number of frames from %u to %u\n",
15114076dd23SAndrew Thompson 			    nframes, CDCE_NCM_SUBFRAMES_MAX);
15124076dd23SAndrew Thompson 			nframes = CDCE_NCM_SUBFRAMES_MAX;
15134076dd23SAndrew Thompson 		}
15144076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
15154076dd23SAndrew Thompson 
15164076dd23SAndrew Thompson 		sumdata = 0;
15174076dd23SAndrew Thompson 
15184076dd23SAndrew Thompson 		for (x = 0; x != nframes; x++) {
15194076dd23SAndrew Thompson 
15204076dd23SAndrew Thompson 			offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
15214076dd23SAndrew Thompson 			temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
15224076dd23SAndrew Thompson 
152329051223SAndrew Thompson 			if ((offset == 0) ||
15246d917491SHans Petter Selasky 			    (temp < (int)sizeof(struct ether_header)) ||
152529051223SAndrew Thompson 			    (temp > (MCLBYTES - ETHER_ALIGN))) {
152629051223SAndrew Thompson 				DPRINTFN(1, "NULL frame detected at %d\n", x);
15274076dd23SAndrew Thompson 				m = NULL;
152829051223SAndrew Thompson 				/* silently ignore this frame */
15294076dd23SAndrew Thompson 				continue;
153029051223SAndrew Thompson 			} else if ((offset + temp) > actlen) {
153129051223SAndrew Thompson 				DPRINTFN(1, "invalid frame "
153229051223SAndrew Thompson 				    "detected at %d\n", x);
153329051223SAndrew Thompson 				m = NULL;
153429051223SAndrew Thompson 				/* silently ignore this frame */
153529051223SAndrew Thompson 				continue;
15366d917491SHans Petter Selasky 			} else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1537c6499eccSGleb Smirnoff 				m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
15384076dd23SAndrew Thompson 			} else {
1539c6499eccSGleb Smirnoff 				m = m_gethdr(M_NOWAIT, MT_DATA);
15404076dd23SAndrew Thompson 			}
15414076dd23SAndrew Thompson 
15424076dd23SAndrew Thompson 			DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
15434076dd23SAndrew Thompson 			    x, offset, temp);
15444076dd23SAndrew Thompson 
15454076dd23SAndrew Thompson 			/* check if we have a buffer */
15464076dd23SAndrew Thompson 			if (m) {
1547a51f9801SHans Petter Selasky 				m->m_len = m->m_pkthdr.len = temp + ETHER_ALIGN;
15484076dd23SAndrew Thompson 				m_adj(m, ETHER_ALIGN);
15494076dd23SAndrew Thompson 
15504076dd23SAndrew Thompson 				usbd_copy_out(pc, offset, m->m_data, temp);
15514076dd23SAndrew Thompson 
15524076dd23SAndrew Thompson 				/* enqueue */
15534076dd23SAndrew Thompson 				uether_rxmbuf(&sc->sc_ue, m, temp);
15544076dd23SAndrew Thompson 
15554076dd23SAndrew Thompson 				sumdata += temp;
15564076dd23SAndrew Thompson 			} else {
1557ecc70d3fSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
15584076dd23SAndrew Thompson 			}
15594076dd23SAndrew Thompson 		}
15604076dd23SAndrew Thompson 
15614076dd23SAndrew Thompson 		DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
15624076dd23SAndrew Thompson 
15634076dd23SAndrew Thompson 	case USB_ST_SETUP:
156485e3d588SAndrew Thompson tr_setup:
15654076dd23SAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
15664076dd23SAndrew Thompson 		usbd_xfer_set_frames(xfer, 1);
15674076dd23SAndrew Thompson 		usbd_transfer_submit(xfer);
15684076dd23SAndrew Thompson 		uether_rxflush(&sc->sc_ue);	/* must be last */
15694076dd23SAndrew Thompson 		break;
15704076dd23SAndrew Thompson 
15714076dd23SAndrew Thompson 	default:			/* Error */
15724076dd23SAndrew Thompson 		DPRINTFN(1, "error = %s\n",
15734076dd23SAndrew Thompson 		    usbd_errstr(error));
15744076dd23SAndrew Thompson 
15754076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
15764076dd23SAndrew Thompson tr_stall:
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 #endif
1588