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