]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/if_rue.c
Note that Keyspan is also InnoSys Inc.
[FreeBSD/FreeBSD.git] / sys / dev / usb / if_rue.c
1 /*-
2  * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
3  * Copyright (c) 1997, 1998, 1999, 2000 Bill Paul <wpaul@ee.columbia.edu>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 /*-
28  * Copyright (c) 1997, 1998, 1999, 2000
29  *      Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. All advertising materials mentioning features or use of this software
40  *    must display the following acknowledgement:
41  *      This product includes software developed by Bill Paul.
42  * 4. Neither the name of the author nor the names of any co-contributors
43  *    may be used to endorse or promote products derived from this software
44  *    without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
50  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
56  * THE POSSIBILITY OF SUCH DAMAGE.
57  */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61
62 /*
63  * RealTek RTL8150 USB to fast ethernet controller driver.
64  * Datasheet is available from
65  * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/.
66  */
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/sockio.h>
71 #include <sys/mbuf.h>
72 #include <sys/malloc.h>
73 #include <sys/kernel.h>
74 #include <sys/module.h>
75 #include <sys/socket.h>
76 #include <sys/sysctl.h>
77
78 #include <net/if.h>
79 #include <net/if_arp.h>
80 #include <net/ethernet.h>
81 #include <net/if_dl.h>
82 #include <net/if_media.h>
83 #include <net/if_types.h>
84
85 #include <net/bpf.h>
86
87 #include <sys/bus.h>
88 #include <machine/bus.h>
89 #if __FreeBSD_version < 500000
90 #include <machine/clock.h>
91 #endif
92
93 #include <dev/usb/usb.h>
94 #include <dev/usb/usbdi.h>
95 #include <dev/usb/usbdi_util.h>
96 #include <dev/usb/usbdivar.h>
97 #include "usbdevs.h"
98 #include <dev/usb/usb_ethersubr.h>
99
100 #include <dev/mii/mii.h>
101 #include <dev/mii/miivar.h>
102
103 #include <dev/usb/if_ruereg.h>
104
105 /* "controller miibus0" required.  See GENERIC if you get errors here. */
106 #include "miibus_if.h"
107
108 #ifdef USB_DEBUG
109 Static int      ruedebug = 0;
110 SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue");
111 SYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RW,
112            &ruedebug, 0, "rue debug level");
113
114 #define DPRINTFN(n, x)  do { \
115                                 if (ruedebug > (n)) \
116                                         logprintf x; \
117                         } while (0);
118 #else
119 #define DPRINTFN(n, x)
120 #endif
121 #define DPRINTF(x)      DPRINTFN(0, x)
122
123 /*
124  * Various supported device vendors/products.
125  */
126
127 Static struct rue_type rue_devs[] = {
128         { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX },
129         { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100 },
130         { 0, 0 }
131 };
132
133 Static int rue_match(device_ptr_t);
134 Static int rue_attach(device_ptr_t);
135 Static int rue_detach(device_ptr_t);
136
137 Static int rue_encap(struct rue_softc *, struct mbuf *, int);
138 #ifdef RUE_INTR_PIPE
139 Static void rue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
140 #endif
141 Static void rue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
142 Static void rue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
143 Static void rue_tick(void *);
144 Static void rue_rxstart(struct ifnet *);
145 Static void rue_start(struct ifnet *);
146 Static int rue_ioctl(struct ifnet *, u_long, caddr_t);
147 Static void rue_init(void *);
148 Static void rue_stop(struct rue_softc *);
149 Static void rue_watchdog(struct ifnet *);
150 Static void rue_shutdown(device_ptr_t);
151 Static int rue_ifmedia_upd(struct ifnet *);
152 Static void rue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
153
154 Static int rue_miibus_readreg(device_ptr_t, int, int);
155 Static int rue_miibus_writereg(device_ptr_t, int, int, int);
156 Static void rue_miibus_statchg(device_ptr_t);
157
158 Static void rue_setmulti(struct rue_softc *);
159 Static void rue_reset(struct rue_softc *);
160
161 Static int rue_read_mem(struct rue_softc *, u_int16_t, void *, u_int16_t);
162 Static int rue_write_mem(struct rue_softc *, u_int16_t, void *, u_int16_t);
163 Static int rue_csr_read_1(struct rue_softc *, int);
164 Static int rue_csr_write_1(struct rue_softc *, int, u_int8_t);
165 Static int rue_csr_read_2(struct rue_softc *, int);
166 Static int rue_csr_write_2(struct rue_softc *, int, u_int16_t);
167 Static int rue_csr_write_4(struct rue_softc *, int, u_int32_t);
168
169 Static device_method_t rue_methods[] = {
170         /* Device interface */
171         DEVMETHOD(device_probe, rue_match),
172         DEVMETHOD(device_attach, rue_attach),
173         DEVMETHOD(device_detach, rue_detach),
174         DEVMETHOD(device_shutdown, rue_shutdown),
175
176         /* Bus interface */
177         DEVMETHOD(bus_print_child, bus_generic_print_child),
178         DEVMETHOD(bus_driver_added, bus_generic_driver_added),
179
180         /* MII interface */
181         DEVMETHOD(miibus_readreg, rue_miibus_readreg),
182         DEVMETHOD(miibus_writereg, rue_miibus_writereg),
183         DEVMETHOD(miibus_statchg, rue_miibus_statchg),
184
185         { 0, 0 }
186 };
187
188 Static driver_t rue_driver = {
189         "rue",
190         rue_methods,
191         sizeof(struct rue_softc)
192 };
193
194 Static devclass_t rue_devclass;
195
196 DRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, usbd_driver_load, 0);
197 DRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0);
198 MODULE_DEPEND(rue, usb, 1, 1, 1);
199 MODULE_DEPEND(rue, ether, 1, 1, 1);
200 MODULE_DEPEND(rue, miibus, 1, 1, 1);
201
202 #define RUE_SETBIT(sc, reg, x) \
203         rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
204
205 #define RUE_CLRBIT(sc, reg, x) \
206         rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
207
208 #define RUE_SETBIT_2(sc, reg, x) \
209         rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) | (x))
210
211 #define RUE_CLRBIT_2(sc, reg, x) \
212         rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) & ~(x))
213
214 Static int
215 rue_read_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
216 {
217         usb_device_request_t    req;
218         usbd_status             err;
219
220         if (sc->rue_dying)
221                 return (0);
222
223         RUE_LOCK(sc);
224
225         req.bmRequestType = UT_READ_VENDOR_DEVICE;
226         req.bRequest = UR_SET_ADDRESS;
227         USETW(req.wValue, addr);
228         USETW(req.wIndex, 0);
229         USETW(req.wLength, len);
230
231         err = usbd_do_request(sc->rue_udev, &req, buf);
232
233         RUE_UNLOCK(sc);
234
235         if (err) {
236                 printf("rue%d: control pipe read failed: %s\n",
237                        sc->rue_unit, usbd_errstr(err));
238                 return (-1);
239         }
240
241         return (0);
242 }
243
244 Static int
245 rue_write_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
246 {
247         usb_device_request_t    req;
248         usbd_status             err;
249
250         if (sc->rue_dying)
251                 return (0);
252
253         RUE_LOCK(sc);
254
255         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
256         req.bRequest = UR_SET_ADDRESS;
257         USETW(req.wValue, addr);
258         USETW(req.wIndex, 0);
259         USETW(req.wLength, len);
260
261         err = usbd_do_request(sc->rue_udev, &req, buf);
262
263         RUE_UNLOCK(sc);
264
265         if (err) {
266                 printf("rue%d: control pipe write failed: %s\n",
267                        sc->rue_unit, usbd_errstr(err));
268                 return (-1);
269         }
270
271         return (0);
272 }
273
274 Static int
275 rue_csr_read_1(struct rue_softc *sc, int reg)
276 {
277         int             err;
278         u_int8_t        val = 0;
279
280         err = rue_read_mem(sc, reg, &val, 1);
281
282         if (err)
283                 return (0);
284
285         return (val);
286 }
287
288 Static int
289 rue_csr_read_2(struct rue_softc *sc, int reg)
290 {
291         int             err;
292         u_int16_t       val = 0;
293         uWord           w;
294
295         USETW(w, val);
296         err = rue_read_mem(sc, reg, &w, 2);
297         val = UGETW(w);
298
299         if (err)
300                 return (0);
301
302         return (val);
303 }
304
305 Static int
306 rue_csr_write_1(struct rue_softc *sc, int reg, u_int8_t val)
307 {
308         int     err;
309
310         err = rue_write_mem(sc, reg, &val, 1);
311
312         if (err)
313                 return (-1);
314
315         return (0);
316 }
317
318 Static int
319 rue_csr_write_2(struct rue_softc *sc, int reg, u_int16_t val)
320 {
321         int     err;
322         uWord   w;
323
324         USETW(w, val);
325         err = rue_write_mem(sc, reg, &w, 2);
326
327         if (err)
328                 return (-1);
329
330         return (0);
331 }
332
333 Static int
334 rue_csr_write_4(struct rue_softc *sc, int reg, u_int32_t val)
335 {
336         int     err;
337         uDWord  dw;
338
339         USETDW(dw, val);
340         err = rue_write_mem(sc, reg, &dw, 4);
341
342         if (err)
343                 return (-1);
344
345         return (0);
346 }
347
348 Static int
349 rue_miibus_readreg(device_ptr_t dev, int phy, int reg)
350 {
351         struct rue_softc        *sc = USBGETSOFTC(dev);
352         int                     rval;
353         int                     ruereg;
354
355         if (phy != 0)           /* RTL8150 supports PHY == 0, only */
356                 return (0);
357
358         switch (reg) {
359         case MII_BMCR:
360                 ruereg = RUE_BMCR;
361                 break;
362         case MII_BMSR:
363                 ruereg = RUE_BMSR;
364                 break;
365         case MII_ANAR:
366                 ruereg = RUE_ANAR;
367                 break;
368         case MII_ANER:
369                 ruereg = RUE_AER;
370                 break;
371         case MII_ANLPAR:
372                 ruereg = RUE_ANLP;
373                 break;
374         case MII_PHYIDR1:
375         case MII_PHYIDR2:
376                 return (0);
377                 break;
378         default:
379                 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
380                         rval = rue_csr_read_1(sc, reg);
381                         return (rval);
382                 }
383                 printf("rue%d: bad phy register\n", sc->rue_unit);
384                 return (0);
385         }
386
387         rval = rue_csr_read_2(sc, ruereg);
388
389         return (rval);
390 }
391
392 Static int
393 rue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
394 {
395         struct rue_softc        *sc = USBGETSOFTC(dev);
396         int                     ruereg;
397
398         if (phy != 0)           /* RTL8150 supports PHY == 0, only */
399                 return (0);
400
401         switch (reg) {
402         case MII_BMCR:
403                 ruereg = RUE_BMCR;
404                 break;
405         case MII_BMSR:
406                 ruereg = RUE_BMSR;
407                 break;
408         case MII_ANAR:
409                 ruereg = RUE_ANAR;
410                 break;
411         case MII_ANER:
412                 ruereg = RUE_AER;
413                 break;
414         case MII_ANLPAR:
415                 ruereg = RUE_ANLP;
416                 break;
417         case MII_PHYIDR1:
418         case MII_PHYIDR2:
419                 return (0);
420                 break;
421         default:
422                 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
423                         rue_csr_write_1(sc, reg, data);
424                         return (0);
425                 }
426                 printf("rue%d: bad phy register\n", sc->rue_unit);
427                 return (0);
428         }
429         rue_csr_write_2(sc, ruereg, data);
430
431         return (0);
432 }
433
434 Static void
435 rue_miibus_statchg(device_ptr_t dev)
436 {
437         /*
438          * When the code below is enabled the card starts doing weird
439          * things after link going from UP to DOWN and back UP.
440          *
441          * Looks like some of register writes below messes up PHY
442          * interface.
443          *
444          * No visible regressions were found after commenting this code
445          * out, so that disable it for good.
446          */
447 #if 0
448         struct rue_softc        *sc = USBGETSOFTC(dev);
449         struct mii_data         *mii = GET_MII(sc);
450         int                     bmcr;
451
452         RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
453
454         bmcr = rue_csr_read_2(sc, RUE_BMCR);
455
456         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
457                 bmcr |= RUE_BMCR_SPD_SET;
458         else
459                 bmcr &= ~RUE_BMCR_SPD_SET;
460
461         if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
462                 bmcr |= RUE_BMCR_DUPLEX;
463         else
464                 bmcr &= ~RUE_BMCR_DUPLEX;
465
466         rue_csr_write_2(sc, RUE_BMCR, bmcr);
467
468         RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
469 #endif
470 }
471
472 /*
473  * Program the 64-bit multicast hash filter.
474  */
475
476 Static void
477 rue_setmulti(struct rue_softc *sc)
478 {
479         struct ifnet            *ifp;
480         int                     h = 0;
481         u_int32_t               hashes[2] = { 0, 0 };
482         struct ifmultiaddr      *ifma;
483         u_int32_t               rxcfg;
484         int                     mcnt = 0;
485
486         ifp = sc->rue_ifp;
487
488         rxcfg = rue_csr_read_2(sc, RUE_RCR);
489
490         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
491                 rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP);
492                 rxcfg &= ~RUE_RCR_AM;
493                 rue_csr_write_2(sc, RUE_RCR, rxcfg);
494                 rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF);
495                 rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF);
496                 return;
497         }
498
499         /* first, zot all the existing hash bits */
500         rue_csr_write_4(sc, RUE_MAR0, 0);
501         rue_csr_write_4(sc, RUE_MAR4, 0);
502
503         /* now program new ones */
504         IF_ADDR_LOCK(ifp);
505 #if __FreeBSD_version >= 500000
506         TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link)
507 #else
508         LIST_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link)
509 #endif
510         {
511                 if (ifma->ifma_addr->sa_family != AF_LINK)
512                         continue;
513                 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
514                     ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
515                 if (h < 32)
516                         hashes[0] |= (1 << h);
517                 else
518                         hashes[1] |= (1 << (h - 32));
519                 mcnt++;
520         }
521         IF_ADDR_UNLOCK(ifp);
522
523         if (mcnt)
524                 rxcfg |= RUE_RCR_AM;
525         else
526                 rxcfg &= ~RUE_RCR_AM;
527
528         rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
529
530         rue_csr_write_2(sc, RUE_RCR, rxcfg);
531         rue_csr_write_4(sc, RUE_MAR0, hashes[0]);
532         rue_csr_write_4(sc, RUE_MAR4, hashes[1]);
533 }
534
535 Static void
536 rue_reset(struct rue_softc *sc)
537 {
538         int     i;
539
540         rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
541
542         for (i = 0; i < RUE_TIMEOUT; i++) {
543                 DELAY(500);
544                 if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST))
545                         break;
546         }
547         if (i == RUE_TIMEOUT)
548                 printf("rue%d: reset never completed!\n", sc->rue_unit);
549
550         DELAY(10000);
551 }
552
553 /*
554  * Probe for a RTL8150 chip.
555  */
556
557 USB_MATCH(rue)
558 {
559         USB_MATCH_START(rue, uaa);
560         struct rue_type *t;
561
562         if (uaa->iface == NULL)
563                 return (UMATCH_NONE);
564
565         t = rue_devs;
566         while (t->rue_vid) {
567                 if (uaa->vendor == t->rue_vid &&
568                     uaa->product == t->rue_did) {
569                         return (UMATCH_VENDOR_PRODUCT);
570                 }
571                 t++;
572         }
573
574         return (UMATCH_NONE);
575 }
576
577 /*
578  * Attach the interface. Allocate softc structures, do ifmedia
579  * setup and ethernet/BPF attach.
580  */
581
582 USB_ATTACH(rue)
583 {
584         USB_ATTACH_START(rue, sc, uaa);
585         char                            *devinfo;
586         u_char                          eaddr[ETHER_ADDR_LEN];
587         struct ifnet                    *ifp;
588         usbd_interface_handle           iface;
589         usbd_status                     err;
590         usb_interface_descriptor_t      *id;
591         usb_endpoint_descriptor_t       *ed;
592         int                             i;
593         struct rue_type                 *t;
594
595         devinfo = malloc(1024, M_USBDEV, M_WAITOK);
596
597         bzero(sc, sizeof (struct rue_softc));
598         usbd_devinfo(uaa->device, 0, devinfo);
599
600         sc->rue_dev = self;
601         sc->rue_udev = uaa->device;
602         sc->rue_unit = device_get_unit(self);
603
604         if (usbd_set_config_no(sc->rue_udev, RUE_CONFIG_NO, 0)) {
605                 printf("rue%d: getting interface handle failed\n",
606                         sc->rue_unit);
607                 goto error;
608         }
609
610         err = usbd_device2interface_handle(uaa->device, RUE_IFACE_IDX, &iface);
611         if (err) {
612                 printf("rue%d: getting interface handle failed\n",
613                        sc->rue_unit);
614                 goto error;
615         }
616
617         sc->rue_iface = iface;
618
619         t = rue_devs;
620         while (t->rue_vid) {
621                 if (uaa->vendor == t->rue_vid &&
622                     uaa->product == t->rue_did) {
623                         sc->rue_info = t;
624                         break;
625                 }
626                 t++;
627         }
628
629         id = usbd_get_interface_descriptor(sc->rue_iface);
630
631         usbd_devinfo(uaa->device, 0, devinfo);
632         device_set_desc_copy(self, devinfo);
633         printf("%s: %s\n", USBDEVNAME(self), devinfo);
634
635         /* Find endpoints */
636         for (i = 0; i < id->bNumEndpoints; i++) {
637                 ed = usbd_interface2endpoint_descriptor(iface, i);
638                 if (ed == NULL) {
639                         printf("rue%d: couldn't get ep %d\n", sc->rue_unit, i);
640                         goto error;
641                 }
642                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
643                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
644                         sc->rue_ed[RUE_ENDPT_RX] = ed->bEndpointAddress;
645                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
646                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
647                         sc->rue_ed[RUE_ENDPT_TX] = ed->bEndpointAddress;
648                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
649                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
650                         sc->rue_ed[RUE_ENDPT_INTR] = ed->bEndpointAddress;
651                 }
652         }
653
654 #if __FreeBSD_version >= 500000
655         mtx_init(&sc->rue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
656                  MTX_DEF | MTX_RECURSE);
657 #endif
658         RUE_LOCK(sc);
659
660         /* Reset the adapter */
661         rue_reset(sc);
662
663         /* Get station address from the EEPROM */
664         err = rue_read_mem(sc, RUE_EEPROM_IDR0,
665                            (caddr_t)&eaddr, ETHER_ADDR_LEN);
666         if (err) {
667                 printf("rue%d: couldn't get station address\n", sc->rue_unit);
668                 goto error1;
669         }
670
671         ifp = sc->rue_ifp = if_alloc(IFT_ETHER);
672         if (ifp == NULL) {
673                 printf("rue%d: can not if_alloc()\n", sc->rue_unit);
674                 goto error1;
675         }
676         ifp->if_softc = sc;
677         if_initname(ifp, "rue", sc->rue_unit);
678         ifp->if_mtu = ETHERMTU;
679         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
680             IFF_NEEDSGIANT;
681         ifp->if_ioctl = rue_ioctl;
682         ifp->if_start = rue_start;
683         ifp->if_watchdog = rue_watchdog;
684         ifp->if_init = rue_init;
685         ifp->if_baudrate = 10000000;
686         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
687
688         /* MII setup */
689         if (mii_phy_probe(self, &sc->rue_miibus,
690                           rue_ifmedia_upd, rue_ifmedia_sts)) {
691                 printf("rue%d: MII without any PHY!\n", sc->rue_unit);
692                 goto error2;
693         }
694
695         sc->rue_qdat.ifp = ifp;
696         sc->rue_qdat.if_rxstart = rue_rxstart;
697
698         /* Call MI attach routine */
699 #if __FreeBSD_version >= 500000
700         ether_ifattach(ifp, eaddr);
701 #else
702         ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
703 #endif
704         callout_handle_init(&sc->rue_stat_ch);
705         usb_register_netisr();
706         sc->rue_dying = 0;
707
708         RUE_UNLOCK(sc);
709         free(devinfo, M_USBDEV);
710         USB_ATTACH_SUCCESS_RETURN;
711
712     error2:
713         if_free(ifp);
714     error1:
715         RUE_UNLOCK(sc);
716 #if __FreeBSD_version >= 500000
717         mtx_destroy(&sc->rue_mtx);
718 #endif
719     error:
720         free(devinfo, M_USBDEV);
721         USB_ATTACH_ERROR_RETURN;
722 }
723
724 Static int
725 rue_detach(device_ptr_t dev)
726 {
727         struct rue_softc        *sc;
728         struct ifnet            *ifp;
729
730         sc = device_get_softc(dev);
731         RUE_LOCK(sc);
732         ifp = sc->rue_ifp;
733
734         sc->rue_dying = 1;
735         untimeout(rue_tick, sc, sc->rue_stat_ch);
736 #if __FreeBSD_version >= 500000
737         ether_ifdetach(ifp);
738 #else
739         ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
740 #endif
741
742         if (sc->rue_ep[RUE_ENDPT_TX] != NULL)
743                 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
744         if (sc->rue_ep[RUE_ENDPT_RX] != NULL)
745                 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
746 #ifdef RUE_INTR_PIPE
747         if (sc->rue_ep[RUE_ENDPT_INTR] != NULL)
748                 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
749 #endif
750
751         RUE_UNLOCK(sc);
752 #if __FreeBSD_version >= 500000
753         mtx_destroy(&sc->rue_mtx);
754 #endif
755
756         return (0);
757 }
758
759 #ifdef RUE_INTR_PIPE
760 Static void
761 rue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
762 {
763         struct rue_softc        *sc = priv;
764         struct ifnet            *ifp;
765         struct rue_intrpkt      *p;
766
767         RUE_LOCK(sc);
768         ifp = sc->rue_ifp;
769
770         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
771                 RUE_UNLOCK(sc);
772                 return;
773         }
774
775         if (status != USBD_NORMAL_COMPLETION) {
776                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
777                         RUE_UNLOCK(sc);
778                         return;
779                 }
780                 printf("rue%d: usb error on intr: %s\n", sc->rue_unit,
781                         usbd_errstr(status));
782                 if (status == USBD_STALLED)
783                         usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_INTR]);
784                 RUE_UNLOCK(sc);
785                 return;
786         }
787
788         usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
789
790         ifp->if_ierrors += p->rue_rxlost_cnt;
791         ifp->if_ierrors += p->rue_crcerr_cnt;
792         ifp->if_collisions += p->rue_col_cnt;
793
794         RUE_UNLOCK(sc);
795 }
796 #endif
797
798 Static void
799 rue_rxstart(struct ifnet *ifp)
800 {
801         struct rue_softc        *sc;
802         struct ue_chain *c;
803
804         sc = ifp->if_softc;
805         RUE_LOCK(sc);
806         c = &sc->rue_cdata.ue_rx_chain[sc->rue_cdata.ue_rx_prod];
807
808         c->ue_mbuf = usb_ether_newbuf();
809         if (c->ue_mbuf == NULL) {
810                 printf("%s: no memory for rx list "
811                     "-- packet dropped!\n", USBDEVNAME(sc->rue_dev));
812                 ifp->if_ierrors++;
813                 RUE_UNLOCK(sc);
814                 return;
815         }
816
817         /* Setup new transfer. */
818         usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
819                 c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
820                 USBD_NO_TIMEOUT, rue_rxeof);
821         usbd_transfer(c->ue_xfer);
822
823         RUE_UNLOCK(sc);
824 }
825
826 /*
827  * A frame has been uploaded: pass the resulting mbuf chain up to
828  * the higher level protocols.
829  */
830
831 Static void
832 rue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
833 {
834         struct ue_chain *c = priv;
835         struct rue_softc        *sc = c->ue_sc;
836         struct mbuf             *m;
837         struct ifnet            *ifp;
838         int                     total_len = 0;
839         struct rue_rxpkt        r;
840
841         if (sc->rue_dying)
842                 return;
843         RUE_LOCK(sc);
844         ifp = sc->rue_ifp;
845
846         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
847                 RUE_UNLOCK(sc);
848                 return;
849         }
850
851         if (status != USBD_NORMAL_COMPLETION) {
852                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
853                         RUE_UNLOCK(sc);
854                         return;
855                 }
856                 if (usbd_ratecheck(&sc->rue_rx_notice))
857                         printf("rue%d: usb error on rx: %s\n", sc->rue_unit,
858                                usbd_errstr(status));
859                 if (status == USBD_STALLED)
860                         usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_RX]);
861                 goto done;
862         }
863
864         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
865
866         if (total_len <= ETHER_CRC_LEN) {
867                 ifp->if_ierrors++;
868                 goto done;
869         }
870
871         m = c->ue_mbuf;
872         bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof (r));
873
874         /* Check recieve packet was valid or not */
875         if ((r.rue_rxstat & RUE_RXSTAT_VALID) == 0) {
876                 ifp->if_ierrors++;
877                 goto done;
878         }
879
880         /* No errors; receive the packet. */
881         total_len -= ETHER_CRC_LEN;
882
883         ifp->if_ipackets++;
884         m->m_pkthdr.rcvif = (void *)&sc->rue_qdat;
885         m->m_pkthdr.len = m->m_len = total_len;
886
887         /* Put the packet on the special USB input queue. */
888         usb_ether_input(m);
889
890         RUE_UNLOCK(sc);
891         return;
892
893     done:
894         /* Setup new transfer. */
895         usbd_setup_xfer(xfer, sc->rue_ep[RUE_ENDPT_RX],
896                         c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
897                         USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
898         usbd_transfer(xfer);
899         RUE_UNLOCK(sc);
900 }
901
902 /*
903  * A frame was downloaded to the chip. It's safe for us to clean up
904  * the list buffers.
905  */
906
907 Static void
908 rue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
909 {
910         struct ue_chain *c = priv;
911         struct rue_softc        *sc = c->ue_sc;
912         struct ifnet            *ifp;
913         usbd_status             err;
914
915         RUE_LOCK(sc);
916
917         ifp = sc->rue_ifp;
918
919         if (status != USBD_NORMAL_COMPLETION) {
920                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
921                         RUE_UNLOCK(sc);
922                         return;
923                 }
924                 printf("rue%d: usb error on tx: %s\n", sc->rue_unit,
925                         usbd_errstr(status));
926                 if (status == USBD_STALLED)
927                         usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_TX]);
928                 RUE_UNLOCK(sc);
929                 return;
930         }
931
932         ifp->if_timer = 0;
933         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
934         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
935
936         if (c->ue_mbuf != NULL) {
937                 c->ue_mbuf->m_pkthdr.rcvif = ifp;
938                 usb_tx_done(c->ue_mbuf);
939                 c->ue_mbuf = NULL;
940         }
941
942         if (err)
943                 ifp->if_oerrors++;
944         else
945                 ifp->if_opackets++;
946
947         RUE_UNLOCK(sc);
948 }
949
950 Static void
951 rue_tick(void *xsc)
952 {
953         struct rue_softc        *sc = xsc;
954         struct ifnet            *ifp;
955         struct mii_data         *mii;
956
957         if (sc == NULL)
958                 return;
959
960         RUE_LOCK(sc);
961
962         ifp = sc->rue_ifp;
963         mii = GET_MII(sc);
964         if (mii == NULL) {
965                 RUE_UNLOCK(sc);
966                 return;
967         }
968
969         mii_tick(mii);
970         if (!sc->rue_link && mii->mii_media_status & IFM_ACTIVE &&
971             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
972                 sc->rue_link++;
973                 if (ifp->if_snd.ifq_head != NULL)
974                         rue_start(ifp);
975         }
976
977         sc->rue_stat_ch = timeout(rue_tick, sc, hz);
978
979         RUE_UNLOCK(sc);
980 }
981
982 Static int
983 rue_encap(struct rue_softc *sc, struct mbuf *m, int idx)
984 {
985         int                     total_len;
986         struct ue_chain *c;
987         usbd_status             err;
988
989         c = &sc->rue_cdata.ue_tx_chain[idx];
990
991         /*
992          * Copy the mbuf data into a contiguous buffer
993          */
994         m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf);
995         c->ue_mbuf = m;
996
997         total_len = m->m_pkthdr.len;
998
999         /*
1000          * This is an undocumented behavior.
1001          * RTL8150 chip doesn't send frame length smaller than
1002          * RUE_MIN_FRAMELEN (60) byte packet.
1003          */
1004         if (total_len < RUE_MIN_FRAMELEN)
1005                 total_len = RUE_MIN_FRAMELEN;
1006
1007         usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_TX],
1008                         c, c->ue_buf, total_len, USBD_FORCE_SHORT_XFER,
1009                         10000, rue_txeof);
1010
1011         /* Transmit */
1012         err = usbd_transfer(c->ue_xfer);
1013         if (err != USBD_IN_PROGRESS) {
1014                 rue_stop(sc);
1015                 return (EIO);
1016         }
1017
1018         sc->rue_cdata.ue_tx_cnt++;
1019
1020         return (0);
1021 }
1022
1023 Static void
1024 rue_start(struct ifnet *ifp)
1025 {
1026         struct rue_softc        *sc = ifp->if_softc;
1027         struct mbuf             *m_head = NULL;
1028
1029         RUE_LOCK(sc);
1030
1031         if (!sc->rue_link) {
1032                 RUE_UNLOCK(sc);
1033                 return;
1034         }
1035
1036         if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1037                 RUE_UNLOCK(sc);
1038                 return;
1039         }
1040
1041         IF_DEQUEUE(&ifp->if_snd, m_head);
1042         if (m_head == NULL) {
1043                 RUE_UNLOCK(sc);
1044                 return;
1045         }
1046
1047         if (rue_encap(sc, m_head, 0)) {
1048                 IF_PREPEND(&ifp->if_snd, m_head);
1049                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1050                 RUE_UNLOCK(sc);
1051                 return;
1052         }
1053
1054         /*
1055          * If there's a BPF listener, bounce a copy of this frame
1056          * to him.
1057          */
1058         BPF_MTAP(ifp, m_head);
1059
1060         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1061
1062         /*
1063          * Set a timeout in case the chip goes out to lunch.
1064          */
1065         ifp->if_timer = 5;
1066
1067         RUE_UNLOCK(sc);
1068 }
1069
1070 Static void
1071 rue_init(void *xsc)
1072 {
1073         struct rue_softc        *sc = xsc;
1074         struct ifnet            *ifp = sc->rue_ifp;
1075         struct mii_data         *mii = GET_MII(sc);
1076         struct ue_chain *c;
1077         usbd_status             err;
1078         int                     i;
1079         int                     rxcfg;
1080
1081         RUE_LOCK(sc);
1082
1083         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1084                 RUE_UNLOCK(sc);
1085                 return;
1086         }
1087
1088         /*
1089          * Cancel pending I/O and free all RX/TX buffers.
1090          */
1091         rue_reset(sc);
1092
1093         /* Set MAC address */
1094         rue_write_mem(sc, RUE_IDR0, IFP2ENADDR(sc->rue_ifp),
1095             ETHER_ADDR_LEN);
1096
1097         /* Init TX ring. */
1098         if (usb_ether_tx_list_init(sc, &sc->rue_cdata,
1099             sc->rue_udev) == ENOBUFS) {
1100                 printf("rue%d: tx list init failed\n", sc->rue_unit);
1101                 RUE_UNLOCK(sc);
1102                 return;
1103         }
1104
1105         /* Init RX ring. */
1106         if (usb_ether_rx_list_init(sc, &sc->rue_cdata,
1107             sc->rue_udev) == ENOBUFS) {
1108                 printf("rue%d: rx list init failed\n", sc->rue_unit);
1109                 RUE_UNLOCK(sc);
1110                 return;
1111         }
1112
1113 #ifdef RUE_INTR_PIPE
1114         sc->rue_cdata.ue_ibuf = malloc(RUE_INTR_PKTLEN, M_USBDEV, M_NOWAIT);
1115 #endif
1116
1117         /*
1118          * Set the initial TX and RX configuration.
1119          */
1120         rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
1121
1122         rxcfg = RUE_RCR_CONFIG;
1123
1124         /* Set capture broadcast bit to capture broadcast frames. */
1125         if (ifp->if_flags & IFF_BROADCAST)
1126                 rxcfg |= RUE_RCR_AB;
1127         else
1128                 rxcfg &= ~RUE_RCR_AB;
1129
1130         /* If we want promiscuous mode, set the allframes bit. */
1131         if (ifp->if_flags & IFF_PROMISC)
1132                 rxcfg |= RUE_RCR_AAP;
1133         else
1134                 rxcfg &= ~RUE_RCR_AAP;
1135
1136         rue_csr_write_2(sc, RUE_RCR, rxcfg);
1137
1138         /* Load the multicast filter. */
1139         rue_setmulti(sc);
1140
1141         /* Enable RX and TX */
1142         rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
1143
1144         mii_mediachg(mii);
1145
1146         /* Open RX and TX pipes. */
1147         err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_RX],
1148                              USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_RX]);
1149         if (err) {
1150                 printf("rue%d: open rx pipe failed: %s\n",
1151                         sc->rue_unit, usbd_errstr(err));
1152                 RUE_UNLOCK(sc);
1153                 return;
1154         }
1155         err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_TX],
1156                              USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_TX]);
1157         if (err) {
1158                 printf("rue%d: open tx pipe failed: %s\n",
1159                         sc->rue_unit, usbd_errstr(err));
1160                 RUE_UNLOCK(sc);
1161                 return;
1162         }
1163
1164 #ifdef RUE_INTR_PIPE
1165         err = usbd_open_pipe_intr(sc->rue_iface, sc->rue_ed[RUE_ENDPT_INTR],
1166                                   USBD_SHORT_XFER_OK,
1167                                   &sc->rue_ep[RUE_ENDPT_INTR], sc,
1168                                   sc->rue_cdata.ue_ibuf, RUE_INTR_PKTLEN,
1169                                   rue_intr, RUE_INTR_INTERVAL);
1170         if (err) {
1171                 printf("rue%d: open intr pipe failed: %s\n",
1172                         sc->rue_unit, usbd_errstr(err));
1173                 RUE_UNLOCK(sc);
1174                 return;
1175         }
1176 #endif
1177
1178         /* Start up the receive pipe. */
1179         for (i = 0; i < UE_RX_LIST_CNT; i++) {
1180                 c = &sc->rue_cdata.ue_rx_chain[i];
1181                 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
1182                                 c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
1183                                 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
1184                 usbd_transfer(c->ue_xfer);
1185         }
1186
1187         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1188         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1189
1190         sc->rue_stat_ch = timeout(rue_tick, sc, hz);
1191
1192         RUE_UNLOCK(sc);
1193 }
1194
1195 /*
1196  * Set media options.
1197  */
1198
1199 Static int
1200 rue_ifmedia_upd(struct ifnet *ifp)
1201 {
1202         struct rue_softc        *sc = ifp->if_softc;
1203         struct mii_data         *mii = GET_MII(sc);
1204
1205         sc->rue_link = 0;
1206         if (mii->mii_instance) {
1207                 struct mii_softc        *miisc;
1208                 LIST_FOREACH (miisc, &mii->mii_phys, mii_list)
1209                         mii_phy_reset(miisc);
1210         }
1211         mii_mediachg(mii);
1212
1213         return (0);
1214 }
1215
1216 /*
1217  * Report current media status.
1218  */
1219
1220 Static void
1221 rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1222 {
1223         struct rue_softc        *sc = ifp->if_softc;
1224         struct mii_data         *mii = GET_MII(sc);
1225
1226         mii_pollstat(mii);
1227         ifmr->ifm_active = mii->mii_media_active;
1228         ifmr->ifm_status = mii->mii_media_status;
1229 }
1230
1231 Static int
1232 rue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1233 {
1234         struct rue_softc        *sc = ifp->if_softc;
1235         struct ifreq            *ifr = (struct ifreq *)data;
1236         struct mii_data         *mii;
1237         int                     error = 0;
1238
1239         RUE_LOCK(sc);
1240
1241         switch (command) {
1242         case SIOCSIFFLAGS:
1243                 if (ifp->if_flags & IFF_UP) {
1244                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1245                             ifp->if_flags & IFF_PROMISC &&
1246                             !(sc->rue_if_flags & IFF_PROMISC)) {
1247                                 RUE_SETBIT_2(sc, RUE_RCR,
1248                                              (RUE_RCR_AAM | RUE_RCR_AAP));
1249                                 rue_setmulti(sc);
1250                         } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1251                                    !(ifp->if_flags & IFF_PROMISC) &&
1252                                    sc->rue_if_flags & IFF_PROMISC) {
1253                                 RUE_CLRBIT_2(sc, RUE_RCR,
1254                                              (RUE_RCR_AAM | RUE_RCR_AAP));
1255                                 rue_setmulti(sc);
1256                         } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1257                                 rue_init(sc);
1258                 } else {
1259                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1260                                 rue_stop(sc);
1261                 }
1262                 sc->rue_if_flags = ifp->if_flags;
1263                 error = 0;
1264                 break;
1265         case SIOCADDMULTI:
1266         case SIOCDELMULTI:
1267                 rue_setmulti(sc);
1268                 error = 0;
1269                 break;
1270         case SIOCGIFMEDIA:
1271         case SIOCSIFMEDIA:
1272                 mii = GET_MII(sc);
1273                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1274                 break;
1275         default:
1276                 error = ether_ioctl(ifp, command, data);
1277                 break;
1278         }
1279
1280         RUE_UNLOCK(sc);
1281
1282         return (error);
1283 }
1284
1285 Static void
1286 rue_watchdog(struct ifnet *ifp)
1287 {
1288         struct rue_softc        *sc = ifp->if_softc;
1289         struct ue_chain *c;
1290         usbd_status             stat;
1291
1292         RUE_LOCK(sc);
1293
1294         ifp->if_oerrors++;
1295         printf("rue%d: watchdog timeout\n", sc->rue_unit);
1296
1297         c = &sc->rue_cdata.ue_tx_chain[0];
1298         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1299         rue_txeof(c->ue_xfer, c, stat);
1300
1301         if (ifp->if_snd.ifq_head != NULL)
1302                 rue_start(ifp);
1303
1304         RUE_UNLOCK(sc);
1305 }
1306
1307 /*
1308  * Stop the adapter and free any mbufs allocated to the
1309  * RX and TX lists.
1310  */
1311
1312 Static void
1313 rue_stop(struct rue_softc *sc)
1314 {
1315         usbd_status     err;
1316         struct ifnet    *ifp;
1317
1318         RUE_LOCK(sc);
1319
1320         ifp = sc->rue_ifp;
1321         ifp->if_timer = 0;
1322
1323         rue_csr_write_1(sc, RUE_CR, 0x00);
1324         rue_reset(sc);
1325
1326         untimeout(rue_tick, sc, sc->rue_stat_ch);
1327
1328         /* Stop transfers. */
1329         if (sc->rue_ep[RUE_ENDPT_RX] != NULL) {
1330                 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1331                 if (err) {
1332                         printf("rue%d: abort rx pipe failed: %s\n",
1333                                sc->rue_unit, usbd_errstr(err));
1334                 }
1335                 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1336                 if (err) {
1337                         printf("rue%d: close rx pipe failed: %s\n",
1338                                sc->rue_unit, usbd_errstr(err));
1339                 }
1340                 sc->rue_ep[RUE_ENDPT_RX] = NULL;
1341         }
1342
1343         if (sc->rue_ep[RUE_ENDPT_TX] != NULL) {
1344                 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1345                 if (err) {
1346                         printf("rue%d: abort tx pipe failed: %s\n",
1347                                sc->rue_unit, usbd_errstr(err));
1348                 }
1349                 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1350                 if (err) {
1351                         printf("rue%d: close tx pipe failed: %s\n",
1352                                sc->rue_unit, usbd_errstr(err));
1353                 }
1354                 sc->rue_ep[RUE_ENDPT_TX] = NULL;
1355         }
1356
1357 #ifdef RUE_INTR_PIPE
1358         if (sc->rue_ep[RUE_ENDPT_INTR] != NULL) {
1359                 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1360                 if (err) {
1361                         printf("rue%d: abort intr pipe failed: %s\n",
1362                                sc->rue_unit, usbd_errstr(err));
1363                 }
1364                 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1365                 if (err) {
1366                         printf("rue%d: close intr pipe failed: %s\n",
1367                                sc->rue_unit, usbd_errstr(err));
1368                 }
1369                 sc->rue_ep[RUE_ENDPT_INTR] = NULL;
1370         }
1371 #endif
1372
1373         /* Free RX resources. */
1374         usb_ether_rx_list_free(&sc->rue_cdata);
1375         /* Free TX resources. */
1376         usb_ether_tx_list_free(&sc->rue_cdata);
1377
1378 #ifdef RUE_INTR_PIPE
1379         free(sc->rue_cdata.ue_ibuf, M_USBDEV);
1380         sc->rue_cdata.ue_ibuf = NULL;
1381 #endif
1382
1383         sc->rue_link = 0;
1384
1385         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1386
1387         RUE_UNLOCK(sc);
1388 }
1389
1390 /*
1391  * Stop all chip I/O so that the kernel's probe routines don't
1392  * get confused by errant DMAs when rebooting.
1393  */
1394
1395 Static void
1396 rue_shutdown(device_ptr_t dev)
1397 {
1398         struct rue_softc        *sc;
1399
1400         sc = device_get_softc(dev);
1401
1402         sc->rue_dying++;
1403         RUE_LOCK(sc);
1404         rue_reset(sc);
1405         rue_stop(sc);
1406         RUE_UNLOCK(sc);
1407 }