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