if_udav.c revision 185948
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: head/sys/dev/usb2/ethernet/if_udav2.c 185948 2008-12-11 23:13:02Z thompsa $	*/
4/*-
5 * Copyright (c) 2003
6 *     Shingo WATANABE <nabe@nabechan.org>.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 */
33
34/*
35 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
36 * The spec can be found at the following url.
37 *   http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-P01-930914.pdf
38 */
39
40/*
41 * NOTE: all function names beginning like "udav_cfg_" can only
42 * be called from within the config thread function !
43 */
44
45/*
46 * TODO:
47 *	Interrupt Endpoint support
48 *	External PHYs
49 */
50
51#include <sys/cdefs.h>
52__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_udav2.c 185948 2008-12-11 23:13:02Z thompsa $");
53
54#include <dev/usb2/include/usb2_devid.h>
55#include <dev/usb2/include/usb2_standard.h>
56#include <dev/usb2/include/usb2_mfunc.h>
57#include <dev/usb2/include/usb2_error.h>
58
59#define	usb2_config_td_cc usb2_ether_cc
60#define	usb2_config_td_softc udav_softc
61
62#define	USB_DEBUG_VAR udav_debug
63
64#include <dev/usb2/core/usb2_core.h>
65#include <dev/usb2/core/usb2_lookup.h>
66#include <dev/usb2/core/usb2_process.h>
67#include <dev/usb2/core/usb2_config_td.h>
68#include <dev/usb2/core/usb2_debug.h>
69#include <dev/usb2/core/usb2_request.h>
70#include <dev/usb2/core/usb2_busdma.h>
71#include <dev/usb2/core/usb2_util.h>
72
73#include <dev/usb2/ethernet/usb2_ethernet.h>
74#include <dev/usb2/ethernet/if_udav2_reg.h>
75
76/* prototypes */
77
78static device_probe_t udav_probe;
79static device_attach_t udav_attach;
80static device_detach_t udav_detach;
81static device_shutdown_t udav_shutdown;
82
83static usb2_callback_t udav_bulk_write_clear_stall_callback;
84static usb2_callback_t udav_bulk_write_callback;
85static usb2_callback_t udav_bulk_read_clear_stall_callback;
86static usb2_callback_t udav_bulk_read_callback;
87static usb2_callback_t udav_intr_clear_stall_callback;
88static usb2_callback_t udav_intr_callback;
89
90static usb2_config_td_command_t udav_cfg_first_time_setup;
91static usb2_config_td_command_t udav_cfg_pre_init;
92static usb2_config_td_command_t udav_cfg_init;
93static usb2_config_td_command_t udav_config_copy;
94static usb2_config_td_command_t udav_cfg_promisc_upd;
95static usb2_config_td_command_t udav_cfg_pre_stop;
96static usb2_config_td_command_t udav_cfg_stop;
97static usb2_config_td_command_t udav_cfg_ifmedia_change;
98static usb2_config_td_command_t udav_cfg_tick;
99
100static void	udav_cfg_do_request(struct udav_softc *,
101		    struct usb2_device_request *, void *);
102static void	udav_cfg_csr_read(struct udav_softc *, uint16_t, void *,
103		    uint16_t);
104static void	udav_cfg_csr_write(struct udav_softc *, uint16_t, void *,
105		    uint16_t);
106static uint8_t	udav_cfg_csr_read1(struct udav_softc *, uint16_t);
107static void	udav_cfg_csr_write1(struct udav_softc *, uint16_t, uint8_t);
108static void	udav_init_cb(void *);
109static void	udav_cfg_reset(struct udav_softc *);
110static void	udav_start_cb(struct ifnet *);
111static void	udav_start_transfers(struct udav_softc *);
112static int	udav_ioctl_cb(struct ifnet *, u_long, caddr_t);
113static void	udav_watchdog(void *);
114static int	udav_ifmedia_change_cb(struct ifnet *);
115static void	udav_ifmedia_status_cb(struct ifnet *, struct ifmediareq *);
116
117static miibus_readreg_t udav_cfg_miibus_readreg;
118static miibus_writereg_t udav_cfg_miibus_writereg;
119static miibus_statchg_t udav_cfg_miibus_statchg;
120
121static const struct usb2_config udav_config[UDAV_ENDPT_MAX] = {
122
123	[0] = {
124		.type = UE_BULK,
125		.endpoint = UE_ADDR_ANY,
126		.direction = UE_DIR_OUT,
127		.mh.bufsize = (MCLBYTES + 2),
128		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
129		.mh.callback = &udav_bulk_write_callback,
130		.mh.timeout = 10000,	/* 10 seconds */
131	},
132
133	[1] = {
134		.type = UE_BULK,
135		.endpoint = UE_ADDR_ANY,
136		.direction = UE_DIR_IN,
137		.mh.bufsize = (MCLBYTES + 3),
138		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
139		.mh.callback = &udav_bulk_read_callback,
140		.mh.timeout = 0,	/* no timeout */
141	},
142
143	[2] = {
144		.type = UE_CONTROL,
145		.endpoint = 0x00,	/* Control pipe */
146		.direction = UE_DIR_ANY,
147		.mh.bufsize = sizeof(struct usb2_device_request),
148		.mh.flags = {},
149		.mh.callback = &udav_bulk_write_clear_stall_callback,
150		.mh.timeout = 1000,	/* 1 second */
151		.mh.interval = 50,	/* 50ms */
152	},
153
154	[3] = {
155		.type = UE_CONTROL,
156		.endpoint = 0x00,	/* Control pipe */
157		.direction = UE_DIR_ANY,
158		.mh.bufsize = sizeof(struct usb2_device_request),
159		.mh.flags = {},
160		.mh.callback = &udav_bulk_read_clear_stall_callback,
161		.mh.timeout = 1000,	/* 1 second */
162		.mh.interval = 50,	/* 50ms */
163	},
164
165	[4] = {
166		.type = UE_INTERRUPT,
167		.endpoint = UE_ADDR_ANY,
168		.direction = UE_DIR_IN,
169		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
170		.mh.bufsize = 0,	/* use wMaxPacketSize */
171		.mh.callback = &udav_intr_callback,
172	},
173
174	[5] = {
175		.type = UE_CONTROL,
176		.endpoint = 0x00,	/* Control pipe */
177		.direction = UE_DIR_ANY,
178		.mh.bufsize = sizeof(struct usb2_device_request),
179		.mh.flags = {},
180		.mh.callback = &udav_intr_clear_stall_callback,
181		.mh.timeout = 1000,	/* 1 second */
182		.mh.interval = 50,	/* 50ms */
183	},
184};
185
186static device_method_t udav_methods[] = {
187	/* Device interface */
188	DEVMETHOD(device_probe, udav_probe),
189	DEVMETHOD(device_attach, udav_attach),
190	DEVMETHOD(device_detach, udav_detach),
191	DEVMETHOD(device_shutdown, udav_shutdown),
192
193	/* bus interface */
194	DEVMETHOD(bus_print_child, bus_generic_print_child),
195	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
196
197	/* MII interface */
198	DEVMETHOD(miibus_readreg, udav_cfg_miibus_readreg),
199	DEVMETHOD(miibus_writereg, udav_cfg_miibus_writereg),
200	DEVMETHOD(miibus_statchg, udav_cfg_miibus_statchg),
201
202	{0, 0}
203};
204
205static driver_t udav_driver = {
206	.name = "udav",
207	.methods = udav_methods,
208	.size = sizeof(struct udav_softc),
209};
210
211static devclass_t udav_devclass;
212
213DRIVER_MODULE(udav, ushub, udav_driver, udav_devclass, NULL, 0);
214DRIVER_MODULE(miibus, udav, miibus_driver, miibus_devclass, 0, 0);
215MODULE_DEPEND(udav, usb2_ethernet, 1, 1, 1);
216MODULE_DEPEND(udav, usb2_core, 1, 1, 1);
217MODULE_DEPEND(udav, ether, 1, 1, 1);
218MODULE_DEPEND(udav, miibus, 1, 1, 1);
219
220#if USB_DEBUG
221static int udav_debug = 0;
222
223SYSCTL_NODE(_hw_usb2, OID_AUTO, udav, CTLFLAG_RW, 0, "USB udav");
224SYSCTL_INT(_hw_usb2_udav, OID_AUTO, debug, CTLFLAG_RW, &udav_debug, 0,
225    "Debug level");
226#endif
227
228#define	UDAV_CFG_SETBIT(sc, reg, x)	\
229	udav_cfg_csr_write1(sc, reg, udav_cfg_csr_read1(sc, reg) | (x))
230
231#define	UDAV_CFG_CLRBIT(sc, reg, x)	\
232	udav_cfg_csr_write1(sc, reg, udav_cfg_csr_read1(sc, reg) & ~(x))
233
234static const struct usb2_device_id udav_devs[] = {
235	/* ShanTou DM9601 USB NIC */
236	{USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_DM9601, 0)},
237
238	/* ShanTou ST268 USB NIC */
239	{USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268, 0)},
240
241	/* Corega USB-TXC */
242	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC, 0)},
243};
244
245static int
246udav_probe(device_t dev)
247{
248	struct usb2_attach_arg *uaa = device_get_ivars(dev);
249
250	if (uaa->usb2_mode != USB_MODE_HOST) {
251		return (ENXIO);
252	}
253	if (uaa->info.bConfigIndex != UDAV_CONFIG_INDEX) {
254		return (ENXIO);
255	}
256	if (uaa->info.bIfaceIndex != UDAV_IFACE_INDEX) {
257		return (ENXIO);
258	}
259	return (usb2_lookup_id_by_uaa(udav_devs, sizeof(udav_devs), uaa));
260}
261
262static int
263udav_attach(device_t dev)
264{
265	struct usb2_attach_arg *uaa = device_get_ivars(dev);
266	struct udav_softc *sc = device_get_softc(dev);
267	int32_t error;
268	uint8_t iface_index;
269
270	if (sc == NULL) {
271		return (ENOMEM);
272	}
273	sc->sc_udev = uaa->device;
274	sc->sc_dev = dev;
275	sc->sc_unit = device_get_unit(dev);
276	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
277
278	device_set_usb2_desc(dev);
279
280	snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
281	    device_get_nameunit(dev));
282
283	mtx_init(&sc->sc_mtx, "udav lock", NULL, MTX_DEF | MTX_RECURSE);
284
285	usb2_callout_init_mtx(&sc->sc_watchdog,
286	    &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
287
288	iface_index = UDAV_IFACE_INDEX;
289	error = usb2_transfer_setup(uaa->device, &iface_index,
290	    sc->sc_xfer, udav_config, UDAV_ENDPT_MAX, sc, &sc->sc_mtx);
291	if (error) {
292		device_printf(dev, "allocating USB "
293		    "transfers failed!\n");
294		goto detach;
295	}
296	error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx,
297	    NULL, sizeof(struct usb2_config_td_cc), 16);
298	if (error) {
299		device_printf(dev, "could not setup config "
300		    "thread!\n");
301		goto detach;
302	}
303	mtx_lock(&sc->sc_mtx);
304
305	sc->sc_flags |= UDAV_FLAG_WAIT_LINK;
306
307	/* start setup */
308
309	usb2_config_td_queue_command
310	    (&sc->sc_config_td, NULL, &udav_cfg_first_time_setup, 0, 0);
311
312	/* start watchdog (will exit mutex) */
313
314	udav_watchdog(sc);
315
316	return (0);			/* success */
317
318detach:
319	udav_detach(dev);
320	return (ENXIO);			/* failure */
321}
322
323static void
324udav_cfg_first_time_setup(struct udav_softc *sc,
325    struct usb2_config_td_cc *cc, uint16_t refcount)
326{
327	struct ifnet *ifp;
328	int error;
329	uint8_t eaddr[min(ETHER_ADDR_LEN, 6)];
330
331	/* reset the adapter */
332
333	udav_cfg_reset(sc);
334
335	/* get Ethernet Address */
336
337	udav_cfg_csr_read(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
338
339	mtx_unlock(&sc->sc_mtx);
340
341	ifp = if_alloc(IFT_ETHER);
342
343	mtx_lock(&sc->sc_mtx);
344
345	if (ifp == NULL) {
346		printf("%s: could not if_alloc()\n",
347		    sc->sc_name);
348		goto done;
349	}
350	sc->sc_evilhack = ifp;
351
352	ifp->if_softc = sc;
353	if_initname(ifp, "udav", sc->sc_unit);
354	ifp->if_mtu = ETHERMTU;
355	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
356	ifp->if_start = udav_start_cb;
357	ifp->if_ioctl = udav_ioctl_cb;
358	ifp->if_watchdog = NULL;
359	ifp->if_init = udav_init_cb;
360	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
361	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
362	IFQ_SET_READY(&ifp->if_snd);
363
364	/*
365	 * XXX need Giant when accessing the device structures !
366	 */
367
368	mtx_unlock(&sc->sc_mtx);
369
370	mtx_lock(&Giant);
371
372	error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus,
373	    &udav_ifmedia_change_cb,
374	    &udav_ifmedia_status_cb);
375	mtx_unlock(&Giant);
376
377	mtx_lock(&sc->sc_mtx);
378
379	if (error) {
380		printf("%s: MII without any PHY!\n",
381		    sc->sc_name);
382		if_free(ifp);
383		goto done;
384	}
385	sc->sc_ifp = ifp;
386
387	mtx_unlock(&sc->sc_mtx);
388
389	/*
390	 * Call MI attach routine.
391	 */
392
393	ether_ifattach(ifp, eaddr);
394
395	mtx_lock(&sc->sc_mtx);
396
397done:
398	return;
399}
400
401static int
402udav_detach(device_t dev)
403{
404	struct udav_softc *sc = device_get_softc(dev);
405	struct ifnet *ifp;
406
407	usb2_config_td_drain(&sc->sc_config_td);
408
409	mtx_lock(&sc->sc_mtx);
410
411	usb2_callout_stop(&sc->sc_watchdog);
412
413	udav_cfg_pre_stop(sc, NULL, 0);
414
415	ifp = sc->sc_ifp;
416
417	mtx_unlock(&sc->sc_mtx);
418
419	/* stop all USB transfers first */
420	usb2_transfer_unsetup(sc->sc_xfer, UDAV_ENDPT_MAX);
421
422	/* get rid of any late children */
423	bus_generic_detach(dev);
424
425	if (ifp) {
426		ether_ifdetach(ifp);
427		if_free(ifp);
428	}
429	usb2_config_td_unsetup(&sc->sc_config_td);
430
431	usb2_callout_drain(&sc->sc_watchdog);
432
433	mtx_destroy(&sc->sc_mtx);
434
435	return (0);
436}
437
438static void
439udav_cfg_do_request(struct udav_softc *sc, struct usb2_device_request *req,
440    void *data)
441{
442	uint16_t length;
443	usb2_error_t err;
444
445	if (usb2_config_td_is_gone(&sc->sc_config_td)) {
446		goto error;
447	}
448	err = usb2_do_request_flags
449	    (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 1000);
450
451	if (err) {
452
453		DPRINTF("device request failed, err=%s "
454		    "(ignored)\n", usb2_errstr(err));
455
456error:
457		length = UGETW(req->wLength);
458
459		if ((req->bmRequestType & UT_READ) && length) {
460			bzero(data, length);
461		}
462	}
463	return;
464}
465
466#if 0
467static void
468udav_cfg_mem_read(struct udav_softc *sc, uint16_t offset, void *buf,
469    uint16_t len)
470{
471	struct usb2_device_request req;
472
473	len &= 0xff;
474
475	req.bmRequestType = UT_READ_VENDOR_DEVICE;
476	req.bRequest = UDAV_REQ_MEM_READ;
477	USETW(req.wValue, 0x0000);
478	USETW(req.wIndex, offset);
479	USETW(req.wLength, len);
480
481	udav_cfg_do_request(sc, &req, buf);
482	return;
483}
484
485static void
486udav_cfg_mem_write(struct udav_softc *sc, uint16_t offset, void *buf,
487    uint16_t len)
488{
489	struct usb2_device_request req;
490
491	len &= 0xff;
492
493	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
494	req.bRequest = UDAV_REQ_MEM_WRITE;
495	USETW(req.wValue, 0x0000);
496	USETW(req.wIndex, offset);
497	USETW(req.wLength, len);
498
499	udav_cfg_do_request(sc, &req, buf);
500	return;
501}
502
503static void
504udav_cfg_mem_write1(struct udav_softc *sc, uint16_t offset,
505    uint8_t ch)
506{
507	struct usb2_device_request req;
508
509	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
510	req.bRequest = UDAV_REQ_MEM_WRITE1;
511	USETW(req.wValue, ch);
512	USETW(req.wIndex, offset);
513	USETW(req.wLength, 0x0000);
514
515	udav_cfg_do_request(sc, &req, NULL);
516	return;
517}
518
519#endif
520
521static void
522udav_cfg_csr_read(struct udav_softc *sc, uint16_t offset, void *buf,
523    uint16_t len)
524{
525	struct usb2_device_request req;
526
527	len &= 0xff;
528
529	req.bmRequestType = UT_READ_VENDOR_DEVICE;
530	req.bRequest = UDAV_REQ_REG_READ;
531	USETW(req.wValue, 0x0000);
532	USETW(req.wIndex, offset);
533	USETW(req.wLength, len);
534
535	udav_cfg_do_request(sc, &req, buf);
536	return;
537}
538
539static void
540udav_cfg_csr_write(struct udav_softc *sc, uint16_t offset, void *buf,
541    uint16_t len)
542{
543	struct usb2_device_request req;
544
545	offset &= 0xff;
546	len &= 0xff;
547
548	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
549	req.bRequest = UDAV_REQ_REG_WRITE;
550	USETW(req.wValue, 0x0000);
551	USETW(req.wIndex, offset);
552	USETW(req.wLength, len);
553
554	udav_cfg_do_request(sc, &req, buf);
555	return;
556}
557
558static uint8_t
559udav_cfg_csr_read1(struct udav_softc *sc, uint16_t offset)
560{
561	uint8_t val;
562
563	udav_cfg_csr_read(sc, offset, &val, 1);
564	return (val);
565}
566
567static void
568udav_cfg_csr_write1(struct udav_softc *sc, uint16_t offset,
569    uint8_t ch)
570{
571	struct usb2_device_request req;
572
573	offset &= 0xff;
574
575	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
576	req.bRequest = UDAV_REQ_REG_WRITE1;
577	USETW(req.wValue, ch);
578	USETW(req.wIndex, offset);
579	USETW(req.wLength, 0x0000);
580
581	udav_cfg_do_request(sc, &req, NULL);
582	return;
583}
584
585static void
586udav_init_cb(void *arg)
587{
588	struct udav_softc *sc = arg;
589
590	mtx_lock(&sc->sc_mtx);
591	usb2_config_td_queue_command
592	    (&sc->sc_config_td, &udav_cfg_pre_init,
593	    &udav_cfg_init, 0, 0);
594	mtx_unlock(&sc->sc_mtx);
595
596	return;
597}
598
599static void
600udav_cfg_pre_init(struct udav_softc *sc,
601    struct usb2_config_td_cc *cc, uint16_t refcount)
602{
603	struct ifnet *ifp = sc->sc_ifp;
604
605	/* immediate configuration */
606
607	udav_cfg_pre_stop(sc, cc, 0);
608
609	ifp->if_drv_flags |= IFF_DRV_RUNNING;
610
611	sc->sc_flags |= UDAV_FLAG_HL_READY;
612
613	return;
614}
615
616static void
617udav_cfg_init(struct udav_softc *sc,
618    struct usb2_config_td_cc *cc, uint16_t refcount)
619{
620	struct mii_data *mii = GET_MII(sc);
621
622	/*
623	 * Cancel pending I/O
624	 */
625
626	udav_cfg_stop(sc, cc, 0);
627
628	/* set MAC address */
629
630	udav_cfg_csr_write(sc, UDAV_PAR, cc->if_lladdr, ETHER_ADDR_LEN);
631
632	/* initialize network control register */
633
634	/* disable loopback  */
635
636	UDAV_CFG_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
637
638	/* Initialize RX control register */
639	UDAV_CFG_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
640
641	/* load multicast filter and update promiscious mode bit */
642	udav_cfg_promisc_upd(sc, cc, 0);
643
644	/* enable RX */
645	UDAV_CFG_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
646
647	/* clear POWER_DOWN state of internal PHY */
648	UDAV_CFG_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
649	UDAV_CFG_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
650
651	mii_mediachg(mii);
652
653	sc->sc_flags |= (UDAV_FLAG_READ_STALL |
654	    UDAV_FLAG_WRITE_STALL |
655	    UDAV_FLAG_LL_READY);
656
657	udav_start_transfers(sc);
658
659	return;
660}
661
662static void
663udav_cfg_reset(struct udav_softc *sc)
664{
665	usb2_error_t err;
666	uint16_t to;
667
668	/* Select PHY */
669#if 1
670	/*
671	 * XXX: force select internal phy.
672	 *	external phy routines are not tested.
673	 */
674	UDAV_CFG_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
675#else
676	if (sc->sc_flags & UDAV_EXT_PHY) {
677		UDAV_CFG_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
678	} else {
679		UDAV_CFG_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
680	}
681#endif
682
683	UDAV_CFG_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
684
685	for (to = 0;; to++) {
686
687		if (to < 100) {
688
689			err = usb2_config_td_sleep(&sc->sc_config_td, hz / 100);
690
691			if (err) {
692				break;
693			}
694			if (!(udav_cfg_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST)) {
695				break;
696			}
697		} else {
698			printf("%s: reset timeout!\n",
699			    sc->sc_name);
700			break;
701		}
702	}
703
704	err = usb2_config_td_sleep(&sc->sc_config_td, hz / 100);
705
706	return;
707}
708
709#define	UDAV_BITS	6
710
711static void
712udav_mchash(struct usb2_config_td_cc *cc, const uint8_t *ptr)
713{
714	uint8_t h;
715
716	h = ether_crc32_le(ptr, ETHER_ADDR_LEN) &
717	    ((1 << UDAV_BITS) - 1);
718	cc->if_hash[h >> 3] |= 1 << (h & 0x7);
719	return;
720}
721
722static void
723udav_config_copy(struct udav_softc *sc,
724    struct usb2_config_td_cc *cc, uint16_t refcount)
725{
726	bzero(cc, sizeof(*cc));
727	usb2_ether_cc(sc->sc_ifp, &udav_mchash, cc);
728	return;
729}
730
731static void
732udav_cfg_promisc_upd(struct udav_softc *sc,
733    struct usb2_config_td_cc *cc, uint16_t refcount)
734{
735	uint8_t rxmode;
736
737	rxmode = udav_cfg_csr_read1(sc, UDAV_RCR);
738
739	rxmode &= ~(UDAV_RCR_ALL | UDAV_RCR_PRMSC);
740
741	if (cc->if_flags & IFF_PROMISC) {
742
743		rxmode |= UDAV_RCR_ALL | UDAV_RCR_PRMSC;
744
745	} else if (cc->if_flags & IFF_ALLMULTI) {
746
747		rxmode |= UDAV_RCR_ALL;
748	}
749	/* write hash value to the register */
750	udav_cfg_csr_write(sc, UDAV_MAR, cc->if_hash, 8);
751
752	/* write new mode bits */
753	udav_cfg_csr_write1(sc, UDAV_RCR, rxmode);
754
755	return;
756}
757
758static void
759udav_start_cb(struct ifnet *ifp)
760{
761	struct udav_softc *sc = ifp->if_softc;
762
763	mtx_lock(&sc->sc_mtx);
764
765	udav_start_transfers(sc);
766
767	mtx_unlock(&sc->sc_mtx);
768
769	return;
770}
771
772static void
773udav_start_transfers(struct udav_softc *sc)
774{
775	if ((sc->sc_flags & UDAV_FLAG_LL_READY) &&
776	    (sc->sc_flags & UDAV_FLAG_HL_READY)) {
777
778		/*
779		 * start the USB transfers, if not already started:
780		 */
781		usb2_transfer_start(sc->sc_xfer[4]);
782		usb2_transfer_start(sc->sc_xfer[1]);
783		usb2_transfer_start(sc->sc_xfer[0]);
784	}
785	return;
786}
787
788static void
789udav_bulk_write_clear_stall_callback(struct usb2_xfer *xfer)
790{
791	struct udav_softc *sc = xfer->priv_sc;
792	struct usb2_xfer *xfer_other = sc->sc_xfer[0];
793
794	if (usb2_clear_stall_callback(xfer, xfer_other)) {
795		DPRINTF("stall cleared\n");
796		sc->sc_flags &= ~UDAV_FLAG_WRITE_STALL;
797		usb2_transfer_start(xfer_other);
798	}
799	return;
800}
801
802static void
803udav_bulk_write_callback(struct usb2_xfer *xfer)
804{
805	struct udav_softc *sc = xfer->priv_sc;
806	struct ifnet *ifp = sc->sc_ifp;
807	struct mbuf *m;
808	uint32_t extra_len;
809	uint32_t temp_len;
810	uint8_t buf[2];
811
812	switch (USB_GET_STATE(xfer)) {
813	case USB_ST_TRANSFERRED:
814		DPRINTFN(11, "transfer complete\n");
815
816		ifp->if_opackets++;
817
818	case USB_ST_SETUP:
819
820		if (sc->sc_flags & UDAV_FLAG_WRITE_STALL) {
821			usb2_transfer_start(sc->sc_xfer[2]);
822			goto done;
823		}
824		if (sc->sc_flags & UDAV_FLAG_WAIT_LINK) {
825			/*
826			 * don't send anything if there is no link !
827			 */
828			goto done;
829		}
830		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
831
832		if (m == NULL) {
833			goto done;
834		}
835		if (m->m_pkthdr.len > MCLBYTES) {
836			m->m_pkthdr.len = MCLBYTES;
837		}
838		if (m->m_pkthdr.len < UDAV_MIN_FRAME_LEN) {
839			extra_len = UDAV_MIN_FRAME_LEN - m->m_pkthdr.len;
840		} else {
841			extra_len = 0;
842		}
843
844		temp_len = (m->m_pkthdr.len + extra_len);
845
846		/*
847		 * the frame length is specified in the first 2 bytes of the
848		 * buffer
849		 */
850		buf[0] = (uint8_t)(temp_len);
851		buf[1] = (uint8_t)(temp_len >> 8);
852
853		temp_len += 2;
854
855		usb2_copy_in(xfer->frbuffers, 0, buf, 2);
856
857		usb2_m_copy_in(xfer->frbuffers, 2,
858		    m, 0, m->m_pkthdr.len);
859
860		if (extra_len) {
861			usb2_bzero(xfer->frbuffers, temp_len - extra_len,
862			    extra_len);
863		}
864		/*
865		 * if there's a BPF listener, bounce a copy
866		 * of this frame to him:
867		 */
868		BPF_MTAP(ifp, m);
869
870		m_freem(m);
871
872		xfer->frlengths[0] = temp_len;
873		usb2_start_hardware(xfer);
874
875done:
876		return;
877
878	default:			/* Error */
879		DPRINTFN(11, "transfer error, %s\n",
880		    usb2_errstr(xfer->error));
881
882		if (xfer->error != USB_ERR_CANCELLED) {
883			/* try to clear stall first */
884			sc->sc_flags |= UDAV_FLAG_WRITE_STALL;
885			usb2_transfer_start(sc->sc_xfer[2]);
886		}
887		ifp->if_oerrors++;
888		return;
889
890	}
891}
892
893static void
894udav_bulk_read_clear_stall_callback(struct usb2_xfer *xfer)
895{
896	struct udav_softc *sc = xfer->priv_sc;
897	struct usb2_xfer *xfer_other = sc->sc_xfer[1];
898
899	if (usb2_clear_stall_callback(xfer, xfer_other)) {
900		DPRINTF("stall cleared\n");
901		sc->sc_flags &= ~UDAV_FLAG_READ_STALL;
902		usb2_transfer_start(xfer_other);
903	}
904	return;
905}
906
907static void
908udav_bulk_read_callback(struct usb2_xfer *xfer)
909{
910	struct udav_softc *sc = xfer->priv_sc;
911	struct ifnet *ifp = sc->sc_ifp;
912	uint8_t status;
913	uint16_t total_len;
914	struct mbuf *m = NULL;
915
916	switch (USB_GET_STATE(xfer)) {
917	case USB_ST_TRANSFERRED:
918
919		if (xfer->actlen < 1) {
920			ifp->if_ierrors++;
921			goto tr_setup;
922		}
923		xfer->actlen -= 1;
924
925		usb2_copy_out(xfer->frbuffers, 0, &status, 1);
926
927		if (status & UDAV_RSR_LCS) {
928			ifp->if_collisions++;
929			goto tr_setup;
930		}
931		if ((status & UDAV_RSR_ERR) || (xfer->actlen < 2)) {
932			ifp->if_ierrors++;
933			goto tr_setup;
934		}
935		usb2_copy_out(xfer->frbuffers, 1, &total_len, 2);
936
937		total_len = le16toh(total_len);
938
939		xfer->actlen -= 2;
940
941		xfer->actlen = min(xfer->actlen, total_len);
942
943		if (xfer->actlen < (sizeof(struct ether_header) + ETHER_CRC_LEN)) {
944			ifp->if_ierrors++;
945			goto tr_setup;
946		}
947		xfer->actlen -= ETHER_CRC_LEN;
948
949		m = usb2_ether_get_mbuf();
950
951		if (m == NULL) {
952			ifp->if_ierrors++;
953			goto tr_setup;
954		}
955		xfer->actlen = min(xfer->actlen, m->m_len);
956
957		usb2_copy_out(xfer->frbuffers, 3, m->m_data, xfer->actlen);
958
959		ifp->if_ipackets++;
960		m->m_pkthdr.rcvif = ifp;
961		m->m_pkthdr.len = m->m_len = xfer->actlen;
962
963	case USB_ST_SETUP:
964tr_setup:
965
966		if (sc->sc_flags & UDAV_FLAG_READ_STALL) {
967			usb2_transfer_start(sc->sc_xfer[3]);
968		} else {
969			xfer->frlengths[0] = xfer->max_data_length;
970			usb2_start_hardware(xfer);
971		}
972
973		/*
974		 * At the end of a USB callback it is always safe to unlock
975		 * the private mutex of a device! That is why we do the
976		 * "if_input" here, and not some lines up!
977		 */
978		if (m) {
979			mtx_unlock(&sc->sc_mtx);
980			(ifp->if_input) (ifp, m);
981			mtx_lock(&sc->sc_mtx);
982		}
983		return;
984
985	default:			/* Error */
986		if (xfer->error != USB_ERR_CANCELLED) {
987			/* try to clear stall first */
988			sc->sc_flags |= UDAV_FLAG_READ_STALL;
989			usb2_transfer_start(sc->sc_xfer[3]);
990		}
991		DPRINTF("bulk read error, %s\n",
992		    usb2_errstr(xfer->error));
993		return;
994
995	}
996}
997
998static void
999udav_intr_clear_stall_callback(struct usb2_xfer *xfer)
1000{
1001	struct udav_softc *sc = xfer->priv_sc;
1002	struct usb2_xfer *xfer_other = sc->sc_xfer[4];
1003
1004	if (usb2_clear_stall_callback(xfer, xfer_other)) {
1005		DPRINTF("stall cleared\n");
1006		sc->sc_flags &= ~UDAV_FLAG_INTR_STALL;
1007		usb2_transfer_start(xfer_other);
1008	}
1009	return;
1010}
1011
1012static void
1013udav_intr_callback(struct usb2_xfer *xfer)
1014{
1015	struct udav_softc *sc = xfer->priv_sc;
1016
1017	switch (USB_GET_STATE(xfer)) {
1018	case USB_ST_TRANSFERRED:
1019
1020	case USB_ST_SETUP:
1021		if (sc->sc_flags & UDAV_FLAG_INTR_STALL) {
1022			usb2_transfer_start(sc->sc_xfer[5]);
1023		} else {
1024			xfer->frlengths[0] = xfer->max_data_length;
1025			usb2_start_hardware(xfer);
1026		}
1027		return;
1028
1029	default:			/* Error */
1030		if (xfer->error != USB_ERR_CANCELLED) {
1031			/* start clear stall */
1032			sc->sc_flags |= UDAV_FLAG_INTR_STALL;
1033			usb2_transfer_start(sc->sc_xfer[5]);
1034		}
1035		return;
1036	}
1037}
1038
1039static int
1040udav_ioctl_cb(struct ifnet *ifp, u_long cmd, caddr_t data)
1041{
1042	struct udav_softc *sc = ifp->if_softc;
1043	struct mii_data *mii;
1044	int error = 0;
1045
1046	switch (cmd) {
1047	case SIOCSIFFLAGS:
1048		mtx_lock(&sc->sc_mtx);
1049		if (ifp->if_flags & IFF_UP) {
1050			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1051				usb2_config_td_queue_command
1052				    (&sc->sc_config_td, &udav_config_copy,
1053				    &udav_cfg_promisc_upd, 0, 0);
1054			} else {
1055				usb2_config_td_queue_command
1056				    (&sc->sc_config_td, &udav_cfg_pre_init,
1057				    &udav_cfg_init, 0, 0);
1058			}
1059		} else {
1060			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1061				usb2_config_td_queue_command
1062				    (&sc->sc_config_td, &udav_cfg_pre_stop,
1063				    &udav_cfg_stop, 0, 0);
1064			}
1065		}
1066		mtx_unlock(&sc->sc_mtx);
1067		break;
1068
1069	case SIOCADDMULTI:
1070	case SIOCDELMULTI:
1071		mtx_lock(&sc->sc_mtx);
1072		usb2_config_td_queue_command
1073		    (&sc->sc_config_td, &udav_config_copy,
1074		    &udav_cfg_promisc_upd, 0, 0);
1075		mtx_unlock(&sc->sc_mtx);
1076		break;
1077
1078	case SIOCGIFMEDIA:
1079	case SIOCSIFMEDIA:
1080		mii = GET_MII(sc);
1081		if (mii == NULL) {
1082			error = EINVAL;
1083		} else {
1084			error = ifmedia_ioctl
1085			    (ifp, (void *)data, &mii->mii_media, cmd);
1086		}
1087		break;
1088
1089	default:
1090		error = ether_ioctl(ifp, cmd, data);
1091		break;
1092	}
1093	return (error);
1094}
1095
1096static void
1097udav_watchdog(void *arg)
1098{
1099	struct udav_softc *sc = arg;
1100
1101	mtx_assert(&sc->sc_mtx, MA_OWNED);
1102
1103	usb2_config_td_queue_command
1104	    (&sc->sc_config_td, NULL, &udav_cfg_tick, 0, 0);
1105
1106	usb2_callout_reset(&sc->sc_watchdog,
1107	    hz, &udav_watchdog, sc);
1108
1109	mtx_unlock(&sc->sc_mtx);
1110	return;
1111}
1112
1113/*
1114 * NOTE: can be called when "ifp" is NULL
1115 */
1116static void
1117udav_cfg_pre_stop(struct udav_softc *sc,
1118    struct usb2_config_td_cc *cc, uint16_t refcount)
1119{
1120	struct ifnet *ifp = sc->sc_ifp;
1121
1122	if (cc) {
1123		/* copy the needed configuration */
1124		udav_config_copy(sc, cc, refcount);
1125	}
1126	/* immediate configuration */
1127
1128	if (ifp) {
1129		/* clear flags */
1130		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1131	}
1132	sc->sc_flags &= ~(UDAV_FLAG_HL_READY |
1133	    UDAV_FLAG_LL_READY);
1134
1135	sc->sc_flags |= UDAV_FLAG_WAIT_LINK;
1136
1137	/*
1138	 * stop all the transfers, if not already stopped:
1139	 */
1140	usb2_transfer_stop(sc->sc_xfer[0]);
1141	usb2_transfer_stop(sc->sc_xfer[1]);
1142	usb2_transfer_stop(sc->sc_xfer[2]);
1143	usb2_transfer_stop(sc->sc_xfer[3]);
1144	usb2_transfer_stop(sc->sc_xfer[4]);
1145	usb2_transfer_stop(sc->sc_xfer[5]);
1146	return;
1147}
1148
1149/*
1150 * NOTE: can be called when "ifp" is NULL
1151 */
1152static void
1153udav_cfg_stop(struct udav_softc *sc,
1154    struct usb2_config_td_cc *cc, uint16_t refcount)
1155{
1156	udav_cfg_reset(sc);
1157	return;
1158}
1159
1160static int
1161udav_ifmedia_change_cb(struct ifnet *ifp)
1162{
1163	struct udav_softc *sc = ifp->if_softc;
1164
1165	mtx_lock(&sc->sc_mtx);
1166	usb2_config_td_queue_command
1167	    (&sc->sc_config_td, NULL,
1168	    &udav_cfg_ifmedia_change, 0, 0);
1169	mtx_unlock(&sc->sc_mtx);
1170
1171	return (0);
1172}
1173
1174static void
1175udav_cfg_ifmedia_change(struct udav_softc *sc,
1176    struct usb2_config_td_cc *cc, uint16_t refcount)
1177{
1178	struct ifnet *ifp = sc->sc_ifp;
1179	struct mii_data *mii = GET_MII(sc);
1180
1181	if ((ifp == NULL) ||
1182	    (mii == NULL)) {
1183		/* not ready */
1184		return;
1185	}
1186	sc->sc_flags |= UDAV_FLAG_WAIT_LINK;
1187
1188	if (mii->mii_instance) {
1189		struct mii_softc *miisc;
1190
1191		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1192			mii_phy_reset(miisc);
1193		}
1194	}
1195	mii_mediachg(mii);
1196
1197	return;
1198}
1199
1200static void
1201udav_ifmedia_status_cb(struct ifnet *ifp, struct ifmediareq *ifmr)
1202{
1203	struct udav_softc *sc = ifp->if_softc;
1204
1205	mtx_lock(&sc->sc_mtx);
1206
1207	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1208		ifmr->ifm_active = sc->sc_media_active;
1209		ifmr->ifm_status = sc->sc_media_status;
1210	} else {
1211		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1212		ifmr->ifm_status = 0;
1213	}
1214
1215	mtx_unlock(&sc->sc_mtx);
1216
1217	return;
1218}
1219
1220static void
1221udav_cfg_tick(struct udav_softc *sc,
1222    struct usb2_config_td_cc *cc, uint16_t refcount)
1223{
1224	struct ifnet *ifp = sc->sc_ifp;
1225	struct mii_data *mii = GET_MII(sc);
1226
1227	if ((ifp == NULL) ||
1228	    (mii == NULL)) {
1229		/* not ready */
1230		return;
1231	}
1232	mii_tick(mii);
1233
1234	mii_pollstat(mii);
1235
1236	if ((sc->sc_flags & UDAV_FLAG_WAIT_LINK) &&
1237	    (mii->mii_media_status & IFM_ACTIVE) &&
1238	    (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)) {
1239		sc->sc_flags &= ~UDAV_FLAG_WAIT_LINK;
1240	}
1241	sc->sc_media_active = mii->mii_media_active;
1242	sc->sc_media_status = mii->mii_media_status;
1243
1244	/* start stopped transfers, if any */
1245
1246	udav_start_transfers(sc);
1247
1248	return;
1249}
1250
1251static int
1252udav_cfg_miibus_readreg(device_t dev, int phy, int reg)
1253{
1254	struct udav_softc *sc = device_get_softc(dev);
1255	uint16_t data16;
1256	uint8_t val[2];
1257	uint8_t do_unlock;
1258
1259	/* XXX: one PHY only for the internal PHY */
1260	if (phy != 0) {
1261		return (0);
1262	}
1263	/* avoid recursive locking */
1264	if (mtx_owned(&sc->sc_mtx)) {
1265		do_unlock = 0;
1266	} else {
1267		mtx_lock(&sc->sc_mtx);
1268		do_unlock = 1;
1269	}
1270
1271	/* select internal PHY and set PHY register address */
1272	udav_cfg_csr_write1(sc, UDAV_EPAR,
1273	    UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1274
1275	/* select PHY operation and start read command */
1276	udav_cfg_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
1277
1278	/* XXX: should we wait? */
1279
1280	/* end read command */
1281	UDAV_CFG_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
1282
1283	/* retrieve the result from data registers */
1284	udav_cfg_csr_read(sc, UDAV_EPDRL, val, 2);
1285
1286	if (do_unlock) {
1287		mtx_unlock(&sc->sc_mtx);
1288	}
1289	data16 = (val[0] | (val[1] << 8));
1290
1291	DPRINTFN(11, "phy=%d reg=0x%04x => 0x%04x\n",
1292	    phy, reg, data16);
1293
1294	return (data16);
1295}
1296
1297static int
1298udav_cfg_miibus_writereg(device_t dev, int phy, int reg, int data)
1299{
1300	struct udav_softc *sc = device_get_softc(dev);
1301	uint8_t val[2];
1302	uint8_t do_unlock;
1303
1304	/* XXX: one PHY only for the internal PHY */
1305	if (phy != 0) {
1306		return (0);
1307	}
1308	/* avoid recursive locking */
1309	if (mtx_owned(&sc->sc_mtx)) {
1310		do_unlock = 0;
1311	} else {
1312		mtx_lock(&sc->sc_mtx);
1313		do_unlock = 1;
1314	}
1315
1316	/* select internal PHY and set PHY register address */
1317	udav_cfg_csr_write1(sc, UDAV_EPAR,
1318	    UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1319
1320	/* put the value to the data registers */
1321	val[0] = (data & 0xff);
1322	val[1] = (data >> 8) & 0xff;
1323	udav_cfg_csr_write(sc, UDAV_EPDRL, val, 2);
1324
1325	/* select PHY operation and start write command */
1326	udav_cfg_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
1327
1328	/* XXX: should we wait? */
1329
1330	/* end write command */
1331	UDAV_CFG_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
1332
1333	if (do_unlock) {
1334		mtx_unlock(&sc->sc_mtx);
1335	}
1336	return (0);
1337}
1338
1339static void
1340udav_cfg_miibus_statchg(device_t dev)
1341{
1342	/* nothing to do */
1343	return;
1344}
1345
1346/*
1347 * Stop all chip I/O so that the kernel's probe routines don't
1348 * get confused by errant DMAs when rebooting.
1349 */
1350static int
1351udav_shutdown(device_t dev)
1352{
1353	struct udav_softc *sc = device_get_softc(dev);
1354
1355	mtx_lock(&sc->sc_mtx);
1356
1357	usb2_config_td_queue_command
1358	    (&sc->sc_config_td, &udav_cfg_pre_stop,
1359	    &udav_cfg_stop, 0, 0);
1360
1361	mtx_unlock(&sc->sc_mtx);
1362
1363	return (0);
1364}
1365