1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2009 Andrew Thompson (thompsa@FreeBSD.org) 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 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/stdint.h> 28 #include <sys/stddef.h> 29 #include <sys/param.h> 30 #include <sys/queue.h> 31 #include <sys/types.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/module.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/condvar.h> 39 #include <sys/sysctl.h> 40 #include <sys/sx.h> 41 #include <sys/unistd.h> 42 #include <sys/callout.h> 43 #include <sys/malloc.h> 44 #include <sys/priv.h> 45 46 #include <dev/usb/usb.h> 47 #include <dev/usb/usbdi.h> 48 49 #include <dev/usb/usb_process.h> 50 #include <dev/usb/net/usb_ethernet.h> 51 52 SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0, "USB Ethernet parameters"); 53 54 #define UE_LOCK(_ue) mtx_lock((_ue)->ue_mtx) 55 #define UE_UNLOCK(_ue) mtx_unlock((_ue)->ue_mtx) 56 #define UE_LOCK_ASSERT(_ue, t) mtx_assert((_ue)->ue_mtx, t) 57 58 MODULE_DEPEND(uether, usb, 1, 1, 1); 59 MODULE_DEPEND(uether, miibus, 1, 1, 1); 60 61 static struct unrhdr *ueunit; 62 63 static usb_proc_callback_t ue_attach_post_task; 64 static usb_proc_callback_t ue_promisc_task; 65 static usb_proc_callback_t ue_setmulti_task; 66 static usb_proc_callback_t ue_ifmedia_task; 67 static usb_proc_callback_t ue_tick_task; 68 static usb_proc_callback_t ue_start_task; 69 static usb_proc_callback_t ue_stop_task; 70 71 static void ue_init(void *); 72 static void ue_start(struct ifnet *); 73 static int ue_ifmedia_upd(struct ifnet *); 74 static void ue_watchdog(void *); 75 76 /* 77 * Return values: 78 * 0: success 79 * Else: device has been detached 80 */ 81 uint8_t 82 uether_pause(struct usb_ether *ue, unsigned int _ticks) 83 { 84 if (usb_proc_is_gone(&ue->ue_tq)) { 85 /* nothing to do */ 86 return (1); 87 } 88 usb_pause_mtx(ue->ue_mtx, _ticks); 89 return (0); 90 } 91 92 static void 93 ue_queue_command(struct usb_ether *ue, 94 usb_proc_callback_t *fn, 95 struct usb_proc_msg *t0, struct usb_proc_msg *t1) 96 { 97 struct usb_ether_cfg_task *task; 98 99 UE_LOCK_ASSERT(ue, MA_OWNED); 100 101 if (usb_proc_is_gone(&ue->ue_tq)) { 102 return; /* nothing to do */ 103 } 104 /* 105 * NOTE: The task cannot get executed before we drop the 106 * "sc_mtx" mutex. It is safe to update fields in the message 107 * structure after that the message got queued. 108 */ 109 task = (struct usb_ether_cfg_task *) 110 usb_proc_msignal(&ue->ue_tq, t0, t1); 111 112 /* Setup callback and self pointers */ 113 task->hdr.pm_callback = fn; 114 task->ue = ue; 115 116 /* 117 * Start and stop must be synchronous! 118 */ 119 if ((fn == ue_start_task) || (fn == ue_stop_task)) 120 usb_proc_mwait(&ue->ue_tq, t0, t1); 121 } 122 123 struct ifnet * 124 uether_getifp(struct usb_ether *ue) 125 { 126 return (ue->ue_ifp); 127 } 128 129 struct mii_data * 130 uether_getmii(struct usb_ether *ue) 131 { 132 return (device_get_softc(ue->ue_miibus)); 133 } 134 135 void * 136 uether_getsc(struct usb_ether *ue) 137 { 138 return (ue->ue_sc); 139 } 140 141 static int 142 ue_sysctl_parent(SYSCTL_HANDLER_ARGS) 143 { 144 struct usb_ether *ue = arg1; 145 const char *name; 146 147 name = device_get_nameunit(ue->ue_dev); 148 return SYSCTL_OUT(req, name, strlen(name)); 149 } 150 151 int 152 uether_ifattach(struct usb_ether *ue) 153 { 154 int error; 155 156 /* check some critical parameters */ 157 if ((ue->ue_dev == NULL) || 158 (ue->ue_udev == NULL) || 159 (ue->ue_mtx == NULL) || 160 (ue->ue_methods == NULL)) 161 return (EINVAL); 162 163 error = usb_proc_create(&ue->ue_tq, ue->ue_mtx, 164 device_get_nameunit(ue->ue_dev), USB_PRI_MED); 165 if (error) { 166 device_printf(ue->ue_dev, "could not setup taskqueue\n"); 167 goto error; 168 } 169 170 /* fork rest of the attach code */ 171 UE_LOCK(ue); 172 ue_queue_command(ue, ue_attach_post_task, 173 &ue->ue_sync_task[0].hdr, 174 &ue->ue_sync_task[1].hdr); 175 UE_UNLOCK(ue); 176 177 error: 178 return (error); 179 } 180 181 static void 182 ue_attach_post_task(struct usb_proc_msg *_task) 183 { 184 struct usb_ether_cfg_task *task = 185 (struct usb_ether_cfg_task *)_task; 186 struct usb_ether *ue = task->ue; 187 struct ifnet *ifp; 188 int error; 189 char num[14]; /* sufficient for 32 bits */ 190 191 /* first call driver's post attach routine */ 192 ue->ue_methods->ue_attach_post(ue); 193 194 UE_UNLOCK(ue); 195 196 ue->ue_unit = alloc_unr(ueunit); 197 usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0); 198 sysctl_ctx_init(&ue->ue_sysctl_ctx); 199 200 ifp = if_alloc(IFT_ETHER); 201 if (ifp == NULL) { 202 device_printf(ue->ue_dev, "could not allocate ifnet\n"); 203 goto error; 204 } 205 206 ifp->if_softc = ue; 207 if_initname(ifp, "ue", ue->ue_unit); 208 ifp->if_mtu = ETHERMTU; 209 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 210 if (ue->ue_methods->ue_ioctl != NULL) 211 ifp->if_ioctl = ue->ue_methods->ue_ioctl; 212 else 213 ifp->if_ioctl = uether_ioctl; 214 ifp->if_start = ue_start; 215 ifp->if_init = ue_init; 216 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 217 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 218 IFQ_SET_READY(&ifp->if_snd); 219 ue->ue_ifp = ifp; 220 221 if (ue->ue_methods->ue_mii_upd != NULL && 222 ue->ue_methods->ue_mii_sts != NULL) { 223 mtx_lock(&Giant); /* device_xxx() depends on this */ 224 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, 225 ue_ifmedia_upd, ue->ue_methods->ue_mii_sts, 226 BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); 227 mtx_unlock(&Giant); 228 if (error) { 229 device_printf(ue->ue_dev, "attaching PHYs failed\n"); 230 goto error; 231 } 232 } 233 234 if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev)); 235 ether_ifattach(ifp, ue->ue_eaddr); 236 237 snprintf(num, sizeof(num), "%u", ue->ue_unit); 238 ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx, 239 &SYSCTL_NODE_CHILDREN(_net, ue), 240 OID_AUTO, num, CTLFLAG_RD, NULL, ""); 241 SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx, 242 SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO, 243 "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0, 244 ue_sysctl_parent, "A", "parent device"); 245 246 UE_LOCK(ue); 247 return; 248 249 error: 250 free_unr(ueunit, ue->ue_unit); 251 if (ue->ue_ifp != NULL) { 252 if_free(ue->ue_ifp); 253 ue->ue_ifp = NULL; 254 } 255 UE_LOCK(ue); 256 return; 257 } 258 259 void 260 uether_ifdetach(struct usb_ether *ue) 261 { 262 struct ifnet *ifp; 263 264 /* wait for any post attach or other command to complete */ 265 usb_proc_drain(&ue->ue_tq); 266 267 /* read "ifnet" pointer after taskqueue drain */ 268 ifp = ue->ue_ifp; 269 270 if (ifp != NULL) { 271 272 /* we are not running any more */ 273 UE_LOCK(ue); 274 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 275 UE_UNLOCK(ue); 276 277 /* drain any callouts */ 278 usb_callout_drain(&ue->ue_watchdog); 279 280 /* detach miibus */ 281 if (ue->ue_miibus != NULL) { 282 mtx_lock(&Giant); /* device_xxx() depends on this */ 283 device_delete_child(ue->ue_dev, ue->ue_miibus); 284 mtx_unlock(&Giant); 285 } 286 287 /* detach ethernet */ 288 ether_ifdetach(ifp); 289 290 /* free interface instance */ 291 if_free(ifp); 292 293 /* free sysctl */ 294 sysctl_ctx_free(&ue->ue_sysctl_ctx); 295 296 /* free unit */ 297 free_unr(ueunit, ue->ue_unit); 298 } 299 300 /* free taskqueue, if any */ 301 usb_proc_free(&ue->ue_tq); 302 } 303 304 uint8_t 305 uether_is_gone(struct usb_ether *ue) 306 { 307 return (usb_proc_is_gone(&ue->ue_tq)); 308 } 309 310 static void 311 ue_init(void *arg) 312 { 313 struct usb_ether *ue = arg; 314 315 UE_LOCK(ue); 316 ue_queue_command(ue, ue_start_task, 317 &ue->ue_sync_task[0].hdr, 318 &ue->ue_sync_task[1].hdr); 319 UE_UNLOCK(ue); 320 } 321 322 static void 323 ue_start_task(struct usb_proc_msg *_task) 324 { 325 struct usb_ether_cfg_task *task = 326 (struct usb_ether_cfg_task *)_task; 327 struct usb_ether *ue = task->ue; 328 struct ifnet *ifp = ue->ue_ifp; 329 330 UE_LOCK_ASSERT(ue, MA_OWNED); 331 332 ue->ue_methods->ue_init(ue); 333 334 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 335 return; 336 337 if (ue->ue_methods->ue_tick != NULL) 338 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 339 } 340 341 static void 342 ue_stop_task(struct usb_proc_msg *_task) 343 { 344 struct usb_ether_cfg_task *task = 345 (struct usb_ether_cfg_task *)_task; 346 struct usb_ether *ue = task->ue; 347 348 UE_LOCK_ASSERT(ue, MA_OWNED); 349 350 usb_callout_stop(&ue->ue_watchdog); 351 352 ue->ue_methods->ue_stop(ue); 353 } 354 355 static void 356 ue_start(struct ifnet *ifp) 357 { 358 struct usb_ether *ue = ifp->if_softc; 359 360 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 361 return; 362 363 UE_LOCK(ue); 364 ue->ue_methods->ue_start(ue); 365 UE_UNLOCK(ue); 366 } 367 368 static void 369 ue_promisc_task(struct usb_proc_msg *_task) 370 { 371 struct usb_ether_cfg_task *task = 372 (struct usb_ether_cfg_task *)_task; 373 struct usb_ether *ue = task->ue; 374 375 ue->ue_methods->ue_setpromisc(ue); 376 } 377 378 static void 379 ue_setmulti_task(struct usb_proc_msg *_task) 380 { 381 struct usb_ether_cfg_task *task = 382 (struct usb_ether_cfg_task *)_task; 383 struct usb_ether *ue = task->ue; 384 385 ue->ue_methods->ue_setmulti(ue); 386 } 387 388 static int 389 ue_ifmedia_upd(struct ifnet *ifp) 390 { 391 struct usb_ether *ue = ifp->if_softc; 392 393 /* Defer to process context */ 394 UE_LOCK(ue); 395 ue_queue_command(ue, ue_ifmedia_task, 396 &ue->ue_media_task[0].hdr, 397 &ue->ue_media_task[1].hdr); 398 UE_UNLOCK(ue); 399 400 return (0); 401 } 402 403 static void 404 ue_ifmedia_task(struct usb_proc_msg *_task) 405 { 406 struct usb_ether_cfg_task *task = 407 (struct usb_ether_cfg_task *)_task; 408 struct usb_ether *ue = task->ue; 409 struct ifnet *ifp = ue->ue_ifp; 410 411 ue->ue_methods->ue_mii_upd(ifp); 412 } 413 414 static void 415 ue_watchdog(void *arg) 416 { 417 struct usb_ether *ue = arg; 418 struct ifnet *ifp = ue->ue_ifp; 419 420 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 421 return; 422 423 ue_queue_command(ue, ue_tick_task, 424 &ue->ue_tick_task[0].hdr, 425 &ue->ue_tick_task[1].hdr); 426 427 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 428 } 429 430 static void 431 ue_tick_task(struct usb_proc_msg *_task) 432 { 433 struct usb_ether_cfg_task *task = 434 (struct usb_ether_cfg_task *)_task; 435 struct usb_ether *ue = task->ue; 436 struct ifnet *ifp = ue->ue_ifp; 437 438 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 439 return; 440 441 ue->ue_methods->ue_tick(ue); 442 } 443 444 int 445 uether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 446 { 447 struct usb_ether *ue = ifp->if_softc; 448 struct ifreq *ifr = (struct ifreq *)data; 449 struct mii_data *mii; 450 int error = 0; 451 452 switch (command) { 453 case SIOCSIFFLAGS: 454 UE_LOCK(ue); 455 if (ifp->if_flags & IFF_UP) { 456 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 457 ue_queue_command(ue, ue_promisc_task, 458 &ue->ue_promisc_task[0].hdr, 459 &ue->ue_promisc_task[1].hdr); 460 else 461 ue_queue_command(ue, ue_start_task, 462 &ue->ue_sync_task[0].hdr, 463 &ue->ue_sync_task[1].hdr); 464 } else { 465 ue_queue_command(ue, ue_stop_task, 466 &ue->ue_sync_task[0].hdr, 467 &ue->ue_sync_task[1].hdr); 468 } 469 UE_UNLOCK(ue); 470 break; 471 case SIOCADDMULTI: 472 case SIOCDELMULTI: 473 UE_LOCK(ue); 474 ue_queue_command(ue, ue_setmulti_task, 475 &ue->ue_multi_task[0].hdr, 476 &ue->ue_multi_task[1].hdr); 477 UE_UNLOCK(ue); 478 break; 479 case SIOCGIFMEDIA: 480 case SIOCSIFMEDIA: 481 if (ue->ue_miibus != NULL) { 482 mii = device_get_softc(ue->ue_miibus); 483 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 484 } else 485 error = ether_ioctl(ifp, command, data); 486 break; 487 default: 488 error = ether_ioctl(ifp, command, data); 489 break; 490 } 491 return (error); 492 } 493 494 static int 495 uether_modevent(module_t mod, int type, void *data) 496 { 497 498 switch (type) { 499 case MOD_LOAD: 500 ueunit = new_unrhdr(0, INT_MAX, NULL); 501 break; 502 case MOD_UNLOAD: 503 break; 504 default: 505 return (EOPNOTSUPP); 506 } 507 return (0); 508 } 509 static moduledata_t uether_mod = { 510 "uether", 511 uether_modevent, 512 0 513 }; 514 515 struct mbuf * 516 uether_newbuf(void) 517 { 518 struct mbuf *m_new; 519 520 m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 521 if (m_new == NULL) 522 return (NULL); 523 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 524 525 m_adj(m_new, ETHER_ALIGN); 526 return (m_new); 527 } 528 529 int 530 uether_rxmbuf(struct usb_ether *ue, struct mbuf *m, 531 unsigned int len) 532 { 533 struct ifnet *ifp = ue->ue_ifp; 534 535 UE_LOCK_ASSERT(ue, MA_OWNED); 536 537 /* finalize mbuf */ 538 ifp->if_ipackets++; 539 m->m_pkthdr.rcvif = ifp; 540 m->m_pkthdr.len = m->m_len = len; 541 542 /* enqueue for later when the lock can be released */ 543 _IF_ENQUEUE(&ue->ue_rxq, m); 544 return (0); 545 } 546 547 int 548 uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc, 549 unsigned int offset, unsigned int len) 550 { 551 struct ifnet *ifp = ue->ue_ifp; 552 struct mbuf *m; 553 554 UE_LOCK_ASSERT(ue, MA_OWNED); 555 556 if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN) 557 return (1); 558 559 m = uether_newbuf(); 560 if (m == NULL) { 561 ifp->if_iqdrops++; 562 return (ENOMEM); 563 } 564 565 usbd_copy_out(pc, offset, mtod(m, uint8_t *), len); 566 567 /* finalize mbuf */ 568 ifp->if_ipackets++; 569 m->m_pkthdr.rcvif = ifp; 570 m->m_pkthdr.len = m->m_len = len; 571 572 /* enqueue for later when the lock can be released */ 573 _IF_ENQUEUE(&ue->ue_rxq, m); 574 return (0); 575 } 576 577 void 578 uether_rxflush(struct usb_ether *ue) 579 { 580 struct ifnet *ifp = ue->ue_ifp; 581 struct mbuf *m; 582 583 UE_LOCK_ASSERT(ue, MA_OWNED); 584 585 for (;;) { 586 _IF_DEQUEUE(&ue->ue_rxq, m); 587 if (m == NULL) 588 break; 589 590 /* 591 * The USB xfer has been resubmitted so its safe to unlock now. 592 */ 593 UE_UNLOCK(ue); 594 ifp->if_input(ifp, m); 595 UE_LOCK(ue); 596 } 597 } 598 599 DECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 600 MODULE_VERSION(uether, 1); 601