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