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