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