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