Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / dev / netif / kue / if_kue.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_kue.c,v 1.17.2.9 2003/04/13 02:39:25 murray Exp $
33  */
34
35 /*
36  * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
37  *
38  * Written by Bill Paul <wpaul@ee.columbia.edu>
39  * Electrical Engineering Department
40  * Columbia University, New York City
41  */
42
43 /*
44  * The KLSI USB to ethernet adapter chip contains an USB serial interface,
45  * ethernet MAC and embedded microcontroller (called the QT Engine).
46  * The chip must have firmware loaded into it before it will operate.
47  * Packets are passed between the chip and host via bulk transfers.
48  * There is an interrupt endpoint mentioned in the software spec, however
49  * it's currently unused. This device is 10Mbps half-duplex only, hence
50  * there is no media selection logic. The MAC supports a 128 entry
51  * multicast filter, though the exact size of the filter can depend
52  * on the firmware. Curiously, while the software spec describes various
53  * ethernet statistics counters, my sample adapter and firmware combination
54  * claims not to support any statistics counters at all.
55  *
56  * Note that once we load the firmware in the device, we have to be
57  * careful not to load it again: if you restart your computer but
58  * leave the adapter attached to the USB controller, it may remain
59  * powered on and retain its firmware. In this case, we don't need
60  * to load the firmware a second time.
61  *
62  * Special thanks to Rob Furr for providing an ADS Technologies
63  * adapter for development and testing. No monkeys were harmed during
64  * the development of this driver.
65  */
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/sockio.h>
70 #include <sys/mbuf.h>
71 #include <sys/malloc.h>
72 #include <sys/kernel.h>
73 #include <sys/socket.h>
74 #include <sys/bus.h>
75
76 #include <net/if.h>
77 #include <net/ifq_var.h>
78 #include <net/if_arp.h>
79 #include <net/ethernet.h>
80 #include <net/if_dl.h>
81 #include <net/if_media.h>
82 #include <net/bpf.h>
83
84 #include <machine/clock.h>
85
86 #include <bus/usb/usb.h>
87 #include <bus/usb/usbdi.h>
88 #include <bus/usb/usbdi_util.h>
89 #include <bus/usb/usbdivar.h>
90 #include <bus/usb/usb_ethersubr.h>
91
92 #include "if_kuereg.h"
93 #include <bus/usb/kue_fw.h>
94
95 MODULE_DEPEND(kue, usb, 1, 1, 1);
96
97 /*
98  * Various supported device vendors/products.
99  */
100 static const struct usb_devno kue_devs[] = {
101         { USB_DEVICE(0x03e8, 0x0008) }, /* AOX USB101 */
102         { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USBETT */
103         { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
104         { USB_DEVICE(0x0557, 0x2002) }, /* ATen UC10T */
105         { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Ethernet adapter */
106         { USB_DEVICE(0x0565, 0x0003) }, /* Peracom Ethernet adapter*/
107         { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Ethernet adapter */
108         { USB_DEVICE(0x05e9, 0x0008) }, /* AOX (rebranded Kawasaki?) USB101*/
109         { USB_DEVICE(0x05e9, 0x0008) }, /* Kawasaki LSI DUH3E10BT */
110         { USB_DEVICE(0x05e9, 0x0009) }, /* Kawasaki LSI DUH3E10BTN */
111         { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
112         { USB_DEVICE(0x06e1, 0x0008) }, /* ADS UBS-10BT */
113         { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2102USB */
114         { USB_DEVICE(0x07aa, 0x0001) }, /* Corega USB-T*/
115         { USB_DEVICE(0x07b8, 0x4000) }, /* AboCom URE450 */
116         { USB_DEVICE(0x0846, 0x1001) }, /* Netgear EA101 */
117         { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45*/
118         { USB_DEVICE(0x2001, 0x4000) }, /* D-Link DSB650C */
119 };
120
121 static int kue_match(device_t);
122 static int kue_attach(device_t);
123 static int kue_detach(device_t);
124 static void kue_shutdown(device_t);
125 static int kue_tx_list_init(struct kue_softc *);
126 static int kue_rx_list_init(struct kue_softc *);
127 static int kue_newbuf(struct kue_softc *, struct kue_chain *, struct mbuf *);
128 static int kue_encap(struct kue_softc *, struct mbuf *, int);
129 static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
130 static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
131 static void kue_start(struct ifnet *);
132 static void kue_rxstart(struct ifnet *);
133 static int kue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
134 static void kue_init(void *);
135 static void kue_stop(struct kue_softc *);
136 static void kue_watchdog(struct ifnet *);
137
138 static void kue_setmulti(struct kue_softc *);
139 static void kue_reset(struct kue_softc *);
140
141 static usbd_status kue_do_request(usbd_device_handle,
142                                   usb_device_request_t *, void *);
143 static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
144                            u_int16_t, char *, int);
145 static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
146 static int kue_load_fw(struct kue_softc *);
147
148 static device_method_t kue_methods[] = {
149         /* Device interface */
150         DEVMETHOD(device_probe,         kue_match),
151         DEVMETHOD(device_attach,        kue_attach),
152         DEVMETHOD(device_detach,        kue_detach),
153         DEVMETHOD(device_shutdown,      kue_shutdown),
154
155         { 0, 0 }
156 };
157
158 static driver_t kue_driver = {
159         "kue",
160         kue_methods,
161         sizeof(struct kue_softc)
162 };
163
164 static devclass_t kue_devclass;
165
166 DECLARE_DUMMY_MODULE(if_kue);
167 DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, usbd_driver_load, NULL);
168
169 /*
170  * We have a custom do_request function which is almost like the
171  * regular do_request function, except it has a much longer timeout.
172  * Why? Because we need to make requests over the control endpoint
173  * to download the firmware to the device, which can take longer
174  * than the default timeout.
175  */
176 static usbd_status
177 kue_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
178 {
179         usbd_xfer_handle        xfer;
180         usbd_status             err;
181
182         xfer = usbd_alloc_xfer(dev);
183         usbd_setup_default_xfer(xfer, dev, 0, 500000, req,
184             data, UGETW(req->wLength), USBD_SHORT_XFER_OK, 0);
185         err = usbd_sync_transfer(xfer);
186         usbd_free_xfer(xfer);
187         return(err);
188 }
189
190 static usbd_status
191 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
192 {
193         usbd_device_handle      dev;
194         usb_device_request_t    req;
195         usbd_status             err;
196
197         if (sc->kue_dying)
198                 return(USBD_NORMAL_COMPLETION);
199
200         dev = sc->kue_udev;
201
202         KUE_LOCK(sc);
203
204         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
205
206         req.bRequest = breq;
207         USETW(req.wValue, word);
208         USETW(req.wIndex, 0);
209         USETW(req.wLength, 0);
210
211         err = kue_do_request(dev, &req, NULL);
212
213         KUE_UNLOCK(sc);
214
215         return(err);
216 }
217
218 static usbd_status
219 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
220         char *data, int len)
221 {
222         usbd_device_handle      dev;
223         usb_device_request_t    req;
224         usbd_status             err;
225
226         dev = sc->kue_udev;
227
228         if (sc->kue_dying)
229                 return(USBD_NORMAL_COMPLETION);
230
231         KUE_LOCK(sc);
232
233         if (rw == KUE_CTL_WRITE)
234                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
235         else
236                 req.bmRequestType = UT_READ_VENDOR_DEVICE;
237
238         req.bRequest = breq;
239         USETW(req.wValue, val);
240         USETW(req.wIndex, 0);
241         USETW(req.wLength, len);
242
243         err = kue_do_request(dev, &req, data);
244
245         KUE_UNLOCK(sc);
246
247         return(err);
248 }
249
250 static int
251 kue_load_fw(struct kue_softc *sc)
252 {
253         usbd_status             err;
254         usb_device_descriptor_t *dd;
255         int                     hwrev;
256
257         dd = &sc->kue_udev->ddesc;
258         hwrev = UGETW(dd->bcdDevice);
259
260         /*
261          * First, check if we even need to load the firmware.
262          * If the device was still attached when the system was
263          * rebooted, it may already have firmware loaded in it.
264          * If this is the case, we don't need to do it again.
265          * And in fact, if we try to load it again, we'll hang,
266          * so we have to avoid this condition if we don't want
267          * to look stupid.
268          *
269          * We can test this quickly by checking the bcdRevision
270          * code. The NIC will return a different revision code if
271          * it's probed while the firmware is still loaded and
272          * running.
273          */
274         if (hwrev == 0x0202)
275                 return(0);
276
277         /* Load code segment */
278         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
279             0, kue_code_seg, sizeof(kue_code_seg));
280         if (err) {
281                 kprintf("kue%d: failed to load code segment: %s\n",
282                     sc->kue_unit, usbd_errstr(err));
283                         return(ENXIO);
284         }
285
286         /* Load fixup segment */
287         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
288             0, kue_fix_seg, sizeof(kue_fix_seg));
289         if (err) {
290                 kprintf("kue%d: failed to load fixup segment: %s\n",
291                     sc->kue_unit, usbd_errstr(err));
292                         return(ENXIO);
293         }
294
295         /* Send trigger command. */
296         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
297             0, kue_trig_seg, sizeof(kue_trig_seg));
298         if (err) {
299                 kprintf("kue%d: failed to load trigger segment: %s\n",
300                     sc->kue_unit, usbd_errstr(err));
301                         return(ENXIO);
302         }
303
304         return(0);
305 }
306
307 static void
308 kue_setmulti(struct kue_softc *sc)
309 {
310         struct ifnet            *ifp;
311         struct ifmultiaddr      *ifma;
312         int                     i = 0;
313
314         ifp = &sc->arpcom.ac_if;
315
316         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
317                 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
318                 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
319                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
320                 return;
321         }
322
323         sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
324
325         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
326                 if (ifma->ifma_addr->sa_family != AF_LINK)
327                         continue;
328                 /*
329                  * If there are too many addresses for the
330                  * internal filter, switch over to allmulti mode.
331                  */
332                 if (i == KUE_MCFILTCNT(sc))
333                         break;
334                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
335                     KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
336                 i++;
337         }
338
339         if (i == KUE_MCFILTCNT(sc))
340                 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
341         else {
342                 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
343                 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
344                     i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
345         }
346
347         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
348
349         return;
350 }
351
352 /*
353  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
354  * done after the firmware is loaded into the adapter in order to
355  * bring it into proper operation.
356  */
357 static void
358 kue_reset(struct kue_softc *sc)
359 {
360         if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0) ||
361             usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
362             &sc->kue_iface)) {
363                 kprintf("kue%d: getting interface handle failed\n",
364                     sc->kue_unit);
365         }
366
367         /* Wait a little while for the chip to get its brains in order. */
368         DELAY(1000);
369         return;
370 }
371
372 /*
373  * Probe for a KLSI chip.
374  */
375 static int
376 kue_match(device_t self)
377 {
378         struct usb_attach_arg *uaa = device_get_ivars(self);
379
380         if (uaa->iface == NULL)
381                 return(UMATCH_NONE);
382
383         return (usb_lookup(kue_devs, uaa->vendor, uaa->product) != NULL ?
384             UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
385 }
386
387 /*
388  * Attach the interface. Allocate softc structures, do
389  * setup and ethernet/BPF attach.
390  */
391 static int
392 kue_attach(device_t self)
393 {
394         struct kue_softc *sc = device_get_softc(self);
395         struct usb_attach_arg *uaa = device_get_ivars(self);
396         struct ifnet            *ifp;
397         usbd_status             err;
398         usb_interface_descriptor_t      *id;
399         usb_endpoint_descriptor_t       *ed;
400         int                     i;
401
402         sc->kue_iface = uaa->iface;
403         sc->kue_udev = uaa->device;
404         sc->kue_unit = device_get_unit(self);
405
406         id = usbd_get_interface_descriptor(uaa->iface);
407
408         /* Find endpoints. */
409         for (i = 0; i < id->bNumEndpoints; i++) {
410                 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
411                 if (!ed) {
412                         kprintf("kue%d: couldn't get ep %d\n",
413                             sc->kue_unit, i);
414                         return ENXIO;
415                 }
416                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
417                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
418                         sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
419                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
420                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
421                         sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
422                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
423                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
424                         sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
425                 }
426         }
427
428 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
429         mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
430             MTX_DEF | MTX_RECURSE);
431 #endif
432         KUE_LOCK(sc);
433
434         /* Load the firmware into the NIC. */
435         if (kue_load_fw(sc)) {
436                 KUE_UNLOCK(sc);
437 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
438                 mtx_destroy(&sc->kue_mtx);
439 #endif
440                 return ENXIO;
441         }
442
443         /* Reset the adapter. */
444         kue_reset(sc);
445
446         /* Read ethernet descriptor */
447         err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
448             0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
449
450         sc->kue_mcfilters = kmalloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
451             M_USBDEV, M_WAITOK);
452
453         ifp = &sc->arpcom.ac_if;
454         ifp->if_softc = sc;
455         if_initname(ifp, "kue", sc->kue_unit);
456         ifp->if_mtu = ETHERMTU;
457         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
458         ifp->if_ioctl = kue_ioctl;
459         ifp->if_start = kue_start;
460         ifp->if_watchdog = kue_watchdog;
461         ifp->if_init = kue_init;
462         ifp->if_baudrate = 10000000;
463         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
464         ifq_set_ready(&ifp->if_snd);
465
466         /*
467          * Call MI attach routine.
468          */
469         ether_ifattach(ifp, sc->kue_desc.kue_macaddr, NULL);
470         usb_register_netisr();
471         sc->kue_dying = 0;
472
473         KUE_UNLOCK(sc);
474
475         return 0;
476 }
477
478 static int
479 kue_detach(device_t dev)
480 {
481         struct kue_softc        *sc;
482         struct ifnet            *ifp;
483
484         sc = device_get_softc(dev);
485         KUE_LOCK(sc);
486         ifp = &sc->arpcom.ac_if;
487
488         sc->kue_dying = 1;
489
490         if (ifp != NULL)
491                 ether_ifdetach(ifp);
492
493         if (sc->kue_ep[KUE_ENDPT_TX] != NULL)
494                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
495         if (sc->kue_ep[KUE_ENDPT_RX] != NULL)
496                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
497         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL)
498                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
499
500         if (sc->kue_mcfilters != NULL)
501                 kfree(sc->kue_mcfilters, M_USBDEV);
502
503         KUE_UNLOCK(sc);
504 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
505         mtx_destroy(&sc->kue_mtx);
506 #endif
507
508         return(0);
509 }
510
511 /*
512  * Initialize an RX descriptor and attach an MBUF cluster.
513  */
514 static int
515 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
516 {
517         struct mbuf             *m_new = NULL;
518
519         if (m == NULL) {
520                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
521                 if (m_new == NULL) {
522                         kprintf("kue%d: no memory for rx list "
523                             "-- packet dropped!\n", sc->kue_unit);
524                         return(ENOBUFS);
525                 }
526
527                 MCLGET(m_new, MB_DONTWAIT);
528                 if (!(m_new->m_flags & M_EXT)) {
529                         kprintf("kue%d: no memory for rx list "
530                             "-- packet dropped!\n", sc->kue_unit);
531                         m_freem(m_new);
532                         return(ENOBUFS);
533                 }
534                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
535         } else {
536                 m_new = m;
537                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
538                 m_new->m_data = m_new->m_ext.ext_buf;
539         }
540
541         c->kue_mbuf = m_new;
542
543         return(0);
544 }
545
546 static int
547 kue_rx_list_init(struct kue_softc *sc)
548 {
549         struct kue_cdata        *cd;
550         struct kue_chain        *c;
551         int                     i;
552
553         cd = &sc->kue_cdata;
554         for (i = 0; i < KUE_RX_LIST_CNT; i++) {
555                 c = &cd->kue_rx_chain[i];
556                 c->kue_sc = sc;
557                 c->kue_idx = i;
558                 if (kue_newbuf(sc, c, NULL) == ENOBUFS)
559                         return(ENOBUFS);
560                 if (c->kue_xfer == NULL) {
561                         c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
562                         if (c->kue_xfer == NULL)
563                                 return(ENOBUFS);
564                 }
565         }
566
567         return(0);
568 }
569
570 static int
571 kue_tx_list_init(struct kue_softc *sc)
572 {
573         struct kue_cdata        *cd;
574         struct kue_chain        *c;
575         int                     i;
576
577         cd = &sc->kue_cdata;
578         for (i = 0; i < KUE_TX_LIST_CNT; i++) {
579                 c = &cd->kue_tx_chain[i];
580                 c->kue_sc = sc;
581                 c->kue_idx = i;
582                 c->kue_mbuf = NULL;
583                 if (c->kue_xfer == NULL) {
584                         c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
585                         if (c->kue_xfer == NULL)
586                                 return(ENOBUFS);
587                 }
588                 c->kue_buf = kmalloc(KUE_BUFSZ, M_USBDEV, M_WAITOK);
589         }
590
591         return(0);
592 }
593
594 static void
595 kue_rxstart(struct ifnet *ifp)
596 {
597         struct kue_softc        *sc;
598         struct kue_chain        *c;
599
600         sc = ifp->if_softc;
601         KUE_LOCK(sc);
602         c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
603
604         if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
605                 ifp->if_ierrors++;
606                 return;
607         }
608
609         /* Setup new transfer. */
610         usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
611             c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
612             USBD_NO_TIMEOUT, kue_rxeof);
613         usbd_transfer(c->kue_xfer);
614
615         KUE_UNLOCK(sc);
616
617         return;
618 }
619
620 /*
621  * A frame has been uploaded: pass the resulting mbuf chain up to
622  * the higher level protocols.
623  */
624 static void
625 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
626 {
627         struct kue_softc        *sc;
628         struct kue_chain        *c;
629         struct mbuf             *m;
630         struct ifnet            *ifp;
631         int                     total_len = 0;
632         u_int16_t               len;
633
634         c = priv;
635         sc = c->kue_sc;
636         KUE_LOCK(sc);
637         ifp = &sc->arpcom.ac_if;
638
639         if (!(ifp->if_flags & IFF_RUNNING)) {
640                 KUE_UNLOCK(sc);
641                 return;
642         }
643
644         if (status != USBD_NORMAL_COMPLETION) {
645                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
646                         KUE_UNLOCK(sc);
647                         return;
648                 }
649                 if (usbd_ratecheck(&sc->kue_rx_notice))
650                         kprintf("kue%d: usb error on rx: %s\n", sc->kue_unit,
651                             usbd_errstr(status));
652                 if (status == USBD_STALLED)
653                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
654                 goto done;
655         }
656
657         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
658         m = c->kue_mbuf;
659         if (total_len <= 1)
660                 goto done;
661
662         len = *mtod(m, u_int16_t *);
663         m_adj(m, sizeof(u_int16_t));
664
665         /* No errors; receive the packet. */
666         total_len = len;
667
668         if (len < sizeof(struct ether_header)) {
669                 ifp->if_ierrors++;
670                 goto done;
671         }
672
673         ifp->if_ipackets++;
674         m->m_pkthdr.rcvif = ifp;
675         m->m_pkthdr.len = m->m_len = total_len;
676
677         /* Put the packet on the special USB input queue. */
678         usb_ether_input(m);
679         kue_rxstart(ifp);
680
681         KUE_UNLOCK(sc);
682
683         return;
684 done:
685
686         /* Setup new transfer. */
687         usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
688             c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
689             USBD_NO_TIMEOUT, kue_rxeof);
690         usbd_transfer(c->kue_xfer);
691         KUE_UNLOCK(sc);
692
693         return;
694 }
695
696 /*
697  * A frame was downloaded to the chip. It's safe for us to clean up
698  * the list buffers.
699  */
700
701 static void
702 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
703 {
704         struct kue_softc        *sc;
705         struct kue_chain        *c;
706         struct ifnet            *ifp;
707         usbd_status             err;
708
709         c = priv;
710         sc = c->kue_sc;
711         KUE_LOCK(sc);
712
713         ifp = &sc->arpcom.ac_if;
714         ifp->if_timer = 0;
715         ifp->if_flags &= ~IFF_OACTIVE;
716
717         if (status != USBD_NORMAL_COMPLETION) {
718                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
719                         KUE_UNLOCK(sc);
720                         return;
721                 }
722                 kprintf("kue%d: usb error on tx: %s\n", sc->kue_unit,
723                     usbd_errstr(status));
724                 if (status == USBD_STALLED)
725                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
726                 KUE_UNLOCK(sc);
727                 return;
728         }
729
730         usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &err);
731
732         if (c->kue_mbuf != NULL) {
733                 m_freem(c->kue_mbuf);
734                 c->kue_mbuf = NULL;
735         }
736
737         if (err)
738                 ifp->if_oerrors++;
739         else
740                 ifp->if_opackets++;
741
742         if (!ifq_is_empty(&ifp->if_snd))
743                 if_devstart(ifp);
744
745         KUE_UNLOCK(sc);
746
747         return;
748 }
749
750 static int
751 kue_encap(struct kue_softc *sc, struct mbuf *m, int idx)
752 {
753         int                     total_len;
754         struct kue_chain        *c;
755         usbd_status             err;
756
757         c = &sc->kue_cdata.kue_tx_chain[idx];
758
759         /*
760          * Copy the mbuf data into a contiguous buffer, leaving two
761          * bytes at the beginning to hold the frame length.
762          */
763         m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
764         c->kue_mbuf = m;
765
766         total_len = m->m_pkthdr.len + 2;
767         total_len += 64 - (total_len % 64);
768
769         /* Frame length is specified in the first 2 bytes of the buffer. */
770         c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
771         c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
772
773         usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
774             c, c->kue_buf, total_len, 0, 10000, kue_txeof);
775
776         /* Transmit */
777         err = usbd_transfer(c->kue_xfer);
778         if (err != USBD_IN_PROGRESS) {
779                 kue_stop(sc);
780                 return(EIO);
781         }
782
783         sc->kue_cdata.kue_tx_cnt++;
784
785         return(0);
786 }
787
788 static void
789 kue_start(struct ifnet *ifp)
790 {
791         struct kue_softc        *sc;
792         struct mbuf             *m_head = NULL;
793
794         sc = ifp->if_softc;
795         KUE_LOCK(sc);
796
797         if (ifp->if_flags & IFF_OACTIVE) {
798                 KUE_UNLOCK(sc);
799                 return;
800         }
801
802         m_head = ifq_dequeue(&ifp->if_snd, NULL);
803         if (m_head == NULL) {
804                 KUE_UNLOCK(sc);
805                 return;
806         }
807
808         if (kue_encap(sc, m_head, 0)) {
809                 /* kue_encap() will free m_head, if we reach here */
810                 ifp->if_flags |= IFF_OACTIVE;
811                 KUE_UNLOCK(sc);
812                 return;
813         }
814
815         /*
816          * If there's a BPF listener, bounce a copy of this frame
817          * to him.
818          */
819         BPF_MTAP(ifp, m_head);
820
821         ifp->if_flags |= IFF_OACTIVE;
822
823         /*
824          * Set a timeout in case the chip goes out to lunch.
825          */
826         ifp->if_timer = 5;
827         KUE_UNLOCK(sc);
828
829         return;
830 }
831
832 static void
833 kue_init(void *xsc)
834 {
835         struct kue_softc        *sc = xsc;
836         struct ifnet            *ifp = &sc->arpcom.ac_if;
837         struct kue_chain        *c;
838         usbd_status             err;
839         int                     i;
840
841         KUE_LOCK(sc);
842
843         if (ifp->if_flags & IFF_RUNNING) {
844                 KUE_UNLOCK(sc);
845                 return;
846         }
847
848         /* Set MAC address */
849         kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
850             0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
851
852         sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST;
853
854          /* If we want promiscuous mode, set the allframes bit. */
855         if (ifp->if_flags & IFF_PROMISC)
856                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
857
858         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
859
860         /* I'm not sure how to tune these. */
861 #ifdef notdef
862         /*
863          * Leave this one alone for now; setting it
864          * wrong causes lockups on some machines/controllers.
865          */
866         kue_setword(sc, KUE_CMD_SET_SOFS, 1);
867 #endif
868         kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
869
870         /* Init TX ring. */
871         if (kue_tx_list_init(sc) == ENOBUFS) {
872                 kprintf("kue%d: tx list init failed\n", sc->kue_unit);
873                 KUE_UNLOCK(sc);
874                 return;
875         }
876
877         /* Init RX ring. */
878         if (kue_rx_list_init(sc) == ENOBUFS) {
879                 kprintf("kue%d: rx list init failed\n", sc->kue_unit);
880                 KUE_UNLOCK(sc);
881                 return;
882         }
883
884         /* Load the multicast filter. */
885         kue_setmulti(sc);
886
887         /* Open RX and TX pipes. */
888         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
889             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
890         if (err) {
891                 kprintf("kue%d: open rx pipe failed: %s\n",
892                     sc->kue_unit, usbd_errstr(err));
893                 KUE_UNLOCK(sc);
894                 return;
895         }
896
897         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
898             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
899         if (err) {
900                 kprintf("kue%d: open tx pipe failed: %s\n",
901                     sc->kue_unit, usbd_errstr(err));
902                 KUE_UNLOCK(sc);
903                 return;
904         }
905
906         /* Start up the receive pipe. */
907         for (i = 0; i < KUE_RX_LIST_CNT; i++) {
908                 c = &sc->kue_cdata.kue_rx_chain[i];
909                 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
910                     c, mtod(c->kue_mbuf, char *), KUE_BUFSZ,
911                 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
912                 usbd_transfer(c->kue_xfer);
913         }
914
915         ifp->if_flags |= IFF_RUNNING;
916         ifp->if_flags &= ~IFF_OACTIVE;
917
918         KUE_UNLOCK(sc);
919
920         return;
921 }
922
923 static int
924 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
925 {
926         struct kue_softc        *sc = ifp->if_softc;
927         int                     error = 0;
928
929         KUE_LOCK(sc);
930
931         switch(command) {
932         case SIOCSIFFLAGS:
933                 if (ifp->if_flags & IFF_UP) {
934                         if (ifp->if_flags & IFF_RUNNING &&
935                             ifp->if_flags & IFF_PROMISC &&
936                             !(sc->kue_if_flags & IFF_PROMISC)) {
937                                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
938                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
939                                     sc->kue_rxfilt);
940                         } else if (ifp->if_flags & IFF_RUNNING &&
941                             !(ifp->if_flags & IFF_PROMISC) &&
942                             sc->kue_if_flags & IFF_PROMISC) {
943                                 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
944                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
945                                     sc->kue_rxfilt);
946                         } else if (!(ifp->if_flags & IFF_RUNNING))
947                                 kue_init(sc);
948                 } else {
949                         if (ifp->if_flags & IFF_RUNNING)
950                                 kue_stop(sc);
951                 }
952                 sc->kue_if_flags = ifp->if_flags;
953                 error = 0;
954                 break;
955         case SIOCADDMULTI:
956         case SIOCDELMULTI:
957                 kue_setmulti(sc);
958                 error = 0;
959                 break;
960         default:
961                 error = ether_ioctl(ifp, command, data);
962                 break;
963         }
964
965         KUE_UNLOCK(sc);
966
967         return(error);
968 }
969
970 static void
971 kue_watchdog(struct ifnet *ifp)
972 {
973         struct kue_softc        *sc;
974         struct kue_chain        *c;
975         usbd_status             stat;
976
977         sc = ifp->if_softc;
978         KUE_LOCK(sc);
979         ifp->if_oerrors++;
980         kprintf("kue%d: watchdog timeout\n", sc->kue_unit);
981
982         c = &sc->kue_cdata.kue_tx_chain[0];
983         usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
984         kue_txeof(c->kue_xfer, c, stat);
985
986         if (!ifq_is_empty(&ifp->if_snd))
987                 if_devstart(ifp);
988         KUE_UNLOCK(sc);
989
990         return;
991 }
992
993 /*
994  * Stop the adapter and free any mbufs allocated to the
995  * RX and TX lists.
996  */
997 static void
998 kue_stop(struct kue_softc *sc)
999 {
1000         usbd_status             err;
1001         struct ifnet            *ifp;
1002         int                     i;
1003
1004         KUE_LOCK(sc);
1005         ifp = &sc->arpcom.ac_if;
1006         ifp->if_timer = 0;
1007
1008         /* Stop transfers. */
1009         if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1010                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1011                 if (err) {
1012                         kprintf("kue%d: abort rx pipe failed: %s\n",
1013                         sc->kue_unit, usbd_errstr(err));
1014                 }
1015                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1016                 if (err) {
1017                         kprintf("kue%d: close rx pipe failed: %s\n",
1018                         sc->kue_unit, usbd_errstr(err));
1019                 }
1020                 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1021         }
1022
1023         if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1024                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1025                 if (err) {
1026                         kprintf("kue%d: abort tx pipe failed: %s\n",
1027                         sc->kue_unit, usbd_errstr(err));
1028                 }
1029                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1030                 if (err) {
1031                         kprintf("kue%d: close tx pipe failed: %s\n",
1032                             sc->kue_unit, usbd_errstr(err));
1033                 }
1034                 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1035         }
1036
1037         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1038                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1039                 if (err) {
1040                         kprintf("kue%d: abort intr pipe failed: %s\n",
1041                         sc->kue_unit, usbd_errstr(err));
1042                 }
1043                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1044                 if (err) {
1045                         kprintf("kue%d: close intr pipe failed: %s\n",
1046                             sc->kue_unit, usbd_errstr(err));
1047                 }
1048                 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1049         }
1050
1051         /* Free RX resources. */
1052         for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1053                 if (sc->kue_cdata.kue_rx_chain[i].kue_buf != NULL) {
1054                         kfree(sc->kue_cdata.kue_rx_chain[i].kue_buf, M_USBDEV);
1055                         sc->kue_cdata.kue_rx_chain[i].kue_buf = NULL;
1056                 }
1057                 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1058                         m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1059                         sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1060                 }
1061                 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1062                         usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1063                         sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1064                 }
1065         }
1066
1067         /* Free TX resources. */
1068         for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1069                 if (sc->kue_cdata.kue_tx_chain[i].kue_buf != NULL) {
1070                         kfree(sc->kue_cdata.kue_tx_chain[i].kue_buf, M_USBDEV);
1071                         sc->kue_cdata.kue_tx_chain[i].kue_buf = NULL;
1072                 }
1073                 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1074                         m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1075                         sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1076                 }
1077                 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1078                         usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1079                         sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1080                 }
1081         }
1082
1083         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1084         KUE_UNLOCK(sc);
1085
1086         return;
1087 }
1088
1089 /*
1090  * Stop all chip I/O so that the kernel's probe routines don't
1091  * get confused by errant DMAs when rebooting.
1092  */
1093 static void
1094 kue_shutdown(device_t dev)
1095 {
1096         struct kue_softc        *sc;
1097
1098         sc = device_get_softc(dev);
1099
1100         kue_stop(sc);
1101
1102         return;
1103 }