if_udav.c revision 1.26 1 /* $NetBSD: if_udav.c,v 1.26 2009/09/23 19:07:19 plunky 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.26 2009/09/23 19:07:19 plunky Exp $");
48
49 #include "opt_inet.h"
50 #include "bpfilter.h"
51 #include "rnd.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/mutex.h>
56 #include <sys/mbuf.h>
57 #include <sys/kernel.h>
58 #include <sys/socket.h>
59 #include <sys/device.h>
60 #if NRND > 0
61 #include <sys/rnd.h>
62 #endif
63
64 #include <net/if.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_media.h>
68
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
72 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
73
74 #include <net/if_ether.h>
75 #ifdef INET
76 #include <netinet/in.h>
77 #include <netinet/if_inarp.h>
78 #endif
79
80 #include <dev/mii/mii.h>
81 #include <dev/mii/miivar.h>
82
83 #include <dev/usb/usb.h>
84 #include <dev/usb/usbdi.h>
85 #include <dev/usb/usbdi_util.h>
86 #include <dev/usb/usbdevs.h>
87
88 #include <dev/usb/if_udavreg.h>
89
90
91 /* Function declarations */
92 USB_DECLARE_DRIVER(udav);
93
94 Static int udav_openpipes(struct udav_softc *);
95 Static int udav_rx_list_init(struct udav_softc *);
96 Static int udav_tx_list_init(struct udav_softc *);
97 Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *);
98 Static void udav_start(struct ifnet *);
99 Static int udav_send(struct udav_softc *, struct mbuf *, int);
100 Static void udav_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
101 Static void udav_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
102 Static void udav_tick(void *);
103 Static void udav_tick_task(void *);
104 Static int udav_ioctl(struct ifnet *, u_long, void *);
105 Static void udav_stop_task(struct udav_softc *);
106 Static void udav_stop(struct ifnet *, int);
107 Static void udav_watchdog(struct ifnet *);
108 Static int udav_ifmedia_change(struct ifnet *);
109 Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
110 Static void udav_lock_mii(struct udav_softc *);
111 Static void udav_unlock_mii(struct udav_softc *);
112 Static int udav_miibus_readreg(device_ptr_t, int, int);
113 Static void udav_miibus_writereg(device_ptr_t, int, int, int);
114 Static void udav_miibus_statchg(device_ptr_t);
115 Static int udav_init(struct ifnet *);
116 Static void udav_setmulti(struct udav_softc *);
117 Static void udav_reset(struct udav_softc *);
118
119 Static int udav_csr_read(struct udav_softc *, int, void *, int);
120 Static int udav_csr_write(struct udav_softc *, int, void *, int);
121 Static int udav_csr_read1(struct udav_softc *, int);
122 Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
123
124 #if 0
125 Static int udav_mem_read(struct udav_softc *, int, void *, int);
126 Static int udav_mem_write(struct udav_softc *, int, void *, int);
127 Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
128 #endif
129
130 /* Macros */
131 #ifdef UDAV_DEBUG
132 #define DPRINTF(x) if (udavdebug) logprintf x
133 #define DPRINTFN(n,x) if (udavdebug >= (n)) logprintf x
134 int udavdebug = 0;
135 #else
136 #define DPRINTF(x)
137 #define DPRINTFN(n,x)
138 #endif
139
140 #define UDAV_SETBIT(sc, reg, x) \
141 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
142
143 #define UDAV_CLRBIT(sc, reg, x) \
144 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
145
146 static const struct udav_type {
147 struct usb_devno udav_dev;
148 u_int16_t udav_flags;
149 #define UDAV_EXT_PHY 0x0001
150 } udav_devs [] = {
151 /* Corega USB-TXC */
152 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
153 /* ShanTou ST268 USB NIC */
154 {{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0},
155 /* ShanTou ADM8515 */
156 {{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515 }, 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 USB_MATCH(udav)
168 {
169 USB_MATCH_START(udav, uaa);
170
171 return (udav_lookup(uaa->vendor, uaa->product) != NULL ?
172 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
173 }
174
175 /* Attach */
176 USB_ATTACH(udav)
177 {
178 USB_ATTACH_START(udav, sc, uaa);
179 usbd_device_handle dev = uaa->device;
180 usbd_interface_handle iface;
181 usbd_status err;
182 usb_interface_descriptor_t *id;
183 usb_endpoint_descriptor_t *ed;
184 char *devinfop;
185 struct ifnet *ifp;
186 struct mii_data *mii;
187 u_char eaddr[ETHER_ADDR_LEN];
188 int i, s;
189
190 sc->sc_dev = self;
191
192 aprint_naive("\n");
193 aprint_normal("\n");
194
195 devinfop = usbd_devinfo_alloc(dev, 0);
196 aprint_normal_dev(self, "%s\n", devinfop);
197 usbd_devinfo_free(devinfop);
198
199 /* Move the device into the configured state. */
200 err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */
201 if (err) {
202 aprint_error_dev(self, "setting config no failed\n");
203 goto bad;
204 }
205
206 usb_init_task(&sc->sc_tick_task, udav_tick_task, sc);
207 mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
208 usb_init_task(&sc->sc_stop_task, (void (*)(void *)) udav_stop_task, sc);
209
210 /* get control interface */
211 err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
212 if (err) {
213 aprint_error_dev(self, "failed to get interface, err=%s\n",
214 usbd_errstr(err));
215 goto bad;
216 }
217
218 sc->sc_udev = dev;
219 sc->sc_ctl_iface = iface;
220 sc->sc_flags = udav_lookup(uaa->vendor, uaa->product)->udav_flags;
221
222 /* get interface descriptor */
223 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
224
225 /* find endpoints */
226 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
227 for (i = 0; i < id->bNumEndpoints; i++) {
228 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
229 if (ed == NULL) {
230 aprint_error_dev(self, "couldn't get endpoint %d\n", i);
231 goto bad;
232 }
233 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
234 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
235 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
236 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
237 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
238 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
239 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
240 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
241 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
242 }
243
244 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
245 sc->sc_intrin_no == -1) {
246 aprint_error_dev(self, "missing endpoint\n");
247 goto bad;
248 }
249
250 s = splnet();
251
252 /* reset the adapter */
253 udav_reset(sc);
254
255 /* Get Ethernet Address */
256 err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
257 if (err) {
258 aprint_error_dev(self, "read MAC address failed\n");
259 splx(s);
260 goto bad;
261 }
262
263 /* Print Ethernet Address */
264 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
265
266 /* initialize interface information */
267 ifp = GET_IFP(sc);
268 ifp->if_softc = sc;
269 ifp->if_mtu = ETHERMTU;
270 strncpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
271 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
272 ifp->if_start = udav_start;
273 ifp->if_ioctl = udav_ioctl;
274 ifp->if_watchdog = udav_watchdog;
275 ifp->if_init = udav_init;
276 ifp->if_stop = udav_stop;
277
278 IFQ_SET_READY(&ifp->if_snd);
279
280 /*
281 * Do ifmedia setup.
282 */
283 mii = &sc->sc_mii;
284 mii->mii_ifp = ifp;
285 mii->mii_readreg = udav_miibus_readreg;
286 mii->mii_writereg = udav_miibus_writereg;
287 mii->mii_statchg = udav_miibus_statchg;
288 mii->mii_flags = MIIF_AUTOTSLEEP;
289 sc->sc_ec.ec_mii = mii;
290 ifmedia_init(&mii->mii_media, 0,
291 udav_ifmedia_change, udav_ifmedia_status);
292 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
293 if (LIST_FIRST(&mii->mii_phys) == NULL) {
294 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
295 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
296 } else
297 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
298
299 /* attach the interface */
300 if_attach(ifp);
301 Ether_ifattach(ifp, eaddr);
302
303 #if NRND > 0
304 rnd_attach_source(&sc->rnd_source, device_xname(self),
305 RND_TYPE_NET, 0);
306 #endif
307
308 usb_callout_init(sc->sc_stat_ch);
309 sc->sc_attached = 1;
310 splx(s);
311
312 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
313
314 USB_ATTACH_SUCCESS_RETURN;
315
316 bad:
317 sc->sc_dying = 1;
318 USB_ATTACH_ERROR_RETURN;
319 }
320
321 /* detach */
322 USB_DETACH(udav)
323 {
324 USB_DETACH_START(udav, sc);
325 struct ifnet *ifp = GET_IFP(sc);
326 int s;
327
328 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
329
330 /* Detached before attached finished */
331 if (!sc->sc_attached)
332 return (0);
333
334 usb_uncallout(sc->sc_stat_ch, udav_tick, sc);
335
336 /* Remove any pending tasks */
337 usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
338 usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
339
340 s = splusb();
341
342 if (--sc->sc_refcnt >= 0) {
343 /* Wait for processes to go away */
344 usb_detach_wait(USBDEV(sc->sc_dev));
345 }
346 if (ifp->if_flags & IFF_RUNNING)
347 udav_stop(GET_IFP(sc), 1);
348
349 #if NRND > 0
350 rnd_detach_source(&sc->rnd_source);
351 #endif
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 USBDEV(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", USBDEVNAME(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(USBDEV(sc->sc_dev));
407 if (err) {
408 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
409 USBDEVNAME(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", USBDEVNAME(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(USBDEV(sc->sc_dev));
444 if (err) {
445 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
446 USBDEVNAME(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", USBDEVNAME(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(USBDEV(sc->sc_dev));
480 if (err) {
481 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
482 USBDEVNAME(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", USBDEVNAME(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(USBDEV(sc->sc_dev));
518 if (err) {
519 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
520 USBDEVNAME(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", USBDEVNAME(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(USBDEV(sc->sc_dev));
555 if (err) {
556 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
557 USBDEVNAME(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", USBDEVNAME(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", USBDEVNAME(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(USBDEV(sc->sc_dev));
608 if (err) {
609 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
610 USBDEVNAME(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", USBDEVNAME(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", USBDEVNAME(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", USBDEVNAME(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 usb_callout(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", USBDEVNAME(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_ptr_t self, enum devact act)
733 {
734 struct udav_softc *sc = device_private(self);
735
736 DPRINTF(("%s: %s: enter, act=%d\n", USBDEVNAME(sc->sc_dev),
737 __func__, act));
738 switch (act) {
739 case DVACT_ACTIVATE:
740 return (EOPNOTSUPP);
741 break;
742
743 case DVACT_DEACTIVATE:
744 if_deactivate(&sc->sc_ec.ec_if);
745 sc->sc_dying = 1;
746 break;
747 }
748 return (0);
749 }
750
751 #define UDAV_BITS 6
752
753 #define UDAV_CALCHASH(addr) \
754 (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
755
756 Static void
757 udav_setmulti(struct udav_softc *sc)
758 {
759 struct ifnet *ifp;
760 struct ether_multi *enm;
761 struct ether_multistep step;
762 u_int8_t hashes[8];
763 int h = 0;
764
765 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
766
767 if (sc->sc_dying)
768 return;
769
770 ifp = GET_IFP(sc);
771
772 if (ifp->if_flags & IFF_PROMISC) {
773 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
774 return;
775 } else if (ifp->if_flags & IFF_ALLMULTI) {
776 allmulti:
777 ifp->if_flags |= IFF_ALLMULTI;
778 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
779 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
780 return;
781 }
782
783 /* first, zot all the existing hash bits */
784 memset(hashes, 0x00, sizeof(hashes));
785 hashes[7] |= 0x80; /* broadcast address */
786 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
787
788 /* now program new ones */
789 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
790 while (enm != NULL) {
791 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
792 ETHER_ADDR_LEN) != 0)
793 goto allmulti;
794
795 h = UDAV_CALCHASH(enm->enm_addrlo);
796 hashes[h>>3] |= 1 << (h & 0x7);
797 ETHER_NEXT_MULTI(step, enm);
798 }
799
800 /* disable all multicast */
801 ifp->if_flags &= ~IFF_ALLMULTI;
802 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
803
804 /* write hash value to the register */
805 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
806 }
807
808 Static int
809 udav_openpipes(struct udav_softc *sc)
810 {
811 struct udav_chain *c;
812 usbd_status err;
813 int i;
814 int error = 0;
815
816 if (sc->sc_dying)
817 return (EIO);
818
819 sc->sc_refcnt++;
820
821 /* Open RX pipe */
822 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
823 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
824 if (err) {
825 printf("%s: open rx pipe failed: %s\n",
826 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
827 error = EIO;
828 goto done;
829 }
830
831 /* Open TX pipe */
832 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
833 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
834 if (err) {
835 printf("%s: open tx pipe failed: %s\n",
836 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
837 error = EIO;
838 goto done;
839 }
840
841 #if 0
842 /* XXX: interrupt endpoint is not yet supported */
843 /* Open Interrupt pipe */
844 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
845 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
846 &sc->sc_cdata.udav_ibuf, UDAV_INTR_PKGLEN,
847 udav_intr, USBD_DEFAULT_INTERVAL);
848 if (err) {
849 printf("%s: open intr pipe failed: %s\n",
850 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
851 error = EIO;
852 goto done;
853 }
854 #endif
855
856
857 /* Start up the receive pipe. */
858 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
859 c = &sc->sc_cdata.udav_rx_chain[i];
860 usbd_setup_xfer(c->udav_xfer, sc->sc_pipe_rx,
861 c, c->udav_buf, UDAV_BUFSZ,
862 USBD_SHORT_XFER_OK | USBD_NO_COPY,
863 USBD_NO_TIMEOUT, udav_rxeof);
864 (void)usbd_transfer(c->udav_xfer);
865 DPRINTF(("%s: %s: start read\n", USBDEVNAME(sc->sc_dev),
866 __func__));
867 }
868
869 done:
870 if (--sc->sc_refcnt < 0)
871 usb_detach_wakeup(USBDEV(sc->sc_dev));
872
873 return (error);
874 }
875
876 Static int
877 udav_newbuf(struct udav_softc *sc, struct udav_chain *c, struct mbuf *m)
878 {
879 struct mbuf *m_new = NULL;
880
881 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
882
883 if (m == NULL) {
884 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
885 if (m_new == NULL) {
886 printf("%s: no memory for rx list "
887 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
888 return (ENOBUFS);
889 }
890 MCLGET(m_new, M_DONTWAIT);
891 if (!(m_new->m_flags & M_EXT)) {
892 printf("%s: no memory for rx list "
893 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
894 m_freem(m_new);
895 return (ENOBUFS);
896 }
897 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
898 } else {
899 m_new = m;
900 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
901 m_new->m_data = m_new->m_ext.ext_buf;
902 }
903
904 m_adj(m_new, ETHER_ALIGN);
905 c->udav_mbuf = m_new;
906
907 return (0);
908 }
909
910
911 Static int
912 udav_rx_list_init(struct udav_softc *sc)
913 {
914 struct udav_cdata *cd;
915 struct udav_chain *c;
916 int i;
917
918 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
919
920 cd = &sc->sc_cdata;
921 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
922 c = &cd->udav_rx_chain[i];
923 c->udav_sc = sc;
924 c->udav_idx = i;
925 if (udav_newbuf(sc, c, NULL) == ENOBUFS)
926 return (ENOBUFS);
927 if (c->udav_xfer == NULL) {
928 c->udav_xfer = usbd_alloc_xfer(sc->sc_udev);
929 if (c->udav_xfer == NULL)
930 return (ENOBUFS);
931 c->udav_buf = usbd_alloc_buffer(c->udav_xfer, UDAV_BUFSZ);
932 if (c->udav_buf == NULL) {
933 usbd_free_xfer(c->udav_xfer);
934 return (ENOBUFS);
935 }
936 }
937 }
938
939 return (0);
940 }
941
942 Static int
943 udav_tx_list_init(struct udav_softc *sc)
944 {
945 struct udav_cdata *cd;
946 struct udav_chain *c;
947 int i;
948
949 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
950
951 cd = &sc->sc_cdata;
952 for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
953 c = &cd->udav_tx_chain[i];
954 c->udav_sc = sc;
955 c->udav_idx = i;
956 c->udav_mbuf = NULL;
957 if (c->udav_xfer == NULL) {
958 c->udav_xfer = usbd_alloc_xfer(sc->sc_udev);
959 if (c->udav_xfer == NULL)
960 return (ENOBUFS);
961 c->udav_buf = usbd_alloc_buffer(c->udav_xfer, UDAV_BUFSZ);
962 if (c->udav_buf == NULL) {
963 usbd_free_xfer(c->udav_xfer);
964 return (ENOBUFS);
965 }
966 }
967 }
968
969 return (0);
970 }
971
972 Static void
973 udav_start(struct ifnet *ifp)
974 {
975 struct udav_softc *sc = ifp->if_softc;
976 struct mbuf *m_head = NULL;
977
978 DPRINTF(("%s: %s: enter, link=%d\n", USBDEVNAME(sc->sc_dev),
979 __func__, sc->sc_link));
980
981 if (sc->sc_dying)
982 return;
983
984 if (!sc->sc_link)
985 return;
986
987 if (ifp->if_flags & IFF_OACTIVE)
988 return;
989
990 IFQ_POLL(&ifp->if_snd, m_head);
991 if (m_head == NULL)
992 return;
993
994 if (udav_send(sc, m_head, 0)) {
995 ifp->if_flags |= IFF_OACTIVE;
996 return;
997 }
998
999 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1000
1001 #if NBPFILTER > 0
1002 if (ifp->if_bpf)
1003 bpf_mtap(ifp->if_bpf, m_head);
1004 #endif
1005
1006 ifp->if_flags |= IFF_OACTIVE;
1007
1008 /* Set a timeout in case the chip goes out to lunch. */
1009 ifp->if_timer = 5;
1010 }
1011
1012 Static int
1013 udav_send(struct udav_softc *sc, struct mbuf *m, int idx)
1014 {
1015 int total_len;
1016 struct udav_chain *c;
1017 usbd_status err;
1018
1019 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
1020
1021 c = &sc->sc_cdata.udav_tx_chain[idx];
1022
1023 /* Copy the mbuf data into a contiguous buffer */
1024 /* first 2 bytes are packet length */
1025 m_copydata(m, 0, m->m_pkthdr.len, c->udav_buf + 2);
1026 c->udav_mbuf = m;
1027 total_len = m->m_pkthdr.len;
1028 if (total_len < UDAV_MIN_FRAME_LEN) {
1029 memset(c->udav_buf + 2 + total_len, 0,
1030 UDAV_MIN_FRAME_LEN - total_len);
1031 total_len = UDAV_MIN_FRAME_LEN;
1032 }
1033
1034 /* Frame length is specified in the first 2bytes of the buffer */
1035 c->udav_buf[0] = (u_int8_t)total_len;
1036 c->udav_buf[1] = (u_int8_t)(total_len >> 8);
1037 total_len += 2;
1038
1039 usbd_setup_xfer(c->udav_xfer, sc->sc_pipe_tx, c, c->udav_buf, total_len,
1040 USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1041 UDAV_TX_TIMEOUT, udav_txeof);
1042
1043 /* Transmit */
1044 sc->sc_refcnt++;
1045 err = usbd_transfer(c->udav_xfer);
1046 if (--sc->sc_refcnt < 0)
1047 usb_detach_wakeup(USBDEV(sc->sc_dev));
1048 if (err != USBD_IN_PROGRESS) {
1049 printf("%s: udav_send error=%s\n", USBDEVNAME(sc->sc_dev),
1050 usbd_errstr(err));
1051 /* Stop the interface */
1052 usb_add_task(sc->sc_udev, &sc->sc_stop_task,
1053 USB_TASKQ_DRIVER);
1054 return (EIO);
1055 }
1056
1057 DPRINTF(("%s: %s: send %d bytes\n", USBDEVNAME(sc->sc_dev),
1058 __func__, total_len));
1059
1060 sc->sc_cdata.udav_tx_cnt++;
1061
1062 return (0);
1063 }
1064
1065 Static void
1066 udav_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
1067 usbd_status status)
1068 {
1069 struct udav_chain *c = priv;
1070 struct udav_softc *sc = c->udav_sc;
1071 struct ifnet *ifp = GET_IFP(sc);
1072 int s;
1073
1074 if (sc->sc_dying)
1075 return;
1076
1077 s = splnet();
1078
1079 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1080
1081 ifp->if_timer = 0;
1082 ifp->if_flags &= ~IFF_OACTIVE;
1083
1084 if (status != USBD_NORMAL_COMPLETION) {
1085 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1086 splx(s);
1087 return;
1088 }
1089 ifp->if_oerrors++;
1090 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
1091 usbd_errstr(status));
1092 if (status == USBD_STALLED) {
1093 sc->sc_refcnt++;
1094 usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
1095 if (--sc->sc_refcnt < 0)
1096 usb_detach_wakeup(USBDEV(sc->sc_dev));
1097 }
1098 splx(s);
1099 return;
1100 }
1101
1102 ifp->if_opackets++;
1103
1104 m_freem(c->udav_mbuf);
1105 c->udav_mbuf = NULL;
1106
1107 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1108 udav_start(ifp);
1109
1110 splx(s);
1111 }
1112
1113 Static void
1114 udav_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1115 {
1116 struct udav_chain *c = priv;
1117 struct udav_softc *sc = c->udav_sc;
1118 struct ifnet *ifp = GET_IFP(sc);
1119 struct mbuf *m;
1120 u_int32_t total_len;
1121 u_int8_t *pktstat;
1122 int s;
1123
1124 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
1125
1126 if (sc->sc_dying)
1127 return;
1128
1129 if (status != USBD_NORMAL_COMPLETION) {
1130 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1131 return;
1132 sc->sc_rx_errs++;
1133 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1134 printf("%s: %u usb errors on rx: %s\n",
1135 USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
1136 usbd_errstr(status));
1137 sc->sc_rx_errs = 0;
1138 }
1139 if (status == USBD_STALLED) {
1140 sc->sc_refcnt++;
1141 usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
1142 if (--sc->sc_refcnt < 0)
1143 usb_detach_wakeup(USBDEV(sc->sc_dev));
1144 }
1145 goto done;
1146 }
1147
1148 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1149
1150 /* copy data to mbuf */
1151 m = c->udav_mbuf;
1152 memcpy(mtod(m, char *), c->udav_buf, total_len);
1153
1154 /* first byte in received data */
1155 pktstat = mtod(m, u_int8_t *);
1156 m_adj(m, sizeof(u_int8_t));
1157 DPRINTF(("%s: RX Status: 0x%02x\n", USBDEVNAME(sc->sc_dev),
1158 *pktstat));
1159
1160 total_len = UGETW(mtod(m, u_int8_t *));
1161 m_adj(m, sizeof(u_int16_t));
1162
1163 if (*pktstat & UDAV_RSR_LCS) {
1164 ifp->if_collisions++;
1165 goto done;
1166 }
1167
1168 if (total_len < sizeof(struct ether_header) ||
1169 *pktstat & UDAV_RSR_ERR) {
1170 ifp->if_ierrors++;
1171 goto done;
1172 }
1173
1174 ifp->if_ipackets++;
1175 total_len -= ETHER_CRC_LEN;
1176
1177 m->m_pkthdr.len = m->m_len = total_len;
1178 m->m_pkthdr.rcvif = ifp;
1179
1180 s = splnet();
1181
1182 if (udav_newbuf(sc, c, NULL) == ENOBUFS) {
1183 ifp->if_ierrors++;
1184 goto done1;
1185 }
1186
1187 #if NBPFILTER > 0
1188 if (ifp->if_bpf)
1189 BPF_MTAP(ifp, m);
1190 #endif
1191
1192 DPRINTF(("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
1193 __func__, m->m_len));
1194 IF_INPUT(ifp, m);
1195
1196 done1:
1197 splx(s);
1198
1199 done:
1200 /* Setup new transfer */
1201 usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->udav_buf, UDAV_BUFSZ,
1202 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1203 USBD_NO_TIMEOUT, udav_rxeof);
1204 sc->sc_refcnt++;
1205 usbd_transfer(xfer);
1206 if (--sc->sc_refcnt < 0)
1207 usb_detach_wakeup(USBDEV(sc->sc_dev));
1208
1209 DPRINTF(("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), __func__));
1210 }
1211
1212 #if 0
1213 Static void udav_intr(void)
1214 {
1215 }
1216 #endif
1217
1218 Static int
1219 udav_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1220 {
1221 struct udav_softc *sc = ifp->if_softc;
1222 int s, error = 0;
1223
1224 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1225
1226 if (sc->sc_dying)
1227 return (EIO);
1228
1229 s = splnet();
1230
1231 error = ether_ioctl(ifp, cmd, data);
1232 if (error == ENETRESET) {
1233 if (ifp->if_flags & IFF_RUNNING)
1234 udav_setmulti(sc);
1235 error = 0;
1236 }
1237
1238 splx(s);
1239
1240 return (error);
1241 }
1242
1243 Static void
1244 udav_watchdog(struct ifnet *ifp)
1245 {
1246 struct udav_softc *sc = ifp->if_softc;
1247 struct udav_chain *c;
1248 usbd_status stat;
1249 int s;
1250
1251 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1252
1253 ifp->if_oerrors++;
1254 printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
1255
1256 s = splusb();
1257 c = &sc->sc_cdata.udav_tx_chain[0];
1258 usbd_get_xfer_status(c->udav_xfer, NULL, NULL, NULL, &stat);
1259 udav_txeof(c->udav_xfer, c, stat);
1260
1261 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1262 udav_start(ifp);
1263 splx(s);
1264 }
1265
1266 Static void
1267 udav_stop_task(struct udav_softc *sc)
1268 {
1269 udav_stop(GET_IFP(sc), 1);
1270 }
1271
1272 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1273 Static void
1274 udav_stop(struct ifnet *ifp, int disable)
1275 {
1276 struct udav_softc *sc = ifp->if_softc;
1277 usbd_status err;
1278 int i;
1279
1280 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1281
1282 ifp->if_timer = 0;
1283
1284 udav_reset(sc);
1285
1286 usb_uncallout(sc->sc_stat_ch, udav_tick, sc);
1287
1288 /* Stop transfers */
1289 /* RX endpoint */
1290 if (sc->sc_pipe_rx != NULL) {
1291 err = usbd_abort_pipe(sc->sc_pipe_rx);
1292 if (err)
1293 printf("%s: abort rx pipe failed: %s\n",
1294 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1295 err = usbd_close_pipe(sc->sc_pipe_rx);
1296 if (err)
1297 printf("%s: close rx pipe failed: %s\n",
1298 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1299 sc->sc_pipe_rx = NULL;
1300 }
1301
1302 /* TX endpoint */
1303 if (sc->sc_pipe_tx != NULL) {
1304 err = usbd_abort_pipe(sc->sc_pipe_tx);
1305 if (err)
1306 printf("%s: abort tx pipe failed: %s\n",
1307 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1308 err = usbd_close_pipe(sc->sc_pipe_tx);
1309 if (err)
1310 printf("%s: close tx pipe failed: %s\n",
1311 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1312 sc->sc_pipe_tx = NULL;
1313 }
1314
1315 #if 0
1316 /* XXX: Interrupt endpoint is not yet supported!! */
1317 /* Interrupt endpoint */
1318 if (sc->sc_pipe_intr != NULL) {
1319 err = usbd_abort_pipe(sc->sc_pipe_intr);
1320 if (err)
1321 printf("%s: abort intr pipe failed: %s\n",
1322 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1323 err = usbd_close_pipe(sc->sc_pipe_intr);
1324 if (err)
1325 printf("%s: close intr pipe failed: %s\n",
1326 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1327 sc->sc_pipe_intr = NULL;
1328 }
1329 #endif
1330
1331 /* Free RX resources. */
1332 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
1333 if (sc->sc_cdata.udav_rx_chain[i].udav_mbuf != NULL) {
1334 m_freem(sc->sc_cdata.udav_rx_chain[i].udav_mbuf);
1335 sc->sc_cdata.udav_rx_chain[i].udav_mbuf = NULL;
1336 }
1337 if (sc->sc_cdata.udav_rx_chain[i].udav_xfer != NULL) {
1338 usbd_free_xfer(sc->sc_cdata.udav_rx_chain[i].udav_xfer);
1339 sc->sc_cdata.udav_rx_chain[i].udav_xfer = NULL;
1340 }
1341 }
1342
1343 /* Free TX resources. */
1344 for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
1345 if (sc->sc_cdata.udav_tx_chain[i].udav_mbuf != NULL) {
1346 m_freem(sc->sc_cdata.udav_tx_chain[i].udav_mbuf);
1347 sc->sc_cdata.udav_tx_chain[i].udav_mbuf = NULL;
1348 }
1349 if (sc->sc_cdata.udav_tx_chain[i].udav_xfer != NULL) {
1350 usbd_free_xfer(sc->sc_cdata.udav_tx_chain[i].udav_xfer);
1351 sc->sc_cdata.udav_tx_chain[i].udav_xfer = NULL;
1352 }
1353 }
1354
1355 sc->sc_link = 0;
1356 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1357 }
1358
1359 /* Set media options */
1360 Static int
1361 udav_ifmedia_change(struct ifnet *ifp)
1362 {
1363 struct udav_softc *sc = ifp->if_softc;
1364 struct mii_data *mii = GET_MII(sc);
1365 int rc;
1366
1367 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1368
1369 if (sc->sc_dying)
1370 return (0);
1371
1372 sc->sc_link = 0;
1373 if ((rc = mii_mediachg(mii)) == ENXIO)
1374 return 0;
1375 return rc;
1376 }
1377
1378 /* Report current media status. */
1379 Static void
1380 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1381 {
1382 struct udav_softc *sc = ifp->if_softc;
1383
1384 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1385
1386 if (sc->sc_dying)
1387 return;
1388
1389 ether_mediastatus(ifp, ifmr);
1390 }
1391
1392 Static void
1393 udav_tick(void *xsc)
1394 {
1395 struct udav_softc *sc = xsc;
1396
1397 if (sc == NULL)
1398 return;
1399
1400 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1401 __func__));
1402
1403 if (sc->sc_dying)
1404 return;
1405
1406 /* Perform periodic stuff in process context */
1407 usb_add_task(sc->sc_udev, &sc->sc_tick_task,
1408 USB_TASKQ_DRIVER);
1409 }
1410
1411 Static void
1412 udav_tick_task(void *xsc)
1413 {
1414 struct udav_softc *sc = xsc;
1415 struct ifnet *ifp;
1416 struct mii_data *mii;
1417 int s;
1418
1419 if (sc == NULL)
1420 return;
1421
1422 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1423 __func__));
1424
1425 if (sc->sc_dying)
1426 return;
1427
1428 ifp = GET_IFP(sc);
1429 mii = GET_MII(sc);
1430
1431 if (mii == NULL)
1432 return;
1433
1434 s = splnet();
1435
1436 mii_tick(mii);
1437 if (!sc->sc_link) {
1438 mii_pollstat(mii);
1439 if (mii->mii_media_status & IFM_ACTIVE &&
1440 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1441 DPRINTF(("%s: %s: got link\n",
1442 USBDEVNAME(sc->sc_dev), __func__));
1443 sc->sc_link++;
1444 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1445 udav_start(ifp);
1446 }
1447 }
1448
1449 usb_callout(sc->sc_stat_ch, hz, udav_tick, sc);
1450
1451 splx(s);
1452 }
1453
1454 /* Get exclusive access to the MII registers */
1455 Static void
1456 udav_lock_mii(struct udav_softc *sc)
1457 {
1458 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1459 __func__));
1460
1461 sc->sc_refcnt++;
1462 mutex_enter(&sc->sc_mii_lock);
1463 }
1464
1465 Static void
1466 udav_unlock_mii(struct udav_softc *sc)
1467 {
1468 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1469 __func__));
1470
1471 mutex_exit(&sc->sc_mii_lock);
1472 if (--sc->sc_refcnt < 0)
1473 usb_detach_wakeup(USBDEV(sc->sc_dev));
1474 }
1475
1476 Static int
1477 udav_miibus_readreg(device_ptr_t dev, int phy, int reg)
1478 {
1479 struct udav_softc *sc;
1480 u_int8_t val[2];
1481 u_int16_t data16;
1482
1483 if (dev == NULL)
1484 return (0);
1485
1486 sc = USBGETSOFTC(dev);
1487
1488 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1489 USBDEVNAME(sc->sc_dev), __func__, phy, reg));
1490
1491 if (sc->sc_dying) {
1492 #ifdef DIAGNOSTIC
1493 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1494 __func__);
1495 #endif
1496 return (0);
1497 }
1498
1499 /* XXX: one PHY only for the internal PHY */
1500 if (phy != 0) {
1501 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1502 USBDEVNAME(sc->sc_dev), __func__, phy));
1503 return (0);
1504 }
1505
1506 udav_lock_mii(sc);
1507
1508 /* select internal PHY and set PHY register address */
1509 udav_csr_write1(sc, UDAV_EPAR,
1510 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1511
1512 /* select PHY operation and start read command */
1513 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
1514
1515 /* XXX: should be wait? */
1516
1517 /* end read command */
1518 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
1519
1520 /* retrieve the result from data registers */
1521 udav_csr_read(sc, UDAV_EPDRL, val, 2);
1522
1523 udav_unlock_mii(sc);
1524
1525 data16 = val[0] | (val[1] << 8);
1526
1527 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1528 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data16));
1529
1530 return (data16);
1531 }
1532
1533 Static void
1534 udav_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1535 {
1536 struct udav_softc *sc;
1537 u_int8_t val[2];
1538
1539 if (dev == NULL)
1540 return;
1541
1542 sc = USBGETSOFTC(dev);
1543
1544 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1545 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data));
1546
1547 if (sc->sc_dying) {
1548 #ifdef DIAGNOSTIC
1549 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1550 __func__);
1551 #endif
1552 return;
1553 }
1554
1555 /* XXX: one PHY only for the internal PHY */
1556 if (phy != 0) {
1557 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1558 USBDEVNAME(sc->sc_dev), __func__, phy));
1559 return;
1560 }
1561
1562 udav_lock_mii(sc);
1563
1564 /* select internal PHY and set PHY register address */
1565 udav_csr_write1(sc, UDAV_EPAR,
1566 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1567
1568 /* put the value to the data registers */
1569 val[0] = data & 0xff;
1570 val[1] = (data >> 8) & 0xff;
1571 udav_csr_write(sc, UDAV_EPDRL, val, 2);
1572
1573 /* select PHY operation and start write command */
1574 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
1575
1576 /* XXX: should be wait? */
1577
1578 /* end write command */
1579 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
1580
1581 udav_unlock_mii(sc);
1582
1583 return;
1584 }
1585
1586 Static void
1587 udav_miibus_statchg(device_ptr_t dev)
1588 {
1589 #ifdef UDAV_DEBUG
1590 struct udav_softc *sc;
1591
1592 if (dev == NULL)
1593 return;
1594
1595 sc = USBGETSOFTC(dev);
1596 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1597 #endif
1598 /* Nothing to do */
1599 }
1600