]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/if_cdce.c
Expand USB_DETACH, USB_ATTACH and USB_MATCH inline. No functional
[FreeBSD/FreeBSD.git] / sys / dev / usb / if_cdce.c
1 /*      $NetBSD: if_cdce.c,v 1.4 2004/10/24 12:50:54 augustss Exp $ */
2
3 /*
4  * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com>
5  * Copyright (c) 2003-2005 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 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sockio.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
52 #include <sys/socket.h>
53 #include <sys/endian.h>
54
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/ethernet.h>
58 #include <net/if_types.h>
59 #include <net/if_media.h>
60
61 #include <net/bpf.h>
62
63 #include <sys/bus.h>
64 #include <machine/bus.h>
65
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include <dev/usb/usbdivar.h>
70 #include <dev/usb/usb_ethersubr.h>
71
72 #include <dev/usb/usbcdc.h>
73 #include "usbdevs.h"
74 #include <dev/usb/if_cdcereg.h>
75
76 static device_shutdown_t cdce_shutdown;
77 USB_DECLARE_DRIVER_INIT(cdce,
78         DEVMETHOD(device_probe, cdce_match),
79         DEVMETHOD(device_attach, cdce_attach),
80         DEVMETHOD(device_detach, cdce_detach),
81         DEVMETHOD(device_shutdown, cdce_shutdown)
82         );
83 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, usbd_driver_load, 0);
84 MODULE_VERSION(cdce, 0);
85
86 static int       cdce_encap(struct cdce_softc *, struct mbuf *, int);
87 static void      cdce_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
88 static void      cdce_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
89 static void      cdce_start(struct ifnet *);
90 static int       cdce_ioctl(struct ifnet *, u_long, caddr_t);
91 static void      cdce_init(void *);
92 static void      cdce_reset(struct cdce_softc *);
93 static void      cdce_stop(struct cdce_softc *);
94 static void      cdce_rxstart(struct ifnet *);
95 static int       cdce_ifmedia_upd(struct ifnet *ifp);
96 static void      cdce_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
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_SLA300 }, 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_SLC700 }, CDCE_ZAURUS | CDCE_NO_UNION },
104   {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750 }, CDCE_ZAURUS | CDCE_NO_UNION },
105   {{ USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00 }, CDCE_NO_UNION },
106   {{ USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET }, CDCE_NO_UNION },
107   {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX }, CDCE_NO_UNION },
108 };
109 #define cdce_lookup(v, p) ((const struct cdce_type *)usb_lookup(cdce_devs, v, p))
110
111 static int
112 cdce_match(device_t self)
113 {
114         USB_MATCH_START(cdce, uaa);
115         usb_interface_descriptor_t *id;
116
117         if (uaa->iface == NULL)
118                 return (UMATCH_NONE);
119
120         id = usbd_get_interface_descriptor(uaa->iface);
121         if (id == NULL)
122                 return (UMATCH_NONE);
123
124         if (cdce_lookup(uaa->vendor, uaa->product) != NULL)
125                 return (UMATCH_VENDOR_PRODUCT);
126
127         if (id->bInterfaceClass == UICLASS_CDC && id->bInterfaceSubClass ==
128             UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL)
129                 return (UMATCH_IFACECLASS_GENERIC);
130
131         return (UMATCH_NONE);
132 }
133
134 static int
135 cdce_attach(device_t self)
136 {
137         USB_ATTACH_START(cdce, sc, uaa);
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_STRING_LEN];
150
151         sc->cdce_dev = self;
152         sc->cdce_udev = uaa->device;
153
154         t = cdce_lookup(uaa->vendor, uaa->product);
155         if (t)
156                 sc->cdce_flags = t->cdce_flags;
157
158         if (sc->cdce_flags & CDCE_NO_UNION)
159                 sc->cdce_data_iface = uaa->iface;
160         else {
161                 ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(sc->cdce_udev,
162                     UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION);
163                 if (ud == NULL) {
164                         device_printf(sc->cdce_dev, "no union descriptor\n");
165                         return ENXIO;
166                 }
167                 data_ifcno = ud->bSlaveInterface[0];
168
169                 for (i = 0; i < uaa->nifaces; i++) {
170                         if (uaa->ifaces[i] != NULL) {
171                                 id = usbd_get_interface_descriptor(
172                                     uaa->ifaces[i]);
173                                 if (id != NULL && id->bInterfaceNumber ==
174                                     data_ifcno) {
175                                         sc->cdce_data_iface = uaa->ifaces[i];
176                                         uaa->ifaces[i] = NULL;
177                                 }
178                         }
179                 }
180         }
181
182         if (sc->cdce_data_iface == NULL) {
183                 device_printf(sc->cdce_dev, "no data interface\n");
184                 return ENXIO;
185         }
186
187         /*
188          * <quote>
189          *  The Data Class interface of a networking device shall have a minimum
190          *  of two interface settings. The first setting (the default interface
191          *  setting) includes no endpoints and therefore no networking traffic is
192          *  exchanged whenever the default interface setting is selected. One or
193          *  more additional interface settings are used for normal operation, and
194          *  therefore each includes a pair of endpoints (one IN, and one OUT) to
195          *  exchange network traffic. Select an alternate interface setting to
196          *  initialize the network aspects of the device and to enable the
197          *  exchange of network traffic.
198          * </quote>
199          *
200          * Some devices, most notably cable modems, include interface settings
201          * that have no IN or OUT endpoint, therefore loop through the list of all
202          * available interface settings looking for one with both IN and OUT
203          * endpoints.
204          */
205         id = usbd_get_interface_descriptor(sc->cdce_data_iface);
206         cd = usbd_get_config_descriptor(sc->cdce_udev);
207         numalts = usbd_get_no_alts(cd, id->bInterfaceNumber);
208
209         for (j = 0; j < numalts; j++) {
210                 if (usbd_set_interface(sc->cdce_data_iface, j)) {
211                         device_printf(sc->cdce_dev,     
212                             "setting alternate interface failed\n");
213                         return ENXIO;
214                 }
215                 /* Find endpoints. */
216                 id = usbd_get_interface_descriptor(sc->cdce_data_iface);
217                 sc->cdce_bulkin_no = sc->cdce_bulkout_no = -1;
218                 for (i = 0; i < id->bNumEndpoints; i++) {
219                         ed = usbd_interface2endpoint_descriptor(sc->cdce_data_iface, i);
220                         if (!ed) {
221                                 device_printf(sc->cdce_dev,
222                                     "could not read endpoint descriptor\n");
223                                 return ENXIO;
224                         }
225                         if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
226                             UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
227                                 sc->cdce_bulkin_no = ed->bEndpointAddress;
228                         } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
229                             UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
230                                 sc->cdce_bulkout_no = ed->bEndpointAddress;
231                         } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
232                             UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
233                                 /* XXX: CDC spec defines an interrupt pipe, but it is not
234                                  * needed for simple host-to-host applications. */
235                         } else {
236                                 device_printf(sc->cdce_dev,
237                                     "unexpected endpoint\n");
238                         }
239                 }
240                 /* If we found something, try and use it... */
241                 if ((sc->cdce_bulkin_no != -1) && (sc->cdce_bulkout_no != -1))
242                         break;
243         }
244
245         if (sc->cdce_bulkin_no == -1) {
246                 device_printf(sc->cdce_dev, "could not find data bulk in\n");
247                 return ENXIO;
248         }
249         if (sc->cdce_bulkout_no == -1 ) {
250                 device_printf(sc->cdce_dev, "could not find data bulk out\n");
251                 return ENXIO;
252         }
253
254         mtx_init(&sc->cdce_mtx, device_get_nameunit(sc->cdce_dev), MTX_NETWORK_LOCK,
255             MTX_DEF | MTX_RECURSE);
256         ifmedia_init(&sc->cdce_ifmedia, 0, cdce_ifmedia_upd, cdce_ifmedia_sts);
257         CDCE_LOCK(sc);
258
259         ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc(dev,
260             UDESC_INTERFACE, UDESCSUB_CDC_ENF);
261         if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str)) {
262                 /* Fake MAC address */
263                 device_printf(sc->cdce_dev, "faking MAC address\n");
264                 eaddr[0]= 0x2a;
265                 memcpy(&eaddr[1], &ticks, sizeof(u_int32_t));
266                 eaddr[5] = (u_int8_t)device_get_unit(sc->cdce_dev);
267         } else {
268                 int i;
269
270                 memset(eaddr, 0, ETHER_ADDR_LEN);
271                 for (i = 0; i < ETHER_ADDR_LEN * 2; i++) {
272                         int c = eaddr_str[i];
273
274                         if ('0' <= c && c <= '9')
275                                 c -= '0';
276                         else
277                                 c -= 'A' - 10;
278                         c &= 0xf;
279                         if (c % 2 == 0)
280                                 c <<= 4;
281                         eaddr[i / 2] |= c;
282                 }
283         }
284
285         ifp = GET_IFP(sc) = if_alloc(IFT_ETHER);
286         if (ifp == NULL) {
287                 device_printf(sc->cdce_dev, "can not if_alloc()\n");
288                 CDCE_UNLOCK(sc);
289                 mtx_destroy(&sc->cdce_mtx);
290                 return ENXIO;
291         }
292         ifp->if_softc = sc;
293         if_initname(ifp, "cdce", device_get_unit(sc->cdce_dev));
294         ifp->if_mtu = ETHERMTU;
295         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
296                 IFF_NEEDSGIANT;
297         ifp->if_ioctl = cdce_ioctl;
298         ifp->if_output = ether_output;
299         ifp->if_start = cdce_start;
300         ifp->if_init = cdce_init;
301         ifp->if_baudrate = 11000000;
302         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
303
304         sc->q.ifp = ifp;
305         sc->q.if_rxstart = cdce_rxstart;
306
307         /* No IFM type for 11Mbps USB, so go with 10baseT */
308         ifmedia_add(&sc->cdce_ifmedia, IFM_ETHER | IFM_10_T, 0, 0);
309         ifmedia_set(&sc->cdce_ifmedia, IFM_ETHER | IFM_10_T);
310
311         ether_ifattach(ifp, eaddr);
312         usb_register_netisr();
313
314         CDCE_UNLOCK(sc);
315
316         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cdce_udev,
317           sc->cdce_dev);
318
319         return 0;
320 }
321
322 static int
323 cdce_detach(device_t self)
324 {
325         USB_DETACH_START(cdce, sc);
326         struct ifnet    *ifp;
327
328         CDCE_LOCK(sc);
329         sc->cdce_dying = 1;
330         ifp = GET_IFP(sc);
331         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
332                 cdce_shutdown(sc->cdce_dev);
333
334         ether_ifdetach(ifp);
335         if_free(ifp);
336         ifmedia_removeall(&sc->cdce_ifmedia);
337         CDCE_UNLOCK(sc);
338         mtx_destroy(&sc->cdce_mtx);
339
340         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cdce_udev,
341           sc->cdce_dev);
342
343         return (0);
344 }
345
346 static void
347 cdce_start(struct ifnet *ifp)
348 {
349         struct cdce_softc       *sc;
350         struct mbuf             *m_head = NULL;
351
352         sc = ifp->if_softc;
353         CDCE_LOCK(sc);
354
355
356         if (sc->cdce_dying ||
357                 ifp->if_drv_flags & IFF_DRV_OACTIVE ||
358                 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
359                 CDCE_UNLOCK(sc);
360                 return;
361         }
362
363         IF_DEQUEUE(&ifp->if_snd, m_head);
364         if (m_head == NULL) {
365                 CDCE_UNLOCK(sc);
366                 return;
367         }
368
369         if (cdce_encap(sc, m_head, 0)) {
370                 IF_PREPEND(&ifp->if_snd, m_head);
371                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
372                 CDCE_UNLOCK(sc);
373                 return;
374         }
375
376         BPF_MTAP(ifp, m_head);
377
378         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
379
380         CDCE_UNLOCK(sc);
381
382         return;
383 }
384
385 static int
386 cdce_encap(struct cdce_softc *sc, struct mbuf *m, int idx)
387 {
388         struct ue_chain *c;
389         usbd_status              err;
390         int                      extra = 0;
391
392         c = &sc->cdce_cdata.ue_tx_chain[idx];
393
394         m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf);
395         if (sc->cdce_flags & CDCE_ZAURUS) {
396                 /* Zaurus wants a 32-bit CRC appended to every frame */
397                 u_int32_t crc;
398
399                 crc = htole32(crc32(c->ue_buf, m->m_pkthdr.len));
400                 bcopy(&crc, c->ue_buf + m->m_pkthdr.len, 4);
401                 extra = 4;
402         }
403         c->ue_mbuf = m;
404
405         usbd_setup_xfer(c->ue_xfer, sc->cdce_bulkout_pipe, c, c->ue_buf,
406             m->m_pkthdr.len + extra, 0, 10000, cdce_txeof);
407         err = usbd_transfer(c->ue_xfer);
408         if (err != USBD_IN_PROGRESS) {
409                 cdce_stop(sc);
410                 return (EIO);
411         }
412
413         sc->cdce_cdata.ue_tx_cnt++;
414
415         return (0);
416 }
417
418 static void
419 cdce_stop(struct cdce_softc *sc)
420 {
421         usbd_status      err;
422         struct ifnet    *ifp;
423
424         CDCE_LOCK(sc);
425
426         cdce_reset(sc);
427
428         ifp = GET_IFP(sc);
429         ifp->if_timer = 0;
430
431         if (sc->cdce_bulkin_pipe != NULL) {
432                 err = usbd_abort_pipe(sc->cdce_bulkin_pipe);
433                 if (err)
434                         device_printf(sc->cdce_dev,
435                             "abort rx pipe failed: %s\n", usbd_errstr(err));
436                 err = usbd_close_pipe(sc->cdce_bulkin_pipe);
437                 if (err)
438                         device_printf(sc->cdce_dev,
439                             "close rx pipe failed: %s\n", usbd_errstr(err));
440                 sc->cdce_bulkin_pipe = NULL;
441         }
442
443         if (sc->cdce_bulkout_pipe != NULL) {
444                 err = usbd_abort_pipe(sc->cdce_bulkout_pipe);
445                 if (err)
446                         device_printf(sc->cdce_dev,
447                             "abort tx pipe failed: %s\n", usbd_errstr(err));
448                 err = usbd_close_pipe(sc->cdce_bulkout_pipe);
449                 if (err)
450                         device_printf(sc->cdce_dev,
451                             "close tx pipe failed: %s\n", usbd_errstr(err));
452                 sc->cdce_bulkout_pipe = NULL;
453         }
454
455         usb_ether_rx_list_free(&sc->cdce_cdata);
456         usb_ether_tx_list_free(&sc->cdce_cdata);
457
458         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
459         CDCE_UNLOCK(sc);
460
461         return;
462 }
463
464 static int
465 cdce_shutdown(device_t dev)
466 {
467         struct cdce_softc *sc;
468
469         sc = device_get_softc(dev);
470         cdce_stop(sc);
471
472         return (0);
473 }
474
475 static int
476 cdce_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
477 {
478         struct cdce_softc       *sc = ifp->if_softc;
479         struct ifreq            *ifr = (struct ifreq *)data;
480         int                      error = 0;
481
482         if (sc->cdce_dying)
483                 return (ENXIO);
484
485         switch(command) {
486         case SIOCSIFFLAGS:
487                 if (ifp->if_flags & IFF_UP) {
488                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
489                                 cdce_init(sc);
490                 } else {
491                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
492                                 cdce_stop(sc);
493                 }
494                 error = 0;
495                 break;
496
497         case SIOCSIFMEDIA:
498         case SIOCGIFMEDIA:
499                 error = ifmedia_ioctl(ifp, ifr, &sc->cdce_ifmedia, command);
500                 break;
501
502         default:
503                 error = ether_ioctl(ifp, command, data);
504                 break;
505         }
506
507         return (error);
508 }
509
510 static void
511 cdce_reset(struct cdce_softc *sc)
512 {
513         /* XXX Maybe reset the bulk pipes here? */
514         return;
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 ue_chain *c;
523         usbd_status              err;
524         int                      i;
525
526         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
527                 return;
528
529         CDCE_LOCK(sc);
530         cdce_reset(sc);
531
532         if (usb_ether_tx_list_init(sc, &sc->cdce_cdata,
533             sc->cdce_udev) == ENOBUFS) {
534                 device_printf(sc->cdce_dev, "tx list init failed\n");
535                 CDCE_UNLOCK(sc);
536                 return;
537         }
538
539         if (usb_ether_rx_list_init(sc, &sc->cdce_cdata,
540             sc->cdce_udev) == ENOBUFS) {
541                 device_printf(sc->cdce_dev, "rx list init failed\n");
542                 CDCE_UNLOCK(sc);
543                 return;
544         }
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                 device_printf(sc->cdce_dev, "open rx pipe failed: %s\n",
552                     usbd_errstr(err));
553                 CDCE_UNLOCK(sc);
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                 device_printf(sc->cdce_dev, "open tx pipe failed: %s\n",
561                     usbd_errstr(err));
562                 CDCE_UNLOCK(sc);
563                 return;
564         }
565
566         for (i = 0; i < UE_RX_LIST_CNT; i++) {
567                 c = &sc->cdce_cdata.ue_rx_chain[i];
568                 usbd_setup_xfer(c->ue_xfer, sc->cdce_bulkin_pipe, c,
569                     mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
570                     USBD_NO_TIMEOUT, cdce_rxeof);
571                 usbd_transfer(c->ue_xfer);
572         }
573
574         ifp->if_drv_flags |= IFF_DRV_RUNNING;
575         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
576
577         CDCE_UNLOCK(sc);
578
579         return;
580 }
581
582 static void
583 cdce_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
584 {
585         struct ue_chain *c = priv;
586         struct cdce_softc       *sc = c->ue_sc;
587         struct ifnet            *ifp;
588         struct mbuf             *m;
589         int                      total_len = 0;
590
591         CDCE_LOCK(sc);
592         ifp = GET_IFP(sc);
593
594         if (sc->cdce_dying || !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
595                 CDCE_UNLOCK(sc);
596                 return;
597         }
598
599         if (status != USBD_NORMAL_COMPLETION) {
600                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
601                         CDCE_UNLOCK(sc);
602                         return;
603                 }
604                 if (sc->cdce_rxeof_errors == 0)
605                         device_printf(sc->cdce_dev, "usb error on rx: %s\n",
606                             usbd_errstr(status));
607                 if (status == USBD_STALLED)
608                         usbd_clear_endpoint_stall_async(sc->cdce_bulkin_pipe);
609                 DELAY(sc->cdce_rxeof_errors * 10000);
610                 sc->cdce_rxeof_errors++;
611                 goto done;
612         }
613
614         sc->cdce_rxeof_errors = 0;
615
616         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
617
618         if (sc->cdce_flags & CDCE_ZAURUS)
619                 total_len -= 4; /* Strip off CRC added by Zaurus */
620
621         m = c->ue_mbuf;
622
623         if (total_len < sizeof(struct ether_header)) {
624                 ifp->if_ierrors++;
625                 goto done;
626         }
627
628         ifp->if_ipackets++;
629         m->m_pkthdr.rcvif = (struct ifnet *)&sc->q;
630         m->m_pkthdr.len = m->m_len = total_len;
631
632         /* Put the packet on the special USB input queue. */
633         usb_ether_input(m);
634         CDCE_UNLOCK(sc);
635
636         return;
637
638 done:
639         /* Setup new transfer. */
640         usbd_setup_xfer(c->ue_xfer, sc->cdce_bulkin_pipe, c,
641             mtod(c->ue_mbuf, char *),
642             UE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
643             cdce_rxeof);
644         usbd_transfer(c->ue_xfer);
645         CDCE_UNLOCK(sc);
646
647         return;
648 }
649
650 static void
651 cdce_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
652 {
653         struct ue_chain *c = priv;
654         struct cdce_softc       *sc = c->ue_sc;
655         struct ifnet            *ifp;
656         usbd_status              err;
657
658         CDCE_LOCK(sc);
659         ifp = GET_IFP(sc);
660
661         if (sc->cdce_dying ||
662                 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
663                 CDCE_UNLOCK(sc);
664                 return;
665         }
666
667         if (status != USBD_NORMAL_COMPLETION) {
668                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
669                         CDCE_UNLOCK(sc);
670                         return;
671                 }
672                 ifp->if_oerrors++;
673                 device_printf(sc->cdce_dev, "usb error on tx: %s\n",
674                     usbd_errstr(status));
675                 if (status == USBD_STALLED)
676                         usbd_clear_endpoint_stall_async(sc->cdce_bulkout_pipe);
677                 CDCE_UNLOCK(sc);
678                 return;
679         }
680
681         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
682         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
683
684         if (c->ue_mbuf != NULL) {
685                 c->ue_mbuf->m_pkthdr.rcvif = ifp;
686                 usb_tx_done(c->ue_mbuf);
687                 c->ue_mbuf = NULL;
688         }
689
690         if (err)
691                 ifp->if_oerrors++;
692         else
693                 ifp->if_opackets++;
694
695         CDCE_UNLOCK(sc);
696
697         return;
698 }
699
700 static void
701 cdce_rxstart(struct ifnet *ifp)
702 {
703         struct cdce_softc   *sc;
704         struct ue_chain   *c;
705
706         sc = ifp->if_softc;
707         CDCE_LOCK(sc);
708
709         if (sc->cdce_dying || !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
710                 CDCE_UNLOCK(sc);
711                 return;
712         }
713
714         c = &sc->cdce_cdata.ue_rx_chain[sc->cdce_cdata.ue_rx_prod];
715
716         c->ue_mbuf = usb_ether_newbuf();
717         if (c->ue_mbuf == NULL) {
718                 device_printf(sc->cdce_dev, "no memory for rx list "
719                     "-- packet dropped!\n");
720                 ifp->if_ierrors++;
721                 CDCE_UNLOCK(sc);
722                 return;
723         }
724
725         usbd_setup_xfer(c->ue_xfer, sc->cdce_bulkin_pipe, c,
726             mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
727             USBD_NO_TIMEOUT, cdce_rxeof);
728         usbd_transfer(c->ue_xfer);
729
730         CDCE_UNLOCK(sc);
731         return;
732 }
733
734 static int
735 cdce_ifmedia_upd(struct ifnet *ifp)
736 {
737
738         /* no-op, cdce has only 1 possible media type */
739         return 0;
740 }
741
742 static void
743 cdce_ifmedia_sts(struct ifnet * const ifp, struct ifmediareq *req)
744 {
745
746         req->ifm_status = IFM_AVALID | IFM_ACTIVE;
747         req->ifm_active = IFM_ETHER | IFM_10_T;
748 }