if_url.c revision 1.12 1 /* $NetBSD: if_url.c,v 1.12 2004/10/30 18:10:06 thorpej 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.12 2004/10/30 18:10:06 thorpej Exp $");
47
48 #include "opt_inet.h"
49 #include "opt_ns.h"
50 #include "bpfilter.h"
51 #include "rnd.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/lock.h>
56 #include <sys/mbuf.h>
57 #include <sys/kernel.h>
58 #include <sys/socket.h>
59
60 #include <sys/device.h>
61 #if NRND > 0
62 #include <sys/rnd.h>
63 #endif
64
65 #include <net/if.h>
66 #include <net/if_arp.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69
70 #if NBPFILTER > 0
71 #include <net/bpf.h>
72 #endif
73 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
74
75 #include <net/if_ether.h>
76 #ifdef INET
77 #include <netinet/in.h>
78 #include <netinet/if_inarp.h>
79 #endif
80 #ifdef NS
81 #include <netns/ns.h>
82 #include <netns/ns_if.h>
83 #endif
84
85 #include <dev/mii/mii.h>
86 #include <dev/mii/miivar.h>
87 #include <dev/mii/urlphyreg.h>
88
89 #include <dev/usb/usb.h>
90 #include <dev/usb/usbdi.h>
91 #include <dev/usb/usbdi_util.h>
92 #include <dev/usb/usbdevs.h>
93
94 #include <dev/usb/if_urlreg.h>
95
96
97 /* Function declarations */
98 USB_DECLARE_DRIVER(url);
99
100 Static int url_openpipes(struct url_softc *);
101 Static int url_rx_list_init(struct url_softc *);
102 Static int url_tx_list_init(struct url_softc *);
103 Static int url_newbuf(struct url_softc *, struct url_chain *, struct mbuf *);
104 Static void url_start(struct ifnet *);
105 Static int url_send(struct url_softc *, struct mbuf *, int);
106 Static void url_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
107 Static void url_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
108 Static void url_tick(void *);
109 Static void url_tick_task(void *);
110 Static int url_ioctl(struct ifnet *, u_long, caddr_t);
111 Static void url_stop_task(struct url_softc *);
112 Static void url_stop(struct ifnet *, int);
113 Static void url_watchdog(struct ifnet *);
114 Static int url_ifmedia_change(struct ifnet *);
115 Static void url_ifmedia_status(struct ifnet *, struct ifmediareq *);
116 Static void url_lock_mii(struct url_softc *);
117 Static void url_unlock_mii(struct url_softc *);
118 Static int url_int_miibus_readreg(device_ptr_t, int, int);
119 Static void url_int_miibus_writereg(device_ptr_t, int, int, int);
120 Static void url_miibus_statchg(device_ptr_t);
121 Static int url_init(struct ifnet *);
122 Static void url_setmulti(struct url_softc *);
123 Static void url_reset(struct url_softc *);
124
125 Static int url_csr_read_1(struct url_softc *, int);
126 Static int url_csr_read_2(struct url_softc *, int);
127 Static int url_csr_write_1(struct url_softc *, int, int);
128 Static int url_csr_write_2(struct url_softc *, int, int);
129 Static int url_csr_write_4(struct url_softc *, int, int);
130 Static int url_mem(struct url_softc *, int, int, void *, int);
131
132 /* Macros */
133 #ifdef URL_DEBUG
134 #define DPRINTF(x) if (urldebug) logprintf x
135 #define DPRINTFN(n,x) if (urldebug >= (n)) logprintf x
136 int urldebug = 0;
137 #else
138 #define DPRINTF(x)
139 #define DPRINTFN(n,x)
140 #endif
141
142 #define URL_SETBIT(sc, reg, x) \
143 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) | (x))
144
145 #define URL_SETBIT2(sc, reg, x) \
146 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) | (x))
147
148 #define URL_CLRBIT(sc, reg, x) \
149 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) & ~(x))
150
151 #define URL_CLRBIT2(sc, reg, x) \
152 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) & ~(x))
153
154 static const struct url_type {
155 struct usb_devno url_dev;
156 u_int16_t url_flags;
157 #define URL_EXT_PHY 0x0001
158 } url_devs [] = {
159 /* MELCO LUA-KTX */
160 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0},
161 /* Realtek RTL8150L Generic (GREEN HOUSE USBKR100) */
162 {{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8150L}, 0},
163 /* Longshine LCS-8138TX */
164 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_LCS8138TX}, 0},
165 /* Micronet SP128AR */
166 {{ USB_VENDOR_MICRONET, USB_PRODUCT_MICRONET_SP128AR}, 0},
167 };
168 #define url_lookup(v, p) ((struct url_type *)usb_lookup(url_devs, v, p))
169
170
171 /* Probe */
172 USB_MATCH(url)
173 {
174 USB_MATCH_START(url, uaa);
175
176 if (uaa->iface != NULL)
177 return (UMATCH_NONE);
178
179 return (url_lookup(uaa->vendor, uaa->product) != NULL ?
180 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
181 }
182 /* Attach */
183 USB_ATTACH(url)
184 {
185 USB_ATTACH_START(url, sc, uaa);
186 usbd_device_handle dev = uaa->device;
187 usbd_interface_handle iface;
188 usbd_status err;
189 usb_interface_descriptor_t *id;
190 usb_endpoint_descriptor_t *ed;
191 char devinfo[1024];
192 char *devname = USBDEVNAME(sc->sc_dev);
193 struct ifnet *ifp;
194 struct mii_data *mii;
195 u_char eaddr[ETHER_ADDR_LEN];
196 int i, s;
197
198 usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
199 USB_ATTACH_SETUP;
200 printf("%s: %s\n", devname, devinfo);
201
202 /* Move the device into the configured state. */
203 err = usbd_set_config_no(dev, URL_CONFIG_NO, 1);
204 if (err) {
205 printf("%s: setting config no failed\n", devname);
206 goto bad;
207 }
208
209 usb_init_task(&sc->sc_tick_task, url_tick_task, sc);
210 lockinit(&sc->sc_mii_lock, PZERO, "urlmii", 0, 0);
211 usb_init_task(&sc->sc_stop_task, (void (*)(void *)) url_stop_task, sc);
212
213 /* get control interface */
214 err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface);
215 if (err) {
216 printf("%s: failed to get interface, err=%s\n", devname,
217 usbd_errstr(err));
218 goto bad;
219 }
220
221 sc->sc_udev = dev;
222 sc->sc_ctl_iface = iface;
223 sc->sc_flags = url_lookup(uaa->vendor, uaa->product)->url_flags;
224
225 /* get interface descriptor */
226 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
227
228 /* find endpoints */
229 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
230 for (i = 0; i < id->bNumEndpoints; i++) {
231 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
232 if (ed == NULL) {
233 printf("%s: couldn't get endpoint %d\n", devname, i);
234 goto bad;
235 }
236 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
237 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
238 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
239 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
240 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
241 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
242 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
243 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
244 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
245 }
246
247 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
248 sc->sc_intrin_no == -1) {
249 printf("%s: missing endpoint\n", devname);
250 goto bad;
251 }
252
253 s = splnet();
254
255 /* reset the adapter */
256 url_reset(sc);
257
258 /* Get Ethernet Address */
259 err = url_mem(sc, URL_CMD_READMEM, URL_IDR0, (void *)eaddr,
260 ETHER_ADDR_LEN);
261 if (err) {
262 printf("%s: read MAC address failed\n", devname);
263 splx(s);
264 goto bad;
265 }
266
267 /* Print Ethernet Address */
268 printf("%s: Ethernet address %s\n", devname, ether_sprintf(eaddr));
269
270 /* initialize interface infomation */
271 ifp = GET_IFP(sc);
272 ifp->if_softc = sc;
273 ifp->if_mtu = ETHERMTU;
274 strncpy(ifp->if_xname, devname, IFNAMSIZ);
275 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
276 ifp->if_start = url_start;
277 ifp->if_ioctl = url_ioctl;
278 ifp->if_watchdog = url_watchdog;
279 ifp->if_init = url_init;
280 ifp->if_stop = url_stop;
281
282 IFQ_SET_READY(&ifp->if_snd);
283
284 /*
285 * Do ifmedia setup.
286 */
287 mii = &sc->sc_mii;
288 mii->mii_ifp = ifp;
289 mii->mii_readreg = url_int_miibus_readreg;
290 mii->mii_writereg = url_int_miibus_writereg;
291 #if 0
292 if (sc->sc_flags & URL_EXT_PHY) {
293 mii->mii_readreg = url_ext_miibus_readreg;
294 mii->mii_writereg = url_ext_miibus_writereg;
295 }
296 #endif
297 mii->mii_statchg = url_miibus_statchg;
298 mii->mii_flags = MIIF_AUTOTSLEEP;
299 ifmedia_init(&mii->mii_media, 0,
300 url_ifmedia_change, url_ifmedia_status);
301 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
302 if (LIST_FIRST(&mii->mii_phys) == NULL) {
303 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
304 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
305 } else
306 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
307
308 /* attach the interface */
309 if_attach(ifp);
310 Ether_ifattach(ifp, eaddr);
311
312 #if NRND > 0
313 rnd_attach_source(&sc->rnd_source, devname, RND_TYPE_NET, 0);
314 #endif
315
316 usb_callout_init(sc->sc_stat_ch);
317 sc->sc_attached = 1;
318 splx(s);
319
320 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
321
322 USB_ATTACH_SUCCESS_RETURN;
323
324 bad:
325 sc->sc_dying = 1;
326 USB_ATTACH_ERROR_RETURN;
327 }
328
329 /* detach */
330 USB_DETACH(url)
331 {
332 USB_DETACH_START(url, sc);
333 struct ifnet *ifp = GET_IFP(sc);
334 int s;
335
336 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
337
338 /* Detached before attached finished */
339 if (!sc->sc_attached)
340 return (0);
341
342 usb_uncallout(sc->sc_stat_ch, url_tick, sc);
343
344 /* Remove any pending tasks */
345 usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
346 usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
347
348 s = splusb();
349
350 if (--sc->sc_refcnt >= 0) {
351 /* Wait for processes to go away */
352 usb_detach_wait(USBDEV(sc->sc_dev));
353 }
354
355 if (ifp->if_flags & IFF_RUNNING)
356 url_stop(GET_IFP(sc), 1);
357
358 #if NRND > 0
359 rnd_detach_source(&sc->rnd_source);
360 #endif
361 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
362 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
363 ether_ifdetach(ifp);
364 if_detach(ifp);
365
366 #ifdef DIAGNOSTIC
367 if (sc->sc_pipe_tx != NULL)
368 printf("%s: detach has active tx endpoint.\n",
369 USBDEVNAME(sc->sc_dev));
370 if (sc->sc_pipe_rx != NULL)
371 printf("%s: detach has active rx endpoint.\n",
372 USBDEVNAME(sc->sc_dev));
373 if (sc->sc_pipe_intr != NULL)
374 printf("%s: detach has active intr endpoint.\n",
375 USBDEVNAME(sc->sc_dev));
376 #endif
377
378 sc->sc_attached = 0;
379
380 splx(s);
381
382 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
383 USBDEV(sc->sc_dev));
384
385 return (0);
386 }
387
388 /* read/write memory */
389 Static int
390 url_mem(struct url_softc *sc, int cmd, int offset, void *buf, int len)
391 {
392 usb_device_request_t req;
393 usbd_status err;
394
395 if (sc == NULL)
396 return (0);
397
398 DPRINTFN(0x200,
399 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
400
401 if (sc->sc_dying)
402 return (0);
403
404 if (cmd == URL_CMD_READMEM)
405 req.bmRequestType = UT_READ_VENDOR_DEVICE;
406 else
407 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
408 req.bRequest = URL_REQ_MEM;
409 USETW(req.wValue, offset);
410 USETW(req.wIndex, 0x0000);
411 USETW(req.wLength, len);
412
413 sc->sc_refcnt++;
414 err = usbd_do_request(sc->sc_udev, &req, buf);
415 if (--sc->sc_refcnt < 0)
416 usb_detach_wakeup(USBDEV(sc->sc_dev));
417 if (err) {
418 DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n",
419 USBDEVNAME(sc->sc_dev),
420 cmd == URL_CMD_READMEM ? "read" : "write",
421 offset, err));
422 }
423
424 return (err);
425 }
426
427 /* read 1byte from register */
428 Static int
429 url_csr_read_1(struct url_softc *sc, int reg)
430 {
431 u_int8_t val = 0;
432
433 DPRINTFN(0x100,
434 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
435
436 if (sc->sc_dying)
437 return (0);
438
439 return (url_mem(sc, URL_CMD_READMEM, reg, &val, 1) ? 0 : val);
440 }
441
442 /* read 2bytes from register */
443 Static int
444 url_csr_read_2(struct url_softc *sc, int reg)
445 {
446 uWord val;
447
448 DPRINTFN(0x100,
449 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
450
451 if (sc->sc_dying)
452 return (0);
453
454 USETW(val, 0);
455 return (url_mem(sc, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val));
456 }
457
458 /* write 1byte to register */
459 Static int
460 url_csr_write_1(struct url_softc *sc, int reg, int aval)
461 {
462 u_int8_t val = aval;
463
464 DPRINTFN(0x100,
465 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
466
467 if (sc->sc_dying)
468 return (0);
469
470 return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0);
471 }
472
473 /* write 2bytes to register */
474 Static int
475 url_csr_write_2(struct url_softc *sc, int reg, int aval)
476 {
477 uWord val;
478
479 DPRINTFN(0x100,
480 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
481
482 USETW(val, aval);
483
484 if (sc->sc_dying)
485 return (0);
486
487 return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0);
488 }
489
490 /* write 4bytes to register */
491 Static int
492 url_csr_write_4(struct url_softc *sc, int reg, int aval)
493 {
494 uDWord val;
495
496 DPRINTFN(0x100,
497 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
498
499 USETDW(val, aval);
500
501 if (sc->sc_dying)
502 return (0);
503
504 return (url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0);
505 }
506
507 Static int
508 url_init(struct ifnet *ifp)
509 {
510 struct url_softc *sc = ifp->if_softc;
511 struct mii_data *mii = GET_MII(sc);
512 u_char *eaddr;
513 int i, s;
514
515 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
516
517 if (sc->sc_dying)
518 return (EIO);
519
520 s = splnet();
521
522 /* Cancel pending I/O and free all TX/RX buffers */
523 url_stop(ifp, 1);
524
525 eaddr = LLADDR(ifp->if_sadl);
526 for (i = 0; i < ETHER_ADDR_LEN; i++)
527 url_csr_write_1(sc, URL_IDR0 + i, eaddr[i]);
528
529 /* Init transmission control register */
530 URL_CLRBIT(sc, URL_TCR,
531 URL_TCR_TXRR1 | URL_TCR_TXRR0 |
532 URL_TCR_IFG1 | URL_TCR_IFG0 |
533 URL_TCR_NOCRC);
534
535 /* Init receive control register */
536 URL_SETBIT2(sc, URL_RCR, URL_RCR_TAIL | URL_RCR_AD);
537 if (ifp->if_flags & IFF_BROADCAST)
538 URL_SETBIT2(sc, URL_RCR, URL_RCR_AB);
539 else
540 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AB);
541
542 /* If we want promiscuous mode, accept all physical frames. */
543 if (ifp->if_flags & IFF_PROMISC)
544 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
545 else
546 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
547
548
549 /* Initialize transmit ring */
550 if (url_tx_list_init(sc) == ENOBUFS) {
551 printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
552 splx(s);
553 return (EIO);
554 }
555
556 /* Initialize receive ring */
557 if (url_rx_list_init(sc) == ENOBUFS) {
558 printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
559 splx(s);
560 return (EIO);
561 }
562
563 /* Load the multicast filter */
564 url_setmulti(sc);
565
566 /* Enable RX and TX */
567 URL_SETBIT(sc, URL_CR, URL_CR_TE | URL_CR_RE);
568
569 mii_mediachg(mii);
570
571 if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
572 if (url_openpipes(sc)) {
573 splx(s);
574 return (EIO);
575 }
576 }
577
578 ifp->if_flags |= IFF_RUNNING;
579 ifp->if_flags &= ~IFF_OACTIVE;
580
581 splx(s);
582
583 usb_callout(sc->sc_stat_ch, hz, url_tick, sc);
584
585 return (0);
586 }
587
588 Static void
589 url_reset(struct url_softc *sc)
590 {
591 int i;
592
593 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
594
595 if (sc->sc_dying)
596 return;
597
598 URL_SETBIT(sc, URL_CR, URL_CR_SOFT_RST);
599
600 for (i = 0; i < URL_TX_TIMEOUT; i++) {
601 if (!(url_csr_read_1(sc, URL_CR) & URL_CR_SOFT_RST))
602 break;
603 delay(10); /* XXX */
604 }
605
606 delay(10000); /* XXX */
607 }
608
609 int
610 url_activate(device_ptr_t self, enum devact act)
611 {
612 struct url_softc *sc = (struct url_softc *)self;
613
614 DPRINTF(("%s: %s: enter, act=%d\n", USBDEVNAME(sc->sc_dev),
615 __func__, act));
616
617 switch (act) {
618 case DVACT_ACTIVATE:
619 return (EOPNOTSUPP);
620 break;
621
622 case DVACT_DEACTIVATE:
623 if_deactivate(&sc->sc_ec.ec_if);
624 sc->sc_dying = 1;
625 break;
626 }
627
628 return (0);
629 }
630
631 #define url_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
632
633
634 Static void
635 url_setmulti(struct url_softc *sc)
636 {
637 struct ifnet *ifp;
638 struct ether_multi *enm;
639 struct ether_multistep step;
640 u_int32_t hashes[2] = { 0, 0 };
641 int h = 0;
642 int mcnt = 0;
643
644 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
645
646 if (sc->sc_dying)
647 return;
648
649 ifp = GET_IFP(sc);
650
651 if (ifp->if_flags & IFF_PROMISC) {
652 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
653 return;
654 } else if (ifp->if_flags & IFF_ALLMULTI) {
655 allmulti:
656 ifp->if_flags |= IFF_ALLMULTI;
657 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM);
658 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAP);
659 return;
660 }
661
662 /* first, zot all the existing hash bits */
663 url_csr_write_4(sc, URL_MAR0, 0);
664 url_csr_write_4(sc, URL_MAR4, 0);
665
666 /* now program new ones */
667 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
668 while (enm != NULL) {
669 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
670 ETHER_ADDR_LEN) != 0)
671 goto allmulti;
672
673 h = url_calchash(enm->enm_addrlo);
674 if (h < 32)
675 hashes[0] |= (1 << h);
676 else
677 hashes[1] |= (1 << (h -32));
678 mcnt++;
679 ETHER_NEXT_MULTI(step, enm);
680 }
681
682 ifp->if_flags &= ~IFF_ALLMULTI;
683
684 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
685
686 if (mcnt){
687 URL_SETBIT2(sc, URL_RCR, URL_RCR_AM);
688 } else {
689 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AM);
690 }
691 url_csr_write_4(sc, URL_MAR0, hashes[0]);
692 url_csr_write_4(sc, URL_MAR4, hashes[1]);
693 }
694
695 Static int
696 url_openpipes(struct url_softc *sc)
697 {
698 struct url_chain *c;
699 usbd_status err;
700 int i;
701 int error = 0;
702
703 if (sc->sc_dying)
704 return (EIO);
705
706 sc->sc_refcnt++;
707
708 /* Open RX pipe */
709 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
710 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
711 if (err) {
712 printf("%s: open rx pipe failed: %s\n",
713 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
714 error = EIO;
715 goto done;
716 }
717
718 /* Open TX pipe */
719 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
720 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
721 if (err) {
722 printf("%s: open tx pipe failed: %s\n",
723 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
724 error = EIO;
725 goto done;
726 }
727
728 #if 0
729 /* XXX: interrupt endpoint is not yet supported */
730 /* Open Interrupt pipe */
731 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
732 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
733 &sc->sc_cdata.url_ibuf, URL_INTR_PKGLEN,
734 url_intr, URL_INTR_INTERVAL);
735 if (err) {
736 printf("%s: open intr pipe failed: %s\n",
737 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
738 error = EIO;
739 goto done;
740 }
741 #endif
742
743
744 /* Start up the receive pipe. */
745 for (i = 0; i < URL_RX_LIST_CNT; i++) {
746 c = &sc->sc_cdata.url_rx_chain[i];
747 usbd_setup_xfer(c->url_xfer, sc->sc_pipe_rx,
748 c, c->url_buf, URL_BUFSZ,
749 USBD_SHORT_XFER_OK | USBD_NO_COPY,
750 USBD_NO_TIMEOUT, url_rxeof);
751 (void)usbd_transfer(c->url_xfer);
752 DPRINTF(("%s: %s: start read\n", USBDEVNAME(sc->sc_dev),
753 __func__));
754 }
755
756 done:
757 if (--sc->sc_refcnt < 0)
758 usb_detach_wakeup(USBDEV(sc->sc_dev));
759
760 return (error);
761 }
762
763 Static int
764 url_newbuf(struct url_softc *sc, struct url_chain *c, struct mbuf *m)
765 {
766 struct mbuf *m_new = NULL;
767
768 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
769
770 if (m == NULL) {
771 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
772 if (m_new == NULL) {
773 printf("%s: no memory for rx list "
774 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
775 return (ENOBUFS);
776 }
777 MCLGET(m_new, M_DONTWAIT);
778 if (!(m_new->m_flags & M_EXT)) {
779 printf("%s: no memory for rx list "
780 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
781 m_freem(m_new);
782 return (ENOBUFS);
783 }
784 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
785 } else {
786 m_new = m;
787 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
788 m_new->m_data = m_new->m_ext.ext_buf;
789 }
790
791 m_adj(m_new, ETHER_ALIGN);
792 c->url_mbuf = m_new;
793
794 return (0);
795 }
796
797
798 Static int
799 url_rx_list_init(struct url_softc *sc)
800 {
801 struct url_cdata *cd;
802 struct url_chain *c;
803 int i;
804
805 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
806
807 cd = &sc->sc_cdata;
808 for (i = 0; i < URL_RX_LIST_CNT; i++) {
809 c = &cd->url_rx_chain[i];
810 c->url_sc = sc;
811 c->url_idx = i;
812 if (url_newbuf(sc, c, NULL) == ENOBUFS)
813 return (ENOBUFS);
814 if (c->url_xfer == NULL) {
815 c->url_xfer = usbd_alloc_xfer(sc->sc_udev);
816 if (c->url_xfer == NULL)
817 return (ENOBUFS);
818 c->url_buf = usbd_alloc_buffer(c->url_xfer, URL_BUFSZ);
819 if (c->url_buf == NULL) {
820 usbd_free_xfer(c->url_xfer);
821 return (ENOBUFS);
822 }
823 }
824 }
825
826 return (0);
827 }
828
829 Static int
830 url_tx_list_init(struct url_softc *sc)
831 {
832 struct url_cdata *cd;
833 struct url_chain *c;
834 int i;
835
836 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
837
838 cd = &sc->sc_cdata;
839 for (i = 0; i < URL_TX_LIST_CNT; i++) {
840 c = &cd->url_tx_chain[i];
841 c->url_sc = sc;
842 c->url_idx = i;
843 c->url_mbuf = NULL;
844 if (c->url_xfer == NULL) {
845 c->url_xfer = usbd_alloc_xfer(sc->sc_udev);
846 if (c->url_xfer == NULL)
847 return (ENOBUFS);
848 c->url_buf = usbd_alloc_buffer(c->url_xfer, URL_BUFSZ);
849 if (c->url_buf == NULL) {
850 usbd_free_xfer(c->url_xfer);
851 return (ENOBUFS);
852 }
853 }
854 }
855
856 return (0);
857 }
858
859 Static void
860 url_start(struct ifnet *ifp)
861 {
862 struct url_softc *sc = ifp->if_softc;
863 struct mbuf *m_head = NULL;
864
865 DPRINTF(("%s: %s: enter, link=%d\n", USBDEVNAME(sc->sc_dev),
866 __func__, sc->sc_link));
867
868 if (sc->sc_dying)
869 return;
870
871 if (!sc->sc_link)
872 return;
873
874 if (ifp->if_flags & IFF_OACTIVE)
875 return;
876
877 IFQ_POLL(&ifp->if_snd, m_head);
878 if (m_head == NULL)
879 return;
880
881 if (url_send(sc, m_head, 0)) {
882 ifp->if_flags |= IFF_OACTIVE;
883 return;
884 }
885
886 IFQ_DEQUEUE(&ifp->if_snd, m_head);
887
888 #if NBPFILTER > 0
889 if (ifp->if_bpf)
890 bpf_mtap(ifp->if_bpf, m_head);
891 #endif
892
893 ifp->if_flags |= IFF_OACTIVE;
894
895 /* Set a timeout in case the chip goes out to lunch. */
896 ifp->if_timer = 5;
897 }
898
899 Static int
900 url_send(struct url_softc *sc, struct mbuf *m, int idx)
901 {
902 int total_len;
903 struct url_chain *c;
904 usbd_status err;
905
906 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
907
908 c = &sc->sc_cdata.url_tx_chain[idx];
909
910 /* Copy the mbuf data into a contiguous buffer */
911 m_copydata(m, 0, m->m_pkthdr.len, c->url_buf);
912 c->url_mbuf = m;
913 total_len = m->m_pkthdr.len;
914
915 if (total_len < URL_MIN_FRAME_LEN) {
916 memset(c->url_buf + total_len, 0,
917 URL_MIN_FRAME_LEN - total_len);
918 total_len = URL_MIN_FRAME_LEN;
919 }
920 usbd_setup_xfer(c->url_xfer, sc->sc_pipe_tx, c, c->url_buf, total_len,
921 USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
922 URL_TX_TIMEOUT, url_txeof);
923
924 /* Transmit */
925 sc->sc_refcnt++;
926 err = usbd_transfer(c->url_xfer);
927 if (--sc->sc_refcnt < 0)
928 usb_detach_wakeup(USBDEV(sc->sc_dev));
929 if (err != USBD_IN_PROGRESS) {
930 printf("%s: url_send error=%s\n", USBDEVNAME(sc->sc_dev),
931 usbd_errstr(err));
932 /* Stop the interface */
933 usb_add_task(sc->sc_udev, &sc->sc_stop_task);
934 return (EIO);
935 }
936
937 DPRINTF(("%s: %s: send %d bytes\n", USBDEVNAME(sc->sc_dev),
938 __func__, total_len));
939
940 sc->sc_cdata.url_tx_cnt++;
941
942 return (0);
943 }
944
945 Static void
946 url_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, 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(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(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, caddr_t 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);
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