1 /* $NetBSD: if_kue.c,v 1.66 2009/09/23 19:07:19 plunky Exp $ */
3 * Copyright (c) 1997, 1998, 1999, 2000
4 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
33 * $FreeBSD: src/sys/dev/usb/if_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $
37 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
39 * Written by Bill Paul <wpaul@ee.columbia.edu>
40 * Electrical Engineering Department
41 * Columbia University, New York City
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.
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.
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.
69 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1.66 2009/09/23 19:07:19 plunky Exp $");
75 #if defined(__NetBSD__)
79 #elif defined(__OpenBSD__)
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/sockio.h>
87 #include <sys/malloc.h>
88 #include <sys/kernel.h>
89 #include <sys/socket.h>
90 #include <sys/device.h>
98 #if defined(__NetBSD__)
99 #include <net/if_arp.h>
101 #include <net/if_dl.h>
107 #if defined(__NetBSD__)
108 #include <net/if_ether.h>
110 #include <netinet/in.h>
111 #include <netinet/if_inarp.h>
113 #endif /* defined (__NetBSD__) */
115 #if defined(__OpenBSD__)
117 #include <netinet/in.h>
118 #include <netinet/in_systm.h>
119 #include <netinet/in_var.h>
120 #include <netinet/ip.h>
121 #include <netinet/if_ether.h>
123 #endif /* defined (__OpenBSD__) */
126 #include <dev/usb/usb.h>
127 #include <dev/usb/usbdi.h>
128 #include <dev/usb/usbdi_util.h>
129 #include <dev/usb/usbdevs.h>
131 #include <dev/usb/if_kuereg.h>
132 #include <dev/usb/kue_fw.h>
135 #define DPRINTF(x) if (kuedebug) logprintf x
136 #define DPRINTFN(n,x) if (kuedebug >= (n)) logprintf x
140 #define DPRINTFN(n,x)
144 * Various supported device vendors/products.
146 Static
const struct usb_devno kue_devs
[] = {
147 { USB_VENDOR_3COM
, USB_PRODUCT_3COM_3C19250
},
148 { USB_VENDOR_3COM
, USB_PRODUCT_3COM_3C460
},
149 { USB_VENDOR_ABOCOM
, USB_PRODUCT_ABOCOM_URE450
},
150 { USB_VENDOR_ADS
, USB_PRODUCT_ADS_UBS10BT
},
151 { USB_VENDOR_ADS
, USB_PRODUCT_ADS_UBS10BTX
},
152 { USB_VENDOR_AOX
, USB_PRODUCT_AOX_USB101
},
153 { USB_VENDOR_ASANTE
, USB_PRODUCT_ASANTE_EA
},
154 { USB_VENDOR_ATEN
, USB_PRODUCT_ATEN_UC10T
},
155 { USB_VENDOR_ATEN
, USB_PRODUCT_ATEN_DSB650C
},
156 { USB_VENDOR_COREGA
, USB_PRODUCT_COREGA_ETHER_USB_T
},
157 { USB_VENDOR_DLINK
, USB_PRODUCT_DLINK_DSB650C
},
158 { USB_VENDOR_ENTREGA
, USB_PRODUCT_ENTREGA_E45
},
159 { USB_VENDOR_ENTREGA
, USB_PRODUCT_ENTREGA_XX1
},
160 { USB_VENDOR_ENTREGA
, USB_PRODUCT_ENTREGA_XX2
},
161 { USB_VENDOR_IODATA
, USB_PRODUCT_IODATA_USBETT
},
162 { USB_VENDOR_JATON
, USB_PRODUCT_JATON_EDA
},
163 { USB_VENDOR_KINGSTON
, USB_PRODUCT_KINGSTON_XX1
},
164 { USB_VENDOR_KLSI
, USB_PRODUCT_KLSI_DUH3E10BT
},
165 { USB_VENDOR_KLSI
, USB_PRODUCT_KLSI_DUH3E10BTN
},
166 { USB_VENDOR_LINKSYS
, USB_PRODUCT_LINKSYS_USB10T
},
167 { USB_VENDOR_MOBILITY
, USB_PRODUCT_MOBILITY_EA
},
168 { USB_VENDOR_NETGEAR
, USB_PRODUCT_NETGEAR_EA101
},
169 { USB_VENDOR_NETGEAR
, USB_PRODUCT_NETGEAR_EA101X
},
170 { USB_VENDOR_PERACOM
, USB_PRODUCT_PERACOM_ENET
},
171 { USB_VENDOR_PERACOM
, USB_PRODUCT_PERACOM_ENET2
},
172 { USB_VENDOR_PERACOM
, USB_PRODUCT_PERACOM_ENET3
},
173 { USB_VENDOR_PORTGEAR
, USB_PRODUCT_PORTGEAR_EA8
},
174 { USB_VENDOR_PORTGEAR
, USB_PRODUCT_PORTGEAR_EA9
},
175 { USB_VENDOR_PORTSMITH
, USB_PRODUCT_PORTSMITH_EEA
},
176 { USB_VENDOR_SHARK
, USB_PRODUCT_SHARK_PA
},
177 { USB_VENDOR_SILICOM
, USB_PRODUCT_SILICOM_U2E
},
178 { USB_VENDOR_SMC
, USB_PRODUCT_SMC_2102USB
},
180 #define kue_lookup(v, p) (usb_lookup(kue_devs, v, p))
182 USB_DECLARE_DRIVER(kue
);
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
, void *);
193 Static
void kue_init(void *);
194 Static
void kue_stop(struct kue_softc
*);
195 Static
void kue_watchdog(struct ifnet
*);
197 Static
void kue_setmulti(struct kue_softc
*);
198 Static
void kue_reset(struct kue_softc
*);
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
*);
206 kue_setword(struct kue_softc
*sc
, u_int8_t breq
, u_int16_t word
)
208 usb_device_request_t req
;
210 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
),__func__
));
212 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
214 USETW(req
.wValue
, word
);
215 USETW(req
.wIndex
, 0);
216 USETW(req
.wLength
, 0);
218 return (usbd_do_request(sc
->kue_udev
, &req
, NULL
));
222 kue_ctl(struct kue_softc
*sc
, int rw
, u_int8_t breq
, u_int16_t val
,
223 void *data
, u_int32_t len
)
225 usb_device_request_t req
;
227 DPRINTFN(10,("%s: %s: enter, len=%d\n", USBDEVNAME(sc
->kue_dev
),
230 if (rw
== KUE_CTL_WRITE
)
231 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
233 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
236 USETW(req
.wValue
, val
);
237 USETW(req
.wIndex
, 0);
238 USETW(req
.wLength
, len
);
240 return (usbd_do_request(sc
->kue_udev
, &req
, data
));
244 kue_load_fw(struct kue_softc
*sc
)
246 usb_device_descriptor_t dd
;
249 DPRINTFN(1,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
), __func__
));
252 * First, check if we even need to load the firmware.
253 * If the device was still attached when the system was
254 * rebooted, it may already have firmware loaded in it.
255 * If this is the case, we don't need to do it again.
256 * And in fact, if we try to load it again, we'll hang,
257 * so we have to avoid this condition if we don't want
260 * We can test this quickly by checking the bcdRevision
261 * code. The NIC will return a different revision code if
262 * it's probed while the firmware is still loaded and
265 if (usbd_get_device_desc(sc
->kue_udev
, &dd
))
267 if (UGETW(dd
.bcdDevice
) == KUE_WARM_REV
) {
268 printf("%s: warm boot, no firmware download\n",
269 USBDEVNAME(sc
->kue_dev
));
273 printf("%s: cold boot, downloading firmware\n",
274 USBDEVNAME(sc
->kue_dev
));
276 /* Load code segment */
277 DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
278 USBDEVNAME(sc
->kue_dev
)));
280 err
= kue_ctl(sc
, KUE_CTL_WRITE
, KUE_CMD_SEND_SCAN
,
281 0, __UNCONST(kue_code_seg
), sizeof(kue_code_seg
));
283 printf("%s: failed to load code segment: %s\n",
284 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
288 /* Load fixup segment */
289 DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
290 USBDEVNAME(sc
->kue_dev
)));
292 err
= kue_ctl(sc
, KUE_CTL_WRITE
, KUE_CMD_SEND_SCAN
,
293 0, __UNCONST(kue_fix_seg
), sizeof(kue_fix_seg
));
295 printf("%s: failed to load fixup segment: %s\n",
296 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
300 /* Send trigger command. */
301 DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
302 USBDEVNAME(sc
->kue_dev
)));
304 err
= kue_ctl(sc
, KUE_CTL_WRITE
, KUE_CMD_SEND_SCAN
,
305 0, __UNCONST(kue_trig_seg
), sizeof(kue_trig_seg
));
307 printf("%s: failed to load trigger segment: %s\n",
308 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
312 usbd_delay_ms(sc
->kue_udev
, 10);
315 * Reload device descriptor.
316 * Why? The chip without the firmware loaded returns
317 * one revision code. The chip with the firmware
318 * loaded and running returns a *different* revision
319 * code. This confuses the quirk mechanism, which is
320 * dependent on the revision data.
322 (void)usbd_reload_device_desc(sc
->kue_udev
);
324 DPRINTFN(1,("%s: %s: done\n", USBDEVNAME(sc
->kue_dev
), __func__
));
326 /* Reset the adapter. */
333 kue_setmulti(struct kue_softc
*sc
)
335 struct ifnet
*ifp
= GET_IFP(sc
);
336 struct ether_multi
*enm
;
337 struct ether_multistep step
;
340 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
), __func__
));
342 if (ifp
->if_flags
& IFF_PROMISC
) {
344 ifp
->if_flags
|= IFF_ALLMULTI
;
345 sc
->kue_rxfilt
|= KUE_RXFILT_ALLMULTI
;
346 sc
->kue_rxfilt
&= ~KUE_RXFILT_MULTICAST
;
347 kue_setword(sc
, KUE_CMD_SET_PKT_FILTER
, sc
->kue_rxfilt
);
351 sc
->kue_rxfilt
&= ~KUE_RXFILT_ALLMULTI
;
354 #if defined (__NetBSD__)
355 ETHER_FIRST_MULTI(step
, &sc
->kue_ec
, enm
);
357 ETHER_FIRST_MULTI(step
, &sc
->arpcom
, enm
);
359 while (enm
!= NULL
) {
360 if (i
== KUE_MCFILTCNT(sc
) ||
361 memcmp(enm
->enm_addrlo
, enm
->enm_addrhi
,
362 ETHER_ADDR_LEN
) != 0)
365 memcpy(KUE_MCFILT(sc
, i
), enm
->enm_addrlo
, ETHER_ADDR_LEN
);
366 ETHER_NEXT_MULTI(step
, enm
);
370 ifp
->if_flags
&= ~IFF_ALLMULTI
;
372 sc
->kue_rxfilt
|= KUE_RXFILT_MULTICAST
;
373 kue_ctl(sc
, KUE_CTL_WRITE
, KUE_CMD_SET_MCAST_FILTERS
,
374 i
, sc
->kue_mcfilters
, i
* ETHER_ADDR_LEN
);
376 kue_setword(sc
, KUE_CMD_SET_PKT_FILTER
, sc
->kue_rxfilt
);
380 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
381 * done after the firmware is loaded into the adapter in order to
382 * bring it into proper operation.
385 kue_reset(struct kue_softc
*sc
)
387 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
), __func__
));
389 if (usbd_set_config_no(sc
->kue_udev
, KUE_CONFIG_NO
, 1) ||
390 usbd_device2interface_handle(sc
->kue_udev
, KUE_IFACE_IDX
,
392 printf("%s: reset failed\n", USBDEVNAME(sc
->kue_dev
));
394 /* Wait a little while for the chip to get its brains in order. */
395 usbd_delay_ms(sc
->kue_udev
, 10);
399 * Probe for a KLSI chip.
403 USB_MATCH_START(kue
, uaa
);
405 DPRINTFN(25,("kue_match: enter\n"));
407 return (kue_lookup(uaa
->vendor
, uaa
->product
) != NULL
?
408 UMATCH_VENDOR_PRODUCT
: UMATCH_NONE
);
412 * Attach the interface. Allocate softc structures, do
413 * setup and ethernet/BPF attach.
417 USB_ATTACH_START(kue
, sc
, uaa
);
421 usbd_device_handle dev
= uaa
->device
;
422 usbd_interface_handle iface
;
424 usb_interface_descriptor_t
*id
;
425 usb_endpoint_descriptor_t
*ed
;
428 DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc
, dev
));
435 devinfop
= usbd_devinfo_alloc(dev
, 0);
436 aprint_normal_dev(self
, "%s\n", devinfop
);
437 usbd_devinfo_free(devinfop
);
439 err
= usbd_set_config_no(dev
, KUE_CONFIG_NO
, 1);
441 aprint_error_dev(self
, " setting config no failed\n");
442 USB_ATTACH_ERROR_RETURN
;
446 sc
->kue_product
= uaa
->product
;
447 sc
->kue_vendor
= uaa
->vendor
;
449 /* Load the firmware into the NIC. */
450 if (kue_load_fw(sc
)) {
451 aprint_error_dev(self
, "loading firmware failed\n");
452 USB_ATTACH_ERROR_RETURN
;
455 err
= usbd_device2interface_handle(dev
, KUE_IFACE_IDX
, &iface
);
457 aprint_error_dev(self
, "getting interface handle failed\n");
458 USB_ATTACH_ERROR_RETURN
;
461 sc
->kue_iface
= iface
;
462 id
= usbd_get_interface_descriptor(iface
);
464 /* Find endpoints. */
465 for (i
= 0; i
< id
->bNumEndpoints
; i
++) {
466 ed
= usbd_interface2endpoint_descriptor(iface
, i
);
468 aprint_error_dev(self
, "couldn't get ep %d\n", i
);
469 USB_ATTACH_ERROR_RETURN
;
471 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
472 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
473 sc
->kue_ed
[KUE_ENDPT_RX
] = ed
->bEndpointAddress
;
474 } else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
475 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
476 sc
->kue_ed
[KUE_ENDPT_TX
] = ed
->bEndpointAddress
;
477 } else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
478 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_INTERRUPT
) {
479 sc
->kue_ed
[KUE_ENDPT_INTR
] = ed
->bEndpointAddress
;
483 if (sc
->kue_ed
[KUE_ENDPT_RX
] == 0 || sc
->kue_ed
[KUE_ENDPT_TX
] == 0) {
484 aprint_error_dev(self
, "missing endpoint\n");
485 USB_ATTACH_ERROR_RETURN
;
488 /* Read ethernet descriptor */
489 err
= kue_ctl(sc
, KUE_CTL_READ
, KUE_CMD_GET_ETHER_DESCRIPTOR
,
490 0, &sc
->kue_desc
, sizeof(sc
->kue_desc
));
492 aprint_error_dev(self
, "could not read Ethernet descriptor\n");
493 USB_ATTACH_ERROR_RETURN
;
496 sc
->kue_mcfilters
= malloc(KUE_MCFILTCNT(sc
) * ETHER_ADDR_LEN
,
498 if (sc
->kue_mcfilters
== NULL
) {
499 aprint_error_dev(self
,
500 "no memory for multicast filter buffer\n");
501 USB_ATTACH_ERROR_RETURN
;
507 * A KLSI chip was detected. Inform the world.
509 aprint_normal_dev(self
, "Ethernet address %s\n",
510 ether_sprintf(sc
->kue_desc
.kue_macaddr
));
512 /* Initialize interface info.*/
515 ifp
->if_mtu
= ETHERMTU
;
516 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
517 ifp
->if_ioctl
= kue_ioctl
;
518 ifp
->if_start
= kue_start
;
519 ifp
->if_watchdog
= kue_watchdog
;
520 #if defined(__OpenBSD__)
521 ifp
->if_snd
.ifq_maxlen
= IFQ_MAXLEN
;
523 strncpy(ifp
->if_xname
, USBDEVNAME(sc
->kue_dev
), IFNAMSIZ
);
525 IFQ_SET_READY(&ifp
->if_snd
);
527 /* Attach the interface. */
529 Ether_ifattach(ifp
, sc
->kue_desc
.kue_macaddr
);
531 rnd_attach_source(&sc
->rnd_source
, USBDEVNAME(sc
->kue_dev
),
535 sc
->kue_attached
= 1;
538 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH
, sc
->kue_udev
,
539 USBDEV(sc
->kue_dev
));
541 USB_ATTACH_SUCCESS_RETURN
;
546 USB_DETACH_START(kue
, sc
);
547 struct ifnet
*ifp
= GET_IFP(sc
);
550 s
= splusb(); /* XXX why? */
552 if (sc
->kue_mcfilters
!= NULL
) {
553 free(sc
->kue_mcfilters
, M_USBDEV
);
554 sc
->kue_mcfilters
= NULL
;
557 if (!sc
->kue_attached
) {
558 /* Detached before attached finished, so just bail out. */
563 if (ifp
->if_flags
& IFF_RUNNING
)
566 #if defined(__NetBSD__)
568 rnd_detach_source(&sc
->rnd_source
);
571 #endif /* __NetBSD__ */
576 if (sc
->kue_ep
[KUE_ENDPT_TX
] != NULL
||
577 sc
->kue_ep
[KUE_ENDPT_RX
] != NULL
||
578 sc
->kue_ep
[KUE_ENDPT_INTR
] != NULL
)
579 aprint_debug_dev(self
, "detach has active endpoints\n");
582 sc
->kue_attached
= 0;
589 kue_activate(device_ptr_t self
, enum devact act
)
591 struct kue_softc
*sc
= device_private(self
);
593 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
), __func__
));
596 case DVACT_DEACTIVATE
:
597 /* Deactivate the interface. */
598 if_deactivate(&sc
->kue_ec
.ec_if
);
607 * Initialize an RX descriptor and attach an MBUF cluster.
610 kue_newbuf(struct kue_softc
*sc
, struct kue_chain
*c
, struct mbuf
*m
)
612 struct mbuf
*m_new
= NULL
;
614 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
),__func__
));
617 MGETHDR(m_new
, M_DONTWAIT
, MT_DATA
);
619 printf("%s: no memory for rx list "
620 "-- packet dropped!\n", USBDEVNAME(sc
->kue_dev
));
624 MCLGET(m_new
, M_DONTWAIT
);
625 if (!(m_new
->m_flags
& M_EXT
)) {
626 printf("%s: no memory for rx list "
627 "-- packet dropped!\n", USBDEVNAME(sc
->kue_dev
));
631 m_new
->m_len
= m_new
->m_pkthdr
.len
= MCLBYTES
;
634 m_new
->m_len
= m_new
->m_pkthdr
.len
= MCLBYTES
;
635 m_new
->m_data
= m_new
->m_ext
.ext_buf
;
644 kue_rx_list_init(struct kue_softc
*sc
)
646 struct kue_cdata
*cd
;
650 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
), __func__
));
653 for (i
= 0; i
< KUE_RX_LIST_CNT
; i
++) {
654 c
= &cd
->kue_rx_chain
[i
];
657 if (kue_newbuf(sc
, c
, NULL
) == ENOBUFS
)
659 if (c
->kue_xfer
== NULL
) {
660 c
->kue_xfer
= usbd_alloc_xfer(sc
->kue_udev
);
661 if (c
->kue_xfer
== NULL
)
663 c
->kue_buf
= usbd_alloc_buffer(c
->kue_xfer
, KUE_BUFSZ
);
664 if (c
->kue_buf
== NULL
)
665 return (ENOBUFS
); /* XXX free xfer */
673 kue_tx_list_init(struct kue_softc
*sc
)
675 struct kue_cdata
*cd
;
679 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
), __func__
));
682 for (i
= 0; i
< KUE_TX_LIST_CNT
; i
++) {
683 c
= &cd
->kue_tx_chain
[i
];
687 if (c
->kue_xfer
== NULL
) {
688 c
->kue_xfer
= usbd_alloc_xfer(sc
->kue_udev
);
689 if (c
->kue_xfer
== NULL
)
691 c
->kue_buf
= usbd_alloc_buffer(c
->kue_xfer
, KUE_BUFSZ
);
692 if (c
->kue_buf
== NULL
)
701 * A frame has been uploaded: pass the resulting mbuf chain up to
702 * the higher level protocols.
705 kue_rxeof(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
707 struct kue_chain
*c
= priv
;
708 struct kue_softc
*sc
= c
->kue_sc
;
709 struct ifnet
*ifp
= GET_IFP(sc
);
714 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc
->kue_dev
),
720 if (!(ifp
->if_flags
& IFF_RUNNING
))
723 if (status
!= USBD_NORMAL_COMPLETION
) {
724 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
)
727 if (usbd_ratecheck(&sc
->kue_rx_notice
)) {
728 printf("%s: %u usb errors on rx: %s\n",
729 USBDEVNAME(sc
->kue_dev
), sc
->kue_rx_errs
,
730 usbd_errstr(status
));
733 if (status
== USBD_STALLED
)
734 usbd_clear_endpoint_stall_async(sc
->kue_ep
[KUE_ENDPT_RX
]);
738 usbd_get_xfer_status(xfer
, NULL
, NULL
, &total_len
, NULL
);
740 DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", USBDEVNAME(sc
->kue_dev
),
742 UGETW(mtod(c
->kue_mbuf
, u_int8_t
*))));
748 /* copy data to mbuf */
749 memcpy(mtod(m
, char *), c
->kue_buf
, total_len
);
751 /* No errors; receive the packet. */
752 total_len
= UGETW(mtod(m
, u_int8_t
*));
753 m_adj(m
, sizeof(u_int16_t
));
755 if (total_len
< sizeof(struct ether_header
)) {
761 m
->m_pkthdr
.len
= m
->m_len
= total_len
;
763 m
->m_pkthdr
.rcvif
= ifp
;
768 if (kue_newbuf(sc
, c
, NULL
) == ENOBUFS
) {
775 * Handle BPF listeners. Let the BPF user see the packet, but
776 * don't pass it up to the ether_input() layer unless it's
777 * a broadcast packet, multicast packet, matches our ethernet
778 * address or the interface is in promiscuous mode.
781 bpf_mtap(ifp
->if_bpf
, m
);
784 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc
->kue_dev
),
785 __func__
, m
->m_len
));
792 /* Setup new transfer. */
793 usbd_setup_xfer(c
->kue_xfer
, sc
->kue_ep
[KUE_ENDPT_RX
],
794 c
, c
->kue_buf
, KUE_BUFSZ
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
795 USBD_NO_TIMEOUT
, kue_rxeof
);
796 usbd_transfer(c
->kue_xfer
);
798 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc
->kue_dev
),
803 * A frame was downloaded to the chip. It's safe for us to clean up
808 kue_txeof(usbd_xfer_handle xfer
, usbd_private_handle priv
,
811 struct kue_chain
*c
= priv
;
812 struct kue_softc
*sc
= c
->kue_sc
;
813 struct ifnet
*ifp
= GET_IFP(sc
);
821 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc
->kue_dev
),
825 ifp
->if_flags
&= ~IFF_OACTIVE
;
827 if (status
!= USBD_NORMAL_COMPLETION
) {
828 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
833 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc
->kue_dev
),
834 usbd_errstr(status
));
835 if (status
== USBD_STALLED
)
836 usbd_clear_endpoint_stall_async(sc
->kue_ep
[KUE_ENDPT_TX
]);
843 m_freem(c
->kue_mbuf
);
846 if (IFQ_IS_EMPTY(&ifp
->if_snd
) == 0)
853 kue_send(struct kue_softc
*sc
, struct mbuf
*m
, int idx
)
859 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
),__func__
));
861 c
= &sc
->kue_cdata
.kue_tx_chain
[idx
];
864 * Copy the mbuf data into a contiguous buffer, leaving two
865 * bytes at the beginning to hold the frame length.
867 m_copydata(m
, 0, m
->m_pkthdr
.len
, c
->kue_buf
+ 2);
870 total_len
= m
->m_pkthdr
.len
+ 2;
871 /* XXX what's this? */
872 total_len
+= 64 - (total_len
% 64);
874 /* Frame length is specified in the first 2 bytes of the buffer. */
875 c
->kue_buf
[0] = (u_int8_t
)m
->m_pkthdr
.len
;
876 c
->kue_buf
[1] = (u_int8_t
)(m
->m_pkthdr
.len
>> 8);
878 usbd_setup_xfer(c
->kue_xfer
, sc
->kue_ep
[KUE_ENDPT_TX
],
879 c
, c
->kue_buf
, total_len
, USBD_NO_COPY
, USBD_DEFAULT_TIMEOUT
,
883 err
= usbd_transfer(c
->kue_xfer
);
884 if (err
!= USBD_IN_PROGRESS
) {
885 printf("%s: kue_send error=%s\n", USBDEVNAME(sc
->kue_dev
),
891 sc
->kue_cdata
.kue_tx_cnt
++;
897 kue_start(struct ifnet
*ifp
)
899 struct kue_softc
*sc
= ifp
->if_softc
;
900 struct mbuf
*m_head
= NULL
;
902 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
),__func__
));
907 if (ifp
->if_flags
& IFF_OACTIVE
)
910 IFQ_POLL(&ifp
->if_snd
, m_head
);
914 if (kue_send(sc
, m_head
, 0)) {
915 ifp
->if_flags
|= IFF_OACTIVE
;
919 IFQ_DEQUEUE(&ifp
->if_snd
, m_head
);
923 * If there's a BPF listener, bounce a copy of this frame
927 bpf_mtap(ifp
->if_bpf
, m_head
);
930 ifp
->if_flags
|= IFF_OACTIVE
;
933 * Set a timeout in case the chip goes out to lunch.
941 struct kue_softc
*sc
= xsc
;
942 struct ifnet
*ifp
= GET_IFP(sc
);
944 u_char eaddr
[ETHER_ADDR_LEN
];
946 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
),__func__
));
948 if (ifp
->if_flags
& IFF_RUNNING
)
953 memcpy(eaddr
, CLLADDR(ifp
->if_sadl
), sizeof(eaddr
));
954 /* Set MAC address */
955 kue_ctl(sc
, KUE_CTL_WRITE
, KUE_CMD_SET_MAC
, 0, eaddr
, ETHER_ADDR_LEN
);
957 sc
->kue_rxfilt
= KUE_RXFILT_UNICAST
| KUE_RXFILT_BROADCAST
;
959 /* If we want promiscuous mode, set the allframes bit. */
960 if (ifp
->if_flags
& IFF_PROMISC
)
961 sc
->kue_rxfilt
|= KUE_RXFILT_PROMISC
;
963 kue_setword(sc
, KUE_CMD_SET_PKT_FILTER
, sc
->kue_rxfilt
);
965 /* I'm not sure how to tune these. */
968 * Leave this one alone for now; setting it
969 * wrong causes lockups on some machines/controllers.
971 kue_setword(sc
, KUE_CMD_SET_SOFS
, 1);
973 kue_setword(sc
, KUE_CMD_SET_URB_SIZE
, 64);
976 if (kue_tx_list_init(sc
) == ENOBUFS
) {
977 printf("%s: tx list init failed\n", USBDEVNAME(sc
->kue_dev
));
983 if (kue_rx_list_init(sc
) == ENOBUFS
) {
984 printf("%s: rx list init failed\n", USBDEVNAME(sc
->kue_dev
));
989 /* Load the multicast filter. */
992 if (sc
->kue_ep
[KUE_ENDPT_RX
] == NULL
) {
993 if (kue_open_pipes(sc
)) {
999 ifp
->if_flags
|= IFF_RUNNING
;
1000 ifp
->if_flags
&= ~IFF_OACTIVE
;
1006 kue_open_pipes(struct kue_softc
*sc
)
1009 struct kue_chain
*c
;
1012 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
),__func__
));
1014 /* Open RX and TX pipes. */
1015 err
= usbd_open_pipe(sc
->kue_iface
, sc
->kue_ed
[KUE_ENDPT_RX
],
1016 USBD_EXCLUSIVE_USE
, &sc
->kue_ep
[KUE_ENDPT_RX
]);
1018 printf("%s: open rx pipe failed: %s\n",
1019 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
1023 err
= usbd_open_pipe(sc
->kue_iface
, sc
->kue_ed
[KUE_ENDPT_TX
],
1024 USBD_EXCLUSIVE_USE
, &sc
->kue_ep
[KUE_ENDPT_TX
]);
1026 printf("%s: open tx pipe failed: %s\n",
1027 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
1031 /* Start up the receive pipe. */
1032 for (i
= 0; i
< KUE_RX_LIST_CNT
; i
++) {
1033 c
= &sc
->kue_cdata
.kue_rx_chain
[i
];
1034 usbd_setup_xfer(c
->kue_xfer
, sc
->kue_ep
[KUE_ENDPT_RX
],
1035 c
, c
->kue_buf
, KUE_BUFSZ
,
1036 USBD_SHORT_XFER_OK
| USBD_NO_COPY
, USBD_NO_TIMEOUT
,
1038 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc
->kue_dev
),
1040 usbd_transfer(c
->kue_xfer
);
1047 kue_ioctl(struct ifnet
*ifp
, u_long command
, void *data
)
1049 struct kue_softc
*sc
= ifp
->if_softc
;
1050 struct ifaddr
*ifa
= (struct ifaddr
*)data
;
1051 struct ifreq
*ifr
= (struct ifreq
*)data
;
1054 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
),__func__
));
1061 printf("%s: no proc!!\n", USBDEVNAME(sc
->kue_dev
));
1069 case SIOCINITIFADDR
:
1070 ifp
->if_flags
|= IFF_UP
;
1073 switch (ifa
->ifa_addr
->sa_family
) {
1076 #if defined(__NetBSD__)
1077 arp_ifinit(ifp
, ifa
);
1079 arp_ifinit(&sc
->arpcom
, ifa
);
1087 if (ifr
->ifr_mtu
< ETHERMIN
|| ifr
->ifr_mtu
> ETHERMTU
)
1089 else if ((error
= ifioctl_common(ifp
, command
, data
)) == ENETRESET
)
1094 if ((error
= ifioctl_common(ifp
, command
, data
)) != 0)
1096 if (ifp
->if_flags
& IFF_UP
) {
1097 if (ifp
->if_flags
& IFF_RUNNING
&&
1098 ifp
->if_flags
& IFF_PROMISC
&&
1099 !(sc
->kue_if_flags
& IFF_PROMISC
)) {
1100 sc
->kue_rxfilt
|= KUE_RXFILT_PROMISC
;
1101 kue_setword(sc
, KUE_CMD_SET_PKT_FILTER
,
1103 } else if (ifp
->if_flags
& IFF_RUNNING
&&
1104 !(ifp
->if_flags
& IFF_PROMISC
) &&
1105 sc
->kue_if_flags
& IFF_PROMISC
) {
1106 sc
->kue_rxfilt
&= ~KUE_RXFILT_PROMISC
;
1107 kue_setword(sc
, KUE_CMD_SET_PKT_FILTER
,
1109 } else if (!(ifp
->if_flags
& IFF_RUNNING
))
1112 if (ifp
->if_flags
& IFF_RUNNING
)
1115 sc
->kue_if_flags
= ifp
->if_flags
;
1124 error
= ether_ioctl(ifp
, command
, data
);
1134 kue_watchdog(struct ifnet
*ifp
)
1136 struct kue_softc
*sc
= ifp
->if_softc
;
1137 struct kue_chain
*c
;
1141 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
),__func__
));
1147 printf("%s: watchdog timeout\n", USBDEVNAME(sc
->kue_dev
));
1150 c
= &sc
->kue_cdata
.kue_tx_chain
[0];
1151 usbd_get_xfer_status(c
->kue_xfer
, NULL
, NULL
, NULL
, &stat
);
1152 kue_txeof(c
->kue_xfer
, c
, stat
);
1154 if (IFQ_IS_EMPTY(&ifp
->if_snd
) == 0)
1160 * Stop the adapter and free any mbufs allocated to the
1164 kue_stop(struct kue_softc
*sc
)
1170 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc
->kue_dev
),__func__
));
1175 /* Stop transfers. */
1176 if (sc
->kue_ep
[KUE_ENDPT_RX
] != NULL
) {
1177 err
= usbd_abort_pipe(sc
->kue_ep
[KUE_ENDPT_RX
]);
1179 printf("%s: abort rx pipe failed: %s\n",
1180 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
1182 err
= usbd_close_pipe(sc
->kue_ep
[KUE_ENDPT_RX
]);
1184 printf("%s: close rx pipe failed: %s\n",
1185 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
1187 sc
->kue_ep
[KUE_ENDPT_RX
] = NULL
;
1190 if (sc
->kue_ep
[KUE_ENDPT_TX
] != NULL
) {
1191 err
= usbd_abort_pipe(sc
->kue_ep
[KUE_ENDPT_TX
]);
1193 printf("%s: abort tx pipe failed: %s\n",
1194 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
1196 err
= usbd_close_pipe(sc
->kue_ep
[KUE_ENDPT_TX
]);
1198 printf("%s: close tx pipe failed: %s\n",
1199 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
1201 sc
->kue_ep
[KUE_ENDPT_TX
] = NULL
;
1204 if (sc
->kue_ep
[KUE_ENDPT_INTR
] != NULL
) {
1205 err
= usbd_abort_pipe(sc
->kue_ep
[KUE_ENDPT_INTR
]);
1207 printf("%s: abort intr pipe failed: %s\n",
1208 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
1210 err
= usbd_close_pipe(sc
->kue_ep
[KUE_ENDPT_INTR
]);
1212 printf("%s: close intr pipe failed: %s\n",
1213 USBDEVNAME(sc
->kue_dev
), usbd_errstr(err
));
1215 sc
->kue_ep
[KUE_ENDPT_INTR
] = NULL
;
1218 /* Free RX resources. */
1219 for (i
= 0; i
< KUE_RX_LIST_CNT
; i
++) {
1220 if (sc
->kue_cdata
.kue_rx_chain
[i
].kue_mbuf
!= NULL
) {
1221 m_freem(sc
->kue_cdata
.kue_rx_chain
[i
].kue_mbuf
);
1222 sc
->kue_cdata
.kue_rx_chain
[i
].kue_mbuf
= NULL
;
1224 if (sc
->kue_cdata
.kue_rx_chain
[i
].kue_xfer
!= NULL
) {
1225 usbd_free_xfer(sc
->kue_cdata
.kue_rx_chain
[i
].kue_xfer
);
1226 sc
->kue_cdata
.kue_rx_chain
[i
].kue_xfer
= NULL
;
1230 /* Free TX resources. */
1231 for (i
= 0; i
< KUE_TX_LIST_CNT
; i
++) {
1232 if (sc
->kue_cdata
.kue_tx_chain
[i
].kue_mbuf
!= NULL
) {
1233 m_freem(sc
->kue_cdata
.kue_tx_chain
[i
].kue_mbuf
);
1234 sc
->kue_cdata
.kue_tx_chain
[i
].kue_mbuf
= NULL
;
1236 if (sc
->kue_cdata
.kue_tx_chain
[i
].kue_xfer
!= NULL
) {
1237 usbd_free_xfer(sc
->kue_cdata
.kue_tx_chain
[i
].kue_xfer
);
1238 sc
->kue_cdata
.kue_tx_chain
[i
].kue_xfer
= NULL
;
1242 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_OACTIVE
);