if_kue.c revision 1.12 1 /* $NetBSD: if_kue.c,v 1.12 2000/03/06 20:58:18 augustss Exp $ */
2 /*
3 * Copyright (c) 1997, 1998, 1999, 2000
4 * Bill Paul <wpaul (at) ee.columbia.edu>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $FreeBSD: src/sys/dev/usb/if_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $
34 */
35
36 /*
37 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
38 *
39 * Written by Bill Paul <wpaul (at) ee.columbia.edu>
40 * Electrical Engineering Department
41 * Columbia University, New York City
42 */
43
44 /*
45 * The KLSI USB to ethernet adapter chip contains an USB serial interface,
46 * ethernet MAC and embedded microcontroller (called the QT Engine).
47 * The chip must have firmware loaded into it before it will operate.
48 * Packets are passed between the chip and host via bulk transfers.
49 * There is an interrupt endpoint mentioned in the software spec, however
50 * it's currently unused. This device is 10Mbps half-duplex only, hence
51 * there is no media selection logic. The MAC supports a 128 entry
52 * multicast filter, though the exact size of the filter can depend
53 * on the firmware. Curiously, while the software spec describes various
54 * ethernet statistics counters, my sample adapter and firmware combination
55 * claims not to support any statistics counters at all.
56 *
57 * Note that once we load the firmware in the device, we have to be
58 * careful not to load it again: if you restart your computer but
59 * leave the adapter attached to the USB controller, it may remain
60 * powered on and retain its firmware. In this case, we don't need
61 * to load the firmware a second time.
62 *
63 * Special thanks to Rob Furr for providing an ADS Technologies
64 * adapter for development and testing. No monkeys were harmed during
65 * the development of this driver.
66 */
67
68 /*
69 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
70 */
71
72 /*
73 * TODO:
74 * only use kue_do_request for downloading firmware.
75 * more DPRINTF
76 * proper cleanup on errors
77 */
78 #if defined(__NetBSD__) || defined(__OpenBSD__)
79 #include "opt_inet.h"
80 #include "opt_ns.h"
81 #include "bpfilter.h"
82 #include "rnd.h"
83 #endif
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/sockio.h>
88 #include <sys/mbuf.h>
89 #include <sys/malloc.h>
90 #include <sys/kernel.h>
91 #include <sys/socket.h>
92
93 #if defined(__FreeBSD__)
94
95 #include <net/ethernet.h>
96 #include <machine/clock.h> /* for DELAY */
97 #include <sys/bus.h>
98
99 #elif defined(__NetBSD__) || defined(__OpenBSD__)
100
101 #include <sys/device.h>
102 #if NRND > 0
103 #include <sys/rnd.h>
104 #endif
105
106 #endif
107
108 #include <net/if.h>
109 #include <net/if_arp.h>
110 #include <net/if_dl.h>
111
112 #if defined(__NetBSD__) || defined(__OpenBSD__)
113 #include <net/if_ether.h>
114 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
115 #else
116 #define BPF_MTAP(ifp, m) bpf_mtap((ifp), (m))
117 #endif
118
119 #if defined(__FreeBSD__) || NBPFILTER > 0
120 #include <net/bpf.h>
121 #endif
122
123 #if defined(__NetBSD__) || defined(__OpenBSD__)
124 #ifdef INET
125 #include <netinet/in.h>
126 #include <netinet/if_inarp.h>
127 #endif
128
129 #ifdef NS
130 #include <netns/ns.h>
131 #include <netns/ns_if.h>
132 #endif
133 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
134
135 #include <dev/usb/usb.h>
136 #include <dev/usb/usbdi.h>
137 #include <dev/usb/usbdi_util.h>
138 #include <dev/usb/usbdevs.h>
139
140 #ifdef __FreeBSD__
141 #include <dev/usb/usb_ethersubr.h>
142 #endif
143
144 #include <dev/usb/if_kuereg.h>
145 #include <dev/usb/kue_fw.h>
146
147 #ifdef KUE_DEBUG
148 #define DPRINTF(x) if (kuedebug) logprintf x
149 #define DPRINTFN(n,x) if (kuedebug >= (n)) logprintf x
150 int kuedebug = 0;
151 #else
152 #define DPRINTF(x)
153 #define DPRINTFN(n,x)
154 #endif
155
156 /*
157 * Various supported device vendors/products.
158 */
159 static struct kue_type kue_devs[] = {
160 { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
161 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
162 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
163 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
164 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
165 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
166 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
167 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
168 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
169 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
170 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
171 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
172 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
173 { 0, 0 }
174 };
175
176 USB_DECLARE_DRIVER(kue);
177
178 static int kue_tx_list_init __P((struct kue_softc *));
179 static int kue_rx_list_init __P((struct kue_softc *));
180 static int kue_newbuf __P((struct kue_softc *, struct kue_chain *,
181 struct mbuf *));
182 static int kue_send __P((struct kue_softc *, struct mbuf *, int));
183 static int kue_open_pipes __P((struct kue_softc *));
184 static void kue_rxeof __P((usbd_xfer_handle,
185 usbd_private_handle, usbd_status));
186 static void kue_txeof __P((usbd_xfer_handle,
187 usbd_private_handle, usbd_status));
188 static void kue_start __P((struct ifnet *));
189 static int kue_ioctl __P((struct ifnet *, u_long, caddr_t));
190 static void kue_init __P((void *));
191 static void kue_stop __P((struct kue_softc *));
192 static void kue_watchdog __P((struct ifnet *));
193
194 static void kue_setmulti __P((struct kue_softc *));
195 static void kue_reset __P((struct kue_softc *));
196
197 static usbd_status kue_do_request
198 __P((usbd_device_handle,
199 usb_device_request_t *, void *, u_int16_t,
200 u_int32_t *));
201 static usbd_status kue_ctl_l __P((struct kue_softc *, int, u_int8_t,
202 u_int16_t, char *, u_int32_t,
203 u_int32_t, u_int32_t *));
204 #define kue_ctl(sc, rw, breq, val, data, len) \
205 kue_ctl_l(sc, rw, breq, val, data, len, 0, 0)
206 static usbd_status kue_setword __P((struct kue_softc *, u_int8_t, u_int16_t));
207 static int kue_load_fw __P((struct kue_softc *));
208
209 #if defined(__FreeBSD__)
210 #ifndef lint
211 static const char rcsid[] =
212 "$FreeBSD: src/sys/dev/usb/if_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $";
213 #endif
214
215 static void kue_rxstart __P((struct ifnet *));
216 static void kue_shutdown __P((device_t));
217
218 static struct usb_qdat kue_qdat;
219
220 static device_method_t kue_methods[] = {
221 /* Device interface */
222 DEVMETHOD(device_probe, kue_match),
223 DEVMETHOD(device_attach, kue_attach),
224 DEVMETHOD(device_detach, kue_detach),
225 DEVMETHOD(device_shutdown, kue_shutdown),
226
227 { 0, 0 }
228 };
229
230 static driver_t kue_driver = {
231 "kue",
232 kue_methods,
233 sizeof(struct kue_softc)
234 };
235
236 static devclass_t kue_devclass;
237
238 DRIVER_MODULE(if_kue, uhub, kue_driver, kue_devclass, usbd_driver_load, 0);
239
240 #endif /* __FreeBSD__ */
241
242 /*
243 * We have a custom do_request function which is almost like the
244 * regular do_request function, except it has a much longer timeout.
245 * Why? Because we need to make requests over the control endpoint
246 * to download the firmware to the device, which can take longer
247 * than the default timeout.
248 */
249 static usbd_status
250 kue_do_request(dev, req, data, flags, lenp)
251 usbd_device_handle dev;
252 usb_device_request_t *req;
253 void *data;
254 u_int16_t flags;
255 u_int32_t *lenp;
256 {
257 usbd_xfer_handle xfer;
258 usbd_status err;
259
260 DPRINTFN(15,("kue_do_request: enter\n"));
261
262 xfer = usbd_alloc_xfer(dev);
263 /* XXX 20000 */
264 usbd_setup_default_xfer(xfer, dev, 0, 20000, req,
265 data, UGETW(req->wLength), flags, 0);
266 err = usbd_sync_transfer(xfer);
267 if (lenp != NULL)
268 usbd_get_xfer_status(xfer, NULL, NULL, lenp, NULL);
269 usbd_free_xfer(xfer);
270
271 return (err);
272 }
273
274 static usbd_status
275 kue_setword(sc, breq, word)
276 struct kue_softc *sc;
277 u_int8_t breq;
278 u_int16_t word;
279 {
280 usb_device_request_t req;
281 usbd_status err;
282 int s;
283
284 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
285
286 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
287 req.bRequest = breq;
288 USETW(req.wValue, word);
289 USETW(req.wIndex, 0);
290 USETW(req.wLength, 0);
291
292 s = splusb();
293 err = kue_do_request(sc->kue_udev, &req, NULL, sc->kue_xfer_flags, 0);
294 splx(s);
295
296 return (err);
297 }
298
299 static usbd_status
300 kue_ctl_l(sc, rw, breq, val, data, len, flags, lenp)
301 struct kue_softc *sc;
302 int rw;
303 u_int8_t breq;
304 u_int16_t val;
305 char *data;
306 u_int32_t len;
307 u_int32_t flags;
308 u_int32_t *lenp;
309 {
310 usb_device_request_t req;
311 usbd_status err;
312 int s;
313
314 DPRINTFN(10,("%s: %s: enter, len=%d\n", USBDEVNAME(sc->kue_dev),
315 __FUNCTION__, len));
316
317 if (rw == KUE_CTL_WRITE)
318 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
319 else
320 req.bmRequestType = UT_READ_VENDOR_DEVICE;
321
322 req.bRequest = breq;
323 USETW(req.wValue, val);
324 USETW(req.wIndex, 0);
325 USETW(req.wLength, len);
326
327 s = splusb();
328 err = kue_do_request(sc->kue_udev, &req, data,
329 sc->kue_xfer_flags | flags, lenp);
330 splx(s);
331
332 return (err);
333 }
334
335 static int
336 kue_load_fw(sc)
337 struct kue_softc *sc;
338 {
339 usbd_status err;
340 u_char eaddr[ETHER_ADDR_LEN];
341 u_int32_t alen;
342
343 DPRINTFN(1,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
344
345 /*
346 * First, check if we even need to load the firmware.
347 * If the device was still attached when the system was
348 * rebooted, it may already have firmware loaded in it.
349 * If this is the case, we don't need to do it again.
350 * And in fact, if we try to load it again, we'll hang,
351 * so we have to avoid this condition if we don't want
352 * to look stupid.
353 *
354 * We can test this quickly by trying to read the MAC
355 * address; if this fails to return any data, the firmware
356 * needs to be reloaded, otherwise the device is already
357 * operational and we can just return.
358 */
359 err = kue_ctl_l(sc, KUE_CTL_READ, KUE_CMD_GET_MAC, 0, (char *)&eaddr,
360 ETHER_ADDR_LEN, USBD_SHORT_XFER_OK, &alen);
361
362 if (err) {
363 printf("%s: kue_load_fw: failed to read MAC: %s\n",
364 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
365 return (EIO);
366 }
367
368 if (alen == ETHER_ADDR_LEN) {
369 printf("%s: warm boot, no firmware download\n",
370 USBDEVNAME(sc->kue_dev));
371 return (0);
372 }
373
374 printf("%s: cold boot, downloading firmware\n",
375 USBDEVNAME(sc->kue_dev));
376
377 /* Load code segment */
378 DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
379 USBDEVNAME(sc->kue_dev)));
380 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
381 0, kue_code_seg, sizeof(kue_code_seg));
382 if (err) {
383 printf("%s: failed to load code segment: %s\n",
384 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
385 return (EIO);
386 }
387
388 /* Load fixup segment */
389 DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
390 USBDEVNAME(sc->kue_dev)));
391 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
392 0, kue_fix_seg, sizeof(kue_fix_seg));
393 if (err) {
394 printf("%s: failed to load fixup segment: %s\n",
395 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
396 return (EIO);
397 }
398
399 /* Send trigger command. */
400 DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
401 USBDEVNAME(sc->kue_dev)));
402 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
403 0, kue_trig_seg, sizeof(kue_trig_seg));
404 if (err) {
405 printf("%s: failed to load trigger segment: %s\n",
406 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
407 return (EIO);
408 }
409
410 usbd_delay_ms(sc->kue_udev, 10);
411
412 /*
413 * Reload device descriptor.
414 * Why? The chip without the firmware loaded returns
415 * one revision code. The chip with the firmware
416 * loaded and running returns a *different* revision
417 * code. This confuses the quirk mechanism, which is
418 * dependent on the revision data.
419 */
420 (void)usbd_reload_device_desc(sc->kue_udev);
421
422 DPRINTFN(1,("%s: %s: done\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
423
424 /* Reset the adapter. */
425 kue_reset(sc);
426
427 return (0);
428 }
429
430 static void
431 kue_setmulti(sc)
432 struct kue_softc *sc;
433 {
434 struct ifnet *ifp = GET_IFP(sc);
435 #if defined(__FreeBSD__)
436 struct ifmultiaddr *ifma;
437 #elif defined(__NetBSD__) || defined(__OpenBSD__)
438 struct ether_multi *enm;
439 struct ether_multistep step;
440 #endif
441 int i;
442
443 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
444
445 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
446 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
447 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
448 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
449 return;
450 }
451
452 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
453
454 i = 0;
455 #if defined(__FreeBSD__)
456 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
457 ifma = ifma->ifma_link.le_next) {
458 if (ifma->ifma_addr->sa_family != AF_LINK)
459 continue;
460 /*
461 * If there are too many addresses for the
462 * internal filter, switch over to allmulti mode.
463 */
464 if (i == KUE_MCFILTCNT(sc))
465 break;
466 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
467 KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
468 i++;
469 }
470 #elif defined(__NetBSD__) || defined(__OpenBSD__)
471 ETHER_FIRST_MULTI(step, &sc->kue_ec, enm);
472 while (enm != NULL) {
473 if (i == KUE_MCFILTCNT(sc))
474 break;
475 #if 0
476 if (memcmp(enm->enm_addrlo,
477 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
478 ifp->if_flags |= IFF_ALLMULTI;
479 /* XXX what now? */
480 return;
481 }
482 #endif
483 memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN);
484 ETHER_NEXT_MULTI(step, enm);
485 i++;
486 }
487 #endif
488
489 if (i == KUE_MCFILTCNT(sc))
490 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
491 else {
492 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
493 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
494 i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
495 }
496
497 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
498 }
499
500 /*
501 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
502 * done after the firmware is loaded into the adapter in order to
503 * bring it into proper operation.
504 */
505 static void
506 kue_reset(sc)
507 struct kue_softc *sc;
508 {
509 usbd_status err;
510
511 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
512
513 err = usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0);
514 if (err)
515 printf("%s: reset failed\n", USBDEVNAME(sc->kue_dev));
516
517 /* Wait a little while for the chip to get its brains in order. */
518 usbd_delay_ms(sc->kue_udev, 10);
519 }
520
521 /*
522 * Probe for a KLSI chip.
523 */
524 USB_MATCH(kue)
525 {
526 USB_MATCH_START(kue, uaa);
527 struct kue_type *t;
528
529 DPRINTFN(25,("kue_match: enter\n"));
530
531 if (uaa->iface != NULL)
532 return (UMATCH_NONE);
533
534 for (t = kue_devs; t->kue_vid != 0; t++)
535 if (uaa->vendor == t->kue_vid && uaa->product == t->kue_did)
536 return (UMATCH_VENDOR_PRODUCT);
537
538 return (UMATCH_NONE);
539 }
540
541 /*
542 * Attach the interface. Allocate softc structures, do
543 * setup and ethernet/BPF attach.
544 */
545 USB_ATTACH(kue)
546 {
547 USB_ATTACH_START(kue, sc, uaa);
548 char devinfo[1024];
549 int s;
550 struct ifnet *ifp;
551 usbd_device_handle dev = uaa->device;
552 usbd_interface_handle iface;
553 usbd_status err;
554 usb_interface_descriptor_t *id;
555 usb_endpoint_descriptor_t *ed;
556 int i;
557
558 #ifdef __FreeBSD__
559 bzero(sc, sizeof(struct kue_softc));
560 #endif
561
562 DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
563
564 usbd_devinfo(dev, 0, devinfo);
565 USB_ATTACH_SETUP;
566 printf("%s: %s\n", USBDEVNAME(sc->kue_dev), devinfo);
567
568 err = usbd_set_config_no(dev, KUE_CONFIG_NO, 0);
569 if (err) {
570 printf("%s: setting config no failed\n",
571 USBDEVNAME(sc->kue_dev));
572 USB_ATTACH_ERROR_RETURN;
573 }
574
575 sc->kue_udev = dev;
576 sc->kue_product = uaa->product;
577 sc->kue_vendor = uaa->vendor;
578
579 /* Load the firmware into the NIC. */
580 if (kue_load_fw(sc)) {
581 printf("%s: loading firmware failed\n",
582 USBDEVNAME(sc->kue_dev));
583 USB_ATTACH_ERROR_RETURN;
584 }
585
586 err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
587 if (err) {
588 printf("%s: getting interface handle failed\n",
589 USBDEVNAME(sc->kue_dev));
590 USB_ATTACH_ERROR_RETURN;
591 }
592
593 sc->kue_iface = iface;
594 id = usbd_get_interface_descriptor(iface);
595
596 /* Find endpoints. */
597 for (i = 0; i < id->bNumEndpoints; i++) {
598 ed = usbd_interface2endpoint_descriptor(iface, i);
599 if (ed == NULL) {
600 printf("%s: couldn't get ep %d\n",
601 USBDEVNAME(sc->kue_dev), i);
602 splx(s);
603 USB_ATTACH_ERROR_RETURN;
604 }
605 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
606 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
607 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
608 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
609 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
610 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
611 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
612 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
613 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
614 }
615 }
616
617 if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
618 printf("%s: missing endpoint\n", USBDEVNAME(sc->kue_dev));
619 USB_ATTACH_ERROR_RETURN;
620 }
621
622 /* Read ethernet descriptor */
623 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
624 0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
625 if (err) {
626 printf("%s: could not read Ethernet descriptor\n",
627 USBDEVNAME(sc->kue_dev));
628 USB_ATTACH_ERROR_RETURN;
629 }
630
631 sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
632 M_USBDEV, M_NOWAIT);
633 if (sc->kue_mcfilters == NULL) {
634 printf("%s: no memory for multicast filter buffer\n",
635 USBDEVNAME(sc->kue_dev));
636 USB_ATTACH_ERROR_RETURN;
637 }
638
639 sc->kue_xfer_flags = USBD_NO_TSLEEP;
640
641 s = splimp();
642
643 /*
644 * A KLSI chip was detected. Inform the world.
645 */
646 #if defined(__FreeBSD__)
647 printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->kue_dev),
648 sc->kue_desc.kue_macaddr, ":");
649
650 bcopy(sc->kue_desc.kue_macaddr,
651 (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
652
653 ifp = GET_IFP(sc);
654 ifp->if_softc = sc;
655 ifp->if_unit = sc->kue_unit;
656 ifp->if_name = "kue";
657 ifp->if_mtu = ETHERMTU;
658 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
659 ifp->if_ioctl = kue_ioctl;
660 ifp->if_output = ether_output;
661 ifp->if_start = kue_start;
662 ifp->if_watchdog = kue_watchdog;
663 ifp->if_init = kue_init;
664 ifp->if_baudrate = 10000000;
665 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
666
667 kue_qdat.ifp = ifp;
668 kue_qdat.if_rxstart = kue_rxstart;
669
670 /*
671 * Call MI attach routines.
672 */
673 if_attach(ifp);
674 ether_ifattach(ifp);
675 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
676 usb_register_netisr();
677
678 #elif defined(__NetBSD__) || defined(__OpenBSD__)
679
680 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->kue_dev),
681 ether_sprintf(sc->kue_desc.kue_macaddr));
682
683 /* Initialize interface info.*/
684 ifp = GET_IFP(sc);
685 ifp->if_softc = sc;
686 ifp->if_mtu = ETHERMTU;
687 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
688 ifp->if_ioctl = kue_ioctl;
689 ifp->if_start = kue_start;
690 ifp->if_watchdog = kue_watchdog;
691 ifp->if_baudrate = 10000000;
692 strncpy(ifp->if_xname, USBDEVNAME(sc->kue_dev), IFNAMSIZ);
693
694 /* Attach the interface. */
695 if_attach(ifp);
696 ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
697
698 #if NBPFILTER > 0
699 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
700 sizeof(struct ether_header));
701 #endif
702 #if NRND > 0
703 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->kue_dev),
704 RND_TYPE_NET, 0);
705 #endif
706
707 #endif /* __NetBSD__ */
708 sc->kue_attached = 1;
709 splx(s);
710
711 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
712 USBDEV(sc->kue_dev));
713
714 USB_ATTACH_SUCCESS_RETURN;
715 }
716
717 USB_DETACH(kue)
718 {
719 USB_DETACH_START(kue, sc);
720 struct ifnet *ifp = GET_IFP(sc);
721 int s;
722
723 s = splusb(); /* XXX why? */
724
725 if (sc->kue_mcfilters != NULL) {
726 free(sc->kue_mcfilters, M_USBDEV);
727 sc->kue_mcfilters = NULL;
728 }
729
730 if (!sc->kue_attached) {
731 /* Detached before attached finished, so just bail out. */
732 splx(s);
733 return (0);
734 }
735
736 if (ifp->if_flags & IFF_RUNNING)
737 kue_stop(sc);
738
739 #if defined(__NetBSD__)
740 #if NRND > 0
741 rnd_detach_source(&sc->rnd_source);
742 #endif
743 #if NBPFILTER > 0
744 bpfdetach(ifp);
745 #endif
746 ether_ifdetach(ifp);
747 #endif /* __NetBSD__ */
748
749 if_detach(ifp);
750
751 #ifdef DIAGNOSTIC
752 if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
753 sc->kue_ep[KUE_ENDPT_RX] != NULL ||
754 sc->kue_ep[KUE_ENDPT_INTR] != NULL)
755 printf("%s: detach has active endpoints\n",
756 USBDEVNAME(sc->kue_dev));
757 #endif
758
759 sc->kue_attached = 0;
760 splx(s);
761
762 return (0);
763 }
764
765 #if defined(__NetBSD__) || defined(__OpenBSD__)
766 int
767 kue_activate(self, act)
768 device_ptr_t self;
769 enum devact act;
770 {
771 struct kue_softc *sc = (struct kue_softc *)self;
772
773 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
774
775 switch (act) {
776 case DVACT_ACTIVATE:
777 return (EOPNOTSUPP);
778 break;
779
780 case DVACT_DEACTIVATE:
781 /* Deactivate the interface. */
782 if_deactivate(&sc->kue_ec.ec_if);
783 sc->kue_dying = 1;
784 break;
785 }
786 return (0);
787 }
788 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
789
790 /*
791 * Initialize an RX descriptor and attach an MBUF cluster.
792 */
793 static int
794 kue_newbuf(sc, c, m)
795 struct kue_softc *sc;
796 struct kue_chain *c;
797 struct mbuf *m;
798 {
799 struct mbuf *m_new = NULL;
800
801 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
802
803 if (m == NULL) {
804 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
805 if (m_new == NULL) {
806 printf("%s: no memory for rx list "
807 "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
808 return (ENOBUFS);
809 }
810
811 MCLGET(m_new, M_DONTWAIT);
812 if (!(m_new->m_flags & M_EXT)) {
813 printf("%s: no memory for rx list "
814 "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
815 m_freem(m_new);
816 return (ENOBUFS);
817 }
818 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
819 } else {
820 m_new = m;
821 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
822 m_new->m_data = m_new->m_ext.ext_buf;
823 }
824
825 c->kue_mbuf = m_new;
826
827 return (0);
828 }
829
830 static int
831 kue_rx_list_init(sc)
832 struct kue_softc *sc;
833 {
834 struct kue_cdata *cd;
835 struct kue_chain *c;
836 int i;
837
838 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
839
840 cd = &sc->kue_cdata;
841 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
842 c = &cd->kue_rx_chain[i];
843 c->kue_sc = sc;
844 c->kue_idx = i;
845 if (kue_newbuf(sc, c, NULL) == ENOBUFS)
846 return (ENOBUFS);
847 if (c->kue_xfer == NULL) {
848 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
849 if (c->kue_xfer == NULL)
850 return (ENOBUFS);
851 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
852 if (c->kue_buf == NULL)
853 return (ENOBUFS); /* XXX free xfer */
854 }
855 }
856
857 return (0);
858 }
859
860 static int
861 kue_tx_list_init(sc)
862 struct kue_softc *sc;
863 {
864 struct kue_cdata *cd;
865 struct kue_chain *c;
866 int i;
867
868 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
869
870 cd = &sc->kue_cdata;
871 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
872 c = &cd->kue_tx_chain[i];
873 c->kue_sc = sc;
874 c->kue_idx = i;
875 c->kue_mbuf = NULL;
876 if (c->kue_xfer == NULL) {
877 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
878 if (c->kue_xfer == NULL)
879 return (ENOBUFS);
880 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
881 if (c->kue_buf == NULL)
882 return (ENOBUFS);
883 }
884 }
885
886 return (0);
887 }
888
889 #ifdef __FreeBSD__
890 static void
891 kue_rxstart(ifp)
892 struct ifnet *ifp;
893 {
894 struct kue_softc *sc;
895 struct kue_chain *c;
896
897 sc = ifp->if_softc;
898 c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
899
900 if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
901 ifp->if_ierrors++;
902 return;
903 }
904
905 /* Setup new transfer. */
906 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
907 c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
908 USBD_NO_TIMEOUT, kue_rxeof);
909 usbd_transfer(c->kue_xfer);
910 }
911 #endif
912
913 /*
914 * A frame has been uploaded: pass the resulting mbuf chain up to
915 * the higher level protocols.
916 */
917 static void
918 kue_rxeof(xfer, priv, status)
919 usbd_xfer_handle xfer;
920 usbd_private_handle priv;
921 usbd_status status;
922 {
923 struct kue_chain *c = priv;
924 struct kue_softc *sc = c->kue_sc;
925 struct ifnet *ifp = GET_IFP(sc);
926 struct mbuf *m;
927 int total_len = 0;
928 #if defined(__NetBSD__) || defined(__OpenBSD__)
929 int s;
930 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
931
932 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
933 __FUNCTION__, status));
934
935 if (sc->kue_dying)
936 return;
937
938 if (!(ifp->if_flags & IFF_RUNNING))
939 return;
940
941 if (status != USBD_NORMAL_COMPLETION) {
942 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
943 return;
944 sc->kue_rx_errs++;
945 if (usbd_ratecheck(&sc->kue_rx_notice)) {
946 printf("%s: %u usb errors on rx: %s\n",
947 USBDEVNAME(sc->kue_dev), sc->kue_rx_errs,
948 usbd_errstr(status));
949 sc->kue_rx_errs = 0;
950 }
951 if (status == USBD_STALLED)
952 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
953 goto done;
954 }
955
956 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
957
958 DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", USBDEVNAME(sc->kue_dev),
959 __FUNCTION__, total_len,
960 UGETW(mtod(c->kue_mbuf, u_int8_t *))));
961
962 m = c->kue_mbuf;
963 if (total_len <= 1)
964 goto done;
965
966 /* copy data to mbuf */
967 memcpy(mtod(m, char*), c->kue_buf, total_len);
968
969 /* No errors; receive the packet. */
970 total_len = UGETW(mtod(m, u_int8_t *));
971 m_adj(m, sizeof(u_int16_t));
972
973 if (total_len < sizeof(struct ether_header)) {
974 ifp->if_ierrors++;
975 goto done;
976 }
977
978 ifp->if_ipackets++;
979 m->m_pkthdr.len = m->m_len = total_len;
980
981 #if defined(__FreeBSD__)
982 m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
983 /* Put the packet on the special USB input queue. */
984 usb_ether_input(m);
985
986 return;
987
988 #elif defined(__NetBSD__) || defined(__OpenBSD__)
989 m->m_pkthdr.rcvif = ifp;
990
991 s = splimp();
992
993 /* XXX ugly */
994 if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
995 ifp->if_ierrors++;
996 goto done1;
997 }
998
999 #if NBPFILTER > 0
1000 /*
1001 * Handle BPF listeners. Let the BPF user see the packet, but
1002 * don't pass it up to the ether_input() layer unless it's
1003 * a broadcast packet, multicast packet, matches our ethernet
1004 * address or the interface is in promiscuous mode.
1005 */
1006 if (ifp->if_bpf) {
1007 struct ether_header *eh = mtod(m, struct ether_header *);
1008 BPF_MTAP(ifp, m);
1009 if ((ifp->if_flags & IFF_PROMISC) &&
1010 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1011 ETHER_ADDR_LEN) &&
1012 !(eh->ether_dhost[0] & 1)) {
1013 m_freem(m);
1014 goto done1;
1015 }
1016 }
1017 #endif
1018
1019 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->kue_dev),
1020 __FUNCTION__, m->m_len));
1021 (*ifp->if_input)(ifp, m);
1022 done1:
1023 splx(s);
1024 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1025
1026 done:
1027
1028 /* Setup new transfer. */
1029 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
1030 c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
1031 USBD_NO_TIMEOUT, kue_rxeof);
1032 usbd_transfer(c->kue_xfer);
1033
1034 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->kue_dev),
1035 __FUNCTION__));
1036 }
1037
1038 /*
1039 * A frame was downloaded to the chip. It's safe for us to clean up
1040 * the list buffers.
1041 */
1042
1043 static void
1044 kue_txeof(xfer, priv, status)
1045 usbd_xfer_handle xfer;
1046 usbd_private_handle priv;
1047 usbd_status status;
1048 {
1049 struct kue_chain *c = priv;
1050 struct kue_softc *sc = c->kue_sc;
1051 struct ifnet *ifp = GET_IFP(sc);
1052 int s;
1053
1054 if (sc->kue_dying)
1055 return;
1056
1057 s = splimp();
1058
1059 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
1060 __FUNCTION__, status));
1061
1062 ifp->if_timer = 0;
1063 ifp->if_flags &= ~IFF_OACTIVE;
1064
1065 if (status != USBD_NORMAL_COMPLETION) {
1066 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1067 splx(s);
1068 return;
1069 }
1070 ifp->if_oerrors++;
1071 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->kue_dev),
1072 usbd_errstr(status));
1073 if (status == USBD_STALLED)
1074 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
1075 splx(s);
1076 return;
1077 }
1078
1079 ifp->if_opackets++;
1080
1081 #if defined(__FreeBSD__)
1082 c->kue_mbuf->m_pkthdr.rcvif = ifp;
1083 usb_tx_done(c->kue_mbuf);
1084 c->kue_mbuf = NULL;
1085 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1086 m_freem(c->kue_mbuf);
1087 c->kue_mbuf = NULL;
1088
1089 if (ifp->if_snd.ifq_head != NULL)
1090 kue_start(ifp);
1091 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1092
1093 splx(s);
1094 }
1095
1096 static int
1097 kue_send(sc, m, idx)
1098 struct kue_softc *sc;
1099 struct mbuf *m;
1100 int idx;
1101 {
1102 int total_len;
1103 struct kue_chain *c;
1104 usbd_status err;
1105
1106 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
1107
1108 c = &sc->kue_cdata.kue_tx_chain[idx];
1109
1110 /*
1111 * Copy the mbuf data into a contiguous buffer, leaving two
1112 * bytes at the beginning to hold the frame length.
1113 */
1114 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
1115 c->kue_mbuf = m;
1116
1117 total_len = m->m_pkthdr.len + 2;
1118 /* XXX what's this? */
1119 total_len += 64 - (total_len % 64);
1120
1121 /* Frame length is specified in the first 2 bytes of the buffer. */
1122 c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1123 c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1124
1125 /* XXX 10000 */
1126 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
1127 c, c->kue_buf, total_len, USBD_NO_COPY, 10000, kue_txeof);
1128
1129 /* Transmit */
1130 err = usbd_transfer(c->kue_xfer);
1131 if (err != USBD_IN_PROGRESS) {
1132 kue_stop(sc);
1133 return (EIO);
1134 }
1135
1136 sc->kue_cdata.kue_tx_cnt++;
1137
1138 return (0);
1139 }
1140
1141 static void
1142 kue_start(ifp)
1143 struct ifnet *ifp;
1144 {
1145 struct kue_softc *sc = ifp->if_softc;
1146 struct mbuf *m_head = NULL;
1147
1148 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
1149
1150 if (sc->kue_dying)
1151 return;
1152
1153 if (ifp->if_flags & IFF_OACTIVE)
1154 return;
1155
1156 IF_DEQUEUE(&ifp->if_snd, m_head);
1157 if (m_head == NULL)
1158 return;
1159
1160 if (kue_send(sc, m_head, 0)) {
1161 IF_PREPEND(&ifp->if_snd, m_head);
1162 ifp->if_flags |= IFF_OACTIVE;
1163 return;
1164 }
1165
1166 #if NBPFILTER > 0
1167 /*
1168 * If there's a BPF listener, bounce a copy of this frame
1169 * to him.
1170 */
1171 if (ifp->if_bpf)
1172 BPF_MTAP(ifp, m_head);
1173 #endif
1174
1175 ifp->if_flags |= IFF_OACTIVE;
1176
1177 /*
1178 * Set a timeout in case the chip goes out to lunch.
1179 */
1180 ifp->if_timer = 5;
1181 }
1182
1183 static void
1184 kue_init(xsc)
1185 void *xsc;
1186 {
1187 struct kue_softc *sc = xsc;
1188 struct ifnet *ifp = GET_IFP(sc);
1189 int s;
1190 u_char *eaddr;
1191
1192 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
1193
1194 if (ifp->if_flags & IFF_RUNNING)
1195 return;
1196
1197 s = splimp();
1198
1199 #if defined(__FreeBSD__)
1200 eaddr = sc->arpcom.ac_enaddr;
1201 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1202 eaddr = LLADDR(ifp->if_sadl);
1203 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1204 /* Set MAC address */
1205 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
1206
1207 sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
1208
1209 /* If we want promiscuous mode, set the allframes bit. */
1210 if (ifp->if_flags & IFF_PROMISC)
1211 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1212
1213 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
1214
1215 /* I'm not sure how to tune these. */
1216 #if 0
1217 /*
1218 * Leave this one alone for now; setting it
1219 * wrong causes lockups on some machines/controllers.
1220 */
1221 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
1222 #endif
1223 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
1224
1225 /* Init TX ring. */
1226 if (kue_tx_list_init(sc) == ENOBUFS) {
1227 printf("%s: tx list init failed\n", USBDEVNAME(sc->kue_dev));
1228 splx(s);
1229 return;
1230 }
1231
1232 /* Init RX ring. */
1233 if (kue_rx_list_init(sc) == ENOBUFS) {
1234 printf("%s: rx list init failed\n", USBDEVNAME(sc->kue_dev));
1235 splx(s);
1236 return;
1237 }
1238
1239 /* Load the multicast filter. */
1240 kue_setmulti(sc);
1241
1242 if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
1243 if (kue_open_pipes(sc)) {
1244 splx(s);
1245 return;
1246 }
1247 }
1248
1249 ifp->if_flags |= IFF_RUNNING;
1250 ifp->if_flags &= ~IFF_OACTIVE;
1251
1252 splx(s);
1253 }
1254
1255 static int
1256 kue_open_pipes(sc)
1257 struct kue_softc *sc;
1258 {
1259 usbd_status err;
1260 struct kue_chain *c;
1261 int i;
1262
1263 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
1264
1265 /* Open RX and TX pipes. */
1266 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
1267 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
1268 if (err) {
1269 printf("%s: open rx pipe failed: %s\n",
1270 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1271 return (EIO);
1272 }
1273
1274 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
1275 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
1276 if (err) {
1277 printf("%s: open tx pipe failed: %s\n",
1278 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1279 return (EIO);
1280 }
1281
1282 /* Start up the receive pipe. */
1283 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1284 c = &sc->kue_cdata.kue_rx_chain[i];
1285 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
1286 c, c->kue_buf, KUE_BUFSZ,
1287 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1288 kue_rxeof);
1289 usbd_transfer(c->kue_xfer);
1290 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->kue_dev),
1291 __FUNCTION__));
1292 }
1293
1294 return (0);
1295 }
1296
1297 static int
1298 kue_ioctl(ifp, command, data)
1299 struct ifnet *ifp;
1300 u_long command;
1301 caddr_t data;
1302 {
1303 struct kue_softc *sc = ifp->if_softc;
1304 #if defined(__NetBSD__) || defined(__OpenBSD__)
1305 struct ifaddr *ifa = (struct ifaddr *)data;
1306 struct ifreq *ifr = (struct ifreq *)data;
1307 #endif
1308 int s, error = 0;
1309
1310 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
1311
1312 if (sc->kue_dying)
1313 return (EIO);
1314
1315 s = splimp();
1316
1317 switch(command) {
1318 #if defined(__FreeBSD__)
1319 case SIOCSIFADDR:
1320 case SIOCGIFADDR:
1321 case SIOCSIFMTU:
1322 error = ether_ioctl(ifp, command, data);
1323 break;
1324 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1325 case SIOCSIFADDR:
1326 ifp->if_flags |= IFF_UP;
1327 kue_init(sc);
1328
1329 switch (ifa->ifa_addr->sa_family) {
1330 #ifdef INET
1331 case AF_INET:
1332 arp_ifinit(ifp, ifa);
1333 break;
1334 #endif /* INET */
1335 #ifdef NS
1336 case AF_NS:
1337 {
1338 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1339
1340 if (ns_nullhost(*ina))
1341 ina->x_host = *(union ns_host *)
1342 LLADDR(ifp->if_sadl);
1343 else
1344 memcpy(LLADDR(ifp->if_sadl),
1345 ina->x_host.c_host,
1346 ifp->if_addrlen);
1347 break;
1348 }
1349 #endif /* NS */
1350 }
1351 break;
1352
1353 case SIOCSIFMTU:
1354 if (ifr->ifr_mtu > ETHERMTU)
1355 error = EINVAL;
1356 else
1357 ifp->if_mtu = ifr->ifr_mtu;
1358 break;
1359
1360 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1361
1362 case SIOCSIFFLAGS:
1363 if (ifp->if_flags & IFF_UP) {
1364 if (ifp->if_flags & IFF_RUNNING &&
1365 ifp->if_flags & IFF_PROMISC &&
1366 !(sc->kue_if_flags & IFF_PROMISC)) {
1367 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1368 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1369 sc->kue_rxfilt);
1370 } else if (ifp->if_flags & IFF_RUNNING &&
1371 !(ifp->if_flags & IFF_PROMISC) &&
1372 sc->kue_if_flags & IFF_PROMISC) {
1373 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1374 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1375 sc->kue_rxfilt);
1376 } else if (!(ifp->if_flags & IFF_RUNNING))
1377 kue_init(sc);
1378 } else {
1379 if (ifp->if_flags & IFF_RUNNING)
1380 kue_stop(sc);
1381 }
1382 sc->kue_if_flags = ifp->if_flags;
1383 error = 0;
1384 break;
1385 case SIOCADDMULTI:
1386 case SIOCDELMULTI:
1387 kue_setmulti(sc);
1388 error = 0;
1389 break;
1390 default:
1391 error = EINVAL;
1392 break;
1393 }
1394
1395 splx(s);
1396
1397 return (error);
1398 }
1399
1400 static void
1401 kue_watchdog(ifp)
1402 struct ifnet *ifp;
1403 {
1404 struct kue_softc *sc = ifp->if_softc;
1405
1406 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
1407
1408 if (sc->kue_dying)
1409 return;
1410
1411 ifp->if_oerrors++;
1412 printf("%s: watchdog timeout\n", USBDEVNAME(sc->kue_dev));
1413
1414 /*
1415 * The polling business is a kludge to avoid allowing the
1416 * USB code to call tsleep() in usbd_delay_ms(), which will
1417 * kill us since the watchdog routine is invoked from
1418 * interrupt context.
1419 */
1420 usbd_set_polling(sc->kue_udev, 1);
1421 kue_stop(sc);
1422 kue_init(sc);
1423 usbd_set_polling(sc->kue_udev, 0);
1424
1425 if (ifp->if_snd.ifq_head != NULL)
1426 kue_start(ifp);
1427 }
1428
1429 /*
1430 * Stop the adapter and free any mbufs allocated to the
1431 * RX and TX lists.
1432 */
1433 static void
1434 kue_stop(sc)
1435 struct kue_softc *sc;
1436 {
1437 usbd_status err;
1438 struct ifnet *ifp;
1439 int i;
1440
1441 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
1442
1443 ifp = GET_IFP(sc);
1444 ifp->if_timer = 0;
1445
1446 /* Stop transfers. */
1447 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1448 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1449 if (err) {
1450 printf("%s: abort rx pipe failed: %s\n",
1451 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1452 }
1453 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1454 if (err) {
1455 printf("%s: close rx pipe failed: %s\n",
1456 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1457 }
1458 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1459 }
1460
1461 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1462 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1463 if (err) {
1464 printf("%s: abort tx pipe failed: %s\n",
1465 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1466 }
1467 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1468 if (err) {
1469 printf("%s: close tx pipe failed: %s\n",
1470 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1471 }
1472 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1473 }
1474
1475 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1476 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1477 if (err) {
1478 printf("%s: abort intr pipe failed: %s\n",
1479 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1480 }
1481 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1482 if (err) {
1483 printf("%s: close intr pipe failed: %s\n",
1484 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1485 }
1486 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1487 }
1488
1489 /* Free RX resources. */
1490 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1491 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1492 m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1493 sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1494 }
1495 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1496 usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1497 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1498 }
1499 }
1500
1501 /* Free TX resources. */
1502 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1503 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1504 m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1505 sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1506 }
1507 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1508 usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1509 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1510 }
1511 }
1512
1513 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1514 }
1515
1516 #ifdef __FreeBSD__
1517 /*
1518 * Stop all chip I/O so that the kernel's probe routines don't
1519 * get confused by errant DMAs when rebooting.
1520 */
1521 static void
1522 kue_shutdown(dev)
1523 device_t dev;
1524 {
1525 struct kue_softc *sc;
1526
1527 sc = device_get_softc(dev);
1528
1529 kue_stop(sc);
1530 }
1531 #endif
1532