]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/if_cue.c
This commit was generated by cvs2svn to compensate for changes in r61519,
[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         if (c->cue_mbuf != NULL) {
816                 c->cue_mbuf->m_pkthdr.rcvif = ifp;
817                 usb_tx_done(c->cue_mbuf);
818                 c->cue_mbuf = NULL;
819         }
820
821         if (err)
822                 ifp->if_oerrors++;
823         else
824                 ifp->if_opackets++;
825
826         splx(s);
827
828         return;
829 }
830
831 Static void cue_tick(xsc)
832         void                    *xsc;
833 {
834         struct cue_softc        *sc;
835         struct ifnet            *ifp;
836         int                     s;
837
838         s = splimp();
839
840         sc = xsc;
841
842         if (sc == NULL) {
843                 splx(s);
844                 return;
845         }
846
847         ifp = &sc->arpcom.ac_if;
848
849         ifp->if_collisions += csr_read_2(sc, CUE_TX_SINGLECOLL);
850         ifp->if_collisions += csr_read_2(sc, CUE_TX_MULTICOLL);
851         ifp->if_collisions += csr_read_2(sc, CUE_TX_EXCESSCOLL);
852
853         if (csr_read_2(sc, CUE_RX_FRAMEERR))
854                 ifp->if_ierrors++;
855
856         sc->cue_stat_ch = timeout(cue_tick, sc, hz);
857
858         splx(s);
859
860         return;
861 }
862
863 Static int cue_encap(sc, m, idx)
864         struct cue_softc        *sc;
865         struct mbuf             *m;
866         int                     idx;
867 {
868         int                     total_len;
869         struct cue_chain        *c;
870         usbd_status             err;
871
872         c = &sc->cue_cdata.cue_tx_chain[idx];
873
874         /*
875          * Copy the mbuf data into a contiguous buffer, leaving two
876          * bytes at the beginning to hold the frame length.
877          */
878         m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
879         c->cue_mbuf = m;
880
881         total_len = m->m_pkthdr.len + 2;
882
883         /* The first two bytes are the frame length */
884         c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
885         c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
886
887         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
888             c, c->cue_buf, total_len, 0, 10000, cue_txeof);
889
890         /* Transmit */
891         err = usbd_transfer(c->cue_xfer);
892         if (err != USBD_IN_PROGRESS) {
893                 cue_stop(sc);
894                 return(EIO);
895         }
896
897         sc->cue_cdata.cue_tx_cnt++;
898
899         return(0);
900 }
901
902 Static void cue_start(ifp)
903         struct ifnet            *ifp;
904 {
905         struct cue_softc        *sc;
906         struct mbuf             *m_head = NULL;
907
908         sc = ifp->if_softc;
909
910         if (ifp->if_flags & IFF_OACTIVE)
911                 return;
912
913         IF_DEQUEUE(&ifp->if_snd, m_head);
914         if (m_head == NULL)
915                 return;
916
917         if (cue_encap(sc, m_head, 0)) {
918                 IF_PREPEND(&ifp->if_snd, m_head);
919                 ifp->if_flags |= IFF_OACTIVE;
920                 return;
921         }
922
923         /*
924          * If there's a BPF listener, bounce a copy of this frame
925          * to him.
926          */
927         if (ifp->if_bpf)
928                 bpf_mtap(ifp, m_head);
929
930         ifp->if_flags |= IFF_OACTIVE;
931
932         /*
933          * Set a timeout in case the chip goes out to lunch.
934          */
935         ifp->if_timer = 5;
936
937         return;
938 }
939
940 Static void cue_init(xsc)
941         void                    *xsc;
942 {
943         struct cue_softc        *sc = xsc;
944         struct ifnet            *ifp = &sc->arpcom.ac_if;
945         struct cue_chain        *c;
946         usbd_status             err;
947         int                     i, s;
948
949         if (ifp->if_flags & IFF_RUNNING)
950                 return;
951
952         s = splimp();
953
954         /*
955          * Cancel pending I/O and free all RX/TX buffers.
956          */
957 #ifdef foo
958         cue_reset(sc);
959 #endif
960
961         /* Set MAC address */
962         for (i = 0; i < ETHER_ADDR_LEN; i++)
963                 csr_write_1(sc, CUE_PAR0 - i, sc->arpcom.ac_enaddr[i]);
964
965         /* Enable RX logic. */
966         csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON|CUE_ETHCTL_MCAST_ON);
967
968          /* If we want promiscuous mode, set the allframes bit. */
969         if (ifp->if_flags & IFF_PROMISC) {
970                 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
971         } else {
972                 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
973         }
974
975         /* Init TX ring. */
976         if (cue_tx_list_init(sc) == ENOBUFS) {
977                 printf("cue%d: tx list init failed\n", sc->cue_unit);
978                 splx(s);
979                 return;
980         }
981
982         /* Init RX ring. */
983         if (cue_rx_list_init(sc) == ENOBUFS) {
984                 printf("cue%d: rx list init failed\n", sc->cue_unit);
985                 splx(s);
986                 return;
987         }
988
989         /* Load the multicast filter. */
990         cue_setmulti(sc);
991
992         /*
993          * Set the number of RX and TX buffers that we want
994          * to reserve inside the ASIC.
995          */
996         csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
997         csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
998
999         /* Set advanced operation modes. */
1000         csr_write_1(sc, CUE_ADVANCED_OPMODES,
1001             CUE_AOP_EMBED_RXLEN|0x01); /* 1 wait state */
1002
1003         /* Program the LED operation. */
1004         csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
1005
1006         /* Open RX and TX pipes. */
1007         err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
1008             USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
1009         if (err) {
1010                 printf("cue%d: open rx pipe failed: %s\n",
1011                     sc->cue_unit, usbd_errstr(err));
1012                 splx(s);
1013                 return;
1014         }
1015         err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
1016             USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
1017         if (err) {
1018                 printf("cue%d: open tx pipe failed: %s\n",
1019                     sc->cue_unit, usbd_errstr(err));
1020                 splx(s);
1021                 return;
1022         }
1023
1024         /* Start up the receive pipe. */
1025         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1026                 c = &sc->cue_cdata.cue_rx_chain[i];
1027                 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
1028                     c, mtod(c->cue_mbuf, char *), CUE_BUFSZ,
1029                     USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
1030                 usbd_transfer(c->cue_xfer);
1031         }
1032
1033         ifp->if_flags |= IFF_RUNNING;
1034         ifp->if_flags &= ~IFF_OACTIVE;
1035
1036         (void)splx(s);
1037
1038         sc->cue_stat_ch = timeout(cue_tick, sc, hz);
1039
1040         return;
1041 }
1042
1043 Static int cue_ioctl(ifp, command, data)
1044         struct ifnet            *ifp;
1045         u_long                  command;
1046         caddr_t                 data;
1047 {
1048         struct cue_softc        *sc = ifp->if_softc;
1049         int                     s, error = 0;
1050
1051         s = splimp();
1052
1053         switch(command) {
1054         case SIOCSIFADDR:
1055         case SIOCGIFADDR:
1056         case SIOCSIFMTU:
1057                 error = ether_ioctl(ifp, command, data);
1058                 break;
1059         case SIOCSIFFLAGS:
1060                 if (ifp->if_flags & IFF_UP) {
1061                         if (ifp->if_flags & IFF_RUNNING &&
1062                             ifp->if_flags & IFF_PROMISC &&
1063                             !(sc->cue_if_flags & IFF_PROMISC)) {
1064                                 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1065                                 cue_setmulti(sc);
1066                         } else if (ifp->if_flags & IFF_RUNNING &&
1067                             !(ifp->if_flags & IFF_PROMISC) &&
1068                             sc->cue_if_flags & IFF_PROMISC) {
1069                                 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1070                                 cue_setmulti(sc);
1071                         } else if (!(ifp->if_flags & IFF_RUNNING))
1072                                 cue_init(sc);
1073                 } else {
1074                         if (ifp->if_flags & IFF_RUNNING)
1075                                 cue_stop(sc);
1076                 }
1077                 sc->cue_if_flags = ifp->if_flags;
1078                 error = 0;
1079                 break;
1080         case SIOCADDMULTI:
1081         case SIOCDELMULTI:
1082                 cue_setmulti(sc);
1083                 error = 0;
1084                 break;
1085         default:
1086                 error = EINVAL;
1087                 break;
1088         }
1089
1090         (void)splx(s);
1091
1092         return(error);
1093 }
1094
1095 Static void cue_watchdog(ifp)
1096         struct ifnet            *ifp;
1097 {
1098         struct cue_softc        *sc;
1099         struct cue_chain        *c;
1100
1101         usbd_status             stat;
1102         sc = ifp->if_softc;
1103
1104         ifp->if_oerrors++;
1105         printf("cue%d: watchdog timeout\n", sc->cue_unit);
1106
1107         c = &sc->cue_cdata.cue_tx_chain[0];
1108         usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1109         cue_txeof(c->cue_xfer, c, stat);
1110
1111         if (ifp->if_snd.ifq_head != NULL)
1112                 cue_start(ifp);
1113
1114         return;
1115 }
1116
1117 /*
1118  * Stop the adapter and free any mbufs allocated to the
1119  * RX and TX lists.
1120  */
1121 Static void cue_stop(sc)
1122         struct cue_softc        *sc;
1123 {
1124         usbd_status             err;
1125         struct ifnet            *ifp;
1126         int                     i;
1127
1128         ifp = &sc->arpcom.ac_if;
1129         ifp->if_timer = 0;
1130
1131         csr_write_1(sc, CUE_ETHCTL, 0);
1132         cue_reset(sc);
1133         untimeout(cue_tick, sc, sc->cue_stat_ch);
1134
1135         /* Stop transfers. */
1136         if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1137                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1138                 if (err) {
1139                         printf("cue%d: abort rx pipe failed: %s\n",
1140                         sc->cue_unit, usbd_errstr(err));
1141                 }
1142                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1143                 if (err) {
1144                         printf("cue%d: close rx pipe failed: %s\n",
1145                         sc->cue_unit, usbd_errstr(err));
1146                 }
1147                 sc->cue_ep[CUE_ENDPT_RX] = NULL;
1148         }
1149
1150         if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1151                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1152                 if (err) {
1153                         printf("cue%d: abort tx pipe failed: %s\n",
1154                         sc->cue_unit, usbd_errstr(err));
1155                 }
1156                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1157                 if (err) {
1158                         printf("cue%d: close tx pipe failed: %s\n",
1159                             sc->cue_unit, usbd_errstr(err));
1160                 }
1161                 sc->cue_ep[CUE_ENDPT_TX] = NULL;
1162         }
1163
1164         if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1165                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1166                 if (err) {
1167                         printf("cue%d: abort intr pipe failed: %s\n",
1168                         sc->cue_unit, usbd_errstr(err));
1169                 }
1170                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1171                 if (err) {
1172                         printf("cue%d: close intr pipe failed: %s\n",
1173                             sc->cue_unit, usbd_errstr(err));
1174                 }
1175                 sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1176         }
1177
1178         /* Free RX resources. */
1179         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1180                 if (sc->cue_cdata.cue_rx_chain[i].cue_buf != NULL) {
1181                         free(sc->cue_cdata.cue_rx_chain[i].cue_buf, M_USBDEV);
1182                         sc->cue_cdata.cue_rx_chain[i].cue_buf = NULL;
1183                 }
1184                 if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1185                         m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1186                         sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1187                 }
1188                 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1189                         usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1190                         sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1191                 }
1192         }
1193
1194         /* Free TX resources. */
1195         for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1196                 if (sc->cue_cdata.cue_tx_chain[i].cue_buf != NULL) {
1197                         free(sc->cue_cdata.cue_tx_chain[i].cue_buf, M_USBDEV);
1198                         sc->cue_cdata.cue_tx_chain[i].cue_buf = NULL;
1199                 }
1200                 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1201                         m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1202                         sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1203                 }
1204                 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1205                         usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1206                         sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1207                 }
1208         }
1209
1210         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1211
1212         return;
1213 }
1214
1215 /*
1216  * Stop all chip I/O so that the kernel's probe routines don't
1217  * get confused by errant DMAs when rebooting.
1218  */
1219 Static void cue_shutdown(dev)
1220         device_t                dev;
1221 {
1222         struct cue_softc        *sc;
1223
1224         sc = device_get_softc(dev);
1225
1226         cue_reset(sc);
1227         cue_stop(sc);
1228
1229         return;
1230 }