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