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