if_cue.c revision 187378
10Sduke/*-
24249Skatleman * Copyright (c) 1997, 1998, 1999, 2000
30Sduke *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
40Sduke *
50Sduke * Redistribution and use in source and binary forms, with or without
60Sduke * modification, are permitted provided that the following conditions
70Sduke * are met:
80Sduke * 1. Redistributions of source code must retain the above copyright
90Sduke *    notice, this list of conditions and the following disclaimer.
100Sduke * 2. Redistributions in binary form must reproduce the above copyright
110Sduke *    notice, this list of conditions and the following disclaimer in the
120Sduke *    documentation and/or other materials provided with the distribution.
130Sduke * 3. All advertising materials mentioning features or use of this software
140Sduke *    must display the following acknowledgement:
150Sduke *	This product includes software developed by Bill Paul.
160Sduke * 4. Neither the name of the author nor the names of any co-contributors
170Sduke *    may be used to endorse or promote products derived from this software
180Sduke *    without specific prior written permission.
192362Sohair *
202362Sohair * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
212362Sohair * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
220Sduke * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
230Sduke * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
240Sduke * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
250Sduke * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
260Sduke * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
270Sduke * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
280Sduke * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
290Sduke * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
300Sduke * THE POSSIBILITY OF SUCH DAMAGE.
310Sduke */
320Sduke
330Sduke#include <sys/cdefs.h>
340Sduke__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_cue2.c 187378 2009-01-18 05:35:58Z thompsa $");
350Sduke
360Sduke/*
370Sduke * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
380Sduke * adapters and others.
390Sduke *
400Sduke * Written by Bill Paul <wpaul@ee.columbia.edu>
410Sduke * Electrical Engineering Department
420Sduke * Columbia University, New York City
430Sduke */
440Sduke
450Sduke/*
460Sduke * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
470Sduke * RX filter uses a 512-bit multicast hash table, single perfect entry
480Sduke * for the station address, and promiscuous mode. Unlike the ADMtek
490Sduke * and KLSI chips, the CATC ASIC supports read and write combining
500Sduke * mode where multiple packets can be transfered using a single bulk
510Sduke * transaction, which helps performance a great deal.
520Sduke */
534081Sdav
544081Sdav/*
554081Sdav * NOTE: all function names beginning like "cue_cfg_" can only
560Sduke * be called from within the config thread function !
570Sduke */
580Sduke
590Sduke#include <dev/usb2/include/usb2_devid.h>
600Sduke#include <dev/usb2/include/usb2_standard.h>
610Sduke#include <dev/usb2/include/usb2_mfunc.h>
620Sduke#include <dev/usb2/include/usb2_error.h>
630Sduke
640Sduke#define	usb2_config_td_cc usb2_ether_cc
650Sduke#define	usb2_config_td_softc cue_softc
660Sduke
670Sduke#define	USB_DEBUG_VAR cue_debug
680Sduke
690Sduke#include <dev/usb2/core/usb2_core.h>
700Sduke#include <dev/usb2/core/usb2_lookup.h>
710Sduke#include <dev/usb2/core/usb2_process.h>
720Sduke#include <dev/usb2/core/usb2_config_td.h>
730Sduke#include <dev/usb2/core/usb2_debug.h>
740Sduke#include <dev/usb2/core/usb2_request.h>
750Sduke#include <dev/usb2/core/usb2_busdma.h>
760Sduke#include <dev/usb2/core/usb2_util.h>
770Sduke
780Sduke#include <dev/usb2/ethernet/usb2_ethernet.h>
790Sduke#include <dev/usb2/ethernet/if_cuereg.h>
800Sduke
810Sduke/*
820Sduke * Various supported device vendors/products.
830Sduke */
840Sduke
850Sduke/* Belkin F5U111 adapter covered by NETMATE entry */
860Sduke
870Sdukestatic const struct usb2_device_id cue_devs[] = {
880Sduke	{USB_VPI(USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE, 0)},
890Sduke	{USB_VPI(USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2, 0)},
900Sduke	{USB_VPI(USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK, 0)},
910Sduke};
920Sduke
930Sduke/* prototypes */
940Sduke
950Sdukestatic device_probe_t cue_probe;
960Sdukestatic device_attach_t cue_attach;
974081Sdavstatic device_detach_t cue_detach;
984081Sdavstatic device_shutdown_t cue_shutdown;
994081Sdav
1004081Sdavstatic usb2_callback_t cue_bulk_read_clear_stall_callback;
1010Sdukestatic usb2_callback_t cue_bulk_read_callback;
1020Sdukestatic usb2_callback_t cue_bulk_write_clear_stall_callback;
1030Sdukestatic 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_N_TRANSFER] = {
139
140	[CUE_BULK_DT_WR] = {
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	[CUE_BULK_DT_RD] = {
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	[CUE_BULK_CS_WR] = {
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	[CUE_BULK_CS_RD] = {
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}
231
232#define	CUE_CFG_SETBIT(sc, reg, x)				\
233	cue_cfg_csr_write_1(sc, reg, cue_cfg_csr_read_1(sc, reg) | (x))
234
235#define	CUE_CFG_CLRBIT(sc, reg, x)				\
236	cue_cfg_csr_write_1(sc, reg, cue_cfg_csr_read_1(sc, reg) & ~(x))
237
238static uint8_t
239cue_cfg_csr_read_1(struct cue_softc *sc, uint16_t reg)
240{
241	struct usb2_device_request req;
242	uint8_t val;
243
244	req.bmRequestType = UT_READ_VENDOR_DEVICE;
245	req.bRequest = CUE_CMD_READREG;
246	USETW(req.wValue, 0);
247	USETW(req.wIndex, reg);
248	USETW(req.wLength, 1);
249
250	cue_cfg_do_request(sc, &req, &val);
251	return (val);
252}
253
254static uint16_t
255cue_cfg_csr_read_2(struct cue_softc *sc, uint8_t reg)
256{
257	struct usb2_device_request req;
258	uint16_t val;
259
260	req.bmRequestType = UT_READ_VENDOR_DEVICE;
261	req.bRequest = CUE_CMD_READREG;
262	USETW(req.wValue, 0);
263	USETW(req.wIndex, reg);
264	USETW(req.wLength, 2);
265
266	cue_cfg_do_request(sc, &req, &val);
267	return (le16toh(val));
268}
269
270static void
271cue_cfg_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val)
272{
273	struct usb2_device_request req;
274
275	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
276	req.bRequest = CUE_CMD_WRITEREG;
277	USETW(req.wValue, val);
278	USETW(req.wIndex, reg);
279	USETW(req.wLength, 0);
280
281	cue_cfg_do_request(sc, &req, NULL);
282}
283
284static void
285cue_cfg_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr,
286    void *buf, uint16_t len)
287{
288	struct usb2_device_request req;
289
290	if (cmd == CUE_CMD_READSRAM) {
291		req.bmRequestType = UT_READ_VENDOR_DEVICE;
292	} else {
293		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
294	}
295	req.bRequest = cmd;
296	USETW(req.wValue, 0);
297	USETW(req.wIndex, addr);
298	USETW(req.wLength, len);
299
300	cue_cfg_do_request(sc, &req, buf);
301}
302
303static void
304cue_cfg_getmac(struct cue_softc *sc, void *buf)
305{
306	struct usb2_device_request req;
307
308	req.bmRequestType = UT_READ_VENDOR_DEVICE;
309	req.bRequest = CUE_CMD_GET_MACADDR;
310	USETW(req.wValue, 0);
311	USETW(req.wIndex, 0);
312	USETW(req.wLength, ETHER_ADDR_LEN);
313
314	cue_cfg_do_request(sc, &req, buf);
315}
316
317#define	CUE_BITS 9
318
319static void
320cue_mchash(struct usb2_config_td_cc *cc, const uint8_t *addr)
321{
322	uint16_t h;
323
324	h = ether_crc32_le(addr, ETHER_ADDR_LEN) &
325	    ((1 << CUE_BITS) - 1);
326	cc->if_hash[h >> 3] |= 1 << (h & 0x7);
327}
328
329static void
330cue_cfg_promisc_upd(struct cue_softc *sc,
331    struct usb2_config_td_cc *cc, uint16_t refcount)
332{
333	/* if we want promiscuous mode, set the allframes bit */
334
335	if (cc->if_flags & IFF_PROMISC) {
336		CUE_CFG_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
337	} else {
338		CUE_CFG_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
339	}
340
341	/* write multicast hash-bits */
342
343	cue_cfg_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
344	    cc->if_hash, CUE_MCAST_TABLE_LEN);
345}
346
347static void
348cue_config_copy(struct cue_softc *sc,
349    struct usb2_config_td_cc *cc, uint16_t refcount)
350{
351	bzero(cc, sizeof(*cc));
352	usb2_ether_cc(sc->sc_ifp, &cue_mchash, cc);
353}
354
355static void
356cue_cfg_reset(struct cue_softc *sc)
357{
358	struct usb2_device_request req;
359
360	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
361	req.bRequest = CUE_CMD_RESET;
362	USETW(req.wValue, 0);
363	USETW(req.wIndex, 0);
364	USETW(req.wLength, 0);
365
366	cue_cfg_do_request(sc, &req, NULL);
367
368	/*
369	 * wait a little while for the chip to get its brains in order:
370	 */
371
372	(void)usb2_config_td_sleep(&sc->sc_config_td, hz / 100);
373}
374
375static int
376cue_probe(device_t dev)
377{
378	struct usb2_attach_arg *uaa = device_get_ivars(dev);
379
380	if (uaa->usb2_mode != USB_MODE_HOST) {
381		return (ENXIO);
382	}
383	if (uaa->info.bConfigIndex != CUE_CONFIG_IDX) {
384		return (ENXIO);
385	}
386	if (uaa->info.bIfaceIndex != CUE_IFACE_IDX) {
387		return (ENXIO);
388	}
389	return (usb2_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa));
390}
391
392static int
393cue_attach(device_t dev)
394{
395	struct usb2_attach_arg *uaa = device_get_ivars(dev);
396	struct cue_softc *sc = device_get_softc(dev);
397	uint8_t iface_index;
398	int32_t error;
399
400	if (sc == NULL) {
401		return (ENOMEM);
402	}
403	sc->sc_udev = uaa->device;
404	sc->sc_dev = dev;
405	sc->sc_unit = device_get_unit(dev);
406
407	device_set_usb2_desc(dev);
408
409	mtx_init(&sc->sc_mtx, "cue lock", NULL, MTX_DEF | MTX_RECURSE);
410
411	usb2_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
412
413	iface_index = CUE_IFACE_IDX;
414	error = usb2_transfer_setup(uaa->device, &iface_index,
415	    sc->sc_xfer, cue_config, CUE_N_TRANSFER, sc, &sc->sc_mtx);
416	if (error) {
417		device_printf(dev, "allocating USB "
418		    "transfers failed!\n");
419		goto detach;
420	}
421	error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx,
422	    NULL, sizeof(struct usb2_config_td_cc), 16);
423	if (error) {
424		device_printf(dev, "could not setup config "
425		    "thread!\n");
426		goto detach;
427	}
428	mtx_lock(&sc->sc_mtx);
429
430	/* start setup */
431
432	usb2_config_td_queue_command
433	    (&sc->sc_config_td, NULL, &cue_cfg_first_time_setup, 0, 0);
434
435	cue_watchdog(sc);
436	mtx_unlock(&sc->sc_mtx);
437	return (0);			/* success */
438
439detach:
440	cue_detach(dev);
441	return (ENXIO);			/* failure */
442}
443
444static void
445cue_cfg_first_time_setup(struct cue_softc *sc,
446    struct usb2_config_td_cc *cc, uint16_t refcount)
447{
448	uint8_t eaddr[ETHER_ADDR_LEN];
449	struct ifnet *ifp;
450
451#if 0
452	/* Reset the adapter. */
453	cue_cfg_reset(sc);
454#endif
455	/*
456	 * Get station address.
457	 */
458	cue_cfg_getmac(sc, eaddr);
459
460	mtx_unlock(&sc->sc_mtx);
461
462	ifp = if_alloc(IFT_ETHER);
463
464	mtx_lock(&sc->sc_mtx);
465
466	if (ifp == NULL) {
467		printf("cue%d: could not if_alloc()\n",
468		    sc->sc_unit);
469		goto done;
470	}
471
472	ifp->if_softc = sc;
473	if_initname(ifp, "cue", sc->sc_unit);
474	ifp->if_mtu = ETHERMTU;
475	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
476	ifp->if_ioctl = cue_ioctl_cb;
477	ifp->if_start = cue_start_cb;
478	ifp->if_watchdog = NULL;
479	ifp->if_init = cue_init_cb;
480	ifp->if_baudrate = 10000000;
481	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
482	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
483	IFQ_SET_READY(&ifp->if_snd);
484
485	sc->sc_ifp = ifp;
486
487	mtx_unlock(&sc->sc_mtx);
488
489	ether_ifattach(ifp, eaddr);
490
491	mtx_lock(&sc->sc_mtx);
492
493done:
494	return;
495}
496
497static int
498cue_detach(device_t dev)
499{
500	struct cue_softc *sc = device_get_softc(dev);
501	struct ifnet *ifp;
502
503	usb2_config_td_drain(&sc->sc_config_td);
504
505	mtx_lock(&sc->sc_mtx);
506
507	usb2_callout_stop(&sc->sc_watchdog);
508
509	cue_cfg_pre_stop(sc, NULL, 0);
510
511	ifp = sc->sc_ifp;
512
513	mtx_unlock(&sc->sc_mtx);
514
515	/* stop all USB transfers first */
516	usb2_transfer_unsetup(sc->sc_xfer, CUE_N_TRANSFER);
517
518	/* get rid of any late children */
519	bus_generic_detach(dev);
520
521	if (ifp) {
522		ether_ifdetach(ifp);
523		if_free(ifp);
524	}
525	usb2_config_td_unsetup(&sc->sc_config_td);
526
527	usb2_callout_drain(&sc->sc_watchdog);
528
529	mtx_destroy(&sc->sc_mtx);
530
531	return (0);
532}
533
534static void
535cue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer)
536{
537	struct cue_softc *sc = xfer->priv_sc;
538	struct usb2_xfer *xfer_other = sc->sc_xfer[CUE_BULK_DT_RD];
539
540	if (usb2_clear_stall_callback(xfer, xfer_other)) {
541		DPRINTF("stall cleared\n");
542		sc->sc_flags &= ~CUE_FLAG_READ_STALL;
543		usb2_transfer_start(xfer_other);
544	}
545}
546
547static void
548cue_bulk_read_callback(struct usb2_xfer *xfer)
549{
550	struct cue_softc *sc = xfer->priv_sc;
551	struct ifnet *ifp = sc->sc_ifp;
552	struct mbuf *m = NULL;
553	uint8_t buf[2];
554	uint16_t len;
555
556	switch (USB_GET_STATE(xfer)) {
557	case USB_ST_TRANSFERRED:
558
559		if (xfer->actlen <= (2 + sizeof(struct ether_header))) {
560			ifp->if_ierrors++;
561			goto tr_setup;
562		}
563		usb2_copy_out(xfer->frbuffers, 0, buf, 2);
564
565		len = buf[0] | (buf[1] << 8);
566
567		xfer->actlen -= 2;
568
569		m = usb2_ether_get_mbuf();
570
571		if (m == NULL) {
572			ifp->if_ierrors++;
573			goto tr_setup;
574		}
575		xfer->actlen = min(xfer->actlen, m->m_len);
576		xfer->actlen = min(xfer->actlen, len);
577
578		usb2_copy_out(xfer->frbuffers, 2, m->m_data, xfer->actlen);
579
580		ifp->if_ipackets++;
581		m->m_pkthdr.rcvif = ifp;
582		m->m_pkthdr.len = m->m_len = xfer->actlen;
583
584	case USB_ST_SETUP:
585tr_setup:
586
587		if (sc->sc_flags & CUE_FLAG_READ_STALL) {
588			usb2_transfer_start(sc->sc_xfer[CUE_BULK_CS_RD]);
589		} else {
590			xfer->frlengths[0] = xfer->max_data_length;
591			usb2_start_hardware(xfer);
592		}
593
594		/*
595		 * At the end of a USB callback it is always safe to unlock
596		 * the private mutex of a device! That is why we do the
597		 * "if_input" here, and not some lines up!
598		 */
599		if (m) {
600			mtx_unlock(&sc->sc_mtx);
601			(ifp->if_input) (ifp, m);
602			mtx_lock(&sc->sc_mtx);
603		}
604		return;
605
606	default:			/* Error */
607		if (xfer->error != USB_ERR_CANCELLED) {
608			/* try to clear stall first */
609			sc->sc_flags |= CUE_FLAG_READ_STALL;
610			usb2_transfer_start(sc->sc_xfer[CUE_BULK_CS_RD]);
611		}
612		DPRINTF("bulk read error, %s\n",
613		    usb2_errstr(xfer->error));
614		return;
615
616	}
617}
618
619static void
620cue_cfg_tick(struct cue_softc *sc,
621    struct usb2_config_td_cc *cc, uint16_t refcount)
622{
623	struct ifnet *ifp = sc->sc_ifp;
624
625	if ((ifp == NULL)) {
626		/* not ready */
627		return;
628	}
629	ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_SINGLECOLL);
630	ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_MULTICOLL);
631	ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_EXCESSCOLL);
632
633	if (cue_cfg_csr_read_2(sc, CUE_RX_FRAMEERR)) {
634		ifp->if_ierrors++;
635	}
636	/* start stopped transfers, if any */
637
638	cue_start_transfers(sc);
639}
640
641static void
642cue_start_cb(struct ifnet *ifp)
643{
644	struct cue_softc *sc = ifp->if_softc;
645
646	mtx_lock(&sc->sc_mtx);
647
648	cue_start_transfers(sc);
649
650	mtx_unlock(&sc->sc_mtx);
651}
652
653static void
654cue_start_transfers(struct cue_softc *sc)
655{
656	if ((sc->sc_flags & CUE_FLAG_LL_READY) &&
657	    (sc->sc_flags & CUE_FLAG_HL_READY)) {
658
659		/*
660		 * start the USB transfers, if not already started:
661		 */
662		usb2_transfer_start(sc->sc_xfer[CUE_BULK_DT_RD]);
663		usb2_transfer_start(sc->sc_xfer[CUE_BULK_DT_WR]);
664	}
665}
666
667static void
668cue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer)
669{
670	struct cue_softc *sc = xfer->priv_sc;
671	struct usb2_xfer *xfer_other = sc->sc_xfer[CUE_BULK_DT_WR];
672
673	if (usb2_clear_stall_callback(xfer, xfer_other)) {
674		DPRINTF("stall cleared\n");
675		sc->sc_flags &= ~CUE_FLAG_WRITE_STALL;
676		usb2_transfer_start(xfer_other);
677	}
678}
679
680static void
681cue_bulk_write_callback(struct usb2_xfer *xfer)
682{
683	struct cue_softc *sc = xfer->priv_sc;
684	struct ifnet *ifp = sc->sc_ifp;
685	struct mbuf *m;
686	uint8_t buf[2];
687
688	switch (USB_GET_STATE(xfer)) {
689	case USB_ST_TRANSFERRED:
690		DPRINTFN(11, "transfer complete\n");
691
692		ifp->if_opackets++;
693
694	case USB_ST_SETUP:
695
696		if (sc->sc_flags & CUE_FLAG_WRITE_STALL) {
697			usb2_transfer_start(sc->sc_xfer[CUE_BULK_CS_WR]);
698			goto done;
699		}
700		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
701
702		if (m == NULL) {
703			goto done;
704		}
705		if (m->m_pkthdr.len > MCLBYTES) {
706			m->m_pkthdr.len = MCLBYTES;
707		}
708		xfer->frlengths[0] = (m->m_pkthdr.len + 2);
709
710		/* the first two bytes are the frame length */
711
712		buf[0] = (uint8_t)(m->m_pkthdr.len);
713		buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
714
715		usb2_copy_in(xfer->frbuffers, 0, buf, 2);
716
717		usb2_m_copy_in(xfer->frbuffers, 2,
718		    m, 0, m->m_pkthdr.len);
719
720		/*
721		 * If there's a BPF listener, bounce a copy of this frame
722		 * to him.
723		 */
724		BPF_MTAP(ifp, m);
725
726		m_freem(m);
727
728		usb2_start_hardware(xfer);
729
730done:
731		return;
732
733	default:			/* Error */
734		DPRINTFN(11, "transfer error, %s\n",
735		    usb2_errstr(xfer->error));
736
737		if (xfer->error != USB_ERR_CANCELLED) {
738			/* try to clear stall first */
739			sc->sc_flags |= CUE_FLAG_WRITE_STALL;
740			usb2_transfer_start(sc->sc_xfer[CUE_BULK_CS_WR]);
741		}
742		ifp->if_oerrors++;
743		return;
744
745	}
746}
747
748static void
749cue_init_cb(void *arg)
750{
751	struct cue_softc *sc = arg;
752
753	mtx_lock(&sc->sc_mtx);
754	usb2_config_td_queue_command
755	    (&sc->sc_config_td, &cue_cfg_pre_init,
756	    &cue_cfg_init, 0, 0);
757	mtx_unlock(&sc->sc_mtx);
758}
759
760static void
761cue_cfg_pre_init(struct cue_softc *sc,
762    struct usb2_config_td_cc *cc, uint16_t refcount)
763{
764	struct ifnet *ifp = sc->sc_ifp;
765
766	/* immediate configuration */
767
768	cue_cfg_pre_stop(sc, cc, 0);
769
770	ifp->if_drv_flags |= IFF_DRV_RUNNING;
771
772	sc->sc_flags |= CUE_FLAG_HL_READY;
773}
774
775static void
776cue_cfg_init(struct cue_softc *sc,
777    struct usb2_config_td_cc *cc, uint16_t refcount)
778{
779	uint8_t i;
780
781	/*
782	 * Cancel pending I/O and free all RX/TX buffers.
783	 */
784	cue_cfg_stop(sc, cc, 0);
785#if 0
786	cue_cfg_reset(sc);
787#endif
788	/* Set MAC address */
789
790	for (i = 0; i < ETHER_ADDR_LEN; i++) {
791		cue_cfg_csr_write_1(sc, CUE_PAR0 - i, cc->if_lladdr[i]);
792	}
793
794	/* Enable RX logic. */
795	cue_cfg_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON);
796
797	/* Load the multicast filter */
798	cue_cfg_promisc_upd(sc, cc, 0);
799
800	/*
801	 * Set the number of RX and TX buffers that we want
802	 * to reserve inside the ASIC.
803	 */
804	cue_cfg_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
805	cue_cfg_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
806
807	/* Set advanced operation modes. */
808	cue_cfg_csr_write_1(sc, CUE_ADVANCED_OPMODES,
809	    CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */
810
811	/* Program the LED operation. */
812	cue_cfg_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
813
814	sc->sc_flags |= (CUE_FLAG_READ_STALL |
815	    CUE_FLAG_WRITE_STALL |
816	    CUE_FLAG_LL_READY);
817
818	cue_start_transfers(sc);
819}
820
821static int
822cue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data)
823{
824	struct cue_softc *sc = ifp->if_softc;
825	int error = 0;
826
827	switch (command) {
828	case SIOCSIFFLAGS:
829		mtx_lock(&sc->sc_mtx);
830		if (ifp->if_flags & IFF_UP) {
831			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
832				usb2_config_td_queue_command
833				    (&sc->sc_config_td, &cue_config_copy,
834				    &cue_cfg_promisc_upd, 0, 0);
835			} else {
836				usb2_config_td_queue_command
837				    (&sc->sc_config_td, &cue_cfg_pre_init,
838				    &cue_cfg_init, 0, 0);
839			}
840		} else {
841			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
842				usb2_config_td_queue_command
843				    (&sc->sc_config_td, &cue_cfg_pre_stop,
844				    &cue_cfg_stop, 0, 0);
845			}
846		}
847		mtx_unlock(&sc->sc_mtx);
848		break;
849
850	case SIOCADDMULTI:
851	case SIOCDELMULTI:
852		mtx_lock(&sc->sc_mtx);
853		usb2_config_td_queue_command
854		    (&sc->sc_config_td, &cue_config_copy,
855		    &cue_cfg_promisc_upd, 0, 0);
856		mtx_unlock(&sc->sc_mtx);
857		break;
858
859	default:
860		error = ether_ioctl(ifp, command, data);
861		break;
862	}
863	return (error);
864}
865
866static void
867cue_watchdog(void *arg)
868{
869	struct cue_softc *sc = arg;
870
871	mtx_assert(&sc->sc_mtx, MA_OWNED);
872
873	usb2_config_td_queue_command
874	    (&sc->sc_config_td, NULL, &cue_cfg_tick, 0, 0);
875
876	usb2_callout_reset(&sc->sc_watchdog,
877	    hz, &cue_watchdog, sc);
878}
879
880/*
881 * Stop the adapter and free any mbufs allocated to the
882 * RX and TX lists.
883 */
884static void
885cue_cfg_pre_stop(struct cue_softc *sc,
886    struct usb2_config_td_cc *cc, uint16_t refcount)
887{
888	struct ifnet *ifp = sc->sc_ifp;
889
890	if (cc) {
891		/* copy the needed configuration */
892		cue_config_copy(sc, cc, refcount);
893	}
894	/* immediate configuration */
895
896	if (ifp) {
897		/* clear flags */
898		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
899	}
900	sc->sc_flags &= ~(CUE_FLAG_HL_READY |
901	    CUE_FLAG_LL_READY);
902
903	/*
904	 * stop all the transfers, if not already stopped:
905	 */
906	usb2_transfer_stop(sc->sc_xfer[CUE_BULK_DT_WR]);
907	usb2_transfer_stop(sc->sc_xfer[CUE_BULK_DT_RD]);
908	usb2_transfer_stop(sc->sc_xfer[CUE_BULK_CS_WR]);
909	usb2_transfer_stop(sc->sc_xfer[CUE_BULK_CS_RD]);
910}
911
912static void
913cue_cfg_stop(struct cue_softc *sc,
914    struct usb2_config_td_cc *cc, uint16_t refcount)
915{
916	cue_cfg_csr_write_1(sc, CUE_ETHCTL, 0);
917	cue_cfg_reset(sc);
918}
919
920/*
921 * Stop all chip I/O so that the kernel's probe routines don't
922 * get confused by errant DMAs when rebooting.
923 */
924static int
925cue_shutdown(device_t dev)
926{
927	struct cue_softc *sc = device_get_softc(dev);
928
929	mtx_lock(&sc->sc_mtx);
930
931	usb2_config_td_queue_command
932	    (&sc->sc_config_td, &cue_cfg_pre_stop,
933	    &cue_cfg_stop, 0, 0);
934
935	mtx_unlock(&sc->sc_mtx);
936
937	return (0);
938}
939