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