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