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