if_kue.c revision 185950
1/*- 2 * Copyright (c) 1997, 1998, 1999, 2000 3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_kue2.c 185950 2008-12-11 23:17:48Z thompsa $"); 35 36/* 37 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver. 38 * 39 * Written by Bill Paul <wpaul@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 * NOTE: all function names beginning like "kue_cfg_" can only 70 * be called from within the config thread function ! 71 */ 72 73#include <dev/usb2/include/usb2_devid.h> 74#include <dev/usb2/include/usb2_standard.h> 75#include <dev/usb2/include/usb2_mfunc.h> 76#include <dev/usb2/include/usb2_error.h> 77 78#define usb2_config_td_cc usb2_ether_cc 79#define usb2_config_td_softc kue_softc 80 81#define USB_DEBUG_VAR kue_debug 82 83#include <dev/usb2/core/usb2_core.h> 84#include <dev/usb2/core/usb2_lookup.h> 85#include <dev/usb2/core/usb2_process.h> 86#include <dev/usb2/core/usb2_config_td.h> 87#include <dev/usb2/core/usb2_debug.h> 88#include <dev/usb2/core/usb2_request.h> 89#include <dev/usb2/core/usb2_busdma.h> 90#include <dev/usb2/core/usb2_util.h> 91 92#include <dev/usb2/ethernet/usb2_ethernet.h> 93#include <dev/usb2/ethernet/if_kue2_reg.h> 94#include <dev/usb2/ethernet/if_kue2_fw.h> 95 96/* 97 * Various supported device vendors/products. 98 */ 99static const struct usb2_device_id kue_devs[] = { 100 {USB_VPI(USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250, 0)}, 101 {USB_VPI(USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460, 0)}, 102 {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450, 0)}, 103 {USB_VPI(USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT, 0)}, 104 {USB_VPI(USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX, 0)}, 105 {USB_VPI(USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101, 0)}, 106 {USB_VPI(USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA, 0)}, 107 {USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C, 0)}, 108 {USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T, 0)}, 109 {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T, 0)}, 110 {USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C, 0)}, 111 {USB_VPI(USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45, 0)}, 112 {USB_VPI(USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1, 0)}, 113 {USB_VPI(USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2, 0)}, 114 {USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT, 0)}, 115 {USB_VPI(USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA, 0)}, 116 {USB_VPI(USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1, 0)}, 117 {USB_VPI(USB_VENDOR_KLSI, USB_PRODUCT_AOX_USB101, 0)}, 118 {USB_VPI(USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT, 0)}, 119 {USB_VPI(USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN, 0)}, 120 {USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T, 0)}, 121 {USB_VPI(USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA, 0)}, 122 {USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101, 0)}, 123 {USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X, 0)}, 124 {USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET, 0)}, 125 {USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2, 0)}, 126 {USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3, 0)}, 127 {USB_VPI(USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8, 0)}, 128 {USB_VPI(USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9, 0)}, 129 {USB_VPI(USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA, 0)}, 130 {USB_VPI(USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA, 0)}, 131 {USB_VPI(USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_GPE, 0)}, 132 {USB_VPI(USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E, 0)}, 133 {USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB, 0)}, 134}; 135 136/* prototypes */ 137 138static device_probe_t kue_probe; 139static device_attach_t kue_attach; 140static device_detach_t kue_detach; 141static device_shutdown_t kue_shutdown; 142 143static usb2_callback_t kue_bulk_read_clear_stall_callback; 144static usb2_callback_t kue_bulk_read_callback; 145static usb2_callback_t kue_bulk_write_clear_stall_callback; 146static usb2_callback_t kue_bulk_write_callback; 147 148static usb2_config_td_command_t kue_cfg_promisc_upd; 149static usb2_config_td_command_t kue_config_copy; 150static usb2_config_td_command_t kue_cfg_first_time_setup; 151static usb2_config_td_command_t kue_cfg_pre_init; 152static usb2_config_td_command_t kue_cfg_init; 153static usb2_config_td_command_t kue_cfg_tick; 154static usb2_config_td_command_t kue_cfg_pre_stop; 155static usb2_config_td_command_t kue_cfg_stop; 156 157static void kue_cfg_do_request(struct kue_softc *, 158 struct usb2_device_request *, void *); 159static void kue_cfg_setword(struct kue_softc *, uint8_t, uint16_t); 160static void kue_cfg_ctl(struct kue_softc *, uint8_t, uint8_t, uint16_t, 161 void *, uint16_t); 162static void kue_cfg_load_fw(struct kue_softc *); 163static void kue_cfg_reset(struct kue_softc *); 164static void kue_start_cb(struct ifnet *); 165static void kue_start_transfers(struct kue_softc *); 166static void kue_init_cb(void *); 167static int kue_ioctl_cb(struct ifnet *, u_long, caddr_t); 168static void kue_watchdog(void *); 169 170#if USB_DEBUG 171static int kue_debug = 0; 172 173SYSCTL_NODE(_hw_usb2, OID_AUTO, kue, CTLFLAG_RW, 0, "USB kue"); 174SYSCTL_INT(_hw_usb2_kue, OID_AUTO, debug, CTLFLAG_RW, &kue_debug, 0, 175 "Debug level"); 176#endif 177 178static const struct usb2_config kue_config[KUE_ENDPT_MAX] = { 179 180 [0] = { 181 .type = UE_BULK, 182 .endpoint = UE_ADDR_ANY, 183 .direction = UE_DIR_OUT, 184 .mh.bufsize = (MCLBYTES + 2 + 64), 185 .mh.flags = {.pipe_bof = 1,}, 186 .mh.callback = &kue_bulk_write_callback, 187 .mh.timeout = 10000, /* 10 seconds */ 188 }, 189 190 [1] = { 191 .type = UE_BULK, 192 .endpoint = UE_ADDR_ANY, 193 .direction = UE_DIR_IN, 194 .mh.bufsize = (MCLBYTES + 2), 195 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 196 .mh.callback = &kue_bulk_read_callback, 197 .mh.timeout = 0, /* no timeout */ 198 }, 199 200 [2] = { 201 .type = UE_CONTROL, 202 .endpoint = 0x00, /* Control pipe */ 203 .direction = UE_DIR_ANY, 204 .mh.bufsize = sizeof(struct usb2_device_request), 205 .mh.flags = {}, 206 .mh.callback = &kue_bulk_write_clear_stall_callback, 207 .mh.timeout = 1000, /* 1 second */ 208 .mh.interval = 50, /* 50ms */ 209 }, 210 211 [3] = { 212 .type = UE_CONTROL, 213 .endpoint = 0x00, /* Control pipe */ 214 .direction = UE_DIR_ANY, 215 .mh.bufsize = sizeof(struct usb2_device_request), 216 .mh.flags = {}, 217 .mh.callback = &kue_bulk_read_clear_stall_callback, 218 .mh.timeout = 1000, /* 1 second */ 219 .mh.interval = 50, /* 50ms */ 220 }, 221}; 222 223static device_method_t kue_methods[] = { 224 /* Device interface */ 225 DEVMETHOD(device_probe, kue_probe), 226 DEVMETHOD(device_attach, kue_attach), 227 DEVMETHOD(device_detach, kue_detach), 228 DEVMETHOD(device_shutdown, kue_shutdown), 229 230 {0, 0} 231}; 232 233static driver_t kue_driver = { 234 .name = "kue", 235 .methods = kue_methods, 236 .size = sizeof(struct kue_softc), 237}; 238 239static devclass_t kue_devclass; 240 241DRIVER_MODULE(kue, ushub, kue_driver, kue_devclass, NULL, 0); 242MODULE_DEPEND(kue, usb2_ethernet, 1, 1, 1); 243MODULE_DEPEND(kue, usb2_core, 1, 1, 1); 244MODULE_DEPEND(kue, ether, 1, 1, 1); 245 246/* 247 * We have a custom do_request function which is almost like the 248 * regular do_request function, except it has a much longer timeout. 249 * Why? Because we need to make requests over the control endpoint 250 * to download the firmware to the device, which can take longer 251 * than the default timeout. 252 */ 253static void 254kue_cfg_do_request(struct kue_softc *sc, struct usb2_device_request *req, 255 void *data) 256{ 257 uint16_t length; 258 usb2_error_t err; 259 260 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 261 goto error; 262 } 263 err = usb2_do_request_flags 264 (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 60000); 265 266 if (err) { 267 DPRINTF("device request failed, err=%s " 268 "(ignored)\n", usb2_errstr(err)); 269 270error: 271 length = UGETW(req->wLength); 272 273 if ((req->bmRequestType & UT_READ) && length) { 274 bzero(data, length); 275 } 276 } 277} 278 279static void 280kue_cfg_setword(struct kue_softc *sc, uint8_t breq, uint16_t word) 281{ 282 struct usb2_device_request req; 283 284 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 285 req.bRequest = breq; 286 USETW(req.wValue, word); 287 USETW(req.wIndex, 0); 288 USETW(req.wLength, 0); 289 290 kue_cfg_do_request(sc, &req, NULL); 291} 292 293static void 294kue_cfg_ctl(struct kue_softc *sc, uint8_t rw, uint8_t breq, 295 uint16_t val, void *data, uint16_t len) 296{ 297 struct usb2_device_request req; 298 299 if (rw == KUE_CTL_WRITE) { 300 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 301 } else { 302 req.bmRequestType = UT_READ_VENDOR_DEVICE; 303 } 304 305 req.bRequest = breq; 306 USETW(req.wValue, val); 307 USETW(req.wIndex, 0); 308 USETW(req.wLength, len); 309 310 kue_cfg_do_request(sc, &req, data); 311} 312 313static void 314kue_cfg_load_fw(struct kue_softc *sc) 315{ 316 struct usb2_device_descriptor *dd; 317 uint16_t hwrev; 318 319 dd = usb2_get_device_descriptor(sc->sc_udev); 320 hwrev = UGETW(dd->bcdDevice); 321 322 /* 323 * First, check if we even need to load the firmware. 324 * If the device was still attached when the system was 325 * rebooted, it may already have firmware loaded in it. 326 * If this is the case, we don't need to do it again. 327 * And in fact, if we try to load it again, we'll hang, 328 * so we have to avoid this condition if we don't want 329 * to look stupid. 330 * 331 * We can test this quickly by checking the bcdRevision 332 * code. The NIC will return a different revision code if 333 * it's probed while the firmware is still loaded and 334 * running. 335 */ 336 if (hwrev == 0x0202) { 337 return; 338 } 339 /* load code segment */ 340 kue_cfg_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 341 0, kue_code_seg, sizeof(kue_code_seg)); 342 343 /* load fixup segment */ 344 kue_cfg_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 345 0, kue_fix_seg, sizeof(kue_fix_seg)); 346 347 /* send trigger command */ 348 kue_cfg_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 349 0, kue_trig_seg, sizeof(kue_trig_seg)); 350} 351 352static void 353kue_cfg_promisc_upd(struct kue_softc *sc, 354 struct usb2_config_td_cc *cc, uint16_t refcount) 355{ 356 kue_cfg_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS, 357 cc->if_nhash, cc->if_hash, cc->if_nhash * ETHER_ADDR_LEN); 358 359 kue_cfg_setword(sc, KUE_CMD_SET_PKT_FILTER, cc->if_rxfilt); 360} 361 362static void 363kue_mchash(struct usb2_config_td_cc *cc, const uint8_t *ptr) 364{ 365 uint8_t i; 366 367 i = cc->if_nhash; 368 369 /* 370 * If there are too many addresses for the internal filter, 371 * switch over to allmulti mode. 372 */ 373 if (i == cc->if_mhash) { 374 cc->if_rxfilt |= KUE_RXFILT_ALLMULTI; 375 } else { 376 bcopy(ptr, cc->if_hash + (i * ETHER_ADDR_LEN), ETHER_ADDR_LEN); 377 cc->if_nhash++; 378 } 379} 380 381static void 382kue_config_copy(struct kue_softc *sc, 383 struct usb2_config_td_cc *cc, uint16_t refcount) 384{ 385 bzero(cc, sizeof(*cc)); 386 387 cc->if_mhash = sc->sc_mcfilt_max; 388 cc->if_rxfilt = (KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST); 389 390 usb2_ether_cc(sc->sc_ifp, &kue_mchash, cc); 391 392 /* 393 * If we want promiscuous mode, set the all-frames bit: 394 */ 395 if (cc->if_flags & IFF_PROMISC) { 396 cc->if_rxfilt |= KUE_RXFILT_PROMISC; 397 } 398 if ((cc->if_flags & IFF_ALLMULTI) || 399 (cc->if_flags & IFF_PROMISC)) { 400 cc->if_rxfilt |= KUE_RXFILT_ALLMULTI; 401 } else if (cc->if_nhash) { 402 cc->if_rxfilt |= KUE_RXFILT_MULTICAST; 403 } 404} 405 406/* 407 * Issue a SET_CONFIGURATION command to reset the MAC. This should be 408 * done after the firmware is loaded into the adapter in order to 409 * bring it into proper operation. 410 */ 411static void 412kue_cfg_reset(struct kue_softc *sc) 413{ 414 struct usb2_config_descriptor *cd; 415 usb2_error_t err; 416 417 cd = usb2_get_config_descriptor(sc->sc_udev); 418 419 err = usb2_req_set_config(sc->sc_udev, &sc->sc_mtx, 420 cd->bConfigurationValue); 421 if (err) { 422 DPRINTF("reset failed (ignored)\n"); 423 } 424 /* 425 * wait a little while for the chip to get its brains in order: 426 */ 427 err = usb2_config_td_sleep(&sc->sc_config_td, hz / 100); 428} 429 430/* 431 * Probe for a KLSI chip. 432 */ 433static int 434kue_probe(device_t dev) 435{ 436 struct usb2_attach_arg *uaa = device_get_ivars(dev); 437 438 if (uaa->usb2_mode != USB_MODE_HOST) { 439 return (ENXIO); 440 } 441 if (uaa->info.bConfigIndex != KUE_CONFIG_IDX) { 442 return (ENXIO); 443 } 444 if (uaa->info.bIfaceIndex != KUE_IFACE_IDX) { 445 return (ENXIO); 446 } 447 return (usb2_lookup_id_by_uaa(kue_devs, sizeof(kue_devs), uaa)); 448} 449 450/* 451 * Attach the interface. Allocate softc structures, do 452 * setup and ethernet/BPF attach. 453 */ 454static int 455kue_attach(device_t dev) 456{ 457 struct usb2_attach_arg *uaa = device_get_ivars(dev); 458 struct kue_softc *sc = device_get_softc(dev); 459 int32_t error; 460 uint8_t iface_index; 461 462 if (sc == NULL) { 463 return (ENOMEM); 464 } 465 sc->sc_udev = uaa->device; 466 sc->sc_dev = dev; 467 sc->sc_unit = device_get_unit(dev); 468 469 device_set_usb2_desc(dev); 470 471 mtx_init(&sc->sc_mtx, "kue lock", NULL, MTX_DEF | MTX_RECURSE); 472 473 usb2_callout_init_mtx(&sc->sc_watchdog, 474 &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 475 476 iface_index = KUE_IFACE_IDX; 477 error = usb2_transfer_setup(uaa->device, &iface_index, 478 sc->sc_xfer, kue_config, KUE_ENDPT_MAX, sc, &sc->sc_mtx); 479 if (error) { 480 device_printf(dev, "allocating USB " 481 "transfers failed!\n"); 482 goto detach; 483 } 484 error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx, 485 NULL, sizeof(struct usb2_config_td_cc), 16); 486 if (error) { 487 device_printf(dev, "could not setup config " 488 "thread!\n"); 489 goto detach; 490 } 491 mtx_lock(&sc->sc_mtx); 492 493 /* start setup */ 494 495 usb2_config_td_queue_command 496 (&sc->sc_config_td, NULL, &kue_cfg_first_time_setup, 0, 0); 497 498 /* start watchdog (will exit mutex) */ 499 500 kue_watchdog(sc); 501 502 return (0); /* success */ 503 504detach: 505 kue_detach(dev); 506 return (ENXIO); /* failure */ 507} 508 509static void 510kue_cfg_first_time_setup(struct kue_softc *sc, 511 struct usb2_config_td_cc *cc, uint16_t refcount) 512{ 513 struct ifnet *ifp; 514 515 /* load the firmware into the NIC */ 516 517 kue_cfg_load_fw(sc); 518 519 /* reset the adapter */ 520 521 kue_cfg_reset(sc); 522 523 /* read ethernet descriptor */ 524 kue_cfg_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR, 525 0, &sc->sc_desc, sizeof(sc->sc_desc)); 526 527 sc->sc_mcfilt_max = KUE_MCFILTCNT(sc); 528 if (sc->sc_mcfilt_max > KUE_MCFILT_MAX) { 529 sc->sc_mcfilt_max = KUE_MCFILT_MAX; 530 } 531 mtx_unlock(&sc->sc_mtx); 532 533 ifp = if_alloc(IFT_ETHER); 534 535 mtx_lock(&sc->sc_mtx); 536 537 if (ifp == NULL) { 538 printf("kue%d: could not if_alloc()\n", 539 sc->sc_unit); 540 goto done; 541 } 542 sc->sc_evilhack = ifp; 543 544 ifp->if_softc = sc; 545 if_initname(ifp, "kue", sc->sc_unit); 546 ifp->if_mtu = ETHERMTU; 547 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 548 ifp->if_ioctl = kue_ioctl_cb; 549 ifp->if_start = kue_start_cb; 550 ifp->if_watchdog = NULL; 551 ifp->if_init = kue_init_cb; 552 ifp->if_baudrate = 10000000; 553 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 554 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 555 IFQ_SET_READY(&ifp->if_snd); 556 557 sc->sc_ifp = ifp; 558 559 mtx_unlock(&sc->sc_mtx); 560 561 ether_ifattach(ifp, sc->sc_desc.kue_macaddr); 562 563 mtx_lock(&sc->sc_mtx); 564done: 565 return; 566} 567 568static int 569kue_detach(device_t dev) 570{ 571 struct kue_softc *sc = device_get_softc(dev); 572 struct ifnet *ifp; 573 574 usb2_config_td_drain(&sc->sc_config_td); 575 576 mtx_lock(&sc->sc_mtx); 577 578 usb2_callout_stop(&sc->sc_watchdog); 579 580 kue_cfg_pre_stop(sc, NULL, 0); 581 582 ifp = sc->sc_ifp; 583 584 mtx_unlock(&sc->sc_mtx); 585 586 /* stop all USB transfers first */ 587 usb2_transfer_unsetup(sc->sc_xfer, KUE_ENDPT_MAX); 588 589 /* get rid of any late children */ 590 bus_generic_detach(dev); 591 592 if (ifp) { 593 ether_ifdetach(ifp); 594 if_free(ifp); 595 } 596 usb2_config_td_unsetup(&sc->sc_config_td); 597 598 usb2_callout_drain(&sc->sc_watchdog); 599 600 mtx_destroy(&sc->sc_mtx); 601 602 return (0); 603} 604 605/* 606 * A frame has been uploaded: pass the resulting mbuf chain up to 607 * the higher level protocols. 608 */ 609static void 610kue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer) 611{ 612 struct kue_softc *sc = xfer->priv_sc; 613 struct usb2_xfer *xfer_other = sc->sc_xfer[1]; 614 615 if (usb2_clear_stall_callback(xfer, xfer_other)) { 616 DPRINTF("stall cleared\n"); 617 sc->sc_flags &= ~KUE_FLAG_READ_STALL; 618 usb2_transfer_start(xfer_other); 619 } 620} 621 622static void 623kue_bulk_read_callback(struct usb2_xfer *xfer) 624{ 625 struct kue_softc *sc = xfer->priv_sc; 626 struct ifnet *ifp = sc->sc_ifp; 627 struct mbuf *m = NULL; 628 uint8_t buf[2]; 629 uint16_t len; 630 631 switch (USB_GET_STATE(xfer)) { 632 case USB_ST_TRANSFERRED: 633 634 if (xfer->actlen <= (2 + sizeof(struct ether_header))) { 635 ifp->if_ierrors++; 636 goto tr_setup; 637 } 638 usb2_copy_out(xfer->frbuffers, 0, buf, 2); 639 640 len = buf[0] | (buf[1] << 8); 641 642 xfer->actlen -= 2; 643 644 m = usb2_ether_get_mbuf(); 645 646 if (m == NULL) { 647 ifp->if_ierrors++; 648 goto tr_setup; 649 } 650 xfer->actlen = min(xfer->actlen, m->m_len); 651 xfer->actlen = min(xfer->actlen, len); 652 653 usb2_copy_out(xfer->frbuffers, 2, m->m_data, xfer->actlen); 654 655 ifp->if_ipackets++; 656 m->m_pkthdr.rcvif = ifp; 657 m->m_pkthdr.len = m->m_len = xfer->actlen; 658 659 case USB_ST_SETUP: 660tr_setup: 661 662 if (sc->sc_flags & KUE_FLAG_READ_STALL) { 663 usb2_transfer_start(sc->sc_xfer[3]); 664 } else { 665 xfer->frlengths[0] = xfer->max_data_length; 666 usb2_start_hardware(xfer); 667 } 668 669 /* 670 * At the end of a USB callback it is always safe to unlock 671 * the private mutex of a device! That is why we do the 672 * "if_input" here, and not some lines up! 673 */ 674 if (m) { 675 mtx_unlock(&sc->sc_mtx); 676 (ifp->if_input) (ifp, m); 677 mtx_lock(&sc->sc_mtx); 678 } 679 return; 680 681 default: /* Error */ 682 if (xfer->error != USB_ERR_CANCELLED) { 683 /* try to clear stall first */ 684 sc->sc_flags |= KUE_FLAG_READ_STALL; 685 usb2_transfer_start(sc->sc_xfer[3]); 686 } 687 DPRINTF("bulk read error, %s\n", 688 usb2_errstr(xfer->error)); 689 return; 690 691 } 692} 693 694static void 695kue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer) 696{ 697 struct kue_softc *sc = xfer->priv_sc; 698 struct usb2_xfer *xfer_other = sc->sc_xfer[0]; 699 700 if (usb2_clear_stall_callback(xfer, xfer_other)) { 701 DPRINTF("stall cleared\n"); 702 sc->sc_flags &= ~KUE_FLAG_WRITE_STALL; 703 usb2_transfer_start(xfer_other); 704 } 705} 706 707static void 708kue_bulk_write_callback(struct usb2_xfer *xfer) 709{ 710 struct kue_softc *sc = xfer->priv_sc; 711 struct ifnet *ifp = sc->sc_ifp; 712 struct mbuf *m; 713 uint32_t total_len; 714 uint32_t temp_len; 715 uint8_t buf[2]; 716 717 switch (USB_GET_STATE(xfer)) { 718 case USB_ST_TRANSFERRED: 719 DPRINTFN(11, "transfer complete\n"); 720 721 ifp->if_opackets++; 722 723 case USB_ST_SETUP: 724 725 if (sc->sc_flags & KUE_FLAG_WRITE_STALL) { 726 usb2_transfer_start(sc->sc_xfer[2]); 727 goto done; 728 } 729 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 730 731 if (m == NULL) { 732 goto done; 733 } 734 if (m->m_pkthdr.len > MCLBYTES) { 735 m->m_pkthdr.len = MCLBYTES; 736 } 737 temp_len = (m->m_pkthdr.len + 2); 738 total_len = (temp_len + (64 - (temp_len % 64))); 739 740 /* the first two bytes are the frame length */ 741 742 buf[0] = (uint8_t)(m->m_pkthdr.len); 743 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); 744 745 usb2_copy_in(xfer->frbuffers, 0, buf, 2); 746 747 usb2_m_copy_in(xfer->frbuffers, 2, 748 m, 0, m->m_pkthdr.len); 749 750 usb2_bzero(xfer->frbuffers, temp_len, 751 total_len - temp_len); 752 753 xfer->frlengths[0] = total_len; 754 755 /* 756 * if there's a BPF listener, bounce a copy 757 * of this frame to him: 758 */ 759 BPF_MTAP(ifp, m); 760 761 m_freem(m); 762 763 usb2_start_hardware(xfer); 764 765done: 766 return; 767 768 default: /* Error */ 769 DPRINTFN(11, "transfer error, %s\n", 770 usb2_errstr(xfer->error)); 771 772 if (xfer->error != USB_ERR_CANCELLED) { 773 /* try to clear stall first */ 774 sc->sc_flags |= KUE_FLAG_WRITE_STALL; 775 usb2_transfer_start(sc->sc_xfer[2]); 776 } 777 ifp->if_oerrors++; 778 return; 779 780 } 781} 782 783static void 784kue_start_cb(struct ifnet *ifp) 785{ 786 struct kue_softc *sc = ifp->if_softc; 787 788 mtx_lock(&sc->sc_mtx); 789 790 kue_start_transfers(sc); 791 792 mtx_unlock(&sc->sc_mtx); 793} 794 795static void 796kue_start_transfers(struct kue_softc *sc) 797{ 798 if ((sc->sc_flags & KUE_FLAG_LL_READY) && 799 (sc->sc_flags & KUE_FLAG_HL_READY)) { 800 801 /* 802 * start the USB transfers, if not already started: 803 */ 804 usb2_transfer_start(sc->sc_xfer[1]); 805 usb2_transfer_start(sc->sc_xfer[0]); 806 } 807} 808 809static void 810kue_init_cb(void *arg) 811{ 812 struct kue_softc *sc = arg; 813 814 mtx_lock(&sc->sc_mtx); 815 usb2_config_td_queue_command 816 (&sc->sc_config_td, &kue_cfg_pre_init, 817 &kue_cfg_init, 0, 0); 818 mtx_unlock(&sc->sc_mtx); 819} 820 821static void 822kue_cfg_pre_init(struct kue_softc *sc, 823 struct usb2_config_td_cc *cc, uint16_t refcount) 824{ 825 struct ifnet *ifp = sc->sc_ifp; 826 827 /* immediate configuration */ 828 829 kue_cfg_pre_stop(sc, cc, 0); 830 831 ifp->if_drv_flags |= IFF_DRV_RUNNING; 832 833 sc->sc_flags |= KUE_FLAG_HL_READY; 834} 835 836static void 837kue_cfg_init(struct kue_softc *sc, 838 struct usb2_config_td_cc *cc, uint16_t refcount) 839{ 840 /* set MAC address */ 841 kue_cfg_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 842 0, cc->if_lladdr, ETHER_ADDR_LEN); 843 844 /* I'm not sure how to tune these. */ 845#if 0 846 /* 847 * Leave this one alone for now; setting it 848 * wrong causes lockups on some machines/controllers. 849 */ 850 kue_cfg_setword(sc, KUE_CMD_SET_SOFS, 1); 851#endif 852 kue_cfg_setword(sc, KUE_CMD_SET_URB_SIZE, 64); 853 854 /* load the multicast filter */ 855 kue_cfg_promisc_upd(sc, cc, 0); 856 857 sc->sc_flags |= (KUE_FLAG_READ_STALL | 858 KUE_FLAG_WRITE_STALL | 859 KUE_FLAG_LL_READY); 860 861 kue_start_transfers(sc); 862} 863 864static int 865kue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data) 866{ 867 struct kue_softc *sc = ifp->if_softc; 868 int error = 0; 869 870 switch (command) { 871 case SIOCSIFFLAGS: 872 mtx_lock(&sc->sc_mtx); 873 if (ifp->if_flags & IFF_UP) { 874 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 875 usb2_config_td_queue_command 876 (&sc->sc_config_td, &kue_config_copy, 877 &kue_cfg_promisc_upd, 0, 0); 878 } else { 879 usb2_config_td_queue_command 880 (&sc->sc_config_td, &kue_cfg_pre_init, 881 &kue_cfg_init, 0, 0); 882 } 883 } else { 884 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 885 usb2_config_td_queue_command 886 (&sc->sc_config_td, &kue_cfg_pre_stop, 887 &kue_cfg_stop, 0, 0); 888 } 889 } 890 mtx_unlock(&sc->sc_mtx); 891 break; 892 893 case SIOCADDMULTI: 894 case SIOCDELMULTI: 895 mtx_lock(&sc->sc_mtx); 896 usb2_config_td_queue_command 897 (&sc->sc_config_td, &kue_config_copy, 898 &kue_cfg_promisc_upd, 0, 0); 899 mtx_unlock(&sc->sc_mtx); 900 break; 901 902 default: 903 error = ether_ioctl(ifp, command, data); 904 break; 905 } 906 return (error); 907} 908 909static void 910kue_cfg_tick(struct kue_softc *sc, 911 struct usb2_config_td_cc *cc, uint16_t refcount) 912{ 913 struct ifnet *ifp = sc->sc_ifp; 914 915 if ((ifp == NULL)) { 916 /* not ready */ 917 return; 918 } 919 /* start stopped transfers, if any */ 920 921 kue_start_transfers(sc); 922} 923 924/* 925 * Stop the adapter and free any mbufs allocated to the 926 * RX and TX lists. 927 */ 928static void 929kue_watchdog(void *arg) 930{ 931 struct kue_softc *sc = arg; 932 933 usb2_config_td_queue_command 934 (&sc->sc_config_td, NULL, &kue_cfg_tick, 0, 0); 935 936 usb2_callout_reset(&sc->sc_watchdog, 937 hz, &kue_watchdog, sc); 938 939 mtx_unlock(&sc->sc_mtx); 940} 941 942static void 943kue_cfg_pre_stop(struct kue_softc *sc, 944 struct usb2_config_td_cc *cc, uint16_t refcount) 945{ 946 struct ifnet *ifp = sc->sc_ifp; 947 948 if (cc) { 949 /* copy the needed configuration */ 950 kue_config_copy(sc, cc, refcount); 951 } 952 /* immediate configuration */ 953 954 if (ifp) { 955 /* clear flags */ 956 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 957 } 958 sc->sc_flags &= ~(KUE_FLAG_HL_READY | 959 KUE_FLAG_LL_READY); 960 961 /* 962 * stop all the transfers, if not already stopped: 963 */ 964 usb2_transfer_stop(sc->sc_xfer[0]); 965 usb2_transfer_stop(sc->sc_xfer[1]); 966 usb2_transfer_stop(sc->sc_xfer[2]); 967 usb2_transfer_stop(sc->sc_xfer[3]); 968} 969 970static void 971kue_cfg_stop(struct kue_softc *sc, 972 struct usb2_config_td_cc *cc, uint16_t refcount) 973{ 974 return; 975} 976 977/* 978 * Stop all chip I/O so that the kernel's probe routines don't 979 * get confused by errant DMAs when rebooting. 980 */ 981static int 982kue_shutdown(device_t dev) 983{ 984 struct kue_softc *sc = device_get_softc(dev); 985 986 mtx_lock(&sc->sc_mtx); 987 988 usb2_config_td_queue_command 989 (&sc->sc_config_td, &kue_cfg_pre_stop, 990 &kue_cfg_stop, 0, 0); 991 992 mtx_unlock(&sc->sc_mtx); 993 994 return (0); 995} 996