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