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