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