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