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