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