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