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