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