xref: /freebsd/sys/dev/usb/net/if_cdce.c (revision 3d5104182c2eb4336905e89aa0d089b67aa746e3)
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);
12002ac6454SAndrew Thompson 
121b850ecc1SAndrew Thompson #ifdef USB_DEBUG
12202ac6454SAndrew Thompson static int cdce_debug = 0;
1237e05daaeSHans Petter Selasky static int cdce_tx_interval = 0;
12402ac6454SAndrew Thompson 
125f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
126f8d2b1f3SPawel Biernacki     "USB CDC-Ethernet");
127ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RWTUN, &cdce_debug, 0,
12802ac6454SAndrew Thompson     "Debug level");
129ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RWTUN, &cdce_tx_interval, 0,
1307e05daaeSHans Petter Selasky     "NCM transmit interval in ms");
131b43d600cSJohn-Mark Gurney #else
132b43d600cSJohn-Mark Gurney #define cdce_debug 0
13302ac6454SAndrew Thompson #endif
13402ac6454SAndrew Thompson 
135760bc48eSAndrew Thompson static const struct usb_config cdce_config[CDCE_N_TRANSFER] = {
1364eae601eSAndrew Thompson 	[CDCE_BULK_RX] = {
13702ac6454SAndrew Thompson 		.type = UE_BULK,
13802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1394eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
14002ac6454SAndrew Thompson 		.if_index = 0,
1414eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1424eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1434eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
1444eae601eSAndrew Thompson 		.callback = cdce_bulk_read_callback,
1454eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
146f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
14702ac6454SAndrew Thompson 	},
14802ac6454SAndrew Thompson 
1494eae601eSAndrew Thompson 	[CDCE_BULK_TX] = {
15002ac6454SAndrew Thompson 		.type = UE_BULK,
15102ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1524eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
15302ac6454SAndrew Thompson 		.if_index = 0,
1544eae601eSAndrew Thompson 		.frames = CDCE_FRAMES_MAX,
1554eae601eSAndrew Thompson 		.bufsize = (CDCE_FRAMES_MAX * MCLBYTES),
1564eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
1574eae601eSAndrew Thompson 		.callback = cdce_bulk_write_callback,
1584eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
159f29a0724SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
16002ac6454SAndrew Thompson 	},
16102ac6454SAndrew Thompson 
1624eae601eSAndrew Thompson 	[CDCE_INTR_RX] = {
16302ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
16402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1654eae601eSAndrew Thompson 		.direction = UE_DIR_RX,
16602ac6454SAndrew Thompson 		.if_index = 1,
1674eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1684eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
1694eae601eSAndrew Thompson 		.callback = cdce_intr_read_callback,
1704eae601eSAndrew Thompson 		.timeout = 0,
1714eae601eSAndrew Thompson 		.usb_mode = USB_MODE_HOST,
1724eae601eSAndrew Thompson 	},
1734eae601eSAndrew Thompson 
1744eae601eSAndrew Thompson 	[CDCE_INTR_TX] = {
1754eae601eSAndrew Thompson 		.type = UE_INTERRUPT,
1764eae601eSAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1774eae601eSAndrew Thompson 		.direction = UE_DIR_TX,
1784eae601eSAndrew Thompson 		.if_index = 1,
1794eae601eSAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
1804eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
1814eae601eSAndrew Thompson 		.callback = cdce_intr_write_callback,
1824eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
1834eae601eSAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
18402ac6454SAndrew Thompson 	},
18502ac6454SAndrew Thompson };
18602ac6454SAndrew Thompson 
1874076dd23SAndrew Thompson #if CDCE_HAVE_NCM
1884076dd23SAndrew Thompson static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = {
1894076dd23SAndrew Thompson 	[CDCE_BULK_RX] = {
1904076dd23SAndrew Thompson 		.type = UE_BULK,
1914076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
1924076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
1934076dd23SAndrew Thompson 		.if_index = 0,
1944076dd23SAndrew Thompson 		.frames = CDCE_NCM_RX_FRAMES_MAX,
1954076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN),
1964076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,},
1974076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_read_callback,
1984076dd23SAndrew Thompson 		.timeout = 0,	/* no timeout */
1994076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
2004076dd23SAndrew Thompson 	},
2014076dd23SAndrew Thompson 
2024076dd23SAndrew Thompson 	[CDCE_BULK_TX] = {
2034076dd23SAndrew Thompson 		.type = UE_BULK,
2044076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2054076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
2064076dd23SAndrew Thompson 		.if_index = 0,
2074076dd23SAndrew Thompson 		.frames = CDCE_NCM_TX_FRAMES_MAX,
2084076dd23SAndrew Thompson 		.bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN),
2097e05daaeSHans Petter Selasky 		.flags = {.pipe_bof = 1,},
2104076dd23SAndrew Thompson 		.callback = cdce_ncm_bulk_write_callback,
2114076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2124076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DUAL,	/* both modes */
2134076dd23SAndrew Thompson 	},
2144076dd23SAndrew Thompson 
2154076dd23SAndrew Thompson 	[CDCE_INTR_RX] = {
2164076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2174076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2184076dd23SAndrew Thompson 		.direction = UE_DIR_RX,
2194076dd23SAndrew Thompson 		.if_index = 1,
2204076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2214076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
2224076dd23SAndrew Thompson 		.callback = cdce_intr_read_callback,
2234076dd23SAndrew Thompson 		.timeout = 0,
2244076dd23SAndrew Thompson 		.usb_mode = USB_MODE_HOST,
2254076dd23SAndrew Thompson 	},
2264076dd23SAndrew Thompson 
2274076dd23SAndrew Thompson 	[CDCE_INTR_TX] = {
2284076dd23SAndrew Thompson 		.type = UE_INTERRUPT,
2294076dd23SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
2304076dd23SAndrew Thompson 		.direction = UE_DIR_TX,
2314076dd23SAndrew Thompson 		.if_index = 1,
2324076dd23SAndrew Thompson 		.bufsize = CDCE_IND_SIZE_MAX,
2334076dd23SAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
2344076dd23SAndrew Thompson 		.callback = cdce_intr_write_callback,
2354076dd23SAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
2364076dd23SAndrew Thompson 		.usb_mode = USB_MODE_DEVICE,
2374076dd23SAndrew Thompson 	},
2384076dd23SAndrew Thompson };
2394076dd23SAndrew Thompson #endif
2404076dd23SAndrew Thompson 
24102ac6454SAndrew Thompson static device_method_t cdce_methods[] = {
24202ac6454SAndrew Thompson 	/* USB interface */
24302ac6454SAndrew Thompson 	DEVMETHOD(usb_handle_request, cdce_handle_request),
24402ac6454SAndrew Thompson 
24502ac6454SAndrew Thompson 	/* Device interface */
24602ac6454SAndrew Thompson 	DEVMETHOD(device_probe, cdce_probe),
24702ac6454SAndrew Thompson 	DEVMETHOD(device_attach, cdce_attach),
24802ac6454SAndrew Thompson 	DEVMETHOD(device_detach, cdce_detach),
24902ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, cdce_suspend),
25002ac6454SAndrew Thompson 	DEVMETHOD(device_resume, cdce_resume),
25102ac6454SAndrew Thompson 
25261bfd867SSofian Brabez 	DEVMETHOD_END
25302ac6454SAndrew Thompson };
25402ac6454SAndrew Thompson 
25502ac6454SAndrew Thompson static driver_t cdce_driver = {
25602ac6454SAndrew Thompson 	.name = "cdce",
25702ac6454SAndrew Thompson 	.methods = cdce_methods,
25802ac6454SAndrew Thompson 	.size = sizeof(struct cdce_softc),
25902ac6454SAndrew Thompson };
26002ac6454SAndrew Thompson 
26102ac6454SAndrew Thompson static devclass_t cdce_devclass;
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)},
268c376f439SNick Hibma };
269c376f439SNick Hibma 
270f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID cdce_host_devs[] = {
27102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
27202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
27302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
27402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
27502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
27702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
27802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
27902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
28202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
2832625e519SHiroki Sato 	{USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8156, 0)},
284c376f439SNick Hibma 
285c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
286c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x16),
287c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
288c376f439SNick Hibma 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
289c376f439SNick Hibma 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x46),
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(0x76),
293c376f439SNick Hibma 		USB_DRIVER_INFO(0)},
294cdadbbb0SHans Petter Selasky 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
295cdadbbb0SHans Petter Selasky 		USB_IFACE_SUBCLASS(0x03), USB_IFACE_PROTOCOL(0x16),
296cdadbbb0SHans Petter Selasky 		USB_DRIVER_INFO(0)},
297f1a16106SHans Petter Selasky };
2989739167cSAlfred Perlstein 
299f1a16106SHans Petter Selasky static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = {
3009739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
3019739167cSAlfred Perlstein 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},
3024076dd23SAndrew Thompson 	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)},
30302ac6454SAndrew Thompson };
30402ac6454SAndrew Thompson 
305f809f280SWarner Losh DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, cdce_driver_loaded, 0);
306f809f280SWarner Losh MODULE_VERSION(cdce, 1);
307f809f280SWarner Losh MODULE_DEPEND(cdce, uether, 1, 1, 1);
308f809f280SWarner Losh MODULE_DEPEND(cdce, usb, 1, 1, 1);
309f809f280SWarner Losh MODULE_DEPEND(cdce, ether, 1, 1, 1);
310f809f280SWarner Losh USB_PNP_DEVICE_INFO(cdce_switch_devs);
311f809f280SWarner Losh USB_PNP_HOST_INFO(cdce_host_devs);
312f809f280SWarner Losh USB_PNP_DUAL_INFO(cdce_dual_devs);
313f809f280SWarner Losh 
314f809f280SWarner Losh static const struct usb_ether_methods cdce_ue_methods = {
315f809f280SWarner Losh 	.ue_attach_post = cdce_attach_post,
316b43d600cSJohn-Mark Gurney 	.ue_attach_post_sub = cdce_attach_post_sub,
317f809f280SWarner Losh 	.ue_start = cdce_start,
318f809f280SWarner Losh 	.ue_init = cdce_init,
319f809f280SWarner Losh 	.ue_stop = cdce_stop,
320f809f280SWarner Losh 	.ue_setmulti = cdce_setmulti,
321f809f280SWarner Losh 	.ue_setpromisc = cdce_setpromisc,
322f809f280SWarner Losh };
323f809f280SWarner Losh 
3244076dd23SAndrew Thompson #if CDCE_HAVE_NCM
3254076dd23SAndrew Thompson /*------------------------------------------------------------------------*
3264076dd23SAndrew Thompson  *	cdce_ncm_init
3274076dd23SAndrew Thompson  *
3284076dd23SAndrew Thompson  * Return values:
3294076dd23SAndrew Thompson  * 0: Success
3304076dd23SAndrew Thompson  * Else: Failure
3314076dd23SAndrew Thompson  *------------------------------------------------------------------------*/
3324076dd23SAndrew Thompson static uint8_t
3334076dd23SAndrew Thompson cdce_ncm_init(struct cdce_softc *sc)
3344076dd23SAndrew Thompson {
3354076dd23SAndrew Thompson 	struct usb_ncm_parameters temp;
3364076dd23SAndrew Thompson 	struct usb_device_request req;
3377e05daaeSHans Petter Selasky 	struct usb_ncm_func_descriptor *ufd;
3387e05daaeSHans Petter Selasky 	uint8_t value[8];
3394076dd23SAndrew Thompson 	int err;
3404076dd23SAndrew Thompson 
3417e05daaeSHans Petter Selasky 	ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL,
3426d917491SHans Petter Selasky 	    sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF,
3436d917491SHans Petter Selasky 	    UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF);
3447e05daaeSHans Petter Selasky 
3457e05daaeSHans Petter Selasky 	/* verify length of NCM functional descriptor */
3467e05daaeSHans Petter Selasky 	if (ufd != NULL) {
3477e05daaeSHans Petter Selasky 		if (ufd->bLength < sizeof(*ufd))
3487e05daaeSHans Petter Selasky 			ufd = NULL;
3497e05daaeSHans Petter Selasky 		else
3507e05daaeSHans Petter Selasky 			DPRINTFN(1, "Found NCM functional descriptor.\n");
3517e05daaeSHans Petter Selasky 	}
3527e05daaeSHans Petter Selasky 
3534076dd23SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
3544076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS;
3554076dd23SAndrew Thompson 	USETW(req.wValue, 0);
3564076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
3574076dd23SAndrew Thompson 	req.wIndex[1] = 0;
3584076dd23SAndrew Thompson 	USETW(req.wLength, sizeof(temp));
3594076dd23SAndrew Thompson 
3604076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
3614076dd23SAndrew Thompson 	    &temp, 0, NULL, 1000 /* ms */);
3624076dd23SAndrew Thompson 	if (err)
3634076dd23SAndrew Thompson 		return (1);
3644076dd23SAndrew Thompson 
3654076dd23SAndrew Thompson 	/* Read correct set of parameters according to device mode */
3664076dd23SAndrew Thompson 
3674076dd23SAndrew Thompson 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
368dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize);
369dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize);
3704076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder);
3714076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor);
3724076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment);
3737e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3744076dd23SAndrew Thompson 	} else {
375dd5c0f87SAndrew Thompson 		sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize);
376dd5c0f87SAndrew Thompson 		sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize);
3774076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder);
3784076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor);
3794076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment);
3807e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams);
3814076dd23SAndrew Thompson 	}
3824076dd23SAndrew Thompson 
3834076dd23SAndrew Thompson 	/* Verify maximum receive length */
3844076dd23SAndrew Thompson 
3857e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.rx_max < 32) ||
3864076dd23SAndrew Thompson 	    (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) {
3874076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum receive length\n");
3884076dd23SAndrew Thompson 		sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN;
3894076dd23SAndrew Thompson 	}
3904076dd23SAndrew Thompson 
3914076dd23SAndrew Thompson 	/* Verify maximum transmit length */
3924076dd23SAndrew Thompson 
3937e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_max < 32) ||
3944076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) {
3954076dd23SAndrew Thompson 		DPRINTFN(1, "Using default maximum transmit length\n");
3964076dd23SAndrew Thompson 		sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN;
3974076dd23SAndrew Thompson 	}
3984076dd23SAndrew Thompson 
3994076dd23SAndrew Thompson 	/*
4004076dd23SAndrew Thompson 	 * Verify that the structure alignment is:
4014076dd23SAndrew Thompson 	 * - power of two
4024076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
4034076dd23SAndrew Thompson 	 * - not less than four bytes
4044076dd23SAndrew Thompson 	 */
4057e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_struct_align < 4) ||
4064076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align !=
4074076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) ||
4084076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) {
4094076dd23SAndrew Thompson 		DPRINTFN(1, "Using default other alignment: 4 bytes\n");
4104076dd23SAndrew Thompson 		sc->sc_ncm.tx_struct_align = 4;
4114076dd23SAndrew Thompson 	}
4124076dd23SAndrew Thompson 
4134076dd23SAndrew Thompson 	/*
4144076dd23SAndrew Thompson 	 * Verify that the payload alignment is:
4154076dd23SAndrew Thompson 	 * - power of two
4164076dd23SAndrew Thompson 	 * - not greater than the maximum transmit length
4174076dd23SAndrew Thompson 	 * - not less than four bytes
4184076dd23SAndrew Thompson 	 */
4197e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_modulus < 4) ||
4204076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus !=
4214076dd23SAndrew Thompson 	     ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) ||
4224076dd23SAndrew Thompson 	    (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) {
4234076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit modulus: 4 bytes\n");
4244076dd23SAndrew Thompson 		sc->sc_ncm.tx_modulus = 4;
4254076dd23SAndrew Thompson 	}
4264076dd23SAndrew Thompson 
4274076dd23SAndrew Thompson 	/* Verify that the payload remainder */
4284076dd23SAndrew Thompson 
4297e05daaeSHans Petter Selasky 	if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) {
4304076dd23SAndrew Thompson 		DPRINTFN(1, "Using default transmit remainder: 0 bytes\n");
4314076dd23SAndrew Thompson 		sc->sc_ncm.tx_remainder = 0;
4324076dd23SAndrew Thompson 	}
4334076dd23SAndrew Thompson 
4347e05daaeSHans Petter Selasky 	/*
4357e05daaeSHans Petter Selasky 	 * Offset the TX remainder so that IP packet payload starts at
4367e05daaeSHans Petter Selasky 	 * the tx_modulus. This is not too clear in the specification.
4377e05daaeSHans Petter Selasky 	 */
4387e05daaeSHans Petter Selasky 
4397e05daaeSHans Petter Selasky 	sc->sc_ncm.tx_remainder =
4407e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) &
4417e05daaeSHans Petter Selasky 	    (sc->sc_ncm.tx_modulus - 1);
4427e05daaeSHans Petter Selasky 
4437e05daaeSHans Petter Selasky 	/* Verify max datagrams */
4447e05daaeSHans Petter Selasky 
4457e05daaeSHans Petter Selasky 	if (sc->sc_ncm.tx_nframe == 0 ||
4467e05daaeSHans Petter Selasky 	    sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) {
4477e05daaeSHans Petter Selasky 		DPRINTFN(1, "Using default max "
4487e05daaeSHans Petter Selasky 		    "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1);
4497e05daaeSHans Petter Selasky 		/* need to reserve one entry for zero padding */
4507e05daaeSHans Petter Selasky 		sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1);
4517e05daaeSHans Petter Selasky 	}
4527e05daaeSHans Petter Selasky 
4534076dd23SAndrew Thompson 	/* Additional configuration, will fail in device side mode, which is OK. */
4544076dd23SAndrew Thompson 
4554076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4564076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE;
4574076dd23SAndrew Thompson 	USETW(req.wValue, 0);
4584076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4594076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4607e05daaeSHans Petter Selasky 
4617e05daaeSHans Petter Selasky 	if (ufd != NULL &&
4627e05daaeSHans Petter Selasky 	    (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) {
4637e05daaeSHans Petter Selasky 		USETW(req.wLength, 8);
4647e05daaeSHans Petter Selasky 		USETDW(value, sc->sc_ncm.rx_max);
4657e05daaeSHans Petter Selasky 		USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1));
4667e05daaeSHans Petter Selasky 		USETW(value + 6, 0);
4677e05daaeSHans Petter Selasky 	} else {
4684076dd23SAndrew Thompson 		USETW(req.wLength, 4);
4694076dd23SAndrew Thompson 		USETDW(value, sc->sc_ncm.rx_max);
4707e05daaeSHans Petter Selasky  	}
4714076dd23SAndrew Thompson 
4724076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4734076dd23SAndrew Thompson 	    &value, 0, NULL, 1000 /* ms */);
4744076dd23SAndrew Thompson 	if (err) {
4754076dd23SAndrew Thompson 		DPRINTFN(1, "Setting input size "
4764076dd23SAndrew Thompson 		    "to %u failed.\n", sc->sc_ncm.rx_max);
4774076dd23SAndrew Thompson 	}
4784076dd23SAndrew Thompson 
4794076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4804076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_CRC_MODE;
4814076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* no CRC */
4824076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4834076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4844076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4854076dd23SAndrew Thompson 
4864076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
4874076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
4884076dd23SAndrew Thompson 	if (err) {
4894076dd23SAndrew Thompson 		DPRINTFN(1, "Setting CRC mode to off failed.\n");
4904076dd23SAndrew Thompson 	}
4914076dd23SAndrew Thompson 
4924076dd23SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
4934076dd23SAndrew Thompson 	req.bRequest = UCDC_NCM_SET_NTB_FORMAT;
4944076dd23SAndrew Thompson 	USETW(req.wValue, 0);	/* NTB-16 */
4954076dd23SAndrew Thompson 	req.wIndex[0] = sc->sc_ifaces_index[1];
4964076dd23SAndrew Thompson 	req.wIndex[1] = 0;
4974076dd23SAndrew Thompson 	USETW(req.wLength, 0);
4984076dd23SAndrew Thompson 
4994076dd23SAndrew Thompson 	err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req,
5004076dd23SAndrew Thompson 	    NULL, 0, NULL, 1000 /* ms */);
5014076dd23SAndrew Thompson 	if (err) {
5024076dd23SAndrew Thompson 		DPRINTFN(1, "Setting NTB format to 16-bit failed.\n");
5034076dd23SAndrew Thompson 	}
5044076dd23SAndrew Thompson 
5054076dd23SAndrew Thompson 	return (0);		/* success */
5064076dd23SAndrew Thompson }
5074076dd23SAndrew Thompson #endif
5084076dd23SAndrew Thompson 
509c376f439SNick Hibma static void
510c376f439SNick Hibma cdce_test_autoinst(void *arg, struct usb_device *udev,
511c376f439SNick Hibma     struct usb_attach_arg *uaa)
512c376f439SNick Hibma {
513c376f439SNick Hibma 	struct usb_interface *iface;
514c376f439SNick Hibma 	struct usb_interface_descriptor *id;
515c376f439SNick Hibma 
516c376f439SNick Hibma 	if (uaa->dev_state != UAA_DEV_READY)
517c376f439SNick Hibma 		return;
518c376f439SNick Hibma 
519c376f439SNick Hibma 	iface = usbd_get_iface(udev, 0);
520c376f439SNick Hibma 	if (iface == NULL)
521c376f439SNick Hibma 		return;
522c376f439SNick Hibma 	id = iface->idesc;
523c376f439SNick Hibma 	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
524c376f439SNick Hibma 		return;
525c376f439SNick Hibma 	if (usbd_lookup_id_by_uaa(cdce_switch_devs, sizeof(cdce_switch_devs), uaa))
526c376f439SNick Hibma 		return;		/* no device match */
527c376f439SNick Hibma 
528c376f439SNick Hibma 	if (usb_msc_eject(udev, 0, USB_GET_DRIVER_INFO(uaa)) == 0) {
529c376f439SNick Hibma 		/* success, mark the udev as disappearing */
530c376f439SNick Hibma 		uaa->dev_state = UAA_DEV_EJECTING;
531c376f439SNick Hibma 	}
532c376f439SNick Hibma }
533c376f439SNick Hibma 
534c376f439SNick Hibma static int
535c376f439SNick Hibma cdce_driver_loaded(struct module *mod, int what, void *arg)
536c376f439SNick Hibma {
537c376f439SNick Hibma 	switch (what) {
538c376f439SNick Hibma 	case MOD_LOAD:
539c376f439SNick Hibma 		/* register our autoinstall handler */
540c376f439SNick Hibma 		cdce_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
541c376f439SNick Hibma 		    cdce_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
542c376f439SNick Hibma 		return (0);
543c376f439SNick Hibma 	case MOD_UNLOAD:
544c376f439SNick Hibma 		EVENTHANDLER_DEREGISTER(usb_dev_configured, cdce_etag);
545c376f439SNick Hibma 		return (0);
546c376f439SNick Hibma 	default:
547c376f439SNick Hibma 		return (EOPNOTSUPP);
548c376f439SNick Hibma 	}
549c376f439SNick Hibma }
550c376f439SNick Hibma 
55102ac6454SAndrew Thompson static int
55202ac6454SAndrew Thompson cdce_probe(device_t dev)
55302ac6454SAndrew Thompson {
554760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
555f1a16106SHans Petter Selasky 	int error;
55602ac6454SAndrew Thompson 
557f1a16106SHans Petter Selasky 	error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa);
558f1a16106SHans Petter Selasky 	if (error)
559f1a16106SHans Petter Selasky 		error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa);
560f1a16106SHans Petter Selasky 	return (error);
56102ac6454SAndrew Thompson }
56202ac6454SAndrew Thompson 
56302ac6454SAndrew Thompson static void
564760bc48eSAndrew Thompson cdce_attach_post(struct usb_ether *ue)
56502ac6454SAndrew Thompson {
56602ac6454SAndrew Thompson 	/* no-op */
56702ac6454SAndrew Thompson 	return;
56802ac6454SAndrew Thompson }
56902ac6454SAndrew Thompson 
57002ac6454SAndrew Thompson static int
571b43d600cSJohn-Mark Gurney cdce_attach_post_sub(struct usb_ether *ue)
572b43d600cSJohn-Mark Gurney {
573b43d600cSJohn-Mark Gurney 	struct cdce_softc *sc = uether_getsc(ue);
574b43d600cSJohn-Mark Gurney 	struct ifnet *ifp = uether_getifp(ue);
575b43d600cSJohn-Mark Gurney 
576b43d600cSJohn-Mark Gurney 	/* mostly copied from usb_ethernet.c */
577b43d600cSJohn-Mark Gurney 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
578b43d600cSJohn-Mark Gurney 	ifp->if_start = uether_start;
579b43d600cSJohn-Mark Gurney 	ifp->if_ioctl = cdce_ioctl;
580b43d600cSJohn-Mark Gurney 	ifp->if_init = uether_init;
581b43d600cSJohn-Mark Gurney 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
582b43d600cSJohn-Mark Gurney 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
583b43d600cSJohn-Mark Gurney 	IFQ_SET_READY(&ifp->if_snd);
584b43d600cSJohn-Mark Gurney 
585b43d600cSJohn-Mark Gurney 	if ((sc->sc_flags & CDCE_FLAG_VLAN) == CDCE_FLAG_VLAN)
586b43d600cSJohn-Mark Gurney 		if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
587b43d600cSJohn-Mark Gurney 
588b43d600cSJohn-Mark Gurney 	if_setcapabilitiesbit(ifp, IFCAP_LINKSTATE, 0);
589b43d600cSJohn-Mark Gurney 	if_setcapenable(ifp, if_getcapabilities(ifp));
590b43d600cSJohn-Mark Gurney 
591b43d600cSJohn-Mark Gurney 	ifmedia_init(&sc->sc_media, IFM_IMASK, cdce_media_change_cb,
592b43d600cSJohn-Mark Gurney 	    cdce_media_status_cb);
593b43d600cSJohn-Mark Gurney 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
594b43d600cSJohn-Mark Gurney 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
595b43d600cSJohn-Mark Gurney 	sc->sc_media.ifm_media = sc->sc_media.ifm_cur->ifm_media;
596b43d600cSJohn-Mark Gurney 
597b43d600cSJohn-Mark Gurney 	return 0;
598b43d600cSJohn-Mark Gurney }
599b43d600cSJohn-Mark Gurney 
600b43d600cSJohn-Mark Gurney static int
60102ac6454SAndrew Thompson cdce_attach(device_t dev)
60202ac6454SAndrew Thompson {
60302ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
604760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
605760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
606760bc48eSAndrew Thompson 	struct usb_interface *iface;
607760bc48eSAndrew Thompson 	const struct usb_cdc_union_descriptor *ud;
608760bc48eSAndrew Thompson 	const struct usb_interface_descriptor *id;
609760bc48eSAndrew Thompson 	const struct usb_cdc_ethernet_descriptor *ued;
6104076dd23SAndrew Thompson 	const struct usb_config *pcfg;
611a8df530dSJohn Baldwin 	uint32_t seed;
61202ac6454SAndrew Thompson 	int error;
61302ac6454SAndrew Thompson 	uint8_t i;
6144076dd23SAndrew Thompson 	uint8_t data_iface_no;
61502ac6454SAndrew Thompson 	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */
61602ac6454SAndrew Thompson 
61702ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
6184076dd23SAndrew Thompson 	sc->sc_ue.ue_udev = uaa->device;
61902ac6454SAndrew Thompson 
620a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
62302ac6454SAndrew Thompson 
6243c8d24f4SAndrew Thompson 	ud = usbd_find_descriptor
62502ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
6266d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF);
62702ac6454SAndrew Thompson 
6284076dd23SAndrew Thompson 	if ((ud == NULL) || (ud->bLength < sizeof(*ud)) ||
6294076dd23SAndrew Thompson 	    (sc->sc_flags & CDCE_FLAG_NO_UNION)) {
6304076dd23SAndrew Thompson 		DPRINTFN(1, "No union descriptor!\n");
6314076dd23SAndrew Thompson 		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
6324076dd23SAndrew Thompson 		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
6334076dd23SAndrew Thompson 		goto alloc_transfers;
63402ac6454SAndrew Thompson 	}
6354076dd23SAndrew Thompson 	data_iface_no = ud->bSlaveInterface[0];
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	for (i = 0;; i++) {
638a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
63902ac6454SAndrew Thompson 
64002ac6454SAndrew Thompson 		if (iface) {
641a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
64202ac6454SAndrew Thompson 
6434076dd23SAndrew Thompson 			if (id && (id->bInterfaceNumber == data_iface_no)) {
64402ac6454SAndrew Thompson 				sc->sc_ifaces_index[0] = i;
64502ac6454SAndrew Thompson 				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
646a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
64702ac6454SAndrew Thompson 				break;
64802ac6454SAndrew Thompson 			}
64902ac6454SAndrew Thompson 		} else {
650767cb2e2SAndrew Thompson 			device_printf(dev, "no data interface found\n");
65102ac6454SAndrew Thompson 			goto detach;
65202ac6454SAndrew Thompson 		}
65302ac6454SAndrew Thompson 	}
65402ac6454SAndrew Thompson 
65502ac6454SAndrew Thompson 	/*
65602ac6454SAndrew Thompson 	 * <quote>
65702ac6454SAndrew Thompson 	 *
65802ac6454SAndrew Thompson 	 *  The Data Class interface of a networking device shall have
65902ac6454SAndrew Thompson 	 *  a minimum of two interface settings. The first setting
66002ac6454SAndrew Thompson 	 *  (the default interface setting) includes no endpoints and
66102ac6454SAndrew Thompson 	 *  therefore no networking traffic is exchanged whenever the
66202ac6454SAndrew Thompson 	 *  default interface setting is selected. One or more
66302ac6454SAndrew Thompson 	 *  additional interface settings are used for normal
66402ac6454SAndrew Thompson 	 *  operation, and therefore each includes a pair of endpoints
66502ac6454SAndrew Thompson 	 *  (one IN, and one OUT) to exchange network traffic. Select
66602ac6454SAndrew Thompson 	 *  an alternate interface setting to initialize the network
66702ac6454SAndrew Thompson 	 *  aspects of the device and to enable the exchange of
66802ac6454SAndrew Thompson 	 *  network traffic.
66902ac6454SAndrew Thompson 	 *
67002ac6454SAndrew Thompson 	 * </quote>
67102ac6454SAndrew Thompson 	 *
67202ac6454SAndrew Thompson 	 * Some devices, most notably cable modems, include interface
67302ac6454SAndrew Thompson 	 * settings that have no IN or OUT endpoint, therefore loop
67402ac6454SAndrew Thompson 	 * through the list of all available interface settings
67502ac6454SAndrew Thompson 	 * looking for one with both IN and OUT endpoints.
67602ac6454SAndrew Thompson 	 */
67702ac6454SAndrew Thompson 
67802ac6454SAndrew Thompson alloc_transfers:
67902ac6454SAndrew Thompson 
6804076dd23SAndrew Thompson 	pcfg = cdce_config;	/* Default Configuration */
6814076dd23SAndrew Thompson 
68202ac6454SAndrew Thompson 	for (i = 0; i != 32; i++) {
6834076dd23SAndrew Thompson 		error = usbd_set_alt_interface_index(uaa->device,
6844076dd23SAndrew Thompson 		    sc->sc_ifaces_index[0], i);
6854076dd23SAndrew Thompson 		if (error)
6864076dd23SAndrew Thompson 			break;
6874076dd23SAndrew Thompson #if CDCE_HAVE_NCM
6884076dd23SAndrew Thompson 		if ((i == 0) && (cdce_ncm_init(sc) == 0))
6894076dd23SAndrew Thompson 			pcfg = cdce_ncm_config;
6904076dd23SAndrew Thompson #endif
6914076dd23SAndrew Thompson 		error = usbd_transfer_setup(uaa->device,
6924076dd23SAndrew Thompson 		    sc->sc_ifaces_index, sc->sc_xfer,
6934076dd23SAndrew Thompson 		    pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx);
69402ac6454SAndrew Thompson 
6954076dd23SAndrew Thompson 		if (error == 0)
69602ac6454SAndrew Thompson 			break;
69702ac6454SAndrew Thompson 	}
6984076dd23SAndrew Thompson 
6994076dd23SAndrew Thompson 	if (error || (i == 32)) {
7004076dd23SAndrew Thompson 		device_printf(dev, "No valid alternate "
701767cb2e2SAndrew Thompson 		    "setting found\n");
7024076dd23SAndrew Thompson 		goto detach;
70302ac6454SAndrew Thompson 	}
70402ac6454SAndrew Thompson 
7053c8d24f4SAndrew Thompson 	ued = usbd_find_descriptor
70602ac6454SAndrew Thompson 	    (uaa->device, NULL, uaa->info.bIfaceIndex,
7076d917491SHans Petter Selasky 	    UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF);
70802ac6454SAndrew Thompson 
70902ac6454SAndrew Thompson 	if ((ued == NULL) || (ued->bLength < sizeof(*ued))) {
71002ac6454SAndrew Thompson 		error = USB_ERR_INVAL;
71102ac6454SAndrew Thompson 	} else {
712b43d600cSJohn-Mark Gurney 		/*
713b43d600cSJohn-Mark Gurney 		 * ECM 1.2 doesn't say it excludes the CRC, but states that it's
714b43d600cSJohn-Mark Gurney 		 * normally 1514, which excludes the CRC.
715b43d600cSJohn-Mark Gurney 		 */
716b43d600cSJohn-Mark Gurney 		DPRINTF("max segsize: %d\n", UGETW(ued->wMaxSegmentSize));
717b43d600cSJohn-Mark Gurney 		if (UGETW(ued->wMaxSegmentSize) >= (ETHER_MAX_LEN - ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN))
718b43d600cSJohn-Mark Gurney 			sc->sc_flags |= CDCE_FLAG_VLAN;
719b43d600cSJohn-Mark Gurney 
720*3d510418SJohn-Mark Gurney 		error = usbd_req_get_string_any(uaa->device, NULL,
721*3d510418SJohn-Mark Gurney 		    eaddr_str, sizeof(eaddr_str), ued->iMacAddress);
722*3d510418SJohn-Mark Gurney 	}
723*3d510418SJohn-Mark Gurney 
72402ac6454SAndrew Thompson 	if (error) {
72502ac6454SAndrew Thompson 		/* fake MAC address */
72602ac6454SAndrew Thompson 
72702ac6454SAndrew Thompson 		device_printf(dev, "faking MAC address\n");
728a8df530dSJohn Baldwin 		seed = ticks;
72902ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[0] = 0x2a;
730a8df530dSJohn Baldwin 		memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t));
73102ac6454SAndrew Thompson 		sc->sc_ue.ue_eaddr[5] = device_get_unit(dev);
73202ac6454SAndrew Thompson 
73302ac6454SAndrew Thompson 	} else {
7347e05daaeSHans Petter Selasky 		memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr));
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson 		for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) {
73702ac6454SAndrew Thompson 			char c = eaddr_str[i];
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 			if ('0' <= c && c <= '9')
74002ac6454SAndrew Thompson 				c -= '0';
74102ac6454SAndrew Thompson 			else if (c != 0)
74202ac6454SAndrew Thompson 				c -= 'A' - 10;
74302ac6454SAndrew Thompson 			else
74402ac6454SAndrew Thompson 				break;
74502ac6454SAndrew Thompson 
74602ac6454SAndrew Thompson 			c &= 0xf;
74702ac6454SAndrew Thompson 
74802ac6454SAndrew Thompson 			if ((i & 1) == 0)
74902ac6454SAndrew Thompson 				c <<= 4;
75002ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[i / 2] |= c;
75102ac6454SAndrew Thompson 		}
75202ac6454SAndrew Thompson 
753f29a0724SAndrew Thompson 		if (uaa->usb_mode == USB_MODE_DEVICE) {
75402ac6454SAndrew Thompson 			/*
75502ac6454SAndrew Thompson 			 * Do not use the same MAC address like the peer !
75602ac6454SAndrew Thompson 			 */
75702ac6454SAndrew Thompson 			sc->sc_ue.ue_eaddr[5] ^= 0xFF;
75802ac6454SAndrew Thompson 		}
75902ac6454SAndrew Thompson 	}
76002ac6454SAndrew Thompson 
76102ac6454SAndrew Thompson 	ue->ue_sc = sc;
76202ac6454SAndrew Thompson 	ue->ue_dev = dev;
76302ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
76402ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
76502ac6454SAndrew Thompson 	ue->ue_methods = &cdce_ue_methods;
76602ac6454SAndrew Thompson 
767a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
76802ac6454SAndrew Thompson 	if (error) {
76902ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
77002ac6454SAndrew Thompson 		goto detach;
77102ac6454SAndrew Thompson 	}
77202ac6454SAndrew Thompson 	return (0);			/* success */
77302ac6454SAndrew Thompson 
77402ac6454SAndrew Thompson detach:
77502ac6454SAndrew Thompson 	cdce_detach(dev);
77602ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
77702ac6454SAndrew Thompson }
77802ac6454SAndrew Thompson 
77902ac6454SAndrew Thompson static int
78002ac6454SAndrew Thompson cdce_detach(device_t dev)
78102ac6454SAndrew Thompson {
78202ac6454SAndrew Thompson 	struct cdce_softc *sc = device_get_softc(dev);
783760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson 	/* stop all USB transfers first */
786a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);
787a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
78802ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
78902ac6454SAndrew Thompson 
790b43d600cSJohn-Mark Gurney 	ifmedia_removeall(&sc->sc_media);
791b43d600cSJohn-Mark Gurney 
79202ac6454SAndrew Thompson 	return (0);
79302ac6454SAndrew Thompson }
79402ac6454SAndrew Thompson 
79502ac6454SAndrew Thompson static void
796760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue)
79702ac6454SAndrew Thompson {
798a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
79902ac6454SAndrew Thompson 
80002ac6454SAndrew Thompson 	/*
80102ac6454SAndrew Thompson 	 * Start the USB transfers, if not already started:
80202ac6454SAndrew Thompson 	 */
803a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]);
804a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]);
80502ac6454SAndrew Thompson }
80602ac6454SAndrew Thompson 
807b43d600cSJohn-Mark Gurney static int
808b43d600cSJohn-Mark Gurney cdce_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
809b43d600cSJohn-Mark Gurney {
810b43d600cSJohn-Mark Gurney 	struct usb_ether *ue = ifp->if_softc;
811b43d600cSJohn-Mark Gurney 	struct cdce_softc *sc = uether_getsc(ue);
812b43d600cSJohn-Mark Gurney 	struct ifreq *ifr = (struct ifreq *)data;
813b43d600cSJohn-Mark Gurney 	int error;
814b43d600cSJohn-Mark Gurney 
815b43d600cSJohn-Mark Gurney 	error = 0;
816b43d600cSJohn-Mark Gurney 
817b43d600cSJohn-Mark Gurney 	switch(command) {
818b43d600cSJohn-Mark Gurney 	case SIOCGIFMEDIA:
819b43d600cSJohn-Mark Gurney 	case SIOCSIFMEDIA:
820b43d600cSJohn-Mark Gurney 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
821b43d600cSJohn-Mark Gurney 		break;
822b43d600cSJohn-Mark Gurney 	default:
823b43d600cSJohn-Mark Gurney 		error = uether_ioctl(ifp, command, data);
824b43d600cSJohn-Mark Gurney 		break;
825b43d600cSJohn-Mark Gurney 	}
826b43d600cSJohn-Mark Gurney 
827b43d600cSJohn-Mark Gurney 	return (error);
828b43d600cSJohn-Mark Gurney }
829b43d600cSJohn-Mark Gurney 
83002ac6454SAndrew Thompson static void
83102ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n)
83202ac6454SAndrew Thompson {
83302ac6454SAndrew Thompson 	uint8_t x;
83402ac6454SAndrew Thompson 	for (x = 0; x != n; x++) {
83502ac6454SAndrew Thompson 		if (ppm[x] != NULL) {
83602ac6454SAndrew Thompson 			m_freem(ppm[x]);
83702ac6454SAndrew Thompson 			ppm[x] = NULL;
83802ac6454SAndrew Thompson 		}
83902ac6454SAndrew Thompson 	}
84002ac6454SAndrew Thompson }
84102ac6454SAndrew Thompson 
842b43d600cSJohn-Mark Gurney static int
843b43d600cSJohn-Mark Gurney cdce_media_change_cb(struct ifnet *ifp)
844b43d600cSJohn-Mark Gurney {
845b43d600cSJohn-Mark Gurney 
846b43d600cSJohn-Mark Gurney 	return (EOPNOTSUPP);
847b43d600cSJohn-Mark Gurney }
848b43d600cSJohn-Mark Gurney 
849b43d600cSJohn-Mark Gurney static void
850b43d600cSJohn-Mark Gurney cdce_media_status_cb(struct ifnet *ifp, struct ifmediareq *ifmr)
851b43d600cSJohn-Mark Gurney {
852b43d600cSJohn-Mark Gurney 
853b43d600cSJohn-Mark Gurney 	if ((if_getflags(ifp) & IFF_UP) == 0)
854b43d600cSJohn-Mark Gurney 		return;
855b43d600cSJohn-Mark Gurney 
856b43d600cSJohn-Mark Gurney 	ifmr->ifm_active = IFM_ETHER;
857b43d600cSJohn-Mark Gurney 	ifmr->ifm_status = IFM_AVALID;
858b43d600cSJohn-Mark Gurney 	ifmr->ifm_status |=
859b43d600cSJohn-Mark Gurney 	    ifp->if_link_state == LINK_STATE_UP ? IFM_ACTIVE : 0;
860b43d600cSJohn-Mark Gurney }
861b43d600cSJohn-Mark Gurney 
86202ac6454SAndrew Thompson static void
863ed6d949aSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
86402ac6454SAndrew Thompson {
865ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
866a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
86702ac6454SAndrew Thompson 	struct mbuf *m;
86802ac6454SAndrew Thompson 	struct mbuf *mt;
86902ac6454SAndrew Thompson 	uint32_t crc;
87002ac6454SAndrew Thompson 	uint8_t x;
871ed6d949aSAndrew Thompson 	int actlen, aframes;
872ed6d949aSAndrew Thompson 
873ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
87402ac6454SAndrew Thompson 
87502ac6454SAndrew Thompson 	DPRINTFN(1, "\n");
87602ac6454SAndrew Thompson 
87702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
87802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
879ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
880ed6d949aSAndrew Thompson 		    actlen, aframes);
88102ac6454SAndrew Thompson 
882ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
88302ac6454SAndrew Thompson 
88402ac6454SAndrew Thompson 		/* free all previous TX buffers */
88502ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson 		/* FALLTHROUGH */
88802ac6454SAndrew Thompson 	case USB_ST_SETUP:
88902ac6454SAndrew Thompson tr_setup:
89002ac6454SAndrew Thompson 		for (x = 0; x != CDCE_FRAMES_MAX; x++) {
89102ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
89202ac6454SAndrew Thompson 
89302ac6454SAndrew Thompson 			if (m == NULL)
89402ac6454SAndrew Thompson 				break;
89502ac6454SAndrew Thompson 
89602ac6454SAndrew Thompson 			if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
89702ac6454SAndrew Thompson 				/*
89802ac6454SAndrew Thompson 				 * Zaurus wants a 32-bit CRC appended
89902ac6454SAndrew Thompson 				 * to every frame
90002ac6454SAndrew Thompson 				 */
90102ac6454SAndrew Thompson 
90202ac6454SAndrew Thompson 				crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
90302ac6454SAndrew Thompson 				crc = htole32(crc);
90402ac6454SAndrew Thompson 
90502ac6454SAndrew Thompson 				if (!m_append(m, 4, (void *)&crc)) {
90602ac6454SAndrew Thompson 					m_freem(m);
907ecc70d3fSGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
90802ac6454SAndrew Thompson 					continue;
90902ac6454SAndrew Thompson 				}
91002ac6454SAndrew Thompson 			}
91102ac6454SAndrew Thompson 			if (m->m_len != m->m_pkthdr.len) {
912c6499eccSGleb Smirnoff 				mt = m_defrag(m, M_NOWAIT);
91302ac6454SAndrew Thompson 				if (mt == NULL) {
91402ac6454SAndrew Thompson 					m_freem(m);
915ecc70d3fSGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
91602ac6454SAndrew Thompson 					continue;
91702ac6454SAndrew Thompson 				}
91802ac6454SAndrew Thompson 				m = mt;
91902ac6454SAndrew Thompson 			}
92002ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
92102ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
92202ac6454SAndrew Thompson 			}
92302ac6454SAndrew Thompson 			sc->sc_tx_buf[x] = m;
924ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
92502ac6454SAndrew Thompson 
92602ac6454SAndrew Thompson 			/*
92702ac6454SAndrew Thompson 			 * If there's a BPF listener, bounce a copy of
92802ac6454SAndrew Thompson 			 * this frame to him:
92902ac6454SAndrew Thompson 			 */
93002ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
93102ac6454SAndrew Thompson 		}
93202ac6454SAndrew Thompson 		if (x != 0) {
933ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
934ed6d949aSAndrew Thompson 
935a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
93602ac6454SAndrew Thompson 		}
93702ac6454SAndrew Thompson 		break;
93802ac6454SAndrew Thompson 
93902ac6454SAndrew Thompson 	default:			/* Error */
94002ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
941ed6d949aSAndrew Thompson 		    usbd_errstr(error));
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 		/* free all previous TX buffers */
94402ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX);
94502ac6454SAndrew Thompson 
94602ac6454SAndrew Thompson 		/* count output errors */
947ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
94802ac6454SAndrew Thompson 
949ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
9507d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
95102ac6454SAndrew Thompson 				/* try to clear stall first */
952ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
95302ac6454SAndrew Thompson 			}
954d69eefebSRuslan Bukin 			goto tr_setup;
9557d502f32SRuslan Bukin 		}
95602ac6454SAndrew Thompson 		break;
95702ac6454SAndrew Thompson 	}
95802ac6454SAndrew Thompson }
95902ac6454SAndrew Thompson 
96002ac6454SAndrew Thompson static int32_t
96102ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
96202ac6454SAndrew Thompson {
96302ac6454SAndrew Thompson 	uint32_t *p_crc = arg;
96402ac6454SAndrew Thompson 
96502ac6454SAndrew Thompson 	*p_crc = crc32_raw(src, count, *p_crc);
96602ac6454SAndrew Thompson 	return (0);
96702ac6454SAndrew Thompson }
96802ac6454SAndrew Thompson 
96902ac6454SAndrew Thompson static uint32_t
97002ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
97102ac6454SAndrew Thompson {
97202ac6454SAndrew Thompson 	uint32_t crc = 0xFFFFFFFF;
97302ac6454SAndrew Thompson 	int error;
97402ac6454SAndrew Thompson 
97502ac6454SAndrew Thompson 	error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc);
97602ac6454SAndrew Thompson 	return (crc ^ 0xFFFFFFFF);
97702ac6454SAndrew Thompson }
97802ac6454SAndrew Thompson 
97902ac6454SAndrew Thompson static void
980760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue)
98102ac6454SAndrew Thompson {
982a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
983a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
98402ac6454SAndrew Thompson 
98502ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
98602ac6454SAndrew Thompson 
98702ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
98802ac6454SAndrew Thompson 
98902ac6454SAndrew Thompson 	/* start interrupt transfer */
990a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]);
991a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
99202ac6454SAndrew Thompson 
993b0b74fb3SRuslan Bukin 	/*
994b0b74fb3SRuslan Bukin 	 * Stall data write direction, which depends on USB mode.
995b0b74fb3SRuslan Bukin 	 *
996b0b74fb3SRuslan Bukin 	 * Some USB host stacks (e.g. Mac OS X) don't clears stall
997b0b74fb3SRuslan Bukin 	 * bit as it should, so set it in our host mode only.
998b0b74fb3SRuslan Bukin 	 */
999b0b74fb3SRuslan Bukin 	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1000ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]);
100102ac6454SAndrew Thompson 
100202ac6454SAndrew Thompson 	/* start data transfers */
100302ac6454SAndrew Thompson 	cdce_start(ue);
100402ac6454SAndrew Thompson }
100502ac6454SAndrew Thompson 
100602ac6454SAndrew Thompson static void
1007760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue)
100802ac6454SAndrew Thompson {
1009a593f6b8SAndrew Thompson 	struct cdce_softc *sc = uether_getsc(ue);
1010a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
101102ac6454SAndrew Thompson 
101202ac6454SAndrew Thompson 	CDCE_LOCK_ASSERT(sc, MA_OWNED);
101302ac6454SAndrew Thompson 
101402ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
101502ac6454SAndrew Thompson 
101602ac6454SAndrew Thompson 	/*
101702ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
101802ac6454SAndrew Thompson 	 */
1019a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]);
1020a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]);
1021a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]);
1022a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]);
102302ac6454SAndrew Thompson }
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson static void
1026760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue)
102702ac6454SAndrew Thompson {
102802ac6454SAndrew Thompson 	/* no-op */
102902ac6454SAndrew Thompson 	return;
103002ac6454SAndrew Thompson }
103102ac6454SAndrew Thompson 
103202ac6454SAndrew Thompson static void
1033760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue)
103402ac6454SAndrew Thompson {
103502ac6454SAndrew Thompson 	/* no-op */
103602ac6454SAndrew Thompson 	return;
103702ac6454SAndrew Thompson }
103802ac6454SAndrew Thompson 
103902ac6454SAndrew Thompson static int
104002ac6454SAndrew Thompson cdce_suspend(device_t dev)
104102ac6454SAndrew Thompson {
104202ac6454SAndrew Thompson 	device_printf(dev, "Suspending\n");
104302ac6454SAndrew Thompson 	return (0);
104402ac6454SAndrew Thompson }
104502ac6454SAndrew Thompson 
104602ac6454SAndrew Thompson static int
104702ac6454SAndrew Thompson cdce_resume(device_t dev)
104802ac6454SAndrew Thompson {
104902ac6454SAndrew Thompson 	device_printf(dev, "Resuming\n");
105002ac6454SAndrew Thompson 	return (0);
105102ac6454SAndrew Thompson }
105202ac6454SAndrew Thompson 
105302ac6454SAndrew Thompson static void
1054ed6d949aSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
105502ac6454SAndrew Thompson {
1056ed6d949aSAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
105702ac6454SAndrew Thompson 	struct mbuf *m;
105802ac6454SAndrew Thompson 	uint8_t x;
10596d917491SHans Petter Selasky 	int actlen;
10606d917491SHans Petter Selasky 	int aframes;
10616d917491SHans Petter Selasky 	int len;
1062ed6d949aSAndrew Thompson 
1063ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
106402ac6454SAndrew Thompson 
106502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
106602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
106702ac6454SAndrew Thompson 
1068ed6d949aSAndrew Thompson 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
106902ac6454SAndrew Thompson 
1070ed6d949aSAndrew Thompson 		for (x = 0; x != aframes; x++) {
107102ac6454SAndrew Thompson 			m = sc->sc_rx_buf[x];
107202ac6454SAndrew Thompson 			sc->sc_rx_buf[x] = NULL;
10738f9e0ef9SAndrew Thompson 			len = usbd_xfer_frame_len(xfer, x);
107402ac6454SAndrew Thompson 
107502ac6454SAndrew Thompson 			/* Strip off CRC added by Zaurus, if any */
1076ed6d949aSAndrew Thompson 			if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14)
1077ed6d949aSAndrew Thompson 				len -= 4;
107802ac6454SAndrew Thompson 
10796d917491SHans Petter Selasky 			if (len < (int)sizeof(struct ether_header)) {
108002ac6454SAndrew Thompson 				m_freem(m);
108102ac6454SAndrew Thompson 				continue;
108202ac6454SAndrew Thompson 			}
108302ac6454SAndrew Thompson 			/* queue up mbuf */
1084ed6d949aSAndrew Thompson 			uether_rxmbuf(&sc->sc_ue, m, len);
108502ac6454SAndrew Thompson 		}
108602ac6454SAndrew Thompson 
108702ac6454SAndrew Thompson 		/* FALLTHROUGH */
108802ac6454SAndrew Thompson 	case USB_ST_SETUP:
108902ac6454SAndrew Thompson 		/*
109002ac6454SAndrew Thompson 		 * TODO: Implement support for multi frame transfers,
109102ac6454SAndrew Thompson 		 * when the USB hardware supports it.
109202ac6454SAndrew Thompson 		 */
109302ac6454SAndrew Thompson 		for (x = 0; x != 1; x++) {
109402ac6454SAndrew Thompson 			if (sc->sc_rx_buf[x] == NULL) {
1095a593f6b8SAndrew Thompson 				m = uether_newbuf();
109602ac6454SAndrew Thompson 				if (m == NULL)
109702ac6454SAndrew Thompson 					goto tr_stall;
109802ac6454SAndrew Thompson 				sc->sc_rx_buf[x] = m;
109902ac6454SAndrew Thompson 			} else {
110002ac6454SAndrew Thompson 				m = sc->sc_rx_buf[x];
110102ac6454SAndrew Thompson 			}
110202ac6454SAndrew Thompson 
1103ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
110402ac6454SAndrew Thompson 		}
110502ac6454SAndrew Thompson 		/* set number of frames and start hardware */
1106ed6d949aSAndrew Thompson 		usbd_xfer_set_frames(xfer, x);
1107a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
110802ac6454SAndrew Thompson 		/* flush any received frames */
1109a593f6b8SAndrew Thompson 		uether_rxflush(&sc->sc_ue);
111002ac6454SAndrew Thompson 		break;
111102ac6454SAndrew Thompson 
111202ac6454SAndrew Thompson 	default:			/* Error */
111302ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
1114ed6d949aSAndrew Thompson 		    usbd_errstr(error));
111502ac6454SAndrew Thompson 
1116ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
111702ac6454SAndrew Thompson tr_stall:
11187d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
111902ac6454SAndrew Thompson 				/* try to clear stall first */
1120ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
1121ed6d949aSAndrew Thompson 				usbd_xfer_set_frames(xfer, 0);
1122a593f6b8SAndrew Thompson 				usbd_transfer_submit(xfer);
11237d502f32SRuslan Bukin 			}
112402ac6454SAndrew Thompson 			break;
112502ac6454SAndrew Thompson 		}
112602ac6454SAndrew Thompson 
112702ac6454SAndrew Thompson 		/* need to free the RX-mbufs when we are cancelled */
112802ac6454SAndrew Thompson 		cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX);
112902ac6454SAndrew Thompson 		break;
113002ac6454SAndrew Thompson 	}
113102ac6454SAndrew Thompson }
113202ac6454SAndrew Thompson 
113302ac6454SAndrew Thompson static void
1134ed6d949aSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
113502ac6454SAndrew Thompson {
1136b43d600cSJohn-Mark Gurney 	u_char buf[CDCE_IND_SIZE_MAX];
1137b43d600cSJohn-Mark Gurney 	struct usb_cdc_notification ucn;
1138b43d600cSJohn-Mark Gurney 	struct cdce_softc *sc;
1139b43d600cSJohn-Mark Gurney 	struct ifnet *ifp;
1140b43d600cSJohn-Mark Gurney 	struct usb_page_cache *pc;
1141b43d600cSJohn-Mark Gurney 	int off, actlen;
1142b43d600cSJohn-Mark Gurney 	uint32_t downrate, uprate;
1143b43d600cSJohn-Mark Gurney 
1144b43d600cSJohn-Mark Gurney 	sc = usbd_xfer_softc(xfer);
1145b43d600cSJohn-Mark Gurney 	ifp = uether_getifp(&sc->sc_ue);
1146b43d600cSJohn-Mark Gurney 	pc = usbd_xfer_get_frame(xfer, 0);
1147ed6d949aSAndrew Thompson 
1148ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1149ed6d949aSAndrew Thompson 
115002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
115102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
1152b43d600cSJohn-Mark Gurney 		if (USB_DEBUG_VAR)
1153b43d600cSJohn-Mark Gurney 			usbd_copy_out(pc, 0, buf, MIN(actlen, sizeof buf));
1154b43d600cSJohn-Mark Gurney 		DPRINTF("Received %d bytes: %*D\n", actlen,
1155b43d600cSJohn-Mark Gurney 		    (int)MIN(actlen, sizeof buf), buf, "");
115602ac6454SAndrew Thompson 
1157b43d600cSJohn-Mark Gurney 		off = 0;
1158b43d600cSJohn-Mark Gurney 		while (actlen - off >= UCDC_NOTIFICATION_LENGTH) {
1159b43d600cSJohn-Mark Gurney 			usbd_copy_out(pc, off, &ucn, UCDC_NOTIFICATION_LENGTH);
116002ac6454SAndrew Thompson 
1161b43d600cSJohn-Mark Gurney 			do {
1162b43d600cSJohn-Mark Gurney 				if (ucn.bmRequestType != 0xa1)
1163b43d600cSJohn-Mark Gurney 					break;
1164b43d600cSJohn-Mark Gurney 
1165b43d600cSJohn-Mark Gurney 				switch (ucn.bNotification) {
1166b43d600cSJohn-Mark Gurney 				case UCDC_N_NETWORK_CONNECTION:
1167b43d600cSJohn-Mark Gurney 					DPRINTF("changing link state: %d\n",
1168b43d600cSJohn-Mark Gurney 					    UGETW(ucn.wValue));
1169b43d600cSJohn-Mark Gurney 					if_link_state_change(ifp,
1170b43d600cSJohn-Mark Gurney 					    UGETW(ucn.wValue) ? LINK_STATE_UP :
1171b43d600cSJohn-Mark Gurney 					    LINK_STATE_DOWN);
1172b43d600cSJohn-Mark Gurney 					break;
1173b43d600cSJohn-Mark Gurney 
1174b43d600cSJohn-Mark Gurney 				case UCDC_N_CONNECTION_SPEED_CHANGE:
1175b43d600cSJohn-Mark Gurney 					if (UGETW(ucn.wLength) != 8)
1176b43d600cSJohn-Mark Gurney 						break;
1177b43d600cSJohn-Mark Gurney 
1178b43d600cSJohn-Mark Gurney 					usbd_copy_out(pc, off +
1179b43d600cSJohn-Mark Gurney 					    UCDC_NOTIFICATION_LENGTH,
1180b43d600cSJohn-Mark Gurney 					    &ucn.data, UGETW(ucn.wLength));
1181b43d600cSJohn-Mark Gurney 					downrate = UGETDW(ucn.data);
1182b43d600cSJohn-Mark Gurney 					uprate = UGETDW(ucn.data);
1183b43d600cSJohn-Mark Gurney 					if (downrate != uprate)
1184b43d600cSJohn-Mark Gurney 						break;
1185b43d600cSJohn-Mark Gurney 
1186b43d600cSJohn-Mark Gurney 					/* set rate */
1187b43d600cSJohn-Mark Gurney 					DPRINTF("changing baudrate: %u\n",
1188b43d600cSJohn-Mark Gurney 					    downrate);
1189b43d600cSJohn-Mark Gurney 					if_setbaudrate(ifp, downrate);
1190b43d600cSJohn-Mark Gurney 					break;
1191b43d600cSJohn-Mark Gurney 
1192b43d600cSJohn-Mark Gurney 				default:
1193b43d600cSJohn-Mark Gurney 					break;
1194b43d600cSJohn-Mark Gurney 				}
1195b43d600cSJohn-Mark Gurney 			} while (0);
1196b43d600cSJohn-Mark Gurney 
1197b43d600cSJohn-Mark Gurney 			off += UCDC_NOTIFICATION_LENGTH + UGETW(ucn.wLength);
1198b43d600cSJohn-Mark Gurney 		}
119902ac6454SAndrew Thompson 
120002ac6454SAndrew Thompson 		/* FALLTHROUGH */
120102ac6454SAndrew Thompson 	case USB_ST_SETUP:
120202ac6454SAndrew Thompson tr_setup:
1203ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1204a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
120502ac6454SAndrew Thompson 		break;
120602ac6454SAndrew Thompson 
120702ac6454SAndrew Thompson 	default:			/* Error */
1208ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
120902ac6454SAndrew Thompson 			/* start clear stall */
12107d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
1211ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
121202ac6454SAndrew Thompson 			goto tr_setup;
121302ac6454SAndrew Thompson 		}
121402ac6454SAndrew Thompson 		break;
121502ac6454SAndrew Thompson 	}
121602ac6454SAndrew Thompson }
121702ac6454SAndrew Thompson 
121802ac6454SAndrew Thompson static void
1219ed6d949aSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
122002ac6454SAndrew Thompson {
1221b0b74fb3SRuslan Bukin 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
1222b0b74fb3SRuslan Bukin 	struct usb_cdc_notification req;
1223b0b74fb3SRuslan Bukin 	struct usb_page_cache *pc;
1224b0b74fb3SRuslan Bukin 	uint32_t speed;
1225ed6d949aSAndrew Thompson 	int actlen;
1226ed6d949aSAndrew Thompson 
1227ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1228ed6d949aSAndrew Thompson 
122902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
123002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
123102ac6454SAndrew Thompson 
1232ed6d949aSAndrew Thompson 		DPRINTF("Transferred %d bytes\n", actlen);
123302ac6454SAndrew Thompson 
12347d502f32SRuslan Bukin 		switch (sc->sc_notify_state) {
1235d69eefebSRuslan Bukin 		case CDCE_NOTIFY_NETWORK_CONNECTION:
12367d502f32SRuslan Bukin 			sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
12377d502f32SRuslan Bukin 			break;
1238d69eefebSRuslan Bukin 		case CDCE_NOTIFY_SPEED_CHANGE:
12397d502f32SRuslan Bukin 			sc->sc_notify_state = CDCE_NOTIFY_DONE;
12407d502f32SRuslan Bukin 			break;
12417d502f32SRuslan Bukin 		default:
12427d502f32SRuslan Bukin 			break;
12437d502f32SRuslan Bukin 		}
12447d502f32SRuslan Bukin 
124502ac6454SAndrew Thompson 		/* FALLTHROUGH */
124602ac6454SAndrew Thompson 	case USB_ST_SETUP:
124702ac6454SAndrew Thompson tr_setup:
1248b0b74fb3SRuslan Bukin 		/*
1249b0b74fb3SRuslan Bukin 		 * Inform host about connection. Required according to USB CDC
1250b0b74fb3SRuslan Bukin 		 * specification and communicating to Mac OS X USB host stack.
1251b0b74fb3SRuslan Bukin 		 * Some of the values seems ignored by Mac OS X though.
1252b0b74fb3SRuslan Bukin 		 */
1253b0b74fb3SRuslan Bukin 		if (sc->sc_notify_state == CDCE_NOTIFY_NETWORK_CONNECTION) {
1254b0b74fb3SRuslan Bukin 			req.bmRequestType = UCDC_NOTIFICATION;
1255b0b74fb3SRuslan Bukin 			req.bNotification = UCDC_N_NETWORK_CONNECTION;
1256b0b74fb3SRuslan Bukin 			req.wIndex[0] = sc->sc_ifaces_index[1];
1257b0b74fb3SRuslan Bukin 			req.wIndex[1] = 0;
1258b0b74fb3SRuslan Bukin 			USETW(req.wValue, 1); /* Connected */
1259b0b74fb3SRuslan Bukin 			USETW(req.wLength, 0);
1260b0b74fb3SRuslan Bukin 
1261b0b74fb3SRuslan Bukin 			pc = usbd_xfer_get_frame(xfer, 0);
1262b0b74fb3SRuslan Bukin 			usbd_copy_in(pc, 0, &req, sizeof(req));
1263b0b74fb3SRuslan Bukin 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1264b0b74fb3SRuslan Bukin 			usbd_xfer_set_frames(xfer, 1);
1265a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
1266b0b74fb3SRuslan Bukin 
1267b0b74fb3SRuslan Bukin 		} else if (sc->sc_notify_state == CDCE_NOTIFY_SPEED_CHANGE) {
1268b0b74fb3SRuslan Bukin 			req.bmRequestType = UCDC_NOTIFICATION;
1269b0b74fb3SRuslan Bukin 			req.bNotification = UCDC_N_CONNECTION_SPEED_CHANGE;
1270b0b74fb3SRuslan Bukin 			req.wIndex[0] = sc->sc_ifaces_index[1];
1271b0b74fb3SRuslan Bukin 			req.wIndex[1] = 0;
1272b0b74fb3SRuslan Bukin 			USETW(req.wValue, 0);
1273b0b74fb3SRuslan Bukin 			USETW(req.wLength, 8);
1274b0b74fb3SRuslan Bukin 
1275b0b74fb3SRuslan Bukin 			/* Peak theoretical bulk trasfer rate in bits/s */
12767d502f32SRuslan Bukin 			if (usbd_get_speed(sc->sc_ue.ue_udev) != USB_SPEED_FULL)
1277b0b74fb3SRuslan Bukin 				speed = (13 * 512 * 8 * 1000 * 8);
1278b0b74fb3SRuslan Bukin 			else
1279b0b74fb3SRuslan Bukin 				speed = (19 * 64 * 1 * 1000 * 8);
1280b0b74fb3SRuslan Bukin 
1281b0b74fb3SRuslan Bukin 			USETDW(req.data + 0, speed); /* Upstream bit rate */
1282b0b74fb3SRuslan Bukin 			USETDW(req.data + 4, speed); /* Downstream bit rate */
1283b0b74fb3SRuslan Bukin 
1284b0b74fb3SRuslan Bukin 			pc = usbd_xfer_get_frame(xfer, 0);
1285b0b74fb3SRuslan Bukin 			usbd_copy_in(pc, 0, &req, sizeof(req));
1286b0b74fb3SRuslan Bukin 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1287b0b74fb3SRuslan Bukin 			usbd_xfer_set_frames(xfer, 1);
1288b0b74fb3SRuslan Bukin 			usbd_transfer_submit(xfer);
1289b0b74fb3SRuslan Bukin 		}
129002ac6454SAndrew Thompson 		break;
129102ac6454SAndrew Thompson 
129202ac6454SAndrew Thompson 	default:			/* Error */
1293ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
12947d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
129502ac6454SAndrew Thompson 				/* start clear stall */
1296ed6d949aSAndrew Thompson 				usbd_xfer_set_stall(xfer);
129702ac6454SAndrew Thompson 			}
1298d69eefebSRuslan Bukin 			goto tr_setup;
12997d502f32SRuslan Bukin 		}
130002ac6454SAndrew Thompson 		break;
130102ac6454SAndrew Thompson 	}
130202ac6454SAndrew Thompson }
130302ac6454SAndrew Thompson 
130402ac6454SAndrew Thompson static int
130502ac6454SAndrew Thompson cdce_handle_request(device_t dev,
13067d502f32SRuslan Bukin     const void *preq, void **pptr, uint16_t *plen,
130729bd7d7eSAndrew Thompson     uint16_t offset, uint8_t *pstate)
130802ac6454SAndrew Thompson {
13097d502f32SRuslan Bukin 	struct cdce_softc *sc = device_get_softc(dev);
13107d502f32SRuslan Bukin 	const struct usb_device_request *req = preq;
13117d502f32SRuslan Bukin 	uint8_t is_complete = *pstate;
13127d502f32SRuslan Bukin 
13137d502f32SRuslan Bukin 	/*
13147d502f32SRuslan Bukin 	 * When Mac OS X resumes after suspending it expects
13157d502f32SRuslan Bukin 	 * to be notified again after this request.
13167d502f32SRuslan Bukin 	 */
13177d502f32SRuslan Bukin 	if (req->bmRequestType == UT_WRITE_CLASS_INTERFACE && \
13187d502f32SRuslan Bukin 	    req->bRequest == UCDC_NCM_SET_ETHERNET_PACKET_FILTER) {
13197d502f32SRuslan Bukin 		if (is_complete == 1) {
13207d502f32SRuslan Bukin 			mtx_lock(&sc->sc_mtx);
13217d502f32SRuslan Bukin 			sc->sc_notify_state = CDCE_NOTIFY_SPEED_CHANGE;
13227d502f32SRuslan Bukin 			usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]);
13237d502f32SRuslan Bukin 			mtx_unlock(&sc->sc_mtx);
13247d502f32SRuslan Bukin 		}
13257d502f32SRuslan Bukin 
13267d502f32SRuslan Bukin 		return (0);
13277d502f32SRuslan Bukin 	}
13287d502f32SRuslan Bukin 
132902ac6454SAndrew Thompson 	return (ENXIO);			/* use builtin handler */
133002ac6454SAndrew Thompson }
13314076dd23SAndrew Thompson 
13324076dd23SAndrew Thompson #if CDCE_HAVE_NCM
13337e05daaeSHans Petter Selasky static void
13347e05daaeSHans Petter Selasky cdce_ncm_tx_zero(struct usb_page_cache *pc,
13357e05daaeSHans Petter Selasky     uint32_t start, uint32_t end)
13367e05daaeSHans Petter Selasky {
13377e05daaeSHans Petter Selasky 	if (start >= CDCE_NCM_TX_MAXLEN)
13387e05daaeSHans Petter Selasky 		return;
13397e05daaeSHans Petter Selasky 	if (end > CDCE_NCM_TX_MAXLEN)
13407e05daaeSHans Petter Selasky 		end = CDCE_NCM_TX_MAXLEN;
13417e05daaeSHans Petter Selasky 
13427e05daaeSHans Petter Selasky 	usbd_frame_zero(pc, start, end - start);
13437e05daaeSHans Petter Selasky }
13447e05daaeSHans Petter Selasky 
13454076dd23SAndrew Thompson static uint8_t
13464076dd23SAndrew Thompson cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index)
13474076dd23SAndrew Thompson {
13484076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
13494076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
13504076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index);
13514076dd23SAndrew Thompson 	struct mbuf *m;
13524076dd23SAndrew Thompson 	uint32_t rem;
13534076dd23SAndrew Thompson 	uint32_t offset;
13544076dd23SAndrew Thompson 	uint32_t last_offset;
13557e05daaeSHans Petter Selasky 	uint16_t n;
13567e05daaeSHans Petter Selasky 	uint8_t retval;
13574076dd23SAndrew Thompson 
13584076dd23SAndrew Thompson 	usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index);
13594076dd23SAndrew Thompson 
13604076dd23SAndrew Thompson 	offset = sizeof(sc->sc_ncm.hdr) +
13614076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp);
13624076dd23SAndrew Thompson 
13634076dd23SAndrew Thompson 	/* Store last valid offset before alignment */
13644076dd23SAndrew Thompson 	last_offset = offset;
13654076dd23SAndrew Thompson 
13667e05daaeSHans Petter Selasky 	/* Align offset */
13677e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
13687e05daaeSHans Petter Selasky 	    offset, sc->sc_ncm.tx_modulus);
13694076dd23SAndrew Thompson 
13707e05daaeSHans Petter Selasky 	/* Zero pad */
13717e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
13727e05daaeSHans Petter Selasky 
13737e05daaeSHans Petter Selasky 	/* buffer full */
13747e05daaeSHans Petter Selasky 	retval = 2;
13757e05daaeSHans Petter Selasky 
13767e05daaeSHans Petter Selasky 	for (n = 0; n != sc->sc_ncm.tx_nframe; n++) {
13774076dd23SAndrew Thompson 		/* check if end of transmit buffer is reached */
13784076dd23SAndrew Thompson 
13794076dd23SAndrew Thompson 		if (offset >= sc->sc_ncm.tx_max)
13804076dd23SAndrew Thompson 			break;
13814076dd23SAndrew Thompson 
13824076dd23SAndrew Thompson 		/* compute maximum buffer size */
13834076dd23SAndrew Thompson 
13844076dd23SAndrew Thompson 		rem = sc->sc_ncm.tx_max - offset;
13854076dd23SAndrew Thompson 
13864076dd23SAndrew Thompson 		IFQ_DRV_DEQUEUE(&(ifp->if_snd), m);
13874076dd23SAndrew Thompson 
13887e05daaeSHans Petter Selasky 		if (m == NULL) {
13897e05daaeSHans Petter Selasky 			/* buffer not full */
13907e05daaeSHans Petter Selasky 			retval = 1;
13914076dd23SAndrew Thompson 			break;
13927e05daaeSHans Petter Selasky 		}
13934076dd23SAndrew Thompson 
13946d917491SHans Petter Selasky 		if (m->m_pkthdr.len > (int)rem) {
13954076dd23SAndrew Thompson 			if (n == 0) {
13964076dd23SAndrew Thompson 				/* The frame won't fit in our buffer */
13974076dd23SAndrew Thompson 				DPRINTFN(1, "Frame too big to be transmitted!\n");
13984076dd23SAndrew Thompson 				m_freem(m);
1399ecc70d3fSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
14004076dd23SAndrew Thompson 				n--;
14014076dd23SAndrew Thompson 				continue;
14024076dd23SAndrew Thompson 			}
14034076dd23SAndrew Thompson 			/* Wait till next buffer becomes ready */
14044076dd23SAndrew Thompson 			IFQ_DRV_PREPEND(&(ifp->if_snd), m);
14054076dd23SAndrew Thompson 			break;
14064076dd23SAndrew Thompson 		}
14074076dd23SAndrew Thompson 		usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len);
14084076dd23SAndrew Thompson 
14094076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len);
14104076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, offset);
14114076dd23SAndrew Thompson 
14124076dd23SAndrew Thompson 		/* Update offset */
14134076dd23SAndrew Thompson 		offset += m->m_pkthdr.len;
14144076dd23SAndrew Thompson 
14154076dd23SAndrew Thompson 		/* Store last valid offset before alignment */
14164076dd23SAndrew Thompson 		last_offset = offset;
14174076dd23SAndrew Thompson 
14187e05daaeSHans Petter Selasky 		/* Align offset */
14197e05daaeSHans Petter Selasky 		offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder,
14207e05daaeSHans Petter Selasky 		    offset, sc->sc_ncm.tx_modulus);
14217e05daaeSHans Petter Selasky 
14227e05daaeSHans Petter Selasky 		/* Zero pad */
14237e05daaeSHans Petter Selasky 		cdce_ncm_tx_zero(pc, last_offset, offset);
14244076dd23SAndrew Thompson 
14254076dd23SAndrew Thompson 		/*
14264076dd23SAndrew Thompson 		 * If there's a BPF listener, bounce a copy
14274076dd23SAndrew Thompson 		 * of this frame to him:
14284076dd23SAndrew Thompson 		 */
14294076dd23SAndrew Thompson 		BPF_MTAP(ifp, m);
14304076dd23SAndrew Thompson 
14314076dd23SAndrew Thompson 		/* Free mbuf */
14324076dd23SAndrew Thompson 
14334076dd23SAndrew Thompson 		m_freem(m);
14344076dd23SAndrew Thompson 
14354076dd23SAndrew Thompson 		/* Pre-increment interface counter */
14364076dd23SAndrew Thompson 
1437ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
14384076dd23SAndrew Thompson 	}
14394076dd23SAndrew Thompson 
14404076dd23SAndrew Thompson 	if (n == 0)
14417e05daaeSHans Petter Selasky 		return (0);
14424076dd23SAndrew Thompson 
14434076dd23SAndrew Thompson 	rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4);
14444076dd23SAndrew Thompson 
14454076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wLength, rem);
14464076dd23SAndrew Thompson 
14474076dd23SAndrew Thompson 	/* zero the rest of the data pointer entries */
14484076dd23SAndrew Thompson 	for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) {
14494076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameLength, 0);
14504076dd23SAndrew Thompson 		USETW(sc->sc_ncm.dp[n].wFrameIndex, 0);
14514076dd23SAndrew Thompson 	}
14524076dd23SAndrew Thompson 
14537e05daaeSHans Petter Selasky 	offset = last_offset;
14547e05daaeSHans Petter Selasky 
14557e05daaeSHans Petter Selasky 	/* Align offset */
14567e05daaeSHans Petter Selasky 	offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN);
14577e05daaeSHans Petter Selasky 
14587e05daaeSHans Petter Selasky 	/* Optimise, save bandwidth and force short termination */
14597e05daaeSHans Petter Selasky 	if (offset >= sc->sc_ncm.tx_max)
14607e05daaeSHans Petter Selasky 		offset = sc->sc_ncm.tx_max;
14617e05daaeSHans Petter Selasky 	else
14627e05daaeSHans Petter Selasky 		offset ++;
14637e05daaeSHans Petter Selasky 
14647e05daaeSHans Petter Selasky 	/* Zero pad */
14657e05daaeSHans Petter Selasky 	cdce_ncm_tx_zero(pc, last_offset, offset);
14667e05daaeSHans Petter Selasky 
14674076dd23SAndrew Thompson 	/* set frame length */
14687e05daaeSHans Petter Selasky 	usbd_xfer_set_frame_len(xfer, index, offset);
14694076dd23SAndrew Thompson 
14704076dd23SAndrew Thompson 	/* Fill out 16-bit header */
14714076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[0] = 'N';
14724076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[1] = 'C';
14734076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[2] = 'M';
14744076dd23SAndrew Thompson 	sc->sc_ncm.hdr.dwSignature[3] = 'H';
14754076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr));
14767e05daaeSHans Petter Selasky 	USETW(sc->sc_ncm.hdr.wBlockLength, offset);
14774076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq);
14784076dd23SAndrew Thompson 	USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr));
14794076dd23SAndrew Thompson 
14804076dd23SAndrew Thompson 	sc->sc_ncm.tx_seq++;
14814076dd23SAndrew Thompson 
14824076dd23SAndrew Thompson 	/* Fill out 16-bit frame table header */
14834076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[0] = 'N';
14844076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[1] = 'C';
14854076dd23SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[2] = 'M';
148685e3d588SAndrew Thompson 	sc->sc_ncm.dpt.dwSignature[3] = '0';
14874076dd23SAndrew Thompson 	USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0);		/* reserved */
14884076dd23SAndrew Thompson 
14894076dd23SAndrew Thompson 	usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr));
14904076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt),
14914076dd23SAndrew Thompson 	    sizeof(sc->sc_ncm.dpt));
14924076dd23SAndrew Thompson 	usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt),
14934076dd23SAndrew Thompson 	    &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp));
14947e05daaeSHans Petter Selasky 	return (retval);
14954076dd23SAndrew Thompson }
14964076dd23SAndrew Thompson 
14974076dd23SAndrew Thompson static void
14984076dd23SAndrew Thompson cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
14994076dd23SAndrew Thompson {
15004076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
15014076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
15024076dd23SAndrew Thompson 	uint16_t x;
15037e05daaeSHans Petter Selasky 	uint8_t temp;
15044076dd23SAndrew Thompson 	int actlen;
15054076dd23SAndrew Thompson 	int aframes;
15064076dd23SAndrew Thompson 
15074076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
15084076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
15094076dd23SAndrew Thompson 
15104076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
15114076dd23SAndrew Thompson 
15124076dd23SAndrew Thompson 		DPRINTFN(10, "transfer complete: "
15134076dd23SAndrew Thompson 		    "%u bytes in %u frames\n", actlen, aframes);
15144076dd23SAndrew Thompson 
15154076dd23SAndrew Thompson 	case USB_ST_SETUP:
15164076dd23SAndrew Thompson 		for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) {
15177e05daaeSHans Petter Selasky 			temp = cdce_ncm_fill_tx_frames(xfer, x);
15187e05daaeSHans Petter Selasky 			if (temp == 0)
15194076dd23SAndrew Thompson 				break;
15207e05daaeSHans Petter Selasky 			if (temp == 1) {
15217e05daaeSHans Petter Selasky 				x++;
15227e05daaeSHans Petter Selasky 				break;
15237e05daaeSHans Petter Selasky 			}
15244076dd23SAndrew Thompson 		}
15254076dd23SAndrew Thompson 
15264076dd23SAndrew Thompson 		if (x != 0) {
15277e05daaeSHans Petter Selasky #ifdef USB_DEBUG
15287e05daaeSHans Petter Selasky 			usbd_xfer_set_interval(xfer, cdce_tx_interval);
15297e05daaeSHans Petter Selasky #endif
15304076dd23SAndrew Thompson 			usbd_xfer_set_frames(xfer, x);
15314076dd23SAndrew Thompson 			usbd_transfer_submit(xfer);
15324076dd23SAndrew Thompson 		}
15334076dd23SAndrew Thompson 		break;
15344076dd23SAndrew Thompson 
15354076dd23SAndrew Thompson 	default:			/* Error */
15364076dd23SAndrew Thompson 		DPRINTFN(10, "Transfer error: %s\n",
15374076dd23SAndrew Thompson 		    usbd_errstr(error));
15384076dd23SAndrew Thompson 
15394076dd23SAndrew Thompson 		/* update error counter */
1540ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
15414076dd23SAndrew Thompson 
15424076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
15437d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
15444076dd23SAndrew Thompson 				/* try to clear stall first */
15454076dd23SAndrew Thompson 				usbd_xfer_set_stall(xfer);
15464076dd23SAndrew Thompson 				usbd_xfer_set_frames(xfer, 0);
15474076dd23SAndrew Thompson 				usbd_transfer_submit(xfer);
15484076dd23SAndrew Thompson 			}
15497d502f32SRuslan Bukin 		}
15504076dd23SAndrew Thompson 		break;
15514076dd23SAndrew Thompson 	}
15524076dd23SAndrew Thompson }
15534076dd23SAndrew Thompson 
15544076dd23SAndrew Thompson static void
15554076dd23SAndrew Thompson cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
15564076dd23SAndrew Thompson {
15574076dd23SAndrew Thompson 	struct cdce_softc *sc = usbd_xfer_softc(xfer);
15584076dd23SAndrew Thompson 	struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0);
15594076dd23SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
15604076dd23SAndrew Thompson 	struct mbuf *m;
15614076dd23SAndrew Thompson 	int sumdata;
15624076dd23SAndrew Thompson 	int sumlen;
15634076dd23SAndrew Thompson 	int actlen;
15644076dd23SAndrew Thompson 	int aframes;
15654076dd23SAndrew Thompson 	int temp;
15664076dd23SAndrew Thompson 	int nframes;
15674076dd23SAndrew Thompson 	int x;
15684076dd23SAndrew Thompson 	int offset;
15694076dd23SAndrew Thompson 
15704076dd23SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
15714076dd23SAndrew Thompson 	case USB_ST_TRANSFERRED:
15724076dd23SAndrew Thompson 
15734076dd23SAndrew Thompson 		usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL);
15744076dd23SAndrew Thompson 
15754076dd23SAndrew Thompson 		DPRINTFN(1, "received %u bytes in %u frames\n",
15764076dd23SAndrew Thompson 		    actlen, aframes);
15774076dd23SAndrew Thompson 
15786d917491SHans Petter Selasky 		if (actlen < (int)(sizeof(sc->sc_ncm.hdr) +
15794076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt))) {
15804076dd23SAndrew Thompson 			DPRINTFN(1, "frame too short\n");
158185e3d588SAndrew Thompson 			goto tr_setup;
15824076dd23SAndrew Thompson 		}
15834076dd23SAndrew Thompson 		usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr),
15844076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.hdr));
15854076dd23SAndrew Thompson 
15864076dd23SAndrew Thompson 		if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') ||
15874076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[1] != 'C') ||
15884076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[2] != 'M') ||
15894076dd23SAndrew Thompson 		    (sc->sc_ncm.hdr.dwSignature[3] != 'H')) {
159085e3d588SAndrew Thompson 			DPRINTFN(1, "invalid HDR signature: "
159185e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
159285e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[0],
159385e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[1],
159485e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[2],
159585e3d588SAndrew Thompson 			    sc->sc_ncm.hdr.dwSignature[3]);
15964076dd23SAndrew Thompson 			goto tr_stall;
15974076dd23SAndrew Thompson 		}
15984076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wBlockLength);
15994076dd23SAndrew Thompson 		if (temp > sumlen) {
16004076dd23SAndrew Thompson 			DPRINTFN(1, "unsupported block length %u/%u\n",
16014076dd23SAndrew Thompson 			    temp, sumlen);
16024076dd23SAndrew Thompson 			goto tr_stall;
16034076dd23SAndrew Thompson 		}
16044076dd23SAndrew Thompson 		temp = UGETW(sc->sc_ncm.hdr.wDptIndex);
16056d917491SHans Petter Selasky 		if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) {
160685e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp);
16074076dd23SAndrew Thompson 			goto tr_stall;
16084076dd23SAndrew Thompson 		}
16094076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt),
16104076dd23SAndrew Thompson 		    sizeof(sc->sc_ncm.dpt));
16114076dd23SAndrew Thompson 
16124076dd23SAndrew Thompson 		if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') ||
16134076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[1] != 'C') ||
16144076dd23SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[2] != 'M') ||
161585e3d588SAndrew Thompson 		    (sc->sc_ncm.dpt.dwSignature[3] != '0')) {
161685e3d588SAndrew Thompson 			DPRINTFN(1, "invalid DPT signature"
161785e3d588SAndrew Thompson 			    "0x%02x:0x%02x:0x%02x:0x%02x\n",
161885e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[0],
161985e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[1],
162085e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[2],
162185e3d588SAndrew Thompson 			    sc->sc_ncm.dpt.dwSignature[3]);
16224076dd23SAndrew Thompson 			goto tr_stall;
16234076dd23SAndrew Thompson 		}
16244076dd23SAndrew Thompson 		nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4;
16254076dd23SAndrew Thompson 
16264076dd23SAndrew Thompson 		/* Subtract size of header and last zero padded entry */
16274076dd23SAndrew Thompson 		if (nframes >= (2 + 1))
16284076dd23SAndrew Thompson 			nframes -= (2 + 1);
16294076dd23SAndrew Thompson 		else
16304076dd23SAndrew Thompson 			nframes = 0;
16314076dd23SAndrew Thompson 
16324076dd23SAndrew Thompson 		DPRINTFN(1, "nframes = %u\n", nframes);
16334076dd23SAndrew Thompson 
16344076dd23SAndrew Thompson 		temp += sizeof(sc->sc_ncm.dpt);
16354076dd23SAndrew Thompson 
16364076dd23SAndrew Thompson 		if ((temp + (4 * nframes)) > actlen)
16374076dd23SAndrew Thompson 			goto tr_stall;
16384076dd23SAndrew Thompson 
16394076dd23SAndrew Thompson 		if (nframes > CDCE_NCM_SUBFRAMES_MAX) {
16404076dd23SAndrew Thompson 			DPRINTFN(1, "Truncating number of frames from %u to %u\n",
16414076dd23SAndrew Thompson 			    nframes, CDCE_NCM_SUBFRAMES_MAX);
16424076dd23SAndrew Thompson 			nframes = CDCE_NCM_SUBFRAMES_MAX;
16434076dd23SAndrew Thompson 		}
16444076dd23SAndrew Thompson 		usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes));
16454076dd23SAndrew Thompson 
16464076dd23SAndrew Thompson 		sumdata = 0;
16474076dd23SAndrew Thompson 
16484076dd23SAndrew Thompson 		for (x = 0; x != nframes; x++) {
16494076dd23SAndrew Thompson 			offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex);
16504076dd23SAndrew Thompson 			temp = UGETW(sc->sc_ncm.dp[x].wFrameLength);
16514076dd23SAndrew Thompson 
165229051223SAndrew Thompson 			if ((offset == 0) ||
16536d917491SHans Petter Selasky 			    (temp < (int)sizeof(struct ether_header)) ||
165429051223SAndrew Thompson 			    (temp > (MCLBYTES - ETHER_ALIGN))) {
165529051223SAndrew Thompson 				DPRINTFN(1, "NULL frame detected at %d\n", x);
16564076dd23SAndrew Thompson 				m = NULL;
165729051223SAndrew Thompson 				/* silently ignore this frame */
16584076dd23SAndrew Thompson 				continue;
165929051223SAndrew Thompson 			} else if ((offset + temp) > actlen) {
166029051223SAndrew Thompson 				DPRINTFN(1, "invalid frame "
166129051223SAndrew Thompson 				    "detected at %d\n", x);
166229051223SAndrew Thompson 				m = NULL;
166329051223SAndrew Thompson 				/* silently ignore this frame */
166429051223SAndrew Thompson 				continue;
16656d917491SHans Petter Selasky 			} else if (temp > (int)(MHLEN - ETHER_ALIGN)) {
1666c6499eccSGleb Smirnoff 				m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
16674076dd23SAndrew Thompson 			} else {
1668c6499eccSGleb Smirnoff 				m = m_gethdr(M_NOWAIT, MT_DATA);
16694076dd23SAndrew Thompson 			}
16704076dd23SAndrew Thompson 
16714076dd23SAndrew Thompson 			DPRINTFN(16, "frame %u, offset = %u, length = %u \n",
16724076dd23SAndrew Thompson 			    x, offset, temp);
16734076dd23SAndrew Thompson 
16744076dd23SAndrew Thompson 			/* check if we have a buffer */
16754076dd23SAndrew Thompson 			if (m) {
1676a51f9801SHans Petter Selasky 				m->m_len = m->m_pkthdr.len = temp + ETHER_ALIGN;
16774076dd23SAndrew Thompson 				m_adj(m, ETHER_ALIGN);
16784076dd23SAndrew Thompson 
16794076dd23SAndrew Thompson 				usbd_copy_out(pc, offset, m->m_data, temp);
16804076dd23SAndrew Thompson 
16814076dd23SAndrew Thompson 				/* enqueue */
16824076dd23SAndrew Thompson 				uether_rxmbuf(&sc->sc_ue, m, temp);
16834076dd23SAndrew Thompson 
16844076dd23SAndrew Thompson 				sumdata += temp;
16854076dd23SAndrew Thompson 			} else {
1686ecc70d3fSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
16874076dd23SAndrew Thompson 			}
16884076dd23SAndrew Thompson 		}
16894076dd23SAndrew Thompson 
16904076dd23SAndrew Thompson 		DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen);
16914076dd23SAndrew Thompson 
16924076dd23SAndrew Thompson 	case USB_ST_SETUP:
169385e3d588SAndrew Thompson tr_setup:
16944076dd23SAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max);
16954076dd23SAndrew Thompson 		usbd_xfer_set_frames(xfer, 1);
16964076dd23SAndrew Thompson 		usbd_transfer_submit(xfer);
16974076dd23SAndrew Thompson 		uether_rxflush(&sc->sc_ue);	/* must be last */
16984076dd23SAndrew Thompson 		break;
16994076dd23SAndrew Thompson 
17004076dd23SAndrew Thompson 	default:			/* Error */
17014076dd23SAndrew Thompson 		DPRINTFN(1, "error = %s\n",
17024076dd23SAndrew Thompson 		    usbd_errstr(error));
17034076dd23SAndrew Thompson 
17044076dd23SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
17054076dd23SAndrew Thompson tr_stall:
17067d502f32SRuslan Bukin 			if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) {
17074076dd23SAndrew Thompson 				/* try to clear stall first */
17084076dd23SAndrew Thompson 				usbd_xfer_set_stall(xfer);
17094076dd23SAndrew Thompson 				usbd_xfer_set_frames(xfer, 0);
17104076dd23SAndrew Thompson 				usbd_transfer_submit(xfer);
17114076dd23SAndrew Thompson 			}
17127d502f32SRuslan Bukin 		}
17134076dd23SAndrew Thompson 		break;
17144076dd23SAndrew Thompson 	}
17154076dd23SAndrew Thompson }
17164076dd23SAndrew Thompson #endif
1717