]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/if_cue.c
This commit was generated by cvs2svn to compensate for changes in r60786,
[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 routines.
559          */
560         if_attach(ifp);
561         ether_ifattach(ifp);
562         callout_handle_init(&sc->cue_stat_ch);
563         bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
564         usb_register_netisr();
565         sc->cue_gone = 0;
566
567         splx(s);
568         USB_ATTACH_SUCCESS_RETURN;
569 }
570
571 Static int cue_detach(dev)
572         device_t                dev;
573 {
574         struct cue_softc        *sc;
575         struct ifnet            *ifp;
576         int                     s;
577
578         s = splusb();
579
580         sc = device_get_softc(dev);
581         ifp = &sc->arpcom.ac_if;
582
583         sc->cue_gone = 1;
584         untimeout(cue_tick, sc, sc->cue_stat_ch);
585         bpfdetach(ifp);
586         if_detach(ifp);
587
588         if (sc->cue_ep[CUE_ENDPT_TX] != NULL)
589                 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
590         if (sc->cue_ep[CUE_ENDPT_RX] != NULL)
591                 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
592         if (sc->cue_ep[CUE_ENDPT_INTR] != NULL)
593                 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
594
595         splx(s);
596
597         return(0);
598 }
599
600 /*
601  * Initialize an RX descriptor and attach an MBUF cluster.
602  */
603 Static int cue_newbuf(sc, c, m)
604         struct cue_softc        *sc;
605         struct cue_chain        *c;
606         struct mbuf             *m;
607 {
608         struct mbuf             *m_new = NULL;
609
610         if (m == NULL) {
611                 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
612                 if (m_new == NULL) {
613                         printf("cue%d: no memory for rx list "
614                             "-- packet dropped!\n", sc->cue_unit);
615                         return(ENOBUFS);
616                 }
617
618                 MCLGET(m_new, M_DONTWAIT);
619                 if (!(m_new->m_flags & M_EXT)) {
620                         printf("cue%d: no memory for rx list "
621                             "-- packet dropped!\n", sc->cue_unit);
622                         m_freem(m_new);
623                         return(ENOBUFS);
624                 }
625                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
626         } else {
627                 m_new = m;
628                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
629                 m_new->m_data = m_new->m_ext.ext_buf;
630         }
631
632         m_adj(m_new, ETHER_ALIGN);
633         c->cue_mbuf = m_new;
634
635         return(0);
636 }
637
638 Static int cue_rx_list_init(sc)
639         struct cue_softc        *sc;
640 {
641         struct cue_cdata        *cd;
642         struct cue_chain        *c;
643         int                     i;
644
645         cd = &sc->cue_cdata;
646         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
647                 c = &cd->cue_rx_chain[i];
648                 c->cue_sc = sc;
649                 c->cue_idx = i;
650                 if (cue_newbuf(sc, c, NULL) == ENOBUFS)
651                         return(ENOBUFS);
652                 if (c->cue_xfer == NULL) {
653                         c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
654                         if (c->cue_xfer == NULL)
655                                 return(ENOBUFS);
656                 }
657         }
658
659         return(0);
660 }
661
662 Static int cue_tx_list_init(sc)
663         struct cue_softc        *sc;
664 {
665         struct cue_cdata        *cd;
666         struct cue_chain        *c;
667         int                     i;
668
669         cd = &sc->cue_cdata;
670         for (i = 0; i < CUE_TX_LIST_CNT; i++) {
671                 c = &cd->cue_tx_chain[i];
672                 c->cue_sc = sc;
673                 c->cue_idx = i;
674                 c->cue_mbuf = NULL;
675                 if (c->cue_xfer == NULL) {
676                         c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
677                         if (c->cue_xfer == NULL)
678                                 return(ENOBUFS);
679                 }
680                 c->cue_buf = malloc(CUE_BUFSZ, M_USBDEV, M_NOWAIT);
681                 if (c->cue_buf == NULL)
682                         return(ENOBUFS);
683         }
684
685         return(0);
686 }
687
688 Static void cue_rxstart(ifp)
689         struct ifnet            *ifp;
690 {
691         struct cue_softc        *sc;
692         struct cue_chain        *c;
693
694         sc = ifp->if_softc;
695         c = &sc->cue_cdata.cue_rx_chain[sc->cue_cdata.cue_rx_prod];
696
697         if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
698                 ifp->if_ierrors++;
699                 return;
700         }
701
702         /* Setup new transfer. */
703         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
704             c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
705             USBD_NO_TIMEOUT, cue_rxeof);
706         usbd_transfer(c->cue_xfer);
707
708         return;
709 }
710
711 /*
712  * A frame has been uploaded: pass the resulting mbuf chain up to
713  * the higher level protocols.
714  */
715 Static void cue_rxeof(xfer, priv, status)
716         usbd_xfer_handle        xfer;
717         usbd_private_handle     priv;
718         usbd_status             status;
719 {
720         struct cue_softc        *sc;
721         struct cue_chain        *c;
722         struct mbuf             *m;
723         struct ifnet            *ifp;
724         int                     total_len = 0;
725         u_int16_t               len;
726
727         c = priv;
728         sc = c->cue_sc;
729         ifp = &sc->arpcom.ac_if;
730
731         if (!(ifp->if_flags & IFF_RUNNING))
732                 return;
733
734         if (status != USBD_NORMAL_COMPLETION) {
735                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
736                         return;
737                 printf("cue%d: usb error on rx: %s\n", sc->cue_unit,
738                     usbd_errstr(status));
739                 if (status == USBD_STALLED)
740                         usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
741                 goto done;
742         }
743
744         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
745
746         m = c->cue_mbuf;
747         len = *mtod(m, u_int16_t *);
748
749         /* No errors; receive the packet. */
750         total_len = len;
751
752         if (len < sizeof(struct ether_header)) {
753                 ifp->if_ierrors++;
754                 goto done;
755         }
756
757         ifp->if_ipackets++;
758         m_adj(m, sizeof(u_int16_t));
759         m->m_pkthdr.rcvif = (struct ifnet *)&cue_qdat;
760         m->m_pkthdr.len = m->m_len = total_len;
761
762         /* Put the packet on the special USB input queue. */
763         usb_ether_input(m);
764
765         return;
766 done:
767         /* Setup new transfer. */
768         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
769             c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
770             USBD_NO_TIMEOUT, cue_rxeof);
771         usbd_transfer(c->cue_xfer);
772
773         return;
774 }
775
776 /*
777  * A frame was downloaded to the chip. It's safe for us to clean up
778  * the list buffers.
779  */
780
781 Static void cue_txeof(xfer, priv, status)
782         usbd_xfer_handle        xfer;
783         usbd_private_handle     priv;
784         usbd_status             status;
785 {
786         struct cue_softc        *sc;
787         struct cue_chain        *c;
788         struct ifnet            *ifp;
789         usbd_status             err;
790         int                     s;
791
792         s = splimp();
793
794         c = priv;
795         sc = c->cue_sc;
796         ifp = &sc->arpcom.ac_if;
797
798         if (status != USBD_NORMAL_COMPLETION) {
799                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
800                         splx(s);
801                         return;
802                 }
803                 printf("cue%d: usb error on tx: %s\n", sc->cue_unit,
804                     usbd_errstr(status));
805                 if (status == USBD_STALLED)
806                         usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
807                 splx(s);
808                 return;
809         }
810
811         ifp->if_timer = 0;
812         ifp->if_flags &= ~IFF_OACTIVE;
813         usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &err);
814
815         c->cue_mbuf->m_pkthdr.rcvif = ifp;
816         usb_tx_done(c->cue_mbuf);
817         c->cue_mbuf = NULL;
818
819         if (err)
820                 ifp->if_oerrors++;
821         else
822                 ifp->if_opackets++;
823
824         splx(s);
825
826         return;
827 }
828
829 Static void cue_tick(xsc)
830         void                    *xsc;
831 {
832         struct cue_softc        *sc;
833         struct ifnet            *ifp;
834         int                     s;
835
836         s = splimp();
837
838         sc = xsc;
839
840         if (sc == NULL) {
841                 splx(s);
842                 return;
843         }
844
845         ifp = &sc->arpcom.ac_if;
846
847         ifp->if_collisions += csr_read_2(sc, CUE_TX_SINGLECOLL);
848         ifp->if_collisions += csr_read_2(sc, CUE_TX_MULTICOLL);
849         ifp->if_collisions += csr_read_2(sc, CUE_TX_EXCESSCOLL);
850
851         if (csr_read_2(sc, CUE_RX_FRAMEERR))
852                 ifp->if_ierrors++;
853
854         sc->cue_stat_ch = timeout(cue_tick, sc, hz);
855
856         splx(s);
857
858         return;
859 }
860
861 Static int cue_encap(sc, m, idx)
862         struct cue_softc        *sc;
863         struct mbuf             *m;
864         int                     idx;
865 {
866         int                     total_len;
867         struct cue_chain        *c;
868         usbd_status             err;
869
870         c = &sc->cue_cdata.cue_tx_chain[idx];
871
872         /*
873          * Copy the mbuf data into a contiguous buffer, leaving two
874          * bytes at the beginning to hold the frame length.
875          */
876         m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
877         c->cue_mbuf = m;
878
879         total_len = m->m_pkthdr.len + 2;
880
881         /* The first two bytes are the frame length */
882         c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
883         c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
884
885         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
886             c, c->cue_buf, total_len, 0, 10000, cue_txeof);
887
888         /* Transmit */
889         err = usbd_transfer(c->cue_xfer);
890         if (err != USBD_IN_PROGRESS) {
891                 cue_stop(sc);
892                 return(EIO);
893         }
894
895         sc->cue_cdata.cue_tx_cnt++;
896
897         return(0);
898 }
899
900 Static void cue_start(ifp)
901         struct ifnet            *ifp;
902 {
903         struct cue_softc        *sc;
904         struct mbuf             *m_head = NULL;
905
906         sc = ifp->if_softc;
907
908         if (ifp->if_flags & IFF_OACTIVE)
909                 return;
910
911         IF_DEQUEUE(&ifp->if_snd, m_head);
912         if (m_head == NULL)
913                 return;
914
915         if (cue_encap(sc, m_head, 0)) {
916                 IF_PREPEND(&ifp->if_snd, m_head);
917                 ifp->if_flags |= IFF_OACTIVE;
918                 return;
919         }
920
921         /*
922          * If there's a BPF listener, bounce a copy of this frame
923          * to him.
924          */
925         if (ifp->if_bpf)
926                 bpf_mtap(ifp, m_head);
927
928         ifp->if_flags |= IFF_OACTIVE;
929
930         /*
931          * Set a timeout in case the chip goes out to lunch.
932          */
933         ifp->if_timer = 5;
934
935         return;
936 }
937
938 Static void cue_init(xsc)
939         void                    *xsc;
940 {
941         struct cue_softc        *sc = xsc;
942         struct ifnet            *ifp = &sc->arpcom.ac_if;
943         struct cue_chain        *c;
944         usbd_status             err;
945         int                     i, s;
946
947         if (ifp->if_flags & IFF_RUNNING)
948                 return;
949
950         s = splimp();
951
952         /*
953          * Cancel pending I/O and free all RX/TX buffers.
954          */
955 #ifdef foo
956         cue_reset(sc);
957 #endif
958
959         /* Set MAC address */
960         for (i = 0; i < ETHER_ADDR_LEN; i++)
961                 csr_write_1(sc, CUE_PAR0 - i, sc->arpcom.ac_enaddr[i]);
962
963         /* Enable RX logic. */
964         csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON|CUE_ETHCTL_MCAST_ON);
965
966          /* If we want promiscuous mode, set the allframes bit. */
967         if (ifp->if_flags & IFF_PROMISC) {
968                 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
969         } else {
970                 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
971         }
972
973         /* Init TX ring. */
974         if (cue_tx_list_init(sc) == ENOBUFS) {
975                 printf("cue%d: tx list init failed\n", sc->cue_unit);
976                 splx(s);
977                 return;
978         }
979
980         /* Init RX ring. */
981         if (cue_rx_list_init(sc) == ENOBUFS) {
982                 printf("cue%d: rx list init failed\n", sc->cue_unit);
983                 splx(s);
984                 return;
985         }
986
987         /* Load the multicast filter. */
988         cue_setmulti(sc);
989
990         /*
991          * Set the number of RX and TX buffers that we want
992          * to reserve inside the ASIC.
993          */
994         csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
995         csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
996
997         /* Set advanced operation modes. */
998         csr_write_1(sc, CUE_ADVANCED_OPMODES,
999             CUE_AOP_EMBED_RXLEN|0x01); /* 1 wait state */
1000
1001         /* Program the LED operation. */
1002         csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
1003
1004         /* Open RX and TX pipes. */
1005         err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
1006             USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
1007         if (err) {
1008                 printf("cue%d: open rx pipe failed: %s\n",
1009                     sc->cue_unit, usbd_errstr(err));
1010                 splx(s);
1011                 return;
1012         }
1013         err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
1014             USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
1015         if (err) {
1016                 printf("cue%d: open tx pipe failed: %s\n",
1017                     sc->cue_unit, usbd_errstr(err));
1018                 splx(s);
1019                 return;
1020         }
1021
1022         /* Start up the receive pipe. */
1023         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1024                 c = &sc->cue_cdata.cue_rx_chain[i];
1025                 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
1026                     c, mtod(c->cue_mbuf, char *), CUE_BUFSZ,
1027                     USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
1028                 usbd_transfer(c->cue_xfer);
1029         }
1030
1031         ifp->if_flags |= IFF_RUNNING;
1032         ifp->if_flags &= ~IFF_OACTIVE;
1033
1034         (void)splx(s);
1035
1036         sc->cue_stat_ch = timeout(cue_tick, sc, hz);
1037
1038         return;
1039 }
1040
1041 Static int cue_ioctl(ifp, command, data)
1042         struct ifnet            *ifp;
1043         u_long                  command;
1044         caddr_t                 data;
1045 {
1046         struct cue_softc        *sc = ifp->if_softc;
1047         int                     s, error = 0;
1048
1049         s = splimp();
1050
1051         switch(command) {
1052         case SIOCSIFADDR:
1053         case SIOCGIFADDR:
1054         case SIOCSIFMTU:
1055                 error = ether_ioctl(ifp, command, data);
1056                 break;
1057         case SIOCSIFFLAGS:
1058                 if (ifp->if_flags & IFF_UP) {
1059                         if (ifp->if_flags & IFF_RUNNING &&
1060                             ifp->if_flags & IFF_PROMISC &&
1061                             !(sc->cue_if_flags & IFF_PROMISC)) {
1062                                 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1063                                 cue_setmulti(sc);
1064                         } else if (ifp->if_flags & IFF_RUNNING &&
1065                             !(ifp->if_flags & IFF_PROMISC) &&
1066                             sc->cue_if_flags & IFF_PROMISC) {
1067                                 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1068                                 cue_setmulti(sc);
1069                         } else if (!(ifp->if_flags & IFF_RUNNING))
1070                                 cue_init(sc);
1071                 } else {
1072                         if (ifp->if_flags & IFF_RUNNING)
1073                                 cue_stop(sc);
1074                 }
1075                 sc->cue_if_flags = ifp->if_flags;
1076                 error = 0;
1077                 break;
1078         case SIOCADDMULTI:
1079         case SIOCDELMULTI:
1080                 cue_setmulti(sc);
1081                 error = 0;
1082                 break;
1083         default:
1084                 error = EINVAL;
1085                 break;
1086         }
1087
1088         (void)splx(s);
1089
1090         return(error);
1091 }
1092
1093 Static void cue_watchdog(ifp)
1094         struct ifnet            *ifp;
1095 {
1096         struct cue_softc        *sc;
1097
1098         sc = ifp->if_softc;
1099
1100         ifp->if_oerrors++;
1101         printf("cue%d: watchdog timeout\n", sc->cue_unit);
1102
1103         cue_init(sc);
1104
1105         if (ifp->if_snd.ifq_head != NULL)
1106                 cue_start(ifp);
1107
1108         return;
1109 }
1110
1111 /*
1112  * Stop the adapter and free any mbufs allocated to the
1113  * RX and TX lists.
1114  */
1115 Static void cue_stop(sc)
1116         struct cue_softc        *sc;
1117 {
1118         usbd_status             err;
1119         struct ifnet            *ifp;
1120         int                     i;
1121
1122         ifp = &sc->arpcom.ac_if;
1123         ifp->if_timer = 0;
1124
1125         csr_write_1(sc, CUE_ETHCTL, 0);
1126         cue_reset(sc);
1127         untimeout(cue_tick, sc, sc->cue_stat_ch);
1128
1129         /* Stop transfers. */
1130         if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1131                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1132                 if (err) {
1133                         printf("cue%d: abort rx pipe failed: %s\n",
1134                         sc->cue_unit, usbd_errstr(err));
1135                 }
1136                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1137                 if (err) {
1138                         printf("cue%d: close rx pipe failed: %s\n",
1139                         sc->cue_unit, usbd_errstr(err));
1140                 }
1141                 sc->cue_ep[CUE_ENDPT_RX] = NULL;
1142         }
1143
1144         if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1145                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1146                 if (err) {
1147                         printf("cue%d: abort tx pipe failed: %s\n",
1148                         sc->cue_unit, usbd_errstr(err));
1149                 }
1150                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1151                 if (err) {
1152                         printf("cue%d: close tx pipe failed: %s\n",
1153                             sc->cue_unit, usbd_errstr(err));
1154                 }
1155                 sc->cue_ep[CUE_ENDPT_TX] = NULL;
1156         }
1157
1158         if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1159                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1160                 if (err) {
1161                         printf("cue%d: abort intr pipe failed: %s\n",
1162                         sc->cue_unit, usbd_errstr(err));
1163                 }
1164                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1165                 if (err) {
1166                         printf("cue%d: close intr pipe failed: %s\n",
1167                             sc->cue_unit, usbd_errstr(err));
1168                 }
1169                 sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1170         }
1171
1172         /* Free RX resources. */
1173         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1174                 if (sc->cue_cdata.cue_rx_chain[i].cue_buf != NULL) {
1175                         free(sc->cue_cdata.cue_rx_chain[i].cue_buf, M_USBDEV);
1176                         sc->cue_cdata.cue_rx_chain[i].cue_buf = NULL;
1177                 }
1178                 if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1179                         m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1180                         sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1181                 }
1182                 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1183                         usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1184                         sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1185                 }
1186         }
1187
1188         /* Free TX resources. */
1189         for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1190                 if (sc->cue_cdata.cue_tx_chain[i].cue_buf != NULL) {
1191                         free(sc->cue_cdata.cue_tx_chain[i].cue_buf, M_USBDEV);
1192                         sc->cue_cdata.cue_tx_chain[i].cue_buf = NULL;
1193                 }
1194                 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1195                         m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1196                         sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1197                 }
1198                 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1199                         usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1200                         sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1201                 }
1202         }
1203
1204         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1205
1206         return;
1207 }
1208
1209 /*
1210  * Stop all chip I/O so that the kernel's probe routines don't
1211  * get confused by errant DMAs when rebooting.
1212  */
1213 Static void cue_shutdown(dev)
1214         device_t                dev;
1215 {
1216         struct cue_softc        *sc;
1217
1218         sc = device_get_softc(dev);
1219
1220         cue_reset(sc);
1221         cue_stop(sc);
1222
1223         return;
1224 }