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