if_kue.c revision 187259
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 187259 2009-01-15 02:35:40Z 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_kuereg.h> 94#include <dev/usb2/ethernet/if_kuefw.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_N_TRANSFER] = { 179 180 [KUE_BULK_DT_WR] = { 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 [KUE_BULK_DT_RD] = { 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 [KUE_BULK_CS_WR] = { 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 [KUE_BULK_CS_RD] = { 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, &sc->sc_mtx, 0); 474 475 iface_index = KUE_IFACE_IDX; 476 error = usb2_transfer_setup(uaa->device, &iface_index, 477 sc->sc_xfer, kue_config, KUE_N_TRANSFER, sc, &sc->sc_mtx); 478 if (error) { 479 device_printf(dev, "allocating USB " 480 "transfers failed!\n"); 481 goto detach; 482 } 483 error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx, 484 NULL, sizeof(struct usb2_config_td_cc), 16); 485 if (error) { 486 device_printf(dev, "could not setup config " 487 "thread!\n"); 488 goto detach; 489 } 490 mtx_lock(&sc->sc_mtx); 491 492 /* start setup */ 493 494 usb2_config_td_queue_command 495 (&sc->sc_config_td, NULL, &kue_cfg_first_time_setup, 0, 0); 496 497 kue_watchdog(sc); 498 mtx_unlock(&sc->sc_mtx); 499 return (0); /* success */ 500 501detach: 502 kue_detach(dev); 503 return (ENXIO); /* failure */ 504} 505 506static void 507kue_cfg_first_time_setup(struct kue_softc *sc, 508 struct usb2_config_td_cc *cc, uint16_t refcount) 509{ 510 struct ifnet *ifp; 511 512 /* load the firmware into the NIC */ 513 514 kue_cfg_load_fw(sc); 515 516 /* reset the adapter */ 517 518 kue_cfg_reset(sc); 519 520 /* read ethernet descriptor */ 521 kue_cfg_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR, 522 0, &sc->sc_desc, sizeof(sc->sc_desc)); 523 524 sc->sc_mcfilt_max = KUE_MCFILTCNT(sc); 525 if (sc->sc_mcfilt_max > KUE_MCFILT_MAX) { 526 sc->sc_mcfilt_max = KUE_MCFILT_MAX; 527 } 528 mtx_unlock(&sc->sc_mtx); 529 530 ifp = if_alloc(IFT_ETHER); 531 532 mtx_lock(&sc->sc_mtx); 533 534 if (ifp == NULL) { 535 printf("kue%d: could not if_alloc()\n", 536 sc->sc_unit); 537 goto done; 538 } 539 sc->sc_evilhack = ifp; 540 541 ifp->if_softc = sc; 542 if_initname(ifp, "kue", sc->sc_unit); 543 ifp->if_mtu = ETHERMTU; 544 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 545 ifp->if_ioctl = kue_ioctl_cb; 546 ifp->if_start = kue_start_cb; 547 ifp->if_watchdog = NULL; 548 ifp->if_init = kue_init_cb; 549 ifp->if_baudrate = 10000000; 550 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 551 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 552 IFQ_SET_READY(&ifp->if_snd); 553 554 sc->sc_ifp = ifp; 555 556 mtx_unlock(&sc->sc_mtx); 557 558 ether_ifattach(ifp, sc->sc_desc.kue_macaddr); 559 560 mtx_lock(&sc->sc_mtx); 561done: 562 return; 563} 564 565static int 566kue_detach(device_t dev) 567{ 568 struct kue_softc *sc = device_get_softc(dev); 569 struct ifnet *ifp; 570 571 usb2_config_td_drain(&sc->sc_config_td); 572 573 mtx_lock(&sc->sc_mtx); 574 575 usb2_callout_stop(&sc->sc_watchdog); 576 577 kue_cfg_pre_stop(sc, NULL, 0); 578 579 ifp = sc->sc_ifp; 580 581 mtx_unlock(&sc->sc_mtx); 582 583 /* stop all USB transfers first */ 584 usb2_transfer_unsetup(sc->sc_xfer, KUE_N_TRANSFER); 585 586 /* get rid of any late children */ 587 bus_generic_detach(dev); 588 589 if (ifp) { 590 ether_ifdetach(ifp); 591 if_free(ifp); 592 } 593 usb2_config_td_unsetup(&sc->sc_config_td); 594 595 usb2_callout_drain(&sc->sc_watchdog); 596 597 mtx_destroy(&sc->sc_mtx); 598 599 return (0); 600} 601 602/* 603 * A frame has been uploaded: pass the resulting mbuf chain up to 604 * the higher level protocols. 605 */ 606static void 607kue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer) 608{ 609 struct kue_softc *sc = xfer->priv_sc; 610 struct usb2_xfer *xfer_other = sc->sc_xfer[KUE_BULK_DT_RD]; 611 612 if (usb2_clear_stall_callback(xfer, xfer_other)) { 613 DPRINTF("stall cleared\n"); 614 sc->sc_flags &= ~KUE_FLAG_READ_STALL; 615 usb2_transfer_start(xfer_other); 616 } 617} 618 619static void 620kue_bulk_read_callback(struct usb2_xfer *xfer) 621{ 622 struct kue_softc *sc = xfer->priv_sc; 623 struct ifnet *ifp = sc->sc_ifp; 624 struct mbuf *m = NULL; 625 uint8_t buf[2]; 626 uint16_t len; 627 628 switch (USB_GET_STATE(xfer)) { 629 case USB_ST_TRANSFERRED: 630 631 if (xfer->actlen <= (2 + sizeof(struct ether_header))) { 632 ifp->if_ierrors++; 633 goto tr_setup; 634 } 635 usb2_copy_out(xfer->frbuffers, 0, buf, 2); 636 637 len = buf[0] | (buf[1] << 8); 638 639 xfer->actlen -= 2; 640 641 m = usb2_ether_get_mbuf(); 642 643 if (m == NULL) { 644 ifp->if_ierrors++; 645 goto tr_setup; 646 } 647 xfer->actlen = min(xfer->actlen, m->m_len); 648 xfer->actlen = min(xfer->actlen, len); 649 650 usb2_copy_out(xfer->frbuffers, 2, m->m_data, xfer->actlen); 651 652 ifp->if_ipackets++; 653 m->m_pkthdr.rcvif = ifp; 654 m->m_pkthdr.len = m->m_len = xfer->actlen; 655 656 case USB_ST_SETUP: 657tr_setup: 658 659 if (sc->sc_flags & KUE_FLAG_READ_STALL) { 660 usb2_transfer_start(sc->sc_xfer[KUE_BULK_CS_RD]); 661 } else { 662 xfer->frlengths[0] = xfer->max_data_length; 663 usb2_start_hardware(xfer); 664 } 665 666 /* 667 * At the end of a USB callback it is always safe to unlock 668 * the private mutex of a device! That is why we do the 669 * "if_input" here, and not some lines up! 670 */ 671 if (m) { 672 mtx_unlock(&sc->sc_mtx); 673 (ifp->if_input) (ifp, m); 674 mtx_lock(&sc->sc_mtx); 675 } 676 return; 677 678 default: /* Error */ 679 if (xfer->error != USB_ERR_CANCELLED) { 680 /* try to clear stall first */ 681 sc->sc_flags |= KUE_FLAG_READ_STALL; 682 usb2_transfer_start(sc->sc_xfer[KUE_BULK_CS_RD]); 683 } 684 DPRINTF("bulk read error, %s\n", 685 usb2_errstr(xfer->error)); 686 return; 687 688 } 689} 690 691static void 692kue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer) 693{ 694 struct kue_softc *sc = xfer->priv_sc; 695 struct usb2_xfer *xfer_other = sc->sc_xfer[KUE_BULK_DT_WR]; 696 697 if (usb2_clear_stall_callback(xfer, xfer_other)) { 698 DPRINTF("stall cleared\n"); 699 sc->sc_flags &= ~KUE_FLAG_WRITE_STALL; 700 usb2_transfer_start(xfer_other); 701 } 702} 703 704static void 705kue_bulk_write_callback(struct usb2_xfer *xfer) 706{ 707 struct kue_softc *sc = xfer->priv_sc; 708 struct ifnet *ifp = sc->sc_ifp; 709 struct mbuf *m; 710 uint32_t total_len; 711 uint32_t temp_len; 712 uint8_t buf[2]; 713 714 switch (USB_GET_STATE(xfer)) { 715 case USB_ST_TRANSFERRED: 716 DPRINTFN(11, "transfer complete\n"); 717 718 ifp->if_opackets++; 719 720 case USB_ST_SETUP: 721 722 if (sc->sc_flags & KUE_FLAG_WRITE_STALL) { 723 usb2_transfer_start(sc->sc_xfer[KUE_BULK_CS_WR]); 724 goto done; 725 } 726 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 727 728 if (m == NULL) { 729 goto done; 730 } 731 if (m->m_pkthdr.len > MCLBYTES) { 732 m->m_pkthdr.len = MCLBYTES; 733 } 734 temp_len = (m->m_pkthdr.len + 2); 735 total_len = (temp_len + (64 - (temp_len % 64))); 736 737 /* the first two bytes are the frame length */ 738 739 buf[0] = (uint8_t)(m->m_pkthdr.len); 740 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); 741 742 usb2_copy_in(xfer->frbuffers, 0, buf, 2); 743 744 usb2_m_copy_in(xfer->frbuffers, 2, 745 m, 0, m->m_pkthdr.len); 746 747 usb2_bzero(xfer->frbuffers, temp_len, 748 total_len - temp_len); 749 750 xfer->frlengths[0] = total_len; 751 752 /* 753 * if there's a BPF listener, bounce a copy 754 * of this frame to him: 755 */ 756 BPF_MTAP(ifp, m); 757 758 m_freem(m); 759 760 usb2_start_hardware(xfer); 761 762done: 763 return; 764 765 default: /* Error */ 766 DPRINTFN(11, "transfer error, %s\n", 767 usb2_errstr(xfer->error)); 768 769 if (xfer->error != USB_ERR_CANCELLED) { 770 /* try to clear stall first */ 771 sc->sc_flags |= KUE_FLAG_WRITE_STALL; 772 usb2_transfer_start(sc->sc_xfer[KUE_BULK_CS_WR]); 773 } 774 ifp->if_oerrors++; 775 return; 776 777 } 778} 779 780static void 781kue_start_cb(struct ifnet *ifp) 782{ 783 struct kue_softc *sc = ifp->if_softc; 784 785 mtx_lock(&sc->sc_mtx); 786 787 kue_start_transfers(sc); 788 789 mtx_unlock(&sc->sc_mtx); 790} 791 792static void 793kue_start_transfers(struct kue_softc *sc) 794{ 795 if ((sc->sc_flags & KUE_FLAG_LL_READY) && 796 (sc->sc_flags & KUE_FLAG_HL_READY)) { 797 798 /* 799 * start the USB transfers, if not already started: 800 */ 801 usb2_transfer_start(sc->sc_xfer[KUE_BULK_DT_RD]); 802 usb2_transfer_start(sc->sc_xfer[KUE_BULK_DT_WR]); 803 } 804} 805 806static void 807kue_init_cb(void *arg) 808{ 809 struct kue_softc *sc = arg; 810 811 mtx_lock(&sc->sc_mtx); 812 usb2_config_td_queue_command 813 (&sc->sc_config_td, &kue_cfg_pre_init, 814 &kue_cfg_init, 0, 0); 815 mtx_unlock(&sc->sc_mtx); 816} 817 818static void 819kue_cfg_pre_init(struct kue_softc *sc, 820 struct usb2_config_td_cc *cc, uint16_t refcount) 821{ 822 struct ifnet *ifp = sc->sc_ifp; 823 824 /* immediate configuration */ 825 826 kue_cfg_pre_stop(sc, cc, 0); 827 828 ifp->if_drv_flags |= IFF_DRV_RUNNING; 829 830 sc->sc_flags |= KUE_FLAG_HL_READY; 831} 832 833static void 834kue_cfg_init(struct kue_softc *sc, 835 struct usb2_config_td_cc *cc, uint16_t refcount) 836{ 837 /* set MAC address */ 838 kue_cfg_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 839 0, cc->if_lladdr, ETHER_ADDR_LEN); 840 841 /* I'm not sure how to tune these. */ 842#if 0 843 /* 844 * Leave this one alone for now; setting it 845 * wrong causes lockups on some machines/controllers. 846 */ 847 kue_cfg_setword(sc, KUE_CMD_SET_SOFS, 1); 848#endif 849 kue_cfg_setword(sc, KUE_CMD_SET_URB_SIZE, 64); 850 851 /* load the multicast filter */ 852 kue_cfg_promisc_upd(sc, cc, 0); 853 854 sc->sc_flags |= (KUE_FLAG_READ_STALL | 855 KUE_FLAG_WRITE_STALL | 856 KUE_FLAG_LL_READY); 857 858 kue_start_transfers(sc); 859} 860 861static int 862kue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data) 863{ 864 struct kue_softc *sc = ifp->if_softc; 865 int error = 0; 866 867 switch (command) { 868 case SIOCSIFFLAGS: 869 mtx_lock(&sc->sc_mtx); 870 if (ifp->if_flags & IFF_UP) { 871 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 872 usb2_config_td_queue_command 873 (&sc->sc_config_td, &kue_config_copy, 874 &kue_cfg_promisc_upd, 0, 0); 875 } else { 876 usb2_config_td_queue_command 877 (&sc->sc_config_td, &kue_cfg_pre_init, 878 &kue_cfg_init, 0, 0); 879 } 880 } else { 881 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 882 usb2_config_td_queue_command 883 (&sc->sc_config_td, &kue_cfg_pre_stop, 884 &kue_cfg_stop, 0, 0); 885 } 886 } 887 mtx_unlock(&sc->sc_mtx); 888 break; 889 890 case SIOCADDMULTI: 891 case SIOCDELMULTI: 892 mtx_lock(&sc->sc_mtx); 893 usb2_config_td_queue_command 894 (&sc->sc_config_td, &kue_config_copy, 895 &kue_cfg_promisc_upd, 0, 0); 896 mtx_unlock(&sc->sc_mtx); 897 break; 898 899 default: 900 error = ether_ioctl(ifp, command, data); 901 break; 902 } 903 return (error); 904} 905 906static void 907kue_cfg_tick(struct kue_softc *sc, 908 struct usb2_config_td_cc *cc, uint16_t refcount) 909{ 910 struct ifnet *ifp = sc->sc_ifp; 911 912 if ((ifp == NULL)) { 913 /* not ready */ 914 return; 915 } 916 /* start stopped transfers, if any */ 917 918 kue_start_transfers(sc); 919} 920 921/* 922 * Stop the adapter and free any mbufs allocated to the 923 * RX and TX lists. 924 */ 925static void 926kue_watchdog(void *arg) 927{ 928 struct kue_softc *sc = arg; 929 930 usb2_config_td_queue_command 931 (&sc->sc_config_td, NULL, &kue_cfg_tick, 0, 0); 932 933 usb2_callout_reset(&sc->sc_watchdog, 934 hz, &kue_watchdog, sc); 935} 936 937static void 938kue_cfg_pre_stop(struct kue_softc *sc, 939 struct usb2_config_td_cc *cc, uint16_t refcount) 940{ 941 struct ifnet *ifp = sc->sc_ifp; 942 943 if (cc) { 944 /* copy the needed configuration */ 945 kue_config_copy(sc, cc, refcount); 946 } 947 /* immediate configuration */ 948 949 if (ifp) { 950 /* clear flags */ 951 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 952 } 953 sc->sc_flags &= ~(KUE_FLAG_HL_READY | 954 KUE_FLAG_LL_READY); 955 956 /* 957 * stop all the transfers, if not already stopped: 958 */ 959 usb2_transfer_stop(sc->sc_xfer[KUE_BULK_DT_WR]); 960 usb2_transfer_stop(sc->sc_xfer[KUE_BULK_DT_RD]); 961 usb2_transfer_stop(sc->sc_xfer[KUE_BULK_CS_WR]); 962 usb2_transfer_stop(sc->sc_xfer[KUE_BULK_CS_RD]); 963} 964 965static void 966kue_cfg_stop(struct kue_softc *sc, 967 struct usb2_config_td_cc *cc, uint16_t refcount) 968{ 969 return; 970} 971 972/* 973 * Stop all chip I/O so that the kernel's probe routines don't 974 * get confused by errant DMAs when rebooting. 975 */ 976static int 977kue_shutdown(device_t dev) 978{ 979 struct kue_softc *sc = device_get_softc(dev); 980 981 mtx_lock(&sc->sc_mtx); 982 983 usb2_config_td_queue_command 984 (&sc->sc_config_td, &kue_cfg_pre_stop, 985 &kue_cfg_stop, 0, 0); 986 987 mtx_unlock(&sc->sc_mtx); 988 989 return (0); 990} 991