if_kue.c revision 1.68.4.2 1 /* $NetBSD: if_kue.c,v 1.68.4.2 2011/03/05 20:54:11 rmind 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 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1.68.4.2 2011/03/05 20:54:11 rmind Exp $");
74
75 #include "opt_inet.h"
76 #include "rnd.h"
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/sockio.h>
81 #include <sys/mbuf.h>
82 #include <sys/malloc.h>
83 #include <sys/kernel.h>
84 #include <sys/socket.h>
85 #include <sys/device.h>
86 #include <sys/proc.h>
87
88 #if NRND > 0
89 #include <sys/rnd.h>
90 #endif
91
92 #include <net/if.h>
93 #include <net/if_arp.h>
94 #include <net/if_dl.h>
95
96 #include <net/bpf.h>
97
98 #include <net/if_ether.h>
99 #ifdef INET
100 #include <netinet/in.h>
101 #include <netinet/if_inarp.h>
102 #endif
103
104
105 #include <dev/usb/usb.h>
106 #include <dev/usb/usbdi.h>
107 #include <dev/usb/usbdi_util.h>
108 #include <dev/usb/usbdevs.h>
109
110 #include <dev/usb/if_kuereg.h>
111 #include <dev/usb/kue_fw.h>
112
113 #ifdef KUE_DEBUG
114 #define DPRINTF(x) if (kuedebug) printf x
115 #define DPRINTFN(n,x) if (kuedebug >= (n)) printf x
116 int kuedebug = 0;
117 #else
118 #define DPRINTF(x)
119 #define DPRINTFN(n,x)
120 #endif
121
122 /*
123 * Various supported device vendors/products.
124 */
125 static const struct usb_devno kue_devs[] = {
126 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
127 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
128 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
129 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
130 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX },
131 { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
132 { USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA },
133 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
134 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C },
135 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
136 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
137 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
138 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1 },
139 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2 },
140 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
141 { USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA },
142 { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1 },
143 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
144 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
145 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
146 { USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA },
147 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
148 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X },
149 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
150 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
151 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
152 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8 },
153 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9 },
154 { USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA },
155 { USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA },
156 { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E },
157 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
158 };
159 #define kue_lookup(v, p) (usb_lookup(kue_devs, v, p))
160
161 int kue_match(device_t, cfdata_t, void *);
162 void kue_attach(device_t, device_t, void *);
163 int kue_detach(device_t, int);
164 int kue_activate(device_t, enum devact);
165 extern struct cfdriver kue_cd;
166 CFATTACH_DECL_NEW(kue, sizeof(struct kue_softc), kue_match, kue_attach,
167 kue_detach, kue_activate);
168
169 static int kue_tx_list_init(struct kue_softc *);
170 static int kue_rx_list_init(struct kue_softc *);
171 static int kue_send(struct kue_softc *, struct mbuf *, int);
172 static int kue_open_pipes(struct kue_softc *);
173 static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
174 static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
175 static void kue_start(struct ifnet *);
176 static int kue_ioctl(struct ifnet *, u_long, void *);
177 static void kue_init(void *);
178 static void kue_stop(struct kue_softc *);
179 static void kue_watchdog(struct ifnet *);
180
181 static void kue_setmulti(struct kue_softc *);
182 static void kue_reset(struct kue_softc *);
183
184 static usbd_status kue_ctl(struct kue_softc *, int, uint8_t,
185 uint16_t, void *, uint32_t);
186 static usbd_status kue_setword(struct kue_softc *, uint8_t, uint16_t);
187 static int kue_load_fw(struct kue_softc *);
188
189 static usbd_status
190 kue_setword(struct kue_softc *sc, uint8_t breq, uint16_t word)
191 {
192 usb_device_request_t req;
193
194 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
195
196 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
197 req.bRequest = breq;
198 USETW(req.wValue, word);
199 USETW(req.wIndex, 0);
200 USETW(req.wLength, 0);
201
202 return (usbd_do_request(sc->kue_udev, &req, NULL));
203 }
204
205 static usbd_status
206 kue_ctl(struct kue_softc *sc, int rw, uint8_t breq, uint16_t val,
207 void *data, uint32_t len)
208 {
209 usb_device_request_t req;
210
211 DPRINTFN(10,("%s: %s: enter, len=%d\n", device_xname(sc->kue_dev),
212 __func__, len));
213
214 if (rw == KUE_CTL_WRITE)
215 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
216 else
217 req.bmRequestType = UT_READ_VENDOR_DEVICE;
218
219 req.bRequest = breq;
220 USETW(req.wValue, val);
221 USETW(req.wIndex, 0);
222 USETW(req.wLength, len);
223
224 return (usbd_do_request(sc->kue_udev, &req, data));
225 }
226
227 static int
228 kue_load_fw(struct kue_softc *sc)
229 {
230 usb_device_descriptor_t dd;
231 usbd_status err;
232
233 DPRINTFN(1,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
234
235 /*
236 * First, check if we even need to load the firmware.
237 * If the device was still attached when the system was
238 * rebooted, it may already have firmware loaded in it.
239 * If this is the case, we don't need to do it again.
240 * And in fact, if we try to load it again, we'll hang,
241 * so we have to avoid this condition if we don't want
242 * to look stupid.
243 *
244 * We can test this quickly by checking the bcdRevision
245 * code. The NIC will return a different revision code if
246 * it's probed while the firmware is still loaded and
247 * running.
248 */
249 if (usbd_get_device_desc(sc->kue_udev, &dd))
250 return (EIO);
251 if (UGETW(dd.bcdDevice) == KUE_WARM_REV) {
252 printf("%s: warm boot, no firmware download\n",
253 device_xname(sc->kue_dev));
254 return (0);
255 }
256
257 printf("%s: cold boot, downloading firmware\n",
258 device_xname(sc->kue_dev));
259
260 /* Load code segment */
261 DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
262 device_xname(sc->kue_dev)));
263 /*XXXUNCONST*/
264 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
265 0, __UNCONST(kue_code_seg), sizeof(kue_code_seg));
266 if (err) {
267 printf("%s: failed to load code segment: %s\n",
268 device_xname(sc->kue_dev), usbd_errstr(err));
269 return (EIO);
270 }
271
272 /* Load fixup segment */
273 DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
274 device_xname(sc->kue_dev)));
275 /*XXXUNCONST*/
276 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
277 0, __UNCONST(kue_fix_seg), sizeof(kue_fix_seg));
278 if (err) {
279 printf("%s: failed to load fixup segment: %s\n",
280 device_xname(sc->kue_dev), usbd_errstr(err));
281 return (EIO);
282 }
283
284 /* Send trigger command. */
285 DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
286 device_xname(sc->kue_dev)));
287 /*XXXUNCONST*/
288 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
289 0, __UNCONST(kue_trig_seg), sizeof(kue_trig_seg));
290 if (err) {
291 printf("%s: failed to load trigger segment: %s\n",
292 device_xname(sc->kue_dev), usbd_errstr(err));
293 return (EIO);
294 }
295
296 usbd_delay_ms(sc->kue_udev, 10);
297
298 /*
299 * Reload device descriptor.
300 * Why? The chip without the firmware loaded returns
301 * one revision code. The chip with the firmware
302 * loaded and running returns a *different* revision
303 * code. This confuses the quirk mechanism, which is
304 * dependent on the revision data.
305 */
306 (void)usbd_reload_device_desc(sc->kue_udev);
307
308 DPRINTFN(1,("%s: %s: done\n", device_xname(sc->kue_dev), __func__));
309
310 /* Reset the adapter. */
311 kue_reset(sc);
312
313 return (0);
314 }
315
316 static void
317 kue_setmulti(struct kue_softc *sc)
318 {
319 struct ifnet *ifp = GET_IFP(sc);
320 struct ether_multi *enm;
321 struct ether_multistep step;
322 int i;
323
324 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
325
326 if (ifp->if_flags & IFF_PROMISC) {
327 allmulti:
328 ifp->if_flags |= IFF_ALLMULTI;
329 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
330 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
331 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
332 return;
333 }
334
335 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
336
337 i = 0;
338 ETHER_FIRST_MULTI(step, &sc->kue_ec, enm);
339 while (enm != NULL) {
340 if (i == KUE_MCFILTCNT(sc) ||
341 memcmp(enm->enm_addrlo, enm->enm_addrhi,
342 ETHER_ADDR_LEN) != 0)
343 goto allmulti;
344
345 memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN);
346 ETHER_NEXT_MULTI(step, enm);
347 i++;
348 }
349
350 ifp->if_flags &= ~IFF_ALLMULTI;
351
352 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
353 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
354 i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
355
356 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
357 }
358
359 /*
360 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
361 * done after the firmware is loaded into the adapter in order to
362 * bring it into proper operation.
363 */
364 static void
365 kue_reset(struct kue_softc *sc)
366 {
367 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
368
369 if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) ||
370 usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
371 &sc->kue_iface))
372 printf("%s: reset failed\n", device_xname(sc->kue_dev));
373
374 /* Wait a little while for the chip to get its brains in order. */
375 usbd_delay_ms(sc->kue_udev, 10);
376 }
377
378 /*
379 * Probe for a KLSI chip.
380 */
381 int
382 kue_match(device_t parent, cfdata_t match, void *aux)
383 {
384 struct usb_attach_arg *uaa = aux;
385
386 DPRINTFN(25,("kue_match: enter\n"));
387
388 return (kue_lookup(uaa->vendor, uaa->product) != NULL ?
389 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
390 }
391
392 /*
393 * Attach the interface. Allocate softc structures, do
394 * setup and ethernet/BPF attach.
395 */
396 void
397 kue_attach(device_t parent, device_t self, void *aux)
398 {
399 struct kue_softc *sc = device_private(self);
400 struct usb_attach_arg *uaa = aux;
401 char *devinfop;
402 int s;
403 struct ifnet *ifp;
404 usbd_device_handle dev = uaa->device;
405 usbd_interface_handle iface;
406 usbd_status err;
407 usb_interface_descriptor_t *id;
408 usb_endpoint_descriptor_t *ed;
409 int i;
410
411 DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
412
413 sc->kue_dev = self;
414
415 aprint_naive("\n");
416 aprint_normal("\n");
417
418 devinfop = usbd_devinfo_alloc(dev, 0);
419 aprint_normal_dev(self, "%s\n", devinfop);
420 usbd_devinfo_free(devinfop);
421
422 err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
423 if (err) {
424 aprint_error_dev(self, " setting config no failed\n");
425 return;
426 }
427
428 sc->kue_udev = dev;
429 sc->kue_product = uaa->product;
430 sc->kue_vendor = uaa->vendor;
431
432 /* Load the firmware into the NIC. */
433 if (kue_load_fw(sc)) {
434 aprint_error_dev(self, "loading firmware failed\n");
435 return;
436 }
437
438 err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
439 if (err) {
440 aprint_error_dev(self, "getting interface handle failed\n");
441 return;
442 }
443
444 sc->kue_iface = iface;
445 id = usbd_get_interface_descriptor(iface);
446
447 /* Find endpoints. */
448 for (i = 0; i < id->bNumEndpoints; i++) {
449 ed = usbd_interface2endpoint_descriptor(iface, i);
450 if (ed == NULL) {
451 aprint_error_dev(self, "couldn't get ep %d\n", i);
452 return;
453 }
454 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
455 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
456 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
457 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
458 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
459 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
460 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
461 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
462 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
463 }
464 }
465
466 if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
467 aprint_error_dev(self, "missing endpoint\n");
468 return;
469 }
470
471 /* Read ethernet descriptor */
472 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
473 0, &sc->kue_desc, sizeof(sc->kue_desc));
474 if (err) {
475 aprint_error_dev(self, "could not read Ethernet descriptor\n");
476 return;
477 }
478
479 sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
480 M_USBDEV, M_NOWAIT);
481 if (sc->kue_mcfilters == NULL) {
482 aprint_error_dev(self,
483 "no memory for multicast filter buffer\n");
484 return;
485 }
486
487 s = splnet();
488
489 /*
490 * A KLSI chip was detected. Inform the world.
491 */
492 aprint_normal_dev(self, "Ethernet address %s\n",
493 ether_sprintf(sc->kue_desc.kue_macaddr));
494
495 /* Initialize interface info.*/
496 ifp = GET_IFP(sc);
497 ifp->if_softc = sc;
498 ifp->if_mtu = ETHERMTU;
499 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
500 ifp->if_ioctl = kue_ioctl;
501 ifp->if_start = kue_start;
502 ifp->if_watchdog = kue_watchdog;
503 strncpy(ifp->if_xname, device_xname(sc->kue_dev), IFNAMSIZ);
504
505 IFQ_SET_READY(&ifp->if_snd);
506
507 /* Attach the interface. */
508 if_attach(ifp);
509 ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
510 #if NRND > 0
511 rnd_attach_source(&sc->rnd_source, device_xname(sc->kue_dev),
512 RND_TYPE_NET, 0);
513 #endif
514
515 sc->kue_attached = true;
516 splx(s);
517
518 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
519 sc->kue_dev);
520
521 return;
522 }
523
524 int
525 kue_detach(device_t self, int flags)
526 {
527 struct kue_softc *sc = device_private(self);
528 struct ifnet *ifp = GET_IFP(sc);
529 int s;
530
531 s = splusb(); /* XXX why? */
532
533 if (sc->kue_mcfilters != NULL) {
534 free(sc->kue_mcfilters, M_USBDEV);
535 sc->kue_mcfilters = NULL;
536 }
537
538 if (!sc->kue_attached) {
539 /* Detached before attached finished, so just bail out. */
540 splx(s);
541 return (0);
542 }
543
544 if (ifp->if_flags & IFF_RUNNING)
545 kue_stop(sc);
546
547 #if NRND > 0
548 rnd_detach_source(&sc->rnd_source);
549 #endif
550 ether_ifdetach(ifp);
551
552 if_detach(ifp);
553
554 #ifdef DIAGNOSTIC
555 if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
556 sc->kue_ep[KUE_ENDPT_RX] != NULL ||
557 sc->kue_ep[KUE_ENDPT_INTR] != NULL)
558 aprint_debug_dev(self, "detach has active endpoints\n");
559 #endif
560
561 sc->kue_attached = false;
562 splx(s);
563
564 return (0);
565 }
566
567 int
568 kue_activate(device_t self, enum devact act)
569 {
570 struct kue_softc *sc = device_private(self);
571
572 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
573
574 switch (act) {
575 case DVACT_DEACTIVATE:
576 /* Deactivate the interface. */
577 if_deactivate(&sc->kue_ec.ec_if);
578 sc->kue_dying = true;
579 return 0;
580 default:
581 return EOPNOTSUPP;
582 }
583 }
584
585 static int
586 kue_rx_list_init(struct kue_softc *sc)
587 {
588 struct kue_cdata *cd;
589 struct kue_chain *c;
590 int i;
591
592 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
593
594 cd = &sc->kue_cdata;
595 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
596 c = &cd->kue_rx_chain[i];
597 c->kue_sc = sc;
598 c->kue_idx = i;
599 if (c->kue_xfer == NULL) {
600 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
601 if (c->kue_xfer == NULL)
602 return (ENOBUFS);
603 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
604 if (c->kue_buf == NULL)
605 return (ENOBUFS); /* XXX free xfer */
606 }
607 }
608
609 return (0);
610 }
611
612 static int
613 kue_tx_list_init(struct kue_softc *sc)
614 {
615 struct kue_cdata *cd;
616 struct kue_chain *c;
617 int i;
618
619 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
620
621 cd = &sc->kue_cdata;
622 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
623 c = &cd->kue_tx_chain[i];
624 c->kue_sc = sc;
625 c->kue_idx = i;
626 if (c->kue_xfer == NULL) {
627 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
628 if (c->kue_xfer == NULL)
629 return (ENOBUFS);
630 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
631 if (c->kue_buf == NULL)
632 return (ENOBUFS);
633 }
634 }
635
636 return (0);
637 }
638
639 /*
640 * A frame has been uploaded: pass the resulting mbuf chain up to
641 * the higher level protocols.
642 */
643 static void
644 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
645 {
646 struct kue_chain *c = priv;
647 struct kue_softc *sc = c->kue_sc;
648 struct ifnet *ifp = GET_IFP(sc);
649 struct mbuf *m;
650 int total_len, pktlen;
651 int s;
652
653 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev),
654 __func__, status));
655
656 if (sc->kue_dying)
657 return;
658
659 if (!(ifp->if_flags & IFF_RUNNING))
660 return;
661
662 if (status != USBD_NORMAL_COMPLETION) {
663 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
664 return;
665 sc->kue_rx_errs++;
666 if (usbd_ratecheck(&sc->kue_rx_notice)) {
667 printf("%s: %u usb errors on rx: %s\n",
668 device_xname(sc->kue_dev), sc->kue_rx_errs,
669 usbd_errstr(status));
670 sc->kue_rx_errs = 0;
671 }
672 if (status == USBD_STALLED)
673 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]);
674 goto done;
675 }
676
677 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
678
679 DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", device_xname(sc->kue_dev),
680 __func__, total_len,
681 le16dec(c->kue_buf)));
682
683 if (total_len <= 1)
684 goto done;
685
686 pktlen = le16dec(c->kue_buf);
687 if (pktlen > total_len - 2)
688 pktlen = total_len - 2;
689
690 if (pktlen < ETHER_MIN_LEN - ETHER_CRC_LEN ||
691 pktlen > MCLBYTES - ETHER_ALIGN) {
692 ifp->if_ierrors++;
693 goto done;
694 }
695
696 /* No errors; receive the packet. */
697 MGETHDR(m, M_DONTWAIT, MT_DATA);
698 if (m == NULL) {
699 ifp->if_ierrors++;
700 goto done;
701 }
702 if (pktlen > MHLEN - ETHER_ALIGN) {
703 MCLGET(m, M_DONTWAIT);
704 if ((m->m_flags & M_EXT) == 0) {
705 m_freem(m);
706 ifp->if_ierrors++;
707 goto done;
708 }
709 }
710 m->m_data += ETHER_ALIGN;
711
712 /* copy data to mbuf */
713 memcpy(mtod(m, uint8_t *), c->kue_buf + 2, pktlen);
714
715 ifp->if_ipackets++;
716 m->m_pkthdr.len = m->m_len = pktlen;
717 m->m_pkthdr.rcvif = ifp;
718
719 s = splnet();
720
721 /*
722 * Handle BPF listeners. Let the BPF user see the packet, but
723 * don't pass it up to the ether_input() layer unless it's
724 * a broadcast packet, multicast packet, matches our ethernet
725 * address or the interface is in promiscuous mode.
726 */
727 bpf_mtap(ifp, m);
728
729 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->kue_dev),
730 __func__, m->m_len));
731 (*ifp->if_input)(ifp, m);
732
733 splx(s);
734
735 done:
736
737 /* Setup new transfer. */
738 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
739 c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
740 USBD_NO_TIMEOUT, kue_rxeof);
741 usbd_transfer(c->kue_xfer);
742
743 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->kue_dev),
744 __func__));
745 }
746
747 /*
748 * A frame was downloaded to the chip. It's safe for us to clean up
749 * the list buffers.
750 */
751
752 static void
753 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
754 usbd_status status)
755 {
756 struct kue_chain *c = priv;
757 struct kue_softc *sc = c->kue_sc;
758 struct ifnet *ifp = GET_IFP(sc);
759 int s;
760
761 if (sc->kue_dying)
762 return;
763
764 s = splnet();
765
766 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev),
767 __func__, status));
768
769 ifp->if_timer = 0;
770 ifp->if_flags &= ~IFF_OACTIVE;
771
772 if (status != USBD_NORMAL_COMPLETION) {
773 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
774 splx(s);
775 return;
776 }
777 ifp->if_oerrors++;
778 printf("%s: usb error on tx: %s\n", device_xname(sc->kue_dev),
779 usbd_errstr(status));
780 if (status == USBD_STALLED)
781 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
782 splx(s);
783 return;
784 }
785
786 ifp->if_opackets++;
787
788 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
789 kue_start(ifp);
790
791 splx(s);
792 }
793
794 static int
795 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
796 {
797 int total_len;
798 struct kue_chain *c;
799 usbd_status err;
800
801 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
802
803 c = &sc->kue_cdata.kue_tx_chain[idx];
804
805 /* Frame length is specified in the first 2 bytes of the buffer. */
806 le16enc(c->kue_buf, (uint16_t)m->m_pkthdr.len);
807
808 /*
809 * Copy the mbuf data into a contiguous buffer, leaving two
810 * bytes at the beginning to hold the frame length.
811 */
812 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
813
814 total_len = 2 + m->m_pkthdr.len;
815 total_len = roundup2(total_len, 64);
816
817 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
818 c, c->kue_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
819 kue_txeof);
820
821 /* Transmit */
822 err = usbd_transfer(c->kue_xfer);
823 if (err != USBD_IN_PROGRESS) {
824 printf("%s: kue_send error=%s\n", device_xname(sc->kue_dev),
825 usbd_errstr(err));
826 kue_stop(sc);
827 return (EIO);
828 }
829
830 sc->kue_cdata.kue_tx_cnt++;
831
832 return (0);
833 }
834
835 static void
836 kue_start(struct ifnet *ifp)
837 {
838 struct kue_softc *sc = ifp->if_softc;
839 struct mbuf *m;
840
841 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
842
843 if (sc->kue_dying)
844 return;
845
846 if (ifp->if_flags & IFF_OACTIVE)
847 return;
848
849 IFQ_POLL(&ifp->if_snd, m);
850 if (m == NULL)
851 return;
852
853 if (kue_send(sc, m, 0)) {
854 ifp->if_flags |= IFF_OACTIVE;
855 return;
856 }
857
858 IFQ_DEQUEUE(&ifp->if_snd, m);
859
860 /*
861 * If there's a BPF listener, bounce a copy of this frame
862 * to him.
863 */
864 bpf_mtap(ifp, m);
865 m_freem(m);
866
867 ifp->if_flags |= IFF_OACTIVE;
868
869 /*
870 * Set a timeout in case the chip goes out to lunch.
871 */
872 ifp->if_timer = 6;
873 }
874
875 static void
876 kue_init(void *xsc)
877 {
878 struct kue_softc *sc = xsc;
879 struct ifnet *ifp = GET_IFP(sc);
880 int s;
881 uint8_t eaddr[ETHER_ADDR_LEN];
882
883 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
884
885 if (ifp->if_flags & IFF_RUNNING)
886 return;
887
888 s = splnet();
889
890 memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
891 /* Set MAC address */
892 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
893
894 sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
895
896 /* If we want promiscuous mode, set the allframes bit. */
897 if (ifp->if_flags & IFF_PROMISC)
898 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
899
900 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
901
902 /* I'm not sure how to tune these. */
903 #if 0
904 /*
905 * Leave this one alone for now; setting it
906 * wrong causes lockups on some machines/controllers.
907 */
908 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
909 #endif
910 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
911
912 /* Init TX ring. */
913 if (kue_tx_list_init(sc) == ENOBUFS) {
914 printf("%s: tx list init failed\n", device_xname(sc->kue_dev));
915 splx(s);
916 return;
917 }
918
919 /* Init RX ring. */
920 if (kue_rx_list_init(sc) == ENOBUFS) {
921 printf("%s: rx list init failed\n", device_xname(sc->kue_dev));
922 splx(s);
923 return;
924 }
925
926 /* Load the multicast filter. */
927 kue_setmulti(sc);
928
929 if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
930 if (kue_open_pipes(sc)) {
931 splx(s);
932 return;
933 }
934 }
935
936 ifp->if_flags |= IFF_RUNNING;
937 ifp->if_flags &= ~IFF_OACTIVE;
938
939 splx(s);
940 }
941
942 static int
943 kue_open_pipes(struct kue_softc *sc)
944 {
945 usbd_status err;
946 struct kue_chain *c;
947 int i;
948
949 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
950
951 /* Open RX and TX pipes. */
952 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
953 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
954 if (err) {
955 printf("%s: open rx pipe failed: %s\n",
956 device_xname(sc->kue_dev), usbd_errstr(err));
957 return (EIO);
958 }
959
960 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
961 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
962 if (err) {
963 printf("%s: open tx pipe failed: %s\n",
964 device_xname(sc->kue_dev), usbd_errstr(err));
965 return (EIO);
966 }
967
968 /* Start up the receive pipe. */
969 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
970 c = &sc->kue_cdata.kue_rx_chain[i];
971 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
972 c, c->kue_buf, KUE_BUFSZ,
973 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
974 kue_rxeof);
975 DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->kue_dev),
976 __func__));
977 usbd_transfer(c->kue_xfer);
978 }
979
980 return (0);
981 }
982
983 static int
984 kue_ioctl(struct ifnet *ifp, u_long command, void *data)
985 {
986 struct kue_softc *sc = ifp->if_softc;
987 struct ifaddr *ifa = (struct ifaddr *)data;
988 struct ifreq *ifr = (struct ifreq *)data;
989 int s, error = 0;
990
991 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
992
993 if (sc->kue_dying)
994 return (EIO);
995
996 #ifdef DIAGNOSTIC
997 if (!curproc) {
998 printf("%s: no proc!!\n", device_xname(sc->kue_dev));
999 return EIO;
1000 }
1001 #endif
1002
1003 s = splnet();
1004
1005 switch(command) {
1006 case SIOCINITIFADDR:
1007 ifp->if_flags |= IFF_UP;
1008 kue_init(sc);
1009
1010 switch (ifa->ifa_addr->sa_family) {
1011 #ifdef INET
1012 case AF_INET:
1013 arp_ifinit(ifp, ifa);
1014 break;
1015 #endif /* INET */
1016 }
1017 break;
1018
1019 case SIOCSIFMTU:
1020 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
1021 error = EINVAL;
1022 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
1023 error = 0;
1024 break;
1025
1026 case SIOCSIFFLAGS:
1027 if ((error = ifioctl_common(ifp, command, data)) != 0)
1028 break;
1029 if (ifp->if_flags & IFF_UP) {
1030 if (ifp->if_flags & IFF_RUNNING &&
1031 ifp->if_flags & IFF_PROMISC &&
1032 !(sc->kue_if_flags & IFF_PROMISC)) {
1033 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1034 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1035 sc->kue_rxfilt);
1036 } else if (ifp->if_flags & IFF_RUNNING &&
1037 !(ifp->if_flags & IFF_PROMISC) &&
1038 sc->kue_if_flags & IFF_PROMISC) {
1039 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1040 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1041 sc->kue_rxfilt);
1042 } else if (!(ifp->if_flags & IFF_RUNNING))
1043 kue_init(sc);
1044 } else {
1045 if (ifp->if_flags & IFF_RUNNING)
1046 kue_stop(sc);
1047 }
1048 sc->kue_if_flags = ifp->if_flags;
1049 error = 0;
1050 break;
1051 case SIOCADDMULTI:
1052 case SIOCDELMULTI:
1053 kue_setmulti(sc);
1054 error = 0;
1055 break;
1056 default:
1057 error = ether_ioctl(ifp, command, data);
1058 break;
1059 }
1060
1061 splx(s);
1062
1063 return (error);
1064 }
1065
1066 static void
1067 kue_watchdog(struct ifnet *ifp)
1068 {
1069 struct kue_softc *sc = ifp->if_softc;
1070 struct kue_chain *c;
1071 usbd_status stat;
1072 int s;
1073
1074 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
1075
1076 if (sc->kue_dying)
1077 return;
1078
1079 ifp->if_oerrors++;
1080 printf("%s: watchdog timeout\n", device_xname(sc->kue_dev));
1081
1082 s = splusb();
1083 c = &sc->kue_cdata.kue_tx_chain[0];
1084 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
1085 kue_txeof(c->kue_xfer, c, stat);
1086
1087 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1088 kue_start(ifp);
1089 splx(s);
1090 }
1091
1092 /*
1093 * Stop the adapter and free any mbufs allocated to the
1094 * RX and TX lists.
1095 */
1096 static void
1097 kue_stop(struct kue_softc *sc)
1098 {
1099 usbd_status err;
1100 struct ifnet *ifp;
1101 int i;
1102
1103 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
1104
1105 ifp = GET_IFP(sc);
1106 ifp->if_timer = 0;
1107
1108 /* Stop transfers. */
1109 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1110 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1111 if (err) {
1112 printf("%s: abort rx pipe failed: %s\n",
1113 device_xname(sc->kue_dev), usbd_errstr(err));
1114 }
1115 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1116 if (err) {
1117 printf("%s: close rx pipe failed: %s\n",
1118 device_xname(sc->kue_dev), usbd_errstr(err));
1119 }
1120 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1121 }
1122
1123 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1124 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1125 if (err) {
1126 printf("%s: abort tx pipe failed: %s\n",
1127 device_xname(sc->kue_dev), usbd_errstr(err));
1128 }
1129 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1130 if (err) {
1131 printf("%s: close tx pipe failed: %s\n",
1132 device_xname(sc->kue_dev), usbd_errstr(err));
1133 }
1134 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1135 }
1136
1137 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1138 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1139 if (err) {
1140 printf("%s: abort intr pipe failed: %s\n",
1141 device_xname(sc->kue_dev), usbd_errstr(err));
1142 }
1143 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1144 if (err) {
1145 printf("%s: close intr pipe failed: %s\n",
1146 device_xname(sc->kue_dev), usbd_errstr(err));
1147 }
1148 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1149 }
1150
1151 /* Free RX resources. */
1152 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1153 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1154 usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1155 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1156 }
1157 }
1158
1159 /* Free TX resources. */
1160 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1161 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1162 usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1163 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1164 }
1165 }
1166
1167 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1168 }
1169