Home | History | Annotate | Line # | Download | only in usb
if_cdce.c revision 1.37
      1 /*	$NetBSD: if_cdce.c,v 1.37 2012/04/05 16:31:53 plunky 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.37 2012/04/05 16:31:53 plunky Exp $");
     45 
     46 #if defined(_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(usbd_xfer_handle, usbd_private_handle, usbd_status);
     89 Static void	 cdce_txeof(usbd_xfer_handle, usbd_private_handle, 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 *uaa = aux;
    124 
    125 	if (cdce_lookup(uaa->vendor, uaa->product) != NULL)
    126 		return (UMATCH_VENDOR_PRODUCT);
    127 
    128 	if (uaa->class == UICLASS_CDC && uaa->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 *uaa = aux;
    140 	char				*devinfop;
    141 	int				 s;
    142 	struct ifnet			*ifp;
    143 	usbd_device_handle		 dev = uaa->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 = uaa->device;
    165 	sc->cdce_ctl_iface = uaa->iface;
    166 
    167 	t = cdce_lookup(uaa->vendor, uaa->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 < uaa->nifaces; i++) {
    183 			if (uaa->ifaces[i] != NULL) {
    184 				id = usbd_get_interface_descriptor(
    185 				    uaa->ifaces[i]);
    186 				if (id != NULL && id->bInterfaceNumber ==
    187 				    data_ifcno) {
    188 					sc->cdce_data_iface = uaa->ifaces[i];
    189 					uaa->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, sc->cdce_bulkout_pipe, c, c->cdce_buf,
    382 	    m->m_pkthdr.len + extra, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
    383 	    10000, cdce_txeof);
    384 	err = usbd_transfer(c->cdce_xfer);
    385 	if (err != USBD_IN_PROGRESS) {
    386 		cdce_stop(sc);
    387 		return (EIO);
    388 	}
    389 
    390 	sc->cdce_cdata.cdce_tx_cnt++;
    391 
    392 	return (0);
    393 }
    394 
    395 Static void
    396 cdce_stop(struct cdce_softc *sc)
    397 {
    398 	usbd_status	 err;
    399 	struct ifnet	*ifp = GET_IFP(sc);
    400 	int		 i;
    401 
    402 	ifp->if_timer = 0;
    403 
    404 	if (sc->cdce_bulkin_pipe != NULL) {
    405 		err = usbd_abort_pipe(sc->cdce_bulkin_pipe);
    406 		if (err)
    407 			printf("%s: abort rx pipe failed: %s\n",
    408 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    409 		err = usbd_close_pipe(sc->cdce_bulkin_pipe);
    410 		if (err)
    411 			printf("%s: close rx pipe failed: %s\n",
    412 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    413 		sc->cdce_bulkin_pipe = NULL;
    414 	}
    415 
    416 	if (sc->cdce_bulkout_pipe != NULL) {
    417 		err = usbd_abort_pipe(sc->cdce_bulkout_pipe);
    418 		if (err)
    419 			printf("%s: abort tx pipe failed: %s\n",
    420 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    421 		err = usbd_close_pipe(sc->cdce_bulkout_pipe);
    422 		if (err)
    423 			printf("%s: close tx pipe failed: %s\n",
    424 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    425 		sc->cdce_bulkout_pipe = NULL;
    426 	}
    427 
    428 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
    429 		if (sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf != NULL) {
    430 			m_freem(sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf);
    431 			sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf = NULL;
    432 		}
    433 		if (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer != NULL) {
    434 			usbd_free_xfer
    435 			    (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer);
    436 			sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer = NULL;
    437 		}
    438 	}
    439 
    440 	for (i = 0; i < CDCE_TX_LIST_CNT; i++) {
    441 		if (sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf != NULL) {
    442 			m_freem(sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf);
    443 			sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf = NULL;
    444 		}
    445 		if (sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer != NULL) {
    446 			usbd_free_xfer(
    447 				sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer);
    448 			sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer = NULL;
    449 		}
    450 	}
    451 
    452 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    453 }
    454 
    455 Static int
    456 cdce_ioctl(struct ifnet *ifp, u_long command, void *data)
    457 {
    458 	struct cdce_softc	*sc = ifp->if_softc;
    459 	struct ifaddr		*ifa = (struct ifaddr *)data;
    460 	struct ifreq		*ifr = (struct ifreq *)data;
    461 	int			 s, error = 0;
    462 
    463 	if (sc->cdce_dying)
    464 		return (EIO);
    465 
    466 	s = splnet();
    467 
    468 	switch(command) {
    469 	case SIOCINITIFADDR:
    470 		ifp->if_flags |= IFF_UP;
    471 		cdce_init(sc);
    472 		switch (ifa->ifa_addr->sa_family) {
    473 #ifdef INET
    474 		case AF_INET:
    475 			arp_ifinit(ifp, ifa);
    476 			break;
    477 #endif /* INET */
    478 		}
    479 		break;
    480 
    481 	case SIOCSIFMTU:
    482 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
    483 			error = EINVAL;
    484 		else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
    485 			error = 0;
    486 		break;
    487 
    488 	case SIOCSIFFLAGS:
    489 		if ((error = ifioctl_common(ifp, command, data)) != 0)
    490 			break;
    491 		/* XXX re-use ether_ioctl() */
    492 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    493 		case IFF_UP:
    494 			cdce_init(sc);
    495 			break;
    496 		case IFF_RUNNING:
    497 			cdce_stop(sc);
    498 			break;
    499 		default:
    500 			break;
    501 		}
    502 		break;
    503 
    504 	default:
    505 		error = ether_ioctl(ifp, command, data);
    506 		break;
    507 	}
    508 
    509 	splx(s);
    510 
    511 	if (error == ENETRESET)
    512 		error = 0;
    513 
    514 	return (error);
    515 }
    516 
    517 Static void
    518 cdce_watchdog(struct ifnet *ifp)
    519 {
    520 	struct cdce_softc	*sc = ifp->if_softc;
    521 
    522 	if (sc->cdce_dying)
    523 		return;
    524 
    525 	ifp->if_oerrors++;
    526 	printf("%s: watchdog timeout\n", device_xname(sc->cdce_dev));
    527 }
    528 
    529 Static void
    530 cdce_init(void *xsc)
    531 {
    532 	struct cdce_softc	*sc = xsc;
    533 	struct ifnet		*ifp = GET_IFP(sc);
    534 	struct cdce_chain	*c;
    535 	usbd_status		 err;
    536 	int			 s, i;
    537 
    538 	if (ifp->if_flags & IFF_RUNNING)
    539 		return;
    540 
    541 	s = splnet();
    542 
    543 	if (cdce_tx_list_init(sc) == ENOBUFS) {
    544 		printf("%s: tx list init failed\n", device_xname(sc->cdce_dev));
    545 		splx(s);
    546 		return;
    547 	}
    548 
    549 	if (cdce_rx_list_init(sc) == ENOBUFS) {
    550 		printf("%s: rx list init failed\n", device_xname(sc->cdce_dev));
    551 		splx(s);
    552 		return;
    553 	}
    554 
    555 	/* Maybe set multicast / broadcast here??? */
    556 
    557 	err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkin_no,
    558 	    USBD_EXCLUSIVE_USE, &sc->cdce_bulkin_pipe);
    559 	if (err) {
    560 		printf("%s: open rx pipe failed: %s\n", device_xname(sc->cdce_dev),
    561 		    usbd_errstr(err));
    562 		splx(s);
    563 		return;
    564 	}
    565 
    566 	err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkout_no,
    567 	    USBD_EXCLUSIVE_USE, &sc->cdce_bulkout_pipe);
    568 	if (err) {
    569 		printf("%s: open tx pipe failed: %s\n",
    570 		    device_xname(sc->cdce_dev), usbd_errstr(err));
    571 		splx(s);
    572 		return;
    573 	}
    574 
    575 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
    576 		c = &sc->cdce_cdata.cdce_rx_chain[i];
    577 		usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkin_pipe, c,
    578 		    c->cdce_buf, CDCE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    579 		    USBD_NO_TIMEOUT, cdce_rxeof);
    580 		usbd_transfer(c->cdce_xfer);
    581 	}
    582 
    583 	ifp->if_flags |= IFF_RUNNING;
    584 	ifp->if_flags &= ~IFF_OACTIVE;
    585 
    586 	splx(s);
    587 }
    588 
    589 Static int
    590 cdce_newbuf(struct cdce_softc *sc, struct cdce_chain *c, struct mbuf *m)
    591 {
    592 	struct mbuf	*m_new = NULL;
    593 
    594 	if (m == NULL) {
    595 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    596 		if (m_new == NULL) {
    597 			printf("%s: no memory for rx list "
    598 			    "-- packet dropped!\n", device_xname(sc->cdce_dev));
    599 			return (ENOBUFS);
    600 		}
    601 		MCLGET(m_new, M_DONTWAIT);
    602 		if (!(m_new->m_flags & M_EXT)) {
    603 			printf("%s: no memory for rx list "
    604 			    "-- packet dropped!\n", device_xname(sc->cdce_dev));
    605 			m_freem(m_new);
    606 			return (ENOBUFS);
    607 		}
    608 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    609 	} else {
    610 		m_new = m;
    611 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    612 		m_new->m_data = m_new->m_ext.ext_buf;
    613 	}
    614 	c->cdce_mbuf = m_new;
    615 	return (0);
    616 }
    617 
    618 Static int
    619 cdce_rx_list_init(struct cdce_softc *sc)
    620 {
    621 	struct cdce_cdata	*cd;
    622 	struct cdce_chain	*c;
    623 	int			 i;
    624 
    625 	cd = &sc->cdce_cdata;
    626 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
    627 		c = &cd->cdce_rx_chain[i];
    628 		c->cdce_sc = sc;
    629 		c->cdce_idx = i;
    630 		if (cdce_newbuf(sc, c, NULL) == ENOBUFS)
    631 			return (ENOBUFS);
    632 		if (c->cdce_xfer == NULL) {
    633 			c->cdce_xfer = usbd_alloc_xfer(sc->cdce_udev);
    634 			if (c->cdce_xfer == NULL)
    635 				return (ENOBUFS);
    636 			c->cdce_buf = usbd_alloc_buffer(c->cdce_xfer,
    637 			    CDCE_BUFSZ);
    638 			if (c->cdce_buf == NULL)
    639 				return (ENOBUFS);
    640 		}
    641 	}
    642 
    643 	return (0);
    644 }
    645 
    646 Static int
    647 cdce_tx_list_init(struct cdce_softc *sc)
    648 {
    649 	struct cdce_cdata	*cd;
    650 	struct cdce_chain	*c;
    651 	int			 i;
    652 
    653 	cd = &sc->cdce_cdata;
    654 	for (i = 0; i < CDCE_TX_LIST_CNT; i++) {
    655 		c = &cd->cdce_tx_chain[i];
    656 		c->cdce_sc = sc;
    657 		c->cdce_idx = i;
    658 		c->cdce_mbuf = NULL;
    659 		if (c->cdce_xfer == NULL) {
    660 			c->cdce_xfer = usbd_alloc_xfer(sc->cdce_udev);
    661 			if (c->cdce_xfer == NULL)
    662 				return (ENOBUFS);
    663 			c->cdce_buf = usbd_alloc_buffer(c->cdce_xfer, CDCE_BUFSZ);
    664 			if (c->cdce_buf == NULL)
    665 				return (ENOBUFS);
    666 		}
    667 	}
    668 
    669 	return (0);
    670 }
    671 
    672 Static void
    673 cdce_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    674 {
    675 	struct cdce_chain	*c = priv;
    676 	struct cdce_softc	*sc = c->cdce_sc;
    677 	struct ifnet		*ifp = GET_IFP(sc);
    678 	struct mbuf		*m;
    679 	int			 total_len = 0;
    680 	int			 s;
    681 
    682 	if (sc->cdce_dying || !(ifp->if_flags & IFF_RUNNING))
    683 		return;
    684 
    685 	if (status != USBD_NORMAL_COMPLETION) {
    686 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    687 			return;
    688 		if (sc->cdce_rxeof_errors == 0)
    689 			printf("%s: usb error on rx: %s\n",
    690 			    device_xname(sc->cdce_dev), usbd_errstr(status));
    691 		if (status == USBD_STALLED)
    692 			usbd_clear_endpoint_stall_async(sc->cdce_bulkin_pipe);
    693 		DELAY(sc->cdce_rxeof_errors * 10000);
    694 		sc->cdce_rxeof_errors++;
    695 		goto done;
    696 	}
    697 
    698 	sc->cdce_rxeof_errors = 0;
    699 
    700 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    701 	if (sc->cdce_flags & CDCE_ZAURUS)
    702 		total_len -= 4;	/* Strip off CRC added by Zaurus */
    703 	if (total_len <= 1)
    704 		goto done;
    705 
    706 	m = c->cdce_mbuf;
    707 	memcpy(mtod(m, char *), c->cdce_buf, total_len);
    708 
    709 	if (total_len < sizeof(struct ether_header)) {
    710 		ifp->if_ierrors++;
    711 		goto done;
    712 	}
    713 
    714 	ifp->if_ipackets++;
    715 	m->m_pkthdr.len = m->m_len = total_len;
    716 	m->m_pkthdr.rcvif = ifp;
    717 
    718 	s = splnet();
    719 
    720 	if (cdce_newbuf(sc, c, NULL) == ENOBUFS) {
    721 		ifp->if_ierrors++;
    722 		goto done1;
    723 	}
    724 
    725 	bpf_mtap(ifp, m);
    726 
    727 	(*(ifp)->if_input)((ifp), (m));
    728 
    729 done1:
    730 	splx(s);
    731 
    732 done:
    733 	/* Setup new transfer. */
    734 	usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkin_pipe, c, c->cdce_buf,
    735 	    CDCE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
    736 	    cdce_rxeof);
    737 	usbd_transfer(c->cdce_xfer);
    738 }
    739 
    740 Static void
    741 cdce_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
    742     usbd_status status)
    743 {
    744 	struct cdce_chain	*c = priv;
    745 	struct cdce_softc	*sc = c->cdce_sc;
    746 	struct ifnet		*ifp = GET_IFP(sc);
    747 	usbd_status		 err;
    748 	int			 s;
    749 
    750 	if (sc->cdce_dying)
    751 		return;
    752 
    753 	s = splnet();
    754 
    755 	ifp->if_timer = 0;
    756 	ifp->if_flags &= ~IFF_OACTIVE;
    757 
    758 	if (status != USBD_NORMAL_COMPLETION) {
    759 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    760 			splx(s);
    761 			return;
    762 		}
    763 		ifp->if_oerrors++;
    764 		printf("%s: usb error on tx: %s\n", device_xname(sc->cdce_dev),
    765 		    usbd_errstr(status));
    766 		if (status == USBD_STALLED)
    767 			usbd_clear_endpoint_stall_async(sc->cdce_bulkout_pipe);
    768 		splx(s);
    769 		return;
    770 	}
    771 
    772 	usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &err);
    773 
    774 	if (c->cdce_mbuf != NULL) {
    775 		m_freem(c->cdce_mbuf);
    776 		c->cdce_mbuf = NULL;
    777 	}
    778 
    779 	if (err)
    780 		ifp->if_oerrors++;
    781 	else
    782 		ifp->if_opackets++;
    783 
    784 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    785 		cdce_start(ifp);
    786 
    787 	splx(s);
    788 }
    789 
    790 int
    791 cdce_activate(device_t self, enum devact act)
    792 {
    793 	struct cdce_softc *sc = device_private(self);
    794 
    795 	switch (act) {
    796 	case DVACT_DEACTIVATE:
    797 		if_deactivate(GET_IFP(sc));
    798 		sc->cdce_dying = 1;
    799 		return 0;
    800 	default:
    801 		return EOPNOTSUPP;
    802 	}
    803 }
    804