xref: /openbsd-src/sys/dev/usb/if_cue.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: if_cue.c,v 1.10 2001/07/08 23:38:08 fgsch Exp $ */
2 /*	$NetBSD: if_cue.c,v 1.34 2001/04/12 23:54:56 augustss Exp $	*/
3 /*
4  * Copyright (c) 1997, 1998, 1999, 2000
5  *	Bill Paul <wpaul@ee.columbia.edu>.  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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $FreeBSD: src/sys/dev/usb/if_cue.c,v 1.4 2000/01/16 22:45:06 wpaul Exp $
35  */
36 
37 /*
38  * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
39  * adapters and others.
40  *
41  * Written by Bill Paul <wpaul@ee.columbia.edu>
42  * Electrical Engineering Department
43  * Columbia University, New York City
44  */
45 
46 /*
47  * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
48  * RX filter uses a 512-bit multicast hash table, single perfect entry
49  * for the station address, and promiscuous mode. Unlike the ADMtek
50  * and KLSI chips, the CATC ASIC supports read and write combining
51  * mode where multiple packets can be transferred using a single bulk
52  * transaction, which helps performance a great deal.
53  */
54 
55 /*
56  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
57  */
58 
59 #if defined(__NetBSD__)
60 #include "opt_inet.h"
61 #include "opt_ns.h"
62 #include "bpfilter.h"
63 #include "rnd.h"
64 #elif defined(__OpenBSD__)
65 #include "bpfilter.h"
66 #endif /* defined(__OpenBSD__) */
67 
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #if !defined(__OpenBSD__)
71 #include <sys/callout.h>
72 #endif
73 #include <sys/sockio.h>
74 #include <sys/mbuf.h>
75 #include <sys/malloc.h>
76 #include <sys/kernel.h>
77 #include <sys/socket.h>
78 
79 #include <sys/device.h>
80 #if NRND > 0
81 #include <sys/rnd.h>
82 #endif
83 
84 #include <net/if.h>
85 #if defined(__NetBSD__)
86 #include <net/if_arp.h>
87 #endif
88 #include <net/if_dl.h>
89 
90 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
91 
92 #if NBPFILTER > 0
93 #include <net/bpf.h>
94 #endif
95 
96 #if defined(__NetBSD__)
97 #include <net/if_ether.h>
98 #ifdef INET
99 #include <netinet/in.h>
100 #include <netinet/if_inarp.h>
101 #endif
102 #endif /* defined(__NetBSD__) */
103 
104 #if defined(__OpenBSD__)
105 #ifdef INET
106 #include <netinet/in.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/in_var.h>
109 #include <netinet/ip.h>
110 #include <netinet/if_ether.h>
111 #endif
112 #endif /* defined(__OpenBSD__) */
113 
114 #ifdef NS
115 #include <netns/ns.h>
116 #include <netns/ns_if.h>
117 #endif
118 
119 #include <dev/usb/usb.h>
120 #include <dev/usb/usbdi.h>
121 #include <dev/usb/usbdi_util.h>
122 #include <dev/usb/usbdevs.h>
123 
124 #include <dev/usb/if_cuereg.h>
125 
126 #ifdef CUE_DEBUG
127 #define DPRINTF(x)	if (cuedebug) logprintf x
128 #define DPRINTFN(n,x)	if (cuedebug >= (n)) logprintf x
129 int	cuedebug = 0;
130 #else
131 #define DPRINTF(x)
132 #define DPRINTFN(n,x)
133 #endif
134 
135 /*
136  * Various supported device vendors/products.
137  */
138 Static struct cue_type cue_devs[] = {
139 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE },
140 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 },
141 	{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK },
142 	/* Belkin F5U111 adapter covered by NETMATE entry */
143 	{ 0, 0 }
144 };
145 
146 USB_DECLARE_DRIVER(cue);
147 
148 Static int cue_open_pipes(struct cue_softc *);
149 Static int cue_tx_list_init(struct cue_softc *);
150 Static int cue_rx_list_init(struct cue_softc *);
151 Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
152 Static int cue_send(struct cue_softc *, struct mbuf *, int);
153 Static void cue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
154 Static void cue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
155 Static void cue_tick(void *);
156 Static void cue_tick_task(void *);
157 Static void cue_start(struct ifnet *);
158 Static int cue_ioctl(struct ifnet *, u_long, caddr_t);
159 Static void cue_init(void *);
160 Static void cue_stop(struct cue_softc *);
161 Static void cue_watchdog(struct ifnet *);
162 
163 Static void cue_setmulti(struct cue_softc *);
164 Static u_int32_t cue_crc(caddr_t);
165 Static void cue_reset(struct cue_softc *);
166 
167 Static int cue_csr_read_1(struct cue_softc *, int);
168 Static int cue_csr_write_1(struct cue_softc *, int, int);
169 Static int cue_csr_read_2(struct cue_softc *, int);
170 #if 0
171 Static int cue_csr_write_2(struct cue_softc *, int, int);
172 #endif
173 Static int cue_mem(struct cue_softc *, int, int, void *, int);
174 Static int cue_getmac(struct cue_softc *, void *);
175 
176 #define CUE_SETBIT(sc, reg, x)				\
177 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
178 
179 #define CUE_CLRBIT(sc, reg, x)				\
180 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
181 
182 Static int
183 cue_csr_read_1(struct cue_softc *sc, int reg)
184 {
185 	usb_device_request_t	req;
186 	usbd_status		err;
187 	u_int8_t		val = 0;
188 
189 	if (sc->cue_dying)
190 		return (0);
191 
192 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
193 	req.bRequest = CUE_CMD_READREG;
194 	USETW(req.wValue, 0);
195 	USETW(req.wIndex, reg);
196 	USETW(req.wLength, 1);
197 
198 	err = usbd_do_request(sc->cue_udev, &req, &val);
199 
200 	if (err) {
201 		DPRINTF(("%s: cue_csr_read_1: reg=0x%x err=%s\n",
202 			 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
203 		return (0);
204 	}
205 
206 	DPRINTFN(10,("%s: cue_csr_read_1 reg=0x%x val=0x%x\n",
207 		     USBDEVNAME(sc->cue_dev), reg, val));
208 
209 	return (val);
210 }
211 
212 Static int
213 cue_csr_read_2(struct cue_softc *sc, int reg)
214 {
215 	usb_device_request_t	req;
216 	usbd_status		err;
217 	uWord			val;
218 
219 	if (sc->cue_dying)
220 		return (0);
221 
222 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
223 	req.bRequest = CUE_CMD_READREG;
224 	USETW(req.wValue, 0);
225 	USETW(req.wIndex, reg);
226 	USETW(req.wLength, 2);
227 
228 	err = usbd_do_request(sc->cue_udev, &req, &val);
229 
230 	DPRINTFN(10,("%s: cue_csr_read_2 reg=0x%x val=0x%x\n",
231 		     USBDEVNAME(sc->cue_dev), reg, UGETW(val)));
232 
233 	if (err) {
234 		DPRINTF(("%s: cue_csr_read_2: reg=0x%x err=%s\n",
235 			 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
236 		return (0);
237 	}
238 
239 	return (UGETW(val));
240 }
241 
242 Static int
243 cue_csr_write_1(struct cue_softc *sc, int reg, int val)
244 {
245 	usb_device_request_t	req;
246 	usbd_status		err;
247 
248 	if (sc->cue_dying)
249 		return (0);
250 
251 	DPRINTFN(10,("%s: cue_csr_write_1 reg=0x%x val=0x%x\n",
252 		     USBDEVNAME(sc->cue_dev), reg, val));
253 
254 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
255 	req.bRequest = CUE_CMD_WRITEREG;
256 	USETW(req.wValue, val);
257 	USETW(req.wIndex, reg);
258 	USETW(req.wLength, 0);
259 
260 	err = usbd_do_request(sc->cue_udev, &req, NULL);
261 
262 	if (err) {
263 		DPRINTF(("%s: cue_csr_write_1: reg=0x%x err=%s\n",
264 			 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
265 		return (-1);
266 	}
267 
268 	DPRINTFN(20,("%s: cue_csr_write_1, after reg=0x%x val=0x%x\n",
269 		     USBDEVNAME(sc->cue_dev), reg, cue_csr_read_1(sc, reg)));
270 
271 	return (0);
272 }
273 
274 #if 0
275 Static int
276 cue_csr_write_2(struct cue_softc *sc, int reg, int aval)
277 {
278 	usb_device_request_t	req;
279 	usbd_status		err;
280 	uWord			val;
281 	int			s;
282 
283 	if (sc->cue_dying)
284 		return (0);
285 
286 	DPRINTFN(10,("%s: cue_csr_write_2 reg=0x%x val=0x%x\n",
287 		     USBDEVNAME(sc->cue_dev), reg, aval));
288 
289 	USETW(val, aval);
290 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
291 	req.bRequest = CUE_CMD_WRITEREG;
292 	USETW(req.wValue, val);
293 	USETW(req.wIndex, reg);
294 	USETW(req.wLength, 0);
295 
296 	err = usbd_do_request(sc->cue_udev, &req, NULL);
297 
298 	if (err) {
299 		DPRINTF(("%s: cue_csr_write_2: reg=0x%x err=%s\n",
300 			 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
301 		return (-1);
302 	}
303 
304 	return (0);
305 }
306 #endif
307 
308 Static int
309 cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
310 {
311 	usb_device_request_t	req;
312 	usbd_status		err;
313 
314 	DPRINTFN(10,("%s: cue_mem cmd=0x%x addr=0x%x len=%d\n",
315 		     USBDEVNAME(sc->cue_dev), cmd, addr, len));
316 
317 	if (cmd == CUE_CMD_READSRAM)
318 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
319 	else
320 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
321 	req.bRequest = cmd;
322 	USETW(req.wValue, 0);
323 	USETW(req.wIndex, addr);
324 	USETW(req.wLength, len);
325 
326 	err = usbd_do_request(sc->cue_udev, &req, buf);
327 
328 	if (err) {
329 		DPRINTF(("%s: cue_csr_mem: addr=0x%x err=%s\n",
330 			 USBDEVNAME(sc->cue_dev), addr, usbd_errstr(err)));
331 		return (-1);
332 	}
333 
334 	return (0);
335 }
336 
337 Static int
338 cue_getmac(struct cue_softc *sc, void *buf)
339 {
340 	usb_device_request_t	req;
341 	usbd_status		err;
342 
343 	DPRINTFN(10,("%s: cue_getmac\n", USBDEVNAME(sc->cue_dev)));
344 
345 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
346 	req.bRequest = CUE_CMD_GET_MACADDR;
347 	USETW(req.wValue, 0);
348 	USETW(req.wIndex, 0);
349 	USETW(req.wLength, ETHER_ADDR_LEN);
350 
351 	err = usbd_do_request(sc->cue_udev, &req, buf);
352 
353 	if (err) {
354 		printf("%s: read MAC address failed\n",
355 		       USBDEVNAME(sc->cue_dev));
356 		return (-1);
357 	}
358 
359 	return (0);
360 }
361 
362 #define CUE_POLY	0xEDB88320
363 #define CUE_BITS	9
364 
365 Static u_int32_t
366 cue_crc(caddr_t addr)
367 {
368 	u_int32_t		idx, bit, data, crc;
369 
370 	/* Compute CRC for the address value. */
371 	crc = 0xFFFFFFFF; /* initial value */
372 
373 	for (idx = 0; idx < 6; idx++) {
374 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
375 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0);
376 	}
377 
378 	return (crc & ((1 << CUE_BITS) - 1));
379 }
380 
381 Static void
382 cue_setmulti(struct cue_softc *sc)
383 {
384 	struct ifnet		*ifp;
385 	struct ether_multi	*enm;
386 	struct ether_multistep	step;
387 	u_int32_t		h, i;
388 
389 	ifp = GET_IFP(sc);
390 
391 	DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n",
392 		    USBDEVNAME(sc->cue_dev), ifp->if_flags));
393 
394 	if (ifp->if_flags & IFF_PROMISC) {
395 allmulti:
396 		ifp->if_flags |= IFF_ALLMULTI;
397 		for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
398 			sc->cue_mctab[i] = 0xFF;
399 		cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
400 		    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
401 		return;
402 	}
403 
404 	/* first, zot all the existing hash bits */
405 	for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
406 		sc->cue_mctab[i] = 0;
407 
408 	/* now program new ones */
409 #if defined(__NetBSD__)
410 	ETHER_FIRST_MULTI(step, &sc->cue_ec, enm);
411 #else
412 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
413 #endif
414 	while (enm != NULL) {
415 		if (memcmp(enm->enm_addrlo,
416 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
417 			goto allmulti;
418 
419 		h = cue_crc(enm->enm_addrlo);
420 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
421 		ETHER_NEXT_MULTI(step, enm);
422 	}
423 
424 	ifp->if_flags &= ~IFF_ALLMULTI;
425 
426 	/*
427 	 * Also include the broadcast address in the filter
428 	 * so we can receive broadcast frames.
429 	 */
430 	if (ifp->if_flags & IFF_BROADCAST) {
431 		h = cue_crc(etherbroadcastaddr);
432 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
433 	}
434 
435 	cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
436 	    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
437 }
438 
439 Static void
440 cue_reset(struct cue_softc *sc)
441 {
442 	usb_device_request_t	req;
443 	usbd_status		err;
444 
445 	DPRINTFN(2,("%s: cue_reset\n", USBDEVNAME(sc->cue_dev)));
446 
447 	if (sc->cue_dying)
448 		return;
449 
450 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
451 	req.bRequest = CUE_CMD_RESET;
452 	USETW(req.wValue, 0);
453 	USETW(req.wIndex, 0);
454 	USETW(req.wLength, 0);
455 
456 	err = usbd_do_request(sc->cue_udev, &req, NULL);
457 
458 	if (err)
459 		printf("%s: reset failed\n", USBDEVNAME(sc->cue_dev));
460 
461 	/* Wait a little while for the chip to get its brains in order. */
462 	usbd_delay_ms(sc->cue_udev, 1);
463 }
464 
465 /*
466  * Probe for a CATC chip.
467  */
468 USB_MATCH(cue)
469 {
470 	USB_MATCH_START(cue, uaa);
471 	struct cue_type			*t;
472 
473 	if (uaa->iface != NULL)
474 		return (UMATCH_NONE);
475 
476 	for (t = cue_devs; t->cue_vid != 0; t++)
477 		if (uaa->vendor == t->cue_vid && uaa->product == t->cue_did)
478 			return (UMATCH_VENDOR_PRODUCT);
479 
480 	return (UMATCH_NONE);
481 }
482 
483 /*
484  * Attach the interface. Allocate softc structures, do ifmedia
485  * setup and ethernet/BPF attach.
486  */
487 USB_ATTACH(cue)
488 {
489 	USB_ATTACH_START(cue, sc, uaa);
490 	char			devinfo[1024];
491 	int			s;
492 	u_char			eaddr[ETHER_ADDR_LEN];
493 	usbd_device_handle	dev = uaa->device;
494 	usbd_interface_handle	iface;
495 	usbd_status		err;
496 	struct ifnet		*ifp;
497 	usb_interface_descriptor_t	*id;
498 	usb_endpoint_descriptor_t	*ed;
499 	int			i;
500 
501 	DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev));
502 
503 	usbd_devinfo(dev, 0, devinfo);
504 	USB_ATTACH_SETUP;
505 	printf("%s: %s\n", USBDEVNAME(sc->cue_dev), devinfo);
506 
507 	err = usbd_set_config_no(dev, CUE_CONFIG_NO, 1);
508 	if (err) {
509 		printf("%s: setting config no failed\n",
510 		    USBDEVNAME(sc->cue_dev));
511 		USB_ATTACH_ERROR_RETURN;
512 	}
513 
514 	sc->cue_udev = dev;
515 	sc->cue_product = uaa->product;
516 	sc->cue_vendor = uaa->vendor;
517 
518 	usb_init_task(&sc->cue_tick_task, cue_tick_task, sc);
519 	usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc);
520 
521 	err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface);
522 	if (err) {
523 		printf("%s: getting interface handle failed\n",
524 		    USBDEVNAME(sc->cue_dev));
525 		USB_ATTACH_ERROR_RETURN;
526 	}
527 
528 	sc->cue_iface = iface;
529 	id = usbd_get_interface_descriptor(iface);
530 
531 	/* Find endpoints. */
532 	for (i = 0; i < id->bNumEndpoints; i++) {
533 		ed = usbd_interface2endpoint_descriptor(iface, i);
534 		if (ed == NULL) {
535 			printf("%s: couldn't get ep %d\n",
536 			    USBDEVNAME(sc->cue_dev), i);
537 			USB_ATTACH_ERROR_RETURN;
538 		}
539 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
540 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
541 			sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
542 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
543 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
544 			sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
545 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
546 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
547 			sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
548 		}
549 	}
550 
551 #if 0
552 	/* Reset the adapter. */
553 	cue_reset(sc);
554 #endif
555 	/*
556 	 * Get station address.
557 	 */
558 	cue_getmac(sc, &eaddr);
559 
560 	s = splnet();
561 
562 	/*
563 	 * A CATC chip was detected. Inform the world.
564 	 */
565 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->cue_dev),
566 	    ether_sprintf(eaddr));
567 
568 #if defined(__OpenBSD__)
569 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
570 #endif
571 
572 	/* Initialize interface info.*/
573 	ifp = GET_IFP(sc);
574 	ifp->if_softc = sc;
575 	ifp->if_mtu = ETHERMTU;
576 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
577 	ifp->if_ioctl = cue_ioctl;
578 	ifp->if_start = cue_start;
579 	ifp->if_watchdog = cue_watchdog;
580 	IFQ_SET_READY(&ifp->if_snd);
581 	strncpy(ifp->if_xname, USBDEVNAME(sc->cue_dev), IFNAMSIZ);
582 
583 	/* Attach the interface. */
584 	if_attach(ifp);
585 	Ether_ifattach(ifp, eaddr);
586 #if NRND > 0
587 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->cue_dev),
588 	    RND_TYPE_NET, 0);
589 #endif
590 
591 	usb_callout_init(sc->cue_stat_ch);
592 
593 	sc->cue_attached = 1;
594 	splx(s);
595 
596 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev,
597 	    USBDEV(sc->cue_dev));
598 
599 	USB_ATTACH_SUCCESS_RETURN;
600 }
601 
602 USB_DETACH(cue)
603 {
604 	USB_DETACH_START(cue, sc);
605 	struct ifnet		*ifp = GET_IFP(sc);
606 	int			s;
607 
608 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
609 
610 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
611 	/*
612 	 * Remove any pending task.  It cannot be executing because it run
613 	 * in the same thread as detach.
614 	 */
615 	usb_rem_task(sc->cue_udev, &sc->cue_tick_task);
616 	usb_rem_task(sc->cue_udev, &sc->cue_stop_task);
617 
618 	if (!sc->cue_attached) {
619 		/* Detached before attached finished, so just bail out. */
620 		return (0);
621 	}
622 
623 	s = splusb();
624 
625 	if (ifp->if_flags & IFF_RUNNING)
626 		cue_stop(sc);
627 
628 #if defined(__NetBSD__)
629 #if NRND > 0
630 	rnd_detach_source(&sc->rnd_source);
631 #endif
632 #if NBPFILTER > 0
633 	bpfdetach(ifp);
634 #endif
635 #endif /* __NetBSD__ */
636 	ether_ifdetach(ifp);
637 
638 	if_detach(ifp);
639 
640 #ifdef DIAGNOSTIC
641 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
642 	    sc->cue_ep[CUE_ENDPT_RX] != NULL ||
643 	    sc->cue_ep[CUE_ENDPT_INTR] != NULL)
644 		printf("%s: detach has active endpoints\n",
645 		       USBDEVNAME(sc->cue_dev));
646 #endif
647 
648 	sc->cue_attached = 0;
649 	splx(s);
650 
651 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev,
652 	    USBDEV(sc->cue_dev));
653 
654 	return (0);
655 }
656 
657 int
658 cue_activate(device_ptr_t self, enum devact act)
659 {
660 	struct cue_softc *sc = (struct cue_softc *)self;
661 
662 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
663 
664 	switch (act) {
665 	case DVACT_ACTIVATE:
666 		return (EOPNOTSUPP);
667 		break;
668 
669 	case DVACT_DEACTIVATE:
670 		/* Deactivate the interface. */
671 		if_deactivate(&sc->cue_ec.ec_if);
672 		sc->cue_dying = 1;
673 		break;
674 	}
675 	return (0);
676 }
677 
678 /*
679  * Initialize an RX descriptor and attach an MBUF cluster.
680  */
681 Static int
682 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
683 {
684 	struct mbuf		*m_new = NULL;
685 
686 	if (m == NULL) {
687 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
688 		if (m_new == NULL) {
689 			printf("%s: no memory for rx list "
690 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
691 			return (ENOBUFS);
692 		}
693 
694 		MCLGET(m_new, M_DONTWAIT);
695 		if (!(m_new->m_flags & M_EXT)) {
696 			printf("%s: no memory for rx list "
697 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
698 			m_freem(m_new);
699 			return (ENOBUFS);
700 		}
701 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
702 	} else {
703 		m_new = m;
704 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
705 		m_new->m_data = m_new->m_ext.ext_buf;
706 	}
707 
708 	m_adj(m_new, ETHER_ALIGN);
709 	c->cue_mbuf = m_new;
710 
711 	return (0);
712 }
713 
714 Static int
715 cue_rx_list_init(struct cue_softc *sc)
716 {
717 	struct cue_cdata	*cd;
718 	struct cue_chain	*c;
719 	int			i;
720 
721 	cd = &sc->cue_cdata;
722 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
723 		c = &cd->cue_rx_chain[i];
724 		c->cue_sc = sc;
725 		c->cue_idx = i;
726 		if (cue_newbuf(sc, c, NULL) == ENOBUFS)
727 			return (ENOBUFS);
728 		if (c->cue_xfer == NULL) {
729 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
730 			if (c->cue_xfer == NULL)
731 				return (ENOBUFS);
732 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
733 			if (c->cue_buf == NULL) {
734 				usbd_free_xfer(c->cue_xfer);
735 				return (ENOBUFS);
736 			}
737 		}
738 	}
739 
740 	return (0);
741 }
742 
743 Static int
744 cue_tx_list_init(struct cue_softc *sc)
745 {
746 	struct cue_cdata	*cd;
747 	struct cue_chain	*c;
748 	int			i;
749 
750 	cd = &sc->cue_cdata;
751 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
752 		c = &cd->cue_tx_chain[i];
753 		c->cue_sc = sc;
754 		c->cue_idx = i;
755 		c->cue_mbuf = NULL;
756 		if (c->cue_xfer == NULL) {
757 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
758 			if (c->cue_xfer == NULL)
759 				return (ENOBUFS);
760 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
761 			if (c->cue_buf == NULL) {
762 				usbd_free_xfer(c->cue_xfer);
763 				return (ENOBUFS);
764 			}
765 		}
766 	}
767 
768 	return (0);
769 }
770 
771 /*
772  * A frame has been uploaded: pass the resulting mbuf chain up to
773  * the higher level protocols.
774  */
775 Static void
776 cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
777 {
778 	struct cue_chain	*c = priv;
779 	struct cue_softc	*sc = c->cue_sc;
780 	struct ifnet		*ifp = GET_IFP(sc);
781 	struct mbuf		*m;
782 	int			total_len = 0;
783 	u_int16_t		len;
784 	int			s;
785 
786 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
787 		     __FUNCTION__, status));
788 
789 	if (sc->cue_dying)
790 		return;
791 
792 	if (!(ifp->if_flags & IFF_RUNNING))
793 		return;
794 
795 	if (status != USBD_NORMAL_COMPLETION) {
796 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
797 			return;
798 		sc->cue_rx_errs++;
799 		if (usbd_ratecheck(&sc->cue_rx_notice)) {
800 			printf("%s: %u usb errors on rx: %s\n",
801 			    USBDEVNAME(sc->cue_dev), sc->cue_rx_errs,
802 			    usbd_errstr(status));
803 			sc->cue_rx_errs = 0;
804 		}
805 		if (status == USBD_STALLED)
806 			usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
807 		goto done;
808 	}
809 
810 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
811 
812 	memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
813 
814 	m = c->cue_mbuf;
815 	len = UGETW(mtod(m, u_int8_t *));
816 
817 	/* No errors; receive the packet. */
818 	total_len = len;
819 
820 	if (len < sizeof(struct ether_header)) {
821 		ifp->if_ierrors++;
822 		goto done;
823 	}
824 
825 	ifp->if_ipackets++;
826 	m_adj(m, sizeof(u_int16_t));
827 	m->m_pkthdr.len = m->m_len = total_len;
828 
829 	m->m_pkthdr.rcvif = ifp;
830 
831 	s = splnet();
832 
833 	/* XXX ugly */
834 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
835 		ifp->if_ierrors++;
836 		goto done1;
837 	}
838 
839 #if NBPFILTER > 0
840 	/*
841 	 * Handle BPF listeners. Let the BPF user see the packet, but
842 	 * don't pass it up to the ether_input() layer unless it's
843 	 * a broadcast packet, multicast packet, matches our ethernet
844 	 * address or the interface is in promiscuous mode.
845 	 */
846 	if (ifp->if_bpf)
847 		BPF_MTAP(ifp, m);
848 #endif
849 
850 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->cue_dev),
851 		    __FUNCTION__, m->m_len));
852 	IF_INPUT(ifp, m);
853  done1:
854 	splx(s);
855 
856 done:
857 	/* Setup new transfer. */
858 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
859 	    c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
860 	    USBD_NO_TIMEOUT, cue_rxeof);
861 	usbd_transfer(c->cue_xfer);
862 
863 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->cue_dev),
864 		    __FUNCTION__));
865 }
866 
867 /*
868  * A frame was downloaded to the chip. It's safe for us to clean up
869  * the list buffers.
870  */
871 Static void
872 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
873 {
874 	struct cue_chain	*c = priv;
875 	struct cue_softc	*sc = c->cue_sc;
876 	struct ifnet		*ifp = GET_IFP(sc);
877 	int			s;
878 
879 	if (sc->cue_dying)
880 		return;
881 
882 	s = splnet();
883 
884 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
885 		    __FUNCTION__, status));
886 
887 	ifp->if_timer = 0;
888 	ifp->if_flags &= ~IFF_OACTIVE;
889 
890 	if (status != USBD_NORMAL_COMPLETION) {
891 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
892 			splx(s);
893 			return;
894 		}
895 		ifp->if_oerrors++;
896 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->cue_dev),
897 		    usbd_errstr(status));
898 		if (status == USBD_STALLED)
899 			usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
900 		splx(s);
901 		return;
902 	}
903 
904 	ifp->if_opackets++;
905 
906 	m_freem(c->cue_mbuf);
907 	c->cue_mbuf = NULL;
908 
909 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
910 		cue_start(ifp);
911 
912 	splx(s);
913 }
914 
915 Static void
916 cue_tick(void *xsc)
917 {
918 	struct cue_softc	*sc = xsc;
919 
920 	if (sc == NULL)
921 		return;
922 
923 	if (sc->cue_dying)
924 		return;
925 
926 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
927 
928 	/* Perform statistics update in process context. */
929 	usb_add_task(sc->cue_udev, &sc->cue_tick_task);
930 }
931 
932 Static void
933 cue_tick_task(void *xsc)
934 {
935 	struct cue_softc	*sc = xsc;
936 	struct ifnet		*ifp;
937 
938 	if (sc->cue_dying)
939 		return;
940 
941 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
942 
943 	ifp = GET_IFP(sc);
944 
945 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
946 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
947 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
948 
949 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
950 		ifp->if_ierrors++;
951 }
952 
953 Static int
954 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
955 {
956 	int			total_len;
957 	struct cue_chain	*c;
958 	usbd_status		err;
959 
960 	c = &sc->cue_cdata.cue_tx_chain[idx];
961 
962 	/*
963 	 * Copy the mbuf data into a contiguous buffer, leaving two
964 	 * bytes at the beginning to hold the frame length.
965 	 */
966 	m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
967 	c->cue_mbuf = m;
968 
969 	total_len = m->m_pkthdr.len + 2;
970 
971 	DPRINTFN(10,("%s: %s: total_len=%d\n",
972 		     USBDEVNAME(sc->cue_dev), __FUNCTION__, total_len));
973 
974 	/* The first two bytes are the frame length */
975 	c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
976 	c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
977 
978 	/* XXX 10000 */
979 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
980 	    c, c->cue_buf, total_len, USBD_NO_COPY, 10000, cue_txeof);
981 
982 	/* Transmit */
983 	err = usbd_transfer(c->cue_xfer);
984 	if (err != USBD_IN_PROGRESS) {
985 		printf("%s: cue_send error=%s\n", USBDEVNAME(sc->cue_dev),
986 		       usbd_errstr(err));
987 		/* Stop the interface from process context. */
988 		usb_add_task(sc->cue_udev, &sc->cue_stop_task);
989 		return (EIO);
990 	}
991 
992 	sc->cue_cdata.cue_tx_cnt++;
993 
994 	return (0);
995 }
996 
997 Static void
998 cue_start(struct ifnet *ifp)
999 {
1000 	struct cue_softc	*sc = ifp->if_softc;
1001 	struct mbuf		*m_head = NULL;
1002 
1003 	if (sc->cue_dying)
1004 		return;
1005 
1006 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1007 
1008 	if (ifp->if_flags & IFF_OACTIVE)
1009 		return;
1010 
1011 	IFQ_POLL(&ifp->if_snd, m_head);
1012 	if (m_head == NULL)
1013 		return;
1014 
1015 	if (cue_send(sc, m_head, 0)) {
1016 		ifp->if_flags |= IFF_OACTIVE;
1017 		return;
1018 	}
1019 
1020 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1021 
1022 #if NBPFILTER > 0
1023 	/*
1024 	 * If there's a BPF listener, bounce a copy of this frame
1025 	 * to him.
1026 	 */
1027 	if (ifp->if_bpf)
1028 		BPF_MTAP(ifp, m_head);
1029 #endif
1030 
1031 	ifp->if_flags |= IFF_OACTIVE;
1032 
1033 	/*
1034 	 * Set a timeout in case the chip goes out to lunch.
1035 	 */
1036 	ifp->if_timer = 5;
1037 }
1038 
1039 Static void
1040 cue_init(void *xsc)
1041 {
1042 	struct cue_softc	*sc = xsc;
1043 	struct ifnet		*ifp = GET_IFP(sc);
1044 	int			i, s, ctl;
1045 	u_char			*eaddr;
1046 
1047 	if (sc->cue_dying)
1048 		return;
1049 
1050 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1051 
1052 	if (ifp->if_flags & IFF_RUNNING)
1053 		return;
1054 
1055 	s = splnet();
1056 
1057 	/*
1058 	 * Cancel pending I/O and free all RX/TX buffers.
1059 	 */
1060 #if 1
1061 	cue_reset(sc);
1062 #endif
1063 
1064 	/* Set advanced operation modes. */
1065 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1066 	    CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
1067 
1068 #if defined(__OpenBSD__)
1069 	eaddr = sc->arpcom.ac_enaddr;
1070 #elif defined(__NetBSD__)
1071 	eaddr = LLADDR(ifp->if_sadl);
1072 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1073 	/* Set MAC address */
1074 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1075 		cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
1076 
1077 	/* Enable RX logic. */
1078 	ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
1079 	if (ifp->if_flags & IFF_PROMISC)
1080 		ctl |= CUE_ETHCTL_PROMISC;
1081 	cue_csr_write_1(sc, CUE_ETHCTL, ctl);
1082 
1083 	/* Init TX ring. */
1084 	if (cue_tx_list_init(sc) == ENOBUFS) {
1085 		printf("%s: tx list init failed\n", USBDEVNAME(sc->cue_dev));
1086 		splx(s);
1087 		return;
1088 	}
1089 
1090 	/* Init RX ring. */
1091 	if (cue_rx_list_init(sc) == ENOBUFS) {
1092 		printf("%s: rx list init failed\n", USBDEVNAME(sc->cue_dev));
1093 		splx(s);
1094 		return;
1095 	}
1096 
1097 	/* Load the multicast filter. */
1098 	cue_setmulti(sc);
1099 
1100 	/*
1101 	 * Set the number of RX and TX buffers that we want
1102 	 * to reserve inside the ASIC.
1103 	 */
1104 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
1105 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
1106 
1107 	/* Set advanced operation modes. */
1108 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1109 	    CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
1110 
1111 	/* Program the LED operation. */
1112 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
1113 
1114 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
1115 		if (cue_open_pipes(sc)) {
1116 			splx(s);
1117 			return;
1118 		}
1119 	}
1120 
1121 	ifp->if_flags |= IFF_RUNNING;
1122 	ifp->if_flags &= ~IFF_OACTIVE;
1123 
1124 	splx(s);
1125 
1126 	usb_callout(sc->cue_stat_ch, hz, cue_tick, sc);
1127 }
1128 
1129 Static int
1130 cue_open_pipes(struct cue_softc *sc)
1131 {
1132 	struct cue_chain	*c;
1133 	usbd_status		err;
1134 	int			i;
1135 
1136 	/* Open RX and TX pipes. */
1137 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
1138 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
1139 	if (err) {
1140 		printf("%s: open rx pipe failed: %s\n",
1141 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1142 		return (EIO);
1143 	}
1144 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
1145 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
1146 	if (err) {
1147 		printf("%s: open tx pipe failed: %s\n",
1148 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1149 		return (EIO);
1150 	}
1151 
1152 	/* Start up the receive pipe. */
1153 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1154 		c = &sc->cue_cdata.cue_rx_chain[i];
1155 		usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
1156 		    c, c->cue_buf, CUE_BUFSZ,
1157 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1158 		    cue_rxeof);
1159 		usbd_transfer(c->cue_xfer);
1160 	}
1161 
1162 	return (0);
1163 }
1164 
1165 Static int
1166 cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1167 {
1168 	struct cue_softc	*sc = ifp->if_softc;
1169 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1170 	struct ifreq		*ifr = (struct ifreq *)data;
1171 	int			s, error = 0;
1172 
1173 	if (sc->cue_dying)
1174 		return (EIO);
1175 
1176 	s = splnet();
1177 
1178 	switch(command) {
1179 	case SIOCSIFADDR:
1180 		ifp->if_flags |= IFF_UP;
1181 		cue_init(sc);
1182 
1183 		switch (ifa->ifa_addr->sa_family) {
1184 #ifdef INET
1185 		case AF_INET:
1186 #if defined(__NetBSD__)
1187 			arp_ifinit(ifp, ifa);
1188 #else
1189 			arp_ifinit(&sc->arpcom, ifa);
1190 #endif
1191 			break;
1192 #endif /* INET */
1193 #ifdef NS
1194 		case AF_NS:
1195 		    {
1196 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1197 
1198 			if (ns_nullhost(*ina))
1199 				ina->x_host = *(union ns_host *)
1200 					LLADDR(ifp->if_sadl);
1201 			else
1202 				memcpy(LLADDR(ifp->if_sadl),
1203 				       ina->x_host.c_host,
1204 				       ifp->if_addrlen);
1205 			break;
1206 		    }
1207 #endif /* NS */
1208 		}
1209 		break;
1210 
1211 	case SIOCSIFMTU:
1212 		if (ifr->ifr_mtu > ETHERMTU)
1213 			error = EINVAL;
1214 		else
1215 			ifp->if_mtu = ifr->ifr_mtu;
1216 		break;
1217 
1218 	case SIOCSIFFLAGS:
1219 		if (ifp->if_flags & IFF_UP) {
1220 			if (ifp->if_flags & IFF_RUNNING &&
1221 			    ifp->if_flags & IFF_PROMISC &&
1222 			    !(sc->cue_if_flags & IFF_PROMISC)) {
1223 				CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1224 				cue_setmulti(sc);
1225 			} else if (ifp->if_flags & IFF_RUNNING &&
1226 			    !(ifp->if_flags & IFF_PROMISC) &&
1227 			    sc->cue_if_flags & IFF_PROMISC) {
1228 				CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1229 				cue_setmulti(sc);
1230 			} else if (!(ifp->if_flags & IFF_RUNNING))
1231 				cue_init(sc);
1232 		} else {
1233 			if (ifp->if_flags & IFF_RUNNING)
1234 				cue_stop(sc);
1235 		}
1236 		sc->cue_if_flags = ifp->if_flags;
1237 		error = 0;
1238 		break;
1239 	case SIOCADDMULTI:
1240 	case SIOCDELMULTI:
1241 		cue_setmulti(sc);
1242 		error = 0;
1243 		break;
1244 	default:
1245 		error = EINVAL;
1246 		break;
1247 	}
1248 
1249 	splx(s);
1250 
1251 	return (error);
1252 }
1253 
1254 Static void
1255 cue_watchdog(struct ifnet *ifp)
1256 {
1257 	struct cue_softc	*sc = ifp->if_softc;
1258 	struct cue_chain	*c;
1259 	usbd_status		stat;
1260 	int			s;
1261 
1262 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1263 
1264 	if (sc->cue_dying)
1265 		return;
1266 
1267 	ifp->if_oerrors++;
1268 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->cue_dev));
1269 
1270 	s = splusb();
1271 	c = &sc->cue_cdata.cue_tx_chain[0];
1272 	usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1273 	cue_txeof(c->cue_xfer, c, stat);
1274 
1275 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1276 		cue_start(ifp);
1277 	splx(s);
1278 }
1279 
1280 /*
1281  * Stop the adapter and free any mbufs allocated to the
1282  * RX and TX lists.
1283  */
1284 Static void
1285 cue_stop(struct cue_softc *sc)
1286 {
1287 	usbd_status		err;
1288 	struct ifnet		*ifp;
1289 	int			i;
1290 
1291 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1292 
1293 	ifp = GET_IFP(sc);
1294 	ifp->if_timer = 0;
1295 
1296 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
1297 	cue_reset(sc);
1298 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
1299 
1300 	/* Stop transfers. */
1301 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1302 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1303 		if (err) {
1304 			printf("%s: abort rx pipe failed: %s\n",
1305 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1306 		}
1307 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1308 		if (err) {
1309 			printf("%s: close rx pipe failed: %s\n",
1310 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1311 		}
1312 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
1313 	}
1314 
1315 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1316 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1317 		if (err) {
1318 			printf("%s: abort tx pipe failed: %s\n",
1319 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1320 		}
1321 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1322 		if (err) {
1323 			printf("%s: close tx pipe failed: %s\n",
1324 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1325 		}
1326 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
1327 	}
1328 
1329 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1330 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1331 		if (err) {
1332 			printf("%s: abort intr pipe failed: %s\n",
1333 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1334 		}
1335 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1336 		if (err) {
1337 			printf("%s: close intr pipe failed: %s\n",
1338 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1339 		}
1340 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1341 	}
1342 
1343 	/* Free RX resources. */
1344 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1345 		if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1346 			m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1347 			sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1348 		}
1349 		if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1350 			usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1351 			sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1352 		}
1353 	}
1354 
1355 	/* Free TX resources. */
1356 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1357 		if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1358 			m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1359 			sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1360 		}
1361 		if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1362 			usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1363 			sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1364 		}
1365 	}
1366 
1367 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1368 }
1369