1/*	$NetBSD$	*/
2/*	$nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $	*/
3/*
4 * Copyright (c) 2003
5 *     Shingo WATANABE <nabe@nabechan.org>.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33/*
34 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
35 * The spec can be found at the following url.
36 *   http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-F01-062202s.pdf
37 */
38
39/*
40 * TODO:
41 *	Interrupt Endpoint support
42 *	External PHYs
43 *	powerhook() support?
44 */
45
46#include <sys/cdefs.h>
47__KERNEL_RCSID(0, "$NetBSD$");
48
49#include "opt_inet.h"
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/mutex.h>
54#include <sys/mbuf.h>
55#include <sys/kernel.h>
56#include <sys/socket.h>
57#include <sys/device.h>
58#include <sys/rnd.h>
59
60#include <net/if.h>
61#include <net/if_arp.h>
62#include <net/if_dl.h>
63#include <net/if_media.h>
64
65#include <net/bpf.h>
66
67#include <net/if_ether.h>
68#ifdef INET
69#include <netinet/in.h>
70#include <netinet/if_inarp.h>
71#endif
72
73#include <dev/mii/mii.h>
74#include <dev/mii/miivar.h>
75
76#include <dev/usb/usb.h>
77#include <dev/usb/usbdi.h>
78#include <dev/usb/usbdi_util.h>
79#include <dev/usb/usbdevs.h>
80
81#include <dev/usb/if_udavreg.h>
82
83
84/* Function declarations */
85int             udav_match(device_t, cfdata_t, void *);
86void            udav_attach(device_t, device_t, void *);
87int             udav_detach(device_t, int);
88int             udav_activate(device_t, enum devact);
89extern struct cfdriver udav_cd;
90CFATTACH_DECL_NEW(udav, sizeof(struct udav_softc), udav_match, udav_attach, udav_detach, udav_activate);
91
92Static int udav_openpipes(struct udav_softc *);
93Static int udav_rx_list_init(struct udav_softc *);
94Static int udav_tx_list_init(struct udav_softc *);
95Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *);
96Static void udav_start(struct ifnet *);
97Static int udav_send(struct udav_softc *, struct mbuf *, int);
98Static void udav_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
99Static void udav_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
100Static void udav_tick(void *);
101Static void udav_tick_task(void *);
102Static int udav_ioctl(struct ifnet *, u_long, void *);
103Static void udav_stop_task(struct udav_softc *);
104Static void udav_stop(struct ifnet *, int);
105Static void udav_watchdog(struct ifnet *);
106Static int udav_ifmedia_change(struct ifnet *);
107Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
108Static void udav_lock_mii(struct udav_softc *);
109Static void udav_unlock_mii(struct udav_softc *);
110Static int udav_miibus_readreg(device_t, int, int);
111Static void udav_miibus_writereg(device_t, int, int, int);
112Static void udav_miibus_statchg(device_t);
113Static int udav_init(struct ifnet *);
114Static void udav_setmulti(struct udav_softc *);
115Static void udav_reset(struct udav_softc *);
116
117Static int udav_csr_read(struct udav_softc *, int, void *, int);
118Static int udav_csr_write(struct udav_softc *, int, void *, int);
119Static int udav_csr_read1(struct udav_softc *, int);
120Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
121
122#if 0
123Static int udav_mem_read(struct udav_softc *, int, void *, int);
124Static int udav_mem_write(struct udav_softc *, int, void *, int);
125Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
126#endif
127
128/* Macros */
129#ifdef UDAV_DEBUG
130#define DPRINTF(x)	if (udavdebug) printf x
131#define DPRINTFN(n,x)	if (udavdebug >= (n)) printf x
132int udavdebug = 0;
133#else
134#define DPRINTF(x)
135#define DPRINTFN(n,x)
136#endif
137
138#define	UDAV_SETBIT(sc, reg, x)	\
139	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
140
141#define	UDAV_CLRBIT(sc, reg, x)	\
142	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
143
144static const struct udav_type {
145	struct usb_devno udav_dev;
146	u_int16_t udav_flags;
147#define UDAV_EXT_PHY	0x0001
148} udav_devs [] = {
149	/* Corega USB-TXC */
150	{{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
151	/* ShanTou ST268 USB NIC */
152	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0},
153	/* ShanTou ADM8515 */
154	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515 }, 0},
155	/* SUNRISING SR9600 */
156	{{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_SR9600 }, 0 },
157	/* QUAN DM9601 */
158	{{USB_VENDOR_QUAN, USB_PRODUCT_QUAN_DM9601 }, 0},
159#if 0
160	/* DAVICOM DM9601 Generic? */
161	/*  XXX: The following ids was obtained from the data sheet. */
162	{{ 0x0a46, 0x9601 }, 0},
163#endif
164};
165#define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p))
166
167
168/* Probe */
169int
170udav_match(device_t parent, cfdata_t match, void *aux)
171{
172	struct usb_attach_arg *uaa = aux;
173
174	return (udav_lookup(uaa->vendor, uaa->product) != NULL ?
175		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
176}
177
178/* Attach */
179void
180udav_attach(device_t parent, device_t self, void *aux)
181{
182	struct udav_softc *sc = device_private(self);
183	struct usb_attach_arg *uaa = aux;
184	usbd_device_handle dev = uaa->device;
185	usbd_interface_handle iface;
186	usbd_status err;
187	usb_interface_descriptor_t *id;
188	usb_endpoint_descriptor_t *ed;
189	char *devinfop;
190	struct ifnet *ifp;
191	struct mii_data *mii;
192	u_char eaddr[ETHER_ADDR_LEN];
193	int i, s;
194
195	sc->sc_dev = self;
196
197	aprint_naive("\n");
198	aprint_normal("\n");
199
200	devinfop = usbd_devinfo_alloc(dev, 0);
201	aprint_normal_dev(self, "%s\n", devinfop);
202	usbd_devinfo_free(devinfop);
203
204	/* Move the device into the configured state. */
205	err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */
206	if (err) {
207		aprint_error_dev(self, "setting config no failed\n");
208		goto bad;
209	}
210
211	usb_init_task(&sc->sc_tick_task, udav_tick_task, sc);
212	mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
213	usb_init_task(&sc->sc_stop_task, (void (*)(void *)) udav_stop_task, sc);
214
215	/* get control interface */
216	err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
217	if (err) {
218		aprint_error_dev(self, "failed to get interface, err=%s\n",
219		       usbd_errstr(err));
220		goto bad;
221	}
222
223	sc->sc_udev = dev;
224	sc->sc_ctl_iface = iface;
225	sc->sc_flags = udav_lookup(uaa->vendor, uaa->product)->udav_flags;
226
227	/* get interface descriptor */
228	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
229
230	/* find endpoints */
231	sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
232	for (i = 0; i < id->bNumEndpoints; i++) {
233		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
234		if (ed == NULL) {
235			aprint_error_dev(self, "couldn't get endpoint %d\n", i);
236			goto bad;
237		}
238		if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
239		    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
240			sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
241		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
242			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
243			sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
244		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
245			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
246			sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
247	}
248
249	if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
250	    sc->sc_intrin_no == -1) {
251		aprint_error_dev(self, "missing endpoint\n");
252		goto bad;
253	}
254
255	s = splnet();
256
257	/* reset the adapter */
258	udav_reset(sc);
259
260	/* Get Ethernet Address */
261	err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
262	if (err) {
263		aprint_error_dev(self, "read MAC address failed\n");
264		splx(s);
265		goto bad;
266	}
267
268	/* Print Ethernet Address */
269	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
270
271	/* initialize interface information */
272	ifp = GET_IFP(sc);
273	ifp->if_softc = sc;
274	ifp->if_mtu = ETHERMTU;
275	strncpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
276	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
277	ifp->if_start = udav_start;
278	ifp->if_ioctl = udav_ioctl;
279	ifp->if_watchdog = udav_watchdog;
280	ifp->if_init = udav_init;
281	ifp->if_stop = udav_stop;
282
283	IFQ_SET_READY(&ifp->if_snd);
284
285	/*
286	 * Do ifmedia setup.
287	 */
288	mii = &sc->sc_mii;
289	mii->mii_ifp = ifp;
290	mii->mii_readreg = udav_miibus_readreg;
291	mii->mii_writereg = udav_miibus_writereg;
292	mii->mii_statchg = udav_miibus_statchg;
293	mii->mii_flags = MIIF_AUTOTSLEEP;
294	sc->sc_ec.ec_mii = mii;
295	ifmedia_init(&mii->mii_media, 0,
296		     udav_ifmedia_change, udav_ifmedia_status);
297	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
298	if (LIST_FIRST(&mii->mii_phys) == NULL) {
299		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
300		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
301	} else
302		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
303
304	/* attach the interface */
305	if_attach(ifp);
306	ether_ifattach(ifp, eaddr);
307
308	rnd_attach_source(&sc->rnd_source, device_xname(self),
309	    RND_TYPE_NET, 0);
310
311	callout_init(&sc->sc_stat_ch, 0);
312	sc->sc_attached = 1;
313	splx(s);
314
315	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
316
317	return;
318
319 bad:
320	sc->sc_dying = 1;
321	return;
322}
323
324/* detach */
325int
326udav_detach(device_t self, int flags)
327{
328	struct udav_softc *sc = device_private(self);
329	struct ifnet *ifp = GET_IFP(sc);
330	int s;
331
332	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
333
334	/* Detached before attached finished */
335	if (!sc->sc_attached)
336		return (0);
337
338	callout_stop(&sc->sc_stat_ch);
339
340	/* Remove any pending tasks */
341	usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
342	usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
343
344	s = splusb();
345
346	if (--sc->sc_refcnt >= 0) {
347		/* Wait for processes to go away */
348		usb_detach_wait(sc->sc_dev);
349	}
350	if (ifp->if_flags & IFF_RUNNING)
351		udav_stop(GET_IFP(sc), 1);
352
353	rnd_detach_source(&sc->rnd_source);
354	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
355	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
356	ether_ifdetach(ifp);
357	if_detach(ifp);
358
359#ifdef DIAGNOSTIC
360	if (sc->sc_pipe_tx != NULL)
361		aprint_debug_dev(self, "detach has active tx endpoint.\n");
362	if (sc->sc_pipe_rx != NULL)
363		aprint_debug_dev(self, "detach has active rx endpoint.\n");
364	if (sc->sc_pipe_intr != NULL)
365		aprint_debug_dev(self, "detach has active intr endpoint.\n");
366#endif
367	sc->sc_attached = 0;
368
369	splx(s);
370
371	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
372			   sc->sc_dev);
373
374	mutex_destroy(&sc->sc_mii_lock);
375
376	return (0);
377}
378
379#if 0
380/* read memory */
381Static int
382udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len)
383{
384	usb_device_request_t req;
385	usbd_status err;
386
387	if (sc == NULL)
388		return (0);
389
390	DPRINTFN(0x200,
391		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
392
393	if (sc->sc_dying)
394		return (0);
395
396	offset &= 0xffff;
397	len &= 0xff;
398
399	req.bmRequestType = UT_READ_VENDOR_DEVICE;
400	req.bRequest = UDAV_REQ_MEM_READ;
401	USETW(req.wValue, 0x0000);
402	USETW(req.wIndex, offset);
403	USETW(req.wLength, len);
404
405	sc->sc_refcnt++;
406	err = usbd_do_request(sc->sc_udev, &req, buf);
407	if (--sc->sc_refcnt < 0)
408		usb_detach_wakeup(sc->sc_dev);
409	if (err) {
410		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
411			 device_xname(sc->sc_dev), __func__, offset, err));
412	}
413
414	return (err);
415}
416
417/* write memory */
418Static int
419udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len)
420{
421	usb_device_request_t req;
422	usbd_status err;
423
424	if (sc == NULL)
425		return (0);
426
427	DPRINTFN(0x200,
428		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
429
430	if (sc->sc_dying)
431		return (0);
432
433	offset &= 0xffff;
434	len &= 0xff;
435
436	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
437	req.bRequest = UDAV_REQ_MEM_WRITE;
438	USETW(req.wValue, 0x0000);
439	USETW(req.wIndex, offset);
440	USETW(req.wLength, len);
441
442	sc->sc_refcnt++;
443	err = usbd_do_request(sc->sc_udev, &req, buf);
444	if (--sc->sc_refcnt < 0)
445		usb_detach_wakeup(sc->sc_dev);
446	if (err) {
447		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
448			 device_xname(sc->sc_dev), __func__, offset, err));
449	}
450
451	return (err);
452}
453
454/* write memory */
455Static int
456udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch)
457{
458	usb_device_request_t req;
459	usbd_status err;
460
461	if (sc == NULL)
462		return (0);
463
464	DPRINTFN(0x200,
465		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
466
467	if (sc->sc_dying)
468		return (0);
469
470	offset &= 0xffff;
471
472	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
473	req.bRequest = UDAV_REQ_MEM_WRITE1;
474	USETW(req.wValue, ch);
475	USETW(req.wIndex, offset);
476	USETW(req.wLength, 0x0000);
477
478	sc->sc_refcnt++;
479	err = usbd_do_request(sc->sc_udev, &req, NULL);
480	if (--sc->sc_refcnt < 0)
481		usb_detach_wakeup(sc->sc_dev);
482	if (err) {
483		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
484			 device_xname(sc->sc_dev), __func__, offset, err));
485	}
486
487	return (err);
488}
489#endif
490
491/* read register(s) */
492Static int
493udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len)
494{
495	usb_device_request_t req;
496	usbd_status err;
497
498	if (sc == NULL)
499		return (0);
500
501	DPRINTFN(0x200,
502		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
503
504	if (sc->sc_dying)
505		return (0);
506
507	offset &= 0xff;
508	len &= 0xff;
509
510	req.bmRequestType = UT_READ_VENDOR_DEVICE;
511	req.bRequest = UDAV_REQ_REG_READ;
512	USETW(req.wValue, 0x0000);
513	USETW(req.wIndex, offset);
514	USETW(req.wLength, len);
515
516	sc->sc_refcnt++;
517	err = usbd_do_request(sc->sc_udev, &req, buf);
518	if (--sc->sc_refcnt < 0)
519		usb_detach_wakeup(sc->sc_dev);
520	if (err) {
521		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
522			 device_xname(sc->sc_dev), __func__, offset, err));
523	}
524
525	return (err);
526}
527
528/* write register(s) */
529Static int
530udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len)
531{
532	usb_device_request_t req;
533	usbd_status err;
534
535	if (sc == NULL)
536		return (0);
537
538	DPRINTFN(0x200,
539		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
540
541	if (sc->sc_dying)
542		return (0);
543
544	offset &= 0xff;
545	len &= 0xff;
546
547	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
548	req.bRequest = UDAV_REQ_REG_WRITE;
549	USETW(req.wValue, 0x0000);
550	USETW(req.wIndex, offset);
551	USETW(req.wLength, len);
552
553	sc->sc_refcnt++;
554	err = usbd_do_request(sc->sc_udev, &req, buf);
555	if (--sc->sc_refcnt < 0)
556		usb_detach_wakeup(sc->sc_dev);
557	if (err) {
558		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
559			 device_xname(sc->sc_dev), __func__, offset, err));
560	}
561
562	return (err);
563}
564
565Static int
566udav_csr_read1(struct udav_softc *sc, int offset)
567{
568	u_int8_t val = 0;
569
570	if (sc == NULL)
571		return (0);
572
573	DPRINTFN(0x200,
574		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
575
576	if (sc->sc_dying)
577		return (0);
578
579	return (udav_csr_read(sc, offset, &val, 1) ? 0 : val);
580}
581
582/* write a register */
583Static int
584udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch)
585{
586	usb_device_request_t req;
587	usbd_status err;
588
589	if (sc == NULL)
590		return (0);
591
592	DPRINTFN(0x200,
593		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
594
595	if (sc->sc_dying)
596		return (0);
597
598	offset &= 0xff;
599
600	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
601	req.bRequest = UDAV_REQ_REG_WRITE1;
602	USETW(req.wValue, ch);
603	USETW(req.wIndex, offset);
604	USETW(req.wLength, 0x0000);
605
606	sc->sc_refcnt++;
607	err = usbd_do_request(sc->sc_udev, &req, NULL);
608	if (--sc->sc_refcnt < 0)
609		usb_detach_wakeup(sc->sc_dev);
610	if (err) {
611		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
612			 device_xname(sc->sc_dev), __func__, offset, err));
613	}
614
615	return (err);
616}
617
618Static int
619udav_init(struct ifnet *ifp)
620{
621	struct udav_softc *sc = ifp->if_softc;
622	struct mii_data *mii = GET_MII(sc);
623	uint8_t eaddr[ETHER_ADDR_LEN];
624	int rc, s;
625
626	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
627
628	if (sc->sc_dying)
629		return (EIO);
630
631	s = splnet();
632
633	/* Cancel pending I/O and free all TX/RX buffers */
634	udav_stop(ifp, 1);
635
636	memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
637	udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
638
639	/* Initialize network control register */
640	/*  Disable loopback  */
641	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
642
643	/* Initialize RX control register */
644	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
645
646	/* If we want promiscuous mode, accept all physical frames. */
647	if (ifp->if_flags & IFF_PROMISC)
648		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
649	else
650		UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
651
652	/* Initialize transmit ring */
653	if (udav_tx_list_init(sc) == ENOBUFS) {
654		printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
655		splx(s);
656		return (EIO);
657	}
658
659	/* Initialize receive ring */
660	if (udav_rx_list_init(sc) == ENOBUFS) {
661		printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
662		splx(s);
663		return (EIO);
664	}
665
666	/* Load the multicast filter */
667	udav_setmulti(sc);
668
669	/* Enable RX */
670	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
671
672	/* clear POWER_DOWN state of internal PHY */
673	UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
674	UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
675
676	if ((rc = mii_mediachg(mii)) == ENXIO)
677		rc = 0;
678	else if (rc != 0)
679		goto out;
680
681	if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
682		if (udav_openpipes(sc)) {
683			splx(s);
684			return (EIO);
685		}
686	}
687
688	ifp->if_flags |= IFF_RUNNING;
689	ifp->if_flags &= ~IFF_OACTIVE;
690
691	callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc);
692
693out:
694	splx(s);
695	return rc;
696}
697
698Static void
699udav_reset(struct udav_softc *sc)
700{
701	int i;
702
703	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
704
705	if (sc->sc_dying)
706		return;
707
708	/* Select PHY */
709#if 1
710	/*
711	 * XXX: force select internal phy.
712	 *	external phy routines are not tested.
713	 */
714	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
715#else
716	if (sc->sc_flags & UDAV_EXT_PHY) {
717		UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
718	} else {
719		UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
720	}
721#endif
722
723	UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
724
725	for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
726		if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
727			break;
728		delay(10);	/* XXX */
729	}
730	delay(10000);		/* XXX */
731}
732
733int
734udav_activate(device_t self, enum devact act)
735{
736	struct udav_softc *sc = device_private(self);
737
738	DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev),
739		 __func__, act));
740	switch (act) {
741	case DVACT_DEACTIVATE:
742		if_deactivate(&sc->sc_ec.ec_if);
743		sc->sc_dying = 1;
744		return 0;
745	default:
746		return EOPNOTSUPP;
747	}
748}
749
750#define UDAV_BITS	6
751
752#define UDAV_CALCHASH(addr) \
753	(ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
754
755Static void
756udav_setmulti(struct udav_softc *sc)
757{
758	struct ifnet *ifp;
759	struct ether_multi *enm;
760	struct ether_multistep step;
761	u_int8_t hashes[8];
762	int h = 0;
763
764	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
765
766	if (sc->sc_dying)
767		return;
768
769	ifp = GET_IFP(sc);
770
771	if (ifp->if_flags & IFF_PROMISC) {
772		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
773		return;
774	} else if (ifp->if_flags & IFF_ALLMULTI) {
775	allmulti:
776		ifp->if_flags |= IFF_ALLMULTI;
777		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
778		UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
779		return;
780	}
781
782	/* first, zot all the existing hash bits */
783	memset(hashes, 0x00, sizeof(hashes));
784	hashes[7] |= 0x80;	/* broadcast address */
785	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
786
787	/* now program new ones */
788	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
789	while (enm != NULL) {
790		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
791			   ETHER_ADDR_LEN) != 0)
792			goto allmulti;
793
794		h = UDAV_CALCHASH(enm->enm_addrlo);
795		hashes[h>>3] |= 1 << (h & 0x7);
796		ETHER_NEXT_MULTI(step, enm);
797	}
798
799	/* disable all multicast */
800	ifp->if_flags &= ~IFF_ALLMULTI;
801	UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
802
803	/* write hash value to the register */
804	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
805}
806
807Static int
808udav_openpipes(struct udav_softc *sc)
809{
810	struct udav_chain *c;
811	usbd_status err;
812	int i;
813	int error = 0;
814
815	if (sc->sc_dying)
816		return (EIO);
817
818	sc->sc_refcnt++;
819
820	/* Open RX pipe */
821	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
822			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
823	if (err) {
824		printf("%s: open rx pipe failed: %s\n",
825		       device_xname(sc->sc_dev), usbd_errstr(err));
826		error = EIO;
827		goto done;
828	}
829
830	/* Open TX pipe */
831	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
832			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
833	if (err) {
834		printf("%s: open tx pipe failed: %s\n",
835		       device_xname(sc->sc_dev), usbd_errstr(err));
836		error = EIO;
837		goto done;
838	}
839
840#if 0
841	/* XXX: interrupt endpoint is not yet supported */
842	/* Open Interrupt pipe */
843	err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
844				  USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
845				  &sc->sc_cdata.udav_ibuf, UDAV_INTR_PKGLEN,
846				  udav_intr, USBD_DEFAULT_INTERVAL);
847	if (err) {
848		printf("%s: open intr pipe failed: %s\n",
849		       device_xname(sc->sc_dev), usbd_errstr(err));
850		error = EIO;
851		goto done;
852	}
853#endif
854
855
856	/* Start up the receive pipe. */
857	for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
858		c = &sc->sc_cdata.udav_rx_chain[i];
859		usbd_setup_xfer(c->udav_xfer, sc->sc_pipe_rx,
860				c, c->udav_buf, UDAV_BUFSZ,
861				USBD_SHORT_XFER_OK | USBD_NO_COPY,
862				USBD_NO_TIMEOUT, udav_rxeof);
863		(void)usbd_transfer(c->udav_xfer);
864		DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev),
865			 __func__));
866	}
867
868 done:
869	if (--sc->sc_refcnt < 0)
870		usb_detach_wakeup(sc->sc_dev);
871
872	return (error);
873}
874
875Static int
876udav_newbuf(struct udav_softc *sc, struct udav_chain *c, struct mbuf *m)
877{
878	struct mbuf *m_new = NULL;
879
880	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
881
882	if (m == NULL) {
883		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
884		if (m_new == NULL) {
885			printf("%s: no memory for rx list "
886			       "-- packet dropped!\n", device_xname(sc->sc_dev));
887			return (ENOBUFS);
888		}
889		MCLGET(m_new, M_DONTWAIT);
890		if (!(m_new->m_flags & M_EXT)) {
891			printf("%s: no memory for rx list "
892			       "-- packet dropped!\n", device_xname(sc->sc_dev));
893			m_freem(m_new);
894			return (ENOBUFS);
895		}
896		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
897	} else {
898		m_new = m;
899		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
900		m_new->m_data = m_new->m_ext.ext_buf;
901	}
902
903	m_adj(m_new, ETHER_ALIGN);
904	c->udav_mbuf = m_new;
905
906	return (0);
907}
908
909
910Static int
911udav_rx_list_init(struct udav_softc *sc)
912{
913	struct udav_cdata *cd;
914	struct udav_chain *c;
915	int i;
916
917	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
918
919	cd = &sc->sc_cdata;
920	for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
921		c = &cd->udav_rx_chain[i];
922		c->udav_sc = sc;
923		c->udav_idx = i;
924		if (udav_newbuf(sc, c, NULL) == ENOBUFS)
925			return (ENOBUFS);
926		if (c->udav_xfer == NULL) {
927			c->udav_xfer = usbd_alloc_xfer(sc->sc_udev);
928			if (c->udav_xfer == NULL)
929				return (ENOBUFS);
930			c->udav_buf = usbd_alloc_buffer(c->udav_xfer, UDAV_BUFSZ);
931			if (c->udav_buf == NULL) {
932				usbd_free_xfer(c->udav_xfer);
933				return (ENOBUFS);
934			}
935		}
936	}
937
938	return (0);
939}
940
941Static int
942udav_tx_list_init(struct udav_softc *sc)
943{
944	struct udav_cdata *cd;
945	struct udav_chain *c;
946	int i;
947
948	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
949
950	cd = &sc->sc_cdata;
951	for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
952		c = &cd->udav_tx_chain[i];
953		c->udav_sc = sc;
954		c->udav_idx = i;
955		c->udav_mbuf = NULL;
956		if (c->udav_xfer == NULL) {
957			c->udav_xfer = usbd_alloc_xfer(sc->sc_udev);
958			if (c->udav_xfer == NULL)
959				return (ENOBUFS);
960			c->udav_buf = usbd_alloc_buffer(c->udav_xfer, UDAV_BUFSZ);
961			if (c->udav_buf == NULL) {
962				usbd_free_xfer(c->udav_xfer);
963				return (ENOBUFS);
964			}
965		}
966	}
967
968	return (0);
969}
970
971Static void
972udav_start(struct ifnet *ifp)
973{
974	struct udav_softc *sc = ifp->if_softc;
975	struct mbuf *m_head = NULL;
976
977	DPRINTF(("%s: %s: enter, link=%d\n", device_xname(sc->sc_dev),
978		 __func__, sc->sc_link));
979
980	if (sc->sc_dying)
981		return;
982
983	if (!sc->sc_link)
984		return;
985
986	if (ifp->if_flags & IFF_OACTIVE)
987		return;
988
989	IFQ_POLL(&ifp->if_snd, m_head);
990	if (m_head == NULL)
991		return;
992
993	if (udav_send(sc, m_head, 0)) {
994		ifp->if_flags |= IFF_OACTIVE;
995		return;
996	}
997
998	IFQ_DEQUEUE(&ifp->if_snd, m_head);
999
1000	bpf_mtap(ifp, m_head);
1001
1002	ifp->if_flags |= IFF_OACTIVE;
1003
1004	/* Set a timeout in case the chip goes out to lunch. */
1005	ifp->if_timer = 5;
1006}
1007
1008Static int
1009udav_send(struct udav_softc *sc, struct mbuf *m, int idx)
1010{
1011	int total_len;
1012	struct udav_chain *c;
1013	usbd_status err;
1014
1015	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
1016
1017	c = &sc->sc_cdata.udav_tx_chain[idx];
1018
1019	/* Copy the mbuf data into a contiguous buffer */
1020	/*  first 2 bytes are packet length */
1021	m_copydata(m, 0, m->m_pkthdr.len, c->udav_buf + 2);
1022	c->udav_mbuf = m;
1023	total_len = m->m_pkthdr.len;
1024	if (total_len < UDAV_MIN_FRAME_LEN) {
1025		memset(c->udav_buf + 2 + total_len, 0,
1026		    UDAV_MIN_FRAME_LEN - total_len);
1027		total_len = UDAV_MIN_FRAME_LEN;
1028	}
1029
1030	/* Frame length is specified in the first 2bytes of the buffer */
1031	c->udav_buf[0] = (u_int8_t)total_len;
1032	c->udav_buf[1] = (u_int8_t)(total_len >> 8);
1033	total_len += 2;
1034
1035	usbd_setup_xfer(c->udav_xfer, sc->sc_pipe_tx, c, c->udav_buf, total_len,
1036			USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1037			UDAV_TX_TIMEOUT, udav_txeof);
1038
1039	/* Transmit */
1040	sc->sc_refcnt++;
1041	err = usbd_transfer(c->udav_xfer);
1042	if (--sc->sc_refcnt < 0)
1043		usb_detach_wakeup(sc->sc_dev);
1044	if (err != USBD_IN_PROGRESS) {
1045		printf("%s: udav_send error=%s\n", device_xname(sc->sc_dev),
1046		       usbd_errstr(err));
1047		/* Stop the interface */
1048		usb_add_task(sc->sc_udev, &sc->sc_stop_task,
1049		    USB_TASKQ_DRIVER);
1050		return (EIO);
1051	}
1052
1053	DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev),
1054		 __func__, total_len));
1055
1056	sc->sc_cdata.udav_tx_cnt++;
1057
1058	return (0);
1059}
1060
1061Static void
1062udav_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
1063    usbd_status status)
1064{
1065	struct udav_chain *c = priv;
1066	struct udav_softc *sc = c->udav_sc;
1067	struct ifnet *ifp = GET_IFP(sc);
1068	int s;
1069
1070	if (sc->sc_dying)
1071		return;
1072
1073	s = splnet();
1074
1075	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1076
1077	ifp->if_timer = 0;
1078	ifp->if_flags &= ~IFF_OACTIVE;
1079
1080	if (status != USBD_NORMAL_COMPLETION) {
1081		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1082			splx(s);
1083			return;
1084		}
1085		ifp->if_oerrors++;
1086		printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
1087		       usbd_errstr(status));
1088		if (status == USBD_STALLED) {
1089			sc->sc_refcnt++;
1090			usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
1091			if (--sc->sc_refcnt < 0)
1092				usb_detach_wakeup(sc->sc_dev);
1093		}
1094		splx(s);
1095		return;
1096	}
1097
1098	ifp->if_opackets++;
1099
1100	m_freem(c->udav_mbuf);
1101	c->udav_mbuf = NULL;
1102
1103	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1104		udav_start(ifp);
1105
1106	splx(s);
1107}
1108
1109Static void
1110udav_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1111{
1112	struct udav_chain *c = priv;
1113	struct udav_softc *sc = c->udav_sc;
1114	struct ifnet *ifp = GET_IFP(sc);
1115	struct mbuf *m;
1116	u_int32_t total_len;
1117	u_int8_t *pktstat;
1118	int s;
1119
1120	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
1121
1122	if (sc->sc_dying)
1123		return;
1124
1125	if (status != USBD_NORMAL_COMPLETION) {
1126		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1127			return;
1128		sc->sc_rx_errs++;
1129		if (usbd_ratecheck(&sc->sc_rx_notice)) {
1130			printf("%s: %u usb errors on rx: %s\n",
1131			       device_xname(sc->sc_dev), sc->sc_rx_errs,
1132			       usbd_errstr(status));
1133			sc->sc_rx_errs = 0;
1134		}
1135		if (status == USBD_STALLED) {
1136			sc->sc_refcnt++;
1137			usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
1138			if (--sc->sc_refcnt < 0)
1139				usb_detach_wakeup(sc->sc_dev);
1140		}
1141		goto done;
1142	}
1143
1144	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1145
1146	/* copy data to mbuf */
1147	m = c->udav_mbuf;
1148	memcpy(mtod(m, char *), c->udav_buf, total_len);
1149
1150	/* first byte in received data */
1151	pktstat = mtod(m, u_int8_t *);
1152	m_adj(m, sizeof(u_int8_t));
1153	DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(sc->sc_dev),
1154				*pktstat));
1155
1156	total_len = UGETW(mtod(m, u_int8_t *));
1157	m_adj(m, sizeof(u_int16_t));
1158
1159	if (*pktstat & UDAV_RSR_LCS) {
1160		ifp->if_collisions++;
1161		goto done;
1162	}
1163
1164	if (total_len < sizeof(struct ether_header) ||
1165	    *pktstat & UDAV_RSR_ERR) {
1166		ifp->if_ierrors++;
1167		goto done;
1168	}
1169
1170	ifp->if_ipackets++;
1171	total_len -= ETHER_CRC_LEN;
1172
1173	m->m_pkthdr.len = m->m_len = total_len;
1174	m->m_pkthdr.rcvif = ifp;
1175
1176	s = splnet();
1177
1178	if (udav_newbuf(sc, c, NULL) == ENOBUFS) {
1179		ifp->if_ierrors++;
1180		goto done1;
1181	}
1182
1183	bpf_mtap(ifp, m);
1184
1185	DPRINTF(("%s: %s: deliver %d\n", device_xname(sc->sc_dev),
1186		 __func__, m->m_len));
1187	(*(ifp)->if_input)((ifp), (m));
1188
1189 done1:
1190	splx(s);
1191
1192 done:
1193	/* Setup new transfer */
1194	usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->udav_buf, UDAV_BUFSZ,
1195			USBD_SHORT_XFER_OK | USBD_NO_COPY,
1196			USBD_NO_TIMEOUT, udav_rxeof);
1197	sc->sc_refcnt++;
1198	usbd_transfer(xfer);
1199	if (--sc->sc_refcnt < 0)
1200		usb_detach_wakeup(sc->sc_dev);
1201
1202	DPRINTF(("%s: %s: start rx\n", device_xname(sc->sc_dev), __func__));
1203}
1204
1205#if 0
1206Static void udav_intr(void)
1207{
1208}
1209#endif
1210
1211Static int
1212udav_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1213{
1214	struct udav_softc *sc = ifp->if_softc;
1215	int s, error = 0;
1216
1217	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1218
1219	if (sc->sc_dying)
1220		return (EIO);
1221
1222	s = splnet();
1223
1224	error = ether_ioctl(ifp, cmd, data);
1225	if (error == ENETRESET) {
1226		if (ifp->if_flags & IFF_RUNNING)
1227			udav_setmulti(sc);
1228		error = 0;
1229	}
1230
1231	splx(s);
1232
1233	return (error);
1234}
1235
1236Static void
1237udav_watchdog(struct ifnet *ifp)
1238{
1239	struct udav_softc *sc = ifp->if_softc;
1240	struct udav_chain *c;
1241	usbd_status stat;
1242	int s;
1243
1244	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1245
1246	ifp->if_oerrors++;
1247	printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
1248
1249	s = splusb();
1250	c = &sc->sc_cdata.udav_tx_chain[0];
1251	usbd_get_xfer_status(c->udav_xfer, NULL, NULL, NULL, &stat);
1252	udav_txeof(c->udav_xfer, c, stat);
1253
1254	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1255		udav_start(ifp);
1256	splx(s);
1257}
1258
1259Static void
1260udav_stop_task(struct udav_softc *sc)
1261{
1262	udav_stop(GET_IFP(sc), 1);
1263}
1264
1265/* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1266Static void
1267udav_stop(struct ifnet *ifp, int disable)
1268{
1269	struct udav_softc *sc = ifp->if_softc;
1270	usbd_status err;
1271	int i;
1272
1273	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1274
1275	ifp->if_timer = 0;
1276
1277	udav_reset(sc);
1278
1279	callout_stop(&sc->sc_stat_ch);
1280
1281	/* Stop transfers */
1282	/* RX endpoint */
1283	if (sc->sc_pipe_rx != NULL) {
1284		err = usbd_abort_pipe(sc->sc_pipe_rx);
1285		if (err)
1286			printf("%s: abort rx pipe failed: %s\n",
1287			       device_xname(sc->sc_dev), usbd_errstr(err));
1288		err = usbd_close_pipe(sc->sc_pipe_rx);
1289		if (err)
1290			printf("%s: close rx pipe failed: %s\n",
1291			       device_xname(sc->sc_dev), usbd_errstr(err));
1292		sc->sc_pipe_rx = NULL;
1293	}
1294
1295	/* TX endpoint */
1296	if (sc->sc_pipe_tx != NULL) {
1297		err = usbd_abort_pipe(sc->sc_pipe_tx);
1298		if (err)
1299			printf("%s: abort tx pipe failed: %s\n",
1300			       device_xname(sc->sc_dev), usbd_errstr(err));
1301		err = usbd_close_pipe(sc->sc_pipe_tx);
1302		if (err)
1303			printf("%s: close tx pipe failed: %s\n",
1304			       device_xname(sc->sc_dev), usbd_errstr(err));
1305		sc->sc_pipe_tx = NULL;
1306	}
1307
1308#if 0
1309	/* XXX: Interrupt endpoint is not yet supported!! */
1310	/* Interrupt endpoint */
1311	if (sc->sc_pipe_intr != NULL) {
1312		err = usbd_abort_pipe(sc->sc_pipe_intr);
1313		if (err)
1314			printf("%s: abort intr pipe failed: %s\n",
1315			       device_xname(sc->sc_dev), usbd_errstr(err));
1316		err = usbd_close_pipe(sc->sc_pipe_intr);
1317		if (err)
1318			printf("%s: close intr pipe failed: %s\n",
1319			       device_xname(sc->sc_dev), usbd_errstr(err));
1320		sc->sc_pipe_intr = NULL;
1321	}
1322#endif
1323
1324	/* Free RX resources. */
1325	for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
1326		if (sc->sc_cdata.udav_rx_chain[i].udav_mbuf != NULL) {
1327			m_freem(sc->sc_cdata.udav_rx_chain[i].udav_mbuf);
1328			sc->sc_cdata.udav_rx_chain[i].udav_mbuf = NULL;
1329		}
1330		if (sc->sc_cdata.udav_rx_chain[i].udav_xfer != NULL) {
1331			usbd_free_xfer(sc->sc_cdata.udav_rx_chain[i].udav_xfer);
1332			sc->sc_cdata.udav_rx_chain[i].udav_xfer = NULL;
1333		}
1334	}
1335
1336	/* Free TX resources. */
1337	for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
1338		if (sc->sc_cdata.udav_tx_chain[i].udav_mbuf != NULL) {
1339			m_freem(sc->sc_cdata.udav_tx_chain[i].udav_mbuf);
1340			sc->sc_cdata.udav_tx_chain[i].udav_mbuf = NULL;
1341		}
1342		if (sc->sc_cdata.udav_tx_chain[i].udav_xfer != NULL) {
1343			usbd_free_xfer(sc->sc_cdata.udav_tx_chain[i].udav_xfer);
1344			sc->sc_cdata.udav_tx_chain[i].udav_xfer = NULL;
1345		}
1346	}
1347
1348	sc->sc_link = 0;
1349	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1350}
1351
1352/* Set media options */
1353Static int
1354udav_ifmedia_change(struct ifnet *ifp)
1355{
1356	struct udav_softc *sc = ifp->if_softc;
1357	struct mii_data *mii = GET_MII(sc);
1358	int rc;
1359
1360	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1361
1362	if (sc->sc_dying)
1363		return (0);
1364
1365	sc->sc_link = 0;
1366	if ((rc = mii_mediachg(mii)) == ENXIO)
1367		return 0;
1368	return rc;
1369}
1370
1371/* Report current media status. */
1372Static void
1373udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1374{
1375	struct udav_softc *sc = ifp->if_softc;
1376
1377	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1378
1379	if (sc->sc_dying)
1380		return;
1381
1382	ether_mediastatus(ifp, ifmr);
1383}
1384
1385Static void
1386udav_tick(void *xsc)
1387{
1388	struct udav_softc *sc = xsc;
1389
1390	if (sc == NULL)
1391		return;
1392
1393	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1394			__func__));
1395
1396	if (sc->sc_dying)
1397		return;
1398
1399	/* Perform periodic stuff in process context */
1400	usb_add_task(sc->sc_udev, &sc->sc_tick_task,
1401	    USB_TASKQ_DRIVER);
1402}
1403
1404Static void
1405udav_tick_task(void *xsc)
1406{
1407	struct udav_softc *sc = xsc;
1408	struct ifnet *ifp;
1409	struct mii_data *mii;
1410	int s;
1411
1412	if (sc == NULL)
1413		return;
1414
1415	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1416			__func__));
1417
1418	if (sc->sc_dying)
1419		return;
1420
1421	ifp = GET_IFP(sc);
1422	mii = GET_MII(sc);
1423
1424	if (mii == NULL)
1425		return;
1426
1427	s = splnet();
1428
1429	mii_tick(mii);
1430	if (!sc->sc_link) {
1431		mii_pollstat(mii);
1432		if (mii->mii_media_status & IFM_ACTIVE &&
1433		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1434			DPRINTF(("%s: %s: got link\n",
1435				 device_xname(sc->sc_dev), __func__));
1436			sc->sc_link++;
1437			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1438				   udav_start(ifp);
1439		}
1440	}
1441
1442	callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc);
1443
1444	splx(s);
1445}
1446
1447/* Get exclusive access to the MII registers */
1448Static void
1449udav_lock_mii(struct udav_softc *sc)
1450{
1451	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1452			__func__));
1453
1454	sc->sc_refcnt++;
1455	mutex_enter(&sc->sc_mii_lock);
1456}
1457
1458Static void
1459udav_unlock_mii(struct udav_softc *sc)
1460{
1461	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1462		       __func__));
1463
1464	mutex_exit(&sc->sc_mii_lock);
1465	if (--sc->sc_refcnt < 0)
1466		usb_detach_wakeup(sc->sc_dev);
1467}
1468
1469Static int
1470udav_miibus_readreg(device_t dev, int phy, int reg)
1471{
1472	struct udav_softc *sc;
1473	u_int8_t val[2];
1474	u_int16_t data16;
1475
1476	if (dev == NULL)
1477		return (0);
1478
1479	sc = device_private(dev);
1480
1481	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1482		 device_xname(sc->sc_dev), __func__, phy, reg));
1483
1484	if (sc->sc_dying) {
1485#ifdef DIAGNOSTIC
1486		printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1487		       __func__);
1488#endif
1489		return (0);
1490	}
1491
1492	/* XXX: one PHY only for the internal PHY */
1493	if (phy != 0) {
1494		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1495			 device_xname(sc->sc_dev), __func__, phy));
1496		return (0);
1497	}
1498
1499	udav_lock_mii(sc);
1500
1501	/* select internal PHY and set PHY register address */
1502	udav_csr_write1(sc, UDAV_EPAR,
1503			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1504
1505	/* select PHY operation and start read command */
1506	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
1507
1508	/* XXX: should be wait? */
1509
1510	/* end read command */
1511	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
1512
1513	/* retrieve the result from data registers */
1514	udav_csr_read(sc, UDAV_EPDRL, val, 2);
1515
1516	udav_unlock_mii(sc);
1517
1518	data16 = val[0] | (val[1] << 8);
1519
1520	DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1521		 device_xname(sc->sc_dev), __func__, phy, reg, data16));
1522
1523	return (data16);
1524}
1525
1526Static void
1527udav_miibus_writereg(device_t dev, int phy, int reg, int data)
1528{
1529	struct udav_softc *sc;
1530	u_int8_t val[2];
1531
1532	if (dev == NULL)
1533		return;
1534
1535	sc = device_private(dev);
1536
1537	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1538		 device_xname(sc->sc_dev), __func__, phy, reg, data));
1539
1540	if (sc->sc_dying) {
1541#ifdef DIAGNOSTIC
1542		printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1543		       __func__);
1544#endif
1545		return;
1546	}
1547
1548	/* XXX: one PHY only for the internal PHY */
1549	if (phy != 0) {
1550		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1551			 device_xname(sc->sc_dev), __func__, phy));
1552		return;
1553	}
1554
1555	udav_lock_mii(sc);
1556
1557	/* select internal PHY and set PHY register address */
1558	udav_csr_write1(sc, UDAV_EPAR,
1559			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1560
1561	/* put the value to the data registers */
1562	val[0] = data & 0xff;
1563	val[1] = (data >> 8) & 0xff;
1564	udav_csr_write(sc, UDAV_EPDRL, val, 2);
1565
1566	/* select PHY operation and start write command */
1567	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
1568
1569	/* XXX: should be wait? */
1570
1571	/* end write command */
1572	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
1573
1574	udav_unlock_mii(sc);
1575
1576	return;
1577}
1578
1579Static void
1580udav_miibus_statchg(device_t dev)
1581{
1582#ifdef UDAV_DEBUG
1583	struct udav_softc *sc;
1584
1585	if (dev == NULL)
1586		return;
1587
1588	sc = device_private(dev);
1589	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1590#endif
1591	/* Nothing to do */
1592}
1593