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