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