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