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