Home | History | Annotate | Line # | Download | only in usb
if_cdce.c revision 1.53
      1 /*	$NetBSD: if_cdce.c,v 1.53 2019/07/21 10:27:56 mrg 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.53 2019/07/21 10:27:56 mrg 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 #include <sys/rndsource.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_arp.h>
     61 #include <net/if_dl.h>
     62 #include <net/if_media.h>
     63 
     64 #include <net/bpf.h>
     65 
     66 #include <net/if_ether.h>
     67 #ifdef INET
     68 #include <netinet/in.h>
     69 #include <netinet/if_inarp.h>
     70 #endif
     71 
     72 #include <dev/usb/usb.h>
     73 #include <dev/usb/usbdi.h>
     74 #include <dev/usb/usbdivar.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 struct cdce_type {
     82 	struct usb_devno	 cdce_dev;
     83 	uint16_t		 cdce_flags;
     84 #define CDCE_ZAURUS	1
     85 #define CDCE_NO_UNION	2
     86 };
     87 
     88 struct cdce_softc;
     89 
     90 struct cdce_chain {
     91 	struct cdce_softc	*cdce_sc;
     92 	struct usbd_xfer	*cdce_xfer;
     93 	char			*cdce_buf;
     94 	struct mbuf		*cdce_mbuf;
     95 	int			 cdce_accum;
     96 	int			 cdce_idx;
     97 };
     98 
     99 struct cdce_cdata {
    100 	struct cdce_chain	 cdce_rx_chain[CDCE_RX_LIST_CNT];
    101 	struct cdce_chain	 cdce_tx_chain[CDCE_TX_LIST_CNT];
    102 	int			 cdce_tx_prod;
    103 	int			 cdce_tx_cnt;
    104 };
    105 
    106 struct cdce_softc {
    107 	device_t cdce_dev;
    108 	struct ethercom		 cdce_ec;
    109 	krndsource_t	 rnd_source;
    110 #define GET_IFP(sc) (&(sc)->cdce_ec.ec_if)
    111 	struct usbd_device *	 cdce_udev;
    112 	struct usbd_interface *	 cdce_ctl_iface;
    113 	struct usbd_interface *	 cdce_data_iface;
    114 	int			 cdce_bulkin_no;
    115 	struct usbd_pipe *	 cdce_bulkin_pipe;
    116 	int			 cdce_bulkout_no;
    117 	struct usbd_pipe *	 cdce_bulkout_pipe;
    118 	int			 cdce_unit;
    119 	struct cdce_cdata	 cdce_cdata;
    120 	int			 cdce_rxeof_errors;
    121 	uint16_t		 cdce_flags;
    122 	uint16_t		 cdce_timer;
    123 
    124 	bool			 cdce_attached;
    125 	bool			 cdce_dying;
    126 	bool			 cdce_stopping;
    127 
    128 	struct usb_task		 cdce_tick_task;
    129 	struct callout		 cdce_stat_ch;
    130 
    131 	kmutex_t		 cdce_lock;
    132 	kmutex_t		 cdce_mii_lock;
    133 	kmutex_t		 cdce_rxlock;
    134 	kmutex_t		 cdce_txlock;
    135 };
    136 
    137 static int	cdce_tx_list_init(struct cdce_softc *);
    138 static int	cdce_rx_list_init(struct cdce_softc *);
    139 static int	cdce_newbuf(struct cdce_softc *, struct cdce_chain *,
    140 		    struct mbuf *);
    141 static int	cdce_encap(struct cdce_softc *, struct mbuf *, int);
    142 static void	cdce_rxeof(struct usbd_xfer *, void *, usbd_status);
    143 static void	cdce_txeof(struct usbd_xfer *, void *, usbd_status);
    144 static void	cdce_start(struct ifnet *);
    145 static int	cdce_ioctl(struct ifnet *, u_long, void *);
    146 static void	cdce_init(void *);
    147 static void	cdce_stop(struct cdce_softc *);
    148 static void	cdce_tick(void *);
    149 static void	cdce_tick_task(void *);
    150 
    151 static const struct cdce_type cdce_devs[] = {
    152   {{ USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632 }, CDCE_NO_UNION },
    153   {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX }, CDCE_NO_UNION },
    154   {{ USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00 }, CDCE_NO_UNION },
    155   {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN }, CDCE_ZAURUS | CDCE_NO_UNION },
    156   {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2 }, CDCE_ZAURUS | CDCE_NO_UNION },
    157   {{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501 }, CDCE_NO_UNION },
    158   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500 }, CDCE_ZAURUS },
    159   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_A300 }, CDCE_ZAURUS | CDCE_NO_UNION },
    160   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600 }, CDCE_ZAURUS | CDCE_NO_UNION },
    161   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C700 }, CDCE_ZAURUS | CDCE_NO_UNION },
    162   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C750 }, CDCE_ZAURUS | CDCE_NO_UNION },
    163 };
    164 #define cdce_lookup(v, p) \
    165 	((const struct cdce_type *)usb_lookup(cdce_devs, v, p))
    166 
    167 int cdce_match(device_t, cfdata_t, void *);
    168 void cdce_attach(device_t, device_t, void *);
    169 int cdce_detach(device_t, int);
    170 int cdce_activate(device_t, enum devact);
    171 
    172 CFATTACH_DECL_NEW(cdce, sizeof(struct cdce_softc), cdce_match, cdce_attach,
    173     cdce_detach, cdce_activate);
    174 
    175 int
    176 cdce_match(device_t parent, cfdata_t match, void *aux)
    177 {
    178 	struct usbif_attach_arg *uiaa = aux;
    179 
    180 	if (cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
    181 		return UMATCH_VENDOR_PRODUCT;
    182 
    183 	if (uiaa->uiaa_class == UICLASS_CDC && uiaa->uiaa_subclass ==
    184 	    UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL)
    185 		return UMATCH_IFACECLASS_GENERIC;
    186 
    187 	return UMATCH_NONE;
    188 }
    189 
    190 void
    191 cdce_attach(device_t parent, device_t self, void *aux)
    192 {
    193 	struct cdce_softc *sc = device_private(self);
    194 	struct usbif_attach_arg *uiaa = aux;
    195 	char				*devinfop;
    196 	struct ifnet			*ifp;
    197 	struct usbd_device	        *dev = uiaa->uiaa_device;
    198 	const struct cdce_type		*t;
    199 	usb_interface_descriptor_t	*id;
    200 	usb_endpoint_descriptor_t	*ed;
    201 	const usb_cdc_union_descriptor_t *ud;
    202 	usb_config_descriptor_t		*cd;
    203 	int				 data_ifcno;
    204 	int				 i, j, numalts;
    205 	u_char				 eaddr[ETHER_ADDR_LEN];
    206 	const usb_cdc_ethernet_descriptor_t *ue;
    207 	char				 eaddr_str[USB_MAX_ENCODED_STRING_LEN];
    208 
    209 	sc->cdce_dev = self;
    210 
    211 	aprint_naive("\n");
    212 	aprint_normal("\n");
    213 
    214 	devinfop = usbd_devinfo_alloc(dev, 0);
    215 	aprint_normal_dev(self, "%s\n", devinfop);
    216 	usbd_devinfo_free(devinfop);
    217 
    218 	sc->cdce_udev = uiaa->uiaa_device;
    219 	sc->cdce_ctl_iface = uiaa->uiaa_iface;
    220 
    221 	t = cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
    222 	if (t)
    223 		sc->cdce_flags = t->cdce_flags;
    224 
    225 	if (sc->cdce_flags & CDCE_NO_UNION)
    226 		sc->cdce_data_iface = sc->cdce_ctl_iface;
    227 	else {
    228 		ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(sc->cdce_udev,
    229 		    UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION);
    230 		if (ud == NULL) {
    231 			aprint_error_dev(self, "no union descriptor\n");
    232 			return;
    233 		}
    234 		data_ifcno = ud->bSlaveInterface[0];
    235 
    236 		for (i = 0; i < uiaa->uiaa_nifaces; i++) {
    237 			if (uiaa->uiaa_ifaces[i] != NULL) {
    238 				id = usbd_get_interface_descriptor(
    239 				    uiaa->uiaa_ifaces[i]);
    240 				if (id != NULL && id->bInterfaceNumber ==
    241 				    data_ifcno) {
    242 					sc->cdce_data_iface = uiaa->uiaa_ifaces[i];
    243 					uiaa->uiaa_ifaces[i] = NULL;
    244 				}
    245 			}
    246 		}
    247 	}
    248 
    249 	if (sc->cdce_data_iface == NULL) {
    250 		aprint_error_dev(self, "no data interface\n");
    251 		return;
    252 	}
    253 
    254 	/*
    255 	 * <quote>
    256 	 *  The Data Class interface of a networking device shall have a minimum
    257 	 *  of two interface settings. The first setting (the default interface
    258 	 *  setting) includes no endpoints and therefore no networking traffic is
    259 	 *  exchanged whenever the default interface setting is selected. One or
    260 	 *  more additional interface settings are used for normal operation, and
    261 	 *  therefore each includes a pair of endpoints (one IN, and one OUT) to
    262 	 *  exchange network traffic. Select an alternate interface setting to
    263 	 *  initialize the network aspects of the device and to enable the
    264 	 *  exchange of network traffic.
    265 	 * </quote>
    266 	 *
    267 	 * Some devices, most notably cable modems, include interface settings
    268 	 * that have no IN or OUT endpoint, therefore loop through the list of all
    269 	 * available interface settings looking for one with both IN and OUT
    270 	 * endpoints.
    271 	 */
    272 	id = usbd_get_interface_descriptor(sc->cdce_data_iface);
    273 	cd = usbd_get_config_descriptor(sc->cdce_udev);
    274 	numalts = usbd_get_no_alts(cd, id->bInterfaceNumber);
    275 
    276 	for (j = 0; j < numalts; j++) {
    277 		if (usbd_set_interface(sc->cdce_data_iface, j)) {
    278 			aprint_error_dev(sc->cdce_dev,
    279 					"setting alternate interface failed\n");
    280 			return;
    281 		}
    282 		/* Find endpoints. */
    283 		id = usbd_get_interface_descriptor(sc->cdce_data_iface);
    284 		sc->cdce_bulkin_no = sc->cdce_bulkout_no = -1;
    285 		for (i = 0; i < id->bNumEndpoints; i++) {
    286 			ed = usbd_interface2endpoint_descriptor(sc->cdce_data_iface, i);
    287 			if (!ed) {
    288 				aprint_error_dev(self,
    289 						"could not read endpoint descriptor\n");
    290 				return;
    291 			}
    292 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    293 					UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    294 				sc->cdce_bulkin_no = ed->bEndpointAddress;
    295 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    296 					UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    297 				sc->cdce_bulkout_no = ed->bEndpointAddress;
    298 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    299 					UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    300 				/* XXX: CDC spec defines an interrupt pipe, but it is not
    301 				 * needed for simple host-to-host applications. */
    302 			} else {
    303 				aprint_error_dev(self, "unexpected endpoint\n");
    304 			}
    305 		}
    306 		/* If we found something, try and use it... */
    307 		if ((sc->cdce_bulkin_no != -1) && (sc->cdce_bulkout_no != -1))
    308 			break;
    309 	}
    310 
    311 	if (sc->cdce_bulkin_no == -1) {
    312 		aprint_error_dev(self, "could not find data bulk in\n");
    313 		return;
    314 	}
    315 	if (sc->cdce_bulkout_no == -1 ) {
    316 		aprint_error_dev(self, "could not find data bulk out\n");
    317 		return;
    318 	}
    319 
    320 	ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc(dev,
    321 	    UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF);
    322 	if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) ||
    323 	    ether_aton_r(eaddr, sizeof(eaddr), eaddr_str)) {
    324 		aprint_normal_dev(self, "faking address\n");
    325 		eaddr[0]= 0x2a;
    326 		memcpy(&eaddr[1], &hardclock_ticks, sizeof(uint32_t));
    327 		eaddr[5] = (uint8_t)(device_unit(sc->cdce_dev));
    328 	}
    329 
    330 	mutex_init(&sc->cdce_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    331 	mutex_init(&sc->cdce_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    332 	mutex_init(&sc->cdce_lock, MUTEX_DEFAULT, IPL_NONE);
    333 
    334 	usb_init_task(&sc->cdce_tick_task, cdce_tick_task, sc, USB_TASKQ_MPSAFE);
    335 
    336 	aprint_normal_dev(self, "address %s\n", ether_sprintf(eaddr));
    337 
    338 	ifp = GET_IFP(sc);
    339 	ifp->if_softc = sc;
    340 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    341 	ifp->if_extflags = IFEF_MPSAFE;
    342 	ifp->if_ioctl = cdce_ioctl;
    343 	ifp->if_start = cdce_start;
    344 	strlcpy(ifp->if_xname, device_xname(sc->cdce_dev), IFNAMSIZ);
    345 
    346 	IFQ_SET_READY(&ifp->if_snd);
    347 
    348 	if_attach(ifp);
    349 	ether_ifattach(ifp, eaddr);
    350 
    351 	callout_init(&sc->cdce_stat_ch, CALLOUT_MPSAFE);
    352 	callout_setfunc(&sc->cdce_stat_ch, cdce_tick, sc);
    353 
    354 	sc->cdce_attached = true;
    355 
    356 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cdce_udev,
    357 	    sc->cdce_dev);
    358 
    359 	if (!pmf_device_register(self, NULL, NULL))
    360 		aprint_error_dev(self, "couldn't establish power handler\n");
    361 
    362 	return;
    363 }
    364 
    365 int
    366 cdce_detach(device_t self, int flags)
    367 {
    368 	struct cdce_softc *sc = device_private(self);
    369 	struct ifnet	*ifp = GET_IFP(sc);
    370 
    371 	mutex_enter(&sc->cdce_lock);
    372 	sc->cdce_dying = true;
    373 	mutex_exit(&sc->cdce_lock);
    374 
    375 	if (!sc->cdce_attached)
    376 		return 0;
    377 
    378 	pmf_device_deregister(self);
    379 
    380 	callout_halt(&sc->cdce_stat_ch, NULL);
    381 	usb_rem_task_wait(sc->cdce_udev, &sc->cdce_tick_task,
    382 	    USB_TASKQ_DRIVER, NULL);
    383 
    384 	if (ifp->if_flags & IFF_RUNNING) {
    385 		IFNET_LOCK(ifp);
    386 		cdce_stop(sc);
    387 		IFNET_UNLOCK(ifp);
    388 	}
    389 
    390 	callout_destroy(&sc->cdce_stat_ch);
    391 	ether_ifdetach(ifp);
    392 	if_detach(ifp);
    393 	sc->cdce_attached = false;
    394 
    395 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cdce_udev,sc->cdce_dev);
    396 
    397 	mutex_destroy(&sc->cdce_lock);
    398 	mutex_destroy(&sc->cdce_rxlock);
    399 	mutex_destroy(&sc->cdce_txlock);
    400 
    401 	return 0;
    402 }
    403 
    404 static void
    405 cdce_start_locked(struct ifnet *ifp)
    406 {
    407 	struct cdce_softc	*sc = ifp->if_softc;
    408 	struct mbuf		*m_head = NULL;
    409 
    410 	KASSERT(mutex_owned(&sc->cdce_txlock));
    411 
    412 	if (sc->cdce_dying || sc->cdce_cdata.cdce_tx_cnt == CDCE_TX_LIST_CNT)
    413 		return;
    414 
    415 	IFQ_POLL(&ifp->if_snd, m_head);
    416 	if (m_head == NULL)
    417 		return;
    418 
    419 	if (cdce_encap(sc, m_head, 0)) {
    420 		ifp->if_flags |= IFF_OACTIVE;
    421 		return;
    422 	}
    423 
    424 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    425 
    426 	bpf_mtap(ifp, m_head, BPF_D_OUT);
    427 
    428 	/*
    429 	 * Set a timeout in case the chip goes out to lunch.
    430 	 */
    431 	sc->cdce_timer = 6;
    432 }
    433 
    434 static void
    435 cdce_start(struct ifnet *ifp)
    436 {
    437 	struct cdce_softc * const sc = ifp->if_softc;
    438 
    439 	mutex_enter(&sc->cdce_txlock);
    440 	cdce_start_locked(ifp);
    441 	mutex_exit(&sc->cdce_txlock);
    442 }
    443 
    444 static int
    445 cdce_encap(struct cdce_softc *sc, struct mbuf *m, int idx)
    446 {
    447 	struct cdce_chain	*c;
    448 	usbd_status		 err;
    449 	int			 extra = 0;
    450 
    451 	KASSERT(mutex_owned(&sc->cdce_txlock));
    452 
    453 	c = &sc->cdce_cdata.cdce_tx_chain[idx];
    454 
    455 	m_copydata(m, 0, m->m_pkthdr.len, c->cdce_buf);
    456 	if (sc->cdce_flags & CDCE_ZAURUS) {
    457 		/* Zaurus wants a 32-bit CRC appended to every frame */
    458 		uint32_t crc;
    459 
    460 		crc = htole32(~ether_crc32_le(c->cdce_buf, m->m_pkthdr.len));
    461 		memcpy(c->cdce_buf + m->m_pkthdr.len, &crc, sizeof(crc));
    462 		extra = sizeof(crc);
    463 	}
    464 	c->cdce_mbuf = m;
    465 
    466 	usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, m->m_pkthdr.len + extra,
    467 	    USBD_FORCE_SHORT_XFER, 10000, cdce_txeof);
    468 	err = usbd_transfer(c->cdce_xfer);
    469 	if (err != USBD_IN_PROGRESS) {
    470 		/* XXXSMP IFNET_LOCK */
    471 		cdce_stop(sc);
    472 		return EIO;
    473 	}
    474 
    475 	sc->cdce_cdata.cdce_tx_cnt++;
    476 	KASSERT(sc->cdce_cdata.cdce_tx_cnt <= CDCE_TX_LIST_CNT);
    477 
    478 	return 0;
    479 }
    480 
    481 static void
    482 cdce_stop_locked(struct cdce_softc *sc)
    483 {
    484 	usbd_status	 err;
    485 	struct ifnet	*ifp = GET_IFP(sc);
    486 	int		 i;
    487 
    488 	/* XXXSMP can't KASSERT(IFNET_LOCKED(ifp)); */
    489 	KASSERT(mutex_owned(&sc->cdce_lock));
    490 
    491 	mutex_enter(&sc->cdce_rxlock);
    492 	mutex_enter(&sc->cdce_txlock);
    493 	sc->cdce_stopping = true;
    494 	mutex_exit(&sc->cdce_txlock);
    495 	mutex_exit(&sc->cdce_rxlock);
    496 
    497 	/*
    498 	 * XXXSMP Would like to
    499 	 *	KASSERT(IFNET_LOCKED(ifp))
    500 	 * here but the locking order is:
    501 	 *	ifnet -> sc lock -> rxlock -> txlock
    502 	 * and sc lock is already held.
    503 	 */
    504 	ifp->if_flags &= ~IFF_RUNNING;
    505 	sc->cdce_timer = 0;
    506 
    507 	callout_stop(&sc->cdce_stat_ch);
    508 
    509 	if (sc->cdce_bulkin_pipe != NULL) {
    510 		err = usbd_abort_pipe(sc->cdce_bulkin_pipe);
    511 		if (err)
    512 			printf("%s: abort rx pipe failed: %s\n",
    513 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    514 	}
    515 
    516 	if (sc->cdce_bulkout_pipe != NULL) {
    517 		err = usbd_abort_pipe(sc->cdce_bulkout_pipe);
    518 		if (err)
    519 			printf("%s: abort tx pipe failed: %s\n",
    520 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    521 	}
    522 
    523 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
    524 		if (sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf != NULL) {
    525 			m_freem(sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf);
    526 			sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf = NULL;
    527 		}
    528 		if (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer != NULL) {
    529 			usbd_destroy_xfer
    530 			    (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer);
    531 			sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer = NULL;
    532 		}
    533 	}
    534 
    535 	for (i = 0; i < CDCE_TX_LIST_CNT; i++) {
    536 		if (sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf != NULL) {
    537 			m_freem(sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf);
    538 			sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf = NULL;
    539 		}
    540 		if (sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer != NULL) {
    541 			usbd_destroy_xfer(
    542 				sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer);
    543 			sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer = NULL;
    544 		}
    545 	}
    546 
    547 	if (sc->cdce_bulkin_pipe != NULL) {
    548 		err = usbd_close_pipe(sc->cdce_bulkin_pipe);
    549 		if (err)
    550 			printf("%s: close rx pipe failed: %s\n",
    551 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    552 		sc->cdce_bulkin_pipe = NULL;
    553 	}
    554 
    555 	if (sc->cdce_bulkout_pipe != NULL) {
    556 		err = usbd_close_pipe(sc->cdce_bulkout_pipe);
    557 		if (err)
    558 			printf("%s: close tx pipe failed: %s\n",
    559 			    device_xname(sc->cdce_dev), usbd_errstr(err));
    560 		sc->cdce_bulkout_pipe = NULL;
    561 	}
    562 }
    563 
    564 static void
    565 cdce_stop(struct cdce_softc *sc)
    566 {
    567 
    568 	mutex_enter(&sc->cdce_lock);
    569 	cdce_stop_locked(sc);
    570 	mutex_exit(&sc->cdce_lock);
    571 }
    572 
    573 static int
    574 cdce_ioctl(struct ifnet *ifp, u_long command, void *data)
    575 {
    576 	struct cdce_softc	*sc = ifp->if_softc;
    577 	struct ifaddr		*ifa = (struct ifaddr *)data;
    578 	struct ifreq		*ifr = (struct ifreq *)data;
    579 	int			 error = 0;
    580 
    581 	if (sc->cdce_dying)
    582 		return EIO;
    583 
    584 	switch(command) {
    585 	case SIOCINITIFADDR:
    586 		ifp->if_flags |= IFF_UP;
    587 		cdce_init(sc);
    588 		switch (ifa->ifa_addr->sa_family) {
    589 #ifdef INET
    590 		case AF_INET:
    591 			arp_ifinit(ifp, ifa);
    592 			break;
    593 #endif /* INET */
    594 		}
    595 		break;
    596 
    597 	case SIOCSIFMTU:
    598 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
    599 			error = EINVAL;
    600 		else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
    601 			error = 0;
    602 		break;
    603 
    604 	case SIOCSIFFLAGS:
    605 		if ((error = ifioctl_common(ifp, command, data)) != 0)
    606 			break;
    607 		/* XXX re-use ether_ioctl() */
    608 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    609 		case IFF_UP:
    610 			cdce_init(sc);
    611 			break;
    612 		case IFF_RUNNING:
    613 			cdce_stop(sc);
    614 			break;
    615 		default:
    616 			break;
    617 		}
    618 		break;
    619 
    620 	default:
    621 		error = ether_ioctl(ifp, command, data);
    622 		break;
    623 	}
    624 
    625 	if (error == ENETRESET)
    626 		error = 0;
    627 
    628 	return error;
    629 }
    630 
    631 static void
    632 cdce_watchdog(struct ifnet *ifp)
    633 {
    634 	struct cdce_softc *sc = ifp->if_softc;
    635 	struct cdce_chain *c;
    636 	usbd_status stat;
    637 
    638 	KASSERT(mutex_owned(&sc->cdce_lock));
    639 
    640 	if (sc->cdce_dying)
    641 		return;
    642 
    643 	ifp->if_oerrors++;
    644 	aprint_error_dev(sc->cdce_dev, "watchdog timeout\n");
    645 
    646 	mutex_enter(&sc->cdce_txlock);
    647 
    648 	c = &sc->cdce_cdata.cdce_rx_chain[0];
    649 	usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &stat);
    650 	cdce_txeof(c->cdce_xfer, c, stat);
    651 
    652 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
    653 		cdce_start_locked(ifp);
    654 
    655 	mutex_exit(&sc->cdce_txlock);
    656 }
    657 
    658 static void
    659 cdce_init(void *xsc)
    660 {
    661 	struct cdce_softc	*sc = xsc;
    662 	struct ifnet		*ifp = GET_IFP(sc);
    663 	struct cdce_chain	*c;
    664 	usbd_status		 err;
    665 	int			 i;
    666 
    667 	mutex_enter(&sc->cdce_lock);
    668 	if (ifp->if_flags & IFF_RUNNING)
    669 		goto out;
    670 
    671 	/* Maybe set multicast / broadcast here??? */
    672 
    673 	err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkin_no,
    674 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->cdce_bulkin_pipe);
    675 	if (err) {
    676 		printf("%s: open rx pipe failed: %s\n", device_xname(sc->cdce_dev),
    677 		    usbd_errstr(err));
    678 		goto out;
    679 	}
    680 
    681 	err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkout_no,
    682 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->cdce_bulkout_pipe);
    683 	if (err) {
    684 		printf("%s: open tx pipe failed: %s\n",
    685 		    device_xname(sc->cdce_dev), usbd_errstr(err));
    686 		goto out;
    687 	}
    688 
    689 	if (cdce_tx_list_init(sc)) {
    690 		printf("%s: tx list init failed\n", device_xname(sc->cdce_dev));
    691 		goto out;
    692 	}
    693 
    694 	if (cdce_rx_list_init(sc)) {
    695 		printf("%s: rx list init failed\n", device_xname(sc->cdce_dev));
    696 		goto out;
    697 	}
    698 
    699 	mutex_enter(&sc->cdce_rxlock);
    700 	mutex_enter(&sc->cdce_txlock);
    701 	sc->cdce_stopping = false;
    702 
    703 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
    704 		c = &sc->cdce_cdata.cdce_rx_chain[i];
    705 
    706 		usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, CDCE_BUFSZ,
    707 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cdce_rxeof);
    708 		usbd_transfer(c->cdce_xfer);
    709 	}
    710 
    711 	mutex_exit(&sc->cdce_txlock);
    712 	mutex_exit(&sc->cdce_rxlock);
    713 
    714 	ifp->if_flags |= IFF_RUNNING;
    715 
    716 	callout_schedule(&sc->cdce_stat_ch, hz);
    717 
    718 out:
    719 	mutex_exit(&sc->cdce_lock);
    720 }
    721 
    722 static int
    723 cdce_newbuf(struct cdce_softc *sc, struct cdce_chain *c, struct mbuf *m)
    724 {
    725 	struct mbuf	*m_new = NULL;
    726 
    727 	if (m == NULL) {
    728 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    729 		if (m_new == NULL) {
    730 			printf("%s: no memory for rx list "
    731 			    "-- packet dropped!\n", device_xname(sc->cdce_dev));
    732 			return ENOBUFS;
    733 		}
    734 		MCLGET(m_new, M_DONTWAIT);
    735 		if (!(m_new->m_flags & M_EXT)) {
    736 			printf("%s: no memory for rx list "
    737 			    "-- packet dropped!\n", device_xname(sc->cdce_dev));
    738 			m_freem(m_new);
    739 			return ENOBUFS;
    740 		}
    741 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    742 	} else {
    743 		m_new = m;
    744 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    745 		m_new->m_data = m_new->m_ext.ext_buf;
    746 	}
    747 	c->cdce_mbuf = m_new;
    748 	return 0;
    749 }
    750 
    751 static int
    752 cdce_rx_list_init(struct cdce_softc *sc)
    753 {
    754 	struct cdce_cdata	*cd;
    755 	struct cdce_chain	*c;
    756 	int			 i;
    757 
    758 	cd = &sc->cdce_cdata;
    759 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
    760 		c = &cd->cdce_rx_chain[i];
    761 		c->cdce_sc = sc;
    762 		c->cdce_idx = i;
    763 		if (cdce_newbuf(sc, c, NULL) == ENOBUFS)
    764 			return ENOBUFS;
    765 		if (c->cdce_xfer == NULL) {
    766 			int err = usbd_create_xfer(sc->cdce_bulkin_pipe,
    767 			    CDCE_BUFSZ, 0, 0, &c->cdce_xfer);
    768 			if (err)
    769 				return err;
    770 			c->cdce_buf = usbd_get_buffer(c->cdce_xfer);
    771 		}
    772 	}
    773 
    774 	return 0;
    775 }
    776 
    777 static int
    778 cdce_tx_list_init(struct cdce_softc *sc)
    779 {
    780 	struct cdce_cdata	*cd;
    781 	struct cdce_chain	*c;
    782 	int			 i;
    783 
    784 	cd = &sc->cdce_cdata;
    785 	for (i = 0; i < CDCE_TX_LIST_CNT; i++) {
    786 		c = &cd->cdce_tx_chain[i];
    787 		c->cdce_sc = sc;
    788 		c->cdce_idx = i;
    789 		c->cdce_mbuf = NULL;
    790 		if (c->cdce_xfer == NULL) {
    791 			int err = usbd_create_xfer(sc->cdce_bulkout_pipe,
    792 			    CDCE_BUFSZ, USBD_FORCE_SHORT_XFER, 0,
    793 			    &c->cdce_xfer);
    794 			if (err)
    795 				return err;
    796 			c->cdce_buf = usbd_get_buffer(c->cdce_xfer);
    797 		}
    798 	}
    799 
    800 	return 0;
    801 }
    802 
    803 static void
    804 cdce_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    805 {
    806 	struct cdce_chain	*c = priv;
    807 	struct cdce_softc	*sc = c->cdce_sc;
    808 	struct ifnet		*ifp = GET_IFP(sc);
    809 	struct mbuf		*m;
    810 	int			 total_len = 0;
    811 
    812 	mutex_enter(&sc->cdce_rxlock);
    813 
    814 	if (sc->cdce_dying || sc->cdce_stopping ||
    815 	    status == USBD_INVAL || status == USBD_NOT_STARTED ||
    816 	    status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) {
    817 		mutex_exit(&sc->cdce_rxlock);
    818 		return;
    819 	}
    820 
    821 	if (status != USBD_NORMAL_COMPLETION) {
    822 		if (sc->cdce_rxeof_errors == 0)
    823 			printf("%s: usb error on rx: %s\n",
    824 			    device_xname(sc->cdce_dev), usbd_errstr(status));
    825 		if (status == USBD_STALLED)
    826 			usbd_clear_endpoint_stall_async(sc->cdce_bulkin_pipe);
    827 		usbd_delay_ms(sc->cdce_udev, 10);
    828 		sc->cdce_rxeof_errors++;
    829 		goto done;
    830 	}
    831 
    832 	sc->cdce_rxeof_errors = 0;
    833 
    834 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    835 	if (sc->cdce_flags & CDCE_ZAURUS)
    836 		total_len -= 4;	/* Strip off CRC added by Zaurus */
    837 	if (total_len <= 1) {
    838 		ifp->if_ierrors++;
    839 		goto done;
    840 	}
    841 
    842 	m = c->cdce_mbuf;
    843 	memcpy(mtod(m, char *), c->cdce_buf, total_len);
    844 
    845 	if (total_len < sizeof(struct ether_header)) {
    846 		ifp->if_ierrors++;
    847 		goto done;
    848 	}
    849 
    850 	m->m_pkthdr.len = m->m_len = total_len;
    851 	m_set_rcvif(m, ifp);
    852 
    853 	if (cdce_newbuf(sc, c, NULL) == ENOBUFS) {
    854 		ifp->if_ierrors++;
    855 		goto done;
    856 	}
    857 
    858 	mutex_exit(&sc->cdce_rxlock);
    859 	if_percpuq_enqueue(ifp->if_percpuq, m);
    860 	mutex_enter(&sc->cdce_rxlock);
    861 
    862 done:
    863 	if (sc->cdce_stopping || sc->cdce_dying) {
    864 		mutex_exit(&sc->cdce_rxlock);
    865 		return;
    866 	}
    867 
    868 	mutex_exit(&sc->cdce_rxlock);
    869 
    870 	/* Setup new transfer. */
    871 	usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, CDCE_BUFSZ,
    872 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cdce_rxeof);
    873 	usbd_transfer(c->cdce_xfer);
    874 }
    875 
    876 static void
    877 cdce_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    878 {
    879 	struct cdce_chain	*c = priv;
    880 	struct cdce_softc	*sc = c->cdce_sc;
    881 	struct ifnet		*ifp = GET_IFP(sc);
    882 	usbd_status		 err;
    883 
    884 	mutex_enter(&sc->cdce_txlock);
    885 
    886 	if (sc->cdce_dying)
    887 		goto out;
    888 
    889 	KASSERT(sc->cdce_cdata.cdce_tx_cnt > 0);
    890 	sc->cdce_cdata.cdce_tx_cnt--;
    891 
    892 	sc->cdce_timer = 0;
    893 
    894 	switch (status) {
    895 	case USBD_NOT_STARTED:
    896 	case USBD_CANCELLED:
    897 		goto out;
    898 
    899 	case USBD_NORMAL_COMPLETION:
    900 		break;
    901 
    902 	default:
    903 		ifp->if_oerrors++;
    904 		printf("%s: usb error on tx: %s\n", device_xname(sc->cdce_dev),
    905 		    usbd_errstr(status));
    906 		if (status == USBD_STALLED)
    907 			usbd_clear_endpoint_stall_async(sc->cdce_bulkout_pipe);
    908 		goto out;
    909 	}
    910 
    911 	usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &err);
    912 
    913 	if (c->cdce_mbuf != NULL) {
    914 		m_freem(c->cdce_mbuf);
    915 		c->cdce_mbuf = NULL;
    916 	}
    917 
    918 	if (err)
    919 		ifp->if_oerrors++;
    920 	else
    921 		ifp->if_opackets++;
    922 
    923 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    924 		cdce_start_locked(ifp);
    925 
    926 out:
    927 	mutex_exit(&sc->cdce_txlock);
    928 }
    929 
    930 static void
    931 cdce_tick(void *xsc)
    932 {
    933 	struct cdce_softc * const sc = xsc;
    934 
    935 	if (sc == NULL)
    936 		return;
    937 
    938 	mutex_enter(&sc->cdce_lock);
    939 
    940 	if (sc->cdce_stopping || sc->cdce_dying) {
    941 		mutex_exit(&sc->cdce_lock);
    942 		return;
    943 	}
    944 
    945 	/* Perform periodic stuff in process context */
    946 	usb_add_task(sc->cdce_udev, &sc->cdce_tick_task, USB_TASKQ_DRIVER);
    947 
    948 	mutex_exit(&sc->cdce_lock);
    949 }
    950 
    951 static void
    952 cdce_tick_task(void *xsc)
    953 {
    954 	struct cdce_softc * const sc = xsc;
    955 	struct ifnet *ifp;
    956 
    957 	if (sc == NULL)
    958 		return;
    959 
    960 	mutex_enter(&sc->cdce_lock);
    961 
    962 	if (sc->cdce_stopping || sc->cdce_dying) {
    963 		mutex_exit(&sc->cdce_lock);
    964 		return;
    965 	}
    966 
    967 	ifp = GET_IFP(sc);
    968 
    969 	if (sc->cdce_timer != 0 && --sc->cdce_timer == 0)
    970 		cdce_watchdog(ifp);
    971 	if (!sc->cdce_stopping && !sc->cdce_dying)
    972 		callout_schedule(&sc->cdce_stat_ch, hz);
    973 
    974 	mutex_exit(&sc->cdce_lock);
    975 }
    976 
    977 int
    978 cdce_activate(device_t self, enum devact act)
    979 {
    980 	struct cdce_softc *sc = device_private(self);
    981 
    982 	switch (act) {
    983 	case DVACT_DEACTIVATE:
    984 		if_deactivate(GET_IFP(sc));
    985 
    986 		mutex_enter(&sc->cdce_lock);
    987 		sc->cdce_dying = true;
    988 		mutex_exit(&sc->cdce_lock);
    989 
    990 		mutex_enter(&sc->cdce_rxlock);
    991 		mutex_enter(&sc->cdce_txlock);
    992 		sc->cdce_stopping = true;
    993 		mutex_exit(&sc->cdce_txlock);
    994 		mutex_exit(&sc->cdce_rxlock);
    995 
    996 		return 0;
    997 	default:
    998 		return EOPNOTSUPP;
    999 	}
   1000 }
   1001