linprocfs - Introduce /proc/mounts
[dragonfly.git] / sys / dev / netif / rue / if_rue.c
blobe731a60b7a6483e17784e8bf79436e468b095540
1 /*-
2 * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
3 * All rights reserved.
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.
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.
26 /*-
27 * Copyright (c) 1997, 1998, 1999, 2000
28 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
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.
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.
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.13 2008/05/14 11:59:21 sephe Exp $
62 * RealTek RTL8150 USB to fast ethernet controller driver.
63 * Datasheet is available from
64 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/.
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 #include <sys/bus.h>
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>
85 #include <net/bpf.h>
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>
91 #include <bus/usb/usb_ethersubr.h>
93 #include <dev/netif/mii_layer/mii.h>
94 #include <dev/netif/mii_layer/miivar.h>
96 #include <dev/netif/rue/if_ruereg.h>
98 #include "miibus_if.h"
100 #ifdef RUE_DEBUG
101 SYSCTL_NODE(_hw, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue");
103 static int rue_debug = 0;
104 SYSCTL_INT(_hw_rue, OID_AUTO, debug, CTLFLAG_RW, &rue_debug, 0,
105 "rue debug level");
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)
115 * Various supported device vendors/products.
118 static const struct usb_devno rue_devs[] = {
119 { USB_DEVICE(0x0411, 0x0012) }, /* Melco LUA-KTX*/
120 { USB_DEVICE(0x0bda, 0x8150) }, /* Realtek USBKR100 (GREEN HOUSE) */
123 static int rue_match(device_t);
124 static int rue_attach(device_t);
125 static int rue_detach(device_t);
127 static int rue_tx_list_init(struct rue_softc *);
128 static int rue_rx_list_init(struct rue_softc *);
129 static int rue_newbuf(struct rue_softc *, struct rue_chain *, struct mbuf *);
130 static int rue_encap(struct rue_softc *, struct mbuf *, int);
131 #ifdef RUE_INTR_PIPE
132 static void rue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
133 #endif
134 static void rue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
135 static void rue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
136 static void rue_tick(void *);
137 static void rue_rxstart(struct ifnet *);
138 static void rue_start(struct ifnet *);
139 static int rue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
140 static void rue_init(void *);
141 static void rue_stop(struct rue_softc *);
142 static void rue_watchdog(struct ifnet *);
143 static void rue_shutdown(device_t);
144 static int rue_ifmedia_upd(struct ifnet *);
145 static void rue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
147 static int rue_miibus_readreg(device_t, int, int);
148 static int rue_miibus_writereg(device_t, int, int, int);
149 static void rue_miibus_statchg(device_t);
151 static void rue_setmulti(struct rue_softc *);
152 static void rue_reset(struct rue_softc *);
154 static int rue_read_mem(struct rue_softc *, u_int16_t, void *, u_int16_t);
155 static int rue_write_mem(struct rue_softc *, u_int16_t, void *, u_int16_t);
156 static int rue_csr_read_1(struct rue_softc *, int);
157 static int rue_csr_write_1(struct rue_softc *, int, u_int8_t);
158 static int rue_csr_read_2(struct rue_softc *, int);
159 static int rue_csr_write_2(struct rue_softc *, int, u_int16_t);
160 static int rue_csr_write_4(struct rue_softc *, int, u_int32_t);
162 static device_method_t rue_methods[] = {
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),
169 /* Bus interface */
170 DEVMETHOD(bus_print_child, bus_generic_print_child),
171 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
173 /* MII interface */
174 DEVMETHOD(miibus_readreg, rue_miibus_readreg),
175 DEVMETHOD(miibus_writereg, rue_miibus_writereg),
176 DEVMETHOD(miibus_statchg, rue_miibus_statchg),
178 { 0, 0 }
181 static driver_t rue_driver = {
182 "rue",
183 rue_methods,
184 sizeof(struct rue_softc)
187 static devclass_t rue_devclass;
189 DRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, usbd_driver_load, 0);
190 DRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0);
191 MODULE_DEPEND(rue, usb, 1, 1, 1);
192 MODULE_DEPEND(rue, miibus, 1, 1, 1);
194 #define RUE_SETBIT(sc, reg, x) \
195 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
197 #define RUE_CLRBIT(sc, reg, x) \
198 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
200 #define RUE_SETBIT_2(sc, reg, x) \
201 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) | (x))
203 #define RUE_CLRBIT_2(sc, reg, x) \
204 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) & ~(x))
206 static int
207 rue_read_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
209 usb_device_request_t req;
210 usbd_status err;
212 if (sc->rue_dying)
213 return (0);
215 RUE_LOCK(sc);
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);
223 err = usbd_do_request(sc->rue_udev, &req, buf);
225 RUE_UNLOCK(sc);
227 if (err) {
228 if_printf(&sc->arpcom.ac_if, "control pipe read failed: %s\n",
229 usbd_errstr(err));
230 return (-1);
233 return (0);
236 static int
237 rue_write_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
239 usb_device_request_t req;
240 usbd_status err;
242 if (sc->rue_dying)
243 return (0);
245 RUE_LOCK(sc);
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);
253 err = usbd_do_request(sc->rue_udev, &req, buf);
255 RUE_UNLOCK(sc);
257 if (err) {
258 if_printf(&sc->arpcom.ac_if, "control pipe write failed: %s\n",
259 usbd_errstr(err));
260 return (-1);
263 return (0);
266 static int
267 rue_csr_read_1(struct rue_softc *sc, int reg)
269 int err;
270 u_int8_t val = 0;
272 err = rue_read_mem(sc, reg, &val, 1);
274 if (err)
275 return (0);
277 return (val);
280 static int
281 rue_csr_read_2(struct rue_softc *sc, int reg)
283 int err;
284 u_int16_t val = 0;
285 uWord w;
287 USETW(w, val);
288 err = rue_read_mem(sc, reg, &w, 2);
289 val = UGETW(w);
291 if (err)
292 return (0);
294 return (val);
297 static int
298 rue_csr_write_1(struct rue_softc *sc, int reg, u_int8_t val)
300 int err;
302 err = rue_write_mem(sc, reg, &val, 1);
304 if (err)
305 return (-1);
307 return (0);
310 static int
311 rue_csr_write_2(struct rue_softc *sc, int reg, u_int16_t val)
313 int err;
314 uWord w;
316 USETW(w, val);
317 err = rue_write_mem(sc, reg, &w, 2);
319 if (err)
320 return (-1);
322 return (0);
325 static int
326 rue_csr_write_4(struct rue_softc *sc, int reg, u_int32_t val)
328 int err;
329 uDWord dw;
331 USETDW(dw, val);
332 err = rue_write_mem(sc, reg, &dw, 4);
334 if (err)
335 return (-1);
337 return (0);
340 static int
341 rue_miibus_readreg(device_t dev, int phy, int reg)
343 struct rue_softc *sc = device_get_softc(dev);
344 int rval;
345 int ruereg;
347 if (phy != 0) /* RTL8150 supports PHY == 0, only */
348 return (0);
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);
375 if_printf(&sc->arpcom.ac_if, "bad phy register\n");
376 return (0);
379 rval = rue_csr_read_2(sc, ruereg);
381 return (rval);
384 static int
385 rue_miibus_writereg(device_t dev, int phy, int reg, int data)
387 struct rue_softc *sc = device_get_softc(dev);
388 int ruereg;
390 if (phy != 0) /* RTL8150 supports PHY == 0, only */
391 return (0);
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);
418 if_printf(&sc->arpcom.ac_if, "bad phy register\n");
419 return (0);
421 rue_csr_write_2(sc, ruereg, data);
423 return (0);
426 static void
427 rue_miibus_statchg(device_t dev)
432 * Program the 64-bit multicast hash filter.
435 static void
436 rue_setmulti(struct rue_softc *sc)
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;
445 ifp = &sc->arpcom.ac_if;
447 rxcfg = rue_csr_read_2(sc, RUE_RCR);
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;
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);
462 /* now program new ones */
463 LIST_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link) {
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++;
475 if (mcnt)
476 rxcfg |= RUE_RCR_AM;
477 else
478 rxcfg &= ~RUE_RCR_AM;
480 rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
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]);
487 static void
488 rue_reset(struct rue_softc *sc)
490 int i;
492 rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
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;
499 if (i == RUE_TIMEOUT)
500 if_printf(&sc->arpcom.ac_if, "reset never completed!\n");
502 DELAY(10000);
506 * Probe for a RTL8150 chip.
509 static int
510 rue_match(device_t self)
512 struct usb_attach_arg *uaa = device_get_ivars(self);
514 if (uaa->iface == NULL)
515 return (UMATCH_NONE);
517 return (usb_lookup(rue_devs, uaa->vendor, uaa->product) != NULL ?
518 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
522 * Attach the interface. Allocate softc structures, do ifmedia
523 * setup and ethernet/BPF attach.
526 static int
527 rue_attach(device_t self)
529 struct rue_softc *sc = device_get_softc(self);
530 struct usb_attach_arg *uaa = device_get_ivars(self);
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;
537 sc->rue_udev = uaa->device;
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);
542 return ENXIO;
545 if (usbd_device2interface_handle(uaa->device, RUE_IFACE_IDX,
546 &sc->rue_iface)) {
547 device_printf(self, "getting interface handle failed\n");
548 return ENXIO;
551 id = usbd_get_interface_descriptor(sc->rue_iface);
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);
558 return ENXIO;
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;
572 ifp = &sc->arpcom.ac_if;
573 if_initname(ifp, device_get_name(self), device_get_unit(self));
575 /* Reset the adapter */
576 rue_reset(sc);
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");
581 return ENXIO;
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);
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");
599 return ENXIO;
602 /* Call MI attach routine */
603 ether_ifattach(ifp, eaddr, NULL);
605 callout_init(&sc->rue_stat_ch);
606 sc->rue_dying = 0;
608 usb_register_netisr();
610 return 0;
613 static int
614 rue_detach(device_t dev)
616 struct rue_softc *sc;
617 struct ifnet *ifp;
619 sc = device_get_softc(dev);
620 RUE_LOCK(sc);
621 ifp = &sc->arpcom.ac_if;
623 sc->rue_dying = 1;
624 callout_stop(&sc->rue_stat_ch);
626 ether_ifdetach(ifp);
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
637 RUE_UNLOCK(sc);
639 return (0);
643 * Initialize an RX descriptor and attach an MBUF cluster.
646 static int
647 rue_newbuf(struct rue_softc *sc, struct rue_chain *c, struct mbuf *m)
649 struct mbuf *m_new = NULL;
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);
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;
665 m_adj(m_new, ETHER_ALIGN);
666 c->rue_mbuf = m_new;
668 return (0);
671 static int
672 rue_rx_list_init(struct rue_softc *sc)
674 struct rue_cdata *cd;
675 struct rue_chain *c;
676 int i;
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);
692 return (0);
695 static int
696 rue_tx_list_init(struct rue_softc *sc)
698 struct rue_cdata *cd;
699 struct rue_chain *c;
700 int i;
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);
713 c->rue_buf = kmalloc(RUE_BUFSZ, M_USBDEV, M_WAITOK);
716 return (0);
719 #ifdef RUE_INTR_PIPE
720 static void
721 rue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
723 struct rue_softc *sc = priv;
724 struct ifnet *ifp;
725 struct rue_intrpkt *p;
727 RUE_LOCK(sc);
728 ifp = &sc->arpcom.ac_if;
730 if (!(ifp->if_flags & IFF_RUNNING)) {
731 RUE_UNLOCK(sc);
732 return;
735 if (status != USBD_NORMAL_COMPLETION) {
736 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
737 RUE_UNLOCK(sc);
738 return;
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;
747 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
749 ifp->if_ierrors += p->rue_rxlost_cnt;
750 ifp->if_ierrors += p->rue_crcerr_cnt;
751 ifp->if_collisions += p->rue_col_cnt;
753 RUE_UNLOCK(sc);
755 #endif
757 static void
758 rue_rxstart(struct ifnet *ifp)
760 struct rue_softc *sc;
761 struct rue_chain *c;
763 sc = ifp->if_softc;
764 RUE_LOCK(sc);
765 c = &sc->rue_cdata.rue_rx_chain[sc->rue_cdata.rue_rx_prod];
767 if (rue_newbuf(sc, c, NULL) == ENOBUFS) {
768 ifp->if_ierrors++;
769 RUE_UNLOCK(sc);
770 return;
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);
779 RUE_UNLOCK(sc);
783 * A frame has been uploaded: pass the resulting mbuf chain up to
784 * the higher level protocols.
787 static void
788 rue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
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;
797 if (sc->rue_dying)
798 return;
799 RUE_LOCK(sc);
800 ifp = &sc->arpcom.ac_if;
802 if (!(ifp->if_flags & IFF_RUNNING)) {
803 RUE_UNLOCK(sc);
804 return;
807 if (status != USBD_NORMAL_COMPLETION) {
808 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
809 RUE_UNLOCK(sc);
810 return;
812 if (usbd_ratecheck(&sc->rue_rx_notice)) {
813 if_printf(ifp, "usb error on rx: %s\n",
814 usbd_errstr(status));
816 if (status == USBD_STALLED)
817 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_RX]);
818 goto done;
821 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
823 if (total_len <= ETHER_CRC_LEN) {
824 ifp->if_ierrors++;
825 goto done;
828 m = c->rue_mbuf;
829 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof (r));
831 /* Check recieve packet was valid or not */
832 if ((r.rue_rxstat & RUE_RXSTAT_VALID) == 0) {
833 ifp->if_ierrors++;
834 goto done;
837 /* No errors; receive the packet. */
838 total_len -= ETHER_CRC_LEN;
840 ifp->if_ipackets++;
841 m->m_pkthdr.rcvif = ifp;
842 m->m_pkthdr.len = m->m_len = total_len;
844 /* Put the packet on the special USB input queue. */
845 usb_ether_input(m);
846 rue_rxstart(ifp);
848 RUE_UNLOCK(sc);
849 return;
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);
861 * A frame was downloaded to the chip. It's safe for us to clean up
862 * the list buffers.
865 static void
866 rue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
868 struct rue_chain *c = priv;
869 struct rue_softc *sc = c->rue_sc;
870 struct ifnet *ifp;
871 usbd_status err;
873 RUE_LOCK(sc);
875 ifp = &sc->arpcom.ac_if;
877 if (status != USBD_NORMAL_COMPLETION) {
878 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
879 RUE_UNLOCK(sc);
880 return;
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;
889 ifp->if_timer = 0;
890 ifp->if_flags &= ~IFF_OACTIVE;
891 usbd_get_xfer_status(c->rue_xfer, NULL, NULL, NULL, &err);
893 if (c->rue_mbuf != NULL) {
894 m_freem(c->rue_mbuf);
895 c->rue_mbuf = NULL;
898 if (err)
899 ifp->if_oerrors++;
900 else
901 ifp->if_opackets++;
903 if (!ifq_is_empty(&ifp->if_snd))
904 if_devstart(ifp);
906 RUE_UNLOCK(sc);
909 static void
910 rue_tick(void *xsc)
912 struct rue_softc *sc = xsc;
913 struct ifnet *ifp;
914 struct mii_data *mii;
916 if (sc == NULL)
917 return;
919 RUE_LOCK(sc);
921 ifp = &sc->arpcom.ac_if;
922 mii = GET_MII(sc);
923 if (mii == NULL) {
924 RUE_UNLOCK(sc);
925 return;
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))
933 if_devstart(ifp);
936 callout_reset(&sc->rue_stat_ch, hz, rue_tick, sc);
938 RUE_UNLOCK(sc);
941 static int
942 rue_encap(struct rue_softc *sc, struct mbuf *m, int idx)
944 int total_len;
945 struct rue_chain *c;
946 usbd_status err;
948 c = &sc->rue_cdata.rue_tx_chain[idx];
951 * Copy the mbuf data into a contiguous buffer
953 m_copydata(m, 0, m->m_pkthdr.len, c->rue_buf);
954 c->rue_mbuf = m;
956 total_len = m->m_pkthdr.len;
959 * This is an undocumented behavior.
960 * RTL8150 chip doesn't send frame length smaller than
961 * RUE_MIN_FRAMELEN (60) byte packet.
963 if (total_len < RUE_MIN_FRAMELEN)
964 total_len = RUE_MIN_FRAMELEN;
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);
970 /* Transmit */
971 err = usbd_transfer(c->rue_xfer);
972 if (err != USBD_IN_PROGRESS) {
973 rue_stop(sc);
974 return (EIO);
977 sc->rue_cdata.rue_tx_cnt++;
979 return (0);
982 static void
983 rue_start(struct ifnet *ifp)
985 struct rue_softc *sc = ifp->if_softc;
986 struct mbuf *m_head = NULL;
988 RUE_LOCK(sc);
990 if (!sc->rue_link) {
991 ifq_purge(&ifp->if_snd);
992 RUE_UNLOCK(sc);
993 return;
996 if (ifp->if_flags & IFF_OACTIVE) {
997 RUE_UNLOCK(sc);
998 return;
1001 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1002 if (m_head == NULL) {
1003 RUE_UNLOCK(sc);
1004 return;
1007 if (rue_encap(sc, m_head, 0)) {
1008 /* rue_encap() will free m_head, if we reach here */
1009 ifp->if_flags |= IFF_OACTIVE;
1010 RUE_UNLOCK(sc);
1011 return;
1015 * If there's a BPF listener, bounce a copy of this frame
1016 * to him.
1018 BPF_MTAP(ifp, m_head);
1020 ifp->if_flags |= IFF_OACTIVE;
1023 * Set a timeout in case the chip goes out to lunch.
1025 ifp->if_timer = 5;
1027 RUE_UNLOCK(sc);
1030 static void
1031 rue_init(void *xsc)
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;
1041 RUE_LOCK(sc);
1043 if (ifp->if_flags & IFF_RUNNING) {
1044 RUE_UNLOCK(sc);
1045 return;
1049 * Cancel pending I/O and free all RX/TX buffers.
1051 rue_reset(sc);
1053 /* Set MAC address */
1054 rue_write_mem(sc, RUE_IDR0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
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;
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;
1070 #ifdef RUE_INTR_PIPE
1071 sc->rue_cdata.rue_ibuf = kmalloc(RUE_INTR_PKTLEN, M_USBDEV, M_WAITOK);
1072 #endif
1075 * Set the initial TX and RX configuration.
1077 rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
1079 rxcfg = RUE_RCR_CONFIG;
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;
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;
1093 rue_csr_write_2(sc, RUE_RCR, rxcfg);
1095 /* Load the multicast filter. */
1096 rue_setmulti(sc);
1098 /* Enable RX and TX */
1099 rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
1101 mii_mediachg(mii);
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;
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;
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;
1130 #endif
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);
1141 ifp->if_flags |= IFF_RUNNING;
1142 ifp->if_flags &= ~IFF_OACTIVE;
1144 callout_reset(&sc->rue_stat_ch, hz, rue_tick, sc);
1146 RUE_UNLOCK(sc);
1150 * Set media options.
1153 static int
1154 rue_ifmedia_upd(struct ifnet *ifp)
1156 struct rue_softc *sc = ifp->if_softc;
1157 struct mii_data *mii = GET_MII(sc);
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);
1165 mii_mediachg(mii);
1167 return (0);
1171 * Report current media status.
1174 static void
1175 rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1177 struct rue_softc *sc = ifp->if_softc;
1178 struct mii_data *mii = GET_MII(sc);
1180 mii_pollstat(mii);
1181 ifmr->ifm_active = mii->mii_media_active;
1182 ifmr->ifm_status = mii->mii_media_status;
1185 static int
1186 rue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1188 struct rue_softc *sc = ifp->if_softc;
1189 struct ifreq *ifr = (struct ifreq *)data;
1190 struct mii_data *mii;
1191 int error = 0;
1193 RUE_LOCK(sc);
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);
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;
1234 RUE_UNLOCK(sc);
1236 return (error);
1239 static void
1240 rue_watchdog(struct ifnet *ifp)
1242 struct rue_softc *sc = ifp->if_softc;
1243 struct rue_chain *c;
1244 usbd_status stat;
1246 RUE_LOCK(sc);
1248 ifp->if_oerrors++;
1249 if_printf(ifp, "watchdog timeout\n");
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);
1255 if (!ifq_is_empty(&ifp->if_snd))
1256 if_devstart(ifp);
1258 RUE_UNLOCK(sc);
1262 * Stop the adapter and free any mbufs allocated to the
1263 * RX and TX lists.
1266 static void
1267 rue_stop(struct rue_softc *sc)
1269 usbd_status err;
1270 struct ifnet *ifp;
1271 int i;
1273 RUE_LOCK(sc);
1275 ifp = &sc->arpcom.ac_if;
1276 ifp->if_timer = 0;
1278 rue_csr_write_1(sc, RUE_CR, 0x00);
1279 rue_reset(sc);
1281 callout_stop(&sc->rue_stat_ch);
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));
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));
1295 sc->rue_ep[RUE_ENDPT_RX] = NULL;
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));
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));
1309 sc->rue_ep[RUE_ENDPT_TX] = NULL;
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));
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));
1324 sc->rue_ep[RUE_ENDPT_INTR] = NULL;
1326 #endif
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) {
1331 kfree(sc->rue_cdata.rue_rx_chain[i].rue_buf, M_USBDEV);
1332 sc->rue_cdata.rue_rx_chain[i].rue_buf = NULL;
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;
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;
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) {
1347 kfree(sc->rue_cdata.rue_tx_chain[i].rue_buf, M_USBDEV);
1348 sc->rue_cdata.rue_tx_chain[i].rue_buf = NULL;
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;
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;
1360 #ifdef RUE_INTR_PIPE
1361 if (sc->rue_cdata.rue_ibuf != NULL) {
1362 kfree(sc->rue_cdata.rue_ibuf, M_USBDEV);
1363 sc->rue_cdata.rue_ibuf = NULL;
1365 #endif
1367 sc->rue_link = 0;
1369 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1371 RUE_UNLOCK(sc);
1375 * Stop all chip I/O so that the kernel's probe routines don't
1376 * get confused by errant DMAs when rebooting.
1379 static void
1380 rue_shutdown(device_t dev)
1382 struct rue_softc *sc;
1384 sc = device_get_softc(dev);
1386 sc->rue_dying++;
1387 RUE_LOCK(sc);
1388 rue_reset(sc);
1389 rue_stop(sc);
1390 RUE_UNLOCK(sc);