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