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