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