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