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