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