102ac6454SAndrew Thompson /* $NetBSD: if_cdce.c,v 1.4 2004/10/24 12:50:54 augustss Exp $ */ 202ac6454SAndrew Thompson 302ac6454SAndrew Thompson /*- 402ac6454SAndrew Thompson * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com> 502ac6454SAndrew Thompson * Copyright (c) 2003-2005 Craig Boston 602ac6454SAndrew Thompson * Copyright (c) 2004 Daniel Hartmeier 702ac6454SAndrew Thompson * Copyright (c) 2009 Hans Petter Selasky 802ac6454SAndrew Thompson * All rights reserved. 902ac6454SAndrew Thompson * 1002ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 1102ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 1202ac6454SAndrew Thompson * are met: 1302ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1402ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1502ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1602ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1702ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1802ac6454SAndrew Thompson * 3. All advertising materials mentioning features or use of this software 1902ac6454SAndrew Thompson * must display the following acknowledgement: 2002ac6454SAndrew Thompson * This product includes software developed by Bill Paul. 2102ac6454SAndrew Thompson * 4. Neither the name of the author nor the names of any co-contributors 2202ac6454SAndrew Thompson * may be used to endorse or promote products derived from this software 2302ac6454SAndrew Thompson * without specific prior written permission. 2402ac6454SAndrew Thompson * 2502ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2602ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2702ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2802ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR 2902ac6454SAndrew Thompson * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 3002ac6454SAndrew Thompson * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 3102ac6454SAndrew Thompson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3202ac6454SAndrew Thompson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3302ac6454SAndrew Thompson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3402ac6454SAndrew Thompson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3502ac6454SAndrew Thompson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3602ac6454SAndrew Thompson */ 3702ac6454SAndrew Thompson 3802ac6454SAndrew Thompson /* 3902ac6454SAndrew Thompson * USB Communication Device Class (Ethernet Networking Control Model) 4002ac6454SAndrew Thompson * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf 4102ac6454SAndrew Thompson */ 4202ac6454SAndrew Thompson 434076dd23SAndrew Thompson /* 444076dd23SAndrew Thompson * USB Network Control Model (NCM) 454076dd23SAndrew Thompson * http://www.usb.org/developers/devclass_docs/NCM10.zip 464076dd23SAndrew Thompson */ 474076dd23SAndrew Thompson 4802ac6454SAndrew Thompson #include <sys/cdefs.h> 4902ac6454SAndrew Thompson __FBSDID("$FreeBSD$"); 5002ac6454SAndrew Thompson 51ed6d949aSAndrew Thompson #include <sys/stdint.h> 52ed6d949aSAndrew Thompson #include <sys/stddef.h> 53ed6d949aSAndrew Thompson #include <sys/param.h> 54ed6d949aSAndrew Thompson #include <sys/queue.h> 55ed6d949aSAndrew Thompson #include <sys/types.h> 56ed6d949aSAndrew Thompson #include <sys/systm.h> 57*76039bc8SGleb Smirnoff #include <sys/socket.h> 58ed6d949aSAndrew Thompson #include <sys/kernel.h> 59ed6d949aSAndrew Thompson #include <sys/bus.h> 60ed6d949aSAndrew Thompson #include <sys/module.h> 61ed6d949aSAndrew Thompson #include <sys/lock.h> 62ed6d949aSAndrew Thompson #include <sys/mutex.h> 63ed6d949aSAndrew Thompson #include <sys/condvar.h> 64ed6d949aSAndrew Thompson #include <sys/sysctl.h> 65ed6d949aSAndrew Thompson #include <sys/sx.h> 66ed6d949aSAndrew Thompson #include <sys/unistd.h> 67ed6d949aSAndrew Thompson #include <sys/callout.h> 68ed6d949aSAndrew Thompson #include <sys/malloc.h> 69ed6d949aSAndrew Thompson #include <sys/priv.h> 70ed6d949aSAndrew Thompson 71*76039bc8SGleb Smirnoff #include <net/if.h> 72*76039bc8SGleb Smirnoff #include <net/if_var.h> 73*76039bc8SGleb Smirnoff 7402ac6454SAndrew Thompson #include <dev/usb/usb.h> 75ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 76ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 7702ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h> 78ed6d949aSAndrew Thompson #include "usbdevs.h" 7902ac6454SAndrew Thompson 8002ac6454SAndrew Thompson #define USB_DEBUG_VAR cdce_debug 8102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 82ed6d949aSAndrew Thompson #include <dev/usb/usb_process.h> 83ed6d949aSAndrew Thompson #include "usb_if.h" 8402ac6454SAndrew Thompson 8502ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h> 8602ac6454SAndrew Thompson #include <dev/usb/net/if_cdcereg.h> 8702ac6454SAndrew Thompson 8802ac6454SAndrew Thompson static device_probe_t cdce_probe; 8902ac6454SAndrew Thompson static device_attach_t cdce_attach; 9002ac6454SAndrew Thompson static device_detach_t cdce_detach; 9102ac6454SAndrew Thompson static device_suspend_t cdce_suspend; 9202ac6454SAndrew Thompson static device_resume_t cdce_resume; 9302ac6454SAndrew Thompson static usb_handle_request_t cdce_handle_request; 9402ac6454SAndrew Thompson 95e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_write_callback; 96e0a69b51SAndrew Thompson static usb_callback_t cdce_bulk_read_callback; 97e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_read_callback; 98e0a69b51SAndrew Thompson static usb_callback_t cdce_intr_write_callback; 9902ac6454SAndrew Thompson 1004076dd23SAndrew Thompson #if CDCE_HAVE_NCM 1014076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_write_callback; 1024076dd23SAndrew Thompson static usb_callback_t cdce_ncm_bulk_read_callback; 1034076dd23SAndrew Thompson #endif 1044076dd23SAndrew Thompson 105e0a69b51SAndrew Thompson static uether_fn_t cdce_attach_post; 106e0a69b51SAndrew Thompson static uether_fn_t cdce_init; 107e0a69b51SAndrew Thompson static uether_fn_t cdce_stop; 108e0a69b51SAndrew Thompson static uether_fn_t cdce_start; 109e0a69b51SAndrew Thompson static uether_fn_t cdce_setmulti; 110e0a69b51SAndrew Thompson static uether_fn_t cdce_setpromisc; 11102ac6454SAndrew Thompson 11202ac6454SAndrew Thompson static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t); 11302ac6454SAndrew Thompson 114b850ecc1SAndrew Thompson #ifdef USB_DEBUG 11502ac6454SAndrew Thompson static int cdce_debug = 0; 1167e05daaeSHans Petter Selasky static int cdce_tx_interval = 0; 11702ac6454SAndrew Thompson 1186472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet"); 1199360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0, 12002ac6454SAndrew Thompson "Debug level"); 1217e05daaeSHans Petter Selasky SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RW, &cdce_tx_interval, 0, 1227e05daaeSHans Petter Selasky "NCM transmit interval in ms"); 12302ac6454SAndrew Thompson #endif 12402ac6454SAndrew Thompson 125760bc48eSAndrew Thompson static const struct usb_config cdce_config[CDCE_N_TRANSFER] = { 12602ac6454SAndrew Thompson 1274eae601eSAndrew Thompson [CDCE_BULK_RX] = { 12802ac6454SAndrew Thompson .type = UE_BULK, 12902ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 1304eae601eSAndrew Thompson .direction = UE_DIR_RX, 13102ac6454SAndrew Thompson .if_index = 0, 1324eae601eSAndrew Thompson .frames = CDCE_FRAMES_MAX, 1334eae601eSAndrew Thompson .bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 1344eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,}, 1354eae601eSAndrew Thompson .callback = cdce_bulk_read_callback, 1364eae601eSAndrew Thompson .timeout = 0, /* no timeout */ 137f29a0724SAndrew Thompson .usb_mode = USB_MODE_DUAL, /* both modes */ 13802ac6454SAndrew Thompson }, 13902ac6454SAndrew Thompson 1404eae601eSAndrew Thompson [CDCE_BULK_TX] = { 14102ac6454SAndrew Thompson .type = UE_BULK, 14202ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 1434eae601eSAndrew Thompson .direction = UE_DIR_TX, 14402ac6454SAndrew Thompson .if_index = 0, 1454eae601eSAndrew Thompson .frames = CDCE_FRAMES_MAX, 1464eae601eSAndrew Thompson .bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 1474eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,}, 1484eae601eSAndrew Thompson .callback = cdce_bulk_write_callback, 1494eae601eSAndrew Thompson .timeout = 10000, /* 10 seconds */ 150f29a0724SAndrew Thompson .usb_mode = USB_MODE_DUAL, /* both modes */ 15102ac6454SAndrew Thompson }, 15202ac6454SAndrew Thompson 1534eae601eSAndrew Thompson [CDCE_INTR_RX] = { 15402ac6454SAndrew Thompson .type = UE_INTERRUPT, 15502ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 1564eae601eSAndrew Thompson .direction = UE_DIR_RX, 15702ac6454SAndrew Thompson .if_index = 1, 1584eae601eSAndrew Thompson .bufsize = CDCE_IND_SIZE_MAX, 1594eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,}, 1604eae601eSAndrew Thompson .callback = cdce_intr_read_callback, 1614eae601eSAndrew Thompson .timeout = 0, 1624eae601eSAndrew Thompson .usb_mode = USB_MODE_HOST, 1634eae601eSAndrew Thompson }, 1644eae601eSAndrew Thompson 1654eae601eSAndrew Thompson [CDCE_INTR_TX] = { 1664eae601eSAndrew Thompson .type = UE_INTERRUPT, 1674eae601eSAndrew Thompson .endpoint = UE_ADDR_ANY, 1684eae601eSAndrew Thompson .direction = UE_DIR_TX, 1694eae601eSAndrew Thompson .if_index = 1, 1704eae601eSAndrew Thompson .bufsize = CDCE_IND_SIZE_MAX, 1714eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 1724eae601eSAndrew Thompson .callback = cdce_intr_write_callback, 1734eae601eSAndrew Thompson .timeout = 10000, /* 10 seconds */ 1744eae601eSAndrew Thompson .usb_mode = USB_MODE_DEVICE, 17502ac6454SAndrew Thompson }, 17602ac6454SAndrew Thompson }; 17702ac6454SAndrew Thompson 1784076dd23SAndrew Thompson #if CDCE_HAVE_NCM 1794076dd23SAndrew Thompson static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = { 1804076dd23SAndrew Thompson 1814076dd23SAndrew Thompson [CDCE_BULK_RX] = { 1824076dd23SAndrew Thompson .type = UE_BULK, 1834076dd23SAndrew Thompson .endpoint = UE_ADDR_ANY, 1844076dd23SAndrew Thompson .direction = UE_DIR_RX, 1854076dd23SAndrew Thompson .if_index = 0, 1864076dd23SAndrew Thompson .frames = CDCE_NCM_RX_FRAMES_MAX, 1874076dd23SAndrew Thompson .bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN), 1884076dd23SAndrew Thompson .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,}, 1894076dd23SAndrew Thompson .callback = cdce_ncm_bulk_read_callback, 1904076dd23SAndrew Thompson .timeout = 0, /* no timeout */ 1914076dd23SAndrew Thompson .usb_mode = USB_MODE_DUAL, /* both modes */ 1924076dd23SAndrew Thompson }, 1934076dd23SAndrew Thompson 1944076dd23SAndrew Thompson [CDCE_BULK_TX] = { 1954076dd23SAndrew Thompson .type = UE_BULK, 1964076dd23SAndrew Thompson .endpoint = UE_ADDR_ANY, 1974076dd23SAndrew Thompson .direction = UE_DIR_TX, 1984076dd23SAndrew Thompson .if_index = 0, 1994076dd23SAndrew Thompson .frames = CDCE_NCM_TX_FRAMES_MAX, 2004076dd23SAndrew Thompson .bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN), 2017e05daaeSHans Petter Selasky .flags = {.pipe_bof = 1,}, 2024076dd23SAndrew Thompson .callback = cdce_ncm_bulk_write_callback, 2034076dd23SAndrew Thompson .timeout = 10000, /* 10 seconds */ 2044076dd23SAndrew Thompson .usb_mode = USB_MODE_DUAL, /* both modes */ 2054076dd23SAndrew Thompson }, 2064076dd23SAndrew Thompson 2074076dd23SAndrew Thompson [CDCE_INTR_RX] = { 2084076dd23SAndrew Thompson .type = UE_INTERRUPT, 2094076dd23SAndrew Thompson .endpoint = UE_ADDR_ANY, 2104076dd23SAndrew Thompson .direction = UE_DIR_RX, 2114076dd23SAndrew Thompson .if_index = 1, 2124076dd23SAndrew Thompson .bufsize = CDCE_IND_SIZE_MAX, 2134076dd23SAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,}, 2144076dd23SAndrew Thompson .callback = cdce_intr_read_callback, 2154076dd23SAndrew Thompson .timeout = 0, 2164076dd23SAndrew Thompson .usb_mode = USB_MODE_HOST, 2174076dd23SAndrew Thompson }, 2184076dd23SAndrew Thompson 2194076dd23SAndrew Thompson [CDCE_INTR_TX] = { 2204076dd23SAndrew Thompson .type = UE_INTERRUPT, 2214076dd23SAndrew Thompson .endpoint = UE_ADDR_ANY, 2224076dd23SAndrew Thompson .direction = UE_DIR_TX, 2234076dd23SAndrew Thompson .if_index = 1, 2244076dd23SAndrew Thompson .bufsize = CDCE_IND_SIZE_MAX, 2254076dd23SAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 2264076dd23SAndrew Thompson .callback = cdce_intr_write_callback, 2274076dd23SAndrew Thompson .timeout = 10000, /* 10 seconds */ 2284076dd23SAndrew Thompson .usb_mode = USB_MODE_DEVICE, 2294076dd23SAndrew Thompson }, 2304076dd23SAndrew Thompson }; 2314076dd23SAndrew Thompson #endif 2324076dd23SAndrew Thompson 23302ac6454SAndrew Thompson static device_method_t cdce_methods[] = { 23402ac6454SAndrew Thompson /* USB interface */ 23502ac6454SAndrew Thompson DEVMETHOD(usb_handle_request, cdce_handle_request), 23602ac6454SAndrew Thompson 23702ac6454SAndrew Thompson /* Device interface */ 23802ac6454SAndrew Thompson DEVMETHOD(device_probe, cdce_probe), 23902ac6454SAndrew Thompson DEVMETHOD(device_attach, cdce_attach), 24002ac6454SAndrew Thompson DEVMETHOD(device_detach, cdce_detach), 24102ac6454SAndrew Thompson DEVMETHOD(device_suspend, cdce_suspend), 24202ac6454SAndrew Thompson DEVMETHOD(device_resume, cdce_resume), 24302ac6454SAndrew Thompson 24461bfd867SSofian Brabez DEVMETHOD_END 24502ac6454SAndrew Thompson }; 24602ac6454SAndrew Thompson 24702ac6454SAndrew Thompson static driver_t cdce_driver = { 24802ac6454SAndrew Thompson .name = "cdce", 24902ac6454SAndrew Thompson .methods = cdce_methods, 25002ac6454SAndrew Thompson .size = sizeof(struct cdce_softc), 25102ac6454SAndrew Thompson }; 25202ac6454SAndrew Thompson 25302ac6454SAndrew Thompson static devclass_t cdce_devclass; 25402ac6454SAndrew Thompson 2559aef556dSAndrew Thompson DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0); 25602ac6454SAndrew Thompson MODULE_VERSION(cdce, 1); 25702ac6454SAndrew Thompson MODULE_DEPEND(cdce, uether, 1, 1, 1); 25802ac6454SAndrew Thompson MODULE_DEPEND(cdce, usb, 1, 1, 1); 25902ac6454SAndrew Thompson MODULE_DEPEND(cdce, ether, 1, 1, 1); 26002ac6454SAndrew Thompson 261760bc48eSAndrew Thompson static const struct usb_ether_methods cdce_ue_methods = { 26202ac6454SAndrew Thompson .ue_attach_post = cdce_attach_post, 26302ac6454SAndrew Thompson .ue_start = cdce_start, 26402ac6454SAndrew Thompson .ue_init = cdce_init, 26502ac6454SAndrew Thompson .ue_stop = cdce_stop, 26602ac6454SAndrew Thompson .ue_setmulti = cdce_setmulti, 26702ac6454SAndrew Thompson .ue_setpromisc = cdce_setpromisc, 26802ac6454SAndrew Thompson }; 26902ac6454SAndrew Thompson 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_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)}, 27802ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)}, 27902ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)}, 28002ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 28102ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 28202ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 28302ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 284f1a16106SHans Petter Selasky }; 2859739167cSAlfred Perlstein 286f1a16106SHans Petter Selasky static const STRUCT_USB_DUAL_ID cdce_dual_devs[] = { 2879739167cSAlfred Perlstein {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)}, 2889739167cSAlfred Perlstein {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)}, 2894076dd23SAndrew Thompson {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)}, 29002ac6454SAndrew Thompson }; 29102ac6454SAndrew Thompson 2924076dd23SAndrew Thompson #if CDCE_HAVE_NCM 2934076dd23SAndrew Thompson /*------------------------------------------------------------------------* 2944076dd23SAndrew Thompson * cdce_ncm_init 2954076dd23SAndrew Thompson * 2964076dd23SAndrew Thompson * Return values: 2974076dd23SAndrew Thompson * 0: Success 2984076dd23SAndrew Thompson * Else: Failure 2994076dd23SAndrew Thompson *------------------------------------------------------------------------*/ 3004076dd23SAndrew Thompson static uint8_t 3014076dd23SAndrew Thompson cdce_ncm_init(struct cdce_softc *sc) 3024076dd23SAndrew Thompson { 3034076dd23SAndrew Thompson struct usb_ncm_parameters temp; 3044076dd23SAndrew Thompson struct usb_device_request req; 3057e05daaeSHans Petter Selasky struct usb_ncm_func_descriptor *ufd; 3067e05daaeSHans Petter Selasky uint8_t value[8]; 3074076dd23SAndrew Thompson int err; 3084076dd23SAndrew Thompson 3097e05daaeSHans Petter Selasky ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL, 3106d917491SHans Petter Selasky sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0xFF, 3116d917491SHans Petter Selasky UCDC_NCM_FUNC_DESC_SUBTYPE, 0xFF); 3127e05daaeSHans Petter Selasky 3137e05daaeSHans Petter Selasky /* verify length of NCM functional descriptor */ 3147e05daaeSHans Petter Selasky if (ufd != NULL) { 3157e05daaeSHans Petter Selasky if (ufd->bLength < sizeof(*ufd)) 3167e05daaeSHans Petter Selasky ufd = NULL; 3177e05daaeSHans Petter Selasky else 3187e05daaeSHans Petter Selasky DPRINTFN(1, "Found NCM functional descriptor.\n"); 3197e05daaeSHans Petter Selasky } 3207e05daaeSHans Petter Selasky 3214076dd23SAndrew Thompson req.bmRequestType = UT_READ_CLASS_INTERFACE; 3224076dd23SAndrew Thompson req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS; 3234076dd23SAndrew Thompson USETW(req.wValue, 0); 3244076dd23SAndrew Thompson req.wIndex[0] = sc->sc_ifaces_index[1]; 3254076dd23SAndrew Thompson req.wIndex[1] = 0; 3264076dd23SAndrew Thompson USETW(req.wLength, sizeof(temp)); 3274076dd23SAndrew Thompson 3284076dd23SAndrew Thompson err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req, 3294076dd23SAndrew Thompson &temp, 0, NULL, 1000 /* ms */); 3304076dd23SAndrew Thompson if (err) 3314076dd23SAndrew Thompson return (1); 3324076dd23SAndrew Thompson 3334076dd23SAndrew Thompson /* Read correct set of parameters according to device mode */ 3344076dd23SAndrew Thompson 3354076dd23SAndrew Thompson if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) { 336dd5c0f87SAndrew Thompson sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize); 337dd5c0f87SAndrew Thompson sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize); 3384076dd23SAndrew Thompson sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder); 3394076dd23SAndrew Thompson sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor); 3404076dd23SAndrew Thompson sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment); 3417e05daaeSHans Petter Selasky sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams); 3424076dd23SAndrew Thompson } else { 343dd5c0f87SAndrew Thompson sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize); 344dd5c0f87SAndrew Thompson sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize); 3454076dd23SAndrew Thompson sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder); 3464076dd23SAndrew Thompson sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor); 3474076dd23SAndrew Thompson sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment); 3487e05daaeSHans Petter Selasky sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams); 3494076dd23SAndrew Thompson } 3504076dd23SAndrew Thompson 3514076dd23SAndrew Thompson /* Verify maximum receive length */ 3524076dd23SAndrew Thompson 3537e05daaeSHans Petter Selasky if ((sc->sc_ncm.rx_max < 32) || 3544076dd23SAndrew Thompson (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) { 3554076dd23SAndrew Thompson DPRINTFN(1, "Using default maximum receive length\n"); 3564076dd23SAndrew Thompson sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN; 3574076dd23SAndrew Thompson } 3584076dd23SAndrew Thompson 3594076dd23SAndrew Thompson /* Verify maximum transmit length */ 3604076dd23SAndrew Thompson 3617e05daaeSHans Petter Selasky if ((sc->sc_ncm.tx_max < 32) || 3624076dd23SAndrew Thompson (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) { 3634076dd23SAndrew Thompson DPRINTFN(1, "Using default maximum transmit length\n"); 3644076dd23SAndrew Thompson sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN; 3654076dd23SAndrew Thompson } 3664076dd23SAndrew Thompson 3674076dd23SAndrew Thompson /* 3684076dd23SAndrew Thompson * Verify that the structure alignment is: 3694076dd23SAndrew Thompson * - power of two 3704076dd23SAndrew Thompson * - not greater than the maximum transmit length 3714076dd23SAndrew Thompson * - not less than four bytes 3724076dd23SAndrew Thompson */ 3737e05daaeSHans Petter Selasky if ((sc->sc_ncm.tx_struct_align < 4) || 3744076dd23SAndrew Thompson (sc->sc_ncm.tx_struct_align != 3754076dd23SAndrew Thompson ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) || 3764076dd23SAndrew Thompson (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) { 3774076dd23SAndrew Thompson DPRINTFN(1, "Using default other alignment: 4 bytes\n"); 3784076dd23SAndrew Thompson sc->sc_ncm.tx_struct_align = 4; 3794076dd23SAndrew Thompson } 3804076dd23SAndrew Thompson 3814076dd23SAndrew Thompson /* 3824076dd23SAndrew Thompson * Verify that the payload alignment is: 3834076dd23SAndrew Thompson * - power of two 3844076dd23SAndrew Thompson * - not greater than the maximum transmit length 3854076dd23SAndrew Thompson * - not less than four bytes 3864076dd23SAndrew Thompson */ 3877e05daaeSHans Petter Selasky if ((sc->sc_ncm.tx_modulus < 4) || 3884076dd23SAndrew Thompson (sc->sc_ncm.tx_modulus != 3894076dd23SAndrew Thompson ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) || 3904076dd23SAndrew Thompson (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) { 3914076dd23SAndrew Thompson DPRINTFN(1, "Using default transmit modulus: 4 bytes\n"); 3924076dd23SAndrew Thompson sc->sc_ncm.tx_modulus = 4; 3934076dd23SAndrew Thompson } 3944076dd23SAndrew Thompson 3954076dd23SAndrew Thompson /* Verify that the payload remainder */ 3964076dd23SAndrew Thompson 3977e05daaeSHans Petter Selasky if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) { 3984076dd23SAndrew Thompson DPRINTFN(1, "Using default transmit remainder: 0 bytes\n"); 3994076dd23SAndrew Thompson sc->sc_ncm.tx_remainder = 0; 4004076dd23SAndrew Thompson } 4014076dd23SAndrew Thompson 4027e05daaeSHans Petter Selasky /* 4037e05daaeSHans Petter Selasky * Offset the TX remainder so that IP packet payload starts at 4047e05daaeSHans Petter Selasky * the tx_modulus. This is not too clear in the specification. 4057e05daaeSHans Petter Selasky */ 4067e05daaeSHans Petter Selasky 4077e05daaeSHans Petter Selasky sc->sc_ncm.tx_remainder = 4087e05daaeSHans Petter Selasky (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) & 4097e05daaeSHans Petter Selasky (sc->sc_ncm.tx_modulus - 1); 4107e05daaeSHans Petter Selasky 4117e05daaeSHans Petter Selasky /* Verify max datagrams */ 4127e05daaeSHans Petter Selasky 4137e05daaeSHans Petter Selasky if (sc->sc_ncm.tx_nframe == 0 || 4147e05daaeSHans Petter Selasky sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) { 4157e05daaeSHans Petter Selasky DPRINTFN(1, "Using default max " 4167e05daaeSHans Petter Selasky "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1); 4177e05daaeSHans Petter Selasky /* need to reserve one entry for zero padding */ 4187e05daaeSHans Petter Selasky sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1); 4197e05daaeSHans Petter Selasky } 4207e05daaeSHans Petter Selasky 4214076dd23SAndrew Thompson /* Additional configuration, will fail in device side mode, which is OK. */ 4224076dd23SAndrew Thompson 4234076dd23SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 4244076dd23SAndrew Thompson req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE; 4254076dd23SAndrew Thompson USETW(req.wValue, 0); 4264076dd23SAndrew Thompson req.wIndex[0] = sc->sc_ifaces_index[1]; 4274076dd23SAndrew Thompson req.wIndex[1] = 0; 4287e05daaeSHans Petter Selasky 4297e05daaeSHans Petter Selasky if (ufd != NULL && 4307e05daaeSHans Petter Selasky (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) { 4317e05daaeSHans Petter Selasky USETW(req.wLength, 8); 4327e05daaeSHans Petter Selasky USETDW(value, sc->sc_ncm.rx_max); 4337e05daaeSHans Petter Selasky USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1)); 4347e05daaeSHans Petter Selasky USETW(value + 6, 0); 4357e05daaeSHans Petter Selasky } else { 4364076dd23SAndrew Thompson USETW(req.wLength, 4); 4374076dd23SAndrew Thompson USETDW(value, sc->sc_ncm.rx_max); 4387e05daaeSHans Petter Selasky } 4394076dd23SAndrew Thompson 4404076dd23SAndrew Thompson err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req, 4414076dd23SAndrew Thompson &value, 0, NULL, 1000 /* ms */); 4424076dd23SAndrew Thompson if (err) { 4434076dd23SAndrew Thompson DPRINTFN(1, "Setting input size " 4444076dd23SAndrew Thompson "to %u failed.\n", sc->sc_ncm.rx_max); 4454076dd23SAndrew Thompson } 4464076dd23SAndrew Thompson 4474076dd23SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 4484076dd23SAndrew Thompson req.bRequest = UCDC_NCM_SET_CRC_MODE; 4494076dd23SAndrew Thompson USETW(req.wValue, 0); /* no CRC */ 4504076dd23SAndrew Thompson req.wIndex[0] = sc->sc_ifaces_index[1]; 4514076dd23SAndrew Thompson req.wIndex[1] = 0; 4524076dd23SAndrew Thompson USETW(req.wLength, 0); 4534076dd23SAndrew Thompson 4544076dd23SAndrew Thompson err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req, 4554076dd23SAndrew Thompson NULL, 0, NULL, 1000 /* ms */); 4564076dd23SAndrew Thompson if (err) { 4574076dd23SAndrew Thompson DPRINTFN(1, "Setting CRC mode to off failed.\n"); 4584076dd23SAndrew Thompson } 4594076dd23SAndrew Thompson 4604076dd23SAndrew Thompson req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 4614076dd23SAndrew Thompson req.bRequest = UCDC_NCM_SET_NTB_FORMAT; 4624076dd23SAndrew Thompson USETW(req.wValue, 0); /* NTB-16 */ 4634076dd23SAndrew Thompson req.wIndex[0] = sc->sc_ifaces_index[1]; 4644076dd23SAndrew Thompson req.wIndex[1] = 0; 4654076dd23SAndrew Thompson USETW(req.wLength, 0); 4664076dd23SAndrew Thompson 4674076dd23SAndrew Thompson err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req, 4684076dd23SAndrew Thompson NULL, 0, NULL, 1000 /* ms */); 4694076dd23SAndrew Thompson if (err) { 4704076dd23SAndrew Thompson DPRINTFN(1, "Setting NTB format to 16-bit failed.\n"); 4714076dd23SAndrew Thompson } 4724076dd23SAndrew Thompson 4734076dd23SAndrew Thompson return (0); /* success */ 4744076dd23SAndrew Thompson } 4754076dd23SAndrew Thompson #endif 4764076dd23SAndrew Thompson 47702ac6454SAndrew Thompson static int 47802ac6454SAndrew Thompson cdce_probe(device_t dev) 47902ac6454SAndrew Thompson { 480760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 481f1a16106SHans Petter Selasky int error; 48202ac6454SAndrew Thompson 483f1a16106SHans Petter Selasky error = usbd_lookup_id_by_uaa(cdce_host_devs, sizeof(cdce_host_devs), uaa); 484f1a16106SHans Petter Selasky if (error) 485f1a16106SHans Petter Selasky error = usbd_lookup_id_by_uaa(cdce_dual_devs, sizeof(cdce_dual_devs), uaa); 486f1a16106SHans Petter Selasky return (error); 48702ac6454SAndrew Thompson } 48802ac6454SAndrew Thompson 48902ac6454SAndrew Thompson static void 490760bc48eSAndrew Thompson cdce_attach_post(struct usb_ether *ue) 49102ac6454SAndrew Thompson { 49202ac6454SAndrew Thompson /* no-op */ 49302ac6454SAndrew Thompson return; 49402ac6454SAndrew Thompson } 49502ac6454SAndrew Thompson 49602ac6454SAndrew Thompson static int 49702ac6454SAndrew Thompson cdce_attach(device_t dev) 49802ac6454SAndrew Thompson { 49902ac6454SAndrew Thompson struct cdce_softc *sc = device_get_softc(dev); 500760bc48eSAndrew Thompson struct usb_ether *ue = &sc->sc_ue; 501760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 502760bc48eSAndrew Thompson struct usb_interface *iface; 503760bc48eSAndrew Thompson const struct usb_cdc_union_descriptor *ud; 504760bc48eSAndrew Thompson const struct usb_interface_descriptor *id; 505760bc48eSAndrew Thompson const struct usb_cdc_ethernet_descriptor *ued; 5064076dd23SAndrew Thompson const struct usb_config *pcfg; 507a8df530dSJohn Baldwin uint32_t seed; 50802ac6454SAndrew Thompson int error; 50902ac6454SAndrew Thompson uint8_t i; 5104076dd23SAndrew Thompson uint8_t data_iface_no; 51102ac6454SAndrew Thompson char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */ 51202ac6454SAndrew Thompson 51302ac6454SAndrew Thompson sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 5144076dd23SAndrew Thompson sc->sc_ue.ue_udev = uaa->device; 51502ac6454SAndrew Thompson 516a593f6b8SAndrew Thompson device_set_usb_desc(dev); 51702ac6454SAndrew Thompson 51802ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 51902ac6454SAndrew Thompson 5203c8d24f4SAndrew Thompson ud = usbd_find_descriptor 52102ac6454SAndrew Thompson (uaa->device, NULL, uaa->info.bIfaceIndex, 5226d917491SHans Petter Selasky UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_UNION, 0xFF); 52302ac6454SAndrew Thompson 5244076dd23SAndrew Thompson if ((ud == NULL) || (ud->bLength < sizeof(*ud)) || 5254076dd23SAndrew Thompson (sc->sc_flags & CDCE_FLAG_NO_UNION)) { 5264076dd23SAndrew Thompson DPRINTFN(1, "No union descriptor!\n"); 5274076dd23SAndrew Thompson sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex; 5284076dd23SAndrew Thompson sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex; 5294076dd23SAndrew Thompson goto alloc_transfers; 53002ac6454SAndrew Thompson } 5314076dd23SAndrew Thompson data_iface_no = ud->bSlaveInterface[0]; 53202ac6454SAndrew Thompson 53302ac6454SAndrew Thompson for (i = 0;; i++) { 53402ac6454SAndrew Thompson 535a593f6b8SAndrew Thompson iface = usbd_get_iface(uaa->device, i); 53602ac6454SAndrew Thompson 53702ac6454SAndrew Thompson if (iface) { 53802ac6454SAndrew Thompson 539a593f6b8SAndrew Thompson id = usbd_get_interface_descriptor(iface); 54002ac6454SAndrew Thompson 5414076dd23SAndrew Thompson if (id && (id->bInterfaceNumber == data_iface_no)) { 54202ac6454SAndrew Thompson sc->sc_ifaces_index[0] = i; 54302ac6454SAndrew Thompson sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex; 544a593f6b8SAndrew Thompson usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex); 54502ac6454SAndrew Thompson break; 54602ac6454SAndrew Thompson } 54702ac6454SAndrew Thompson } else { 548767cb2e2SAndrew Thompson device_printf(dev, "no data interface found\n"); 54902ac6454SAndrew Thompson goto detach; 55002ac6454SAndrew Thompson } 55102ac6454SAndrew Thompson } 55202ac6454SAndrew Thompson 55302ac6454SAndrew Thompson /* 55402ac6454SAndrew Thompson * <quote> 55502ac6454SAndrew Thompson * 55602ac6454SAndrew Thompson * The Data Class interface of a networking device shall have 55702ac6454SAndrew Thompson * a minimum of two interface settings. The first setting 55802ac6454SAndrew Thompson * (the default interface setting) includes no endpoints and 55902ac6454SAndrew Thompson * therefore no networking traffic is exchanged whenever the 56002ac6454SAndrew Thompson * default interface setting is selected. One or more 56102ac6454SAndrew Thompson * additional interface settings are used for normal 56202ac6454SAndrew Thompson * operation, and therefore each includes a pair of endpoints 56302ac6454SAndrew Thompson * (one IN, and one OUT) to exchange network traffic. Select 56402ac6454SAndrew Thompson * an alternate interface setting to initialize the network 56502ac6454SAndrew Thompson * aspects of the device and to enable the exchange of 56602ac6454SAndrew Thompson * network traffic. 56702ac6454SAndrew Thompson * 56802ac6454SAndrew Thompson * </quote> 56902ac6454SAndrew Thompson * 57002ac6454SAndrew Thompson * Some devices, most notably cable modems, include interface 57102ac6454SAndrew Thompson * settings that have no IN or OUT endpoint, therefore loop 57202ac6454SAndrew Thompson * through the list of all available interface settings 57302ac6454SAndrew Thompson * looking for one with both IN and OUT endpoints. 57402ac6454SAndrew Thompson */ 57502ac6454SAndrew Thompson 57602ac6454SAndrew Thompson alloc_transfers: 57702ac6454SAndrew Thompson 5784076dd23SAndrew Thompson pcfg = cdce_config; /* Default Configuration */ 5794076dd23SAndrew Thompson 58002ac6454SAndrew Thompson for (i = 0; i != 32; i++) { 58102ac6454SAndrew Thompson 5824076dd23SAndrew Thompson error = usbd_set_alt_interface_index(uaa->device, 5834076dd23SAndrew Thompson sc->sc_ifaces_index[0], i); 5844076dd23SAndrew Thompson if (error) 5854076dd23SAndrew Thompson break; 5864076dd23SAndrew Thompson #if CDCE_HAVE_NCM 5874076dd23SAndrew Thompson if ((i == 0) && (cdce_ncm_init(sc) == 0)) 5884076dd23SAndrew Thompson pcfg = cdce_ncm_config; 5894076dd23SAndrew Thompson #endif 5904076dd23SAndrew Thompson error = usbd_transfer_setup(uaa->device, 5914076dd23SAndrew Thompson sc->sc_ifaces_index, sc->sc_xfer, 5924076dd23SAndrew Thompson pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx); 59302ac6454SAndrew Thompson 5944076dd23SAndrew Thompson if (error == 0) 59502ac6454SAndrew Thompson break; 59602ac6454SAndrew Thompson } 5974076dd23SAndrew Thompson 5984076dd23SAndrew Thompson if (error || (i == 32)) { 5994076dd23SAndrew Thompson device_printf(dev, "No valid alternate " 600767cb2e2SAndrew Thompson "setting found\n"); 6014076dd23SAndrew Thompson goto detach; 60202ac6454SAndrew Thompson } 60302ac6454SAndrew Thompson 6043c8d24f4SAndrew Thompson ued = usbd_find_descriptor 60502ac6454SAndrew Thompson (uaa->device, NULL, uaa->info.bIfaceIndex, 6066d917491SHans Petter Selasky UDESC_CS_INTERFACE, 0xFF, UDESCSUB_CDC_ENF, 0xFF); 60702ac6454SAndrew Thompson 60802ac6454SAndrew Thompson if ((ued == NULL) || (ued->bLength < sizeof(*ued))) { 60902ac6454SAndrew Thompson error = USB_ERR_INVAL; 61002ac6454SAndrew Thompson } else { 611a593f6b8SAndrew Thompson error = usbd_req_get_string_any(uaa->device, NULL, 61202ac6454SAndrew Thompson eaddr_str, sizeof(eaddr_str), ued->iMacAddress); 61302ac6454SAndrew Thompson } 61402ac6454SAndrew Thompson 61502ac6454SAndrew Thompson if (error) { 61602ac6454SAndrew Thompson 61702ac6454SAndrew Thompson /* fake MAC address */ 61802ac6454SAndrew Thompson 61902ac6454SAndrew Thompson device_printf(dev, "faking MAC address\n"); 620a8df530dSJohn Baldwin seed = ticks; 62102ac6454SAndrew Thompson sc->sc_ue.ue_eaddr[0] = 0x2a; 622a8df530dSJohn Baldwin memcpy(&sc->sc_ue.ue_eaddr[1], &seed, sizeof(uint32_t)); 62302ac6454SAndrew Thompson sc->sc_ue.ue_eaddr[5] = device_get_unit(dev); 62402ac6454SAndrew Thompson 62502ac6454SAndrew Thompson } else { 62602ac6454SAndrew Thompson 6277e05daaeSHans Petter Selasky memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr)); 62802ac6454SAndrew Thompson 62902ac6454SAndrew Thompson for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) { 63002ac6454SAndrew Thompson 63102ac6454SAndrew Thompson char c = eaddr_str[i]; 63202ac6454SAndrew Thompson 63302ac6454SAndrew Thompson if ('0' <= c && c <= '9') 63402ac6454SAndrew Thompson c -= '0'; 63502ac6454SAndrew Thompson else if (c != 0) 63602ac6454SAndrew Thompson c -= 'A' - 10; 63702ac6454SAndrew Thompson else 63802ac6454SAndrew Thompson break; 63902ac6454SAndrew Thompson 64002ac6454SAndrew Thompson c &= 0xf; 64102ac6454SAndrew Thompson 64202ac6454SAndrew Thompson if ((i & 1) == 0) 64302ac6454SAndrew Thompson c <<= 4; 64402ac6454SAndrew Thompson sc->sc_ue.ue_eaddr[i / 2] |= c; 64502ac6454SAndrew Thompson } 64602ac6454SAndrew Thompson 647f29a0724SAndrew Thompson if (uaa->usb_mode == USB_MODE_DEVICE) { 64802ac6454SAndrew Thompson /* 64902ac6454SAndrew Thompson * Do not use the same MAC address like the peer ! 65002ac6454SAndrew Thompson */ 65102ac6454SAndrew Thompson sc->sc_ue.ue_eaddr[5] ^= 0xFF; 65202ac6454SAndrew Thompson } 65302ac6454SAndrew Thompson } 65402ac6454SAndrew Thompson 65502ac6454SAndrew Thompson ue->ue_sc = sc; 65602ac6454SAndrew Thompson ue->ue_dev = dev; 65702ac6454SAndrew Thompson ue->ue_udev = uaa->device; 65802ac6454SAndrew Thompson ue->ue_mtx = &sc->sc_mtx; 65902ac6454SAndrew Thompson ue->ue_methods = &cdce_ue_methods; 66002ac6454SAndrew Thompson 661a593f6b8SAndrew Thompson error = uether_ifattach(ue); 66202ac6454SAndrew Thompson if (error) { 66302ac6454SAndrew Thompson device_printf(dev, "could not attach interface\n"); 66402ac6454SAndrew Thompson goto detach; 66502ac6454SAndrew Thompson } 66602ac6454SAndrew Thompson return (0); /* success */ 66702ac6454SAndrew Thompson 66802ac6454SAndrew Thompson detach: 66902ac6454SAndrew Thompson cdce_detach(dev); 67002ac6454SAndrew Thompson return (ENXIO); /* failure */ 67102ac6454SAndrew Thompson } 67202ac6454SAndrew Thompson 67302ac6454SAndrew Thompson static int 67402ac6454SAndrew Thompson cdce_detach(device_t dev) 67502ac6454SAndrew Thompson { 67602ac6454SAndrew Thompson struct cdce_softc *sc = device_get_softc(dev); 677760bc48eSAndrew Thompson struct usb_ether *ue = &sc->sc_ue; 67802ac6454SAndrew Thompson 67902ac6454SAndrew Thompson /* stop all USB transfers first */ 680a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER); 681a593f6b8SAndrew Thompson uether_ifdetach(ue); 68202ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx); 68302ac6454SAndrew Thompson 68402ac6454SAndrew Thompson return (0); 68502ac6454SAndrew Thompson } 68602ac6454SAndrew Thompson 68702ac6454SAndrew Thompson static void 688760bc48eSAndrew Thompson cdce_start(struct usb_ether *ue) 68902ac6454SAndrew Thompson { 690a593f6b8SAndrew Thompson struct cdce_softc *sc = uether_getsc(ue); 69102ac6454SAndrew Thompson 69202ac6454SAndrew Thompson /* 69302ac6454SAndrew Thompson * Start the USB transfers, if not already started: 69402ac6454SAndrew Thompson */ 695a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]); 696a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]); 69702ac6454SAndrew Thompson } 69802ac6454SAndrew Thompson 69902ac6454SAndrew Thompson static void 70002ac6454SAndrew Thompson cdce_free_queue(struct mbuf **ppm, uint8_t n) 70102ac6454SAndrew Thompson { 70202ac6454SAndrew Thompson uint8_t x; 70302ac6454SAndrew Thompson for (x = 0; x != n; x++) { 70402ac6454SAndrew Thompson if (ppm[x] != NULL) { 70502ac6454SAndrew Thompson m_freem(ppm[x]); 70602ac6454SAndrew Thompson ppm[x] = NULL; 70702ac6454SAndrew Thompson } 70802ac6454SAndrew Thompson } 70902ac6454SAndrew Thompson } 71002ac6454SAndrew Thompson 71102ac6454SAndrew Thompson static void 712ed6d949aSAndrew Thompson cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 71302ac6454SAndrew Thompson { 714ed6d949aSAndrew Thompson struct cdce_softc *sc = usbd_xfer_softc(xfer); 715a593f6b8SAndrew Thompson struct ifnet *ifp = uether_getifp(&sc->sc_ue); 71602ac6454SAndrew Thompson struct mbuf *m; 71702ac6454SAndrew Thompson struct mbuf *mt; 71802ac6454SAndrew Thompson uint32_t crc; 71902ac6454SAndrew Thompson uint8_t x; 720ed6d949aSAndrew Thompson int actlen, aframes; 721ed6d949aSAndrew Thompson 722ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 72302ac6454SAndrew Thompson 72402ac6454SAndrew Thompson DPRINTFN(1, "\n"); 72502ac6454SAndrew Thompson 72602ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 72702ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 728ed6d949aSAndrew Thompson DPRINTFN(11, "transfer complete: %u bytes in %u frames\n", 729ed6d949aSAndrew Thompson actlen, aframes); 73002ac6454SAndrew Thompson 73102ac6454SAndrew Thompson ifp->if_opackets++; 73202ac6454SAndrew Thompson 73302ac6454SAndrew Thompson /* free all previous TX buffers */ 73402ac6454SAndrew Thompson cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX); 73502ac6454SAndrew Thompson 73602ac6454SAndrew Thompson /* FALLTHROUGH */ 73702ac6454SAndrew Thompson case USB_ST_SETUP: 73802ac6454SAndrew Thompson tr_setup: 73902ac6454SAndrew Thompson for (x = 0; x != CDCE_FRAMES_MAX; x++) { 74002ac6454SAndrew Thompson 74102ac6454SAndrew Thompson IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 74202ac6454SAndrew Thompson 74302ac6454SAndrew Thompson if (m == NULL) 74402ac6454SAndrew Thompson break; 74502ac6454SAndrew Thompson 74602ac6454SAndrew Thompson if (sc->sc_flags & CDCE_FLAG_ZAURUS) { 74702ac6454SAndrew Thompson /* 74802ac6454SAndrew Thompson * Zaurus wants a 32-bit CRC appended 74902ac6454SAndrew Thompson * to every frame 75002ac6454SAndrew Thompson */ 75102ac6454SAndrew Thompson 75202ac6454SAndrew Thompson crc = cdce_m_crc32(m, 0, m->m_pkthdr.len); 75302ac6454SAndrew Thompson crc = htole32(crc); 75402ac6454SAndrew Thompson 75502ac6454SAndrew Thompson if (!m_append(m, 4, (void *)&crc)) { 75602ac6454SAndrew Thompson m_freem(m); 75702ac6454SAndrew Thompson ifp->if_oerrors++; 75802ac6454SAndrew Thompson continue; 75902ac6454SAndrew Thompson } 76002ac6454SAndrew Thompson } 76102ac6454SAndrew Thompson if (m->m_len != m->m_pkthdr.len) { 762c6499eccSGleb Smirnoff mt = m_defrag(m, M_NOWAIT); 76302ac6454SAndrew Thompson if (mt == NULL) { 76402ac6454SAndrew Thompson m_freem(m); 76502ac6454SAndrew Thompson ifp->if_oerrors++; 76602ac6454SAndrew Thompson continue; 76702ac6454SAndrew Thompson } 76802ac6454SAndrew Thompson m = mt; 76902ac6454SAndrew Thompson } 77002ac6454SAndrew Thompson if (m->m_pkthdr.len > MCLBYTES) { 77102ac6454SAndrew Thompson m->m_pkthdr.len = MCLBYTES; 77202ac6454SAndrew Thompson } 77302ac6454SAndrew Thompson sc->sc_tx_buf[x] = m; 774ed6d949aSAndrew Thompson usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len); 77502ac6454SAndrew Thompson 77602ac6454SAndrew Thompson /* 77702ac6454SAndrew Thompson * If there's a BPF listener, bounce a copy of 77802ac6454SAndrew Thompson * this frame to him: 77902ac6454SAndrew Thompson */ 78002ac6454SAndrew Thompson BPF_MTAP(ifp, m); 78102ac6454SAndrew Thompson } 78202ac6454SAndrew Thompson if (x != 0) { 783ed6d949aSAndrew Thompson usbd_xfer_set_frames(xfer, x); 784ed6d949aSAndrew Thompson 785a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 78602ac6454SAndrew Thompson } 78702ac6454SAndrew Thompson break; 78802ac6454SAndrew Thompson 78902ac6454SAndrew Thompson default: /* Error */ 79002ac6454SAndrew Thompson DPRINTFN(11, "transfer error, %s\n", 791ed6d949aSAndrew Thompson usbd_errstr(error)); 79202ac6454SAndrew Thompson 79302ac6454SAndrew Thompson /* free all previous TX buffers */ 79402ac6454SAndrew Thompson cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX); 79502ac6454SAndrew Thompson 79602ac6454SAndrew Thompson /* count output errors */ 79702ac6454SAndrew Thompson ifp->if_oerrors++; 79802ac6454SAndrew Thompson 799ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 80002ac6454SAndrew Thompson /* try to clear stall first */ 801ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 80202ac6454SAndrew Thompson goto tr_setup; 80302ac6454SAndrew Thompson } 80402ac6454SAndrew Thompson break; 80502ac6454SAndrew Thompson } 80602ac6454SAndrew Thompson } 80702ac6454SAndrew Thompson 80802ac6454SAndrew Thompson static int32_t 80902ac6454SAndrew Thompson cdce_m_crc32_cb(void *arg, void *src, uint32_t count) 81002ac6454SAndrew Thompson { 81102ac6454SAndrew Thompson uint32_t *p_crc = arg; 81202ac6454SAndrew Thompson 81302ac6454SAndrew Thompson *p_crc = crc32_raw(src, count, *p_crc); 81402ac6454SAndrew Thompson return (0); 81502ac6454SAndrew Thompson } 81602ac6454SAndrew Thompson 81702ac6454SAndrew Thompson static uint32_t 81802ac6454SAndrew Thompson cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len) 81902ac6454SAndrew Thompson { 82002ac6454SAndrew Thompson uint32_t crc = 0xFFFFFFFF; 82102ac6454SAndrew Thompson int error; 82202ac6454SAndrew Thompson 82302ac6454SAndrew Thompson error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc); 82402ac6454SAndrew Thompson return (crc ^ 0xFFFFFFFF); 82502ac6454SAndrew Thompson } 82602ac6454SAndrew Thompson 82702ac6454SAndrew Thompson static void 828760bc48eSAndrew Thompson cdce_init(struct usb_ether *ue) 82902ac6454SAndrew Thompson { 830a593f6b8SAndrew Thompson struct cdce_softc *sc = uether_getsc(ue); 831a593f6b8SAndrew Thompson struct ifnet *ifp = uether_getifp(ue); 83202ac6454SAndrew Thompson 83302ac6454SAndrew Thompson CDCE_LOCK_ASSERT(sc, MA_OWNED); 83402ac6454SAndrew Thompson 83502ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_RUNNING; 83602ac6454SAndrew Thompson 83702ac6454SAndrew Thompson /* start interrupt transfer */ 838a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]); 839a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]); 84002ac6454SAndrew Thompson 84102ac6454SAndrew Thompson /* stall data write direction, which depends on USB mode */ 842ed6d949aSAndrew Thompson usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]); 84302ac6454SAndrew Thompson 84402ac6454SAndrew Thompson /* start data transfers */ 84502ac6454SAndrew Thompson cdce_start(ue); 84602ac6454SAndrew Thompson } 84702ac6454SAndrew Thompson 84802ac6454SAndrew Thompson static void 849760bc48eSAndrew Thompson cdce_stop(struct usb_ether *ue) 85002ac6454SAndrew Thompson { 851a593f6b8SAndrew Thompson struct cdce_softc *sc = uether_getsc(ue); 852a593f6b8SAndrew Thompson struct ifnet *ifp = uether_getifp(ue); 85302ac6454SAndrew Thompson 85402ac6454SAndrew Thompson CDCE_LOCK_ASSERT(sc, MA_OWNED); 85502ac6454SAndrew Thompson 85602ac6454SAndrew Thompson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 85702ac6454SAndrew Thompson 85802ac6454SAndrew Thompson /* 85902ac6454SAndrew Thompson * stop all the transfers, if not already stopped: 86002ac6454SAndrew Thompson */ 861a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]); 862a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]); 863a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]); 864a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]); 86502ac6454SAndrew Thompson } 86602ac6454SAndrew Thompson 86702ac6454SAndrew Thompson static void 868760bc48eSAndrew Thompson cdce_setmulti(struct usb_ether *ue) 86902ac6454SAndrew Thompson { 87002ac6454SAndrew Thompson /* no-op */ 87102ac6454SAndrew Thompson return; 87202ac6454SAndrew Thompson } 87302ac6454SAndrew Thompson 87402ac6454SAndrew Thompson static void 875760bc48eSAndrew Thompson cdce_setpromisc(struct usb_ether *ue) 87602ac6454SAndrew Thompson { 87702ac6454SAndrew Thompson /* no-op */ 87802ac6454SAndrew Thompson return; 87902ac6454SAndrew Thompson } 88002ac6454SAndrew Thompson 88102ac6454SAndrew Thompson static int 88202ac6454SAndrew Thompson cdce_suspend(device_t dev) 88302ac6454SAndrew Thompson { 88402ac6454SAndrew Thompson device_printf(dev, "Suspending\n"); 88502ac6454SAndrew Thompson return (0); 88602ac6454SAndrew Thompson } 88702ac6454SAndrew Thompson 88802ac6454SAndrew Thompson static int 88902ac6454SAndrew Thompson cdce_resume(device_t dev) 89002ac6454SAndrew Thompson { 89102ac6454SAndrew Thompson device_printf(dev, "Resuming\n"); 89202ac6454SAndrew Thompson return (0); 89302ac6454SAndrew Thompson } 89402ac6454SAndrew Thompson 89502ac6454SAndrew Thompson static void 896ed6d949aSAndrew Thompson cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 89702ac6454SAndrew Thompson { 898ed6d949aSAndrew Thompson struct cdce_softc *sc = usbd_xfer_softc(xfer); 89902ac6454SAndrew Thompson struct mbuf *m; 90002ac6454SAndrew Thompson uint8_t x; 9016d917491SHans Petter Selasky int actlen; 9026d917491SHans Petter Selasky int aframes; 9036d917491SHans Petter Selasky int len; 904ed6d949aSAndrew Thompson 905ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 90602ac6454SAndrew Thompson 90702ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 90802ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 90902ac6454SAndrew Thompson 910ed6d949aSAndrew Thompson DPRINTF("received %u bytes in %u frames\n", actlen, aframes); 91102ac6454SAndrew Thompson 912ed6d949aSAndrew Thompson for (x = 0; x != aframes; x++) { 91302ac6454SAndrew Thompson 91402ac6454SAndrew Thompson m = sc->sc_rx_buf[x]; 91502ac6454SAndrew Thompson sc->sc_rx_buf[x] = NULL; 9168f9e0ef9SAndrew Thompson len = usbd_xfer_frame_len(xfer, x); 91702ac6454SAndrew Thompson 91802ac6454SAndrew Thompson /* Strip off CRC added by Zaurus, if any */ 919ed6d949aSAndrew Thompson if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14) 920ed6d949aSAndrew Thompson len -= 4; 92102ac6454SAndrew Thompson 9226d917491SHans Petter Selasky if (len < (int)sizeof(struct ether_header)) { 92302ac6454SAndrew Thompson m_freem(m); 92402ac6454SAndrew Thompson continue; 92502ac6454SAndrew Thompson } 92602ac6454SAndrew Thompson /* queue up mbuf */ 927ed6d949aSAndrew Thompson uether_rxmbuf(&sc->sc_ue, m, len); 92802ac6454SAndrew Thompson } 92902ac6454SAndrew Thompson 93002ac6454SAndrew Thompson /* FALLTHROUGH */ 93102ac6454SAndrew Thompson case USB_ST_SETUP: 93202ac6454SAndrew Thompson /* 93302ac6454SAndrew Thompson * TODO: Implement support for multi frame transfers, 93402ac6454SAndrew Thompson * when the USB hardware supports it. 93502ac6454SAndrew Thompson */ 93602ac6454SAndrew Thompson for (x = 0; x != 1; x++) { 93702ac6454SAndrew Thompson if (sc->sc_rx_buf[x] == NULL) { 938a593f6b8SAndrew Thompson m = uether_newbuf(); 93902ac6454SAndrew Thompson if (m == NULL) 94002ac6454SAndrew Thompson goto tr_stall; 94102ac6454SAndrew Thompson sc->sc_rx_buf[x] = m; 94202ac6454SAndrew Thompson } else { 94302ac6454SAndrew Thompson m = sc->sc_rx_buf[x]; 94402ac6454SAndrew Thompson } 94502ac6454SAndrew Thompson 946ed6d949aSAndrew Thompson usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len); 94702ac6454SAndrew Thompson } 94802ac6454SAndrew Thompson /* set number of frames and start hardware */ 949ed6d949aSAndrew Thompson usbd_xfer_set_frames(xfer, x); 950a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 95102ac6454SAndrew Thompson /* flush any received frames */ 952a593f6b8SAndrew Thompson uether_rxflush(&sc->sc_ue); 95302ac6454SAndrew Thompson break; 95402ac6454SAndrew Thompson 95502ac6454SAndrew Thompson default: /* Error */ 95602ac6454SAndrew Thompson DPRINTF("error = %s\n", 957ed6d949aSAndrew Thompson usbd_errstr(error)); 95802ac6454SAndrew Thompson 959ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 96002ac6454SAndrew Thompson tr_stall: 96102ac6454SAndrew Thompson /* try to clear stall first */ 962ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 963ed6d949aSAndrew Thompson usbd_xfer_set_frames(xfer, 0); 964a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 96502ac6454SAndrew Thompson break; 96602ac6454SAndrew Thompson } 96702ac6454SAndrew Thompson 96802ac6454SAndrew Thompson /* need to free the RX-mbufs when we are cancelled */ 96902ac6454SAndrew Thompson cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX); 97002ac6454SAndrew Thompson break; 97102ac6454SAndrew Thompson } 97202ac6454SAndrew Thompson } 97302ac6454SAndrew Thompson 97402ac6454SAndrew Thompson static void 975ed6d949aSAndrew Thompson cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 97602ac6454SAndrew Thompson { 977ed6d949aSAndrew Thompson int actlen; 978ed6d949aSAndrew Thompson 979ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 980ed6d949aSAndrew Thompson 98102ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 98202ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 98302ac6454SAndrew Thompson 984ed6d949aSAndrew Thompson DPRINTF("Received %d bytes\n", actlen); 98502ac6454SAndrew Thompson 98602ac6454SAndrew Thompson /* TODO: decode some indications */ 98702ac6454SAndrew Thompson 98802ac6454SAndrew Thompson /* FALLTHROUGH */ 98902ac6454SAndrew Thompson case USB_ST_SETUP: 99002ac6454SAndrew Thompson tr_setup: 991ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 992a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 99302ac6454SAndrew Thompson break; 99402ac6454SAndrew Thompson 99502ac6454SAndrew Thompson default: /* Error */ 996ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 99702ac6454SAndrew Thompson /* start clear stall */ 998ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 99902ac6454SAndrew Thompson goto tr_setup; 100002ac6454SAndrew Thompson } 100102ac6454SAndrew Thompson break; 100202ac6454SAndrew Thompson } 100302ac6454SAndrew Thompson } 100402ac6454SAndrew Thompson 100502ac6454SAndrew Thompson static void 1006ed6d949aSAndrew Thompson cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error) 100702ac6454SAndrew Thompson { 1008ed6d949aSAndrew Thompson int actlen; 1009ed6d949aSAndrew Thompson 1010ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1011ed6d949aSAndrew Thompson 101202ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 101302ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 101402ac6454SAndrew Thompson 1015ed6d949aSAndrew Thompson DPRINTF("Transferred %d bytes\n", actlen); 101602ac6454SAndrew Thompson 101702ac6454SAndrew Thompson /* FALLTHROUGH */ 101802ac6454SAndrew Thompson case USB_ST_SETUP: 101902ac6454SAndrew Thompson tr_setup: 102002ac6454SAndrew Thompson #if 0 1021ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, XXX); 1022a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 102302ac6454SAndrew Thompson #endif 102402ac6454SAndrew Thompson break; 102502ac6454SAndrew Thompson 102602ac6454SAndrew Thompson default: /* Error */ 1027ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 102802ac6454SAndrew Thompson /* start clear stall */ 1029ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 103002ac6454SAndrew Thompson goto tr_setup; 103102ac6454SAndrew Thompson } 103202ac6454SAndrew Thompson break; 103302ac6454SAndrew Thompson } 103402ac6454SAndrew Thompson } 103502ac6454SAndrew Thompson 103602ac6454SAndrew Thompson static int 103702ac6454SAndrew Thompson cdce_handle_request(device_t dev, 103802ac6454SAndrew Thompson const void *req, void **pptr, uint16_t *plen, 103929bd7d7eSAndrew Thompson uint16_t offset, uint8_t *pstate) 104002ac6454SAndrew Thompson { 104102ac6454SAndrew Thompson return (ENXIO); /* use builtin handler */ 104202ac6454SAndrew Thompson } 10434076dd23SAndrew Thompson 10444076dd23SAndrew Thompson #if CDCE_HAVE_NCM 10457e05daaeSHans Petter Selasky static void 10467e05daaeSHans Petter Selasky cdce_ncm_tx_zero(struct usb_page_cache *pc, 10477e05daaeSHans Petter Selasky uint32_t start, uint32_t end) 10487e05daaeSHans Petter Selasky { 10497e05daaeSHans Petter Selasky if (start >= CDCE_NCM_TX_MAXLEN) 10507e05daaeSHans Petter Selasky return; 10517e05daaeSHans Petter Selasky if (end > CDCE_NCM_TX_MAXLEN) 10527e05daaeSHans Petter Selasky end = CDCE_NCM_TX_MAXLEN; 10537e05daaeSHans Petter Selasky 10547e05daaeSHans Petter Selasky usbd_frame_zero(pc, start, end - start); 10557e05daaeSHans Petter Selasky } 10567e05daaeSHans Petter Selasky 10574076dd23SAndrew Thompson static uint8_t 10584076dd23SAndrew Thompson cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index) 10594076dd23SAndrew Thompson { 10604076dd23SAndrew Thompson struct cdce_softc *sc = usbd_xfer_softc(xfer); 10614076dd23SAndrew Thompson struct ifnet *ifp = uether_getifp(&sc->sc_ue); 10624076dd23SAndrew Thompson struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index); 10634076dd23SAndrew Thompson struct mbuf *m; 10644076dd23SAndrew Thompson uint32_t rem; 10654076dd23SAndrew Thompson uint32_t offset; 10664076dd23SAndrew Thompson uint32_t last_offset; 10677e05daaeSHans Petter Selasky uint16_t n; 10687e05daaeSHans Petter Selasky uint8_t retval; 10694076dd23SAndrew Thompson 10704076dd23SAndrew Thompson usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index); 10714076dd23SAndrew Thompson 10724076dd23SAndrew Thompson offset = sizeof(sc->sc_ncm.hdr) + 10734076dd23SAndrew Thompson sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp); 10744076dd23SAndrew Thompson 10754076dd23SAndrew Thompson /* Store last valid offset before alignment */ 10764076dd23SAndrew Thompson last_offset = offset; 10774076dd23SAndrew Thompson 10787e05daaeSHans Petter Selasky /* Align offset */ 10797e05daaeSHans Petter Selasky offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder, 10807e05daaeSHans Petter Selasky offset, sc->sc_ncm.tx_modulus); 10814076dd23SAndrew Thompson 10827e05daaeSHans Petter Selasky /* Zero pad */ 10837e05daaeSHans Petter Selasky cdce_ncm_tx_zero(pc, last_offset, offset); 10847e05daaeSHans Petter Selasky 10857e05daaeSHans Petter Selasky /* buffer full */ 10867e05daaeSHans Petter Selasky retval = 2; 10877e05daaeSHans Petter Selasky 10887e05daaeSHans Petter Selasky for (n = 0; n != sc->sc_ncm.tx_nframe; n++) { 10894076dd23SAndrew Thompson 10904076dd23SAndrew Thompson /* check if end of transmit buffer is reached */ 10914076dd23SAndrew Thompson 10924076dd23SAndrew Thompson if (offset >= sc->sc_ncm.tx_max) 10934076dd23SAndrew Thompson break; 10944076dd23SAndrew Thompson 10954076dd23SAndrew Thompson /* compute maximum buffer size */ 10964076dd23SAndrew Thompson 10974076dd23SAndrew Thompson rem = sc->sc_ncm.tx_max - offset; 10984076dd23SAndrew Thompson 10994076dd23SAndrew Thompson IFQ_DRV_DEQUEUE(&(ifp->if_snd), m); 11004076dd23SAndrew Thompson 11017e05daaeSHans Petter Selasky if (m == NULL) { 11027e05daaeSHans Petter Selasky /* buffer not full */ 11037e05daaeSHans Petter Selasky retval = 1; 11044076dd23SAndrew Thompson break; 11057e05daaeSHans Petter Selasky } 11064076dd23SAndrew Thompson 11076d917491SHans Petter Selasky if (m->m_pkthdr.len > (int)rem) { 11084076dd23SAndrew Thompson if (n == 0) { 11094076dd23SAndrew Thompson /* The frame won't fit in our buffer */ 11104076dd23SAndrew Thompson DPRINTFN(1, "Frame too big to be transmitted!\n"); 11114076dd23SAndrew Thompson m_freem(m); 11124076dd23SAndrew Thompson ifp->if_oerrors++; 11134076dd23SAndrew Thompson n--; 11144076dd23SAndrew Thompson continue; 11154076dd23SAndrew Thompson } 11164076dd23SAndrew Thompson /* Wait till next buffer becomes ready */ 11174076dd23SAndrew Thompson IFQ_DRV_PREPEND(&(ifp->if_snd), m); 11184076dd23SAndrew Thompson break; 11194076dd23SAndrew Thompson } 11204076dd23SAndrew Thompson usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len); 11214076dd23SAndrew Thompson 11224076dd23SAndrew Thompson USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len); 11234076dd23SAndrew Thompson USETW(sc->sc_ncm.dp[n].wFrameIndex, offset); 11244076dd23SAndrew Thompson 11254076dd23SAndrew Thompson /* Update offset */ 11264076dd23SAndrew Thompson offset += m->m_pkthdr.len; 11274076dd23SAndrew Thompson 11284076dd23SAndrew Thompson /* Store last valid offset before alignment */ 11294076dd23SAndrew Thompson last_offset = offset; 11304076dd23SAndrew Thompson 11317e05daaeSHans Petter Selasky /* Align offset */ 11327e05daaeSHans Petter Selasky offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder, 11337e05daaeSHans Petter Selasky offset, sc->sc_ncm.tx_modulus); 11347e05daaeSHans Petter Selasky 11357e05daaeSHans Petter Selasky /* Zero pad */ 11367e05daaeSHans Petter Selasky cdce_ncm_tx_zero(pc, last_offset, offset); 11374076dd23SAndrew Thompson 11384076dd23SAndrew Thompson /* 11394076dd23SAndrew Thompson * If there's a BPF listener, bounce a copy 11404076dd23SAndrew Thompson * of this frame to him: 11414076dd23SAndrew Thompson */ 11424076dd23SAndrew Thompson BPF_MTAP(ifp, m); 11434076dd23SAndrew Thompson 11444076dd23SAndrew Thompson /* Free mbuf */ 11454076dd23SAndrew Thompson 11464076dd23SAndrew Thompson m_freem(m); 11474076dd23SAndrew Thompson 11484076dd23SAndrew Thompson /* Pre-increment interface counter */ 11494076dd23SAndrew Thompson 11504076dd23SAndrew Thompson ifp->if_opackets++; 11514076dd23SAndrew Thompson } 11524076dd23SAndrew Thompson 11534076dd23SAndrew Thompson if (n == 0) 11547e05daaeSHans Petter Selasky return (0); 11554076dd23SAndrew Thompson 11564076dd23SAndrew Thompson rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4); 11574076dd23SAndrew Thompson 11584076dd23SAndrew Thompson USETW(sc->sc_ncm.dpt.wLength, rem); 11594076dd23SAndrew Thompson 11604076dd23SAndrew Thompson /* zero the rest of the data pointer entries */ 11614076dd23SAndrew Thompson for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) { 11624076dd23SAndrew Thompson USETW(sc->sc_ncm.dp[n].wFrameLength, 0); 11634076dd23SAndrew Thompson USETW(sc->sc_ncm.dp[n].wFrameIndex, 0); 11644076dd23SAndrew Thompson } 11654076dd23SAndrew Thompson 11667e05daaeSHans Petter Selasky offset = last_offset; 11677e05daaeSHans Petter Selasky 11687e05daaeSHans Petter Selasky /* Align offset */ 11697e05daaeSHans Petter Selasky offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN); 11707e05daaeSHans Petter Selasky 11717e05daaeSHans Petter Selasky /* Optimise, save bandwidth and force short termination */ 11727e05daaeSHans Petter Selasky if (offset >= sc->sc_ncm.tx_max) 11737e05daaeSHans Petter Selasky offset = sc->sc_ncm.tx_max; 11747e05daaeSHans Petter Selasky else 11757e05daaeSHans Petter Selasky offset ++; 11767e05daaeSHans Petter Selasky 11777e05daaeSHans Petter Selasky /* Zero pad */ 11787e05daaeSHans Petter Selasky cdce_ncm_tx_zero(pc, last_offset, offset); 11797e05daaeSHans Petter Selasky 11804076dd23SAndrew Thompson /* set frame length */ 11817e05daaeSHans Petter Selasky usbd_xfer_set_frame_len(xfer, index, offset); 11824076dd23SAndrew Thompson 11834076dd23SAndrew Thompson /* Fill out 16-bit header */ 11844076dd23SAndrew Thompson sc->sc_ncm.hdr.dwSignature[0] = 'N'; 11854076dd23SAndrew Thompson sc->sc_ncm.hdr.dwSignature[1] = 'C'; 11864076dd23SAndrew Thompson sc->sc_ncm.hdr.dwSignature[2] = 'M'; 11874076dd23SAndrew Thompson sc->sc_ncm.hdr.dwSignature[3] = 'H'; 11884076dd23SAndrew Thompson USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr)); 11897e05daaeSHans Petter Selasky USETW(sc->sc_ncm.hdr.wBlockLength, offset); 11904076dd23SAndrew Thompson USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq); 11914076dd23SAndrew Thompson USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr)); 11924076dd23SAndrew Thompson 11934076dd23SAndrew Thompson sc->sc_ncm.tx_seq++; 11944076dd23SAndrew Thompson 11954076dd23SAndrew Thompson /* Fill out 16-bit frame table header */ 11964076dd23SAndrew Thompson sc->sc_ncm.dpt.dwSignature[0] = 'N'; 11974076dd23SAndrew Thompson sc->sc_ncm.dpt.dwSignature[1] = 'C'; 11984076dd23SAndrew Thompson sc->sc_ncm.dpt.dwSignature[2] = 'M'; 119985e3d588SAndrew Thompson sc->sc_ncm.dpt.dwSignature[3] = '0'; 12004076dd23SAndrew Thompson USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0); /* reserved */ 12014076dd23SAndrew Thompson 12024076dd23SAndrew Thompson usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr)); 12034076dd23SAndrew Thompson usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt), 12044076dd23SAndrew Thompson sizeof(sc->sc_ncm.dpt)); 12054076dd23SAndrew Thompson usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt), 12064076dd23SAndrew Thompson &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp)); 12077e05daaeSHans Petter Selasky return (retval); 12084076dd23SAndrew Thompson } 12094076dd23SAndrew Thompson 12104076dd23SAndrew Thompson static void 12114076dd23SAndrew Thompson cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 12124076dd23SAndrew Thompson { 12134076dd23SAndrew Thompson struct cdce_softc *sc = usbd_xfer_softc(xfer); 12144076dd23SAndrew Thompson struct ifnet *ifp = uether_getifp(&sc->sc_ue); 12154076dd23SAndrew Thompson uint16_t x; 12167e05daaeSHans Petter Selasky uint8_t temp; 12174076dd23SAndrew Thompson int actlen; 12184076dd23SAndrew Thompson int aframes; 12194076dd23SAndrew Thompson 12204076dd23SAndrew Thompson switch (USB_GET_STATE(xfer)) { 12214076dd23SAndrew Thompson case USB_ST_TRANSFERRED: 12224076dd23SAndrew Thompson 12234076dd23SAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 12244076dd23SAndrew Thompson 12254076dd23SAndrew Thompson DPRINTFN(10, "transfer complete: " 12264076dd23SAndrew Thompson "%u bytes in %u frames\n", actlen, aframes); 12274076dd23SAndrew Thompson 12284076dd23SAndrew Thompson case USB_ST_SETUP: 12294076dd23SAndrew Thompson for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) { 12307e05daaeSHans Petter Selasky temp = cdce_ncm_fill_tx_frames(xfer, x); 12317e05daaeSHans Petter Selasky if (temp == 0) 12324076dd23SAndrew Thompson break; 12337e05daaeSHans Petter Selasky if (temp == 1) { 12347e05daaeSHans Petter Selasky x++; 12357e05daaeSHans Petter Selasky break; 12367e05daaeSHans Petter Selasky } 12374076dd23SAndrew Thompson } 12384076dd23SAndrew Thompson 12394076dd23SAndrew Thompson if (x != 0) { 12407e05daaeSHans Petter Selasky #ifdef USB_DEBUG 12417e05daaeSHans Petter Selasky usbd_xfer_set_interval(xfer, cdce_tx_interval); 12427e05daaeSHans Petter Selasky #endif 12434076dd23SAndrew Thompson usbd_xfer_set_frames(xfer, x); 12444076dd23SAndrew Thompson usbd_transfer_submit(xfer); 12454076dd23SAndrew Thompson } 12464076dd23SAndrew Thompson break; 12474076dd23SAndrew Thompson 12484076dd23SAndrew Thompson default: /* Error */ 12494076dd23SAndrew Thompson DPRINTFN(10, "Transfer error: %s\n", 12504076dd23SAndrew Thompson usbd_errstr(error)); 12514076dd23SAndrew Thompson 12524076dd23SAndrew Thompson /* update error counter */ 12534076dd23SAndrew Thompson ifp->if_oerrors += 1; 12544076dd23SAndrew Thompson 12554076dd23SAndrew Thompson if (error != USB_ERR_CANCELLED) { 12564076dd23SAndrew Thompson /* try to clear stall first */ 12574076dd23SAndrew Thompson usbd_xfer_set_stall(xfer); 12584076dd23SAndrew Thompson usbd_xfer_set_frames(xfer, 0); 12594076dd23SAndrew Thompson usbd_transfer_submit(xfer); 12604076dd23SAndrew Thompson } 12614076dd23SAndrew Thompson break; 12624076dd23SAndrew Thompson } 12634076dd23SAndrew Thompson } 12644076dd23SAndrew Thompson 12654076dd23SAndrew Thompson static void 12664076dd23SAndrew Thompson cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 12674076dd23SAndrew Thompson { 12684076dd23SAndrew Thompson struct cdce_softc *sc = usbd_xfer_softc(xfer); 12694076dd23SAndrew Thompson struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0); 12704076dd23SAndrew Thompson struct ifnet *ifp = uether_getifp(&sc->sc_ue); 12714076dd23SAndrew Thompson struct mbuf *m; 12724076dd23SAndrew Thompson int sumdata; 12734076dd23SAndrew Thompson int sumlen; 12744076dd23SAndrew Thompson int actlen; 12754076dd23SAndrew Thompson int aframes; 12764076dd23SAndrew Thompson int temp; 12774076dd23SAndrew Thompson int nframes; 12784076dd23SAndrew Thompson int x; 12794076dd23SAndrew Thompson int offset; 12804076dd23SAndrew Thompson 12814076dd23SAndrew Thompson switch (USB_GET_STATE(xfer)) { 12824076dd23SAndrew Thompson case USB_ST_TRANSFERRED: 12834076dd23SAndrew Thompson 12844076dd23SAndrew Thompson usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL); 12854076dd23SAndrew Thompson 12864076dd23SAndrew Thompson DPRINTFN(1, "received %u bytes in %u frames\n", 12874076dd23SAndrew Thompson actlen, aframes); 12884076dd23SAndrew Thompson 12896d917491SHans Petter Selasky if (actlen < (int)(sizeof(sc->sc_ncm.hdr) + 12904076dd23SAndrew Thompson sizeof(sc->sc_ncm.dpt))) { 12914076dd23SAndrew Thompson DPRINTFN(1, "frame too short\n"); 129285e3d588SAndrew Thompson goto tr_setup; 12934076dd23SAndrew Thompson } 12944076dd23SAndrew Thompson usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr), 12954076dd23SAndrew Thompson sizeof(sc->sc_ncm.hdr)); 12964076dd23SAndrew Thompson 12974076dd23SAndrew Thompson if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') || 12984076dd23SAndrew Thompson (sc->sc_ncm.hdr.dwSignature[1] != 'C') || 12994076dd23SAndrew Thompson (sc->sc_ncm.hdr.dwSignature[2] != 'M') || 13004076dd23SAndrew Thompson (sc->sc_ncm.hdr.dwSignature[3] != 'H')) { 130185e3d588SAndrew Thompson DPRINTFN(1, "invalid HDR signature: " 130285e3d588SAndrew Thompson "0x%02x:0x%02x:0x%02x:0x%02x\n", 130385e3d588SAndrew Thompson sc->sc_ncm.hdr.dwSignature[0], 130485e3d588SAndrew Thompson sc->sc_ncm.hdr.dwSignature[1], 130585e3d588SAndrew Thompson sc->sc_ncm.hdr.dwSignature[2], 130685e3d588SAndrew Thompson sc->sc_ncm.hdr.dwSignature[3]); 13074076dd23SAndrew Thompson goto tr_stall; 13084076dd23SAndrew Thompson } 13094076dd23SAndrew Thompson temp = UGETW(sc->sc_ncm.hdr.wBlockLength); 13104076dd23SAndrew Thompson if (temp > sumlen) { 13114076dd23SAndrew Thompson DPRINTFN(1, "unsupported block length %u/%u\n", 13124076dd23SAndrew Thompson temp, sumlen); 13134076dd23SAndrew Thompson goto tr_stall; 13144076dd23SAndrew Thompson } 13154076dd23SAndrew Thompson temp = UGETW(sc->sc_ncm.hdr.wDptIndex); 13166d917491SHans Petter Selasky if ((int)(temp + sizeof(sc->sc_ncm.dpt)) > actlen) { 131785e3d588SAndrew Thompson DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp); 13184076dd23SAndrew Thompson goto tr_stall; 13194076dd23SAndrew Thompson } 13204076dd23SAndrew Thompson usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt), 13214076dd23SAndrew Thompson sizeof(sc->sc_ncm.dpt)); 13224076dd23SAndrew Thompson 13234076dd23SAndrew Thompson if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') || 13244076dd23SAndrew Thompson (sc->sc_ncm.dpt.dwSignature[1] != 'C') || 13254076dd23SAndrew Thompson (sc->sc_ncm.dpt.dwSignature[2] != 'M') || 132685e3d588SAndrew Thompson (sc->sc_ncm.dpt.dwSignature[3] != '0')) { 132785e3d588SAndrew Thompson DPRINTFN(1, "invalid DPT signature" 132885e3d588SAndrew Thompson "0x%02x:0x%02x:0x%02x:0x%02x\n", 132985e3d588SAndrew Thompson sc->sc_ncm.dpt.dwSignature[0], 133085e3d588SAndrew Thompson sc->sc_ncm.dpt.dwSignature[1], 133185e3d588SAndrew Thompson sc->sc_ncm.dpt.dwSignature[2], 133285e3d588SAndrew Thompson sc->sc_ncm.dpt.dwSignature[3]); 13334076dd23SAndrew Thompson goto tr_stall; 13344076dd23SAndrew Thompson } 13354076dd23SAndrew Thompson nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4; 13364076dd23SAndrew Thompson 13374076dd23SAndrew Thompson /* Subtract size of header and last zero padded entry */ 13384076dd23SAndrew Thompson if (nframes >= (2 + 1)) 13394076dd23SAndrew Thompson nframes -= (2 + 1); 13404076dd23SAndrew Thompson else 13414076dd23SAndrew Thompson nframes = 0; 13424076dd23SAndrew Thompson 13434076dd23SAndrew Thompson DPRINTFN(1, "nframes = %u\n", nframes); 13444076dd23SAndrew Thompson 13454076dd23SAndrew Thompson temp += sizeof(sc->sc_ncm.dpt); 13464076dd23SAndrew Thompson 13474076dd23SAndrew Thompson if ((temp + (4 * nframes)) > actlen) 13484076dd23SAndrew Thompson goto tr_stall; 13494076dd23SAndrew Thompson 13504076dd23SAndrew Thompson if (nframes > CDCE_NCM_SUBFRAMES_MAX) { 13514076dd23SAndrew Thompson DPRINTFN(1, "Truncating number of frames from %u to %u\n", 13524076dd23SAndrew Thompson nframes, CDCE_NCM_SUBFRAMES_MAX); 13534076dd23SAndrew Thompson nframes = CDCE_NCM_SUBFRAMES_MAX; 13544076dd23SAndrew Thompson } 13554076dd23SAndrew Thompson usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes)); 13564076dd23SAndrew Thompson 13574076dd23SAndrew Thompson sumdata = 0; 13584076dd23SAndrew Thompson 13594076dd23SAndrew Thompson for (x = 0; x != nframes; x++) { 13604076dd23SAndrew Thompson 13614076dd23SAndrew Thompson offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex); 13624076dd23SAndrew Thompson temp = UGETW(sc->sc_ncm.dp[x].wFrameLength); 13634076dd23SAndrew Thompson 136429051223SAndrew Thompson if ((offset == 0) || 13656d917491SHans Petter Selasky (temp < (int)sizeof(struct ether_header)) || 136629051223SAndrew Thompson (temp > (MCLBYTES - ETHER_ALIGN))) { 136729051223SAndrew Thompson DPRINTFN(1, "NULL frame detected at %d\n", x); 13684076dd23SAndrew Thompson m = NULL; 136929051223SAndrew Thompson /* silently ignore this frame */ 13704076dd23SAndrew Thompson continue; 137129051223SAndrew Thompson } else if ((offset + temp) > actlen) { 137229051223SAndrew Thompson DPRINTFN(1, "invalid frame " 137329051223SAndrew Thompson "detected at %d\n", x); 137429051223SAndrew Thompson m = NULL; 137529051223SAndrew Thompson /* silently ignore this frame */ 137629051223SAndrew Thompson continue; 13776d917491SHans Petter Selasky } else if (temp > (int)(MHLEN - ETHER_ALIGN)) { 1378c6499eccSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 13794076dd23SAndrew Thompson } else { 1380c6499eccSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 13814076dd23SAndrew Thompson } 13824076dd23SAndrew Thompson 13834076dd23SAndrew Thompson DPRINTFN(16, "frame %u, offset = %u, length = %u \n", 13844076dd23SAndrew Thompson x, offset, temp); 13854076dd23SAndrew Thompson 13864076dd23SAndrew Thompson /* check if we have a buffer */ 13874076dd23SAndrew Thompson if (m) { 13884076dd23SAndrew Thompson m_adj(m, ETHER_ALIGN); 13894076dd23SAndrew Thompson 13904076dd23SAndrew Thompson usbd_copy_out(pc, offset, m->m_data, temp); 13914076dd23SAndrew Thompson 13924076dd23SAndrew Thompson /* enqueue */ 13934076dd23SAndrew Thompson uether_rxmbuf(&sc->sc_ue, m, temp); 13944076dd23SAndrew Thompson 13954076dd23SAndrew Thompson sumdata += temp; 13964076dd23SAndrew Thompson } else { 13974076dd23SAndrew Thompson ifp->if_ierrors++; 13984076dd23SAndrew Thompson } 13994076dd23SAndrew Thompson } 14004076dd23SAndrew Thompson 14014076dd23SAndrew Thompson DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen); 14024076dd23SAndrew Thompson 14034076dd23SAndrew Thompson case USB_ST_SETUP: 140485e3d588SAndrew Thompson tr_setup: 14054076dd23SAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max); 14064076dd23SAndrew Thompson usbd_xfer_set_frames(xfer, 1); 14074076dd23SAndrew Thompson usbd_transfer_submit(xfer); 14084076dd23SAndrew Thompson uether_rxflush(&sc->sc_ue); /* must be last */ 14094076dd23SAndrew Thompson break; 14104076dd23SAndrew Thompson 14114076dd23SAndrew Thompson default: /* Error */ 14124076dd23SAndrew Thompson DPRINTFN(1, "error = %s\n", 14134076dd23SAndrew Thompson usbd_errstr(error)); 14144076dd23SAndrew Thompson 14154076dd23SAndrew Thompson if (error != USB_ERR_CANCELLED) { 14164076dd23SAndrew Thompson tr_stall: 14174076dd23SAndrew Thompson /* try to clear stall first */ 14184076dd23SAndrew Thompson usbd_xfer_set_stall(xfer); 14194076dd23SAndrew Thompson usbd_xfer_set_frames(xfer, 0); 14204076dd23SAndrew Thompson usbd_transfer_submit(xfer); 14214076dd23SAndrew Thompson } 14224076dd23SAndrew Thompson break; 14234076dd23SAndrew Thompson } 14244076dd23SAndrew Thompson } 14254076dd23SAndrew Thompson #endif 1426