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