if_url.c revision 1.2 1 /* $NetBSD: if_url.c,v 1.2 2002/03/28 21:49:19 ichiro 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. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Shingo WATANABE.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 */
34
35 /*
36 * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at
37 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf
38 * ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf
39 */
40
41 /*
42 * TODO:
43 * Interrupt Endpoint support
44 * External PHYs
45 * powerhook() support?
46 */
47
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1.2 2002/03/28 21:49:19 ichiro Exp $");
50
51 #include "opt_inet.h"
52 #include "opt_ns.h"
53 #include "bpfilter.h"
54 #include "rnd.h"
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/lock.h>
59 #include <sys/mbuf.h>
60 #include <sys/kernel.h>
61 #include <sys/socket.h>
62
63 #include <sys/device.h>
64 #if NRND > 0
65 #include <sys/rnd.h>
66 #endif
67
68 #include <net/if.h>
69 #include <net/if_arp.h>
70 #include <net/if_dl.h>
71 #include <net/if_media.h>
72
73 #if NBPFILTER > 0
74 #include <net/bpf.h>
75 #endif
76 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
77
78 #include <net/if_ether.h>
79 #ifdef INET
80 #include <netinet/in.h>
81 #include <netinet/if_inarp.h>
82 #endif
83 #ifdef NS
84 #include <netns/ns.h>
85 #include <netns/ns_if.h>
86 #endif
87
88 #include <dev/mii/mii.h>
89 #include <dev/mii/miivar.h>
90 #include <dev/mii/urlphyreg.h>
91
92 #include <dev/usb/usb.h>
93 #include <dev/usb/usbdi.h>
94 #include <dev/usb/usbdi_util.h>
95 #include <dev/usb/usbdevs.h>
96
97 #include <dev/usb/if_urlreg.h>
98
99
100 /* Function declarations */
101 USB_DECLARE_DRIVER(url);
102
103 Static int url_openpipes(struct url_softc *);
104 Static int url_rx_list_init(struct url_softc *);
105 Static int url_tx_list_init(struct url_softc *);
106 Static int url_newbuf(struct url_softc *, struct url_chain *, struct mbuf *);
107 Static void url_start(struct ifnet *);
108 Static int url_send(struct url_softc *, struct mbuf *, int);
109 Static void url_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
110 Static void url_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
111 Static void url_tick(void *);
112 Static void url_tick_task(void *);
113 Static int url_ioctl(struct ifnet *, u_long, caddr_t);
114 Static void url_stop_task(struct url_softc *);
115 Static void url_stop(struct ifnet *, int);
116 Static void url_watchdog(struct ifnet *);
117 Static int url_ifmedia_change(struct ifnet *);
118 Static void url_ifmedia_status(struct ifnet *, struct ifmediareq *);
119 Static void url_lock_mii(struct url_softc *);
120 Static void url_unlock_mii(struct url_softc *);
121 Static int url_int_miibus_readreg(device_ptr_t, int, int);
122 Static void url_int_miibus_writereg(device_ptr_t, int, int, int);
123 Static void url_miibus_statchg(device_ptr_t);
124 Static int url_init(struct ifnet *);
125 Static void url_setmulti(struct url_softc *);
126 Static void url_reset(struct url_softc *);
127
128 Static int url_csr_read_1(struct url_softc *, int);
129 Static int url_csr_read_2(struct url_softc *, int);
130 Static int url_csr_write_1(struct url_softc *, int, int);
131 Static int url_csr_write_2(struct url_softc *, int, int);
132 Static int url_csr_write_4(struct url_softc *, int, int);
133 Static int url_mem(struct url_softc *, int, int, void *, int);
134
135 /* Macros */
136 #ifdef URL_DEBUG
137 #define DPRINTF(x) if (urldebug) logprintf x
138 #define DPRINTFN(n,x) if (urldebug >= (n)) logprintf x
139 int urldebug = 0;
140 #else
141 #define DPRINTF(x)
142 #define DPRINTFN(n,x)
143 #endif
144
145 #define URL_SETBIT(sc, reg, x) \
146 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) | (x))
147
148 #define URL_SETBIT2(sc, reg, x) \
149 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) | (x))
150
151 #define URL_CLRBIT(sc, reg, x) \
152 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) & ~(x))
153
154 #define URL_CLRBIT2(sc, reg, x) \
155 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) & ~(x))
156
157 static const struct url_type {
158 struct usb_devno url_dev;
159 u_int16_t url_flags;
160 #define URL_EXT_PHY 0x0001
161 } url_devs [] = {
162 /* MELCO LUA-KTX */
163 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0},
164 /* GREEN HOUSE USBKR100 */
165 {{ USB_VENDOR_GREENHOUSE2, USB_PRODUCT_GREENHOUSE2_USBKR100}, 0}
166 };
167 #define url_lookup(v, p) ((struct url_type *)usb_lookup(url_devs, v, p))
168
169
170 /* Probe */
171 USB_MATCH(url)
172 {
173 USB_MATCH_START(url, uaa);
174
175 if (uaa->iface != NULL)
176 return (UMATCH_NONE);
177
178 return (url_lookup(uaa->vendor, uaa->product) != NULL ?
179 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
180 }
181 /* Attach */
182 USB_ATTACH(url)
183 {
184 USB_ATTACH_START(url, sc, uaa);
185 usbd_device_handle dev = uaa->device;
186 usbd_interface_handle iface;
187 usbd_status err;
188 usb_interface_descriptor_t *id;
189 usb_endpoint_descriptor_t *ed;
190 char devinfo[1024];
191 char *devname = USBDEVNAME(sc->sc_dev);
192 struct ifnet *ifp;
193 struct mii_data *mii;
194 u_char eaddr[ETHER_ADDR_LEN];
195 int i, s;
196
197 usbd_devinfo(dev, 0, devinfo);
198 USB_ATTACH_SETUP;
199 printf("%s: %s\n", devname, devinfo);
200
201 /* Move the device into the configured state. */
202 err = usbd_set_config_no(dev, URL_CONFIG_NO, 1);
203 if (err) {
204 printf("%s: setting config no failed\n", devname);
205 goto bad;
206 }
207
208 usb_init_task(&sc->sc_tick_task, url_tick_task, sc);
209 lockinit(&sc->sc_mii_lock, PZERO, "urlmii", 0, 0);
210 usb_init_task(&sc->sc_stop_task, (void (*)(void *)) url_stop_task, sc);
211
212 /* get control interface */
213 err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface);
214 if (err) {
215 printf("%s: failed to get interface, err=%s\n", devname,
216 usbd_errstr(err));
217 goto bad;
218 }
219
220 sc->sc_udev = dev;
221 sc->sc_ctl_iface = iface;
222 sc->sc_flags = url_lookup(uaa->vendor, uaa->product)->url_flags;
223
224 /* get interface descriptor */
225 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
226
227 /* find endpoints */
228 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
229 for (i = 0; i < id->bNumEndpoints; i++) {
230 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
231 if (ed == NULL) {
232 printf("%s: couldn't get endpoint %d\n", devname, i);
233 goto bad;
234 }
235 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
236 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
237 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
238 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
239 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
240 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
241 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
242 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
243 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
244 }
245
246 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
247 sc->sc_intrin_no == -1) {
248 printf("%s: missing endpoint\n", devname);
249 goto bad;
250 }
251
252 s = splnet();
253
254 /* reset the adapter */
255 url_reset(sc);
256
257 /* Get Ethernet Address */
258 err = url_mem(sc, URL_CMD_READMEM, URL_IDR0, (void *)eaddr,
259 ETHER_ADDR_LEN);
260 if (err) {
261 printf("%s: read MAC address faild\n", devname);
262 splx(s);
263 goto bad;
264 }
265
266 /* Print Ethernet Address */
267 printf("%s: Ethernet address %s\n", devname, ether_sprintf(eaddr));
268
269 /* initialize interface infomation */
270 ifp = GET_IFP(sc);
271 ifp->if_softc = sc;
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), __FUNCTION__));
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), __FUNCTION__));
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), __FUNCTION__));
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), __FUNCTION__));
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), __FUNCTION__));
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), __FUNCTION__));
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), __FUNCTION__));
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), __FUNCTION__));
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), __FUNCTION__));
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 __FUNCTION__, 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), __FUNCTION__));
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, URL_INTR_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 __FUNCTION__));
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), __FUNCTION__));
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), __FUNCTION__));
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), __FUNCTION__));
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 __FUNCTION__, 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),__FUNCTION__));
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 total_len = URL_MIN_FRAME_LEN;
915 usbd_setup_xfer(c->url_xfer, sc->sc_pipe_tx, c, c->url_buf, total_len,
916 USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
917 URL_TX_TIMEOUT, url_txeof);
918
919 /* Transmit */
920 sc->sc_refcnt++;
921 err = usbd_transfer(c->url_xfer);
922 if (--sc->sc_refcnt < 0)
923 usb_detach_wakeup(USBDEV(sc->sc_dev));
924 if (err != USBD_IN_PROGRESS) {
925 printf("%s: url_send error=%s\n", USBDEVNAME(sc->sc_dev),
926 usbd_errstr(err));
927 /* Stop the interface */
928 usb_add_task(sc->sc_udev, &sc->sc_stop_task);
929 return (EIO);
930 }
931
932 DPRINTF(("%s: %s: send %d bytes\n", USBDEVNAME(sc->sc_dev),
933 __FUNCTION__, total_len));
934
935 sc->sc_cdata.url_tx_cnt++;
936
937 return (0);
938 }
939
940 Static void
941 url_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
942 {
943 struct url_chain *c = priv;
944 struct url_softc *sc = c->url_sc;
945 struct ifnet *ifp = GET_IFP(sc);
946 int s;
947
948 if (sc->sc_dying)
949 return;
950
951 s = splnet();
952
953 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
954
955 ifp->if_timer = 0;
956 ifp->if_flags &= ~IFF_OACTIVE;
957
958 if (status != USBD_NORMAL_COMPLETION) {
959 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
960 splx(s);
961 return;
962 }
963 ifp->if_oerrors++;
964 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
965 usbd_errstr(status));
966 if (status == USBD_STALLED) {
967 sc->sc_refcnt++;
968 usbd_clear_endpoint_stall(sc->sc_pipe_tx);
969 if (--sc->sc_refcnt < 0)
970 usb_detach_wakeup(USBDEV(sc->sc_dev));
971 }
972 splx(s);
973 return;
974 }
975
976 ifp->if_opackets++;
977
978 m_free(c->url_mbuf);
979 c->url_mbuf = NULL;
980
981 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
982 url_start(ifp);
983
984 splx(s);
985 }
986
987 Static void
988 url_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
989 {
990 struct url_chain *c = priv;
991 struct url_softc *sc = c->url_sc;
992 struct ifnet *ifp = GET_IFP(sc);
993 struct mbuf *m;
994 u_int32_t total_len;
995 url_rxhdr_t rxhdr;
996 int s;
997
998 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
999
1000 if (sc->sc_dying)
1001 return;
1002
1003 if (status != USBD_NORMAL_COMPLETION) {
1004 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1005 return;
1006 sc->sc_rx_errs++;
1007 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1008 printf("%s: %u usb errors on rx: %s\n",
1009 USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
1010 usbd_errstr(status));
1011 sc->sc_rx_errs = 0;
1012 }
1013 if (status == USBD_STALLED) {
1014 sc->sc_refcnt++;
1015 usbd_clear_endpoint_stall(sc->sc_pipe_rx);
1016 if (--sc->sc_refcnt < 0)
1017 usb_detach_wakeup(USBDEV(sc->sc_dev));
1018 }
1019 goto done;
1020 }
1021
1022 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1023
1024 memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len);
1025
1026 if (total_len <= ETHER_CRC_LEN) {
1027 ifp->if_ierrors++;
1028 goto done;
1029 }
1030
1031 memcpy(&rxhdr, c->url_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr));
1032
1033 DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n",
1034 USBDEVNAME(sc->sc_dev),
1035 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK,
1036 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "",
1037 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "",
1038 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "",
1039 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : ""));
1040
1041 if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) {
1042 ifp->if_ierrors++;
1043 goto done;
1044 }
1045
1046 ifp->if_ipackets++;
1047 total_len -= ETHER_CRC_LEN;
1048
1049 m = c->url_mbuf;
1050 m->m_pkthdr.len = m->m_len = total_len;
1051 m->m_pkthdr.rcvif = ifp;
1052
1053 s = splnet();
1054
1055 if (url_newbuf(sc, c, NULL) == ENOBUFS) {
1056 ifp->if_ierrors++;
1057 goto done1;
1058 }
1059
1060 #if NBPFILTER > 0
1061 if (ifp->if_bpf)
1062 BPF_MTAP(ifp, m);
1063 #endif
1064
1065 DPRINTF(("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
1066 __FUNCTION__, m->m_len));
1067 IF_INPUT(ifp, m);
1068
1069 done1:
1070 splx(s);
1071
1072 done:
1073 /* Setup new transfer */
1074 usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->url_buf, URL_BUFSZ,
1075 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1076 USBD_NO_TIMEOUT, url_rxeof);
1077 sc->sc_refcnt++;
1078 usbd_transfer(xfer);
1079 if (--sc->sc_refcnt < 0)
1080 usb_detach_wakeup(USBDEV(sc->sc_dev));
1081
1082 DPRINTF(("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1083 }
1084
1085 #if 0
1086 Static void url_intr()
1087 {
1088 }
1089 #endif
1090
1091 Static int
1092 url_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1093 {
1094 struct url_softc *sc = ifp->if_softc;
1095 struct ifreq *ifr = (struct ifreq *)data;
1096 struct mii_data *mii;
1097 int s, error = 0;
1098
1099 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1100
1101 if (sc->sc_dying)
1102 return (EIO);
1103
1104 s = splnet();
1105
1106 switch (cmd) {
1107 case SIOCGIFMEDIA:
1108 case SIOCSIFMEDIA:
1109 mii = GET_MII(sc);
1110 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1111 break;
1112
1113 default:
1114 error = ether_ioctl(ifp, cmd, data);
1115 if (error == ENETRESET) {
1116 url_setmulti(sc);
1117 error = 0;
1118 }
1119 break;
1120 }
1121
1122 splx(s);
1123
1124 return (error);
1125 }
1126
1127 Static void
1128 url_watchdog(struct ifnet *ifp)
1129 {
1130 struct url_softc *sc = ifp->if_softc;
1131 struct url_chain *c;
1132 usbd_status stat;
1133 int s;
1134
1135 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1136
1137 ifp->if_oerrors++;
1138 printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
1139
1140 s = splusb();
1141 c = &sc->sc_cdata.url_tx_chain[0];
1142 usbd_get_xfer_status(c->url_xfer, NULL, NULL, NULL, &stat);
1143 url_txeof(c->url_xfer, c, stat);
1144
1145 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1146 url_start(ifp);
1147 splx(s);
1148 }
1149
1150 Static void
1151 url_stop_task(struct url_softc *sc)
1152 {
1153 url_stop(GET_IFP(sc), 1);
1154 }
1155
1156 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1157 Static void
1158 url_stop(struct ifnet *ifp, int disable)
1159 {
1160 struct url_softc *sc = ifp->if_softc;
1161 usbd_status err;
1162 int i;
1163
1164 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1165
1166 ifp->if_timer = 0;
1167
1168 url_reset(sc);
1169
1170 usb_uncallout(sc->sc_stat_ch, url_tick, sc);
1171
1172 /* Stop transfers */
1173 /* RX endpoint */
1174 if (sc->sc_pipe_rx != NULL) {
1175 err = usbd_abort_pipe(sc->sc_pipe_rx);
1176 if (err)
1177 printf("%s: abort rx pipe failed: %s\n",
1178 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1179 err = usbd_close_pipe(sc->sc_pipe_rx);
1180 if (err)
1181 printf("%s: close rx pipe failed: %s\n",
1182 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1183 sc->sc_pipe_rx = NULL;
1184 }
1185
1186 /* TX endpoint */
1187 if (sc->sc_pipe_tx != NULL) {
1188 err = usbd_abort_pipe(sc->sc_pipe_tx);
1189 if (err)
1190 printf("%s: abort tx pipe failed: %s\n",
1191 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1192 err = usbd_close_pipe(sc->sc_pipe_tx);
1193 if (err)
1194 printf("%s: close tx pipe failed: %s\n",
1195 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1196 sc->sc_pipe_tx = NULL;
1197 }
1198
1199 #if 0
1200 /* XXX: Interrupt endpoint is not yet supported!! */
1201 /* Interrupt endpoint */
1202 if (sc->sc_pipe_intr != NULL) {
1203 err = usbd_abort_pipe(sc->sc_pipe_intr);
1204 if (err)
1205 printf("%s: abort intr pipe failed: %s\n",
1206 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1207 err = usbd_close_pipe(sc->sc_pipe_intr);
1208 if (err)
1209 printf("%s: close intr pipe failed: %s\n",
1210 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1211 sc->sc_pipe_intr = NULL;
1212 }
1213 #endif
1214
1215 /* Free RX resources. */
1216 for (i = 0; i < URL_RX_LIST_CNT; i++) {
1217 if (sc->sc_cdata.url_rx_chain[i].url_mbuf != NULL) {
1218 m_freem(sc->sc_cdata.url_rx_chain[i].url_mbuf);
1219 sc->sc_cdata.url_rx_chain[i].url_mbuf = NULL;
1220 }
1221 if (sc->sc_cdata.url_rx_chain[i].url_xfer != NULL) {
1222 usbd_free_xfer(sc->sc_cdata.url_rx_chain[i].url_xfer);
1223 sc->sc_cdata.url_rx_chain[i].url_xfer = NULL;
1224 }
1225 }
1226
1227 /* Free TX resources. */
1228 for (i = 0; i < URL_TX_LIST_CNT; i++) {
1229 if (sc->sc_cdata.url_tx_chain[i].url_mbuf != NULL) {
1230 m_freem(sc->sc_cdata.url_tx_chain[i].url_mbuf);
1231 sc->sc_cdata.url_tx_chain[i].url_mbuf = NULL;
1232 }
1233 if (sc->sc_cdata.url_tx_chain[i].url_xfer != NULL) {
1234 usbd_free_xfer(sc->sc_cdata.url_tx_chain[i].url_xfer);
1235 sc->sc_cdata.url_tx_chain[i].url_xfer = NULL;
1236 }
1237 }
1238
1239 sc->sc_link = 0;
1240 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1241 }
1242
1243 /* Set media options */
1244 Static int
1245 url_ifmedia_change(struct ifnet *ifp)
1246 {
1247 struct url_softc *sc = ifp->if_softc;
1248 struct mii_data *mii = GET_MII(sc);
1249
1250 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1251
1252 if (sc->sc_dying)
1253 return (0);
1254
1255 sc->sc_link = 0;
1256 if (mii->mii_instance) {
1257 struct mii_softc *miisc;
1258 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1259 miisc = LIST_NEXT(miisc, mii_list))
1260 mii_phy_reset(miisc);
1261 }
1262
1263 return (mii_mediachg(mii));
1264 }
1265
1266 /* Report current media status. */
1267 Static void
1268 url_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1269 {
1270 struct url_softc *sc = ifp->if_softc;
1271 struct mii_data *mii = GET_MII(sc);
1272
1273 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1274
1275 if (sc->sc_dying)
1276 return;
1277
1278 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1279 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1280 ifmr->ifm_status = 0;
1281 return;
1282 }
1283
1284 mii_pollstat(mii);
1285 ifmr->ifm_active = mii->mii_media_active;
1286 ifmr->ifm_status = mii->mii_media_status;
1287 }
1288
1289 Static void
1290 url_tick(void *xsc)
1291 {
1292 struct url_softc *sc = xsc;
1293
1294 if (sc == NULL)
1295 return;
1296
1297 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1298 __FUNCTION__));
1299
1300 if (sc->sc_dying)
1301 return;
1302
1303 /* Perform periodic stuff in process context */
1304 usb_add_task(sc->sc_udev, &sc->sc_tick_task);
1305 }
1306
1307 Static void
1308 url_tick_task(void *xsc)
1309 {
1310 struct url_softc *sc = xsc;
1311 struct ifnet *ifp;
1312 struct mii_data *mii;
1313 int s;
1314
1315 if (sc == NULL)
1316 return;
1317
1318 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1319 __FUNCTION__));
1320
1321 if (sc->sc_dying)
1322 return;
1323
1324 ifp = GET_IFP(sc);
1325 mii = GET_MII(sc);
1326
1327 if (mii == NULL)
1328 return;
1329
1330 s = splnet();
1331
1332 mii_tick(mii);
1333 if (!sc->sc_link) {
1334 mii_pollstat(mii);
1335 if (mii->mii_media_status & IFM_ACTIVE &&
1336 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1337 DPRINTF(("%s: %s: got link\n",
1338 USBDEVNAME(sc->sc_dev), __FUNCTION__));
1339 sc->sc_link++;
1340 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1341 url_start(ifp);
1342 }
1343 }
1344
1345 usb_callout(sc->sc_stat_ch, hz, url_tick, sc);
1346
1347 splx(s);
1348 }
1349
1350 /* Get exclusive access to the MII registers */
1351 Static void
1352 url_lock_mii(struct url_softc *sc)
1353 {
1354 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1355 __FUNCTION__));
1356
1357 sc->sc_refcnt++;
1358 lockmgr(&sc->sc_mii_lock, LK_EXCLUSIVE, NULL);
1359 }
1360
1361 Static void
1362 url_unlock_mii(struct url_softc *sc)
1363 {
1364 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1365 __FUNCTION__));
1366
1367 lockmgr(&sc->sc_mii_lock, LK_RELEASE, NULL);
1368 if (--sc->sc_refcnt < 0)
1369 usb_detach_wakeup(USBDEV(sc->sc_dev));
1370 }
1371
1372 Static int
1373 url_int_miibus_readreg(device_ptr_t dev, int phy, int reg)
1374 {
1375 struct url_softc *sc;
1376 u_int16_t val;
1377
1378 if (dev == NULL)
1379 return (0);
1380
1381 sc = USBGETSOFTC(dev);
1382
1383 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1384 USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg));
1385
1386 if (sc->sc_dying) {
1387 #ifdef DIAGNOSTIC
1388 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1389 __FUNCTION__);
1390 #endif
1391 return (0);
1392 }
1393
1394 /* XXX: one PHY only for the RTL8150 internal PHY */
1395 if (phy != 0) {
1396 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1397 USBDEVNAME(sc->sc_dev), __FUNCTION__, phy));
1398 return (0);
1399 }
1400
1401 url_lock_mii(sc);
1402
1403 switch (reg) {
1404 case MII_BMCR: /* Control Register */
1405 reg = URL_BMCR;
1406 break;
1407 case MII_BMSR: /* Status Register */
1408 reg = URL_BMSR;
1409 break;
1410 case MII_PHYIDR1:
1411 case MII_PHYIDR2:
1412 val = 0;
1413 goto R_DONE;
1414 break;
1415 case MII_ANAR: /* Autonegotiation advertisement */
1416 reg = URL_ANAR;
1417 break;
1418 case MII_ANLPAR: /* Autonegotiation link partner abilities */
1419 reg = URL_ANLP;
1420 break;
1421 case URLPHY_MSR: /* Media Status Register */
1422 reg = URL_MSR;
1423 break;
1424 default:
1425 printf("%s: %s: bad register %04x\n",
1426 USBDEVNAME(sc->sc_dev), __FUNCTION__, reg);
1427 val = 0;
1428 goto R_DONE;
1429 break;
1430 }
1431
1432 if (reg == URL_MSR)
1433 val = url_csr_read_1(sc, reg);
1434 else
1435 val = url_csr_read_2(sc, reg);
1436
1437 R_DONE:
1438 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1439 USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg, val));
1440
1441 url_unlock_mii(sc);
1442 return (val);
1443 }
1444
1445 Static void
1446 url_int_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1447 {
1448 struct url_softc *sc;
1449
1450 if (dev == NULL)
1451 return;
1452
1453 sc = USBGETSOFTC(dev);
1454
1455 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1456 USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg, data));
1457
1458 if (sc->sc_dying) {
1459 #ifdef DIAGNOSTIC
1460 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1461 __FUNCTION__);
1462 #endif
1463 return;
1464 }
1465
1466 /* XXX: one PHY only for the RTL8150 internal PHY */
1467 if (phy != 0) {
1468 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1469 USBDEVNAME(sc->sc_dev), __FUNCTION__, phy));
1470 return;
1471 }
1472
1473 url_lock_mii(sc);
1474
1475 switch (reg) {
1476 case MII_BMCR: /* Control Register */
1477 reg = URL_BMCR;
1478 break;
1479 case MII_BMSR: /* Status Register */
1480 reg = URL_BMSR;
1481 break;
1482 case MII_PHYIDR1:
1483 case MII_PHYIDR2:
1484 goto W_DONE;
1485 break;
1486 case MII_ANAR: /* Autonegotiation advertisement */
1487 reg = URL_ANAR;
1488 break;
1489 case MII_ANLPAR: /* Autonegotiation link partner abilities */
1490 reg = URL_ANLP;
1491 break;
1492 case URLPHY_MSR: /* Media Status Register */
1493 reg = URL_MSR;
1494 break;
1495 default:
1496 printf("%s: %s: bad register %04x\n",
1497 USBDEVNAME(sc->sc_dev), __FUNCTION__, reg);
1498 goto W_DONE;
1499 break;
1500 }
1501
1502 if (reg == URL_MSR)
1503 url_csr_write_1(sc, reg, data);
1504 else
1505 url_csr_write_2(sc, reg, data);
1506 W_DONE:
1507
1508 url_unlock_mii(sc);
1509 return;
1510 }
1511
1512 Static void
1513 url_miibus_statchg(device_ptr_t dev)
1514 {
1515 #ifdef URL_DEBUG
1516 struct url_softc *sc;
1517
1518 if (dev == NULL)
1519 return;
1520
1521 sc = USBGETSOFTC(dev);
1522 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
1523 #endif
1524 /* Nothing to do */
1525 }
1526
1527 #if 0
1528 /*
1529 * external PHYs support, but not test.
1530 */
1531 Static int
1532 url_ext_miibus_redreg(device_ptr_t dev, int phy, int reg)
1533 {
1534 struct url_softc *sc = USBGETSOFTC(dev);
1535 u_int16_t val;
1536
1537 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n",
1538 USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg));
1539
1540 if (sc->sc_dying) {
1541 #ifdef DIAGNOSTIC
1542 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1543 __FUNCTION__);
1544 #endif
1545 return (0);
1546 }
1547
1548 url_lock_mii(sc);
1549
1550 url_csr_write_1(sc, URL_PHYADD, phy & URL_PHYADD_MASK);
1551 /*
1552 * RTL8150L will initiate a MII management data transaction
1553 * if PHYCNT_OWN bit is set 1 by software. After transaction,
1554 * this bit is auto cleared by TRL8150L.
1555 */
1556 url_csr_write_1(sc, URL_PHYCNT,
1557 (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR);
1558 for (i = 0; i < URL_TIMEOUT; i++) {
1559 if ((url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0)
1560 break;
1561 }
1562 if (i == URL_TIMEOUT) {
1563 printf("%s: MII read timed out\n", USBDEVNAME(sc->sc_dev));
1564 }
1565
1566 val = url_csr_read_2(sc, URL_PHYDAT);
1567
1568 DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1569 USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg, val));
1570
1571 url_unlock_mii(sc);
1572 return (val);
1573 }
1574
1575 Static void
1576 url_ext_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1577 {
1578 struct url_softc *sc = USBGETSOFTC(dev);
1579
1580 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1581 USBDEVNAME(sc->sc_dev), __FUNCTION__, phy, reg, data));
1582
1583 if (sc->sc_dying) {
1584 #ifdef DIAGNOSTIC
1585 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1586 __FUNCTION__);
1587 #endif
1588 return;
1589 }
1590
1591 url_lock_mii(sc);
1592
1593 url_csr_write_2(sc, URL_PHYDAT, data);
1594 url_csr_write_1(sc, URL_PHYADD, phy);
1595 url_csr_write_1(sc, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */
1596
1597 for (i=0; i < URL_TIMEOUT; i++) {
1598 if (url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN)
1599 break;
1600 }
1601
1602 if (i == URL_TIMEOUT) {
1603 printf("%s: MII write timed out\n",
1604 USBDEVNAME(sc->sc_dev));
1605 }
1606
1607 url_unlock_mii(sc);
1608 return;
1609 }
1610 #endif
1611
1612