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