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