]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/if_rue.c
This commit was generated by cvs2svn to compensate for changes in r151497,
[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         if_free(ifp);
739 #else
740         ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
741 #endif
742
743         if (sc->rue_ep[RUE_ENDPT_TX] != NULL)
744                 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
745         if (sc->rue_ep[RUE_ENDPT_RX] != NULL)
746                 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
747 #ifdef RUE_INTR_PIPE
748         if (sc->rue_ep[RUE_ENDPT_INTR] != NULL)
749                 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
750 #endif
751
752         RUE_UNLOCK(sc);
753 #if __FreeBSD_version >= 500000
754         mtx_destroy(&sc->rue_mtx);
755 #endif
756
757         return (0);
758 }
759
760 #ifdef RUE_INTR_PIPE
761 Static void
762 rue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
763 {
764         struct rue_softc        *sc = priv;
765         struct ifnet            *ifp;
766         struct rue_intrpkt      *p;
767
768         RUE_LOCK(sc);
769         ifp = sc->rue_ifp;
770
771         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
772                 RUE_UNLOCK(sc);
773                 return;
774         }
775
776         if (status != USBD_NORMAL_COMPLETION) {
777                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
778                         RUE_UNLOCK(sc);
779                         return;
780                 }
781                 printf("rue%d: usb error on intr: %s\n", sc->rue_unit,
782                         usbd_errstr(status));
783                 if (status == USBD_STALLED)
784                         usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_INTR]);
785                 RUE_UNLOCK(sc);
786                 return;
787         }
788
789         usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
790
791         ifp->if_ierrors += p->rue_rxlost_cnt;
792         ifp->if_ierrors += p->rue_crcerr_cnt;
793         ifp->if_collisions += p->rue_col_cnt;
794
795         RUE_UNLOCK(sc);
796 }
797 #endif
798
799 Static void
800 rue_rxstart(struct ifnet *ifp)
801 {
802         struct rue_softc        *sc;
803         struct ue_chain *c;
804
805         sc = ifp->if_softc;
806         RUE_LOCK(sc);
807         c = &sc->rue_cdata.ue_rx_chain[sc->rue_cdata.ue_rx_prod];
808
809         c->ue_mbuf = usb_ether_newbuf();
810         if (c->ue_mbuf == NULL) {
811                 printf("%s: no memory for rx list "
812                     "-- packet dropped!\n", USBDEVNAME(sc->rue_dev));
813                 ifp->if_ierrors++;
814                 RUE_UNLOCK(sc);
815                 return;
816         }
817
818         /* Setup new transfer. */
819         usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
820                 c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
821                 USBD_NO_TIMEOUT, rue_rxeof);
822         usbd_transfer(c->ue_xfer);
823
824         RUE_UNLOCK(sc);
825 }
826
827 /*
828  * A frame has been uploaded: pass the resulting mbuf chain up to
829  * the higher level protocols.
830  */
831
832 Static void
833 rue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
834 {
835         struct ue_chain *c = priv;
836         struct rue_softc        *sc = c->ue_sc;
837         struct mbuf             *m;
838         struct ifnet            *ifp;
839         int                     total_len = 0;
840         struct rue_rxpkt        r;
841
842         if (sc->rue_dying)
843                 return;
844         RUE_LOCK(sc);
845         ifp = sc->rue_ifp;
846
847         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
848                 RUE_UNLOCK(sc);
849                 return;
850         }
851
852         if (status != USBD_NORMAL_COMPLETION) {
853                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
854                         RUE_UNLOCK(sc);
855                         return;
856                 }
857                 if (usbd_ratecheck(&sc->rue_rx_notice))
858                         printf("rue%d: usb error on rx: %s\n", sc->rue_unit,
859                                usbd_errstr(status));
860                 if (status == USBD_STALLED)
861                         usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_RX]);
862                 goto done;
863         }
864
865         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
866
867         if (total_len <= ETHER_CRC_LEN) {
868                 ifp->if_ierrors++;
869                 goto done;
870         }
871
872         m = c->ue_mbuf;
873         bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof (r));
874
875         /* Check recieve packet was valid or not */
876         if ((r.rue_rxstat & RUE_RXSTAT_VALID) == 0) {
877                 ifp->if_ierrors++;
878                 goto done;
879         }
880
881         /* No errors; receive the packet. */
882         total_len -= ETHER_CRC_LEN;
883
884         ifp->if_ipackets++;
885         m->m_pkthdr.rcvif = (void *)&sc->rue_qdat;
886         m->m_pkthdr.len = m->m_len = total_len;
887
888         /* Put the packet on the special USB input queue. */
889         usb_ether_input(m);
890
891         RUE_UNLOCK(sc);
892         return;
893
894     done:
895         /* Setup new transfer. */
896         usbd_setup_xfer(xfer, sc->rue_ep[RUE_ENDPT_RX],
897                         c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
898                         USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
899         usbd_transfer(xfer);
900         RUE_UNLOCK(sc);
901 }
902
903 /*
904  * A frame was downloaded to the chip. It's safe for us to clean up
905  * the list buffers.
906  */
907
908 Static void
909 rue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
910 {
911         struct ue_chain *c = priv;
912         struct rue_softc        *sc = c->ue_sc;
913         struct ifnet            *ifp;
914         usbd_status             err;
915
916         RUE_LOCK(sc);
917
918         ifp = sc->rue_ifp;
919
920         if (status != USBD_NORMAL_COMPLETION) {
921                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
922                         RUE_UNLOCK(sc);
923                         return;
924                 }
925                 printf("rue%d: usb error on tx: %s\n", sc->rue_unit,
926                         usbd_errstr(status));
927                 if (status == USBD_STALLED)
928                         usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_TX]);
929                 RUE_UNLOCK(sc);
930                 return;
931         }
932
933         ifp->if_timer = 0;
934         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
935         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
936
937         if (c->ue_mbuf != NULL) {
938                 c->ue_mbuf->m_pkthdr.rcvif = ifp;
939                 usb_tx_done(c->ue_mbuf);
940                 c->ue_mbuf = NULL;
941         }
942
943         if (err)
944                 ifp->if_oerrors++;
945         else
946                 ifp->if_opackets++;
947
948         RUE_UNLOCK(sc);
949 }
950
951 Static void
952 rue_tick(void *xsc)
953 {
954         struct rue_softc        *sc = xsc;
955         struct ifnet            *ifp;
956         struct mii_data         *mii;
957
958         if (sc == NULL)
959                 return;
960
961         RUE_LOCK(sc);
962
963         ifp = sc->rue_ifp;
964         mii = GET_MII(sc);
965         if (mii == NULL) {
966                 RUE_UNLOCK(sc);
967                 return;
968         }
969
970         mii_tick(mii);
971         if (!sc->rue_link && mii->mii_media_status & IFM_ACTIVE &&
972             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
973                 sc->rue_link++;
974                 if (ifp->if_snd.ifq_head != NULL)
975                         rue_start(ifp);
976         }
977
978         sc->rue_stat_ch = timeout(rue_tick, sc, hz);
979
980         RUE_UNLOCK(sc);
981 }
982
983 Static int
984 rue_encap(struct rue_softc *sc, struct mbuf *m, int idx)
985 {
986         int                     total_len;
987         struct ue_chain *c;
988         usbd_status             err;
989
990         c = &sc->rue_cdata.ue_tx_chain[idx];
991
992         /*
993          * Copy the mbuf data into a contiguous buffer
994          */
995         m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf);
996         c->ue_mbuf = m;
997
998         total_len = m->m_pkthdr.len;
999
1000         /*
1001          * This is an undocumented behavior.
1002          * RTL8150 chip doesn't send frame length smaller than
1003          * RUE_MIN_FRAMELEN (60) byte packet.
1004          */
1005         if (total_len < RUE_MIN_FRAMELEN)
1006                 total_len = RUE_MIN_FRAMELEN;
1007
1008         usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_TX],
1009                         c, c->ue_buf, total_len, USBD_FORCE_SHORT_XFER,
1010                         10000, rue_txeof);
1011
1012         /* Transmit */
1013         err = usbd_transfer(c->ue_xfer);
1014         if (err != USBD_IN_PROGRESS) {
1015                 rue_stop(sc);
1016                 return (EIO);
1017         }
1018
1019         sc->rue_cdata.ue_tx_cnt++;
1020
1021         return (0);
1022 }
1023
1024 Static void
1025 rue_start(struct ifnet *ifp)
1026 {
1027         struct rue_softc        *sc = ifp->if_softc;
1028         struct mbuf             *m_head = NULL;
1029
1030         RUE_LOCK(sc);
1031
1032         if (!sc->rue_link) {
1033                 RUE_UNLOCK(sc);
1034                 return;
1035         }
1036
1037         if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1038                 RUE_UNLOCK(sc);
1039                 return;
1040         }
1041
1042         IF_DEQUEUE(&ifp->if_snd, m_head);
1043         if (m_head == NULL) {
1044                 RUE_UNLOCK(sc);
1045                 return;
1046         }
1047
1048         if (rue_encap(sc, m_head, 0)) {
1049                 IF_PREPEND(&ifp->if_snd, m_head);
1050                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1051                 RUE_UNLOCK(sc);
1052                 return;
1053         }
1054
1055         /*
1056          * If there's a BPF listener, bounce a copy of this frame
1057          * to him.
1058          */
1059         BPF_MTAP(ifp, m_head);
1060
1061         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1062
1063         /*
1064          * Set a timeout in case the chip goes out to lunch.
1065          */
1066         ifp->if_timer = 5;
1067
1068         RUE_UNLOCK(sc);
1069 }
1070
1071 Static void
1072 rue_init(void *xsc)
1073 {
1074         struct rue_softc        *sc = xsc;
1075         struct ifnet            *ifp = sc->rue_ifp;
1076         struct mii_data         *mii = GET_MII(sc);
1077         struct ue_chain *c;
1078         usbd_status             err;
1079         int                     i;
1080         int                     rxcfg;
1081
1082         RUE_LOCK(sc);
1083
1084         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1085                 RUE_UNLOCK(sc);
1086                 return;
1087         }
1088
1089         /*
1090          * Cancel pending I/O and free all RX/TX buffers.
1091          */
1092         rue_reset(sc);
1093
1094         /* Set MAC address */
1095         rue_write_mem(sc, RUE_IDR0, IFP2ENADDR(sc->rue_ifp),
1096             ETHER_ADDR_LEN);
1097
1098         /* Init TX ring. */
1099         if (usb_ether_tx_list_init(sc, &sc->rue_cdata,
1100             sc->rue_udev) == ENOBUFS) {
1101                 printf("rue%d: tx list init failed\n", sc->rue_unit);
1102                 RUE_UNLOCK(sc);
1103                 return;
1104         }
1105
1106         /* Init RX ring. */
1107         if (usb_ether_rx_list_init(sc, &sc->rue_cdata,
1108             sc->rue_udev) == ENOBUFS) {
1109                 printf("rue%d: rx list init failed\n", sc->rue_unit);
1110                 RUE_UNLOCK(sc);
1111                 return;
1112         }
1113
1114 #ifdef RUE_INTR_PIPE
1115         sc->rue_cdata.ue_ibuf = malloc(RUE_INTR_PKTLEN, M_USBDEV, M_NOWAIT);
1116 #endif
1117
1118         /*
1119          * Set the initial TX and RX configuration.
1120          */
1121         rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
1122
1123         rxcfg = RUE_RCR_CONFIG;
1124
1125         /* Set capture broadcast bit to capture broadcast frames. */
1126         if (ifp->if_flags & IFF_BROADCAST)
1127                 rxcfg |= RUE_RCR_AB;
1128         else
1129                 rxcfg &= ~RUE_RCR_AB;
1130
1131         /* If we want promiscuous mode, set the allframes bit. */
1132         if (ifp->if_flags & IFF_PROMISC)
1133                 rxcfg |= RUE_RCR_AAP;
1134         else
1135                 rxcfg &= ~RUE_RCR_AAP;
1136
1137         rue_csr_write_2(sc, RUE_RCR, rxcfg);
1138
1139         /* Load the multicast filter. */
1140         rue_setmulti(sc);
1141
1142         /* Enable RX and TX */
1143         rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
1144
1145         mii_mediachg(mii);
1146
1147         /* Open RX and TX pipes. */
1148         err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_RX],
1149                              USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_RX]);
1150         if (err) {
1151                 printf("rue%d: open rx pipe failed: %s\n",
1152                         sc->rue_unit, usbd_errstr(err));
1153                 RUE_UNLOCK(sc);
1154                 return;
1155         }
1156         err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_TX],
1157                              USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_TX]);
1158         if (err) {
1159                 printf("rue%d: open tx pipe failed: %s\n",
1160                         sc->rue_unit, usbd_errstr(err));
1161                 RUE_UNLOCK(sc);
1162                 return;
1163         }
1164
1165 #ifdef RUE_INTR_PIPE
1166         err = usbd_open_pipe_intr(sc->rue_iface, sc->rue_ed[RUE_ENDPT_INTR],
1167                                   USBD_SHORT_XFER_OK,
1168                                   &sc->rue_ep[RUE_ENDPT_INTR], sc,
1169                                   sc->rue_cdata.ue_ibuf, RUE_INTR_PKTLEN,
1170                                   rue_intr, RUE_INTR_INTERVAL);
1171         if (err) {
1172                 printf("rue%d: open intr pipe failed: %s\n",
1173                         sc->rue_unit, usbd_errstr(err));
1174                 RUE_UNLOCK(sc);
1175                 return;
1176         }
1177 #endif
1178
1179         /* Start up the receive pipe. */
1180         for (i = 0; i < UE_RX_LIST_CNT; i++) {
1181                 c = &sc->rue_cdata.ue_rx_chain[i];
1182                 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
1183                                 c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
1184                                 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
1185                 usbd_transfer(c->ue_xfer);
1186         }
1187
1188         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1189         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1190
1191         sc->rue_stat_ch = timeout(rue_tick, sc, hz);
1192
1193         RUE_UNLOCK(sc);
1194 }
1195
1196 /*
1197  * Set media options.
1198  */
1199
1200 Static int
1201 rue_ifmedia_upd(struct ifnet *ifp)
1202 {
1203         struct rue_softc        *sc = ifp->if_softc;
1204         struct mii_data         *mii = GET_MII(sc);
1205
1206         sc->rue_link = 0;
1207         if (mii->mii_instance) {
1208                 struct mii_softc        *miisc;
1209                 LIST_FOREACH (miisc, &mii->mii_phys, mii_list)
1210                         mii_phy_reset(miisc);
1211         }
1212         mii_mediachg(mii);
1213
1214         return (0);
1215 }
1216
1217 /*
1218  * Report current media status.
1219  */
1220
1221 Static void
1222 rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1223 {
1224         struct rue_softc        *sc = ifp->if_softc;
1225         struct mii_data         *mii = GET_MII(sc);
1226
1227         mii_pollstat(mii);
1228         ifmr->ifm_active = mii->mii_media_active;
1229         ifmr->ifm_status = mii->mii_media_status;
1230 }
1231
1232 Static int
1233 rue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1234 {
1235         struct rue_softc        *sc = ifp->if_softc;
1236         struct ifreq            *ifr = (struct ifreq *)data;
1237         struct mii_data         *mii;
1238         int                     error = 0;
1239
1240         RUE_LOCK(sc);
1241
1242         switch (command) {
1243         case SIOCSIFFLAGS:
1244                 if (ifp->if_flags & IFF_UP) {
1245                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1246                             ifp->if_flags & IFF_PROMISC &&
1247                             !(sc->rue_if_flags & IFF_PROMISC)) {
1248                                 RUE_SETBIT_2(sc, RUE_RCR,
1249                                              (RUE_RCR_AAM | RUE_RCR_AAP));
1250                                 rue_setmulti(sc);
1251                         } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1252                                    !(ifp->if_flags & IFF_PROMISC) &&
1253                                    sc->rue_if_flags & IFF_PROMISC) {
1254                                 RUE_CLRBIT_2(sc, RUE_RCR,
1255                                              (RUE_RCR_AAM | RUE_RCR_AAP));
1256                                 rue_setmulti(sc);
1257                         } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1258                                 rue_init(sc);
1259                 } else {
1260                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1261                                 rue_stop(sc);
1262                 }
1263                 sc->rue_if_flags = ifp->if_flags;
1264                 error = 0;
1265                 break;
1266         case SIOCADDMULTI:
1267         case SIOCDELMULTI:
1268                 rue_setmulti(sc);
1269                 error = 0;
1270                 break;
1271         case SIOCGIFMEDIA:
1272         case SIOCSIFMEDIA:
1273                 mii = GET_MII(sc);
1274                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1275                 break;
1276         default:
1277                 error = ether_ioctl(ifp, command, data);
1278                 break;
1279         }
1280
1281         RUE_UNLOCK(sc);
1282
1283         return (error);
1284 }
1285
1286 Static void
1287 rue_watchdog(struct ifnet *ifp)
1288 {
1289         struct rue_softc        *sc = ifp->if_softc;
1290         struct ue_chain *c;
1291         usbd_status             stat;
1292
1293         RUE_LOCK(sc);
1294
1295         ifp->if_oerrors++;
1296         printf("rue%d: watchdog timeout\n", sc->rue_unit);
1297
1298         c = &sc->rue_cdata.ue_tx_chain[0];
1299         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1300         rue_txeof(c->ue_xfer, c, stat);
1301
1302         if (ifp->if_snd.ifq_head != NULL)
1303                 rue_start(ifp);
1304
1305         RUE_UNLOCK(sc);
1306 }
1307
1308 /*
1309  * Stop the adapter and free any mbufs allocated to the
1310  * RX and TX lists.
1311  */
1312
1313 Static void
1314 rue_stop(struct rue_softc *sc)
1315 {
1316         usbd_status     err;
1317         struct ifnet    *ifp;
1318
1319         RUE_LOCK(sc);
1320
1321         ifp = sc->rue_ifp;
1322         ifp->if_timer = 0;
1323
1324         rue_csr_write_1(sc, RUE_CR, 0x00);
1325         rue_reset(sc);
1326
1327         untimeout(rue_tick, sc, sc->rue_stat_ch);
1328
1329         /* Stop transfers. */
1330         if (sc->rue_ep[RUE_ENDPT_RX] != NULL) {
1331                 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1332                 if (err) {
1333                         printf("rue%d: abort rx pipe failed: %s\n",
1334                                sc->rue_unit, usbd_errstr(err));
1335                 }
1336                 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1337                 if (err) {
1338                         printf("rue%d: close rx pipe failed: %s\n",
1339                                sc->rue_unit, usbd_errstr(err));
1340                 }
1341                 sc->rue_ep[RUE_ENDPT_RX] = NULL;
1342         }
1343
1344         if (sc->rue_ep[RUE_ENDPT_TX] != NULL) {
1345                 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1346                 if (err) {
1347                         printf("rue%d: abort tx pipe failed: %s\n",
1348                                sc->rue_unit, usbd_errstr(err));
1349                 }
1350                 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1351                 if (err) {
1352                         printf("rue%d: close tx pipe failed: %s\n",
1353                                sc->rue_unit, usbd_errstr(err));
1354                 }
1355                 sc->rue_ep[RUE_ENDPT_TX] = NULL;
1356         }
1357
1358 #ifdef RUE_INTR_PIPE
1359         if (sc->rue_ep[RUE_ENDPT_INTR] != NULL) {
1360                 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1361                 if (err) {
1362                         printf("rue%d: abort intr pipe failed: %s\n",
1363                                sc->rue_unit, usbd_errstr(err));
1364                 }
1365                 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1366                 if (err) {
1367                         printf("rue%d: close intr pipe failed: %s\n",
1368                                sc->rue_unit, usbd_errstr(err));
1369                 }
1370                 sc->rue_ep[RUE_ENDPT_INTR] = NULL;
1371         }
1372 #endif
1373
1374         /* Free RX resources. */
1375         usb_ether_rx_list_free(&sc->rue_cdata);
1376         /* Free TX resources. */
1377         usb_ether_tx_list_free(&sc->rue_cdata);
1378
1379 #ifdef RUE_INTR_PIPE
1380         free(sc->rue_cdata.ue_ibuf, M_USBDEV);
1381         sc->rue_cdata.ue_ibuf = NULL;
1382 #endif
1383
1384         sc->rue_link = 0;
1385
1386         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1387
1388         RUE_UNLOCK(sc);
1389 }
1390
1391 /*
1392  * Stop all chip I/O so that the kernel's probe routines don't
1393  * get confused by errant DMAs when rebooting.
1394  */
1395
1396 Static void
1397 rue_shutdown(device_ptr_t dev)
1398 {
1399         struct rue_softc        *sc;
1400
1401         sc = device_get_softc(dev);
1402
1403         sc->rue_dying++;
1404         RUE_LOCK(sc);
1405         rue_reset(sc);
1406         rue_stop(sc);
1407         RUE_UNLOCK(sc);
1408 }