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