FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/if_udav.c
1 /* $NetBSD: if_udav.c,v 1.2 2003/09/04 15:17:38 tsutsui Exp $ */
2 /* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */
3 /* $FreeBSD$ */
4 /*-
5 * Copyright (c) 2003
6 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 */
33
34 /*
35 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
36 * The spec can be found at the following url.
37 * http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-P01-930914.pdf
38 */
39
40 /*
41 * TODO:
42 * Interrupt Endpoint support
43 * External PHYs
44 * powerhook() support?
45 */
46
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49
50 #include "opt_inet.h"
51 #if defined(__NetBSD__)
52 #include "opt_ns.h"
53 #endif
54 #if defined(__NetBSD__)
55 #include "bpfilter.h"
56 #endif
57 #if defined(__FreeBSD__)
58 #define NBPFILTER 1
59 #endif
60 #if defined(__NetBSD__)
61 #include "rnd.h"
62 #endif
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/lock.h>
67 #include <sys/mbuf.h>
68 #include <sys/kernel.h>
69 #include <sys/module.h>
70 #include <sys/socket.h>
71 #if defined(__FreeBSD__)
72 #include <sys/types.h>
73 #include <sys/lockmgr.h>
74 #include <sys/sockio.h>
75 #endif
76
77 #if defined(__NetBSD__)
78 #include <sys/device.h>
79 #endif
80
81 #if NRND > 0
82 #include <sys/rnd.h>
83 #endif
84
85 #include <net/if.h>
86 #include <net/if_arp.h>
87 #include <net/if_dl.h>
88 #include <net/if_media.h>
89 #include <net/ethernet.h>
90
91 #if NBPFILTER > 0
92 #include <net/bpf.h>
93 #endif
94 #if defined(__NetBSD__)
95 #ifndef BPF_MTAP
96 #define BPF_MTAP(_ifp, _m) do { \
97 if ((_ifp)->if_bpf)) { \
98 bpf_mtap((_ifp)->if_bpf, (_m)) ; \
99 } \
100 } while (0)
101 #endif
102 #endif
103
104 #if defined(__NetBSD__)
105 #include <net/if_ether.h>
106 #ifdef INET
107 #include <netinet/in.h>
108 #include <netinet/if_inarp.h>
109 #endif /* INET */
110 #elif defined(__FreeBSD__) /* defined(__NetBSD__) */
111 #include <netinet/in.h>
112 #include <netinet/if_ether.h>
113 #endif /* defined(__FreeBSD__) */
114
115 #if defined(__NetBSD__)
116 #ifdef NS
117 #include <netns/ns.h>
118 #include <netns/ns_if.h>
119 #endif
120 #endif /* defined (__NetBSD__) */
121
122 #include <sys/bus.h>
123 #include <machine/bus.h>
124 #if __FreeBSD_version < 500000
125 #include <machine/clock.h>
126 #endif
127
128 #include <dev/mii/mii.h>
129 #include <dev/mii/miivar.h>
130
131 #include <dev/usb/usb.h>
132 #include <dev/usb/usbdi.h>
133 #include <dev/usb/usbdi_util.h>
134 #include "usbdevs.h"
135 #include <dev/usb/usbdivar.h>
136 #include <dev/usb/usb_ethersubr.h>
137
138 #include <dev/usb/if_udavreg.h>
139
140 #if defined(__FreeBSD__)
141 MODULE_DEPEND(udav, usb, 1, 1, 1);
142 MODULE_DEPEND(udav, ether, 1, 1, 1);
143 MODULE_DEPEND(udav, miibus, 1, 1, 1);
144 #endif
145
146 /* "controller miibus0" required. See GENERIC if you get errors here. */
147 #include "miibus_if.h"
148
149 #if !defined(__FreeBSD__)
150 /* Function declarations */
151 USB_DECLARE_DRIVER(udav);
152 #endif
153
154 #if defined(__FreeBSD__)
155 Static int udav_match(device_ptr_t);
156 Static int udav_attach(device_ptr_t);
157 Static int udav_detach(device_ptr_t);
158 Static void udav_shutdown(device_ptr_t);
159 #endif
160
161 Static int udav_openpipes(struct udav_softc *);
162 Static void udav_start(struct ifnet *);
163 Static int udav_send(struct udav_softc *, struct mbuf *, int);
164 Static void udav_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
165 #if defined(__FreeBSD__)
166 Static void udav_rxstart(struct ifnet *ifp);
167 #endif
168 Static void udav_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
169 Static void udav_tick(void *);
170 Static void udav_tick_task(void *);
171 Static int udav_ioctl(struct ifnet *, u_long, caddr_t);
172 Static void udav_stop_task(struct udav_softc *);
173 Static void udav_stop(struct ifnet *, int);
174 Static void udav_watchdog(struct ifnet *);
175 Static int udav_ifmedia_change(struct ifnet *);
176 Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
177 Static void udav_lock_mii(struct udav_softc *);
178 Static void udav_unlock_mii(struct udav_softc *);
179 Static int udav_miibus_readreg(device_ptr_t, int, int);
180 Static void udav_miibus_writereg(device_ptr_t, int, int, int);
181 Static void udav_miibus_statchg(device_ptr_t);
182 #if defined(__NetBSD__)
183 Static int udav_init(struct ifnet *);
184 #elif defined(__FreeBSD__)
185 Static void udav_init(void *);
186 #endif
187 Static void udav_setmulti(struct udav_softc *);
188 Static void udav_reset(struct udav_softc *);
189
190 Static int udav_csr_read(struct udav_softc *, int, void *, int);
191 Static int udav_csr_write(struct udav_softc *, int, void *, int);
192 Static int udav_csr_read1(struct udav_softc *, int);
193 Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
194
195 #if 0
196 Static int udav_mem_read(struct udav_softc *, int, void *, int);
197 Static int udav_mem_write(struct udav_softc *, int, void *, int);
198 Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
199 #endif
200
201 #if defined(__FreeBSD__)
202 Static device_method_t udav_methods[] = {
203 /* Device interface */
204 DEVMETHOD(device_probe, udav_match),
205 DEVMETHOD(device_attach, udav_attach),
206 DEVMETHOD(device_detach, udav_detach),
207 DEVMETHOD(device_shutdown, udav_shutdown),
208
209 /* bus interface */
210 DEVMETHOD(bus_print_child, bus_generic_print_child),
211 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
212
213 /* MII interface */
214 DEVMETHOD(miibus_readreg, udav_miibus_readreg),
215 DEVMETHOD(miibus_writereg, udav_miibus_writereg),
216 DEVMETHOD(miibus_statchg, udav_miibus_statchg),
217
218 { 0, 0 }
219 };
220
221 Static driver_t udav_driver = {
222 "udav",
223 udav_methods,
224 sizeof(struct udav_softc)
225 };
226
227 Static devclass_t udav_devclass;
228
229 DRIVER_MODULE(udav, uhub, udav_driver, udav_devclass, usbd_driver_load, 0);
230 DRIVER_MODULE(miibus, udav, miibus_driver, miibus_devclass, 0, 0);
231
232 #endif /* defined(__FreeBSD__) */
233
234 /* Macros */
235 #ifdef UDAV_DEBUG
236 #define DPRINTF(x) if (udavdebug) logprintf x
237 #define DPRINTFN(n,x) if (udavdebug >= (n)) logprintf x
238 int udavdebug = 0;
239 #else
240 #define DPRINTF(x)
241 #define DPRINTFN(n,x)
242 #endif
243
244 #define delay(d) DELAY(d)
245
246 #define UDAV_SETBIT(sc, reg, x) \
247 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
248
249 #define UDAV_CLRBIT(sc, reg, x) \
250 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
251
252 static const struct udav_type {
253 struct usb_devno udav_dev;
254 u_int16_t udav_flags;
255 #define UDAV_EXT_PHY 0x0001
256 } udav_devs [] = {
257 /* Corega USB-TXC */
258 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
259 #if 0
260 /* DAVICOM DM9601 Generic? */
261 /* XXX: The following ids was obtained from the data sheet. */
262 {{ 0x0a46, 0x9601 }, 0},
263 #endif
264 };
265 #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p))
266
267
268 /* Probe */
269 USB_MATCH(udav)
270 {
271 USB_MATCH_START(udav, uaa);
272
273 if (uaa->iface != NULL)
274 return (UMATCH_NONE);
275
276 return (udav_lookup(uaa->vendor, uaa->product) != NULL ?
277 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
278 }
279
280 /* Attach */
281 USB_ATTACH(udav)
282 {
283 USB_ATTACH_START(udav, sc, uaa);
284 usbd_device_handle dev = uaa->device;
285 usbd_interface_handle iface;
286 usbd_status err;
287 usb_interface_descriptor_t *id;
288 usb_endpoint_descriptor_t *ed;
289 char devinfo[1024];
290 const char *devname ;
291 struct ifnet *ifp;
292 #if defined(__NetBSD__)
293 struct mii_data *mii;
294 #endif
295 u_char eaddr[ETHER_ADDR_LEN];
296 int i;
297 #if defined(__NetBSD__)
298 int s;
299 #endif
300
301 bzero(sc, sizeof(struct udav_softc));
302
303 usbd_devinfo(dev, 0, devinfo);
304 USB_ATTACH_SETUP;
305 devname = USBDEVNAME(sc->sc_dev);
306 printf("%s: %s\n", devname, devinfo);
307
308 /* Move the device into the configured state. */
309 err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1);
310 if (err) {
311 printf("%s: setting config no failed\n", devname);
312 goto bad;
313 }
314
315 usb_init_task(&sc->sc_tick_task, udav_tick_task, sc);
316 lockinit(&sc->sc_mii_lock, PZERO, "udavmii", 0, 0);
317 usb_init_task(&sc->sc_stop_task, (void (*)(void *)) udav_stop_task, sc);
318
319 /* get control interface */
320 err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
321 if (err) {
322 printf("%s: failed to get interface, err=%s\n", devname,
323 usbd_errstr(err));
324 goto bad;
325 }
326
327 sc->sc_udev = dev;
328 sc->sc_ctl_iface = iface;
329 sc->sc_flags = udav_lookup(uaa->vendor, uaa->product)->udav_flags;
330
331 /* get interface descriptor */
332 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
333
334 /* find endpoints */
335 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
336 for (i = 0; i < id->bNumEndpoints; i++) {
337 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
338 if (ed == NULL) {
339 printf("%s: couldn't get endpoint %d\n", devname, i);
340 goto bad;
341 }
342 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
343 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
344 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
345 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
346 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
347 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
348 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
349 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
350 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
351 }
352
353 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
354 sc->sc_intrin_no == -1) {
355 printf("%s: missing endpoint\n", devname);
356 goto bad;
357 }
358
359 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
360 mtx_init(&sc->sc_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
361 MTX_DEF | MTX_RECURSE);
362 #endif
363 #if defined(__NetBSD__)
364 s = splnet();
365 #elif defined(__FreeBSD__)
366 UDAV_LOCK(sc);
367 #endif
368
369 /* reset the adapter */
370 udav_reset(sc);
371
372 /* Get Ethernet Address */
373 err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
374 if (err) {
375 printf("%s: read MAC address failed\n", devname);
376 #if defined(__NetBSD__)
377 splx(s);
378 #elif defined(__FreeBSD__)
379 UDAV_UNLOCK(sc);
380 #endif
381 goto bad;
382 }
383
384 /* Print Ethernet Address */
385 printf("%s: Ethernet address %s\n", devname, ether_sprintf(eaddr));
386
387 #if defined(__FreeBSD__)
388 bcopy(eaddr, (char *)&sc->sc_ac.ac_enaddr, ETHER_ADDR_LEN);
389 #endif
390
391 /* initialize interface infomation */
392 ifp = GET_IFP(sc);
393 ifp->if_softc = sc;
394 ifp->if_mtu = ETHERMTU;
395 #if defined(__NetBSD__)
396 strncpy(ifp->if_xname, devname, IFNAMSIZ);
397 #elif defined(__FreeBSD__)
398 if_initname(ifp, "udav", device_get_unit(self));
399 #endif
400 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
401 IFF_NEEDSGIANT;
402 ifp->if_start = udav_start;
403 ifp->if_ioctl = udav_ioctl;
404 ifp->if_watchdog = udav_watchdog;
405 ifp->if_init = udav_init;
406 #if defined(__NetBSD__)
407 ifp->if_stop = udav_stop;
408 #endif
409 #if defined(__FreeBSD__)
410 ifp->if_baudrate = 10000000;
411 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
412 #endif
413 #if defined(__NetBSD__)
414 IFQ_SET_READY(&ifp->if_snd);
415 #endif
416
417
418 #if defined(__NetBSD__)
419 /*
420 * Do ifmedia setup.
421 */
422 mii = &sc->sc_mii;
423 mii->mii_ifp = ifp;
424 mii->mii_readreg = udav_miibus_readreg;
425 mii->mii_writereg = udav_miibus_writereg;
426 mii->mii_statchg = udav_miibus_statchg;
427 mii->mii_flags = MIIF_AUTOTSLEEP;
428 ifmedia_init(&mii->mii_media, 0,
429 udav_ifmedia_change, udav_ifmedia_status);
430 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
431 if (LIST_FIRST(&mii->mii_phys) == NULL) {
432 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
433 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
434 } else
435 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
436
437 /* attach the interface */
438 if_attach(ifp);
439 Ether_ifattach(ifp, eaddr);
440 #elif defined(__FreeBSD__)
441 if (mii_phy_probe(self, &sc->sc_miibus,
442 udav_ifmedia_change, udav_ifmedia_status)) {
443 printf("%s: MII without any PHY!\n", USBDEVNAME(sc->sc_dev));
444 UDAV_UNLOCK(sc);
445 mtx_destroy(&sc->sc_mtx);
446 USB_ATTACH_ERROR_RETURN;
447 }
448
449 sc->sc_qdat.ifp = ifp;
450 sc->sc_qdat.if_rxstart = udav_rxstart;
451
452 /*
453 * Call MI attach routine.
454 */
455
456 ether_ifattach(ifp, eaddr);
457 #endif
458
459 #if NRND > 0
460 rnd_attach_source(&sc->rnd_source, devname, RND_TYPE_NET, 0);
461 #endif
462
463 usb_callout_init(sc->sc_stat_ch);
464 #if defined(__FreeBSD__)
465 usb_register_netisr();
466 #endif
467 sc->sc_attached = 1;
468 #if defined(__NetBSD__)
469 splx(s);
470 #elif defined(__FreeBSD__)
471 UDAV_UNLOCK(sc);
472 #endif
473
474 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
475
476 USB_ATTACH_SUCCESS_RETURN;
477
478 bad:
479 sc->sc_dying = 1;
480 USB_ATTACH_ERROR_RETURN;
481 }
482
483 /* detach */
484 USB_DETACH(udav)
485 {
486 USB_DETACH_START(udav, sc);
487 struct ifnet *ifp = GET_IFP(sc);
488 #if defined(__NetBSD__)
489 int s;
490 #endif
491
492 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
493
494 /* Detached before attached finished */
495 if (!sc->sc_attached)
496 return (0);
497
498 UDAV_LOCK(sc);
499
500 usb_uncallout(sc->sc_stat_ch, udav_tick, sc);
501
502 /* Remove any pending tasks */
503 usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
504 usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
505
506 #if defined(__NetBSD__)
507 s = splusb();
508 #elif defined(__FreeBSD__)
509 UDAV_LOCK(sc);
510 #endif
511
512 if (--sc->sc_refcnt >= 0) {
513 /* Wait for processes to go away */
514 usb_detach_wait(USBDEV(sc->sc_dev));
515 }
516 if (ifp->if_flags & IFF_RUNNING)
517 udav_stop(GET_IFP(sc), 1);
518
519 #if NRND > 0
520 rnd_detach_source(&sc->rnd_source);
521 #endif
522 #if defined(__NetBSD__)
523 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
524 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
525 #endif
526 ether_ifdetach(ifp);
527 #if defined(__NetBSD__)
528 if_detach(ifp);
529 #endif
530
531 #ifdef DIAGNOSTIC
532 if (sc->sc_pipe_tx != NULL)
533 printf("%s: detach has active tx endpoint.\n",
534 USBDEVNAME(sc->sc_dev));
535 if (sc->sc_pipe_rx != NULL)
536 printf("%s: detach has active rx endpoint.\n",
537 USBDEVNAME(sc->sc_dev));
538 if (sc->sc_pipe_intr != NULL)
539 printf("%s: detach has active intr endpoint.\n",
540 USBDEVNAME(sc->sc_dev));
541 #endif
542 sc->sc_attached = 0;
543
544 #if defined(__NetBSD__)
545 splx(s);
546 #elif defined(__FreeBSD__)
547 UDAV_UNLOCK(sc);
548 #endif
549
550 #if defined(__FreeBSD__)
551 mtx_destroy(&sc->sc_mtx);
552 #endif
553
554 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
555 USBDEV(sc->sc_dev));
556 return (0);
557 }
558
559 #if 0
560 /* read memory */
561 Static int
562 udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len)
563 {
564 usb_device_request_t req;
565 usbd_status err;
566
567 if (sc == NULL)
568 return (0);
569
570 DPRINTFN(0x200,
571 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
572
573 if (sc->sc_dying)
574 return (0);
575
576 offset &= 0xffff;
577 len &= 0xff;
578
579 req.bmRequestType = UT_READ_VENDOR_DEVICE;
580 req.bRequest = UDAV_REQ_MEM_READ;
581 USETW(req.wValue, 0x0000);
582 USETW(req.wIndex, offset);
583 USETW(req.wLength, len);
584
585 sc->sc_refcnt++;
586 err = usbd_do_request(sc->sc_udev, &req, buf);
587 if (--sc->sc_refcnt < 0)
588 usb_detach_wakeup(USBDEV(sc->sc_dev));
589 if (err) {
590 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
591 USBDEVNAME(sc->sc_dev), __func__, offset, err));
592 }
593
594 return (err);
595 }
596
597 /* write memory */
598 Static int
599 udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len)
600 {
601 usb_device_request_t req;
602 usbd_status err;
603
604 if (sc == NULL)
605 return (0);
606
607 DPRINTFN(0x200,
608 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
609
610 if (sc->sc_dying)
611 return (0);
612
613 offset &= 0xffff;
614 len &= 0xff;
615
616 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
617 req.bRequest = UDAV_REQ_MEM_WRITE;
618 USETW(req.wValue, 0x0000);
619 USETW(req.wIndex, offset);
620 USETW(req.wLength, len);
621
622 sc->sc_refcnt++;
623 err = usbd_do_request(sc->sc_udev, &req, buf);
624 if (--sc->sc_refcnt < 0)
625 usb_detach_wakeup(USBDEV(sc->sc_dev));
626 if (err) {
627 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
628 USBDEVNAME(sc->sc_dev), __func__, offset, err));
629 }
630
631 return (err);
632 }
633
634 /* write memory */
635 Static int
636 udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch)
637 {
638 usb_device_request_t req;
639 usbd_status err;
640
641 if (sc == NULL)
642 return (0);
643
644 DPRINTFN(0x200,
645 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
646
647 if (sc->sc_dying)
648 return (0);
649
650 offset &= 0xffff;
651
652 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
653 req.bRequest = UDAV_REQ_MEM_WRITE1;
654 USETW(req.wValue, ch);
655 USETW(req.wIndex, offset);
656 USETW(req.wLength, 0x0000);
657
658 sc->sc_refcnt++;
659 err = usbd_do_request(sc->sc_udev, &req, NULL);
660 if (--sc->sc_refcnt < 0)
661 usb_detach_wakeup(USBDEV(sc->sc_dev));
662 if (err) {
663 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
664 USBDEVNAME(sc->sc_dev), __func__, offset, err));
665 }
666
667 return (err);
668 }
669 #endif
670
671 /* read register(s) */
672 Static int
673 udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len)
674 {
675 usb_device_request_t req;
676 usbd_status err;
677
678 if (sc == NULL)
679 return (0);
680
681 DPRINTFN(0x200,
682 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
683
684 if (sc->sc_dying)
685 return (0);
686
687 offset &= 0xff;
688 len &= 0xff;
689
690 req.bmRequestType = UT_READ_VENDOR_DEVICE;
691 req.bRequest = UDAV_REQ_REG_READ;
692 USETW(req.wValue, 0x0000);
693 USETW(req.wIndex, offset);
694 USETW(req.wLength, len);
695
696 sc->sc_refcnt++;
697 err = usbd_do_request(sc->sc_udev, &req, buf);
698 if (--sc->sc_refcnt < 0)
699 usb_detach_wakeup(USBDEV(sc->sc_dev));
700 if (err) {
701 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
702 USBDEVNAME(sc->sc_dev), __func__, offset, err));
703 }
704
705 return (err);
706 }
707
708 /* write register(s) */
709 Static int
710 udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len)
711 {
712 usb_device_request_t req;
713 usbd_status err;
714
715 if (sc == NULL)
716 return (0);
717
718 DPRINTFN(0x200,
719 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
720
721 if (sc->sc_dying)
722 return (0);
723
724 offset &= 0xff;
725 len &= 0xff;
726
727 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
728 req.bRequest = UDAV_REQ_REG_WRITE;
729 USETW(req.wValue, 0x0000);
730 USETW(req.wIndex, offset);
731 USETW(req.wLength, len);
732
733 sc->sc_refcnt++;
734 err = usbd_do_request(sc->sc_udev, &req, buf);
735 if (--sc->sc_refcnt < 0)
736 usb_detach_wakeup(USBDEV(sc->sc_dev));
737 if (err) {
738 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
739 USBDEVNAME(sc->sc_dev), __func__, offset, err));
740 }
741
742 return (err);
743 }
744
745 Static int
746 udav_csr_read1(struct udav_softc *sc, int offset)
747 {
748 u_int8_t val = 0;
749
750 if (sc == NULL)
751 return (0);
752
753 DPRINTFN(0x200,
754 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
755
756 if (sc->sc_dying)
757 return (0);
758
759 return (udav_csr_read(sc, offset, &val, 1) ? 0 : val);
760 }
761
762 /* write a register */
763 Static int
764 udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch)
765 {
766 usb_device_request_t req;
767 usbd_status err;
768
769 if (sc == NULL)
770 return (0);
771
772 DPRINTFN(0x200,
773 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
774
775 if (sc->sc_dying)
776 return (0);
777
778 offset &= 0xff;
779
780 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
781 req.bRequest = UDAV_REQ_REG_WRITE1;
782 USETW(req.wValue, ch);
783 USETW(req.wIndex, offset);
784 USETW(req.wLength, 0x0000);
785
786 sc->sc_refcnt++;
787 err = usbd_do_request(sc->sc_udev, &req, NULL);
788 if (--sc->sc_refcnt < 0)
789 usb_detach_wakeup(USBDEV(sc->sc_dev));
790 if (err) {
791 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
792 USBDEVNAME(sc->sc_dev), __func__, offset, err));
793 }
794
795 return (err);
796 }
797
798 #if defined(__NetBSD__)
799 Static int
800 udav_init(struct ifnet *ifp)
801 #elif defined(__FreeBSD__)
802 Static void
803 udav_init(void *xsc)
804 #endif
805 {
806 #if defined(__NetBSD__)
807 struct udav_softc *sc = ifp->if_softc;
808 #elif defined(__FreeBSD__)
809 struct udav_softc *sc = (struct udav_softc *)xsc;
810 struct ifnet *ifp = GET_IFP(sc);
811 #endif
812 struct mii_data *mii = GET_MII(sc);
813 u_char *eaddr;
814 #if defined(__NetBSD__)
815 int s;
816 #endif
817
818 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
819
820 if (sc->sc_dying)
821 #if defined(__NetBSD__)
822 return (EIO);
823 #elif defined(__FreeBSD__)
824 return ;
825 #endif
826
827 #if defined(__NetBSD__)
828 s = splnet();
829 #elif defined(__FreeBSD__)
830 UDAV_LOCK(sc);
831 #endif
832
833 /* Cancel pending I/O and free all TX/RX buffers */
834 udav_stop(ifp, 1);
835
836 #if defined(__NetBSD__)
837 eaddr = LLADDR(ifp->if_sadl);
838 #elif defined(__FreeBSD__)
839 eaddr = sc->sc_ac.ac_enaddr ;
840 #endif
841 udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
842
843 /* Initialize network control register */
844 /* Disable loopback */
845 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
846
847 /* Initialize RX control register */
848 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
849
850 /* If we want promiscuous mode, accept all physical frames. */
851 if (ifp->if_flags & IFF_PROMISC)
852 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
853 else
854 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
855
856 /* Initialize transmit ring */
857 if (usb_ether_tx_list_init(sc, &sc->sc_cdata,
858 sc->sc_udev) == ENOBUFS) {
859 printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
860 #if defined(__NetBSD__)
861 splx(s);
862 return (EIO);
863 #elif defined(__FreeBSD__)
864 UDAV_UNLOCK(sc);
865 return ;
866 #endif
867
868 }
869
870 /* Initialize receive ring */
871 if (usb_ether_rx_list_init(sc, &sc->sc_cdata,
872 sc->sc_udev) == ENOBUFS) {
873 printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
874 #if defined(__NetBSD__)
875 splx(s);
876 return (EIO);
877 #elif defined(__FreeBSD__)
878 UDAV_UNLOCK(sc);
879 return ;
880 #endif
881 }
882
883 /* Load the multicast filter */
884 udav_setmulti(sc);
885
886 /* Enable RX */
887 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
888
889 /* clear POWER_DOWN state of internal PHY */
890 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
891 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
892
893 mii_mediachg(mii);
894
895 if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
896 if (udav_openpipes(sc)) {
897 #if defined(__NetBSD__)
898 splx(s);
899 return (EIO);
900 #elif defined(__FreeBSD__)
901 UDAV_UNLOCK(sc);
902 return ;
903 #endif
904 }
905 }
906
907 ifp->if_flags |= IFF_RUNNING;
908 ifp->if_flags &= ~IFF_OACTIVE;
909
910 #if defined(__NetBSD__)
911 splx(s);
912 #elif defined(__FreeBSD__)
913 UDAV_UNLOCK(sc);
914 #endif
915
916 usb_callout(sc->sc_stat_ch, hz, udav_tick, sc);
917
918 #if defined(__NetBSD__)
919 return (0);
920 #elif defined(__FreeBSD__)
921 return ;
922 #endif
923 }
924
925 Static void
926 udav_reset(struct udav_softc *sc)
927 {
928 int i;
929
930 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
931
932 if (sc->sc_dying)
933 return;
934
935 /* Select PHY */
936 #if 1
937 /*
938 * XXX: force select internal phy.
939 * external phy routines are not tested.
940 */
941 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
942 #else
943 if (sc->sc_flags & UDAV_EXT_PHY) {
944 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
945 } else {
946 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
947 }
948 #endif
949
950 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
951
952 for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
953 if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
954 break;
955 delay(10); /* XXX */
956 }
957 delay(10000); /* XXX */
958 }
959
960 #if defined(__NetBSD__) || defined(__OpenBSD__)
961 int
962 udav_activate(device_ptr_t self, enum devact act)
963 {
964 struct udav_softc *sc = (struct udav_softc *)self;
965
966 DPRINTF(("%s: %s: enter, act=%d\n", USBDEVNAME(sc->sc_dev),
967 __func__, act));
968 switch (act) {
969 case DVACT_ACTIVATE:
970 return (EOPNOTSUPP);
971 break;
972
973 case DVACT_DEACTIVATE:
974 if_deactivate(&sc->sc_ec.ec_if);
975 sc->sc_dying = 1;
976 break;
977 }
978 return (0);
979 }
980 #endif
981
982 #define UDAV_BITS 6
983
984 #define UDAV_CALCHASH(addr) \
985 (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
986
987 Static void
988 udav_setmulti(struct udav_softc *sc)
989 {
990 struct ifnet *ifp;
991 #if defined(__NetBSD__)
992 struct ether_multi *enm;
993 struct ether_multistep step;
994 #elif defined(__FreeBSD__)
995 struct ifmultiaddr *ifma;
996 #endif
997 u_int8_t hashes[8];
998 int h = 0;
999
1000 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1001
1002 if (sc->sc_dying)
1003 return;
1004
1005 ifp = GET_IFP(sc);
1006
1007 if (ifp->if_flags & IFF_PROMISC) {
1008 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
1009 return;
1010 } else if (ifp->if_flags & IFF_ALLMULTI) {
1011 #if defined(__NetBSD__)
1012 allmulti:
1013 #endif
1014 ifp->if_flags |= IFF_ALLMULTI;
1015 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
1016 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
1017 return;
1018 }
1019
1020 /* first, zot all the existing hash bits */
1021 memset(hashes, 0x00, sizeof(hashes));
1022 hashes[7] |= 0x80; /* broadcast address */
1023 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
1024
1025 /* now program new ones */
1026 #if defined(__NetBSD__)
1027 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
1028 while (enm != NULL) {
1029 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1030 ETHER_ADDR_LEN) != 0)
1031 goto allmulti;
1032
1033 h = UDAV_CALCHASH(enm->enm_addrlo);
1034 hashes[h>>3] |= 1 << (h & 0x7);
1035 ETHER_NEXT_MULTI(step, enm);
1036 }
1037 #elif defined(__FreeBSD__)
1038 IF_ADDR_LOCK(ifp);
1039 #if __FreeBSD_version >= 500000
1040 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1041 #else
1042 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1043 #endif
1044 {
1045 if (ifma->ifma_addr->sa_family != AF_LINK)
1046 continue;
1047 h = UDAV_CALCHASH(LLADDR((struct sockaddr_dl *)
1048 ifma->ifma_addr));
1049 hashes[h>>3] |= 1 << (h & 0x7);
1050 }
1051 IF_ADDR_UNLOCK(ifp);
1052 #endif
1053
1054 /* disable all multicast */
1055 ifp->if_flags &= ~IFF_ALLMULTI;
1056 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
1057
1058 /* write hash value to the register */
1059 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
1060 }
1061
1062 Static int
1063 udav_openpipes(struct udav_softc *sc)
1064 {
1065 struct ue_chain *c;
1066 usbd_status err;
1067 int i;
1068 int error = 0;
1069
1070 if (sc->sc_dying)
1071 return (EIO);
1072
1073 sc->sc_refcnt++;
1074
1075 /* Open RX pipe */
1076 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
1077 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
1078 if (err) {
1079 printf("%s: open rx pipe failed: %s\n",
1080 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1081 error = EIO;
1082 goto done;
1083 }
1084
1085 /* Open TX pipe */
1086 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
1087 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
1088 if (err) {
1089 printf("%s: open tx pipe failed: %s\n",
1090 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1091 error = EIO;
1092 goto done;
1093 }
1094
1095 #if 0
1096 /* XXX: interrupt endpoint is not yet supported */
1097 /* Open Interrupt pipe */
1098 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
1099 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
1100 &sc->sc_cdata.ue_ibuf, UDAV_INTR_PKGLEN,
1101 udav_intr, UDAV_INTR_INTERVAL);
1102 if (err) {
1103 printf("%s: open intr pipe failed: %s\n",
1104 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1105 error = EIO;
1106 goto done;
1107 }
1108 #endif
1109
1110
1111 /* Start up the receive pipe. */
1112 for (i = 0; i < UE_RX_LIST_CNT; i++) {
1113 c = &sc->sc_cdata.ue_rx_chain[i];
1114 usbd_setup_xfer(c->ue_xfer, sc->sc_pipe_rx,
1115 c, c->ue_buf, UE_BUFSZ,
1116 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1117 USBD_NO_TIMEOUT, udav_rxeof);
1118 (void)usbd_transfer(c->ue_xfer);
1119 DPRINTF(("%s: %s: start read\n", USBDEVNAME(sc->sc_dev),
1120 __func__));
1121 }
1122
1123 done:
1124 if (--sc->sc_refcnt < 0)
1125 usb_detach_wakeup(USBDEV(sc->sc_dev));
1126
1127 return (error);
1128 }
1129
1130 Static void
1131 udav_start(struct ifnet *ifp)
1132 {
1133 struct udav_softc *sc = ifp->if_softc;
1134 struct mbuf *m_head = NULL;
1135
1136 DPRINTF(("%s: %s: enter, link=%d\n", USBDEVNAME(sc->sc_dev),
1137 __func__, sc->sc_link));
1138
1139 if (sc->sc_dying)
1140 return;
1141
1142 if (!sc->sc_link)
1143 return;
1144
1145 if (ifp->if_flags & IFF_OACTIVE)
1146 return;
1147 #if defined(__NetBSD__)
1148 IFQ_POLL(&ifp->if_snd, m_head);
1149 #elif defined(__FreeBSD__)
1150 IF_DEQUEUE(&ifp->if_snd, m_head);
1151 #endif
1152 if (m_head == NULL)
1153 return;
1154
1155 if (udav_send(sc, m_head, 0)) {
1156 #if defined(__FreeBSD__)
1157 IF_PREPEND(&ifp->if_snd, m_head);
1158 #endif
1159 ifp->if_flags |= IFF_OACTIVE;
1160 return;
1161 }
1162
1163 #if defined(__NetBSD__)
1164 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1165 #endif
1166
1167 #if NBPFILTER > 0
1168 BPF_MTAP(ifp, m_head);
1169 #endif
1170
1171 ifp->if_flags |= IFF_OACTIVE;
1172
1173 /* Set a timeout in case the chip goes out to lunch. */
1174 ifp->if_timer = 5;
1175 }
1176
1177 Static int
1178 udav_send(struct udav_softc *sc, struct mbuf *m, int idx)
1179 {
1180 int total_len;
1181 struct ue_chain *c;
1182 usbd_status err;
1183
1184 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
1185
1186 c = &sc->sc_cdata.ue_tx_chain[idx];
1187
1188 /* Copy the mbuf data into a contiguous buffer */
1189 /* first 2 bytes are packet length */
1190 m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf + 2);
1191 c->ue_mbuf = m;
1192 total_len = m->m_pkthdr.len;
1193 if (total_len < UDAV_MIN_FRAME_LEN) {
1194 memset(c->ue_buf + 2 + total_len, 0,
1195 UDAV_MIN_FRAME_LEN - total_len);
1196 total_len = UDAV_MIN_FRAME_LEN;
1197 }
1198
1199 /* Frame length is specified in the first 2bytes of the buffer */
1200 c->ue_buf[0] = (u_int8_t)total_len;
1201 c->ue_buf[1] = (u_int8_t)(total_len >> 8);
1202 total_len += 2;
1203
1204 usbd_setup_xfer(c->ue_xfer, sc->sc_pipe_tx, c, c->ue_buf, total_len,
1205 USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1206 UDAV_TX_TIMEOUT, udav_txeof);
1207
1208 /* Transmit */
1209 sc->sc_refcnt++;
1210 err = usbd_transfer(c->ue_xfer);
1211 if (--sc->sc_refcnt < 0)
1212 usb_detach_wakeup(USBDEV(sc->sc_dev));
1213 if (err != USBD_IN_PROGRESS) {
1214 printf("%s: udav_send error=%s\n", USBDEVNAME(sc->sc_dev),
1215 usbd_errstr(err));
1216 /* Stop the interface */
1217 usb_add_task(sc->sc_udev, &sc->sc_stop_task);
1218 return (EIO);
1219 }
1220
1221 DPRINTF(("%s: %s: send %d bytes\n", USBDEVNAME(sc->sc_dev),
1222 __func__, total_len));
1223
1224 sc->sc_cdata.ue_tx_cnt++;
1225
1226 return (0);
1227 }
1228
1229 Static void
1230 udav_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1231 {
1232 struct ue_chain *c = priv;
1233 struct udav_softc *sc = c->ue_sc;
1234 struct ifnet *ifp = GET_IFP(sc);
1235 #if defined(__NetBSD__)
1236 int s;
1237 #endif
1238
1239 if (sc->sc_dying)
1240 return;
1241
1242 #if defined(__NetBSD__)
1243 s = splnet();
1244 #elif defined(__FreeBSD__)
1245 UDAV_LOCK(sc);
1246 #endif
1247
1248 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1249
1250 ifp->if_timer = 0;
1251 ifp->if_flags &= ~IFF_OACTIVE;
1252
1253 if (status != USBD_NORMAL_COMPLETION) {
1254 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1255 #if defined(__NetBSD__)
1256 splx(s);
1257 #elif defined(__FreeBSD__)
1258 UDAV_UNLOCK(sc);
1259 #endif
1260 return;
1261 }
1262 ifp->if_oerrors++;
1263 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
1264 usbd_errstr(status));
1265 if (status == USBD_STALLED) {
1266 sc->sc_refcnt++;
1267 usbd_clear_endpoint_stall(sc->sc_pipe_tx);
1268 if (--sc->sc_refcnt < 0)
1269 usb_detach_wakeup(USBDEV(sc->sc_dev));
1270 }
1271 #if defined(__NetBSD__)
1272 splx(s);
1273 #elif defined(__FreeBSD__)
1274 UDAV_UNLOCK(sc);
1275 #endif
1276 return;
1277 }
1278
1279 ifp->if_opackets++;
1280
1281 m_freem(c->ue_mbuf);
1282 c->ue_mbuf = NULL;
1283
1284 #if defined(__NetBSD__)
1285 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1286 #elif defined(__FreeBSD__)
1287 if ( ifp->if_snd.ifq_head != NULL )
1288 #endif
1289 udav_start(ifp);
1290
1291 #if defined(__NetBSD__)
1292 splx(s);
1293 #elif defined(__FreeBSD__)
1294 UDAV_UNLOCK(sc);
1295 #endif
1296 }
1297
1298 Static void
1299 udav_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1300 {
1301 struct ue_chain *c = priv;
1302 struct udav_softc *sc = c->ue_sc;
1303 struct ifnet *ifp = GET_IFP(sc);
1304 struct mbuf *m;
1305 u_int32_t total_len;
1306 u_int8_t *pktstat;
1307 #if defined(__NetBSD__)
1308 int s;
1309 #endif
1310
1311 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
1312
1313 if (sc->sc_dying)
1314 return;
1315
1316 if (status != USBD_NORMAL_COMPLETION) {
1317 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1318 return;
1319 sc->sc_rx_errs++;
1320 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1321 printf("%s: %u usb errors on rx: %s\n",
1322 USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
1323 usbd_errstr(status));
1324 sc->sc_rx_errs = 0;
1325 }
1326 if (status == USBD_STALLED) {
1327 sc->sc_refcnt++;
1328 usbd_clear_endpoint_stall(sc->sc_pipe_rx);
1329 if (--sc->sc_refcnt < 0)
1330 usb_detach_wakeup(USBDEV(sc->sc_dev));
1331 }
1332 goto done;
1333 }
1334
1335 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1336
1337 /* copy data to mbuf */
1338 m = c->ue_mbuf;
1339 memcpy(mtod(m, char *), c->ue_buf, total_len);
1340
1341 /* first byte in received data */
1342 pktstat = mtod(m, u_int8_t *);
1343 m_adj(m, sizeof(u_int8_t));
1344 DPRINTF(("%s: RX Status: 0x%02x\n", USBDEVNAME(sc->sc_dev), *pktstat));
1345
1346 total_len = UGETW(mtod(m, u_int8_t *));
1347 m_adj(m, sizeof(u_int16_t));
1348
1349 if (*pktstat & UDAV_RSR_LCS) {
1350 ifp->if_collisions++;
1351 goto done;
1352 }
1353
1354 if (total_len < sizeof(struct ether_header) ||
1355 *pktstat & UDAV_RSR_ERR) {
1356 ifp->if_ierrors++;
1357 goto done;
1358 }
1359
1360 ifp->if_ipackets++;
1361 total_len -= ETHER_CRC_LEN;
1362
1363 m->m_pkthdr.len = m->m_len = total_len;
1364 #if defined(__NetBSD__)
1365 m->m_pkthdr.rcvif = ifp;
1366 #elif defined(__FreeBSD__)
1367 m->m_pkthdr.rcvif = (struct ifnet *)&sc->sc_qdat;
1368 #endif
1369
1370 #if defined(__NetBSD__)
1371 s = splnet();
1372 #elif defined(__FreeBSD__)
1373 UDAV_LOCK(sc);
1374 #endif
1375
1376 #if defined(__NetBSD__)
1377 c->ue_mbuf = usb_ether_newbuf();
1378 if (c->ue_mbuf == NULL) {
1379 printf("%s: no memory for rx list "
1380 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
1381 ifp->if_ierrors++;
1382 goto done1;
1383 }
1384 #endif
1385
1386 #if NBPFILTER > 0
1387 BPF_MTAP(ifp, m);
1388 #endif
1389
1390 DPRINTF(("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
1391 __func__, m->m_len));
1392 #if defined(__NetBSD__)
1393 IF_INPUT(ifp, m);
1394 #endif
1395 #if defined(__FreeBSD__)
1396 usb_ether_input(m);
1397 UDAV_UNLOCK(sc);
1398 return ;
1399 #endif
1400
1401 #if defined(__NetBSD__)
1402 done1:
1403 splx(s);
1404 #elif defined(__FreeBSD__)
1405 UDAV_UNLOCK(sc);
1406 #endif
1407 done:
1408 /* Setup new transfer */
1409 usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->ue_buf, UE_BUFSZ,
1410 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1411 USBD_NO_TIMEOUT, udav_rxeof);
1412 sc->sc_refcnt++;
1413 usbd_transfer(xfer);
1414 if (--sc->sc_refcnt < 0)
1415 usb_detach_wakeup(USBDEV(sc->sc_dev));
1416
1417 DPRINTF(("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), __func__));
1418 }
1419
1420 #if 0
1421 Static void udav_intr()
1422 {
1423 }
1424 #endif
1425
1426 Static int
1427 udav_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1428 {
1429 struct udav_softc *sc = ifp->if_softc;
1430 struct ifreq *ifr = (struct ifreq *)data;
1431 struct mii_data *mii;
1432 #if defined(__NetBSD__)
1433 int s;
1434 #endif
1435 int error = 0;
1436
1437 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1438
1439 if (sc->sc_dying)
1440 return (EIO);
1441
1442 #if defined(__NetBSD__)
1443 s = splnet();
1444 #elif defined(__FreeBSD__)
1445 UDAV_LOCK(sc);
1446 #endif
1447
1448 switch (cmd) {
1449 #if defined(__FreeBSD__)
1450 case SIOCSIFFLAGS:
1451 if (ifp->if_flags & IFF_UP) {
1452 if (ifp->if_flags & IFF_RUNNING &&
1453 ifp->if_flags & IFF_PROMISC) {
1454 UDAV_SETBIT(sc, UDAV_RCR,
1455 UDAV_RCR_ALL|UDAV_RCR_PRMSC);
1456 } else if (ifp->if_flags & IFF_RUNNING &&
1457 !(ifp->if_flags & IFF_PROMISC)) {
1458 if (ifp->if_flags & IFF_ALLMULTI)
1459 UDAV_CLRBIT(sc, UDAV_RCR,
1460 UDAV_RCR_PRMSC);
1461 else
1462 UDAV_CLRBIT(sc, UDAV_RCR,
1463 UDAV_RCR_ALL|UDAV_RCR_PRMSC);
1464 } else if (!(ifp->if_flags & IFF_RUNNING))
1465 udav_init(sc);
1466 } else {
1467 if (ifp->if_flags & IFF_RUNNING)
1468 udav_stop(ifp, 1);
1469 }
1470 error = 0;
1471 break;
1472 case SIOCADDMULTI:
1473 case SIOCDELMULTI:
1474 udav_setmulti(sc);
1475 error = 0;
1476 break;
1477 #endif
1478 case SIOCGIFMEDIA:
1479 case SIOCSIFMEDIA:
1480 mii = GET_MII(sc);
1481 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1482 break;
1483
1484 default:
1485 error = ether_ioctl(ifp, cmd, data);
1486 #if defined(__NetBSD__)
1487 if (error == ENETRESET) {
1488 udav_setmulti(sc);
1489 error = 0;
1490 }
1491 #endif
1492 break;
1493 }
1494
1495 #if defined(__NetBSD__)
1496 splx(s);
1497 #elif defined(__FreeBSD__)
1498 UDAV_UNLOCK(sc);
1499 #endif
1500
1501 return (error);
1502 }
1503
1504 Static void
1505 udav_watchdog(struct ifnet *ifp)
1506 {
1507 struct udav_softc *sc = ifp->if_softc;
1508 struct ue_chain *c;
1509 usbd_status stat;
1510 #if defined(__NetBSD__)
1511 int s;
1512 #endif
1513
1514 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1515
1516 ifp->if_oerrors++;
1517 printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
1518
1519 #if defined(__NetBSD__)
1520 s = splusb();
1521 #elif defined(__FreeBSD__)
1522 UDAV_LOCK(sc)
1523 #endif
1524 c = &sc->sc_cdata.ue_tx_chain[0];
1525 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1526 udav_txeof(c->ue_xfer, c, stat);
1527
1528 #if defined(__NetBSD__)
1529 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1530 #elif defined(__FreeBSD__)
1531 if ( ifp->if_snd.ifq_head != NULL )
1532 #endif
1533 udav_start(ifp);
1534 #if defined(__NetBSD__)
1535 splx(s);
1536 #elif defined(__FreeBSD__)
1537 UDAV_UNLOCK(sc);
1538 #endif
1539 }
1540
1541 Static void
1542 udav_stop_task(struct udav_softc *sc)
1543 {
1544 udav_stop(GET_IFP(sc), 1);
1545 }
1546
1547 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1548 Static void
1549 udav_stop(struct ifnet *ifp, int disable)
1550 {
1551 struct udav_softc *sc = ifp->if_softc;
1552 usbd_status err;
1553
1554 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1555
1556 ifp->if_timer = 0;
1557
1558 udav_reset(sc);
1559
1560 usb_uncallout(sc->sc_stat_ch, udav_tick, sc);
1561
1562 /* Stop transfers */
1563 /* RX endpoint */
1564 if (sc->sc_pipe_rx != NULL) {
1565 err = usbd_abort_pipe(sc->sc_pipe_rx);
1566 if (err)
1567 printf("%s: abort rx pipe failed: %s\n",
1568 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1569 err = usbd_close_pipe(sc->sc_pipe_rx);
1570 if (err)
1571 printf("%s: close rx pipe failed: %s\n",
1572 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1573 sc->sc_pipe_rx = NULL;
1574 }
1575
1576 /* TX endpoint */
1577 if (sc->sc_pipe_tx != NULL) {
1578 err = usbd_abort_pipe(sc->sc_pipe_tx);
1579 if (err)
1580 printf("%s: abort tx pipe failed: %s\n",
1581 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1582 err = usbd_close_pipe(sc->sc_pipe_tx);
1583 if (err)
1584 printf("%s: close tx pipe failed: %s\n",
1585 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1586 sc->sc_pipe_tx = NULL;
1587 }
1588
1589 #if 0
1590 /* XXX: Interrupt endpoint is not yet supported!! */
1591 /* Interrupt endpoint */
1592 if (sc->sc_pipe_intr != NULL) {
1593 err = usbd_abort_pipe(sc->sc_pipe_intr);
1594 if (err)
1595 printf("%s: abort intr pipe failed: %s\n",
1596 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1597 err = usbd_close_pipe(sc->sc_pipe_intr);
1598 if (err)
1599 printf("%s: close intr pipe failed: %s\n",
1600 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1601 sc->sc_pipe_intr = NULL;
1602 }
1603 #endif
1604
1605 /* Free RX resources. */
1606 usb_ether_rx_list_free(&sc->sc_cdata);
1607 /* Free TX resources. */
1608 usb_ether_tx_list_free(&sc->sc_cdata);
1609
1610 sc->sc_link = 0;
1611 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1612 }
1613
1614 /* Set media options */
1615 Static int
1616 udav_ifmedia_change(struct ifnet *ifp)
1617 {
1618 struct udav_softc *sc = ifp->if_softc;
1619 struct mii_data *mii = GET_MII(sc);
1620
1621 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1622
1623 if (sc->sc_dying)
1624 return (0);
1625
1626 sc->sc_link = 0;
1627 if (mii->mii_instance) {
1628 struct mii_softc *miisc;
1629 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1630 miisc = LIST_NEXT(miisc, mii_list))
1631 mii_phy_reset(miisc);
1632 }
1633
1634 return (mii_mediachg(mii));
1635 }
1636
1637 /* Report current media status. */
1638 Static void
1639 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1640 {
1641 struct udav_softc *sc = ifp->if_softc;
1642 struct mii_data *mii = GET_MII(sc);
1643
1644 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1645
1646 if (sc->sc_dying)
1647 return;
1648
1649 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1650 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1651 ifmr->ifm_status = 0;
1652 return;
1653 }
1654
1655 mii_pollstat(mii);
1656 ifmr->ifm_active = mii->mii_media_active;
1657 ifmr->ifm_status = mii->mii_media_status;
1658 }
1659
1660 Static void
1661 udav_tick(void *xsc)
1662 {
1663 struct udav_softc *sc = xsc;
1664
1665 if (sc == NULL)
1666 return;
1667
1668 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1669 __func__));
1670
1671 if (sc->sc_dying)
1672 return;
1673
1674 /* Perform periodic stuff in process context */
1675 usb_add_task(sc->sc_udev, &sc->sc_tick_task);
1676 }
1677
1678 Static void
1679 udav_tick_task(void *xsc)
1680 {
1681 struct udav_softc *sc = xsc;
1682 struct ifnet *ifp;
1683 struct mii_data *mii;
1684 #if defined(__NetBSD__)
1685 int s;
1686 #endif
1687
1688 if (sc == NULL)
1689 return;
1690
1691 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1692 __func__));
1693
1694 if (sc->sc_dying)
1695 return;
1696
1697 ifp = GET_IFP(sc);
1698 mii = GET_MII(sc);
1699
1700 if (mii == NULL)
1701 return;
1702
1703 #if defined(__NetBSD__)
1704 s = splnet();
1705 #elif defined(__FreeBSD__)
1706 UDAV_LOCK(sc);
1707 #endif
1708
1709 mii_tick(mii);
1710 if (!sc->sc_link) {
1711 mii_pollstat(mii);
1712 if (mii->mii_media_status & IFM_ACTIVE &&
1713 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1714 DPRINTF(("%s: %s: got link\n",
1715 USBDEVNAME(sc->sc_dev), __func__));
1716 sc->sc_link++;
1717 #if defined(__NetBSD__)
1718 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1719 #elif defined(__FreeBSD__)
1720 if ( ifp->if_snd.ifq_head != NULL )
1721 #endif
1722 udav_start(ifp);
1723 }
1724 }
1725
1726 usb_callout(sc->sc_stat_ch, hz, udav_tick, sc);
1727
1728 #if defined(__NetBSD__)
1729 splx(s);
1730 #elif defined(__FreeBSD__)
1731 UDAV_UNLOCK(sc);
1732 #endif
1733 }
1734
1735 /* Get exclusive access to the MII registers */
1736 Static void
1737 udav_lock_mii(struct udav_softc *sc)
1738 {
1739 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1740 __func__));
1741
1742 sc->sc_refcnt++;
1743 #if defined(__NetBSD__)
1744 lockmgr(&sc->sc_mii_lock, LK_EXCLUSIVE, NULL);
1745 #elif defined(__FreeBSD__)
1746 lockmgr(&sc->sc_mii_lock, LK_EXCLUSIVE, NULL, NULL);
1747 #endif
1748 }
1749
1750 Static void
1751 udav_unlock_mii(struct udav_softc *sc)
1752 {
1753 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1754 __func__));
1755
1756 #if defined(__NetBSD__)
1757 lockmgr(&sc->sc_mii_lock, LK_RELEASE, NULL);
1758 #elif defined(__FreeBSD__)
1759 lockmgr(&sc->sc_mii_lock, LK_RELEASE, NULL, NULL);
1760 #endif
1761 if (--sc->sc_refcnt < 0)
1762 usb_detach_wakeup(USBDEV(sc->sc_dev));
1763 }
1764
1765 Static int
1766 udav_miibus_readreg(device_ptr_t dev, int phy, int reg)
1767 {
1768 struct udav_softc *sc;
1769 u_int8_t val[2];
1770 u_int16_t data16;
1771
1772 if (dev == NULL)
1773 return (0);
1774
1775 sc = USBGETSOFTC(dev);
1776
1777 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1778 USBDEVNAME(sc->sc_dev), __func__, phy, reg));
1779
1780 if (sc->sc_dying) {
1781 #ifdef DIAGNOSTIC
1782 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1783 __func__);
1784 #endif
1785 return (0);
1786 }
1787
1788 /* XXX: one PHY only for the internal PHY */
1789 if (phy != 0) {
1790 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1791 USBDEVNAME(sc->sc_dev), __func__, phy));
1792 return (0);
1793 }
1794
1795 udav_lock_mii(sc);
1796
1797 /* select internal PHY and set PHY register address */
1798 udav_csr_write1(sc, UDAV_EPAR,
1799 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1800
1801 /* select PHY operation and start read command */
1802 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
1803
1804 /* XXX: should be wait? */
1805
1806 /* end read command */
1807 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
1808
1809 /* retrieve the result from data registers */
1810 udav_csr_read(sc, UDAV_EPDRL, val, 2);
1811
1812 udav_unlock_mii(sc);
1813
1814 data16 = val[0] | (val[1] << 8);
1815
1816 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1817 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data16));
1818
1819 return (data16);
1820 }
1821
1822 Static void
1823 udav_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1824 {
1825 struct udav_softc *sc;
1826 u_int8_t val[2];
1827
1828 if (dev == NULL)
1829 return;
1830
1831 sc = USBGETSOFTC(dev);
1832
1833 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1834 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data));
1835
1836 if (sc->sc_dying) {
1837 #ifdef DIAGNOSTIC
1838 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1839 __func__);
1840 #endif
1841 return;
1842 }
1843
1844 /* XXX: one PHY only for the internal PHY */
1845 if (phy != 0) {
1846 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1847 USBDEVNAME(sc->sc_dev), __func__, phy));
1848 return;
1849 }
1850
1851 udav_lock_mii(sc);
1852
1853 /* select internal PHY and set PHY register address */
1854 udav_csr_write1(sc, UDAV_EPAR,
1855 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1856
1857 /* put the value to the data registers */
1858 val[0] = data & 0xff;
1859 val[1] = (data >> 8) & 0xff;
1860 udav_csr_write(sc, UDAV_EPDRL, val, 2);
1861
1862 /* select PHY operation and start write command */
1863 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
1864
1865 /* XXX: should be wait? */
1866
1867 /* end write command */
1868 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
1869
1870 udav_unlock_mii(sc);
1871
1872 return;
1873 }
1874
1875 Static void
1876 udav_miibus_statchg(device_ptr_t dev)
1877 {
1878 #ifdef UDAV_DEBUG
1879 struct udav_softc *sc;
1880
1881 if (dev == NULL)
1882 return;
1883
1884 sc = USBGETSOFTC(dev);
1885 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1886 #endif
1887 /* Nothing to do */
1888 }
1889
1890 #if defined(__FreeBSD__)
1891 /*
1892 * Stop all chip I/O so that the kernel's probe routines don't
1893 * get confused by errant DMAs when rebooting.
1894 */
1895 Static void
1896 udav_shutdown(device_ptr_t dev)
1897 {
1898 struct udav_softc *sc;
1899
1900 sc = device_get_softc(dev);
1901
1902 udav_stop_task(sc);
1903
1904 return;
1905 }
1906
1907 Static void
1908 udav_rxstart(struct ifnet *ifp)
1909 {
1910 struct udav_softc *sc;
1911 struct ue_chain *c;
1912
1913 sc = ifp->if_softc;
1914 UDAV_LOCK(sc);
1915 c = &sc->sc_cdata.ue_rx_chain[sc->sc_cdata.ue_rx_prod];
1916
1917 c->ue_mbuf = usb_ether_newbuf();
1918 if (c->ue_mbuf == NULL) {
1919 printf("%s: no memory for rx list "
1920 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
1921 ifp->if_ierrors++;
1922 UDAV_UNLOCK(sc);
1923 return;
1924 }
1925
1926 /* Setup new transfer. */
1927 usbd_setup_xfer(c->ue_xfer, sc->sc_pipe_rx,
1928 c, c->ue_buf, UE_BUFSZ,
1929 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1930 USBD_NO_TIMEOUT, udav_rxeof);
1931 usbd_transfer(c->ue_xfer);
1932
1933 UDAV_UNLOCK(sc);
1934 return;
1935 }
1936 #endif
Cache object: b330a579c39318b21d438e7dd353a4cb
|