xref: /netbsd-src/sys/dev/usb/if_cdce.c (revision c505c4429840c353a86d4eb53b5e2bfc0092264e)
1 /*	$NetBSD: if_cdce.c,v 1.30 2010/05/19 21:10:42 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.30 2010/05/19 21:10:42 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 	return;
296 }
297 
298 int
299 cdce_detach(device_t self, int flags)
300 {
301 	struct cdce_softc *sc = device_private(self);
302 	struct ifnet	*ifp = GET_IFP(sc);
303 	int		 s;
304 
305 	s = splusb();
306 
307 	if (!sc->cdce_attached) {
308 		splx(s);
309 		return (0);
310 	}
311 
312 	if (ifp->if_flags & IFF_RUNNING)
313 		cdce_stop(sc);
314 
315 	ether_ifdetach(ifp);
316 
317 	if_detach(ifp);
318 
319 	sc->cdce_attached = 0;
320 	splx(s);
321 
322 	return (0);
323 }
324 
325 Static void
326 cdce_start(struct ifnet *ifp)
327 {
328 	struct cdce_softc	*sc = ifp->if_softc;
329 	struct mbuf		*m_head = NULL;
330 
331 	if (sc->cdce_dying || (ifp->if_flags & IFF_OACTIVE))
332 		return;
333 
334 	IFQ_POLL(&ifp->if_snd, m_head);
335 	if (m_head == NULL)
336 		return;
337 
338 	if (cdce_encap(sc, m_head, 0)) {
339 		ifp->if_flags |= IFF_OACTIVE;
340 		return;
341 	}
342 
343 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
344 
345 	bpf_mtap(ifp, m_head);
346 
347 	ifp->if_flags |= IFF_OACTIVE;
348 
349 	ifp->if_timer = 6;
350 }
351 
352 Static int
353 cdce_encap(struct cdce_softc *sc, struct mbuf *m, int idx)
354 {
355 	struct cdce_chain	*c;
356 	usbd_status		 err;
357 	int			 extra = 0;
358 
359 	c = &sc->cdce_cdata.cdce_tx_chain[idx];
360 
361 	m_copydata(m, 0, m->m_pkthdr.len, c->cdce_buf);
362 	if (sc->cdce_flags & CDCE_ZAURUS) {
363 		/* Zaurus wants a 32-bit CRC appended to every frame */
364 		uint32_t crc;
365 
366 		crc = htole32(~ether_crc32_le(c->cdce_buf, m->m_pkthdr.len));
367 		memcpy(c->cdce_buf + m->m_pkthdr.len, &crc, sizeof(crc));
368 		extra = sizeof(crc);
369 	}
370 	c->cdce_mbuf = m;
371 
372 	usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkout_pipe, c, c->cdce_buf,
373 	    m->m_pkthdr.len + extra, USBD_NO_COPY, 10000, cdce_txeof);
374 	err = usbd_transfer(c->cdce_xfer);
375 	if (err != USBD_IN_PROGRESS) {
376 		cdce_stop(sc);
377 		return (EIO);
378 	}
379 
380 	sc->cdce_cdata.cdce_tx_cnt++;
381 
382 	return (0);
383 }
384 
385 Static void
386 cdce_stop(struct cdce_softc *sc)
387 {
388 	usbd_status	 err;
389 	struct ifnet	*ifp = GET_IFP(sc);
390 	int		 i;
391 
392 	ifp->if_timer = 0;
393 
394 	if (sc->cdce_bulkin_pipe != NULL) {
395 		err = usbd_abort_pipe(sc->cdce_bulkin_pipe);
396 		if (err)
397 			printf("%s: abort rx pipe failed: %s\n",
398 			    device_xname(sc->cdce_dev), usbd_errstr(err));
399 		err = usbd_close_pipe(sc->cdce_bulkin_pipe);
400 		if (err)
401 			printf("%s: close rx pipe failed: %s\n",
402 			    device_xname(sc->cdce_dev), usbd_errstr(err));
403 		sc->cdce_bulkin_pipe = NULL;
404 	}
405 
406 	if (sc->cdce_bulkout_pipe != NULL) {
407 		err = usbd_abort_pipe(sc->cdce_bulkout_pipe);
408 		if (err)
409 			printf("%s: abort tx pipe failed: %s\n",
410 			    device_xname(sc->cdce_dev), usbd_errstr(err));
411 		err = usbd_close_pipe(sc->cdce_bulkout_pipe);
412 		if (err)
413 			printf("%s: close tx pipe failed: %s\n",
414 			    device_xname(sc->cdce_dev), usbd_errstr(err));
415 		sc->cdce_bulkout_pipe = NULL;
416 	}
417 
418 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
419 		if (sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf != NULL) {
420 			m_freem(sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf);
421 			sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf = NULL;
422 		}
423 		if (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer != NULL) {
424 			usbd_free_xfer(sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer);
425 			sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer = NULL;
426 		}
427 	}
428 
429 	for (i = 0; i < CDCE_TX_LIST_CNT; i++) {
430 		if (sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf != NULL) {
431 			m_freem(sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf);
432 			sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf = NULL;
433 		}
434 		if (sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer != NULL) {
435 			usbd_free_xfer(sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer);
436 			sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer = NULL;
437 		}
438 	}
439 
440 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
441 }
442 
443 Static int
444 cdce_ioctl(struct ifnet *ifp, u_long command, void *data)
445 {
446 	struct cdce_softc	*sc = ifp->if_softc;
447 	struct ifaddr		*ifa = (struct ifaddr *)data;
448 	struct ifreq		*ifr = (struct ifreq *)data;
449 	int			 s, error = 0;
450 
451 	if (sc->cdce_dying)
452 		return (EIO);
453 
454 	s = splnet();
455 
456 	switch(command) {
457 	case SIOCINITIFADDR:
458 		ifp->if_flags |= IFF_UP;
459 		cdce_init(sc);
460 		switch (ifa->ifa_addr->sa_family) {
461 #ifdef INET
462 		case AF_INET:
463 			arp_ifinit(ifp, ifa);
464 			break;
465 #endif /* INET */
466 		}
467 		break;
468 
469 	case SIOCSIFMTU:
470 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
471 			error = EINVAL;
472 		else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
473 			error = 0;
474 		break;
475 
476 	case SIOCSIFFLAGS:
477 		if ((error = ifioctl_common(ifp, command, data)) != 0)
478 			break;
479 		/* XXX re-use ether_ioctl() */
480 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
481 		case IFF_UP:
482 			cdce_init(sc);
483 			break;
484 		case IFF_RUNNING:
485 			cdce_stop(sc);
486 			break;
487 		default:
488 			break;
489 		}
490 		break;
491 
492 	default:
493 		error = ether_ioctl(ifp, command, data);
494 		break;
495 	}
496 
497 	splx(s);
498 
499 	if (error == ENETRESET)
500 		error = 0;
501 
502 	return (error);
503 }
504 
505 Static void
506 cdce_watchdog(struct ifnet *ifp)
507 {
508 	struct cdce_softc	*sc = ifp->if_softc;
509 
510 	if (sc->cdce_dying)
511 		return;
512 
513 	ifp->if_oerrors++;
514 	printf("%s: watchdog timeout\n", device_xname(sc->cdce_dev));
515 }
516 
517 Static void
518 cdce_init(void *xsc)
519 {
520 	struct cdce_softc	*sc = xsc;
521 	struct ifnet		*ifp = GET_IFP(sc);
522 	struct cdce_chain	*c;
523 	usbd_status		 err;
524 	int			 s, i;
525 
526 	if (ifp->if_flags & IFF_RUNNING)
527 		return;
528 
529 	s = splnet();
530 
531 	if (cdce_tx_list_init(sc) == ENOBUFS) {
532 		printf("%s: tx list init failed\n", device_xname(sc->cdce_dev));
533 		splx(s);
534 		return;
535 	}
536 
537 	if (cdce_rx_list_init(sc) == ENOBUFS) {
538 		printf("%s: rx list init failed\n", device_xname(sc->cdce_dev));
539 		splx(s);
540 		return;
541 	}
542 
543 	/* Maybe set multicast / broadcast here??? */
544 
545 	err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkin_no,
546 	    USBD_EXCLUSIVE_USE, &sc->cdce_bulkin_pipe);
547 	if (err) {
548 		printf("%s: open rx pipe failed: %s\n", device_xname(sc->cdce_dev),
549 		    usbd_errstr(err));
550 		splx(s);
551 		return;
552 	}
553 
554 	err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkout_no,
555 	    USBD_EXCLUSIVE_USE, &sc->cdce_bulkout_pipe);
556 	if (err) {
557 		printf("%s: open tx pipe failed: %s\n",
558 		    device_xname(sc->cdce_dev), usbd_errstr(err));
559 		splx(s);
560 		return;
561 	}
562 
563 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
564 		c = &sc->cdce_cdata.cdce_rx_chain[i];
565 		usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkin_pipe, c,
566 		    c->cdce_buf, CDCE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
567 		    USBD_NO_TIMEOUT, cdce_rxeof);
568 		usbd_transfer(c->cdce_xfer);
569 	}
570 
571 	ifp->if_flags |= IFF_RUNNING;
572 	ifp->if_flags &= ~IFF_OACTIVE;
573 
574 	splx(s);
575 }
576 
577 Static int
578 cdce_newbuf(struct cdce_softc *sc, struct cdce_chain *c, struct mbuf *m)
579 {
580 	struct mbuf	*m_new = NULL;
581 
582 	if (m == NULL) {
583 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
584 		if (m_new == NULL) {
585 			printf("%s: no memory for rx list "
586 			    "-- packet dropped!\n", device_xname(sc->cdce_dev));
587 			return (ENOBUFS);
588 		}
589 		MCLGET(m_new, M_DONTWAIT);
590 		if (!(m_new->m_flags & M_EXT)) {
591 			printf("%s: no memory for rx list "
592 			    "-- packet dropped!\n", device_xname(sc->cdce_dev));
593 			m_freem(m_new);
594 			return (ENOBUFS);
595 		}
596 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
597 	} else {
598 		m_new = m;
599 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
600 		m_new->m_data = m_new->m_ext.ext_buf;
601 	}
602 	c->cdce_mbuf = m_new;
603 	return (0);
604 }
605 
606 Static int
607 cdce_rx_list_init(struct cdce_softc *sc)
608 {
609 	struct cdce_cdata	*cd;
610 	struct cdce_chain	*c;
611 	int			 i;
612 
613 	cd = &sc->cdce_cdata;
614 	for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
615 		c = &cd->cdce_rx_chain[i];
616 		c->cdce_sc = sc;
617 		c->cdce_idx = i;
618 		if (cdce_newbuf(sc, c, NULL) == ENOBUFS)
619 			return (ENOBUFS);
620 		if (c->cdce_xfer == NULL) {
621 			c->cdce_xfer = usbd_alloc_xfer(sc->cdce_udev);
622 			if (c->cdce_xfer == NULL)
623 				return (ENOBUFS);
624 			c->cdce_buf = usbd_alloc_buffer(c->cdce_xfer, CDCE_BUFSZ);
625 			if (c->cdce_buf == NULL)
626 				return (ENOBUFS);
627 		}
628 	}
629 
630 	return (0);
631 }
632 
633 Static int
634 cdce_tx_list_init(struct cdce_softc *sc)
635 {
636 	struct cdce_cdata	*cd;
637 	struct cdce_chain	*c;
638 	int			 i;
639 
640 	cd = &sc->cdce_cdata;
641 	for (i = 0; i < CDCE_TX_LIST_CNT; i++) {
642 		c = &cd->cdce_tx_chain[i];
643 		c->cdce_sc = sc;
644 		c->cdce_idx = i;
645 		c->cdce_mbuf = NULL;
646 		if (c->cdce_xfer == NULL) {
647 			c->cdce_xfer = usbd_alloc_xfer(sc->cdce_udev);
648 			if (c->cdce_xfer == NULL)
649 				return (ENOBUFS);
650 			c->cdce_buf = usbd_alloc_buffer(c->cdce_xfer, CDCE_BUFSZ);
651 			if (c->cdce_buf == NULL)
652 				return (ENOBUFS);
653 		}
654 	}
655 
656 	return (0);
657 }
658 
659 Static void
660 cdce_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
661 {
662 	struct cdce_chain	*c = priv;
663 	struct cdce_softc	*sc = c->cdce_sc;
664 	struct ifnet		*ifp = GET_IFP(sc);
665 	struct mbuf		*m;
666 	int			 total_len = 0;
667 	int			 s;
668 
669 	if (sc->cdce_dying || !(ifp->if_flags & IFF_RUNNING))
670 		return;
671 
672 	if (status != USBD_NORMAL_COMPLETION) {
673 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
674 			return;
675 		if (sc->cdce_rxeof_errors == 0)
676 			printf("%s: usb error on rx: %s\n",
677 			    device_xname(sc->cdce_dev), usbd_errstr(status));
678 		if (status == USBD_STALLED)
679 			usbd_clear_endpoint_stall_async(sc->cdce_bulkin_pipe);
680 		DELAY(sc->cdce_rxeof_errors * 10000);
681 		sc->cdce_rxeof_errors++;
682 		goto done;
683 	}
684 
685 	sc->cdce_rxeof_errors = 0;
686 
687 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
688 	if (sc->cdce_flags & CDCE_ZAURUS)
689 		total_len -= 4;	/* Strip off CRC added by Zaurus */
690 	if (total_len <= 1)
691 		goto done;
692 
693 	m = c->cdce_mbuf;
694 	memcpy(mtod(m, char *), c->cdce_buf, total_len);
695 
696 	if (total_len < sizeof(struct ether_header)) {
697 		ifp->if_ierrors++;
698 		goto done;
699 	}
700 
701 	ifp->if_ipackets++;
702 	m->m_pkthdr.len = m->m_len = total_len;
703 	m->m_pkthdr.rcvif = ifp;
704 
705 	s = splnet();
706 
707 	if (cdce_newbuf(sc, c, NULL) == ENOBUFS) {
708 		ifp->if_ierrors++;
709 		goto done1;
710 	}
711 
712 	bpf_mtap(ifp, m);
713 
714 	(*(ifp)->if_input)((ifp), (m));
715 
716 done1:
717 	splx(s);
718 
719 done:
720 	/* Setup new transfer. */
721 	usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkin_pipe, c, c->cdce_buf,
722 	    CDCE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
723 	    cdce_rxeof);
724 	usbd_transfer(c->cdce_xfer);
725 }
726 
727 Static void
728 cdce_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
729     usbd_status status)
730 {
731 	struct cdce_chain	*c = priv;
732 	struct cdce_softc	*sc = c->cdce_sc;
733 	struct ifnet		*ifp = GET_IFP(sc);
734 	usbd_status		 err;
735 	int			 s;
736 
737 	if (sc->cdce_dying)
738 		return;
739 
740 	s = splnet();
741 
742 	ifp->if_timer = 0;
743 	ifp->if_flags &= ~IFF_OACTIVE;
744 
745 	if (status != USBD_NORMAL_COMPLETION) {
746 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
747 			splx(s);
748 			return;
749 		}
750 		ifp->if_oerrors++;
751 		printf("%s: usb error on tx: %s\n", device_xname(sc->cdce_dev),
752 		    usbd_errstr(status));
753 		if (status == USBD_STALLED)
754 			usbd_clear_endpoint_stall_async(sc->cdce_bulkout_pipe);
755 		splx(s);
756 		return;
757 	}
758 
759 	usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &err);
760 
761 	if (c->cdce_mbuf != NULL) {
762 		m_freem(c->cdce_mbuf);
763 		c->cdce_mbuf = NULL;
764 	}
765 
766 	if (err)
767 		ifp->if_oerrors++;
768 	else
769 		ifp->if_opackets++;
770 
771 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
772 		cdce_start(ifp);
773 
774 	splx(s);
775 }
776 
777 int
778 cdce_activate(device_t self, enum devact act)
779 {
780 	struct cdce_softc *sc = device_private(self);
781 
782 	switch (act) {
783 	case DVACT_DEACTIVATE:
784 		if_deactivate(GET_IFP(sc));
785 		sc->cdce_dying = 1;
786 		return 0;
787 	default:
788 		return EOPNOTSUPP;
789 	}
790 }
791