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