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