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