if_url.c revision 1.25 1 /* $NetBSD: if_url.c,v 1.25 2007/03/04 06:02:48 christos Exp $ */
2 /*
3 * Copyright (c) 2001, 2002
4 * Shingo WATANABE <nabe (at) nabechan.org>. 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 * 3. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 /*
33 * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at
34 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf
35 * ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf
36 */
37
38 /*
39 * TODO:
40 * Interrupt Endpoint support
41 * External PHYs
42 * powerhook() support?
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1.25 2007/03/04 06:02:48 christos Exp $");
47
48 #include "opt_inet.h"
49 #include "bpfilter.h"
50 #include "rnd.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/lock.h>
55 #include <sys/mbuf.h>
56 #include <sys/kernel.h>
57 #include <sys/socket.h>
58
59 #include <sys/device.h>
60 #if NRND > 0
61 #include <sys/rnd.h>
62 #endif
63
64 #include <net/if.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_media.h>
68
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
72 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
73
74 #include <net/if_ether.h>
75 #ifdef INET
76 #include <netinet/in.h>
77 #include <netinet/if_inarp.h>
78 #endif
79
80 #include <dev/mii/mii.h>
81 #include <dev/mii/miivar.h>
82 #include <dev/mii/urlphyreg.h>
83
84 #include <dev/usb/usb.h>
85 #include <dev/usb/usbdi.h>
86 #include <dev/usb/usbdi_util.h>
87 #include <dev/usb/usbdevs.h>
88
89 #include <dev/usb/if_urlreg.h>
90
91
92 /* Function declarations */
93 USB_DECLARE_DRIVER(url);
94
95 Static int url_openpipes(struct url_softc *);
96 Static int url_rx_list_init(struct url_softc *);
97 Static int url_tx_list_init(struct url_softc *);
98 Static int url_newbuf(struct url_softc *, struct url_chain *, struct mbuf *);
99 Static void url_start(struct ifnet *);
100 Static int url_send(struct url_softc *, struct mbuf *, int);
101 Static void url_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
102 Static void url_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
103 Static void url_tick(void *);
104 Static void url_tick_task(void *);
105 Static int url_ioctl(struct ifnet *, u_long, void *);
106 Static void url_stop_task(struct url_softc *);
107 Static void url_stop(struct ifnet *, int);
108 Static void url_watchdog(struct ifnet *);
109 Static int url_ifmedia_change(struct ifnet *);
110 Static void url_ifmedia_status(struct ifnet *, struct ifmediareq *);
111 Static void url_lock_mii(struct url_softc *);
112 Static void url_unlock_mii(struct url_softc *);
113 Static int url_int_miibus_readreg(device_ptr_t, int, int);
114 Static void url_int_miibus_writereg(device_ptr_t, int, int, int);
115 Static void url_miibus_statchg(device_ptr_t);
116 Static int url_init(struct ifnet *);
117 Static void url_setmulti(struct url_softc *);
118 Static void url_reset(struct url_softc *);
119
120 Static int url_csr_read_1(struct url_softc *, int);
121 Static int url_csr_read_2(struct url_softc *, int);
122 Static int url_csr_write_1(struct url_softc *, int, int);
123 Static int url_csr_write_2(struct url_softc *, int, int);
124 Static int url_csr_write_4(struct url_softc *, int, int);
125 Static int url_mem(struct url_softc *, int, int, void *, int);
126
127 /* Macros */
128 #ifdef URL_DEBUG
129 #define DPRINTF(x) if (urldebug) logprintf x
130 #define DPRINTFN(n,x) if (urldebug >= (n)) logprintf x
131 int urldebug = 0;
132 #else
133 #define DPRINTF(x)
134 #define DPRINTFN(n,x)
135 #endif
136
137 #define URL_SETBIT(sc, reg, x) \
138 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) | (x))
139
140 #define URL_SETBIT2(sc, reg, x) \
141 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) | (x))
142
143 #define URL_CLRBIT(sc, reg, x) \
144 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) & ~(x))
145
146 #define URL_CLRBIT2(sc, reg, x) \
147 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) & ~(x))
148
149 static const struct url_type {
150 struct usb_devno url_dev;
151 u_int16_t url_flags;
152 #define URL_EXT_PHY 0x0001
153 } url_devs [] = {
154 /* MELCO LUA-KTX */
155 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0},
156 /* Realtek RTL8150L Generic (GREEN HOUSE USBKR100) */
157 {{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8150L}, 0},
158 /* Longshine LCS-8138TX */
159 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_LCS8138TX}, 0},
160 /* Micronet SP128AR */
161 {{ USB_VENDOR_MICRONET, USB_PRODUCT_MICRONET_SP128AR}, 0},
162 /* OQO model 01 */
163 {{ USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01}, 0},
164 };
165 #define url_lookup(v, p) ((const struct url_type *)usb_lookup(url_devs, v, p))
166
167
168 /* Probe */
169 USB_MATCH(url)
170 {
171 USB_MATCH_START(url, uaa);
172
173 if (uaa->iface != NULL)
174 return (UMATCH_NONE);
175
176 return (url_lookup(uaa->vendor, uaa->product) != NULL ?
177 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
178 }
179 /* Attach */
180 USB_ATTACH(url)
181 {
182 USB_ATTACH_START(url, sc, uaa);
183 usbd_device_handle dev = uaa->device;
184 usbd_interface_handle iface;
185 usbd_status err;
186 usb_interface_descriptor_t *id;
187 usb_endpoint_descriptor_t *ed;
188 char *devinfop;
189 char *devname = USBDEVNAME(sc->sc_dev);
190 struct ifnet *ifp;
191 struct mii_data *mii;
192 u_char eaddr[ETHER_ADDR_LEN];
193 int i, s;
194
195 devinfop = usbd_devinfo_alloc(dev, 0);
196 USB_ATTACH_SETUP;
197 printf("%s: %s\n", devname, devinfop);
198 usbd_devinfo_free(devinfop);
199
200 /* Move the device into the configured state. */
201 err = usbd_set_config_no(dev, URL_CONFIG_NO, 1);
202 if (err) {
203 printf("%s: setting config no failed\n", devname);
204 goto bad;
205 }
206
207 usb_init_task(&sc->sc_tick_task, url_tick_task, sc);
208 lockinit(&sc->sc_mii_lock, PZERO, "urlmii", 0, 0);
209 usb_init_task(&sc->sc_stop_task, (void (*)(void *)) url_stop_task, sc);
210
211 /* get control interface */
212 err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface);
213 if (err) {
214 printf("%s: failed to get interface, err=%s\n", devname,
215 usbd_errstr(err));
216 goto bad;
217 }
218
219 sc->sc_udev = dev;
220 sc->sc_ctl_iface = iface;
221 sc->sc_flags = url_lookup(uaa->vendor, uaa->product)->url_flags;
222
223 /* get interface descriptor */
224 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
225
226 /* find endpoints */
227 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
228 for (i = 0; i < id->bNumEndpoints; i++) {
229 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
230 if (ed == NULL) {
231 printf("%s: couldn't get endpoint %d\n", devname, i);
232 goto bad;
233 }
234 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
235 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
236 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
237 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
238 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
239 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
240 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
241 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
242 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
243 }
244
245 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
246 sc->sc_intrin_no == -1) {
247 printf("%s: missing endpoint\n", devname);
248 goto bad;
249 }
250
251 s = splnet();
252
253 /* reset the adapter */
254 url_reset(sc);
255
256 /* Get Ethernet Address */
257 err = url_mem(sc, URL_CMD_READMEM, URL_IDR0, (void *)eaddr,
258 ETHER_ADDR_LEN);
259 if (err) {
260 printf("%s: read MAC address failed\n", devname);
261 splx(s);
262 goto bad;
263 }
264
265 /* Print Ethernet Address */
266 printf("%s: Ethernet address %s\n", devname, ether_sprintf(eaddr));
267
268 /* initialize interface information */
269 ifp = GET_IFP(sc);
270 ifp->if_softc = sc;
271 ifp->if_mtu = ETHERMTU;
272 strncpy(ifp->if_xname, devname, IFNAMSIZ);
273 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
274 ifp->if_start = url_start;
275 ifp->if_ioctl = url_ioctl;
276 ifp->if_watchdog = url_watchdog;
277 ifp->if_init = url_init;
278 ifp->if_stop = url_stop;
279
280 IFQ_SET_READY(&ifp->if_snd);
281
282 /*
283 * Do ifmedia setup.
284 */
285 mii = &sc->sc_mii;
286 mii->mii_ifp = ifp;
287 mii->mii_readreg = url_int_miibus_readreg;
288 mii->mii_writereg = url_int_miibus_writereg;
289 #if 0
290 if (sc->sc_flags & URL_EXT_PHY) {
291 mii->mii_readreg = url_ext_miibus_readreg;
292 mii->mii_writereg = url_ext_miibus_writereg;
293 }
294 #endif
295 mii->mii_statchg = url_miibus_statchg;
296 mii->mii_flags = MIIF_AUTOTSLEEP;
297 ifmedia_init(&mii->mii_media, 0,
298 url_ifmedia_change, url_ifmedia_status);
299 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
300 if (LIST_FIRST(&mii->mii_phys) == NULL) {
301 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
302 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
303 } else
304 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
305
306 /* attach the interface */
307 if_attach(ifp);
308 Ether_ifattach(ifp, eaddr);
309
310 #if NRND > 0
311 rnd_attach_source(&sc->rnd_source, devname, RND_TYPE_NET, 0);
312 #endif
313
314 usb_callout_init(sc->sc_stat_ch);
315 sc->sc_attached = 1;
316 splx(s);
317
318 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
319
320 USB_ATTACH_SUCCESS_RETURN;
321
322 bad:
323 sc->sc_dying = 1;
324 USB_ATTACH_ERROR_RETURN;
325 }
326
327 /* detach */
328 USB_DETACH(url)
329 {
330 USB_DETACH_START(url, sc);
331 struct ifnet *ifp = GET_IFP(sc);
332 int s;
333
334 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
335
336 /* Detached before attached finished */
337 if (!sc->sc_attached)
338 return (0);
339
340 usb_uncallout(sc->sc_stat_ch, url_tick, sc);
341
342 /* Remove any pending tasks */
343 usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
344 usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
345
346 s = splusb();
347
348 if (--sc->sc_refcnt >= 0) {
349 /* Wait for processes to go away */
350 usb_detach_wait(USBDEV(sc->sc_dev));
351 }
352
353 if (ifp->if_flags & IFF_RUNNING)
354 url_stop(GET_IFP(sc), 1);
355
356 #if NRND > 0
357 rnd_detach_source(&sc->rnd_source);
358 #endif
359 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
360 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
361 ether_ifdetach(ifp);
362 if_detach(ifp);
363
364 #ifdef DIAGNOSTIC
365 if (sc->sc_pipe_tx != NULL)
366 printf("%s: detach has active tx endpoint.\n",
367 USBDEVNAME(sc->sc_dev));
368 if (sc->sc_pipe_rx != NULL)
369 printf("%s: detach has active rx endpoint.\n",
370 USBDEVNAME(sc->sc_dev));
371 if (sc->sc_pipe_intr != NULL)
372 printf("%s: detach has active intr endpoint.\n",
373 USBDEVNAME(sc->sc_dev));
374 #endif
375
376 sc->sc_attached = 0;
377
378 splx(s);
379
380 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
381 USBDEV(sc->sc_dev));
382
383 return (0);
384 }
385
386 /* read/write memory */
387 Static int
388 url_mem(struct url_softc *sc, int cmd, int offset, void *buf, int len)
389 {
390 usb_device_request_t req;
391 usbd_status err;
392
393 if (sc == NULL)
394 return (0);
395
396 DPRINTFN(0x200,
397 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
398
399 if (sc->sc_dying)
400 return (0);
401
402 if (cmd == URL_CMD_READMEM)
403 req.bmRequestType = UT_READ_VENDOR_DEVICE;
404 else
405 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
406 req.bRequest = URL_REQ_MEM;
407 USETW(req.wValue, offset);
408 USETW(req.wIndex, 0x0000);
409 USETW(req.wLength, len);
410
411 sc->sc_refcnt++;
412 err = usbd_do_request(sc->sc_udev, &req, buf);
413 if (--sc->sc_refcnt < 0)
414 usb_detach_wakeup(USBDEV(sc->sc_dev));
415 if (err) {
416 DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n",
417 USBDEVNAME(sc->sc_dev),
418 cmd == URL_CMD_READMEM ? "read" : "write",
419 offset, err));
420 }
421
422 return (err);
423 }
424
425 /* read 1byte from register */
426 Static int
427 url_csr_read_1(struct url_softc *sc, int reg)
428 {
429 u_int8_t val = 0;
430
431 DPRINTFN(0x100,
432 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
433
434 if (sc->sc_dying)
435 return (0);
436
437 return (url_mem(sc, URL_CMD_READMEM, reg, &val, 1) ? 0 : val);
438 }
439
440 /* read 2bytes from register */
441 Static int
442 url_csr_read_2(struct url_softc *sc, int reg)
443 {
444 uWord val;
445
446 DPRINTFN(0x100,
447 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
448
449 if (sc->sc_dying)
450 return (0);
451
452 USETW(val, 0);
453 return (url_mem(sc, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val));
454 }
455
456 /* write 1byte to register */
457 Static int
458 url_csr_write_1(struct url_softc *sc, int reg, int aval)
459 {
460 u_int8_t val = aval;
461
462 DPRINTFN(0x100,
463 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
464
465 if (sc->sc_dying)
466 return (0);
467
468 return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0);
469 }
470
471 /* write 2bytes to register */
472 Static int
473 url_csr_write_2(struct url_softc *sc, int reg, int aval)
474 {
475 uWord val;
476
477 DPRINTFN(0x100,
478 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
479
480 USETW(val, aval);
481
482 if (sc->sc_dying)
483 return (0);
484
485 return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0);
486 }
487
488 /* write 4bytes to register */
489 Static int
490 url_csr_write_4(struct url_softc *sc, int reg, int aval)
491 {
492 uDWord val;
493
494 DPRINTFN(0x100,
495 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
496
497 USETDW(val, aval);
498
499 if (sc->sc_dying)
500 return (0);
501
502 return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0);
503 }
504
505 Static int
506 url_init(struct ifnet *ifp)
507 {
508 struct url_softc *sc = ifp->if_softc;
509 struct mii_data *mii = GET_MII(sc);
510 u_char *eaddr;
511 int i, s;
512
513 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
514
515 if (sc->sc_dying)
516 return (EIO);
517
518 s = splnet();
519
520 /* Cancel pending I/O and free all TX/RX buffers */
521 url_stop(ifp, 1);
522
523 eaddr = LLADDR(ifp->if_sadl);
524 for (i = 0; i < ETHER_ADDR_LEN; i++)
525 url_csr_write_1(sc, URL_IDR0 + i, eaddr[i]);
526
527 /* Init transmission control register */
528 URL_CLRBIT(sc, URL_TCR,
529 URL_TCR_TXRR1 | URL_TCR_TXRR0 |
530 URL_TCR_IFG1 | URL_TCR_IFG0 |
531 URL_TCR_NOCRC);
532
533 /* Init receive control register */
534 URL_SETBIT2(sc, URL_RCR, URL_RCR_TAIL | URL_RCR_AD);
535 if (ifp->if_flags & IFF_BROADCAST)
536 URL_SETBIT2(sc, URL_RCR, URL_RCR_AB);
537 else
538 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AB);
539
540 /* If we want promiscuous mode, accept all physical frames. */
541 if (ifp->if_flags & IFF_PROMISC)
542 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
543 else
544 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
545
546
547 /* Initialize transmit ring */
548 if (url_tx_list_init(sc) == ENOBUFS) {
549 printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
550 splx(s);
551 return (EIO);
552 }
553
554 /* Initialize receive ring */
555 if (url_rx_list_init(sc) == ENOBUFS) {
556 printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
557 splx(s);
558 return (EIO);
559 }
560
561 /* Load the multicast filter */
562 url_setmulti(sc);
563
564 /* Enable RX and TX */
565 URL_SETBIT(sc, URL_CR, URL_CR_TE | URL_CR_RE);
566
567 mii_mediachg(mii);
568
569 if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
570 if (url_openpipes(sc)) {
571 splx(s);
572 return (EIO);
573 }
574 }
575
576 ifp->if_flags |= IFF_RUNNING;
577 ifp->if_flags &= ~IFF_OACTIVE;
578
579 splx(s);
580
581 usb_callout(sc->sc_stat_ch, hz, url_tick, sc);
582
583 return (0);
584 }
585
586 Static void
587 url_reset(struct url_softc *sc)
588 {
589 int i;
590
591 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
592
593 if (sc->sc_dying)
594 return;
595
596 URL_SETBIT(sc, URL_CR, URL_CR_SOFT_RST);
597
598 for (i = 0; i < URL_TX_TIMEOUT; i++) {
599 if (!(url_csr_read_1(sc, URL_CR) & URL_CR_SOFT_RST))
600 break;
601 delay(10); /* XXX */
602 }
603
604 delay(10000); /* XXX */
605 }
606
607 int
608 url_activate(device_ptr_t self, enum devact act)
609 {
610 struct url_softc *sc = (struct url_softc *)self;
611
612 DPRINTF(("%s: %s: enter, act=%d\n", USBDEVNAME(sc->sc_dev),
613 __func__, act));
614
615 switch (act) {
616 case DVACT_ACTIVATE:
617 return (EOPNOTSUPP);
618 break;
619
620 case DVACT_DEACTIVATE:
621 if_deactivate(&sc->sc_ec.ec_if);
622 sc->sc_dying = 1;
623 break;
624 }
625
626 return (0);
627 }
628
629 #define url_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
630
631
632 Static void
633 url_setmulti(struct url_softc *sc)
634 {
635 struct ifnet *ifp;
636 struct ether_multi *enm;
637 struct ether_multistep step;
638 u_int32_t hashes[2] = { 0, 0 };
639 int h = 0;
640 int mcnt = 0;
641
642 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
643
644 if (sc->sc_dying)
645 return;
646
647 ifp = GET_IFP(sc);
648
649 if (ifp->if_flags & IFF_PROMISC) {
650 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
651 return;
652 } else if (ifp->if_flags & IFF_ALLMULTI) {
653 allmulti:
654 ifp->if_flags |= IFF_ALLMULTI;
655 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM);
656 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAP);
657 return;
658 }
659
660 /* first, zot all the existing hash bits */
661 url_csr_write_4(sc, URL_MAR0, 0);
662 url_csr_write_4(sc, URL_MAR4, 0);
663
664 /* now program new ones */
665 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
666 while (enm != NULL) {
667 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
668 ETHER_ADDR_LEN) != 0)
669 goto allmulti;
670
671 h = url_calchash(enm->enm_addrlo);
672 if (h < 32)
673 hashes[0] |= (1 << h);
674 else
675 hashes[1] |= (1 << (h -32));
676 mcnt++;
677 ETHER_NEXT_MULTI(step, enm);
678 }
679
680 ifp->if_flags &= ~IFF_ALLMULTI;
681
682 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
683
684 if (mcnt){
685 URL_SETBIT2(sc, URL_RCR, URL_RCR_AM);
686 } else {
687 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AM);
688 }
689 url_csr_write_4(sc, URL_MAR0, hashes[0]);
690 url_csr_write_4(sc, URL_MAR4, hashes[1]);
691 }
692
693 Static int
694 url_openpipes(struct url_softc *sc)
695 {
696 struct url_chain *c;
697 usbd_status err;
698 int i;
699 int error = 0;
700
701 if (sc->sc_dying)
702 return (EIO);
703
704 sc->sc_refcnt++;
705
706 /* Open RX pipe */
707 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
708 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
709 if (err) {
710 printf("%s: open rx pipe failed: %s\n",
711 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
712 error = EIO;
713 goto done;
714 }
715
716 /* Open TX pipe */
717 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
718 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
719 if (err) {
720 printf("%s: open tx pipe failed: %s\n",
721 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
722 error = EIO;
723 goto done;
724 }
725
726 #if 0
727 /* XXX: interrupt endpoint is not yet supported */
728 /* Open Interrupt pipe */
729 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
730 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
731 &sc->sc_cdata.url_ibuf, URL_INTR_PKGLEN,
732 url_intr, USBD_DEFAULT_INTERVAL);
733 if (err) {
734 printf("%s: open intr pipe failed: %s\n",
735 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
736 error = EIO;
737 goto done;
738 }
739 #endif
740
741
742 /* Start up the receive pipe. */
743 for (i = 0; i < URL_RX_LIST_CNT; i++) {
744 c = &sc->sc_cdata.url_rx_chain[i];
745 usbd_setup_xfer(c->url_xfer, sc->sc_pipe_rx,
746 c, c->url_buf, URL_BUFSZ,
747 USBD_SHORT_XFER_OK | USBD_NO_COPY,
748 USBD_NO_TIMEOUT, url_rxeof);
749 (void)usbd_transfer(c->url_xfer);
750 DPRINTF(("%s: %s: start read\n", USBDEVNAME(sc->sc_dev),
751 __func__));
752 }
753
754 done:
755 if (--sc->sc_refcnt < 0)
756 usb_detach_wakeup(USBDEV(sc->sc_dev));
757
758 return (error);
759 }
760
761 Static int
762 url_newbuf(struct url_softc *sc, struct url_chain *c, struct mbuf *m)
763 {
764 struct mbuf *m_new = NULL;
765
766 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
767
768 if (m == NULL) {
769 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
770 if (m_new == NULL) {
771 printf("%s: no memory for rx list "
772 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
773 return (ENOBUFS);
774 }
775 MCLGET(m_new, M_DONTWAIT);
776 if (!(m_new->m_flags & M_EXT)) {
777 printf("%s: no memory for rx list "
778 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
779 m_freem(m_new);
780 return (ENOBUFS);
781 }
782 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
783 } else {
784 m_new = m;
785 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
786 m_new->m_data = m_new->m_ext.ext_buf;
787 }
788
789 m_adj(m_new, ETHER_ALIGN);
790 c->url_mbuf = m_new;
791
792 return (0);
793 }
794
795
796 Static int
797 url_rx_list_init(struct url_softc *sc)
798 {
799 struct url_cdata *cd;
800 struct url_chain *c;
801 int i;
802
803 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
804
805 cd = &sc->sc_cdata;
806 for (i = 0; i < URL_RX_LIST_CNT; i++) {
807 c = &cd->url_rx_chain[i];
808 c->url_sc = sc;
809 c->url_idx = i;
810 if (url_newbuf(sc, c, NULL) == ENOBUFS)
811 return (ENOBUFS);
812 if (c->url_xfer == NULL) {
813 c->url_xfer = usbd_alloc_xfer(sc->sc_udev);
814 if (c->url_xfer == NULL)
815 return (ENOBUFS);
816 c->url_buf = usbd_alloc_buffer(c->url_xfer, URL_BUFSZ);
817 if (c->url_buf == NULL) {
818 usbd_free_xfer(c->url_xfer);
819 return (ENOBUFS);
820 }
821 }
822 }
823
824 return (0);
825 }
826
827 Static int
828 url_tx_list_init(struct url_softc *sc)
829 {
830 struct url_cdata *cd;
831 struct url_chain *c;
832 int i;
833
834 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
835
836 cd = &sc->sc_cdata;
837 for (i = 0; i < URL_TX_LIST_CNT; i++) {
838 c = &cd->url_tx_chain[i];
839 c->url_sc = sc;
840 c->url_idx = i;
841 c->url_mbuf = NULL;
842 if (c->url_xfer == NULL) {
843 c->url_xfer = usbd_alloc_xfer(sc->sc_udev);
844 if (c->url_xfer == NULL)
845 return (ENOBUFS);
846 c->url_buf = usbd_alloc_buffer(c->url_xfer, URL_BUFSZ);
847 if (c->url_buf == NULL) {
848 usbd_free_xfer(c->url_xfer);
849 return (ENOBUFS);
850 }
851 }
852 }
853
854 return (0);
855 }
856
857 Static void
858 url_start(struct ifnet *ifp)
859 {
860 struct url_softc *sc = ifp->if_softc;
861 struct mbuf *m_head = NULL;
862
863 DPRINTF(("%s: %s: enter, link=%d\n", USBDEVNAME(sc->sc_dev),
864 __func__, sc->sc_link));
865
866 if (sc->sc_dying)
867 return;
868
869 if (!sc->sc_link)
870 return;
871
872 if (ifp->if_flags & IFF_OACTIVE)
873 return;
874
875 IFQ_POLL(&ifp->if_snd, m_head);
876 if (m_head == NULL)
877 return;
878
879 if (url_send(sc, m_head, 0)) {
880 ifp->if_flags |= IFF_OACTIVE;
881 return;
882 }
883
884 IFQ_DEQUEUE(&ifp->if_snd, m_head);
885
886 #if NBPFILTER > 0
887 if (ifp->if_bpf)
888 bpf_mtap(ifp->if_bpf, m_head);
889 #endif
890
891 ifp->if_flags |= IFF_OACTIVE;
892
893 /* Set a timeout in case the chip goes out to lunch. */
894 ifp->if_timer = 5;
895 }
896
897 Static int
898 url_send(struct url_softc *sc, struct mbuf *m, int idx)
899 {
900 int total_len;
901 struct url_chain *c;
902 usbd_status err;
903
904 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
905
906 c = &sc->sc_cdata.url_tx_chain[idx];
907
908 /* Copy the mbuf data into a contiguous buffer */
909 m_copydata(m, 0, m->m_pkthdr.len, c->url_buf);
910 c->url_mbuf = m;
911 total_len = m->m_pkthdr.len;
912
913 if (total_len < URL_MIN_FRAME_LEN) {
914 memset(c->url_buf + total_len, 0,
915 URL_MIN_FRAME_LEN - total_len);
916 total_len = URL_MIN_FRAME_LEN;
917 }
918 usbd_setup_xfer(c->url_xfer, sc->sc_pipe_tx, c, c->url_buf, total_len,
919 USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
920 URL_TX_TIMEOUT, url_txeof);
921
922 /* Transmit */
923 sc->sc_refcnt++;
924 err = usbd_transfer(c->url_xfer);
925 if (--sc->sc_refcnt < 0)
926 usb_detach_wakeup(USBDEV(sc->sc_dev));
927 if (err != USBD_IN_PROGRESS) {
928 printf("%s: url_send error=%s\n", USBDEVNAME(sc->sc_dev),
929 usbd_errstr(err));
930 /* Stop the interface */
931 usb_add_task(sc->sc_udev, &sc->sc_stop_task,
932 USB_TASKQ_DRIVER);
933 return (EIO);
934 }
935
936 DPRINTF(("%s: %s: send %d bytes\n", USBDEVNAME(sc->sc_dev),
937 __func__, total_len));
938
939 sc->sc_cdata.url_tx_cnt++;
940
941 return (0);
942 }
943
944 Static void
945 url_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
946 usbd_status status)
947 {
948 struct url_chain *c = priv;
949 struct url_softc *sc = c->url_sc;
950 struct ifnet *ifp = GET_IFP(sc);
951 int s;
952
953 if (sc->sc_dying)
954 return;
955
956 s = splnet();
957
958 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
959
960 ifp->if_timer = 0;
961 ifp->if_flags &= ~IFF_OACTIVE;
962
963 if (status != USBD_NORMAL_COMPLETION) {
964 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
965 splx(s);
966 return;
967 }
968 ifp->if_oerrors++;
969 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
970 usbd_errstr(status));
971 if (status == USBD_STALLED) {
972 sc->sc_refcnt++;
973 usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
974 if (--sc->sc_refcnt < 0)
975 usb_detach_wakeup(USBDEV(sc->sc_dev));
976 }
977 splx(s);
978 return;
979 }
980
981 ifp->if_opackets++;
982
983 m_freem(c->url_mbuf);
984 c->url_mbuf = NULL;
985
986 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
987 url_start(ifp);
988
989 splx(s);
990 }
991
992 Static void
993 url_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
994 {
995 struct url_chain *c = priv;
996 struct url_softc *sc = c->url_sc;
997 struct ifnet *ifp = GET_IFP(sc);
998 struct mbuf *m;
999 u_int32_t total_len;
1000 url_rxhdr_t rxhdr;
1001 int s;
1002
1003 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
1004
1005 if (sc->sc_dying)
1006 return;
1007
1008 if (status != USBD_NORMAL_COMPLETION) {
1009 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1010 return;
1011 sc->sc_rx_errs++;
1012 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1013 printf("%s: %u usb errors on rx: %s\n",
1014 USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
1015 usbd_errstr(status));
1016 sc->sc_rx_errs = 0;
1017 }
1018 if (status == USBD_STALLED) {
1019 sc->sc_refcnt++;
1020 usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
1021 if (--sc->sc_refcnt < 0)
1022 usb_detach_wakeup(USBDEV(sc->sc_dev));
1023 }
1024 goto done;
1025 }
1026
1027 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1028
1029 memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len);
1030
1031 if (total_len <= ETHER_CRC_LEN) {
1032 ifp->if_ierrors++;
1033 goto done;
1034 }
1035
1036 memcpy(&rxhdr, c->url_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr));
1037
1038 DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n",
1039 USBDEVNAME(sc->sc_dev),
1040 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK,
1041 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "",
1042 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "",
1043 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "",
1044 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : ""));
1045
1046 if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) {
1047 ifp->if_ierrors++;
1048 goto done;
1049 }
1050
1051 ifp->if_ipackets++;
1052 total_len -= ETHER_CRC_LEN;
1053
1054 m = c->url_mbuf;
1055 m->m_pkthdr.len = m->m_len = total_len;
1056 m->m_pkthdr.rcvif = ifp;
1057
1058 s = splnet();
1059
1060 if (url_newbuf(sc, c, NULL) == ENOBUFS) {
1061 ifp->if_ierrors++;
1062 goto done1;
1063 }
1064
1065 #if NBPFILTER > 0
1066 if (ifp->if_bpf)
1067 BPF_MTAP(ifp, m);
1068 #endif
1069
1070 DPRINTF(("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
1071 __func__, m->m_len));
1072 IF_INPUT(ifp, m);
1073
1074 done1:
1075 splx(s);
1076
1077 done:
1078 /* Setup new transfer */
1079 usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->url_buf, URL_BUFSZ,
1080 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1081 USBD_NO_TIMEOUT, url_rxeof);
1082 sc->sc_refcnt++;
1083 usbd_transfer(xfer);
1084 if (--sc->sc_refcnt < 0)
1085 usb_detach_wakeup(USBDEV(sc->sc_dev));
1086
1087 DPRINTF(("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), __func__));
1088 }
1089
1090 #if 0
1091 Static void url_intr()
1092 {
1093 }
1094 #endif
1095
1096 Static int
1097 url_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1098 {
1099 struct url_softc *sc = ifp->if_softc;
1100 struct ifreq *ifr = (struct ifreq *)data;
1101 struct mii_data *mii;
1102 int s, error = 0;
1103
1104 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1105
1106 if (sc->sc_dying)
1107 return (EIO);
1108
1109 s = splnet();
1110
1111 switch (cmd) {
1112 case SIOCGIFMEDIA:
1113 case SIOCSIFMEDIA:
1114 mii = GET_MII(sc);
1115 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1116 break;
1117
1118 default:
1119 error = ether_ioctl(ifp, cmd, data);
1120 if (error == ENETRESET) {
1121 if (ifp->if_flags & IFF_RUNNING)
1122 url_setmulti(sc);
1123 error = 0;
1124 }
1125 break;
1126 }
1127
1128 splx(s);
1129
1130 return (error);
1131 }
1132
1133 Static void
1134 url_watchdog(struct ifnet *ifp)
1135 {
1136 struct url_softc *sc = ifp->if_softc;
1137 struct url_chain *c;
1138 usbd_status stat;
1139 int s;
1140
1141 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1142
1143 ifp->if_oerrors++;
1144 printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
1145
1146 s = splusb();
1147 c = &sc->sc_cdata.url_tx_chain[0];
1148 usbd_get_xfer_status(c->url_xfer, NULL, NULL, NULL, &stat);
1149 url_txeof(c->url_xfer, c, stat);
1150
1151 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1152 url_start(ifp);
1153 splx(s);
1154 }
1155
1156 Static void
1157 url_stop_task(struct url_softc *sc)
1158 {
1159 url_stop(GET_IFP(sc), 1);
1160 }
1161
1162 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1163 Static void
1164 url_stop(struct ifnet *ifp, int disable)
1165 {
1166 struct url_softc *sc = ifp->if_softc;
1167 usbd_status err;
1168 int i;
1169
1170 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1171
1172 ifp->if_timer = 0;
1173
1174 url_reset(sc);
1175
1176 usb_uncallout(sc->sc_stat_ch, url_tick, sc);
1177
1178 /* Stop transfers */
1179 /* RX endpoint */
1180 if (sc->sc_pipe_rx != NULL) {
1181 err = usbd_abort_pipe(sc->sc_pipe_rx);
1182 if (err)
1183 printf("%s: abort rx pipe failed: %s\n",
1184 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1185 err = usbd_close_pipe(sc->sc_pipe_rx);
1186 if (err)
1187 printf("%s: close rx pipe failed: %s\n",
1188 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1189 sc->sc_pipe_rx = NULL;
1190 }
1191
1192 /* TX endpoint */
1193 if (sc->sc_pipe_tx != NULL) {
1194 err = usbd_abort_pipe(sc->sc_pipe_tx);
1195 if (err)
1196 printf("%s: abort tx pipe failed: %s\n",
1197 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1198 err = usbd_close_pipe(sc->sc_pipe_tx);
1199 if (err)
1200 printf("%s: close tx pipe failed: %s\n",
1201 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1202 sc->sc_pipe_tx = NULL;
1203 }
1204
1205 #if 0
1206 /* XXX: Interrupt endpoint is not yet supported!! */
1207 /* Interrupt endpoint */
1208 if (sc->sc_pipe_intr != NULL) {
1209 err = usbd_abort_pipe(sc->sc_pipe_intr);
1210 if (err)
1211 printf("%s: abort intr pipe failed: %s\n",
1212 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1213 err = usbd_close_pipe(sc->sc_pipe_intr);
1214 if (err)
1215 printf("%s: close intr pipe failed: %s\n",
1216 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1217 sc->sc_pipe_intr = NULL;
1218 }
1219 #endif
1220
1221 /* Free RX resources. */
1222 for (i = 0; i < URL_RX_LIST_CNT; i++) {
1223 if (sc->sc_cdata.url_rx_chain[i].url_mbuf != NULL) {
1224 m_freem(sc->sc_cdata.url_rx_chain[i].url_mbuf);
1225 sc->sc_cdata.url_rx_chain[i].url_mbuf = NULL;
1226 }
1227 if (sc->sc_cdata.url_rx_chain[i].url_xfer != NULL) {
1228 usbd_free_xfer(sc->sc_cdata.url_rx_chain[i].url_xfer);
1229 sc->sc_cdata.url_rx_chain[i].url_xfer = NULL;
1230 }
1231 }
1232
1233 /* Free TX resources. */
1234 for (i = 0; i < URL_TX_LIST_CNT; i++) {
1235 if (sc->sc_cdata.url_tx_chain[i].url_mbuf != NULL) {
1236 m_freem(sc->sc_cdata.url_tx_chain[i].url_mbuf);
1237 sc->sc_cdata.url_tx_chain[i].url_mbuf = NULL;
1238 }
1239 if (sc->sc_cdata.url_tx_chain[i].url_xfer != NULL) {
1240 usbd_free_xfer(sc->sc_cdata.url_tx_chain[i].url_xfer);
1241 sc->sc_cdata.url_tx_chain[i].url_xfer = NULL;
1242 }
1243 }
1244
1245 sc->sc_link = 0;
1246 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1247 }
1248
1249 /* Set media options */
1250 Static int
1251 url_ifmedia_change(struct ifnet *ifp)
1252 {
1253 struct url_softc *sc = ifp->if_softc;
1254 struct mii_data *mii = GET_MII(sc);
1255
1256 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1257
1258 if (sc->sc_dying)
1259 return (0);
1260
1261 sc->sc_link = 0;
1262 if (mii->mii_instance) {
1263 struct mii_softc *miisc;
1264 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1265 miisc = LIST_NEXT(miisc, mii_list))
1266 mii_phy_reset(miisc);
1267 }
1268
1269 return (mii_mediachg(mii));
1270 }
1271
1272 /* Report current media status. */
1273 Static void
1274 url_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1275 {
1276 struct url_softc *sc = ifp->if_softc;
1277 struct mii_data *mii = GET_MII(sc);
1278
1279 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1280
1281 if (sc->sc_dying)
1282 return;
1283
1284 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1285 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1286 ifmr->ifm_status = 0;
1287 return;
1288 }
1289
1290 mii_pollstat(mii);
1291 ifmr->ifm_active = mii->mii_media_active;
1292 ifmr->ifm_status = mii->mii_media_status;
1293 }
1294
1295 Static void
1296 url_tick(void *xsc)
1297 {
1298 struct url_softc *sc = xsc;
1299
1300 if (sc == NULL)
1301 return;
1302
1303 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1304 __func__));
1305
1306 if (sc->sc_dying)
1307 return;
1308
1309 /* Perform periodic stuff in process context */
1310 usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
1311 }
1312
1313 Static void
1314 url_tick_task(void *xsc)
1315 {
1316 struct url_softc *sc = xsc;
1317 struct ifnet *ifp;
1318 struct mii_data *mii;
1319 int s;
1320
1321 if (sc == NULL)
1322 return;
1323
1324 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1325 __func__));
1326
1327 if (sc->sc_dying)
1328 return;
1329
1330 ifp = GET_IFP(sc);
1331 mii = GET_MII(sc);
1332
1333 if (mii == NULL)
1334 return;
1335
1336 s = splnet();
1337
1338 mii_tick(mii);
1339 if (!sc->sc_link) {
1340 mii_pollstat(mii);
1341 if (mii->mii_media_status & IFM_ACTIVE &&
1342 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1343 DPRINTF(("%s: %s: got link\n",
1344 USBDEVNAME(sc->sc_dev), __func__));
1345 sc->sc_link++;
1346 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1347 url_start(ifp);
1348 }
1349 }
1350
1351 usb_callout(sc->sc_stat_ch, hz, url_tick, sc);
1352
1353 splx(s);
1354 }
1355
1356 /* Get exclusive access to the MII registers */
1357 Static void
1358 url_lock_mii(struct url_softc *sc)
1359 {
1360 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1361 __func__));
1362
1363 sc->sc_refcnt++;
1364 lockmgr(&sc->sc_mii_lock, LK_EXCLUSIVE, NULL);
1365 }
1366
1367 Static void
1368 url_unlock_mii(struct url_softc *sc)
1369 {
1370 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1371 __func__));
1372
1373 lockmgr(&sc->sc_mii_lock, LK_RELEASE, NULL);
1374 if (--sc->sc_refcnt < 0)
1375 usb_detach_wakeup(USBDEV(sc->sc_dev));
1376 }
1377
1378 Static int
1379 url_int_miibus_readreg(device_ptr_t dev, int phy, int reg)
1380 {
1381 struct url_softc *sc;
1382 u_int16_t val;
1383
1384 if (dev == NULL)
1385 return (0);
1386
1387 sc = USBGETSOFTC(dev);
1388
1389 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1390 USBDEVNAME(sc->sc_dev), __func__, phy, reg));
1391
1392 if (sc->sc_dying) {
1393 #ifdef DIAGNOSTIC
1394 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1395 __func__);
1396 #endif
1397 return (0);
1398 }
1399
1400 /* XXX: one PHY only for the RTL8150 internal PHY */
1401 if (phy != 0) {
1402 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1403 USBDEVNAME(sc->sc_dev), __func__, phy));
1404 return (0);
1405 }
1406
1407 url_lock_mii(sc);
1408
1409 switch (reg) {
1410 case MII_BMCR: /* Control Register */
1411 reg = URL_BMCR;
1412 break;
1413 case MII_BMSR: /* Status Register */
1414 reg = URL_BMSR;
1415 break;
1416 case MII_PHYIDR1:
1417 case MII_PHYIDR2:
1418 val = 0;
1419 goto R_DONE;
1420 break;
1421 case MII_ANAR: /* Autonegotiation advertisement */
1422 reg = URL_ANAR;
1423 break;
1424 case MII_ANLPAR: /* Autonegotiation link partner abilities */
1425 reg = URL_ANLP;
1426 break;
1427 case URLPHY_MSR: /* Media Status Register */
1428 reg = URL_MSR;
1429 break;
1430 default:
1431 printf("%s: %s: bad register %04x\n",
1432 USBDEVNAME(sc->sc_dev), __func__, reg);
1433 val = 0;
1434 goto R_DONE;
1435 break;
1436 }
1437
1438 if (reg == URL_MSR)
1439 val = url_csr_read_1(sc, reg);
1440 else
1441 val = url_csr_read_2(sc, reg);
1442
1443 R_DONE:
1444 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1445 USBDEVNAME(sc->sc_dev), __func__, phy, reg, val));
1446
1447 url_unlock_mii(sc);
1448 return (val);
1449 }
1450
1451 Static void
1452 url_int_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1453 {
1454 struct url_softc *sc;
1455
1456 if (dev == NULL)
1457 return;
1458
1459 sc = USBGETSOFTC(dev);
1460
1461 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1462 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data));
1463
1464 if (sc->sc_dying) {
1465 #ifdef DIAGNOSTIC
1466 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1467 __func__);
1468 #endif
1469 return;
1470 }
1471
1472 /* XXX: one PHY only for the RTL8150 internal PHY */
1473 if (phy != 0) {
1474 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1475 USBDEVNAME(sc->sc_dev), __func__, phy));
1476 return;
1477 }
1478
1479 url_lock_mii(sc);
1480
1481 switch (reg) {
1482 case MII_BMCR: /* Control Register */
1483 reg = URL_BMCR;
1484 break;
1485 case MII_BMSR: /* Status Register */
1486 reg = URL_BMSR;
1487 break;
1488 case MII_PHYIDR1:
1489 case MII_PHYIDR2:
1490 goto W_DONE;
1491 break;
1492 case MII_ANAR: /* Autonegotiation advertisement */
1493 reg = URL_ANAR;
1494 break;
1495 case MII_ANLPAR: /* Autonegotiation link partner abilities */
1496 reg = URL_ANLP;
1497 break;
1498 case URLPHY_MSR: /* Media Status Register */
1499 reg = URL_MSR;
1500 break;
1501 default:
1502 printf("%s: %s: bad register %04x\n",
1503 USBDEVNAME(sc->sc_dev), __func__, reg);
1504 goto W_DONE;
1505 break;
1506 }
1507
1508 if (reg == URL_MSR)
1509 url_csr_write_1(sc, reg, data);
1510 else
1511 url_csr_write_2(sc, reg, data);
1512 W_DONE:
1513
1514 url_unlock_mii(sc);
1515 return;
1516 }
1517
1518 Static void
1519 url_miibus_statchg(device_ptr_t dev)
1520 {
1521 #ifdef URL_DEBUG
1522 struct url_softc *sc;
1523
1524 if (dev == NULL)
1525 return;
1526
1527 sc = USBGETSOFTC(dev);
1528 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1529 #endif
1530 /* Nothing to do */
1531 }
1532
1533 #if 0
1534 /*
1535 * external PHYs support, but not test.
1536 */
1537 Static int
1538 url_ext_miibus_redreg(device_ptr_t dev, int phy, int reg)
1539 {
1540 struct url_softc *sc = USBGETSOFTC(dev);
1541 u_int16_t val;
1542
1543 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n",
1544 USBDEVNAME(sc->sc_dev), __func__, phy, reg));
1545
1546 if (sc->sc_dying) {
1547 #ifdef DIAGNOSTIC
1548 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1549 __func__);
1550 #endif
1551 return (0);
1552 }
1553
1554 url_lock_mii(sc);
1555
1556 url_csr_write_1(sc, URL_PHYADD, phy & URL_PHYADD_MASK);
1557 /*
1558 * RTL8150L will initiate a MII management data transaction
1559 * if PHYCNT_OWN bit is set 1 by software. After transaction,
1560 * this bit is auto cleared by TRL8150L.
1561 */
1562 url_csr_write_1(sc, URL_PHYCNT,
1563 (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR);
1564 for (i = 0; i < URL_TIMEOUT; i++) {
1565 if ((url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0)
1566 break;
1567 }
1568 if (i == URL_TIMEOUT) {
1569 printf("%s: MII read timed out\n", USBDEVNAME(sc->sc_dev));
1570 }
1571
1572 val = url_csr_read_2(sc, URL_PHYDAT);
1573
1574 DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1575 USBDEVNAME(sc->sc_dev), __func__, phy, reg, val));
1576
1577 url_unlock_mii(sc);
1578 return (val);
1579 }
1580
1581 Static void
1582 url_ext_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1583 {
1584 struct url_softc *sc = USBGETSOFTC(dev);
1585
1586 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1587 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data));
1588
1589 if (sc->sc_dying) {
1590 #ifdef DIAGNOSTIC
1591 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1592 __func__);
1593 #endif
1594 return;
1595 }
1596
1597 url_lock_mii(sc);
1598
1599 url_csr_write_2(sc, URL_PHYDAT, data);
1600 url_csr_write_1(sc, URL_PHYADD, phy);
1601 url_csr_write_1(sc, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */
1602
1603 for (i=0; i < URL_TIMEOUT; i++) {
1604 if (url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN)
1605 break;
1606 }
1607
1608 if (i == URL_TIMEOUT) {
1609 printf("%s: MII write timed out\n",
1610 USBDEVNAME(sc->sc_dev));
1611 }
1612
1613 url_unlock_mii(sc);
1614 return;
1615 }
1616 #endif
1617
1618