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