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