Home | History | Annotate | Line # | Download | only in usb
if_cdce.c revision 1.38.14.12
      1 /*	$NetBSD: if_cdce.c,v 1.38.14.12 2017/02/05 13:40:46 skrll Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul (at) windriver.com>
      5  * Copyright (c) 2003 Craig Boston
      6  * Copyright (c) 2004 Daniel Hartmeier
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by Bill Paul.
     20  * 4. Neither the name of the author nor the names of any co-contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR
     28  * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     31  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     33  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     34  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 /*
     38  * USB Communication Device Class (Ethernet Networking Control Model)
     39  * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
     40  *
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.38.14.12 2017/02/05 13:40:46 skrll Exp $");
     45 
     46 #ifdef _KERNEL_OPT
     47 #include "opt_inet.h"
     48 #endif
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/sockio.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/kernel.h>
     55 #include <sys/socket.h>
     56 #include <sys/device.h>
     57 
     58 #include <net/if.h>
     59 #include <net/if_arp.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_media.h>
     62 
     63 #include <net/bpf.h>
     64 
     65 #include <net/if_ether.h>
     66 #ifdef INET
     67 #include <netinet/in.h>
     68 #include <netinet/if_inarp.h>
     69 #endif
     70 
     71 
     72 
     73 #include <dev/usb/usb.h>
     74 #include <dev/usb/usbdi.h>
     75 #include <dev/usb/usbdi_util.h>
     76 #include <dev/usb/usbdevs.h>
     77 #include <dev/usb/usbcdc.h>
     78 
     79 #include <dev/usb/if_cdcereg.h>
     80 
     81 Static int	 cdce_tx_list_init(struct cdce_softc *);
     82 Static void	 cdce_tx_list_free(struct cdce_softc *);
     83 Static int	 cdce_rx_list_init(struct cdce_softc *);
     84 Static void	 cdce_rx_list_free(struct cdce_softc *);
     85 Static int	 cdce_newbuf(struct cdce_softc *, struct cdce_chain *,
     86 		    struct mbuf *);
     87 Static int	 cdce_encap(struct cdce_softc *, struct mbuf *, int);
     88 Static void	 cdce_rxeof(struct usbd_xfer *, void *, usbd_status);
     89 Static void	 cdce_txeof(struct usbd_xfer *, void *, usbd_status);
     90 Static void	 cdce_start(struct ifnet *);
     91 Static void	 cdce_start_locked(struct ifnet *);
     92 Static int	 cdce_ioctl(struct ifnet *, u_long, void *);
     93 Static int	 cdce_init(struct ifnet *);
     94 Static int	 cdce_init_locked(struct ifnet *);
     95 Static void	 cdce_watchdog(struct ifnet *);
     96 Static void	 cdce_stop(struct ifnet *, int);
     97 Static void	 cdce_stop_locked(struct ifnet *, int);
     98 
     99 Static const struct cdce_type cdce_devs[] = {
    100   {{ USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632 }, CDCE_NO_UNION },
    101   {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX }, CDCE_NO_UNION },
    102   {{ USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00 }, CDCE_NO_UNION },
    103   {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN }, CDCE_ZAURUS | CDCE_NO_UNION },
    104   {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2 }, CDCE_ZAURUS | CDCE_NO_UNION },
    105   {{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501 }, CDCE_NO_UNION },
    106   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500 }, CDCE_ZAURUS },
    107   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_A300 }, CDCE_ZAURUS | CDCE_NO_UNION },
    108   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600 }, CDCE_ZAURUS | CDCE_NO_UNION },
    109   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C700 }, CDCE_ZAURUS | CDCE_NO_UNION },
    110   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C750 }, CDCE_ZAURUS | CDCE_NO_UNION },
    111 };
    112 #define cdce_lookup(v, p) \
    113 	((const struct cdce_type *)usb_lookup(cdce_devs, v, p))
    114 
    115 int cdce_match(device_t, cfdata_t, void *);
    116 void cdce_attach(device_t, device_t, void *);
    117 int cdce_detach(device_t, int);
    118 int cdce_activate(device_t, enum devact);
    119 extern struct cfdriver cdce_cd;
    120 CFATTACH_DECL_NEW(cdce, sizeof(struct cdce_softc), cdce_match, cdce_attach,
    121     cdce_detach, cdce_activate);
    122 
    123 int
    124 cdce_match(device_t parent, cfdata_t match, void *aux)
    125 {
    126 	struct usbif_attach_arg *uiaa = aux;
    127 
    128 	if (cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
    129 		return UMATCH_VENDOR_PRODUCT;
    130 
    131 	if (uiaa->uiaa_class == UICLASS_CDC && uiaa->uiaa_subclass ==
    132 	    UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL)
    133 		return UMATCH_IFACECLASS_GENERIC;
    134 
    135 	return UMATCH_NONE;
    136 }
    137 
    138 void
    139 cdce_attach(device_t parent, device_t self, void *aux)
    140 {
    141 	struct cdce_softc *sc = device_private(self);
    142 	struct usbif_attach_arg *uiaa = aux;
    143 	char				*devinfop;
    144 	struct ifnet			*ifp;
    145 	struct usbd_device	        *dev = uiaa->uiaa_device;
    146 	const struct cdce_type		*t;
    147 	usb_interface_descriptor_t	*id;
    148 	usb_endpoint_descriptor_t	*ed;
    149 	const usb_cdc_union_descriptor_t *ud;
    150 	usb_config_descriptor_t		*cd;
    151 	int				 data_ifcno;
    152 	int				 i, j, numalts;
    153 	u_char				 eaddr[ETHER_ADDR_LEN];
    154 	const usb_cdc_ethernet_descriptor_t *ue;
    155 	char				 eaddr_str[USB_MAX_ENCODED_STRING_LEN];
    156 
    157 	sc->cdce_dev = self;
    158 
    159 	aprint_naive("\n");
    160 	aprint_normal("\n");
    161 
    162 	devinfop = usbd_devinfo_alloc(dev, 0);
    163 	aprint_normal_dev(self, "%s\n", devinfop);
    164 	usbd_devinfo_free(devinfop);
    165 
    166 	mutex_init(&sc->cdce_lock, MUTEX_DEFAULT, IPL_NONE);
    167 	mutex_init(&sc->cdce_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    168 	mutex_init(&sc->cdce_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    169 
    170 	sc->cdce_udev = uiaa->uiaa_device;
    171 	sc->cdce_ctl_iface = uiaa->uiaa_iface;
    172 
    173 	t = cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
    174 	if (t)
    175 		sc->cdce_flags = t->cdce_flags;
    176 
    177 	if (sc->cdce_flags & CDCE_NO_UNION)
    178 		sc->cdce_data_iface = sc->cdce_ctl_iface;
    179 	else {
    180 		ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(sc->cdce_udev,
    181 		    UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION);
    182 		if (ud == NULL) {
    183 			aprint_error_dev(self, "no union descriptor\n");
    184 			return;
    185 		}
    186 		data_ifcno = ud->bSlaveInterface[0];
    187 
    188 		for (i = 0; i < uiaa->uiaa_nifaces; i++) {
    189 			if (uiaa->uiaa_ifaces[i] != NULL) {
    190 				id = usbd_get_interface_descriptor(
    191 				    uiaa->uiaa_ifaces[i]);
    192 				if (id != NULL && id->bInterfaceNumber ==
    193 				    data_ifcno) {
    194 					sc->cdce_data_iface = uiaa->uiaa_ifaces[i];
    195 					uiaa->uiaa_ifaces[i] = NULL;
    196 				}
    197 			}
    198 		}
    199 	}
    200 
    201 	if (sc->cdce_data_iface == NULL) {
    202 		aprint_error_dev(self, "no data interface\n");
    203 		return;
    204 	}
    205 
    206 	/*
    207 	 * <quote>
    208 	 *  The Data Class interface of a networking device shall have a minimum
    209 	 *  of two interface settings. The first setting (the default interface
    210 	 *  setting) includes no endpoints and therefore no networking traffic is
    211 	 *  exchanged whenever the default interface setting is selected. One or
    212 	 *  more additional interface settings are used for normal operation, and
    213 	 *  therefore each includes a pair of endpoints (one IN, and one OUT) to
    214 	 *  exchange network traffic. Select an alternate interface setting to
    215 	 *  initialize the network aspects of the device and to enable the
    216 	 *  exchange of network traffic.
    217 	 * </quote>
    218 	 *
    219 	 * Some devices, most notably cable modems, include interface settings
    220 	 * that have no IN or OUT endpoint, therefore loop through the list of all
    221 	 * available interface settings looking for one with both IN and OUT
    222 	 * endpoints.
    223 	 */
    224 	id = usbd_get_interface_descriptor(sc->cdce_data_iface);
    225 	cd = usbd_get_config_descriptor(sc->cdce_udev);
    226 	numalts = usbd_get_no_alts(cd, id->bInterfaceNumber);
    227 
    228 	for (j = 0; j < numalts; j++) {
    229 		if (usbd_set_interface(sc->cdce_data_iface, j)) {
    230 			aprint_error_dev(sc->cdce_dev,
    231 					"setting alternate interface failed\n");
    232 			return;
    233 		}
    234 		/* Find endpoints. */
    235 		id = usbd_get_interface_descriptor(sc->cdce_data_iface);
    236 		sc->cdce_bulkin_no = sc->cdce_bulkout_no = -1;
    237 		for (i = 0; i < id->bNumEndpoints; i++) {
    238 			ed = usbd_interface2endpoint_descriptor(sc->cdce_data_iface, i);
    239 			if (!ed) {
    240 				aprint_error_dev(self,
    241 						"could not read endpoint descriptor\n");
    242 				return;
    243 			}
    244 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    245 					UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    246 				sc->cdce_bulkin_no = ed->bEndpointAddress;
    247 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    248 					UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    249 				sc->cdce_bulkout_no = ed->bEndpointAddress;
    250 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    251 					UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    252 				/* XXX: CDC spec defines an interrupt pipe, but it is not
    253 				 * needed for simple host-to-host applications. */
    254 			} else {
    255 				aprint_error_dev(self, "unexpected endpoint\n");
    256 			}
    257 		}
    258 		/* If we found something, try and use it... */
    259 		if ((sc->cdce_bulkin_no != -1) && (sc->cdce_bulkout_no != -1))
    260 			break;
    261 	}
    262 
    263 	if (sc->cdce_bulkin_no == -1) {
    264 		aprint_error_dev(self, "could not find data bulk in\n");
    265 		return;
    266 	}
    267 	if (sc->cdce_bulkout_no == -1 ) {
    268 		aprint_error_dev(self, "could not find data bulk out\n");
    269 		return;
    270 	}
    271 
    272 	ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc(dev,
    273 	    UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF);
    274 	if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) ||
    275 	    ether_aton_r(eaddr, sizeof(eaddr), eaddr_str)) {
    276 		aprint_normal_dev(self, "faking address\n");
    277 		eaddr[0]= 0x2a;
    278 		memcpy(&eaddr[1], &hardclock_ticks, sizeof(uint32_t));
    279 		eaddr[5] = (uint8_t)(device_unit(sc->cdce_dev));
    280 	}
    281 
    282 	aprint_normal_dev(self, "address %s\n", ether_sprintf(eaddr));
    283 
    284 	ifp = GET_IFP(sc);
    285 	ifp->if_softc = sc;
    286 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    287 	ifp->if_extflags = IFEF_START_MPSAFE;
    288 	ifp->if_init = cdce_init;
    289 	ifp->if_ioctl = cdce_ioctl;
    290 	ifp->if_start = cdce_start;
    291 	ifp->if_watchdog = cdce_watchdog;
    292 	strlcpy(ifp->if_xname, device_xname(sc->cdce_dev), IFNAMSIZ);
    293 
    294 	IFQ_SET_READY(&ifp->if_snd);
    295 
    296 	if_initialize(ifp);
    297 	sc->cdce_ipq = if_percpuq_create(&sc->cdce_ec.ec_if);
    298 	ether_ifattach(ifp, eaddr);
    299 	if_register(ifp);
    300 
    301 	sc->cdce_attached = 1;
    302 
    303 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cdce_udev,
    304 	    sc->cdce_dev);
    305 
    306 	if (!pmf_device_register(self, NULL, NULL))
    307 		aprint_error_dev(self, "couldn't establish power handler\n");
    308 
    309 	return;
    310 }
    311 
    312 int
    313 cdce_detach(device_t self, int flags)
    314 {
    315 	struct cdce_softc *sc = device_private(self);
    316 	struct ifnet	*ifp = GET_IFP(sc);
    317 	int		 s;
    318 
    319 	pmf_device_deregister(self);
    320 
    321 	s = splusb();
    322 
    323 	if (!sc->cdce_attached) {
    324 		splx(s);
    325 		return 0;
    326 	}
    327 
    328 	if (ifp->if_flags & IFF_RUNNING)
    329 		cdce_stop(ifp, 1);
    330 
    331 	ether_ifdetach(ifp);
    332 
    333 	if_detach(ifp);
    334 
    335 	mutex_destroy(&sc->cdce_rxlock);
    336 	mutex_destroy(&sc->cdce_txlock);
    337 	mutex_destroy(&sc->cdce_lock);
    338 
    339 	sc->cdce_attached = 0;
    340 	splx(s);
    341 
    342 	return 0;
    343 }
    344 
    345 Static void
    346 cdce_start(struct ifnet *ifp)
    347 {
    348 	struct cdce_softc	*sc = ifp->if_softc;
    349 	KASSERT(ifp->if_extflags & IFEF_START_MPSAFE);
    350 
    351 	mutex_enter(&sc->cdce_txlock);
    352 	cdce_start_locked(ifp);
    353 	mutex_exit(&sc->cdce_txlock);
    354 }
    355 
    356 Static void
    357 cdce_start_locked(struct ifnet *ifp)
    358 {
    359 	struct cdce_softc	*sc = ifp->if_softc;
    360 	struct mbuf		*m_head = NULL;
    361 
    362 	if (sc->cdce_dying || (ifp->if_flags & IFF_OACTIVE))
    363 		return;
    364 
    365 	IFQ_POLL(&ifp->if_snd, m_head);
    366 	if (m_head == NULL)
    367 		return;
    368 
    369 	if (cdce_encap(sc, m_head, 0)) {
    370 		return;
    371 	}
    372 
    373 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    374 
    375 	bpf_mtap(ifp, m_head);
    376 
    377 	ifp->if_flags |= IFF_OACTIVE;
    378 
    379 	ifp->if_timer = 6;
    380 }
    381 
    382 Static int
    383 cdce_encap(struct cdce_softc *sc, struct mbuf *m, int idx)
    384 {
    385 	struct cdce_chain	*c = &sc->cdce_cdata.cdce_tx_chain[idx];
    386 	int			 extra = 0;
    387 
    388 	m_copydata(m, 0, m->m_pkthdr.len, c->cdce_buf);
    389 	if (sc->cdce_flags & CDCE_ZAURUS) {
    390 		/* Zaurus wants a 32-bit CRC appended to every frame */
    391 		uint32_t crc;
    392 
    393 		crc = htole32(~ether_crc32_le(c->cdce_buf, m->m_pkthdr.len));
    394 		memcpy(c->cdce_buf + m->m_pkthdr.len, &crc, sizeof(crc));
    395 		extra = sizeof(crc);
    396 	}
    397 	c->cdce_mbuf = m;
    398 
    399 	usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, m->m_pkthdr.len + extra,
    400 	    USBD_FORCE_SHORT_XFER, 10000, cdce_txeof);
    401 	usbd_status err = usbd_transfer(c->cdce_xfer);
    402 	if (err != USBD_IN_PROGRESS) {
    403 		cdce_stop(GET_IFP(sc), 0);
    404 		return EIO;
    405 	}
    406 
    407 	sc->cdce_cdata.cdce_tx_cnt++;
    408 
    409 	return 0;
    410 }
    411 
    412 Static void
    413 cdce_stop(struct ifnet *ifp, int disable)
    414 {
    415 	struct cdce_softc * const sc = ifp->if_softc;
    416 
    417 	mutex_enter(&sc->cdce_lock);
    418 	cdce_stop_locked(ifp, disable);
    419 	mutex_exit(&sc->cdce_lock);
    420 }
    421 
    422 Static void
    423 cdce_stop_locked(struct ifnet *ifp, int disable)
    424 {
    425 	struct cdce_softc * const sc = ifp->if_softc;
    426 	usbd_status	 err;
    427 
    428 	ifp->if_timer = 0;
    429 
    430 	if (sc->cdce_bulkin_pipe != NULL) {
    431 		err = usbd_abort_pipe(sc->cdce_bulkin_pipe);
    432 		if (err)
    433 			printf("%s: abort rx pipe failed: %s\n",
    434 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    435 	}
    436 
    437 	if (sc->cdce_bulkout_pipe != NULL) {
    438 		err = usbd_abort_pipe(sc->cdce_bulkout_pipe);
    439 		if (err)
    440 			printf("%s: abort tx pipe failed: %s\n",
    441 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    442 	}
    443 
    444 	cdce_rx_list_free(sc);
    445 
    446 	cdce_tx_list_free(sc);
    447 
    448 	if (sc->cdce_bulkin_pipe != NULL) {
    449 		err = usbd_close_pipe(sc->cdce_bulkin_pipe);
    450 		if (err)
    451 			printf("%s: close rx pipe failed: %s\n",
    452 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    453 		sc->cdce_bulkin_pipe = NULL;
    454 	}
    455 
    456 	if (sc->cdce_bulkout_pipe != NULL) {
    457 		err = usbd_close_pipe(sc->cdce_bulkout_pipe);
    458 		if (err)
    459 			printf("%s: close tx pipe failed: %s\n",
    460 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    461 		sc->cdce_bulkout_pipe = NULL;
    462 	}
    463 
    464 	ifp->if_timer = 0;
    465 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    466 }
    467 
    468 Static int
    469 cdce_ioctl(struct ifnet *ifp, u_long command, void *data)
    470 {
    471 	struct cdce_softc	*sc = ifp->if_softc;
    472 	int			 s, error = 0;
    473 
    474 	if (sc->cdce_dying)
    475 		return EIO;
    476 
    477 	s = splnet();
    478 	error = ether_ioctl(ifp, command, data);
    479 	splx(s);
    480 
    481 	if (error == ENETRESET)
    482 		error = 0;
    483 
    484 	return error;
    485 }
    486 
    487 Static void
    488 cdce_watchdog(struct ifnet *ifp)
    489 {
    490 	struct cdce_softc	*sc = ifp->if_softc;
    491 
    492 	if (sc->cdce_dying)
    493 		return;
    494 
    495 	ifp->if_oerrors++;
    496 	printf("%s: watchdog timeout\n", device_xname(sc->cdce_dev));
    497 }
    498 
    499 Static int
    500 cdce_init(struct ifnet *ifp)
    501 {
    502 	struct cdce_softc * const sc = ifp->if_softc;
    503 
    504 	mutex_enter(&sc->cdce_lock);
    505 	int ret = cdce_init_locked(ifp);
    506 	mutex_exit(&sc->cdce_lock);
    507 
    508 	return ret;
    509 }
    510 
    511 Static int
    512 cdce_init_locked(struct ifnet *ifp)
    513 {
    514 	struct cdce_softc * const sc = ifp->if_softc;
    515 	struct cdce_chain	*c;
    516 	usbd_status		 err;
    517 	int			 i;
    518 
    519 	err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkin_no,
    520 	    USBD_EXCLUSIVE_USE, &sc->cdce_bulkin_pipe);
    521 	if (err) {
    522 		printf("%s: open rx pipe failed: %s\n", device_xname(sc->cdce_dev),
    523 		    usbd_errstr(err));
    524 		goto fail;
    525 	}
    526 
    527 	err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkout_no,
    528 	    USBD_EXCLUSIVE_USE, &sc->cdce_bulkout_pipe);
    529 	if (err) {
    530 		printf("%s: open tx pipe failed: %s\n",
    531 		    device_xname(sc->cdce_dev), usbd_errstr(err));
    532 		goto fail1;
    533 	}
    534 
    535 	if (cdce_tx_list_init(sc)) {
    536 		printf("%s: tx list init failed\n", device_xname(sc->cdce_dev));
    537 		goto fail2;
    538 	}
    539 
    540 	if (cdce_rx_list_init(sc)) {
    541 		printf("%s: rx list init failed\n", device_xname(sc->cdce_dev));
    542 		goto fail3;
    543 	}
    544 
    545 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
    546 		c = &sc->cdce_cdata.cdce_rx_chain[i];
    547 
    548 		usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, CDCE_BUFSZ,
    549 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cdce_rxeof);
    550 		usbd_transfer(c->cdce_xfer);
    551 	}
    552 
    553 	ifp->if_flags |= IFF_RUNNING;
    554 	ifp->if_flags &= ~IFF_OACTIVE;
    555 
    556 	return 0;
    557 
    558 fail3:
    559 	cdce_tx_list_free(sc);
    560 fail2:
    561 	usbd_close_pipe(sc->cdce_bulkout_pipe);
    562 fail1:
    563 	usbd_close_pipe(sc->cdce_bulkin_pipe);
    564 fail:
    565 	return EIO;
    566 }
    567 
    568 Static int
    569 cdce_newbuf(struct cdce_softc *sc, struct cdce_chain *c, struct mbuf *m)
    570 {
    571 	struct mbuf	*m_new = NULL;
    572 
    573 	if (m == NULL) {
    574 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    575 		if (m_new == NULL) {
    576 			printf("%s: no memory for rx list "
    577 			    "-- packet dropped!\n", device_xname(sc->cdce_dev));
    578 			return ENOBUFS;
    579 		}
    580 		MCLGET(m_new, M_DONTWAIT);
    581 		if (!(m_new->m_flags & M_EXT)) {
    582 			printf("%s: no memory for rx list "
    583 			    "-- packet dropped!\n", device_xname(sc->cdce_dev));
    584 			m_freem(m_new);
    585 			return ENOBUFS;
    586 		}
    587 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    588 	} else {
    589 		m_new = m;
    590 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    591 		m_new->m_data = m_new->m_ext.ext_buf;
    592 	}
    593 	c->cdce_mbuf = m_new;
    594 	return 0;
    595 }
    596 
    597 Static int
    598 cdce_rx_list_init(struct cdce_softc *sc)
    599 {
    600 	struct cdce_cdata	*cd = &sc->cdce_cdata;
    601 	struct cdce_chain	*c;
    602 	int			 i;
    603 
    604 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
    605 		c = &cd->cdce_rx_chain[i];
    606 		c->cdce_sc = sc;
    607 		c->cdce_idx = i;
    608 		if (cdce_newbuf(sc, c, NULL) == ENOBUFS)
    609 			return ENOBUFS;
    610 		if (c->cdce_xfer == NULL) {
    611 			int err = usbd_create_xfer(sc->cdce_bulkin_pipe,
    612 			    CDCE_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->cdce_xfer);
    613 			if (err)
    614 				return err;
    615 			c->cdce_buf = usbd_get_buffer(c->cdce_xfer);
    616 		}
    617 	}
    618 
    619 	return 0;
    620 }
    621 
    622 Static void
    623 cdce_rx_list_free(struct cdce_softc *sc)
    624 {
    625 	/* Free RX resources */
    626 	for (size_t i = 0; i < CDCE_RX_LIST_CNT; i++) {
    627 		if (sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf != NULL) {
    628 			m_freem(sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf);
    629 			sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf = NULL;
    630 		}
    631 		if (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer != NULL) {
    632 			usbd_destroy_xfer(sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer);
    633 			sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer = NULL;
    634 		}
    635 	}
    636 }
    637 
    638 Static int
    639 cdce_tx_list_init(struct cdce_softc *sc)
    640 {
    641 	struct cdce_cdata	*cd = &sc->cdce_cdata;
    642 	struct cdce_chain	*c;
    643 	int			 i;
    644 
    645 	for (i = 0; i < CDCE_TX_LIST_CNT; i++) {
    646 		c = &cd->cdce_tx_chain[i];
    647 		c->cdce_sc = sc;
    648 		c->cdce_idx = i;
    649 		c->cdce_mbuf = NULL;
    650 		if (c->cdce_xfer == NULL) {
    651 			int err = usbd_create_xfer(sc->cdce_bulkout_pipe,
    652 			    CDCE_BUFSZ, USBD_FORCE_SHORT_XFER, 0,
    653 			    &c->cdce_xfer);
    654 			if (err)
    655 				return err;
    656 			c->cdce_buf = usbd_get_buffer(c->cdce_xfer);
    657 		}
    658 	}
    659 
    660 	return 0;
    661 }
    662 
    663 Static void
    664 cdce_tx_list_free(struct cdce_softc *sc)
    665 {
    666 	/* Free TX resources */
    667 	for (size_t i = 0; i < CDCE_TX_LIST_CNT; i++) {
    668 		if (sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf != NULL) {
    669 			m_freem(sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf);
    670 			sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf = NULL;
    671 		}
    672 		if (sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer != NULL) {
    673 			usbd_destroy_xfer(sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer);
    674 			sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer = NULL;
    675 		}
    676 	}
    677 }
    678 
    679 Static void
    680 cdce_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    681 {
    682 	struct cdce_chain	*c = priv;
    683 	struct cdce_softc	*sc = c->cdce_sc;
    684 	struct ifnet		*ifp = GET_IFP(sc);
    685 	struct mbuf		*m;
    686 	int			 total_len = 0;
    687 
    688 	if (sc->cdce_dying || !(ifp->if_flags & IFF_RUNNING))
    689 		return;
    690 
    691 	if (status != USBD_NORMAL_COMPLETION) {
    692 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    693 			return;
    694 		if (sc->cdce_rxeof_errors == 0)
    695 			printf("%s: usb error on rx: %s\n",
    696 			    device_xname(sc->cdce_dev), usbd_errstr(status));
    697 		if (status == USBD_STALLED)
    698 			usbd_clear_endpoint_stall_async(sc->cdce_bulkin_pipe);
    699 		DELAY(sc->cdce_rxeof_errors * 10000);
    700 		sc->cdce_rxeof_errors++;
    701 		goto done;
    702 	}
    703 
    704 	sc->cdce_rxeof_errors = 0;
    705 
    706 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    707 	if (sc->cdce_flags & CDCE_ZAURUS)
    708 		total_len -= 4;	/* Strip off CRC added by Zaurus */
    709 	if (total_len <= 1)
    710 		goto done;
    711 
    712 	m = c->cdce_mbuf;
    713 	memcpy(mtod(m, char *), c->cdce_buf, total_len);
    714 
    715 	if (total_len < sizeof(struct ether_header)) {
    716 		ifp->if_ierrors++;
    717 		goto done;
    718 	}
    719 
    720 	m->m_pkthdr.len = m->m_len = total_len;
    721 	m_set_rcvif(m, ifp);
    722 
    723 	if (cdce_newbuf(sc, c, NULL) == ENOBUFS) {
    724 		ifp->if_ierrors++;
    725 		goto done;
    726 	}
    727 
    728 	if_percpuq_enqueue(sc->cdce_ipq, (m));
    729 
    730 done:
    731 	/* Setup new transfer. */
    732 	usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, CDCE_BUFSZ,
    733 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cdce_rxeof);
    734 	usbd_transfer(c->cdce_xfer);
    735 }
    736 
    737 Static void
    738 cdce_txeof(struct usbd_xfer *xfer, void *priv,
    739     usbd_status status)
    740 {
    741 	struct cdce_chain	*c = priv;
    742 	struct cdce_softc	*sc = c->cdce_sc;
    743 	struct ifnet		*ifp = GET_IFP(sc);
    744 	usbd_status		 err;
    745 
    746 	if (sc->cdce_dying)
    747 		return;
    748 
    749 	int s = splnet();
    750 
    751 	ifp->if_timer = 0;
    752 	ifp->if_flags &= ~IFF_OACTIVE;
    753 
    754 	if (status != USBD_NORMAL_COMPLETION) {
    755 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    756 			splx(s);
    757 			return;
    758 		}
    759 		ifp->if_oerrors++;
    760 		printf("%s: usb error on tx: %s\n", device_xname(sc->cdce_dev),
    761 		    usbd_errstr(status));
    762 		if (status == USBD_STALLED)
    763 			usbd_clear_endpoint_stall_async(sc->cdce_bulkout_pipe);
    764 		splx(s);
    765 		return;
    766 	}
    767 
    768 	usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &err);
    769 
    770 	if (c->cdce_mbuf != NULL) {
    771 		m_freem(c->cdce_mbuf);
    772 		c->cdce_mbuf = NULL;
    773 	}
    774 
    775 	if (err)
    776 		ifp->if_oerrors++;
    777 	else
    778 		ifp->if_opackets++;
    779 
    780 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    781 		cdce_start(ifp);
    782 
    783 	splx(s);
    784 }
    785 
    786 int
    787 cdce_activate(device_t self, enum devact act)
    788 {
    789 	struct cdce_softc *sc = device_private(self);
    790 
    791 	switch (act) {
    792 	case DVACT_DEACTIVATE:
    793 		if_deactivate(GET_IFP(sc));
    794 		sc->cdce_dying = 1;
    795 		return 0;
    796 	default:
    797 		return EOPNOTSUPP;
    798 	}
    799 }
    800