if_cue.c revision 185948
1/*-
2 * Copyright (c) 1997, 1998, 1999, 2000
3 *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. 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 Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_cue2.c 185948 2008-12-11 23:13:02Z thompsa $");
35
36/*
37 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38 * adapters and others.
39 *
40 * Written by Bill Paul <wpaul@ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45/*
46 * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
47 * RX filter uses a 512-bit multicast hash table, single perfect entry
48 * for the station address, and promiscuous mode. Unlike the ADMtek
49 * and KLSI chips, the CATC ASIC supports read and write combining
50 * mode where multiple packets can be transfered using a single bulk
51 * transaction, which helps performance a great deal.
52 */
53
54/*
55 * NOTE: all function names beginning like "cue_cfg_" can only
56 * be called from within the config thread function !
57 */
58
59#include <dev/usb2/include/usb2_devid.h>
60#include <dev/usb2/include/usb2_standard.h>
61#include <dev/usb2/include/usb2_mfunc.h>
62#include <dev/usb2/include/usb2_error.h>
63
64#define	usb2_config_td_cc usb2_ether_cc
65#define	usb2_config_td_softc cue_softc
66
67#define	USB_DEBUG_VAR cue_debug
68
69#include <dev/usb2/core/usb2_core.h>
70#include <dev/usb2/core/usb2_lookup.h>
71#include <dev/usb2/core/usb2_process.h>
72#include <dev/usb2/core/usb2_config_td.h>
73#include <dev/usb2/core/usb2_debug.h>
74#include <dev/usb2/core/usb2_request.h>
75#include <dev/usb2/core/usb2_busdma.h>
76#include <dev/usb2/core/usb2_util.h>
77
78#include <dev/usb2/ethernet/usb2_ethernet.h>
79#include <dev/usb2/ethernet/if_cue2_reg.h>
80
81/*
82 * Various supported device vendors/products.
83 */
84
85/* Belkin F5U111 adapter covered by NETMATE entry */
86
87static const struct usb2_device_id cue_devs[] = {
88	{USB_VPI(USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE, 0)},
89	{USB_VPI(USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2, 0)},
90	{USB_VPI(USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK, 0)},
91};
92
93/* prototypes */
94
95static device_probe_t cue_probe;
96static device_attach_t cue_attach;
97static device_detach_t cue_detach;
98static device_shutdown_t cue_shutdown;
99
100static usb2_callback_t cue_bulk_read_clear_stall_callback;
101static usb2_callback_t cue_bulk_read_callback;
102static usb2_callback_t cue_bulk_write_clear_stall_callback;
103static usb2_callback_t cue_bulk_write_callback;
104
105static usb2_config_td_command_t cue_cfg_promisc_upd;
106static usb2_config_td_command_t cue_config_copy;
107static usb2_config_td_command_t cue_cfg_first_time_setup;
108static usb2_config_td_command_t cue_cfg_tick;
109static usb2_config_td_command_t cue_cfg_pre_init;
110static usb2_config_td_command_t cue_cfg_init;
111static usb2_config_td_command_t cue_cfg_pre_stop;
112static usb2_config_td_command_t cue_cfg_stop;
113
114static void	cue_cfg_do_request(struct cue_softc *,
115		    struct usb2_device_request *, void *);
116static uint8_t	cue_cfg_csr_read_1(struct cue_softc *, uint16_t);
117static uint16_t	cue_cfg_csr_read_2(struct cue_softc *, uint8_t);
118static void	cue_cfg_csr_write_1(struct cue_softc *, uint16_t, uint16_t);
119static void	cue_cfg_mem(struct cue_softc *, uint8_t, uint16_t, void *,
120		    uint16_t);
121static void	cue_cfg_getmac(struct cue_softc *, void *);
122static void	cue_mchash(struct usb2_config_td_cc *, const uint8_t *);
123static void	cue_cfg_reset(struct cue_softc *);
124static void	cue_start_cb(struct ifnet *);
125static void	cue_start_transfers(struct cue_softc *);
126static void	cue_init_cb(void *);
127static int	cue_ioctl_cb(struct ifnet *, u_long, caddr_t);
128static void	cue_watchdog(void *);
129
130#if USB_DEBUG
131static int cue_debug = 0;
132
133SYSCTL_NODE(_hw_usb2, OID_AUTO, cue, CTLFLAG_RW, 0, "USB cue");
134SYSCTL_INT(_hw_usb2_cue, OID_AUTO, debug, CTLFLAG_RW, &cue_debug, 0,
135    "Debug level");
136#endif
137
138static const struct usb2_config cue_config[CUE_ENDPT_MAX] = {
139
140	[0] = {
141		.type = UE_BULK,
142		.endpoint = UE_ADDR_ANY,
143		.direction = UE_DIR_OUT,
144		.mh.bufsize = (MCLBYTES + 2),
145		.mh.flags = {.pipe_bof = 1,},
146		.mh.callback = &cue_bulk_write_callback,
147		.mh.timeout = 10000,	/* 10 seconds */
148	},
149
150	[1] = {
151		.type = UE_BULK,
152		.endpoint = UE_ADDR_ANY,
153		.direction = UE_DIR_IN,
154		.mh.bufsize = (MCLBYTES + 2),
155		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
156		.mh.callback = &cue_bulk_read_callback,
157	},
158
159	[2] = {
160		.type = UE_CONTROL,
161		.endpoint = 0x00,	/* Control pipe */
162		.direction = UE_DIR_ANY,
163		.mh.bufsize = sizeof(struct usb2_device_request),
164		.mh.flags = {},
165		.mh.callback = &cue_bulk_write_clear_stall_callback,
166		.mh.timeout = 1000,	/* 1 second */
167		.mh.interval = 50,	/* 50ms */
168	},
169
170	[3] = {
171		.type = UE_CONTROL,
172		.endpoint = 0x00,	/* Control pipe */
173		.direction = UE_DIR_ANY,
174		.mh.bufsize = sizeof(struct usb2_device_request),
175		.mh.flags = {},
176		.mh.callback = &cue_bulk_read_clear_stall_callback,
177		.mh.timeout = 1000,	/* 1 second */
178		.mh.interval = 50,	/* 50ms */
179	},
180};
181
182static device_method_t cue_methods[] = {
183	/* Device interface */
184	DEVMETHOD(device_probe, cue_probe),
185	DEVMETHOD(device_attach, cue_attach),
186	DEVMETHOD(device_detach, cue_detach),
187	DEVMETHOD(device_shutdown, cue_shutdown),
188
189	{0, 0}
190};
191
192static driver_t cue_driver = {
193	.name = "cue",
194	.methods = cue_methods,
195	.size = sizeof(struct cue_softc),
196};
197
198static devclass_t cue_devclass;
199
200DRIVER_MODULE(cue, ushub, cue_driver, cue_devclass, NULL, 0);
201MODULE_DEPEND(cue, usb2_ethernet, 1, 1, 1);
202MODULE_DEPEND(cue, usb2_core, 1, 1, 1);
203MODULE_DEPEND(cue, ether, 1, 1, 1);
204
205static void
206cue_cfg_do_request(struct cue_softc *sc, struct usb2_device_request *req,
207    void *data)
208{
209	uint16_t length;
210	usb2_error_t err;
211
212	if (usb2_config_td_is_gone(&sc->sc_config_td)) {
213		goto error;
214	}
215	err = usb2_do_request_flags
216	    (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 1000);
217
218	if (err) {
219
220		DPRINTF("device request failed, err=%s "
221		    "(ignored)\n", usb2_errstr(err));
222
223error:
224		length = UGETW(req->wLength);
225
226		if ((req->bmRequestType & UT_READ) && length) {
227			bzero(data, length);
228		}
229	}
230	return;
231}
232
233#define	CUE_CFG_SETBIT(sc, reg, x)				\
234	cue_cfg_csr_write_1(sc, reg, cue_cfg_csr_read_1(sc, reg) | (x))
235
236#define	CUE_CFG_CLRBIT(sc, reg, x)				\
237	cue_cfg_csr_write_1(sc, reg, cue_cfg_csr_read_1(sc, reg) & ~(x))
238
239static uint8_t
240cue_cfg_csr_read_1(struct cue_softc *sc, uint16_t reg)
241{
242	struct usb2_device_request req;
243	uint8_t val;
244
245	req.bmRequestType = UT_READ_VENDOR_DEVICE;
246	req.bRequest = CUE_CMD_READREG;
247	USETW(req.wValue, 0);
248	USETW(req.wIndex, reg);
249	USETW(req.wLength, 1);
250
251	cue_cfg_do_request(sc, &req, &val);
252	return (val);
253}
254
255static uint16_t
256cue_cfg_csr_read_2(struct cue_softc *sc, uint8_t reg)
257{
258	struct usb2_device_request req;
259	uint16_t val;
260
261	req.bmRequestType = UT_READ_VENDOR_DEVICE;
262	req.bRequest = CUE_CMD_READREG;
263	USETW(req.wValue, 0);
264	USETW(req.wIndex, reg);
265	USETW(req.wLength, 2);
266
267	cue_cfg_do_request(sc, &req, &val);
268	return (le16toh(val));
269}
270
271static void
272cue_cfg_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val)
273{
274	struct usb2_device_request req;
275
276	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
277	req.bRequest = CUE_CMD_WRITEREG;
278	USETW(req.wValue, val);
279	USETW(req.wIndex, reg);
280	USETW(req.wLength, 0);
281
282	cue_cfg_do_request(sc, &req, NULL);
283	return;
284}
285
286static void
287cue_cfg_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr,
288    void *buf, uint16_t len)
289{
290	struct usb2_device_request req;
291
292	if (cmd == CUE_CMD_READSRAM) {
293		req.bmRequestType = UT_READ_VENDOR_DEVICE;
294	} else {
295		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
296	}
297	req.bRequest = cmd;
298	USETW(req.wValue, 0);
299	USETW(req.wIndex, addr);
300	USETW(req.wLength, len);
301
302	cue_cfg_do_request(sc, &req, buf);
303	return;
304}
305
306static void
307cue_cfg_getmac(struct cue_softc *sc, void *buf)
308{
309	struct usb2_device_request req;
310
311	req.bmRequestType = UT_READ_VENDOR_DEVICE;
312	req.bRequest = CUE_CMD_GET_MACADDR;
313	USETW(req.wValue, 0);
314	USETW(req.wIndex, 0);
315	USETW(req.wLength, ETHER_ADDR_LEN);
316
317	cue_cfg_do_request(sc, &req, buf);
318	return;
319}
320
321#define	CUE_BITS 9
322
323static void
324cue_mchash(struct usb2_config_td_cc *cc, const uint8_t *addr)
325{
326	uint16_t h;
327
328	h = ether_crc32_le(addr, ETHER_ADDR_LEN) &
329	    ((1 << CUE_BITS) - 1);
330	cc->if_hash[h >> 3] |= 1 << (h & 0x7);
331	return;
332}
333
334static void
335cue_cfg_promisc_upd(struct cue_softc *sc,
336    struct usb2_config_td_cc *cc, uint16_t refcount)
337{
338	/* if we want promiscuous mode, set the allframes bit */
339
340	if (cc->if_flags & IFF_PROMISC) {
341		CUE_CFG_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
342	} else {
343		CUE_CFG_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
344	}
345
346	/* write multicast hash-bits */
347
348	cue_cfg_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
349	    cc->if_hash, CUE_MCAST_TABLE_LEN);
350	return;
351}
352
353static void
354cue_config_copy(struct cue_softc *sc,
355    struct usb2_config_td_cc *cc, uint16_t refcount)
356{
357	bzero(cc, sizeof(*cc));
358	usb2_ether_cc(sc->sc_ifp, &cue_mchash, cc);
359	return;
360}
361
362static void
363cue_cfg_reset(struct cue_softc *sc)
364{
365	struct usb2_device_request req;
366
367	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
368	req.bRequest = CUE_CMD_RESET;
369	USETW(req.wValue, 0);
370	USETW(req.wIndex, 0);
371	USETW(req.wLength, 0);
372
373	cue_cfg_do_request(sc, &req, NULL);
374
375	/*
376	 * wait a little while for the chip to get its brains in order:
377	 */
378
379	(void)usb2_config_td_sleep(&sc->sc_config_td, hz / 100);
380	return;
381}
382
383static int
384cue_probe(device_t dev)
385{
386	struct usb2_attach_arg *uaa = device_get_ivars(dev);
387
388	if (uaa->usb2_mode != USB_MODE_HOST) {
389		return (ENXIO);
390	}
391	if (uaa->info.bConfigIndex != CUE_CONFIG_IDX) {
392		return (ENXIO);
393	}
394	if (uaa->info.bIfaceIndex != CUE_IFACE_IDX) {
395		return (ENXIO);
396	}
397	return (usb2_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa));
398}
399
400static int
401cue_attach(device_t dev)
402{
403	struct usb2_attach_arg *uaa = device_get_ivars(dev);
404	struct cue_softc *sc = device_get_softc(dev);
405	uint8_t iface_index;
406	int32_t error;
407
408	if (sc == NULL) {
409		return (ENOMEM);
410	}
411	sc->sc_udev = uaa->device;
412	sc->sc_dev = dev;
413	sc->sc_unit = device_get_unit(dev);
414
415	device_set_usb2_desc(dev);
416
417	mtx_init(&sc->sc_mtx, "cue lock", NULL, MTX_DEF | MTX_RECURSE);
418
419	usb2_callout_init_mtx(&sc->sc_watchdog,
420	    &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
421
422	iface_index = CUE_IFACE_IDX;
423	error = usb2_transfer_setup(uaa->device, &iface_index,
424	    sc->sc_xfer, cue_config, CUE_ENDPT_MAX, sc, &sc->sc_mtx);
425	if (error) {
426		device_printf(dev, "allocating USB "
427		    "transfers failed!\n");
428		goto detach;
429	}
430	error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx,
431	    NULL, sizeof(struct usb2_config_td_cc), 16);
432	if (error) {
433		device_printf(dev, "could not setup config "
434		    "thread!\n");
435		goto detach;
436	}
437	mtx_lock(&sc->sc_mtx);
438
439	/* start setup */
440
441	usb2_config_td_queue_command
442	    (&sc->sc_config_td, NULL, &cue_cfg_first_time_setup, 0, 0);
443
444	/* start watchdog (will exit mutex) */
445
446	cue_watchdog(sc);
447
448	return (0);			/* success */
449
450detach:
451	cue_detach(dev);
452	return (ENXIO);			/* failure */
453}
454
455static void
456cue_cfg_first_time_setup(struct cue_softc *sc,
457    struct usb2_config_td_cc *cc, uint16_t refcount)
458{
459	uint8_t eaddr[ETHER_ADDR_LEN];
460	struct ifnet *ifp;
461
462#if 0
463	/* Reset the adapter. */
464	cue_cfg_reset(sc);
465#endif
466	/*
467	 * Get station address.
468	 */
469	cue_cfg_getmac(sc, eaddr);
470
471	mtx_unlock(&sc->sc_mtx);
472
473	ifp = if_alloc(IFT_ETHER);
474
475	mtx_lock(&sc->sc_mtx);
476
477	if (ifp == NULL) {
478		printf("cue%d: could not if_alloc()\n",
479		    sc->sc_unit);
480		goto done;
481	}
482	sc->sc_evilhack = ifp;
483
484	ifp->if_softc = sc;
485	if_initname(ifp, "cue", sc->sc_unit);
486	ifp->if_mtu = ETHERMTU;
487	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
488	ifp->if_ioctl = cue_ioctl_cb;
489	ifp->if_start = cue_start_cb;
490	ifp->if_watchdog = NULL;
491	ifp->if_init = cue_init_cb;
492	ifp->if_baudrate = 10000000;
493	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
494	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
495	IFQ_SET_READY(&ifp->if_snd);
496
497	sc->sc_ifp = ifp;
498
499	mtx_unlock(&sc->sc_mtx);
500
501	ether_ifattach(ifp, eaddr);
502
503	mtx_lock(&sc->sc_mtx);
504
505done:
506	return;
507}
508
509static int
510cue_detach(device_t dev)
511{
512	struct cue_softc *sc = device_get_softc(dev);
513	struct ifnet *ifp;
514
515	usb2_config_td_drain(&sc->sc_config_td);
516
517	mtx_lock(&sc->sc_mtx);
518
519	usb2_callout_stop(&sc->sc_watchdog);
520
521	cue_cfg_pre_stop(sc, NULL, 0);
522
523	ifp = sc->sc_ifp;
524
525	mtx_unlock(&sc->sc_mtx);
526
527	/* stop all USB transfers first */
528	usb2_transfer_unsetup(sc->sc_xfer, CUE_ENDPT_MAX);
529
530	/* get rid of any late children */
531	bus_generic_detach(dev);
532
533	if (ifp) {
534		ether_ifdetach(ifp);
535		if_free(ifp);
536	}
537	usb2_config_td_unsetup(&sc->sc_config_td);
538
539	usb2_callout_drain(&sc->sc_watchdog);
540
541	mtx_destroy(&sc->sc_mtx);
542
543	return (0);
544}
545
546static void
547cue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer)
548{
549	struct cue_softc *sc = xfer->priv_sc;
550	struct usb2_xfer *xfer_other = sc->sc_xfer[1];
551
552	if (usb2_clear_stall_callback(xfer, xfer_other)) {
553		DPRINTF("stall cleared\n");
554		sc->sc_flags &= ~CUE_FLAG_READ_STALL;
555		usb2_transfer_start(xfer_other);
556	}
557	return;
558}
559
560static void
561cue_bulk_read_callback(struct usb2_xfer *xfer)
562{
563	struct cue_softc *sc = xfer->priv_sc;
564	struct ifnet *ifp = sc->sc_ifp;
565	struct mbuf *m = NULL;
566	uint8_t buf[2];
567	uint16_t len;
568
569	switch (USB_GET_STATE(xfer)) {
570	case USB_ST_TRANSFERRED:
571
572		if (xfer->actlen <= (2 + sizeof(struct ether_header))) {
573			ifp->if_ierrors++;
574			goto tr_setup;
575		}
576		usb2_copy_out(xfer->frbuffers, 0, buf, 2);
577
578		len = buf[0] | (buf[1] << 8);
579
580		xfer->actlen -= 2;
581
582		m = usb2_ether_get_mbuf();
583
584		if (m == NULL) {
585			ifp->if_ierrors++;
586			goto tr_setup;
587		}
588		xfer->actlen = min(xfer->actlen, m->m_len);
589		xfer->actlen = min(xfer->actlen, len);
590
591		usb2_copy_out(xfer->frbuffers, 2, m->m_data, xfer->actlen);
592
593		ifp->if_ipackets++;
594		m->m_pkthdr.rcvif = ifp;
595		m->m_pkthdr.len = m->m_len = xfer->actlen;
596
597	case USB_ST_SETUP:
598tr_setup:
599
600		if (sc->sc_flags & CUE_FLAG_READ_STALL) {
601			usb2_transfer_start(sc->sc_xfer[3]);
602		} else {
603			xfer->frlengths[0] = xfer->max_data_length;
604			usb2_start_hardware(xfer);
605		}
606
607		/*
608		 * At the end of a USB callback it is always safe to unlock
609		 * the private mutex of a device! That is why we do the
610		 * "if_input" here, and not some lines up!
611		 */
612		if (m) {
613			mtx_unlock(&sc->sc_mtx);
614			(ifp->if_input) (ifp, m);
615			mtx_lock(&sc->sc_mtx);
616		}
617		return;
618
619	default:			/* Error */
620		if (xfer->error != USB_ERR_CANCELLED) {
621			/* try to clear stall first */
622			sc->sc_flags |= CUE_FLAG_READ_STALL;
623			usb2_transfer_start(sc->sc_xfer[3]);
624		}
625		DPRINTF("bulk read error, %s\n",
626		    usb2_errstr(xfer->error));
627		return;
628
629	}
630}
631
632static void
633cue_cfg_tick(struct cue_softc *sc,
634    struct usb2_config_td_cc *cc, uint16_t refcount)
635{
636	struct ifnet *ifp = sc->sc_ifp;
637
638	if ((ifp == NULL)) {
639		/* not ready */
640		return;
641	}
642	ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_SINGLECOLL);
643	ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_MULTICOLL);
644	ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_EXCESSCOLL);
645
646	if (cue_cfg_csr_read_2(sc, CUE_RX_FRAMEERR)) {
647		ifp->if_ierrors++;
648	}
649	/* start stopped transfers, if any */
650
651	cue_start_transfers(sc);
652
653	return;
654}
655
656static void
657cue_start_cb(struct ifnet *ifp)
658{
659	struct cue_softc *sc = ifp->if_softc;
660
661	mtx_lock(&sc->sc_mtx);
662
663	cue_start_transfers(sc);
664
665	mtx_unlock(&sc->sc_mtx);
666
667	return;
668}
669
670static void
671cue_start_transfers(struct cue_softc *sc)
672{
673	if ((sc->sc_flags & CUE_FLAG_LL_READY) &&
674	    (sc->sc_flags & CUE_FLAG_HL_READY)) {
675
676		/*
677		 * start the USB transfers, if not already started:
678		 */
679		usb2_transfer_start(sc->sc_xfer[1]);
680		usb2_transfer_start(sc->sc_xfer[0]);
681	}
682	return;
683}
684
685static void
686cue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer)
687{
688	struct cue_softc *sc = xfer->priv_sc;
689	struct usb2_xfer *xfer_other = sc->sc_xfer[0];
690
691	if (usb2_clear_stall_callback(xfer, xfer_other)) {
692		DPRINTF("stall cleared\n");
693		sc->sc_flags &= ~CUE_FLAG_WRITE_STALL;
694		usb2_transfer_start(xfer_other);
695	}
696	return;
697}
698
699static void
700cue_bulk_write_callback(struct usb2_xfer *xfer)
701{
702	struct cue_softc *sc = xfer->priv_sc;
703	struct ifnet *ifp = sc->sc_ifp;
704	struct mbuf *m;
705	uint8_t buf[2];
706
707	switch (USB_GET_STATE(xfer)) {
708	case USB_ST_TRANSFERRED:
709		DPRINTFN(11, "transfer complete\n");
710
711		ifp->if_opackets++;
712
713	case USB_ST_SETUP:
714
715		if (sc->sc_flags & CUE_FLAG_WRITE_STALL) {
716			usb2_transfer_start(sc->sc_xfer[2]);
717			goto done;
718		}
719		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
720
721		if (m == NULL) {
722			goto done;
723		}
724		if (m->m_pkthdr.len > MCLBYTES) {
725			m->m_pkthdr.len = MCLBYTES;
726		}
727		xfer->frlengths[0] = (m->m_pkthdr.len + 2);
728
729		/* the first two bytes are the frame length */
730
731		buf[0] = (uint8_t)(m->m_pkthdr.len);
732		buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
733
734		usb2_copy_in(xfer->frbuffers, 0, buf, 2);
735
736		usb2_m_copy_in(xfer->frbuffers, 2,
737		    m, 0, m->m_pkthdr.len);
738
739		/*
740		 * If there's a BPF listener, bounce a copy of this frame
741		 * to him.
742		 */
743		BPF_MTAP(ifp, m);
744
745		m_freem(m);
746
747		usb2_start_hardware(xfer);
748
749done:
750		return;
751
752	default:			/* Error */
753		DPRINTFN(11, "transfer error, %s\n",
754		    usb2_errstr(xfer->error));
755
756		if (xfer->error != USB_ERR_CANCELLED) {
757			/* try to clear stall first */
758			sc->sc_flags |= CUE_FLAG_WRITE_STALL;
759			usb2_transfer_start(sc->sc_xfer[2]);
760		}
761		ifp->if_oerrors++;
762		return;
763
764	}
765}
766
767static void
768cue_init_cb(void *arg)
769{
770	struct cue_softc *sc = arg;
771
772	mtx_lock(&sc->sc_mtx);
773	usb2_config_td_queue_command
774	    (&sc->sc_config_td, &cue_cfg_pre_init,
775	    &cue_cfg_init, 0, 0);
776	mtx_unlock(&sc->sc_mtx);
777
778	return;
779}
780
781static void
782cue_cfg_pre_init(struct cue_softc *sc,
783    struct usb2_config_td_cc *cc, uint16_t refcount)
784{
785	struct ifnet *ifp = sc->sc_ifp;
786
787	/* immediate configuration */
788
789	cue_cfg_pre_stop(sc, cc, 0);
790
791	ifp->if_drv_flags |= IFF_DRV_RUNNING;
792
793	sc->sc_flags |= CUE_FLAG_HL_READY;
794
795	return;
796}
797
798static void
799cue_cfg_init(struct cue_softc *sc,
800    struct usb2_config_td_cc *cc, uint16_t refcount)
801{
802	uint8_t i;
803
804	/*
805	 * Cancel pending I/O and free all RX/TX buffers.
806	 */
807	cue_cfg_stop(sc, cc, 0);
808#if 0
809	cue_cfg_reset(sc);
810#endif
811	/* Set MAC address */
812
813	for (i = 0; i < ETHER_ADDR_LEN; i++) {
814		cue_cfg_csr_write_1(sc, CUE_PAR0 - i, cc->if_lladdr[i]);
815	}
816
817	/* Enable RX logic. */
818	cue_cfg_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON);
819
820	/* Load the multicast filter */
821	cue_cfg_promisc_upd(sc, cc, 0);
822
823	/*
824	 * Set the number of RX and TX buffers that we want
825	 * to reserve inside the ASIC.
826	 */
827	cue_cfg_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
828	cue_cfg_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
829
830	/* Set advanced operation modes. */
831	cue_cfg_csr_write_1(sc, CUE_ADVANCED_OPMODES,
832	    CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */
833
834	/* Program the LED operation. */
835	cue_cfg_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
836
837	sc->sc_flags |= (CUE_FLAG_READ_STALL |
838	    CUE_FLAG_WRITE_STALL |
839	    CUE_FLAG_LL_READY);
840
841	cue_start_transfers(sc);
842	return;
843}
844
845static int
846cue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data)
847{
848	struct cue_softc *sc = ifp->if_softc;
849	int error = 0;
850
851	switch (command) {
852	case SIOCSIFFLAGS:
853		mtx_lock(&sc->sc_mtx);
854		if (ifp->if_flags & IFF_UP) {
855			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
856				usb2_config_td_queue_command
857				    (&sc->sc_config_td, &cue_config_copy,
858				    &cue_cfg_promisc_upd, 0, 0);
859			} else {
860				usb2_config_td_queue_command
861				    (&sc->sc_config_td, &cue_cfg_pre_init,
862				    &cue_cfg_init, 0, 0);
863			}
864		} else {
865			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
866				usb2_config_td_queue_command
867				    (&sc->sc_config_td, &cue_cfg_pre_stop,
868				    &cue_cfg_stop, 0, 0);
869			}
870		}
871		mtx_unlock(&sc->sc_mtx);
872		break;
873
874	case SIOCADDMULTI:
875	case SIOCDELMULTI:
876		mtx_lock(&sc->sc_mtx);
877		usb2_config_td_queue_command
878		    (&sc->sc_config_td, &cue_config_copy,
879		    &cue_cfg_promisc_upd, 0, 0);
880		mtx_unlock(&sc->sc_mtx);
881		break;
882
883	default:
884		error = ether_ioctl(ifp, command, data);
885		break;
886	}
887	return (error);
888}
889
890static void
891cue_watchdog(void *arg)
892{
893	struct cue_softc *sc = arg;
894
895	mtx_assert(&sc->sc_mtx, MA_OWNED);
896
897	usb2_config_td_queue_command
898	    (&sc->sc_config_td, NULL, &cue_cfg_tick, 0, 0);
899
900	usb2_callout_reset(&sc->sc_watchdog,
901	    hz, &cue_watchdog, sc);
902
903	mtx_unlock(&sc->sc_mtx);
904	return;
905}
906
907/*
908 * Stop the adapter and free any mbufs allocated to the
909 * RX and TX lists.
910 */
911static void
912cue_cfg_pre_stop(struct cue_softc *sc,
913    struct usb2_config_td_cc *cc, uint16_t refcount)
914{
915	struct ifnet *ifp = sc->sc_ifp;
916
917	if (cc) {
918		/* copy the needed configuration */
919		cue_config_copy(sc, cc, refcount);
920	}
921	/* immediate configuration */
922
923	if (ifp) {
924		/* clear flags */
925		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
926	}
927	sc->sc_flags &= ~(CUE_FLAG_HL_READY |
928	    CUE_FLAG_LL_READY);
929
930	/*
931	 * stop all the transfers, if not already stopped:
932	 */
933	usb2_transfer_stop(sc->sc_xfer[0]);
934	usb2_transfer_stop(sc->sc_xfer[1]);
935	usb2_transfer_stop(sc->sc_xfer[2]);
936	usb2_transfer_stop(sc->sc_xfer[3]);
937	return;
938}
939
940static void
941cue_cfg_stop(struct cue_softc *sc,
942    struct usb2_config_td_cc *cc, uint16_t refcount)
943{
944	cue_cfg_csr_write_1(sc, CUE_ETHCTL, 0);
945	cue_cfg_reset(sc);
946	return;
947}
948
949/*
950 * Stop all chip I/O so that the kernel's probe routines don't
951 * get confused by errant DMAs when rebooting.
952 */
953static int
954cue_shutdown(device_t dev)
955{
956	struct cue_softc *sc = device_get_softc(dev);
957
958	mtx_lock(&sc->sc_mtx);
959
960	usb2_config_td_queue_command
961	    (&sc->sc_config_td, &cue_cfg_pre_stop,
962	    &cue_cfg_stop, 0, 0);
963
964	mtx_unlock(&sc->sc_mtx);
965
966	return (0);
967}
968