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