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