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