usb4bsd: Port aue(4), cue(4), ipheth(4) and kue(4) USB ethernet drivers.
[dragonfly.git] / sys / bus / u4b / net / if_rue.c
1 /*-
2  * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
3  * Copyright (c) 1997, 1998, 1999, 2000 Bill Paul <wpaul@ee.columbia.edu>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 /*-
28  * Copyright (c) 1997, 1998, 1999, 2000
29  *      Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. All advertising materials mentioning features or use of this software
40  *    must display the following acknowledgement:
41  *      This product includes software developed by Bill Paul.
42  * 4. Neither the name of the author nor the names of any co-contributors
43  *    may be used to endorse or promote products derived from this software
44  *    without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
50  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
56  * THE POSSIBILITY OF SUCH DAMAGE.
57  */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61
62 /*
63  * RealTek RTL8150 USB to fast ethernet controller driver.
64  * Datasheet is available from
65  * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/.
66  */
67
68 #include <sys/stdint.h>
69 #include <sys/stddef.h>
70 #include <sys/param.h>
71 #include <sys/queue.h>
72 #include <sys/types.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/bus.h>
76 #include <sys/module.h>
77 #include <sys/lock.h>
78 #include <sys/mutex.h>
79 #include <sys/condvar.h>
80 #include <sys/sysctl.h>
81 #include <sys/sx.h>
82 #include <sys/unistd.h>
83 #include <sys/callout.h>
84 #include <sys/malloc.h>
85 #include <sys/priv.h>
86
87 #include <dev/usb/usb.h>
88 #include <dev/usb/usbdi.h>
89 #include <dev/usb/usbdi_util.h>
90 #include "usbdevs.h"
91
92 #define USB_DEBUG_VAR rue_debug
93 #include <dev/usb/usb_debug.h>
94 #include <dev/usb/usb_process.h>
95
96 #include <dev/usb/net/usb_ethernet.h>
97 #include <dev/usb/net/if_ruereg.h>
98
99 #ifdef USB_DEBUG
100 static int rue_debug = 0;
101
102 static SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue");
103 SYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RW,
104     &rue_debug, 0, "Debug level");
105 #endif
106
107 /*
108  * Various supported device vendors/products.
109  */
110
111 static const STRUCT_USB_HOST_ID rue_devs[] = {
112         {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)},
113         {USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)},
114         {USB_VPI(USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01, 0)},
115 };
116
117 /* prototypes */
118
119 static device_probe_t rue_probe;
120 static device_attach_t rue_attach;
121 static device_detach_t rue_detach;
122
123 static miibus_readreg_t rue_miibus_readreg;
124 static miibus_writereg_t rue_miibus_writereg;
125 static miibus_statchg_t rue_miibus_statchg;
126
127 static usb_callback_t rue_intr_callback;
128 static usb_callback_t rue_bulk_read_callback;
129 static usb_callback_t rue_bulk_write_callback;
130
131 static uether_fn_t rue_attach_post;
132 static uether_fn_t rue_init;
133 static uether_fn_t rue_stop;
134 static uether_fn_t rue_start;
135 static uether_fn_t rue_tick;
136 static uether_fn_t rue_setmulti;
137 static uether_fn_t rue_setpromisc;
138
139 static int      rue_read_mem(struct rue_softc *, uint16_t, void *, int);
140 static int      rue_write_mem(struct rue_softc *, uint16_t, void *, int);
141 static uint8_t  rue_csr_read_1(struct rue_softc *, uint16_t);
142 static uint16_t rue_csr_read_2(struct rue_softc *, uint16_t);
143 static int      rue_csr_write_1(struct rue_softc *, uint16_t, uint8_t);
144 static int      rue_csr_write_2(struct rue_softc *, uint16_t, uint16_t);
145 static int      rue_csr_write_4(struct rue_softc *, int, uint32_t);
146
147 static void     rue_reset(struct rue_softc *);
148 static int      rue_ifmedia_upd(struct ifnet *);
149 static void     rue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
150
151 static const struct usb_config rue_config[RUE_N_TRANSFER] = {
152
153         [RUE_BULK_DT_WR] = {
154                 .type = UE_BULK,
155                 .endpoint = UE_ADDR_ANY,
156                 .direction = UE_DIR_OUT,
157                 .bufsize = MCLBYTES,
158                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
159                 .callback = rue_bulk_write_callback,
160                 .timeout = 10000,       /* 10 seconds */
161         },
162
163         [RUE_BULK_DT_RD] = {
164                 .type = UE_BULK,
165                 .endpoint = UE_ADDR_ANY,
166                 .direction = UE_DIR_IN,
167                 .bufsize = (MCLBYTES + 4),
168                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
169                 .callback = rue_bulk_read_callback,
170                 .timeout = 0,   /* no timeout */
171         },
172
173         [RUE_INTR_DT_RD] = {
174                 .type = UE_INTERRUPT,
175                 .endpoint = UE_ADDR_ANY,
176                 .direction = UE_DIR_IN,
177                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
178                 .bufsize = 0,   /* use wMaxPacketSize */
179                 .callback = rue_intr_callback,
180         },
181 };
182
183 static device_method_t rue_methods[] = {
184         /* Device interface */
185         DEVMETHOD(device_probe, rue_probe),
186         DEVMETHOD(device_attach, rue_attach),
187         DEVMETHOD(device_detach, rue_detach),
188
189         /* MII interface */
190         DEVMETHOD(miibus_readreg, rue_miibus_readreg),
191         DEVMETHOD(miibus_writereg, rue_miibus_writereg),
192         DEVMETHOD(miibus_statchg, rue_miibus_statchg),
193
194         DEVMETHOD_END
195 };
196
197 static driver_t rue_driver = {
198         .name = "rue",
199         .methods = rue_methods,
200         .size = sizeof(struct rue_softc),
201 };
202
203 static devclass_t rue_devclass;
204
205 DRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, NULL, NULL);
206 DRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, NULL, NULL);
207 MODULE_DEPEND(rue, uether, 1, 1, 1);
208 MODULE_DEPEND(rue, usb, 1, 1, 1);
209 MODULE_DEPEND(rue, ether, 1, 1, 1);
210 MODULE_DEPEND(rue, miibus, 1, 1, 1);
211 MODULE_VERSION(rue, 1);
212
213 static const struct usb_ether_methods rue_ue_methods = {
214         .ue_attach_post = rue_attach_post,
215         .ue_start = rue_start,
216         .ue_init = rue_init,
217         .ue_stop = rue_stop,
218         .ue_tick = rue_tick,
219         .ue_setmulti = rue_setmulti,
220         .ue_setpromisc = rue_setpromisc,
221         .ue_mii_upd = rue_ifmedia_upd,
222         .ue_mii_sts = rue_ifmedia_sts,
223 };
224
225 #define RUE_SETBIT(sc, reg, x) \
226         rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
227
228 #define RUE_CLRBIT(sc, reg, x) \
229         rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
230
231 static int
232 rue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
233 {
234         struct usb_device_request req;
235
236         req.bmRequestType = UT_READ_VENDOR_DEVICE;
237         req.bRequest = UR_SET_ADDRESS;
238         USETW(req.wValue, addr);
239         USETW(req.wIndex, 0);
240         USETW(req.wLength, len);
241
242         return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
243 }
244
245 static int
246 rue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
247 {
248         struct usb_device_request req;
249
250         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
251         req.bRequest = UR_SET_ADDRESS;
252         USETW(req.wValue, addr);
253         USETW(req.wIndex, 0);
254         USETW(req.wLength, len);
255
256         return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
257 }
258
259 static uint8_t
260 rue_csr_read_1(struct rue_softc *sc, uint16_t reg)
261 {
262         uint8_t val;
263
264         rue_read_mem(sc, reg, &val, 1);
265         return (val);
266 }
267
268 static uint16_t
269 rue_csr_read_2(struct rue_softc *sc, uint16_t reg)
270 {
271         uint8_t val[2];
272
273         rue_read_mem(sc, reg, &val, 2);
274         return (UGETW(val));
275 }
276
277 static int
278 rue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val)
279 {
280         return (rue_write_mem(sc, reg, &val, 1));
281 }
282
283 static int
284 rue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val)
285 {
286         uint8_t temp[2];
287
288         USETW(temp, val);
289         return (rue_write_mem(sc, reg, &temp, 2));
290 }
291
292 static int
293 rue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val)
294 {
295         uint8_t temp[4];
296
297         USETDW(temp, val);
298         return (rue_write_mem(sc, reg, &temp, 4));
299 }
300
301 static int
302 rue_miibus_readreg(device_t dev, int phy, int reg)
303 {
304         struct rue_softc *sc = device_get_softc(dev);
305         uint16_t rval;
306         uint16_t ruereg;
307         int locked;
308
309         if (phy != 0)           /* RTL8150 supports PHY == 0, only */
310                 return (0);
311
312         locked = mtx_owned(&sc->sc_mtx);
313         if (!locked)
314                 RUE_LOCK(sc);
315
316         switch (reg) {
317         case MII_BMCR:
318                 ruereg = RUE_BMCR;
319                 break;
320         case MII_BMSR:
321                 ruereg = RUE_BMSR;
322                 break;
323         case MII_ANAR:
324                 ruereg = RUE_ANAR;
325                 break;
326         case MII_ANER:
327                 ruereg = RUE_AER;
328                 break;
329         case MII_ANLPAR:
330                 ruereg = RUE_ANLP;
331                 break;
332         case MII_PHYIDR1:
333         case MII_PHYIDR2:
334                 rval = 0;
335                 goto done;
336         default:
337                 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
338                         rval = rue_csr_read_1(sc, reg);
339                         goto done;
340                 }
341                 device_printf(sc->sc_ue.ue_dev, "bad phy register\n");
342                 rval = 0;
343                 goto done;
344         }
345
346         rval = rue_csr_read_2(sc, ruereg);
347 done:
348         if (!locked)
349                 RUE_UNLOCK(sc);
350         return (rval);
351 }
352
353 static int
354 rue_miibus_writereg(device_t dev, int phy, int reg, int data)
355 {
356         struct rue_softc *sc = device_get_softc(dev);
357         uint16_t ruereg;
358         int locked;
359
360         if (phy != 0)           /* RTL8150 supports PHY == 0, only */
361                 return (0);
362
363         locked = mtx_owned(&sc->sc_mtx);
364         if (!locked)
365                 RUE_LOCK(sc);
366
367         switch (reg) {
368         case MII_BMCR:
369                 ruereg = RUE_BMCR;
370                 break;
371         case MII_BMSR:
372                 ruereg = RUE_BMSR;
373                 break;
374         case MII_ANAR:
375                 ruereg = RUE_ANAR;
376                 break;
377         case MII_ANER:
378                 ruereg = RUE_AER;
379                 break;
380         case MII_ANLPAR:
381                 ruereg = RUE_ANLP;
382                 break;
383         case MII_PHYIDR1:
384         case MII_PHYIDR2:
385                 goto done;
386         default:
387                 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
388                         rue_csr_write_1(sc, reg, data);
389                         goto done;
390                 }
391                 device_printf(sc->sc_ue.ue_dev, " bad phy register\n");
392                 goto done;
393         }
394         rue_csr_write_2(sc, ruereg, data);
395 done:
396         if (!locked)
397                 RUE_UNLOCK(sc);
398         return (0);
399 }
400
401 static void
402 rue_miibus_statchg(device_t dev)
403 {
404         /*
405          * When the code below is enabled the card starts doing weird
406          * things after link going from UP to DOWN and back UP.
407          *
408          * Looks like some of register writes below messes up PHY
409          * interface.
410          *
411          * No visible regressions were found after commenting this code
412          * out, so that disable it for good.
413          */
414 #if 0
415         struct rue_softc *sc = device_get_softc(dev);
416         struct mii_data *mii = GET_MII(sc);
417         uint16_t bmcr;
418         int locked;
419
420         locked = mtx_owned(&sc->sc_mtx);
421         if (!locked)
422                 RUE_LOCK(sc);
423
424         RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
425
426         bmcr = rue_csr_read_2(sc, RUE_BMCR);
427
428         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
429                 bmcr |= RUE_BMCR_SPD_SET;
430         else
431                 bmcr &= ~RUE_BMCR_SPD_SET;
432
433         if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
434                 bmcr |= RUE_BMCR_DUPLEX;
435         else
436                 bmcr &= ~RUE_BMCR_DUPLEX;
437
438         rue_csr_write_2(sc, RUE_BMCR, bmcr);
439
440         RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
441
442         if (!locked)
443                 RUE_UNLOCK(sc);
444 #endif
445 }
446
447 static void
448 rue_setpromisc(struct usb_ether *ue)
449 {
450         struct rue_softc *sc = uether_getsc(ue);
451         struct ifnet *ifp = uether_getifp(ue);
452
453         RUE_LOCK_ASSERT(sc, MA_OWNED);
454
455         /* If we want promiscuous mode, set the allframes bit. */
456         if (ifp->if_flags & IFF_PROMISC)
457                 RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP);
458         else
459                 RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP);
460 }
461
462 /*
463  * Program the 64-bit multicast hash filter.
464  */
465 static void
466 rue_setmulti(struct usb_ether *ue)
467 {
468         struct rue_softc *sc = uether_getsc(ue);
469         struct ifnet *ifp = uether_getifp(ue);
470         uint16_t rxcfg;
471         int h = 0;
472         uint32_t hashes[2] = { 0, 0 };
473         struct ifmultiaddr *ifma;
474         int mcnt = 0;
475
476         RUE_LOCK_ASSERT(sc, MA_OWNED);
477
478         rxcfg = rue_csr_read_2(sc, RUE_RCR);
479
480         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
481                 rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP);
482                 rxcfg &= ~RUE_RCR_AM;
483                 rue_csr_write_2(sc, RUE_RCR, rxcfg);
484                 rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF);
485                 rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF);
486                 return;
487         }
488
489         /* first, zot all the existing hash bits */
490         rue_csr_write_4(sc, RUE_MAR0, 0);
491         rue_csr_write_4(sc, RUE_MAR4, 0);
492
493         /* now program new ones */
494         if_maddr_rlock(ifp);
495         TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link)
496         {
497                 if (ifma->ifma_addr->sa_family != AF_LINK)
498                         continue;
499                 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
500                     ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
501                 if (h < 32)
502                         hashes[0] |= (1 << h);
503                 else
504                         hashes[1] |= (1 << (h - 32));
505                 mcnt++;
506         }
507         if_maddr_runlock(ifp);
508
509         if (mcnt)
510                 rxcfg |= RUE_RCR_AM;
511         else
512                 rxcfg &= ~RUE_RCR_AM;
513
514         rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
515
516         rue_csr_write_2(sc, RUE_RCR, rxcfg);
517         rue_csr_write_4(sc, RUE_MAR0, hashes[0]);
518         rue_csr_write_4(sc, RUE_MAR4, hashes[1]);
519 }
520
521 static void
522 rue_reset(struct rue_softc *sc)
523 {
524         int i;
525
526         rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
527
528         for (i = 0; i != RUE_TIMEOUT; i++) {
529                 if (uether_pause(&sc->sc_ue, hz / 1000))
530                         break;
531                 if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST))
532                         break;
533         }
534         if (i == RUE_TIMEOUT)
535                 device_printf(sc->sc_ue.ue_dev, "reset never completed\n");
536
537         uether_pause(&sc->sc_ue, hz / 100);
538 }
539
540 static void
541 rue_attach_post(struct usb_ether *ue)
542 {
543         struct rue_softc *sc = uether_getsc(ue);
544
545         /* reset the adapter */
546         rue_reset(sc);
547
548         /* get station address from the EEPROM */
549         rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN);
550 }
551
552 /*
553  * Probe for a RTL8150 chip.
554  */
555 static int
556 rue_probe(device_t dev)
557 {
558         struct usb_attach_arg *uaa = device_get_ivars(dev);
559
560         if (uaa->usb_mode != USB_MODE_HOST)
561                 return (ENXIO);
562         if (uaa->info.bConfigIndex != RUE_CONFIG_IDX)
563                 return (ENXIO);
564         if (uaa->info.bIfaceIndex != RUE_IFACE_IDX)
565                 return (ENXIO);
566
567         return (usbd_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa));
568 }
569
570 /*
571  * Attach the interface. Allocate softc structures, do ifmedia
572  * setup and ethernet/BPF attach.
573  */
574 static int
575 rue_attach(device_t dev)
576 {
577         struct usb_attach_arg *uaa = device_get_ivars(dev);
578         struct rue_softc *sc = device_get_softc(dev);
579         struct usb_ether *ue = &sc->sc_ue;
580         uint8_t iface_index;
581         int error;
582
583         device_set_usb_desc(dev);
584         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
585
586         iface_index = RUE_IFACE_IDX;
587         error = usbd_transfer_setup(uaa->device, &iface_index,
588             sc->sc_xfer, rue_config, RUE_N_TRANSFER,
589             sc, &sc->sc_mtx);
590         if (error) {
591                 device_printf(dev, "allocating USB transfers failed\n");
592                 goto detach;
593         }
594
595         ue->ue_sc = sc;
596         ue->ue_dev = dev;
597         ue->ue_udev = uaa->device;
598         ue->ue_mtx = &sc->sc_mtx;
599         ue->ue_methods = &rue_ue_methods;
600
601         error = uether_ifattach(ue);
602         if (error) {
603                 device_printf(dev, "could not attach interface\n");
604                 goto detach;
605         }
606         return (0);                     /* success */
607
608 detach:
609         rue_detach(dev);
610         return (ENXIO);                 /* failure */
611 }
612
613 static int
614 rue_detach(device_t dev)
615 {
616         struct rue_softc *sc = device_get_softc(dev);
617         struct usb_ether *ue = &sc->sc_ue;
618
619         usbd_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER);
620         uether_ifdetach(ue);
621         mtx_destroy(&sc->sc_mtx);
622
623         return (0);
624 }
625
626 static void
627 rue_intr_callback(struct usb_xfer *xfer, usb_error_t error)
628 {
629         struct rue_softc *sc = usbd_xfer_softc(xfer);
630         struct ifnet *ifp = uether_getifp(&sc->sc_ue);
631         struct rue_intrpkt pkt;
632         struct usb_page_cache *pc;
633         int actlen;
634
635         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
636
637         switch (USB_GET_STATE(xfer)) {
638         case USB_ST_TRANSFERRED:
639
640                 if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
641                     actlen >= sizeof(pkt)) {
642
643                         pc = usbd_xfer_get_frame(xfer, 0);
644                         usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
645
646                         ifp->if_ierrors += pkt.rue_rxlost_cnt;
647                         ifp->if_ierrors += pkt.rue_crcerr_cnt;
648                         ifp->if_collisions += pkt.rue_col_cnt;
649                 }
650                 /* FALLTHROUGH */
651         case USB_ST_SETUP:
652 tr_setup:
653                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
654                 usbd_transfer_submit(xfer);
655                 return;
656
657         default:                        /* Error */
658                 if (error != USB_ERR_CANCELLED) {
659                         /* try to clear stall first */
660                         usbd_xfer_set_stall(xfer);
661                         goto tr_setup;
662                 }
663                 return;
664         }
665 }
666
667 static void
668 rue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
669 {
670         struct rue_softc *sc = usbd_xfer_softc(xfer);
671         struct usb_ether *ue = &sc->sc_ue;
672         struct ifnet *ifp = uether_getifp(ue);
673         struct usb_page_cache *pc;
674         uint16_t status;
675         int actlen;
676
677         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
678
679         switch (USB_GET_STATE(xfer)) {
680         case USB_ST_TRANSFERRED:
681
682                 if (actlen < 4) {
683                         ifp->if_ierrors++;
684                         goto tr_setup;
685                 }
686                 pc = usbd_xfer_get_frame(xfer, 0);
687                 usbd_copy_out(pc, actlen - 4, &status, sizeof(status));
688                 actlen -= 4;
689
690                 /* check recieve packet was valid or not */
691                 status = le16toh(status);
692                 if ((status & RUE_RXSTAT_VALID) == 0) {
693                         ifp->if_ierrors++;
694                         goto tr_setup;
695                 }
696                 uether_rxbuf(ue, pc, 0, actlen);
697                 /* FALLTHROUGH */
698         case USB_ST_SETUP:
699 tr_setup:
700                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
701                 usbd_transfer_submit(xfer);
702                 uether_rxflush(ue);
703                 return;
704
705         default:                        /* Error */
706                 DPRINTF("bulk read error, %s\n",
707                     usbd_errstr(error));
708
709                 if (error != USB_ERR_CANCELLED) {
710                         /* try to clear stall first */
711                         usbd_xfer_set_stall(xfer);
712                         goto tr_setup;
713                 }
714                 return;
715         }
716 }
717
718 static void
719 rue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
720 {
721         struct rue_softc *sc = usbd_xfer_softc(xfer);
722         struct ifnet *ifp = uether_getifp(&sc->sc_ue);
723         struct usb_page_cache *pc;
724         struct mbuf *m;
725         int temp_len;
726
727         switch (USB_GET_STATE(xfer)) {
728         case USB_ST_TRANSFERRED:
729                 DPRINTFN(11, "transfer complete\n");
730                 ifp->if_opackets++;
731
732                 /* FALLTHROUGH */
733         case USB_ST_SETUP:
734 tr_setup:
735                 if ((sc->sc_flags & RUE_FLAG_LINK) == 0) {
736                         /*
737                          * don't send anything if there is no link !
738                          */
739                         return;
740                 }
741                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
742
743                 if (m == NULL)
744                         return;
745                 if (m->m_pkthdr.len > MCLBYTES)
746                         m->m_pkthdr.len = MCLBYTES;
747                 temp_len = m->m_pkthdr.len;
748
749                 pc = usbd_xfer_get_frame(xfer, 0);
750                 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
751
752                 /*
753                  * This is an undocumented behavior.
754                  * RTL8150 chip doesn't send frame length smaller than
755                  * RUE_MIN_FRAMELEN (60) byte packet.
756                  */
757                 if (temp_len < RUE_MIN_FRAMELEN) {
758                         usbd_frame_zero(pc, temp_len,
759                             RUE_MIN_FRAMELEN - temp_len);
760                         temp_len = RUE_MIN_FRAMELEN;
761                 }
762                 usbd_xfer_set_frame_len(xfer, 0, temp_len);
763
764                 /*
765                  * if there's a BPF listener, bounce a copy
766                  * of this frame to him:
767                  */
768                 BPF_MTAP(ifp, m);
769
770                 m_freem(m);
771
772                 usbd_transfer_submit(xfer);
773
774                 return;
775
776         default:                        /* Error */
777                 DPRINTFN(11, "transfer error, %s\n",
778                     usbd_errstr(error));
779
780                 ifp->if_oerrors++;
781
782                 if (error != USB_ERR_CANCELLED) {
783                         /* try to clear stall first */
784                         usbd_xfer_set_stall(xfer);
785                         goto tr_setup;
786                 }
787                 return;
788         }
789 }
790
791 static void
792 rue_tick(struct usb_ether *ue)
793 {
794         struct rue_softc *sc = uether_getsc(ue);
795         struct mii_data *mii = GET_MII(sc);
796
797         RUE_LOCK_ASSERT(sc, MA_OWNED);
798
799         mii_tick(mii);
800         if ((sc->sc_flags & RUE_FLAG_LINK) == 0
801             && mii->mii_media_status & IFM_ACTIVE &&
802             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
803                 sc->sc_flags |= RUE_FLAG_LINK;
804                 rue_start(ue);
805         }
806 }
807
808 static void
809 rue_start(struct usb_ether *ue)
810 {
811         struct rue_softc *sc = uether_getsc(ue);
812
813         /*
814          * start the USB transfers, if not already started:
815          */
816         usbd_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]);
817         usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]);
818         usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]);
819 }
820
821 static void
822 rue_init(struct usb_ether *ue)
823 {
824         struct rue_softc *sc = uether_getsc(ue);
825         struct ifnet *ifp = uether_getifp(ue);
826
827         RUE_LOCK_ASSERT(sc, MA_OWNED);
828
829         /*
830          * Cancel pending I/O
831          */
832         rue_reset(sc);
833
834         /* Set MAC address */
835         rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN);
836
837         rue_stop(ue);
838
839         /*
840          * Set the initial TX and RX configuration.
841          */
842         rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
843         rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB);
844
845         /* Load the multicast filter */
846         rue_setpromisc(ue);
847         /* Load the multicast filter. */
848         rue_setmulti(ue);
849
850         /* Enable RX and TX */
851         rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
852
853         usbd_xfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]);
854
855         ifp->if_drv_flags |= IFF_DRV_RUNNING;
856         rue_start(ue);
857 }
858
859 /*
860  * Set media options.
861  */
862 static int
863 rue_ifmedia_upd(struct ifnet *ifp)
864 {
865         struct rue_softc *sc = ifp->if_softc;
866         struct mii_data *mii = GET_MII(sc);
867         struct mii_softc *miisc;
868
869         RUE_LOCK_ASSERT(sc, MA_OWNED);
870
871         sc->sc_flags &= ~RUE_FLAG_LINK;
872         LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
873                 PHY_RESET(miisc);
874         mii_mediachg(mii);
875         return (0);
876 }
877
878 /*
879  * Report current media status.
880  */
881 static void
882 rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
883 {
884         struct rue_softc *sc = ifp->if_softc;
885         struct mii_data *mii = GET_MII(sc);
886
887         RUE_LOCK(sc);
888         mii_pollstat(mii);
889         ifmr->ifm_active = mii->mii_media_active;
890         ifmr->ifm_status = mii->mii_media_status;
891         RUE_UNLOCK(sc);
892 }
893
894 static void
895 rue_stop(struct usb_ether *ue)
896 {
897         struct rue_softc *sc = uether_getsc(ue);
898         struct ifnet *ifp = uether_getifp(ue);
899
900         RUE_LOCK_ASSERT(sc, MA_OWNED);
901
902         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
903         sc->sc_flags &= ~RUE_FLAG_LINK;
904
905         /*
906          * stop all the transfers, if not already stopped:
907          */
908         usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]);
909         usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]);
910         usbd_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]);
911
912         rue_csr_write_1(sc, RUE_CR, 0x00);
913
914         rue_reset(sc);
915 }