xref: /openbsd-src/sys/dev/usb/if_cue.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: if_cue.c,v 1.11 2001/10/31 04:24:44 nate Exp $ */
2 /*	$NetBSD: if_cue.c,v 1.35 2001/04/13 23:30:09 thorpej 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 	strncpy(ifp->if_xname, USBDEVNAME(sc->cue_dev), IFNAMSIZ);
581 
582 	IFQ_SET_READY(&ifp->if_snd);
583 
584 	/* Attach the interface. */
585 	if_attach(ifp);
586 	Ether_ifattach(ifp, eaddr);
587 #if NRND > 0
588 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->cue_dev),
589 	    RND_TYPE_NET, 0);
590 #endif
591 
592 	usb_callout_init(sc->cue_stat_ch);
593 
594 	sc->cue_attached = 1;
595 	splx(s);
596 
597 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev,
598 	    USBDEV(sc->cue_dev));
599 
600 	USB_ATTACH_SUCCESS_RETURN;
601 }
602 
603 USB_DETACH(cue)
604 {
605 	USB_DETACH_START(cue, sc);
606 	struct ifnet		*ifp = GET_IFP(sc);
607 	int			s;
608 
609 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
610 
611 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
612 	/*
613 	 * Remove any pending task.  It cannot be executing because it run
614 	 * in the same thread as detach.
615 	 */
616 	usb_rem_task(sc->cue_udev, &sc->cue_tick_task);
617 	usb_rem_task(sc->cue_udev, &sc->cue_stop_task);
618 
619 	if (!sc->cue_attached) {
620 		/* Detached before attached finished, so just bail out. */
621 		return (0);
622 	}
623 
624 	s = splusb();
625 
626 	if (ifp->if_flags & IFF_RUNNING)
627 		cue_stop(sc);
628 
629 #if defined(__NetBSD__)
630 #if NRND > 0
631 	rnd_detach_source(&sc->rnd_source);
632 #endif
633 #if NBPFILTER > 0
634 	bpfdetach(ifp);
635 #endif
636 #endif /* __NetBSD__ */
637 	ether_ifdetach(ifp);
638 
639 	if_detach(ifp);
640 
641 #ifdef DIAGNOSTIC
642 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
643 	    sc->cue_ep[CUE_ENDPT_RX] != NULL ||
644 	    sc->cue_ep[CUE_ENDPT_INTR] != NULL)
645 		printf("%s: detach has active endpoints\n",
646 		       USBDEVNAME(sc->cue_dev));
647 #endif
648 
649 	sc->cue_attached = 0;
650 	splx(s);
651 
652 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev,
653 	    USBDEV(sc->cue_dev));
654 
655 	return (0);
656 }
657 
658 int
659 cue_activate(device_ptr_t self, enum devact act)
660 {
661 	struct cue_softc *sc = (struct cue_softc *)self;
662 
663 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
664 
665 	switch (act) {
666 	case DVACT_ACTIVATE:
667 		return (EOPNOTSUPP);
668 		break;
669 
670 	case DVACT_DEACTIVATE:
671 		/* Deactivate the interface. */
672 		if_deactivate(&sc->cue_ec.ec_if);
673 		sc->cue_dying = 1;
674 		break;
675 	}
676 	return (0);
677 }
678 
679 /*
680  * Initialize an RX descriptor and attach an MBUF cluster.
681  */
682 Static int
683 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
684 {
685 	struct mbuf		*m_new = NULL;
686 
687 	if (m == NULL) {
688 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
689 		if (m_new == NULL) {
690 			printf("%s: no memory for rx list "
691 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
692 			return (ENOBUFS);
693 		}
694 
695 		MCLGET(m_new, M_DONTWAIT);
696 		if (!(m_new->m_flags & M_EXT)) {
697 			printf("%s: no memory for rx list "
698 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
699 			m_freem(m_new);
700 			return (ENOBUFS);
701 		}
702 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
703 	} else {
704 		m_new = m;
705 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
706 		m_new->m_data = m_new->m_ext.ext_buf;
707 	}
708 
709 	m_adj(m_new, ETHER_ALIGN);
710 	c->cue_mbuf = m_new;
711 
712 	return (0);
713 }
714 
715 Static int
716 cue_rx_list_init(struct cue_softc *sc)
717 {
718 	struct cue_cdata	*cd;
719 	struct cue_chain	*c;
720 	int			i;
721 
722 	cd = &sc->cue_cdata;
723 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
724 		c = &cd->cue_rx_chain[i];
725 		c->cue_sc = sc;
726 		c->cue_idx = i;
727 		if (cue_newbuf(sc, c, NULL) == ENOBUFS)
728 			return (ENOBUFS);
729 		if (c->cue_xfer == NULL) {
730 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
731 			if (c->cue_xfer == NULL)
732 				return (ENOBUFS);
733 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
734 			if (c->cue_buf == NULL) {
735 				usbd_free_xfer(c->cue_xfer);
736 				return (ENOBUFS);
737 			}
738 		}
739 	}
740 
741 	return (0);
742 }
743 
744 Static int
745 cue_tx_list_init(struct cue_softc *sc)
746 {
747 	struct cue_cdata	*cd;
748 	struct cue_chain	*c;
749 	int			i;
750 
751 	cd = &sc->cue_cdata;
752 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
753 		c = &cd->cue_tx_chain[i];
754 		c->cue_sc = sc;
755 		c->cue_idx = i;
756 		c->cue_mbuf = NULL;
757 		if (c->cue_xfer == NULL) {
758 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
759 			if (c->cue_xfer == NULL)
760 				return (ENOBUFS);
761 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
762 			if (c->cue_buf == NULL) {
763 				usbd_free_xfer(c->cue_xfer);
764 				return (ENOBUFS);
765 			}
766 		}
767 	}
768 
769 	return (0);
770 }
771 
772 /*
773  * A frame has been uploaded: pass the resulting mbuf chain up to
774  * the higher level protocols.
775  */
776 Static void
777 cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
778 {
779 	struct cue_chain	*c = priv;
780 	struct cue_softc	*sc = c->cue_sc;
781 	struct ifnet		*ifp = GET_IFP(sc);
782 	struct mbuf		*m;
783 	int			total_len = 0;
784 	u_int16_t		len;
785 	int			s;
786 
787 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
788 		     __FUNCTION__, status));
789 
790 	if (sc->cue_dying)
791 		return;
792 
793 	if (!(ifp->if_flags & IFF_RUNNING))
794 		return;
795 
796 	if (status != USBD_NORMAL_COMPLETION) {
797 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
798 			return;
799 		sc->cue_rx_errs++;
800 		if (usbd_ratecheck(&sc->cue_rx_notice)) {
801 			printf("%s: %u usb errors on rx: %s\n",
802 			    USBDEVNAME(sc->cue_dev), sc->cue_rx_errs,
803 			    usbd_errstr(status));
804 			sc->cue_rx_errs = 0;
805 		}
806 		if (status == USBD_STALLED)
807 			usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
808 		goto done;
809 	}
810 
811 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
812 
813 	memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
814 
815 	m = c->cue_mbuf;
816 	len = UGETW(mtod(m, u_int8_t *));
817 
818 	/* No errors; receive the packet. */
819 	total_len = len;
820 
821 	if (len < sizeof(struct ether_header)) {
822 		ifp->if_ierrors++;
823 		goto done;
824 	}
825 
826 	ifp->if_ipackets++;
827 	m_adj(m, sizeof(u_int16_t));
828 	m->m_pkthdr.len = m->m_len = total_len;
829 
830 	m->m_pkthdr.rcvif = ifp;
831 
832 	s = splnet();
833 
834 	/* XXX ugly */
835 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
836 		ifp->if_ierrors++;
837 		goto done1;
838 	}
839 
840 #if NBPFILTER > 0
841 	/*
842 	 * Handle BPF listeners. Let the BPF user see the packet, but
843 	 * don't pass it up to the ether_input() layer unless it's
844 	 * a broadcast packet, multicast packet, matches our ethernet
845 	 * address or the interface is in promiscuous mode.
846 	 */
847 	if (ifp->if_bpf)
848 		BPF_MTAP(ifp, m);
849 #endif
850 
851 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->cue_dev),
852 		    __FUNCTION__, m->m_len));
853 	IF_INPUT(ifp, m);
854  done1:
855 	splx(s);
856 
857 done:
858 	/* Setup new transfer. */
859 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
860 	    c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
861 	    USBD_NO_TIMEOUT, cue_rxeof);
862 	usbd_transfer(c->cue_xfer);
863 
864 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->cue_dev),
865 		    __FUNCTION__));
866 }
867 
868 /*
869  * A frame was downloaded to the chip. It's safe for us to clean up
870  * the list buffers.
871  */
872 Static void
873 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
874 {
875 	struct cue_chain	*c = priv;
876 	struct cue_softc	*sc = c->cue_sc;
877 	struct ifnet		*ifp = GET_IFP(sc);
878 	int			s;
879 
880 	if (sc->cue_dying)
881 		return;
882 
883 	s = splnet();
884 
885 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
886 		    __FUNCTION__, status));
887 
888 	ifp->if_timer = 0;
889 	ifp->if_flags &= ~IFF_OACTIVE;
890 
891 	if (status != USBD_NORMAL_COMPLETION) {
892 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
893 			splx(s);
894 			return;
895 		}
896 		ifp->if_oerrors++;
897 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->cue_dev),
898 		    usbd_errstr(status));
899 		if (status == USBD_STALLED)
900 			usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
901 		splx(s);
902 		return;
903 	}
904 
905 	ifp->if_opackets++;
906 
907 	m_freem(c->cue_mbuf);
908 	c->cue_mbuf = NULL;
909 
910 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
911 		cue_start(ifp);
912 
913 	splx(s);
914 }
915 
916 Static void
917 cue_tick(void *xsc)
918 {
919 	struct cue_softc	*sc = xsc;
920 
921 	if (sc == NULL)
922 		return;
923 
924 	if (sc->cue_dying)
925 		return;
926 
927 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
928 
929 	/* Perform statistics update in process context. */
930 	usb_add_task(sc->cue_udev, &sc->cue_tick_task);
931 }
932 
933 Static void
934 cue_tick_task(void *xsc)
935 {
936 	struct cue_softc	*sc = xsc;
937 	struct ifnet		*ifp;
938 
939 	if (sc->cue_dying)
940 		return;
941 
942 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
943 
944 	ifp = GET_IFP(sc);
945 
946 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
947 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
948 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
949 
950 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
951 		ifp->if_ierrors++;
952 }
953 
954 Static int
955 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
956 {
957 	int			total_len;
958 	struct cue_chain	*c;
959 	usbd_status		err;
960 
961 	c = &sc->cue_cdata.cue_tx_chain[idx];
962 
963 	/*
964 	 * Copy the mbuf data into a contiguous buffer, leaving two
965 	 * bytes at the beginning to hold the frame length.
966 	 */
967 	m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
968 	c->cue_mbuf = m;
969 
970 	total_len = m->m_pkthdr.len + 2;
971 
972 	DPRINTFN(10,("%s: %s: total_len=%d\n",
973 		     USBDEVNAME(sc->cue_dev), __FUNCTION__, total_len));
974 
975 	/* The first two bytes are the frame length */
976 	c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
977 	c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
978 
979 	/* XXX 10000 */
980 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
981 	    c, c->cue_buf, total_len, USBD_NO_COPY, 10000, cue_txeof);
982 
983 	/* Transmit */
984 	err = usbd_transfer(c->cue_xfer);
985 	if (err != USBD_IN_PROGRESS) {
986 		printf("%s: cue_send error=%s\n", USBDEVNAME(sc->cue_dev),
987 		       usbd_errstr(err));
988 		/* Stop the interface from process context. */
989 		usb_add_task(sc->cue_udev, &sc->cue_stop_task);
990 		return (EIO);
991 	}
992 
993 	sc->cue_cdata.cue_tx_cnt++;
994 
995 	return (0);
996 }
997 
998 Static void
999 cue_start(struct ifnet *ifp)
1000 {
1001 	struct cue_softc	*sc = ifp->if_softc;
1002 	struct mbuf		*m_head = NULL;
1003 
1004 	if (sc->cue_dying)
1005 		return;
1006 
1007 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1008 
1009 	if (ifp->if_flags & IFF_OACTIVE)
1010 		return;
1011 
1012 	IFQ_POLL(&ifp->if_snd, m_head);
1013 	if (m_head == NULL)
1014 		return;
1015 
1016 	if (cue_send(sc, m_head, 0)) {
1017 		ifp->if_flags |= IFF_OACTIVE;
1018 		return;
1019 	}
1020 
1021 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1022 
1023 #if NBPFILTER > 0
1024 	/*
1025 	 * If there's a BPF listener, bounce a copy of this frame
1026 	 * to him.
1027 	 */
1028 	if (ifp->if_bpf)
1029 		BPF_MTAP(ifp, m_head);
1030 #endif
1031 
1032 	ifp->if_flags |= IFF_OACTIVE;
1033 
1034 	/*
1035 	 * Set a timeout in case the chip goes out to lunch.
1036 	 */
1037 	ifp->if_timer = 5;
1038 }
1039 
1040 Static void
1041 cue_init(void *xsc)
1042 {
1043 	struct cue_softc	*sc = xsc;
1044 	struct ifnet		*ifp = GET_IFP(sc);
1045 	int			i, s, ctl;
1046 	u_char			*eaddr;
1047 
1048 	if (sc->cue_dying)
1049 		return;
1050 
1051 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1052 
1053 	if (ifp->if_flags & IFF_RUNNING)
1054 		return;
1055 
1056 	s = splnet();
1057 
1058 	/*
1059 	 * Cancel pending I/O and free all RX/TX buffers.
1060 	 */
1061 #if 1
1062 	cue_reset(sc);
1063 #endif
1064 
1065 	/* Set advanced operation modes. */
1066 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1067 	    CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
1068 
1069 #if defined(__OpenBSD__)
1070 	eaddr = sc->arpcom.ac_enaddr;
1071 #elif defined(__NetBSD__)
1072 	eaddr = LLADDR(ifp->if_sadl);
1073 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1074 	/* Set MAC address */
1075 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1076 		cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
1077 
1078 	/* Enable RX logic. */
1079 	ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
1080 	if (ifp->if_flags & IFF_PROMISC)
1081 		ctl |= CUE_ETHCTL_PROMISC;
1082 	cue_csr_write_1(sc, CUE_ETHCTL, ctl);
1083 
1084 	/* Init TX ring. */
1085 	if (cue_tx_list_init(sc) == ENOBUFS) {
1086 		printf("%s: tx list init failed\n", USBDEVNAME(sc->cue_dev));
1087 		splx(s);
1088 		return;
1089 	}
1090 
1091 	/* Init RX ring. */
1092 	if (cue_rx_list_init(sc) == ENOBUFS) {
1093 		printf("%s: rx list init failed\n", USBDEVNAME(sc->cue_dev));
1094 		splx(s);
1095 		return;
1096 	}
1097 
1098 	/* Load the multicast filter. */
1099 	cue_setmulti(sc);
1100 
1101 	/*
1102 	 * Set the number of RX and TX buffers that we want
1103 	 * to reserve inside the ASIC.
1104 	 */
1105 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
1106 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
1107 
1108 	/* Set advanced operation modes. */
1109 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1110 	    CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
1111 
1112 	/* Program the LED operation. */
1113 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
1114 
1115 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
1116 		if (cue_open_pipes(sc)) {
1117 			splx(s);
1118 			return;
1119 		}
1120 	}
1121 
1122 	ifp->if_flags |= IFF_RUNNING;
1123 	ifp->if_flags &= ~IFF_OACTIVE;
1124 
1125 	splx(s);
1126 
1127 	usb_callout(sc->cue_stat_ch, hz, cue_tick, sc);
1128 }
1129 
1130 Static int
1131 cue_open_pipes(struct cue_softc *sc)
1132 {
1133 	struct cue_chain	*c;
1134 	usbd_status		err;
1135 	int			i;
1136 
1137 	/* Open RX and TX pipes. */
1138 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
1139 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
1140 	if (err) {
1141 		printf("%s: open rx pipe failed: %s\n",
1142 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1143 		return (EIO);
1144 	}
1145 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
1146 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
1147 	if (err) {
1148 		printf("%s: open tx pipe failed: %s\n",
1149 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1150 		return (EIO);
1151 	}
1152 
1153 	/* Start up the receive pipe. */
1154 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1155 		c = &sc->cue_cdata.cue_rx_chain[i];
1156 		usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
1157 		    c, c->cue_buf, CUE_BUFSZ,
1158 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1159 		    cue_rxeof);
1160 		usbd_transfer(c->cue_xfer);
1161 	}
1162 
1163 	return (0);
1164 }
1165 
1166 Static int
1167 cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1168 {
1169 	struct cue_softc	*sc = ifp->if_softc;
1170 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1171 	struct ifreq		*ifr = (struct ifreq *)data;
1172 	int			s, error = 0;
1173 
1174 	if (sc->cue_dying)
1175 		return (EIO);
1176 
1177 	s = splnet();
1178 
1179 	switch(command) {
1180 	case SIOCSIFADDR:
1181 		ifp->if_flags |= IFF_UP;
1182 		cue_init(sc);
1183 
1184 		switch (ifa->ifa_addr->sa_family) {
1185 #ifdef INET
1186 		case AF_INET:
1187 #if defined(__NetBSD__)
1188 			arp_ifinit(ifp, ifa);
1189 #else
1190 			arp_ifinit(&sc->arpcom, ifa);
1191 #endif
1192 			break;
1193 #endif /* INET */
1194 #ifdef NS
1195 		case AF_NS:
1196 		    {
1197 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1198 
1199 			if (ns_nullhost(*ina))
1200 				ina->x_host = *(union ns_host *)
1201 					LLADDR(ifp->if_sadl);
1202 			else
1203 				memcpy(LLADDR(ifp->if_sadl),
1204 				       ina->x_host.c_host,
1205 				       ifp->if_addrlen);
1206 			break;
1207 		    }
1208 #endif /* NS */
1209 		}
1210 		break;
1211 
1212 	case SIOCSIFMTU:
1213 		if (ifr->ifr_mtu > ETHERMTU)
1214 			error = EINVAL;
1215 		else
1216 			ifp->if_mtu = ifr->ifr_mtu;
1217 		break;
1218 
1219 	case SIOCSIFFLAGS:
1220 		if (ifp->if_flags & IFF_UP) {
1221 			if (ifp->if_flags & IFF_RUNNING &&
1222 			    ifp->if_flags & IFF_PROMISC &&
1223 			    !(sc->cue_if_flags & IFF_PROMISC)) {
1224 				CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1225 				cue_setmulti(sc);
1226 			} else if (ifp->if_flags & IFF_RUNNING &&
1227 			    !(ifp->if_flags & IFF_PROMISC) &&
1228 			    sc->cue_if_flags & IFF_PROMISC) {
1229 				CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1230 				cue_setmulti(sc);
1231 			} else if (!(ifp->if_flags & IFF_RUNNING))
1232 				cue_init(sc);
1233 		} else {
1234 			if (ifp->if_flags & IFF_RUNNING)
1235 				cue_stop(sc);
1236 		}
1237 		sc->cue_if_flags = ifp->if_flags;
1238 		error = 0;
1239 		break;
1240 	case SIOCADDMULTI:
1241 	case SIOCDELMULTI:
1242 		cue_setmulti(sc);
1243 		error = 0;
1244 		break;
1245 	default:
1246 		error = EINVAL;
1247 		break;
1248 	}
1249 
1250 	splx(s);
1251 
1252 	return (error);
1253 }
1254 
1255 Static void
1256 cue_watchdog(struct ifnet *ifp)
1257 {
1258 	struct cue_softc	*sc = ifp->if_softc;
1259 	struct cue_chain	*c;
1260 	usbd_status		stat;
1261 	int			s;
1262 
1263 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1264 
1265 	if (sc->cue_dying)
1266 		return;
1267 
1268 	ifp->if_oerrors++;
1269 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->cue_dev));
1270 
1271 	s = splusb();
1272 	c = &sc->cue_cdata.cue_tx_chain[0];
1273 	usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1274 	cue_txeof(c->cue_xfer, c, stat);
1275 
1276 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1277 		cue_start(ifp);
1278 	splx(s);
1279 }
1280 
1281 /*
1282  * Stop the adapter and free any mbufs allocated to the
1283  * RX and TX lists.
1284  */
1285 Static void
1286 cue_stop(struct cue_softc *sc)
1287 {
1288 	usbd_status		err;
1289 	struct ifnet		*ifp;
1290 	int			i;
1291 
1292 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1293 
1294 	ifp = GET_IFP(sc);
1295 	ifp->if_timer = 0;
1296 
1297 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
1298 	cue_reset(sc);
1299 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
1300 
1301 	/* Stop transfers. */
1302 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1303 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1304 		if (err) {
1305 			printf("%s: abort rx pipe failed: %s\n",
1306 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1307 		}
1308 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1309 		if (err) {
1310 			printf("%s: close rx pipe failed: %s\n",
1311 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1312 		}
1313 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
1314 	}
1315 
1316 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1317 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1318 		if (err) {
1319 			printf("%s: abort tx pipe failed: %s\n",
1320 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1321 		}
1322 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1323 		if (err) {
1324 			printf("%s: close tx pipe failed: %s\n",
1325 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1326 		}
1327 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
1328 	}
1329 
1330 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1331 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1332 		if (err) {
1333 			printf("%s: abort intr pipe failed: %s\n",
1334 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1335 		}
1336 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1337 		if (err) {
1338 			printf("%s: close intr pipe failed: %s\n",
1339 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1340 		}
1341 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1342 	}
1343 
1344 	/* Free RX resources. */
1345 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1346 		if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1347 			m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1348 			sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1349 		}
1350 		if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1351 			usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1352 			sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1353 		}
1354 	}
1355 
1356 	/* Free TX resources. */
1357 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1358 		if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1359 			m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1360 			sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1361 		}
1362 		if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1363 			usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1364 			sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1365 		}
1366 	}
1367 
1368 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1369 }
1370