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