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