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