xref: /freebsd/sys/dev/usb/net/if_udav.c (revision de138ec703c3f7d8ed73ba94068d571b2ee7a87c)
1 /*	$NetBSD: if_udav.c,v 1.2 2003/09/04 15:17:38 tsutsui Exp $	*/
2 /*	$nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $	*/
3 /*	$FreeBSD$	*/
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-P01-930914.pdf
38  */
39 
40 /*
41  * TODO:
42  *	Interrupt Endpoint support
43  *	External PHYs
44  */
45 
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 
49 #include <sys/stdint.h>
50 #include <sys/stddef.h>
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/bus.h>
57 #include <sys/module.h>
58 #include <sys/lock.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/sx.h>
63 #include <sys/unistd.h>
64 #include <sys/callout.h>
65 #include <sys/malloc.h>
66 #include <sys/priv.h>
67 
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include "usbdevs.h"
72 
73 #define	USB_DEBUG_VAR udav_debug
74 #include <dev/usb/usb_debug.h>
75 #include <dev/usb/usb_process.h>
76 
77 #include <dev/usb/net/usb_ethernet.h>
78 #include <dev/usb/net/if_udavreg.h>
79 
80 /* prototypes */
81 
82 static device_probe_t udav_probe;
83 static device_attach_t udav_attach;
84 static device_detach_t udav_detach;
85 
86 static usb_callback_t udav_bulk_write_callback;
87 static usb_callback_t udav_bulk_read_callback;
88 static usb_callback_t udav_intr_callback;
89 
90 static uether_fn_t udav_attach_post;
91 static uether_fn_t udav_init;
92 static uether_fn_t udav_stop;
93 static uether_fn_t udav_start;
94 static uether_fn_t udav_tick;
95 static uether_fn_t udav_setmulti;
96 static uether_fn_t udav_setpromisc;
97 
98 static int	udav_csr_read(struct udav_softc *, uint16_t, void *, int);
99 static int	udav_csr_write(struct udav_softc *, uint16_t, void *, int);
100 static uint8_t	udav_csr_read1(struct udav_softc *, uint16_t);
101 static int	udav_csr_write1(struct udav_softc *, uint16_t, uint8_t);
102 static void	udav_reset(struct udav_softc *);
103 static int	udav_ifmedia_upd(struct ifnet *);
104 static void	udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
105 
106 static miibus_readreg_t udav_miibus_readreg;
107 static miibus_writereg_t udav_miibus_writereg;
108 static miibus_statchg_t udav_miibus_statchg;
109 
110 static const struct usb_config udav_config[UDAV_N_TRANSFER] = {
111 
112 	[UDAV_BULK_DT_WR] = {
113 		.type = UE_BULK,
114 		.endpoint = UE_ADDR_ANY,
115 		.direction = UE_DIR_OUT,
116 		.bufsize = (MCLBYTES + 2),
117 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
118 		.callback = udav_bulk_write_callback,
119 		.timeout = 10000,	/* 10 seconds */
120 	},
121 
122 	[UDAV_BULK_DT_RD] = {
123 		.type = UE_BULK,
124 		.endpoint = UE_ADDR_ANY,
125 		.direction = UE_DIR_IN,
126 		.bufsize = (MCLBYTES + 3),
127 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
128 		.callback = udav_bulk_read_callback,
129 		.timeout = 0,	/* no timeout */
130 	},
131 
132 	[UDAV_INTR_DT_RD] = {
133 		.type = UE_INTERRUPT,
134 		.endpoint = UE_ADDR_ANY,
135 		.direction = UE_DIR_IN,
136 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
137 		.bufsize = 0,	/* use wMaxPacketSize */
138 		.callback = udav_intr_callback,
139 	},
140 };
141 
142 static device_method_t udav_methods[] = {
143 	/* Device interface */
144 	DEVMETHOD(device_probe, udav_probe),
145 	DEVMETHOD(device_attach, udav_attach),
146 	DEVMETHOD(device_detach, udav_detach),
147 
148 	/* bus interface */
149 	DEVMETHOD(bus_print_child, bus_generic_print_child),
150 	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
151 
152 	/* MII interface */
153 	DEVMETHOD(miibus_readreg, udav_miibus_readreg),
154 	DEVMETHOD(miibus_writereg, udav_miibus_writereg),
155 	DEVMETHOD(miibus_statchg, udav_miibus_statchg),
156 
157 	{0, 0}
158 };
159 
160 static driver_t udav_driver = {
161 	.name = "udav",
162 	.methods = udav_methods,
163 	.size = sizeof(struct udav_softc),
164 };
165 
166 static devclass_t udav_devclass;
167 
168 DRIVER_MODULE(udav, uhub, udav_driver, udav_devclass, NULL, 0);
169 DRIVER_MODULE(miibus, udav, miibus_driver, miibus_devclass, 0, 0);
170 MODULE_DEPEND(udav, uether, 1, 1, 1);
171 MODULE_DEPEND(udav, usb, 1, 1, 1);
172 MODULE_DEPEND(udav, ether, 1, 1, 1);
173 MODULE_DEPEND(udav, miibus, 1, 1, 1);
174 MODULE_VERSION(udav, 1);
175 
176 static const struct usb_ether_methods udav_ue_methods = {
177 	.ue_attach_post = udav_attach_post,
178 	.ue_start = udav_start,
179 	.ue_init = udav_init,
180 	.ue_stop = udav_stop,
181 	.ue_tick = udav_tick,
182 	.ue_setmulti = udav_setmulti,
183 	.ue_setpromisc = udav_setpromisc,
184 	.ue_mii_upd = udav_ifmedia_upd,
185 	.ue_mii_sts = udav_ifmedia_status,
186 };
187 
188 #ifdef USB_DEBUG
189 static int udav_debug = 0;
190 
191 SYSCTL_NODE(_hw_usb, OID_AUTO, udav, CTLFLAG_RW, 0, "USB udav");
192 SYSCTL_INT(_hw_usb_udav, OID_AUTO, debug, CTLFLAG_RW, &udav_debug, 0,
193     "Debug level");
194 #endif
195 
196 #define	UDAV_SETBIT(sc, reg, x)	\
197 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
198 
199 #define	UDAV_CLRBIT(sc, reg, x)	\
200 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
201 
202 static const STRUCT_USB_HOST_ID udav_devs[] = {
203 	/* ShanTou DM9601 USB NIC */
204 	{USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_DM9601, 0)},
205 	/* ShanTou ST268 USB NIC */
206 	{USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268, 0)},
207 	/* Corega USB-TXC */
208 	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC, 0)},
209 	/* ShanTou AMD8515 USB NIC */
210 	{USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515, 0)},
211 	/* Kontron AG USB Ethernet */
212 	{USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_DM9601, 0)},
213 	{USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_JP1082, 0)},
214 };
215 
216 static void
217 udav_attach_post(struct usb_ether *ue)
218 {
219 	struct udav_softc *sc = uether_getsc(ue);
220 
221 	/* reset the adapter */
222 	udav_reset(sc);
223 
224 	/* Get Ethernet Address */
225 	udav_csr_read(sc, UDAV_PAR, ue->ue_eaddr, ETHER_ADDR_LEN);
226 }
227 
228 static int
229 udav_probe(device_t dev)
230 {
231 	struct usb_attach_arg *uaa = device_get_ivars(dev);
232 
233 	if (uaa->usb_mode != USB_MODE_HOST)
234 		return (ENXIO);
235 	if (uaa->info.bConfigIndex != UDAV_CONFIG_INDEX)
236 		return (ENXIO);
237 	if (uaa->info.bIfaceIndex != UDAV_IFACE_INDEX)
238 		return (ENXIO);
239 
240 	return (usbd_lookup_id_by_uaa(udav_devs, sizeof(udav_devs), uaa));
241 }
242 
243 static int
244 udav_attach(device_t dev)
245 {
246 	struct usb_attach_arg *uaa = device_get_ivars(dev);
247 	struct udav_softc *sc = device_get_softc(dev);
248 	struct usb_ether *ue = &sc->sc_ue;
249 	uint8_t iface_index;
250 	int error;
251 
252 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
253 
254 	device_set_usb_desc(dev);
255 
256 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
257 
258 	iface_index = UDAV_IFACE_INDEX;
259 	error = usbd_transfer_setup(uaa->device, &iface_index,
260 	    sc->sc_xfer, udav_config, UDAV_N_TRANSFER, sc, &sc->sc_mtx);
261 	if (error) {
262 		device_printf(dev, "allocating USB transfers failed\n");
263 		goto detach;
264 	}
265 
266 	ue->ue_sc = sc;
267 	ue->ue_dev = dev;
268 	ue->ue_udev = uaa->device;
269 	ue->ue_mtx = &sc->sc_mtx;
270 	ue->ue_methods = &udav_ue_methods;
271 
272 	error = uether_ifattach(ue);
273 	if (error) {
274 		device_printf(dev, "could not attach interface\n");
275 		goto detach;
276 	}
277 
278 	return (0);			/* success */
279 
280 detach:
281 	udav_detach(dev);
282 	return (ENXIO);			/* failure */
283 }
284 
285 static int
286 udav_detach(device_t dev)
287 {
288 	struct udav_softc *sc = device_get_softc(dev);
289 	struct usb_ether *ue = &sc->sc_ue;
290 
291 	usbd_transfer_unsetup(sc->sc_xfer, UDAV_N_TRANSFER);
292 	uether_ifdetach(ue);
293 	mtx_destroy(&sc->sc_mtx);
294 
295 	return (0);
296 }
297 
298 #if 0
299 static int
300 udav_mem_read(struct udav_softc *sc, uint16_t offset, void *buf,
301     int len)
302 {
303 	struct usb_device_request req;
304 
305 	len &= 0xff;
306 
307 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
308 	req.bRequest = UDAV_REQ_MEM_READ;
309 	USETW(req.wValue, 0x0000);
310 	USETW(req.wIndex, offset);
311 	USETW(req.wLength, len);
312 
313 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
314 }
315 
316 static int
317 udav_mem_write(struct udav_softc *sc, uint16_t offset, void *buf,
318     int len)
319 {
320 	struct usb_device_request req;
321 
322 	len &= 0xff;
323 
324 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
325 	req.bRequest = UDAV_REQ_MEM_WRITE;
326 	USETW(req.wValue, 0x0000);
327 	USETW(req.wIndex, offset);
328 	USETW(req.wLength, len);
329 
330 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
331 }
332 
333 static int
334 udav_mem_write1(struct udav_softc *sc, uint16_t offset,
335     uint8_t ch)
336 {
337 	struct usb_device_request req;
338 
339 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
340 	req.bRequest = UDAV_REQ_MEM_WRITE1;
341 	USETW(req.wValue, ch);
342 	USETW(req.wIndex, offset);
343 	USETW(req.wLength, 0x0000);
344 
345 	return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
346 }
347 #endif
348 
349 static int
350 udav_csr_read(struct udav_softc *sc, uint16_t offset, void *buf, int len)
351 {
352 	struct usb_device_request req;
353 
354 	len &= 0xff;
355 
356 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
357 	req.bRequest = UDAV_REQ_REG_READ;
358 	USETW(req.wValue, 0x0000);
359 	USETW(req.wIndex, offset);
360 	USETW(req.wLength, len);
361 
362 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
363 }
364 
365 static int
366 udav_csr_write(struct udav_softc *sc, uint16_t offset, void *buf, int len)
367 {
368 	struct usb_device_request req;
369 
370 	offset &= 0xff;
371 	len &= 0xff;
372 
373 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
374 	req.bRequest = UDAV_REQ_REG_WRITE;
375 	USETW(req.wValue, 0x0000);
376 	USETW(req.wIndex, offset);
377 	USETW(req.wLength, len);
378 
379 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
380 }
381 
382 static uint8_t
383 udav_csr_read1(struct udav_softc *sc, uint16_t offset)
384 {
385 	uint8_t val;
386 
387 	udav_csr_read(sc, offset, &val, 1);
388 	return (val);
389 }
390 
391 static int
392 udav_csr_write1(struct udav_softc *sc, uint16_t offset,
393     uint8_t ch)
394 {
395 	struct usb_device_request req;
396 
397 	offset &= 0xff;
398 
399 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
400 	req.bRequest = UDAV_REQ_REG_WRITE1;
401 	USETW(req.wValue, ch);
402 	USETW(req.wIndex, offset);
403 	USETW(req.wLength, 0x0000);
404 
405 	return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
406 }
407 
408 static void
409 udav_init(struct usb_ether *ue)
410 {
411 	struct udav_softc *sc = ue->ue_sc;
412 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
413 
414 	UDAV_LOCK_ASSERT(sc, MA_OWNED);
415 
416 	/*
417 	 * Cancel pending I/O
418 	 */
419 	udav_stop(ue);
420 
421 	/* set MAC address */
422 	udav_csr_write(sc, UDAV_PAR, IF_LLADDR(ifp), ETHER_ADDR_LEN);
423 
424 	/* initialize network control register */
425 
426 	/* disable loopback  */
427 	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
428 
429 	/* Initialize RX control register */
430 	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
431 
432 	/* load multicast filter and update promiscious mode bit */
433 	udav_setpromisc(ue);
434 
435 	/* enable RX */
436 	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
437 
438 	/* clear POWER_DOWN state of internal PHY */
439 	UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
440 	UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
441 
442 	usbd_xfer_set_stall(sc->sc_xfer[UDAV_BULK_DT_WR]);
443 
444 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
445 	udav_start(ue);
446 }
447 
448 static void
449 udav_reset(struct udav_softc *sc)
450 {
451 	int i;
452 
453 	/* Select PHY */
454 #if 1
455 	/*
456 	 * XXX: force select internal phy.
457 	 *	external phy routines are not tested.
458 	 */
459 	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
460 #else
461 	if (sc->sc_flags & UDAV_EXT_PHY)
462 		UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
463 	else
464 		UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
465 #endif
466 
467 	UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
468 
469 	for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
470 		if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
471 			break;
472 		if (uether_pause(&sc->sc_ue, hz / 100))
473 			break;
474 	}
475 
476 	uether_pause(&sc->sc_ue, hz / 100);
477 }
478 
479 #define	UDAV_BITS	6
480 static void
481 udav_setmulti(struct usb_ether *ue)
482 {
483 	struct udav_softc *sc = ue->ue_sc;
484 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
485 	struct ifmultiaddr *ifma;
486 	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
487 	int h = 0;
488 
489 	UDAV_LOCK_ASSERT(sc, MA_OWNED);
490 
491 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
492 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
493 		return;
494 	}
495 
496 	/* first, zot all the existing hash bits */
497 	memset(hashtbl, 0x00, sizeof(hashtbl));
498 	hashtbl[7] |= 0x80;	/* broadcast address */
499 	udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl));
500 
501 	/* now program new ones */
502 	if_maddr_rlock(ifp);
503 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
504 	{
505 		if (ifma->ifma_addr->sa_family != AF_LINK)
506 			continue;
507 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
508 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
509 		hashtbl[h / 8] |= 1 << (h % 8);
510 	}
511 	if_maddr_runlock(ifp);
512 
513 	/* disable all multicast */
514 	UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
515 
516 	/* write hash value to the register */
517 	udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl));
518 }
519 
520 static void
521 udav_setpromisc(struct usb_ether *ue)
522 {
523 	struct udav_softc *sc = ue->ue_sc;
524 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
525 	uint8_t rxmode;
526 
527 	rxmode = udav_csr_read1(sc, UDAV_RCR);
528 	rxmode &= ~(UDAV_RCR_ALL | UDAV_RCR_PRMSC);
529 
530 	if (ifp->if_flags & IFF_PROMISC)
531 		rxmode |= UDAV_RCR_ALL | UDAV_RCR_PRMSC;
532 	else if (ifp->if_flags & IFF_ALLMULTI)
533 		rxmode |= UDAV_RCR_ALL;
534 
535 	/* write new mode bits */
536 	udav_csr_write1(sc, UDAV_RCR, rxmode);
537 }
538 
539 static void
540 udav_start(struct usb_ether *ue)
541 {
542 	struct udav_softc *sc = ue->ue_sc;
543 
544 	/*
545 	 * start the USB transfers, if not already started:
546 	 */
547 	usbd_transfer_start(sc->sc_xfer[UDAV_INTR_DT_RD]);
548 	usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_RD]);
549 	usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_WR]);
550 }
551 
552 static void
553 udav_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
554 {
555 	struct udav_softc *sc = usbd_xfer_softc(xfer);
556 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
557 	struct usb_page_cache *pc;
558 	struct mbuf *m;
559 	int extra_len;
560 	int temp_len;
561 	uint8_t buf[2];
562 
563 	switch (USB_GET_STATE(xfer)) {
564 	case USB_ST_TRANSFERRED:
565 		DPRINTFN(11, "transfer complete\n");
566 		ifp->if_opackets++;
567 
568 		/* FALLTHROUGH */
569 	case USB_ST_SETUP:
570 tr_setup:
571 		if ((sc->sc_flags & UDAV_FLAG_LINK) == 0) {
572 			/*
573 			 * don't send anything if there is no link !
574 			 */
575 			return;
576 		}
577 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
578 
579 		if (m == NULL)
580 			return;
581 		if (m->m_pkthdr.len > MCLBYTES)
582 			m->m_pkthdr.len = MCLBYTES;
583 		if (m->m_pkthdr.len < UDAV_MIN_FRAME_LEN) {
584 			extra_len = UDAV_MIN_FRAME_LEN - m->m_pkthdr.len;
585 		} else {
586 			extra_len = 0;
587 		}
588 
589 		temp_len = (m->m_pkthdr.len + extra_len);
590 
591 		/*
592 		 * the frame length is specified in the first 2 bytes of the
593 		 * buffer
594 		 */
595 		buf[0] = (uint8_t)(temp_len);
596 		buf[1] = (uint8_t)(temp_len >> 8);
597 
598 		temp_len += 2;
599 
600 		pc = usbd_xfer_get_frame(xfer, 0);
601 		usbd_copy_in(pc, 0, buf, 2);
602 		usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
603 
604 		if (extra_len)
605 			usbd_frame_zero(pc, temp_len - extra_len, extra_len);
606 		/*
607 		 * if there's a BPF listener, bounce a copy
608 		 * of this frame to him:
609 		 */
610 		BPF_MTAP(ifp, m);
611 
612 		m_freem(m);
613 
614 		usbd_xfer_set_frame_len(xfer, 0, temp_len);
615 		usbd_transfer_submit(xfer);
616 		return;
617 
618 	default:			/* Error */
619 		DPRINTFN(11, "transfer error, %s\n",
620 		    usbd_errstr(error));
621 
622 		ifp->if_oerrors++;
623 
624 		if (error != USB_ERR_CANCELLED) {
625 			/* try to clear stall first */
626 			usbd_xfer_set_stall(xfer);
627 			goto tr_setup;
628 		}
629 		return;
630 	}
631 }
632 
633 static void
634 udav_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
635 {
636 	struct udav_softc *sc = usbd_xfer_softc(xfer);
637 	struct usb_ether *ue = &sc->sc_ue;
638 	struct ifnet *ifp = uether_getifp(ue);
639 	struct usb_page_cache *pc;
640 	struct udav_rxpkt stat;
641 	int len;
642 	int actlen;
643 
644 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
645 
646 	switch (USB_GET_STATE(xfer)) {
647 	case USB_ST_TRANSFERRED:
648 
649 		if (actlen < sizeof(stat) + ETHER_CRC_LEN) {
650 			ifp->if_ierrors++;
651 			goto tr_setup;
652 		}
653 		pc = usbd_xfer_get_frame(xfer, 0);
654 		usbd_copy_out(pc, 0, &stat, sizeof(stat));
655 		actlen -= sizeof(stat);
656 		len = min(actlen, le16toh(stat.pktlen));
657 		len -= ETHER_CRC_LEN;
658 
659 		if (stat.rxstat & UDAV_RSR_LCS) {
660 			ifp->if_collisions++;
661 			goto tr_setup;
662 		}
663 		if (stat.rxstat & UDAV_RSR_ERR) {
664 			ifp->if_ierrors++;
665 			goto tr_setup;
666 		}
667 		uether_rxbuf(ue, pc, sizeof(stat), len);
668 		/* FALLTHROUGH */
669 	case USB_ST_SETUP:
670 tr_setup:
671 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
672 		usbd_transfer_submit(xfer);
673 		uether_rxflush(ue);
674 		return;
675 
676 	default:			/* Error */
677 		DPRINTF("bulk read error, %s\n",
678 		    usbd_errstr(error));
679 
680 		if (error != USB_ERR_CANCELLED) {
681 			/* try to clear stall first */
682 			usbd_xfer_set_stall(xfer);
683 			goto tr_setup;
684 		}
685 		return;
686 	}
687 }
688 
689 static void
690 udav_intr_callback(struct usb_xfer *xfer, usb_error_t error)
691 {
692 	switch (USB_GET_STATE(xfer)) {
693 	case USB_ST_TRANSFERRED:
694 	case USB_ST_SETUP:
695 tr_setup:
696 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
697 		usbd_transfer_submit(xfer);
698 		return;
699 
700 	default:			/* Error */
701 		if (error != USB_ERR_CANCELLED) {
702 			/* try to clear stall first */
703 			usbd_xfer_set_stall(xfer);
704 			goto tr_setup;
705 		}
706 		return;
707 	}
708 }
709 
710 static void
711 udav_stop(struct usb_ether *ue)
712 {
713 	struct udav_softc *sc = ue->ue_sc;
714 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
715 
716 	UDAV_LOCK_ASSERT(sc, MA_OWNED);
717 
718 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
719 	sc->sc_flags &= ~UDAV_FLAG_LINK;
720 
721 	/*
722 	 * stop all the transfers, if not already stopped:
723 	 */
724 	usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_WR]);
725 	usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_RD]);
726 	usbd_transfer_stop(sc->sc_xfer[UDAV_INTR_DT_RD]);
727 
728 	udav_reset(sc);
729 }
730 
731 static int
732 udav_ifmedia_upd(struct ifnet *ifp)
733 {
734 	struct udav_softc *sc = ifp->if_softc;
735 	struct mii_data *mii = GET_MII(sc);
736 	struct mii_softc *miisc;
737 
738 	UDAV_LOCK_ASSERT(sc, MA_OWNED);
739 
740         sc->sc_flags &= ~UDAV_FLAG_LINK;
741 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
742 		PHY_RESET(miisc);
743 	mii_mediachg(mii);
744 	return (0);
745 }
746 
747 static void
748 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
749 {
750 	struct udav_softc *sc = ifp->if_softc;
751 	struct mii_data *mii = GET_MII(sc);
752 
753 	UDAV_LOCK(sc);
754 	mii_pollstat(mii);
755 	UDAV_UNLOCK(sc);
756 	ifmr->ifm_active = mii->mii_media_active;
757 	ifmr->ifm_status = mii->mii_media_status;
758 }
759 
760 static void
761 udav_tick(struct usb_ether *ue)
762 {
763 	struct udav_softc *sc = ue->ue_sc;
764 	struct mii_data *mii = GET_MII(sc);
765 
766 	UDAV_LOCK_ASSERT(sc, MA_OWNED);
767 
768 	mii_tick(mii);
769 	if ((sc->sc_flags & UDAV_FLAG_LINK) == 0
770 	    && mii->mii_media_status & IFM_ACTIVE &&
771 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
772 		sc->sc_flags |= UDAV_FLAG_LINK;
773 		udav_start(ue);
774 	}
775 }
776 
777 static int
778 udav_miibus_readreg(device_t dev, int phy, int reg)
779 {
780 	struct udav_softc *sc = device_get_softc(dev);
781 	uint16_t data16;
782 	uint8_t val[2];
783 	int locked;
784 
785 	/* XXX: one PHY only for the internal PHY */
786 	if (phy != 0)
787 		return (0);
788 
789 	locked = mtx_owned(&sc->sc_mtx);
790 	if (!locked)
791 		UDAV_LOCK(sc);
792 
793 	/* select internal PHY and set PHY register address */
794 	udav_csr_write1(sc, UDAV_EPAR,
795 	    UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
796 
797 	/* select PHY operation and start read command */
798 	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
799 
800 	/* XXX: should we wait? */
801 
802 	/* end read command */
803 	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
804 
805 	/* retrieve the result from data registers */
806 	udav_csr_read(sc, UDAV_EPDRL, val, 2);
807 
808 	data16 = (val[0] | (val[1] << 8));
809 
810 	DPRINTFN(11, "phy=%d reg=0x%04x => 0x%04x\n",
811 	    phy, reg, data16);
812 
813 	if (!locked)
814 		UDAV_UNLOCK(sc);
815 	return (data16);
816 }
817 
818 static int
819 udav_miibus_writereg(device_t dev, int phy, int reg, int data)
820 {
821 	struct udav_softc *sc = device_get_softc(dev);
822 	uint8_t val[2];
823 	int locked;
824 
825 	/* XXX: one PHY only for the internal PHY */
826 	if (phy != 0)
827 		return (0);
828 
829 	locked = mtx_owned(&sc->sc_mtx);
830 	if (!locked)
831 		UDAV_LOCK(sc);
832 
833 	/* select internal PHY and set PHY register address */
834 	udav_csr_write1(sc, UDAV_EPAR,
835 	    UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
836 
837 	/* put the value to the data registers */
838 	val[0] = (data & 0xff);
839 	val[1] = (data >> 8) & 0xff;
840 	udav_csr_write(sc, UDAV_EPDRL, val, 2);
841 
842 	/* select PHY operation and start write command */
843 	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
844 
845 	/* XXX: should we wait? */
846 
847 	/* end write command */
848 	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
849 
850 	if (!locked)
851 		UDAV_UNLOCK(sc);
852 	return (0);
853 }
854 
855 static void
856 udav_miibus_statchg(device_t dev)
857 {
858 	/* nothing to do */
859 }
860