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