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