if_udav.c revision 1.1 1 /* $NetBSD: if_udav.c,v 1.1 2003/08/22 05:13:29 itojun 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Shingo WATANABE.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35
36 /*
37 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
38 * The spec can be found at the following url.
39 * http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-F01-062202s.pdf
40 */
41
42 /*
43 * TODO:
44 * Interrupt Endpoint support
45 * External PHYs
46 * powerhook() support?
47 */
48
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.1 2003/08/22 05:13:29 itojun Exp $");
51
52 #include "opt_inet.h"
53 #include "opt_ns.h"
54 #include "bpfilter.h"
55 #include "rnd.h"
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/lock.h>
60 #include <sys/mbuf.h>
61 #include <sys/kernel.h>
62 #include <sys/socket.h>
63
64 #include <sys/device.h>
65 #if NRND > 0
66 #include <sys/rnd.h>
67 #endif
68
69 #include <net/if.h>
70 #include <net/if_arp.h>
71 #include <net/if_dl.h>
72 #include <net/if_media.h>
73
74 #if NBPFILTER > 0
75 #include <net/bpf.h>
76 #endif
77 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
78
79 #include <net/if_ether.h>
80 #ifdef INET
81 #include <netinet/in.h>
82 #include <netinet/if_inarp.h>
83 #endif
84 #ifdef NS
85 #include <netns/ns.h>
86 #include <netns/ns_if.h>
87 #endif
88
89 #include <dev/mii/mii.h>
90 #include <dev/mii/miivar.h>
91
92 #include <dev/usb/usb.h>
93 #include <dev/usb/usbdi.h>
94 #include <dev/usb/usbdi_util.h>
95 #include <dev/usb/usbdevs.h>
96
97 #include <dev/usb/if_udavreg.h>
98
99
100 /* Function declarations */
101 USB_DECLARE_DRIVER(udav);
102
103 Static int udav_openpipes(struct udav_softc *);
104 Static int udav_rx_list_init(struct udav_softc *);
105 Static int udav_tx_list_init(struct udav_softc *);
106 Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *);
107 Static void udav_start(struct ifnet *);
108 Static int udav_send(struct udav_softc *, struct mbuf *, int);
109 Static void udav_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
110 Static void udav_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
111 Static void udav_tick(void *);
112 Static void udav_tick_task(void *);
113 Static int udav_ioctl(struct ifnet *, u_long, caddr_t);
114 Static void udav_stop_task(struct udav_softc *);
115 Static void udav_stop(struct ifnet *, int);
116 Static void udav_watchdog(struct ifnet *);
117 Static int udav_ifmedia_change(struct ifnet *);
118 Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
119 Static void udav_lock_mii(struct udav_softc *);
120 Static void udav_unlock_mii(struct udav_softc *);
121 Static int udav_miibus_readreg(device_ptr_t, int, int);
122 Static void udav_miibus_writereg(device_ptr_t, int, int, int);
123 Static void udav_miibus_statchg(device_ptr_t);
124 Static int udav_init(struct ifnet *);
125 Static void udav_setmulti(struct udav_softc *);
126 Static void udav_reset(struct udav_softc *);
127
128 Static int udav_csr_read(struct udav_softc *, int, void *, int);
129 Static int udav_csr_write(struct udav_softc *, int, void *, int);
130 Static int udav_csr_read1(struct udav_softc *, int);
131 Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
132
133 #if 0
134 Static int udav_mem_read(struct udav_softc *, int, void *, int);
135 Static int udav_mem_write(struct udav_softc *, int, void *, int);
136 Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
137 #endif
138
139 /* Macros */
140 #ifdef UDAV_DEBUG
141 #define DPRINTF(x) if (udavdebug) logprintf x
142 #define DPRINTFN(n,x) if (udavdebug >= (n)) logprintf x
143 int udavdebug = 0;
144 #else
145 #define DPRINTF(x)
146 #define DPRINTFN(n,x)
147 #endif
148
149 #define UDAV_SETBIT(sc, reg, x) \
150 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
151
152 #define UDAV_CLRBIT(sc, reg, x) \
153 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
154
155 static const struct udav_type {
156 struct usb_devno udav_dev;
157 u_int16_t udav_flags;
158 #define UDAV_EXT_PHY 0x0001
159 } udav_devs [] = {
160 /* Corega USB-TXC */
161 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
162 #if 0
163 /* DAVICOM DM9601 Generic? */
164 /* XXX: The following ids was obtained from the data sheet. */
165 {{ 0x0a46, 0x9601 }, 0},
166 #endif
167 };
168 #define udav_lookup(v, p) ((struct udav_type *)usb_lookup(udav_devs, v, p))
169
170
171 /* Probe */
172 USB_MATCH(udav)
173 {
174 USB_MATCH_START(udav, uaa);
175
176 if (uaa->iface != NULL)
177 return (UMATCH_NONE);
178
179 return (udav_lookup(uaa->vendor, uaa->product) != NULL ?
180 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
181 }
182
183 /* Attach */
184 USB_ATTACH(udav)
185 {
186 USB_ATTACH_START(udav, sc, uaa);
187 usbd_device_handle dev = uaa->device;
188 usbd_interface_handle iface;
189 usbd_status err;
190 usb_interface_descriptor_t *id;
191 usb_endpoint_descriptor_t *ed;
192 char devinfo[1024];
193 char *devname = USBDEVNAME(sc->sc_dev);
194 struct ifnet *ifp;
195 struct mii_data *mii;
196 u_char eaddr[ETHER_ADDR_LEN];
197 int i, s;
198
199 usbd_devinfo(dev, 0, devinfo);
200 USB_ATTACH_SETUP;
201 printf("%s: %s\n", devname, devinfo);
202
203 /* Move the device into the configured state. */
204 err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1);
205 if (err) {
206 printf("%s: setting config no failed\n", devname);
207 goto bad;
208 }
209
210 usb_init_task(&sc->sc_tick_task, udav_tick_task, sc);
211 lockinit(&sc->sc_mii_lock, PZERO, "udavmii", 0, 0);
212 usb_init_task(&sc->sc_stop_task, (void (*)(void *)) udav_stop_task, sc);
213
214 /* get control interface */
215 err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
216 if (err) {
217 printf("%s: failed to get interface, err=%s\n", devname,
218 usbd_errstr(err));
219 goto bad;
220 }
221
222 sc->sc_udev = dev;
223 sc->sc_ctl_iface = iface;
224 sc->sc_flags = udav_lookup(uaa->vendor, uaa->product)->udav_flags;
225
226 /* get interface descriptor */
227 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
228
229 /* find endpoints */
230 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
231 for (i = 0; i < id->bNumEndpoints; i++) {
232 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
233 if (ed == NULL) {
234 printf("%s: couldn't get endpoint %d\n", devname, i);
235 goto bad;
236 }
237 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
238 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
239 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
240 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
241 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
242 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
243 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
244 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
245 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
246 }
247
248 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
249 sc->sc_intrin_no == -1) {
250 printf("%s: missing endpoint\n", devname);
251 goto bad;
252 }
253
254 s = splnet();
255
256 /* reset the adapter */
257 udav_reset(sc);
258
259 /* Get Ethernet Address */
260 err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
261 if (err) {
262 printf("%s: read MAC address failed\n", devname);
263 splx(s);
264 goto bad;
265 }
266
267 /* Print Ethernet Address */
268 printf("%s: Ethernet address %s\n", devname, ether_sprintf(eaddr));
269
270 /* initialize interface infomation */
271 ifp = GET_IFP(sc);
272 ifp->if_softc = sc;
273 ifp->if_mtu = ETHERMTU;
274 strncpy(ifp->if_xname, devname, IFNAMSIZ);
275 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
276 ifp->if_start = udav_start;
277 ifp->if_ioctl = udav_ioctl;
278 ifp->if_watchdog = udav_watchdog;
279 ifp->if_init = udav_init;
280 ifp->if_stop = udav_stop;
281
282 IFQ_SET_READY(&ifp->if_snd);
283
284 /*
285 * Do ifmedia setup.
286 */
287 mii = &sc->sc_mii;
288 mii->mii_ifp = ifp;
289 mii->mii_readreg = udav_miibus_readreg;
290 mii->mii_writereg = udav_miibus_writereg;
291 mii->mii_statchg = udav_miibus_statchg;
292 mii->mii_flags = MIIF_AUTOTSLEEP;
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 #if NRND > 0
307 rnd_attach_source(&sc->rnd_source, devname, RND_TYPE_NET, 0);
308 #endif
309
310 usb_callout_init(sc->sc_stat_ch);
311 sc->sc_attached = 1;
312 splx(s);
313
314 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
315
316 USB_ATTACH_SUCCESS_RETURN;
317
318 bad:
319 sc->sc_dying = 1;
320 USB_ATTACH_ERROR_RETURN;
321 }
322
323 /* detach */
324 USB_DETACH(udav)
325 {
326 USB_DETACH_START(udav, sc);
327 struct ifnet *ifp = GET_IFP(sc);
328 int s;
329
330 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
331
332 /* Detached before attached finished */
333 if (!sc->sc_attached)
334 return (0);
335
336 usb_uncallout(sc->sc_stat_ch, udav_tick, sc);
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(USBDEV(sc->sc_dev));
347 }
348 if (ifp->if_flags & IFF_RUNNING)
349 udav_stop(GET_IFP(sc), 1);
350
351 #if NRND > 0
352 rnd_detach_source(&sc->rnd_source);
353 #endif
354 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
355 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
356 ether_ifdetach(ifp);
357 if_detach(ifp);
358
359 #ifdef DIAGNOSTIC
360 if (sc->sc_pipe_tx != NULL)
361 printf("%s: detach has active tx endpoint.\n",
362 USBDEVNAME(sc->sc_dev));
363 if (sc->sc_pipe_rx != NULL)
364 printf("%s: detach has active rx endpoint.\n",
365 USBDEVNAME(sc->sc_dev));
366 if (sc->sc_pipe_intr != NULL)
367 printf("%s: detach has active intr endpoint.\n",
368 USBDEVNAME(sc->sc_dev));
369 #endif
370 sc->sc_attached = 0;
371
372 splx(s);
373
374 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
375 USBDEV(sc->sc_dev));
376
377 return (0);
378 }
379
380 #if 0
381 /* read memory */
382 Static int
383 udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len)
384 {
385 usb_device_request_t req;
386 usbd_status err;
387
388 if (sc == NULL)
389 return (0);
390
391 DPRINTFN(0x200,
392 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
393
394 if (sc->sc_dying)
395 return (0);
396
397 offset &= 0xffff;
398 len &= 0xff;
399
400 req.bmRequestType = UT_READ_VENDOR_DEVICE;
401 req.bRequest = UDAV_REQ_MEM_READ;
402 USETW(req.wValue, 0x0000);
403 USETW(req.wIndex, offset);
404 USETW(req.wLength, len);
405
406 sc->sc_refcnt++;
407 err = usbd_do_request(sc->sc_udev, &req, buf);
408 if (--sc->sc_refcnt < 0)
409 usb_detach_wakeup(USBDEV(sc->sc_dev));
410 if (err) {
411 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
412 USBDEVNAME(sc->sc_dev), __func__, offset, err));
413 }
414
415 return (err);
416 }
417
418 /* write memory */
419 Static int
420 udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len)
421 {
422 usb_device_request_t req;
423 usbd_status err;
424
425 if (sc == NULL)
426 return (0);
427
428 DPRINTFN(0x200,
429 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
430
431 if (sc->sc_dying)
432 return (0);
433
434 offset &= 0xffff;
435 len &= 0xff;
436
437 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
438 req.bRequest = UDAV_REQ_MEM_WRITE;
439 USETW(req.wValue, 0x0000);
440 USETW(req.wIndex, offset);
441 USETW(req.wLength, len);
442
443 sc->sc_refcnt++;
444 err = usbd_do_request(sc->sc_udev, &req, buf);
445 if (--sc->sc_refcnt < 0)
446 usb_detach_wakeup(USBDEV(sc->sc_dev));
447 if (err) {
448 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
449 USBDEVNAME(sc->sc_dev), __func__, offset, err));
450 }
451
452 return (err);
453 }
454
455 /* write memory */
456 Static int
457 udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch)
458 {
459 usb_device_request_t req;
460 usbd_status err;
461
462 if (sc == NULL)
463 return (0);
464
465 DPRINTFN(0x200,
466 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
467
468 if (sc->sc_dying)
469 return (0);
470
471 offset &= 0xffff;
472
473 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
474 req.bRequest = UDAV_REQ_MEM_WRITE1;
475 USETW(req.wValue, ch);
476 USETW(req.wIndex, offset);
477 USETW(req.wLength, 0x0000);
478
479 sc->sc_refcnt++;
480 err = usbd_do_request(sc->sc_udev, &req, NULL);
481 if (--sc->sc_refcnt < 0)
482 usb_detach_wakeup(USBDEV(sc->sc_dev));
483 if (err) {
484 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
485 USBDEVNAME(sc->sc_dev), __func__, offset, err));
486 }
487
488 return (err);
489 }
490 #endif
491
492 /* read register(s) */
493 Static int
494 udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len)
495 {
496 usb_device_request_t req;
497 usbd_status err;
498
499 if (sc == NULL)
500 return (0);
501
502 DPRINTFN(0x200,
503 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
504
505 if (sc->sc_dying)
506 return (0);
507
508 offset &= 0xff;
509 len &= 0xff;
510
511 req.bmRequestType = UT_READ_VENDOR_DEVICE;
512 req.bRequest = UDAV_REQ_REG_READ;
513 USETW(req.wValue, 0x0000);
514 USETW(req.wIndex, offset);
515 USETW(req.wLength, len);
516
517 sc->sc_refcnt++;
518 err = usbd_do_request(sc->sc_udev, &req, buf);
519 if (--sc->sc_refcnt < 0)
520 usb_detach_wakeup(USBDEV(sc->sc_dev));
521 if (err) {
522 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
523 USBDEVNAME(sc->sc_dev), __func__, offset, err));
524 }
525
526 return (err);
527 }
528
529 /* write register(s) */
530 Static int
531 udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len)
532 {
533 usb_device_request_t req;
534 usbd_status err;
535
536 if (sc == NULL)
537 return (0);
538
539 DPRINTFN(0x200,
540 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
541
542 if (sc->sc_dying)
543 return (0);
544
545 offset &= 0xff;
546 len &= 0xff;
547
548 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
549 req.bRequest = UDAV_REQ_REG_WRITE;
550 USETW(req.wValue, 0x0000);
551 USETW(req.wIndex, offset);
552 USETW(req.wLength, len);
553
554 sc->sc_refcnt++;
555 err = usbd_do_request(sc->sc_udev, &req, buf);
556 if (--sc->sc_refcnt < 0)
557 usb_detach_wakeup(USBDEV(sc->sc_dev));
558 if (err) {
559 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
560 USBDEVNAME(sc->sc_dev), __func__, offset, err));
561 }
562
563 return (err);
564 }
565
566 Static int
567 udav_csr_read1(struct udav_softc *sc, int offset)
568 {
569 u_int8_t val = 0;
570
571 if (sc == NULL)
572 return (0);
573
574 DPRINTFN(0x200,
575 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
576
577 if (sc->sc_dying)
578 return (0);
579
580 return (udav_csr_read(sc, offset, &val, 1) ? 0 : val);
581 }
582
583 /* write a register */
584 Static int
585 udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch)
586 {
587 usb_device_request_t req;
588 usbd_status err;
589
590 if (sc == NULL)
591 return (0);
592
593 DPRINTFN(0x200,
594 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
595
596 if (sc->sc_dying)
597 return (0);
598
599 offset &= 0xff;
600
601 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
602 req.bRequest = UDAV_REQ_REG_WRITE1;
603 USETW(req.wValue, ch);
604 USETW(req.wIndex, offset);
605 USETW(req.wLength, 0x0000);
606
607 sc->sc_refcnt++;
608 err = usbd_do_request(sc->sc_udev, &req, NULL);
609 if (--sc->sc_refcnt < 0)
610 usb_detach_wakeup(USBDEV(sc->sc_dev));
611 if (err) {
612 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
613 USBDEVNAME(sc->sc_dev), __func__, offset, err));
614 }
615
616 return (err);
617 }
618
619 Static int
620 udav_init(struct ifnet *ifp)
621 {
622 struct udav_softc *sc = ifp->if_softc;
623 struct mii_data *mii = GET_MII(sc);
624 u_char *eaddr;
625 int s;
626
627 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
628
629 if (sc->sc_dying)
630 return (EIO);
631
632 s = splnet();
633
634 /* Cancel pending I/O and free all TX/RX buffers */
635 udav_stop(ifp, 1);
636
637 eaddr = LLADDR(ifp->if_sadl);
638 udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
639
640 /* Initialize network control register */
641 /* Disable loopback */
642 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
643
644 /* Initialize RX control register */
645 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
646
647 /* If we want promiscuous mode, accept all physical frames. */
648 if (ifp->if_flags & IFF_PROMISC)
649 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
650 else
651 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
652
653 /* Initialize transmit ring */
654 if (udav_tx_list_init(sc) == ENOBUFS) {
655 printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
656 splx(s);
657 return (EIO);
658 }
659
660 /* Initialize receive ring */
661 if (udav_rx_list_init(sc) == ENOBUFS) {
662 printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
663 splx(s);
664 return (EIO);
665 }
666
667 /* Load the multicast filter */
668 udav_setmulti(sc);
669
670 /* Enable RX */
671 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
672
673 /* clear POWER_DOWN state of internal PHY */
674 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
675 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
676
677 mii_mediachg(mii);
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 splx(s);
690
691 usb_callout(sc->sc_stat_ch, hz, udav_tick, sc);
692
693 return (0);
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 = (struct udav_softc *)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, UDAV_INTR_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 return (EIO);
1054 }
1055
1056 DPRINTF(("%s: %s: send %d bytes\n", USBDEVNAME(sc->sc_dev),
1057 __func__, total_len));
1058
1059 sc->sc_cdata.udav_tx_cnt++;
1060
1061 return (0);
1062 }
1063
1064 Static void
1065 udav_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1066 {
1067 struct udav_chain *c = priv;
1068 struct udav_softc *sc = c->udav_sc;
1069 struct ifnet *ifp = GET_IFP(sc);
1070 int s;
1071
1072 if (sc->sc_dying)
1073 return;
1074
1075 s = splnet();
1076
1077 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1078
1079 ifp->if_timer = 0;
1080 ifp->if_flags &= ~IFF_OACTIVE;
1081
1082 if (status != USBD_NORMAL_COMPLETION) {
1083 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1084 splx(s);
1085 return;
1086 }
1087 ifp->if_oerrors++;
1088 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
1089 usbd_errstr(status));
1090 if (status == USBD_STALLED) {
1091 sc->sc_refcnt++;
1092 usbd_clear_endpoint_stall(sc->sc_pipe_tx);
1093 if (--sc->sc_refcnt < 0)
1094 usb_detach_wakeup(USBDEV(sc->sc_dev));
1095 }
1096 splx(s);
1097 return;
1098 }
1099
1100 ifp->if_opackets++;
1101
1102 m_freem(c->udav_mbuf);
1103 c->udav_mbuf = NULL;
1104
1105 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1106 udav_start(ifp);
1107
1108 splx(s);
1109 }
1110
1111 Static void
1112 udav_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1113 {
1114 struct udav_chain *c = priv;
1115 struct udav_softc *sc = c->udav_sc;
1116 struct ifnet *ifp = GET_IFP(sc);
1117 struct mbuf *m;
1118 u_int32_t total_len;
1119 u_int8_t *pktstat;
1120 int s;
1121
1122 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
1123
1124 if (sc->sc_dying)
1125 return;
1126
1127 if (status != USBD_NORMAL_COMPLETION) {
1128 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1129 return;
1130 sc->sc_rx_errs++;
1131 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1132 printf("%s: %u usb errors on rx: %s\n",
1133 USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
1134 usbd_errstr(status));
1135 sc->sc_rx_errs = 0;
1136 }
1137 if (status == USBD_STALLED) {
1138 sc->sc_refcnt++;
1139 usbd_clear_endpoint_stall(sc->sc_pipe_rx);
1140 if (--sc->sc_refcnt < 0)
1141 usb_detach_wakeup(USBDEV(sc->sc_dev));
1142 }
1143 goto done;
1144 }
1145
1146 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1147
1148 /* copy data to mbuf */
1149 m = c->udav_mbuf;
1150 memcpy(mtod(m, char *), c->udav_buf, total_len);
1151
1152 /* first byte in received data */
1153 pktstat = mtod(m, u_int8_t *);
1154 m_adj(m, sizeof(u_int8_t));
1155 DPRINTF(("%s: RX Status: 0x%02x\n", *pktstat));
1156
1157 total_len = UGETW(mtod(m, u_int8_t *));
1158 m_adj(m, sizeof(u_int16_t));
1159
1160 if (*pktstat & UDAV_RSR_LCS) {
1161 ifp->if_collisions++;
1162 goto done;
1163 }
1164
1165 if (total_len < sizeof(struct ether_header) ||
1166 *pktstat & UDAV_RSR_ERR) {
1167 ifp->if_ierrors++;
1168 goto done;
1169 }
1170
1171 ifp->if_ipackets++;
1172 total_len -= ETHER_CRC_LEN;
1173
1174 m->m_pkthdr.len = m->m_len = total_len;
1175 m->m_pkthdr.rcvif = ifp;
1176
1177 s = splnet();
1178
1179 if (udav_newbuf(sc, c, NULL) == ENOBUFS) {
1180 ifp->if_ierrors++;
1181 goto done1;
1182 }
1183
1184 #if NBPFILTER > 0
1185 if (ifp->if_bpf)
1186 BPF_MTAP(ifp, m);
1187 #endif
1188
1189 DPRINTF(("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
1190 __func__, m->m_len));
1191 IF_INPUT(ifp, m);
1192
1193 done1:
1194 splx(s);
1195
1196 done:
1197 /* Setup new transfer */
1198 usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->udav_buf, UDAV_BUFSZ,
1199 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1200 USBD_NO_TIMEOUT, udav_rxeof);
1201 sc->sc_refcnt++;
1202 usbd_transfer(xfer);
1203 if (--sc->sc_refcnt < 0)
1204 usb_detach_wakeup(USBDEV(sc->sc_dev));
1205
1206 DPRINTF(("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), __func__));
1207 }
1208
1209 #if 0
1210 Static void udav_intr()
1211 {
1212 }
1213 #endif
1214
1215 Static int
1216 udav_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1217 {
1218 struct udav_softc *sc = ifp->if_softc;
1219 struct ifreq *ifr = (struct ifreq *)data;
1220 struct mii_data *mii;
1221 int s, error = 0;
1222
1223 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1224
1225 if (sc->sc_dying)
1226 return (EIO);
1227
1228 s = splnet();
1229
1230 switch (cmd) {
1231 case SIOCGIFMEDIA:
1232 case SIOCSIFMEDIA:
1233 mii = GET_MII(sc);
1234 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1235 break;
1236
1237 default:
1238 error = ether_ioctl(ifp, cmd, data);
1239 if (error == ENETRESET) {
1240 udav_setmulti(sc);
1241 error = 0;
1242 }
1243 break;
1244 }
1245
1246 splx(s);
1247
1248 return (error);
1249 }
1250
1251 Static void
1252 udav_watchdog(struct ifnet *ifp)
1253 {
1254 struct udav_softc *sc = ifp->if_softc;
1255 struct udav_chain *c;
1256 usbd_status stat;
1257 int s;
1258
1259 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1260
1261 ifp->if_oerrors++;
1262 printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
1263
1264 s = splusb();
1265 c = &sc->sc_cdata.udav_tx_chain[0];
1266 usbd_get_xfer_status(c->udav_xfer, NULL, NULL, NULL, &stat);
1267 udav_txeof(c->udav_xfer, c, stat);
1268
1269 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1270 udav_start(ifp);
1271 splx(s);
1272 }
1273
1274 Static void
1275 udav_stop_task(struct udav_softc *sc)
1276 {
1277 udav_stop(GET_IFP(sc), 1);
1278 }
1279
1280 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1281 Static void
1282 udav_stop(struct ifnet *ifp, int disable)
1283 {
1284 struct udav_softc *sc = ifp->if_softc;
1285 usbd_status err;
1286 int i;
1287
1288 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1289
1290 ifp->if_timer = 0;
1291
1292 udav_reset(sc);
1293
1294 usb_uncallout(sc->sc_stat_ch, udav_tick, sc);
1295
1296 /* Stop transfers */
1297 /* RX endpoint */
1298 if (sc->sc_pipe_rx != NULL) {
1299 err = usbd_abort_pipe(sc->sc_pipe_rx);
1300 if (err)
1301 printf("%s: abort rx pipe failed: %s\n",
1302 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1303 err = usbd_close_pipe(sc->sc_pipe_rx);
1304 if (err)
1305 printf("%s: close rx pipe failed: %s\n",
1306 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1307 sc->sc_pipe_rx = NULL;
1308 }
1309
1310 /* TX endpoint */
1311 if (sc->sc_pipe_tx != NULL) {
1312 err = usbd_abort_pipe(sc->sc_pipe_tx);
1313 if (err)
1314 printf("%s: abort tx pipe failed: %s\n",
1315 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1316 err = usbd_close_pipe(sc->sc_pipe_tx);
1317 if (err)
1318 printf("%s: close tx pipe failed: %s\n",
1319 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1320 sc->sc_pipe_tx = NULL;
1321 }
1322
1323 #if 0
1324 /* XXX: Interrupt endpoint is not yet supported!! */
1325 /* Interrupt endpoint */
1326 if (sc->sc_pipe_intr != NULL) {
1327 err = usbd_abort_pipe(sc->sc_pipe_intr);
1328 if (err)
1329 printf("%s: abort intr pipe failed: %s\n",
1330 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1331 err = usbd_close_pipe(sc->sc_pipe_intr);
1332 if (err)
1333 printf("%s: close intr pipe failed: %s\n",
1334 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1335 sc->sc_pipe_intr = NULL;
1336 }
1337 #endif
1338
1339 /* Free RX resources. */
1340 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
1341 if (sc->sc_cdata.udav_rx_chain[i].udav_mbuf != NULL) {
1342 m_freem(sc->sc_cdata.udav_rx_chain[i].udav_mbuf);
1343 sc->sc_cdata.udav_rx_chain[i].udav_mbuf = NULL;
1344 }
1345 if (sc->sc_cdata.udav_rx_chain[i].udav_xfer != NULL) {
1346 usbd_free_xfer(sc->sc_cdata.udav_rx_chain[i].udav_xfer);
1347 sc->sc_cdata.udav_rx_chain[i].udav_xfer = NULL;
1348 }
1349 }
1350
1351 /* Free TX resources. */
1352 for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
1353 if (sc->sc_cdata.udav_tx_chain[i].udav_mbuf != NULL) {
1354 m_freem(sc->sc_cdata.udav_tx_chain[i].udav_mbuf);
1355 sc->sc_cdata.udav_tx_chain[i].udav_mbuf = NULL;
1356 }
1357 if (sc->sc_cdata.udav_tx_chain[i].udav_xfer != NULL) {
1358 usbd_free_xfer(sc->sc_cdata.udav_tx_chain[i].udav_xfer);
1359 sc->sc_cdata.udav_tx_chain[i].udav_xfer = NULL;
1360 }
1361 }
1362
1363 sc->sc_link = 0;
1364 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1365 }
1366
1367 /* Set media options */
1368 Static int
1369 udav_ifmedia_change(struct ifnet *ifp)
1370 {
1371 struct udav_softc *sc = ifp->if_softc;
1372 struct mii_data *mii = GET_MII(sc);
1373
1374 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1375
1376 if (sc->sc_dying)
1377 return (0);
1378
1379 sc->sc_link = 0;
1380 if (mii->mii_instance) {
1381 struct mii_softc *miisc;
1382 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1383 miisc = LIST_NEXT(miisc, mii_list))
1384 mii_phy_reset(miisc);
1385 }
1386
1387 return (mii_mediachg(mii));
1388 }
1389
1390 /* Report current media status. */
1391 Static void
1392 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1393 {
1394 struct udav_softc *sc = ifp->if_softc;
1395 struct mii_data *mii = GET_MII(sc);
1396
1397 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1398
1399 if (sc->sc_dying)
1400 return;
1401
1402 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1403 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1404 ifmr->ifm_status = 0;
1405 return;
1406 }
1407
1408 mii_pollstat(mii);
1409 ifmr->ifm_active = mii->mii_media_active;
1410 ifmr->ifm_status = mii->mii_media_status;
1411 }
1412
1413 Static void
1414 udav_tick(void *xsc)
1415 {
1416 struct udav_softc *sc = xsc;
1417
1418 if (sc == NULL)
1419 return;
1420
1421 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1422 __func__));
1423
1424 if (sc->sc_dying)
1425 return;
1426
1427 /* Perform periodic stuff in process context */
1428 usb_add_task(sc->sc_udev, &sc->sc_tick_task);
1429 }
1430
1431 Static void
1432 udav_tick_task(void *xsc)
1433 {
1434 struct udav_softc *sc = xsc;
1435 struct ifnet *ifp;
1436 struct mii_data *mii;
1437 int s;
1438
1439 if (sc == NULL)
1440 return;
1441
1442 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1443 __func__));
1444
1445 if (sc->sc_dying)
1446 return;
1447
1448 ifp = GET_IFP(sc);
1449 mii = GET_MII(sc);
1450
1451 if (mii == NULL)
1452 return;
1453
1454 s = splnet();
1455
1456 mii_tick(mii);
1457 if (!sc->sc_link) {
1458 mii_pollstat(mii);
1459 if (mii->mii_media_status & IFM_ACTIVE &&
1460 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1461 DPRINTF(("%s: %s: got link\n",
1462 USBDEVNAME(sc->sc_dev), __func__));
1463 sc->sc_link++;
1464 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1465 udav_start(ifp);
1466 }
1467 }
1468
1469 usb_callout(sc->sc_stat_ch, hz, udav_tick, sc);
1470
1471 splx(s);
1472 }
1473
1474 /* Get exclusive access to the MII registers */
1475 Static void
1476 udav_lock_mii(struct udav_softc *sc)
1477 {
1478 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1479 __func__));
1480
1481 sc->sc_refcnt++;
1482 lockmgr(&sc->sc_mii_lock, LK_EXCLUSIVE, NULL);
1483 }
1484
1485 Static void
1486 udav_unlock_mii(struct udav_softc *sc)
1487 {
1488 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1489 __func__));
1490
1491 lockmgr(&sc->sc_mii_lock, LK_RELEASE, NULL);
1492 if (--sc->sc_refcnt < 0)
1493 usb_detach_wakeup(USBDEV(sc->sc_dev));
1494 }
1495
1496 Static int
1497 udav_miibus_readreg(device_ptr_t dev, int phy, int reg)
1498 {
1499 struct udav_softc *sc;
1500 u_int8_t val[2];
1501 u_int16_t data16;
1502
1503 if (dev == NULL)
1504 return (0);
1505
1506 sc = USBGETSOFTC(dev);
1507
1508 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1509 USBDEVNAME(sc->sc_dev), __func__, phy, reg));
1510
1511 if (sc->sc_dying) {
1512 #ifdef DIAGNOSTIC
1513 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1514 __func__);
1515 #endif
1516 return (0);
1517 }
1518
1519 /* XXX: one PHY only for the internal PHY */
1520 if (phy != 0) {
1521 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1522 USBDEVNAME(sc->sc_dev), __func__, phy));
1523 return (0);
1524 }
1525
1526 udav_lock_mii(sc);
1527
1528 /* select internal PHY and set PHY register address */
1529 udav_csr_write1(sc, UDAV_EPAR,
1530 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1531
1532 /* select PHY operation and start read command */
1533 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
1534
1535 /* XXX: should be wait? */
1536
1537 /* end read command */
1538 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
1539
1540 /* retrieve the result from data registers */
1541 udav_csr_read(sc, UDAV_EPDRL, val, 2);
1542
1543 udav_unlock_mii(sc);
1544
1545 data16 = val[0] | (val[1] << 8);
1546
1547 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1548 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data16));
1549
1550 return (data16);
1551 }
1552
1553 Static void
1554 udav_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1555 {
1556 struct udav_softc *sc;
1557 u_int8_t val[2];
1558
1559 if (dev == NULL)
1560 return;
1561
1562 sc = USBGETSOFTC(dev);
1563
1564 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1565 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data));
1566
1567 if (sc->sc_dying) {
1568 #ifdef DIAGNOSTIC
1569 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1570 __func__);
1571 #endif
1572 return;
1573 }
1574
1575 /* XXX: one PHY only for the internal PHY */
1576 if (phy != 0) {
1577 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1578 USBDEVNAME(sc->sc_dev), __func__, phy));
1579 return;
1580 }
1581
1582 udav_lock_mii(sc);
1583
1584 /* select internal PHY and set PHY register address */
1585 udav_csr_write1(sc, UDAV_EPAR,
1586 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1587
1588 /* put the value to the data registers */
1589 val[0] = data & 0xff;
1590 val[1] = (data >> 8) & 0xff;
1591 udav_csr_write(sc, UDAV_EPDRL, val, 2);
1592
1593 /* select PHY operation and start write command */
1594 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
1595
1596 /* XXX: should be wait? */
1597
1598 /* end write command */
1599 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
1600
1601 udav_unlock_mii(sc);
1602
1603 return;
1604 }
1605
1606 Static void
1607 udav_miibus_statchg(device_ptr_t dev)
1608 {
1609 #ifdef UDAV_DEBUG
1610 struct udav_softc *sc;
1611
1612 if (dev == NULL)
1613 return;
1614
1615 sc = USBGETSOFTC(dev);
1616 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1617 #endif
1618 /* Nothing to do */
1619 }
1620