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