1184610Salfred/* $FreeBSD$ */ 2184610Salfred/*- 3188412Sthompsa * Copyright (c) 2009 Andrew Thompson (thompsa@FreeBSD.org) 4184610Salfred * 5184610Salfred * Redistribution and use in source and binary forms, with or without 6184610Salfred * modification, are permitted provided that the following conditions 7184610Salfred * are met: 8184610Salfred * 1. Redistributions of source code must retain the above copyright 9184610Salfred * notice, this list of conditions and the following disclaimer. 10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 11184610Salfred * notice, this list of conditions and the following disclaimer in the 12184610Salfred * documentation and/or other materials provided with the distribution. 13184610Salfred * 14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24184610Salfred * SUCH DAMAGE. 25184610Salfred */ 26184610Salfred 27226709Syongari#include <sys/cdefs.h> 28226709Syongari__FBSDID("$FreeBSD$"); 29226709Syongari 30194677Sthompsa#include <sys/param.h> 31194677Sthompsa#include <sys/systm.h> 32226709Syongari#include <sys/bus.h> 33226709Syongari#include <sys/condvar.h> 34194677Sthompsa#include <sys/kernel.h> 35226709Syongari#include <sys/lock.h> 36226709Syongari#include <sys/malloc.h> 37226709Syongari#include <sys/mbuf.h> 38194677Sthompsa#include <sys/module.h> 39194677Sthompsa#include <sys/mutex.h> 40226709Syongari#include <sys/socket.h> 41226709Syongari#include <sys/sockio.h> 42194677Sthompsa#include <sys/sysctl.h> 43194677Sthompsa#include <sys/sx.h> 44194677Sthompsa 45226709Syongari#include <net/if.h> 46226709Syongari#include <net/ethernet.h> 47226709Syongari#include <net/if_types.h> 48226709Syongari#include <net/if_media.h> 49226709Syongari#include <net/if_vlan_var.h> 50226709Syongari 51226709Syongari#include <dev/mii/mii.h> 52226709Syongari#include <dev/mii/miivar.h> 53226709Syongari 54188942Sthompsa#include <dev/usb/usb.h> 55194677Sthompsa#include <dev/usb/usbdi.h> 56188412Sthompsa 57188942Sthompsa#include <dev/usb/usb_process.h> 58188942Sthompsa#include <dev/usb/net/usb_ethernet.h> 59184610Salfred 60227309Sedstatic SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0, 61227309Sed "USB Ethernet parameters"); 62184610Salfred 63188412Sthompsa#define UE_LOCK(_ue) mtx_lock((_ue)->ue_mtx) 64188412Sthompsa#define UE_UNLOCK(_ue) mtx_unlock((_ue)->ue_mtx) 65188412Sthompsa#define UE_LOCK_ASSERT(_ue, t) mtx_assert((_ue)->ue_mtx, t) 66188412Sthompsa 67188942SthompsaMODULE_DEPEND(uether, usb, 1, 1, 1); 68188942SthompsaMODULE_DEPEND(uether, miibus, 1, 1, 1); 69188552Sthompsa 70188412Sthompsastatic struct unrhdr *ueunit; 71188412Sthompsa 72193045Sthompsastatic usb_proc_callback_t ue_attach_post_task; 73193045Sthompsastatic usb_proc_callback_t ue_promisc_task; 74193045Sthompsastatic usb_proc_callback_t ue_setmulti_task; 75193045Sthompsastatic usb_proc_callback_t ue_ifmedia_task; 76193045Sthompsastatic usb_proc_callback_t ue_tick_task; 77193045Sthompsastatic usb_proc_callback_t ue_start_task; 78193045Sthompsastatic usb_proc_callback_t ue_stop_task; 79188412Sthompsa 80188412Sthompsastatic void ue_init(void *); 81188412Sthompsastatic void ue_start(struct ifnet *); 82188412Sthompsastatic int ue_ifmedia_upd(struct ifnet *); 83188412Sthompsastatic void ue_watchdog(void *); 84188412Sthompsa 85188412Sthompsa/* 86188412Sthompsa * Return values: 87188412Sthompsa * 0: success 88188412Sthompsa * Else: device has been detached 89188412Sthompsa */ 90188412Sthompsauint8_t 91194228Sthompsauether_pause(struct usb_ether *ue, unsigned int _ticks) 92184610Salfred{ 93194228Sthompsa if (usb_proc_is_gone(&ue->ue_tq)) { 94188412Sthompsa /* nothing to do */ 95188412Sthompsa return (1); 96188412Sthompsa } 97194228Sthompsa usb_pause_mtx(ue->ue_mtx, _ticks); 98188412Sthompsa return (0); 99188412Sthompsa} 100184610Salfred 101188412Sthompsastatic void 102192984Sthompsaue_queue_command(struct usb_ether *ue, 103193045Sthompsa usb_proc_callback_t *fn, 104192984Sthompsa struct usb_proc_msg *t0, struct usb_proc_msg *t1) 105188412Sthompsa{ 106192984Sthompsa struct usb_ether_cfg_task *task; 107188412Sthompsa 108188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 109188412Sthompsa 110194228Sthompsa if (usb_proc_is_gone(&ue->ue_tq)) { 111188412Sthompsa return; /* nothing to do */ 112184610Salfred } 113188412Sthompsa /* 114188412Sthompsa * NOTE: The task cannot get executed before we drop the 115188412Sthompsa * "sc_mtx" mutex. It is safe to update fields in the message 116188412Sthompsa * structure after that the message got queued. 117188412Sthompsa */ 118192984Sthompsa task = (struct usb_ether_cfg_task *) 119194228Sthompsa usb_proc_msignal(&ue->ue_tq, t0, t1); 120188412Sthompsa 121188412Sthompsa /* Setup callback and self pointers */ 122188412Sthompsa task->hdr.pm_callback = fn; 123188412Sthompsa task->ue = ue; 124188412Sthompsa 125188412Sthompsa /* 126188412Sthompsa * Start and stop must be synchronous! 127188412Sthompsa */ 128188412Sthompsa if ((fn == ue_start_task) || (fn == ue_stop_task)) 129194228Sthompsa usb_proc_mwait(&ue->ue_tq, t0, t1); 130184610Salfred} 131184610Salfred 132188412Sthompsastruct ifnet * 133194228Sthompsauether_getifp(struct usb_ether *ue) 134184610Salfred{ 135188412Sthompsa return (ue->ue_ifp); 136188412Sthompsa} 137184610Salfred 138188412Sthompsastruct mii_data * 139194228Sthompsauether_getmii(struct usb_ether *ue) 140188412Sthompsa{ 141188412Sthompsa return (device_get_softc(ue->ue_miibus)); 142188412Sthompsa} 143188412Sthompsa 144188412Sthompsavoid * 145194228Sthompsauether_getsc(struct usb_ether *ue) 146188412Sthompsa{ 147188412Sthompsa return (ue->ue_sc); 148188412Sthompsa} 149188412Sthompsa 150188412Sthompsastatic int 151188412Sthompsaue_sysctl_parent(SYSCTL_HANDLER_ARGS) 152188412Sthompsa{ 153192984Sthompsa struct usb_ether *ue = arg1; 154188412Sthompsa const char *name; 155188412Sthompsa 156188412Sthompsa name = device_get_nameunit(ue->ue_dev); 157188412Sthompsa return SYSCTL_OUT(req, name, strlen(name)); 158188412Sthompsa} 159188412Sthompsa 160188412Sthompsaint 161194228Sthompsauether_ifattach(struct usb_ether *ue) 162188412Sthompsa{ 163188412Sthompsa int error; 164188412Sthompsa 165188412Sthompsa /* check some critical parameters */ 166188412Sthompsa if ((ue->ue_dev == NULL) || 167188412Sthompsa (ue->ue_udev == NULL) || 168188412Sthompsa (ue->ue_mtx == NULL) || 169188412Sthompsa (ue->ue_methods == NULL)) 170188412Sthompsa return (EINVAL); 171188412Sthompsa 172194228Sthompsa error = usb_proc_create(&ue->ue_tq, ue->ue_mtx, 173188412Sthompsa device_get_nameunit(ue->ue_dev), USB_PRI_MED); 174188412Sthompsa if (error) { 175188412Sthompsa device_printf(ue->ue_dev, "could not setup taskqueue\n"); 176188412Sthompsa goto error; 177188412Sthompsa } 178188412Sthompsa 179188412Sthompsa /* fork rest of the attach code */ 180188412Sthompsa UE_LOCK(ue); 181188412Sthompsa ue_queue_command(ue, ue_attach_post_task, 182188412Sthompsa &ue->ue_sync_task[0].hdr, 183188412Sthompsa &ue->ue_sync_task[1].hdr); 184188412Sthompsa UE_UNLOCK(ue); 185188412Sthompsa 186188412Sthompsaerror: 187188412Sthompsa return (error); 188188412Sthompsa} 189188412Sthompsa 190188412Sthompsastatic void 191192984Sthompsaue_attach_post_task(struct usb_proc_msg *_task) 192188412Sthompsa{ 193192984Sthompsa struct usb_ether_cfg_task *task = 194192984Sthompsa (struct usb_ether_cfg_task *)_task; 195192984Sthompsa struct usb_ether *ue = task->ue; 196188412Sthompsa struct ifnet *ifp; 197188412Sthompsa int error; 198188412Sthompsa char num[14]; /* sufficient for 32 bits */ 199188412Sthompsa 200188412Sthompsa /* first call driver's post attach routine */ 201188412Sthompsa ue->ue_methods->ue_attach_post(ue); 202188412Sthompsa 203188412Sthompsa UE_UNLOCK(ue); 204188412Sthompsa 205188412Sthompsa ue->ue_unit = alloc_unr(ueunit); 206194228Sthompsa usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0); 207188412Sthompsa sysctl_ctx_init(&ue->ue_sysctl_ctx); 208188412Sthompsa 209226709Syongari error = 0; 210262436Srodrigc CURVNET_SET_QUIET(vnet0); 211188412Sthompsa ifp = if_alloc(IFT_ETHER); 212184610Salfred if (ifp == NULL) { 213188412Sthompsa device_printf(ue->ue_dev, "could not allocate ifnet\n"); 214226709Syongari goto fail; 215184610Salfred } 216184610Salfred 217188412Sthompsa ifp->if_softc = ue; 218188412Sthompsa if_initname(ifp, "ue", ue->ue_unit); 219226709Syongari if (ue->ue_methods->ue_attach_post_sub != NULL) { 220226709Syongari ue->ue_ifp = ifp; 221226709Syongari error = ue->ue_methods->ue_attach_post_sub(ue); 222226709Syongari } else { 223226709Syongari ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 224226709Syongari if (ue->ue_methods->ue_ioctl != NULL) 225226709Syongari ifp->if_ioctl = ue->ue_methods->ue_ioctl; 226226709Syongari else 227226709Syongari ifp->if_ioctl = uether_ioctl; 228226709Syongari ifp->if_start = ue_start; 229226709Syongari ifp->if_init = ue_init; 230226709Syongari IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 231226709Syongari ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 232226709Syongari IFQ_SET_READY(&ifp->if_snd); 233226709Syongari ue->ue_ifp = ifp; 234184610Salfred 235226709Syongari if (ue->ue_methods->ue_mii_upd != NULL && 236226709Syongari ue->ue_methods->ue_mii_sts != NULL) { 237226709Syongari /* device_xxx() depends on this */ 238226709Syongari mtx_lock(&Giant); 239226709Syongari error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, 240226709Syongari ue_ifmedia_upd, ue->ue_methods->ue_mii_sts, 241226709Syongari BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); 242226709Syongari mtx_unlock(&Giant); 243188412Sthompsa } 244188412Sthompsa } 245184610Salfred 246226709Syongari if (error) { 247226709Syongari device_printf(ue->ue_dev, "attaching PHYs failed\n"); 248226709Syongari goto fail; 249226709Syongari } 250226709Syongari 251188412Sthompsa if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev)); 252188412Sthompsa ether_ifattach(ifp, ue->ue_eaddr); 253226709Syongari /* Tell upper layer we support VLAN oversized frames. */ 254226709Syongari if (ifp->if_capabilities & IFCAP_VLAN_MTU) 255226709Syongari ifp->if_hdrlen = sizeof(struct ether_vlan_header); 256188412Sthompsa 257262436Srodrigc CURVNET_RESTORE(); 258262436Srodrigc 259188412Sthompsa snprintf(num, sizeof(num), "%u", ue->ue_unit); 260188412Sthompsa ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx, 261188412Sthompsa &SYSCTL_NODE_CHILDREN(_net, ue), 262188412Sthompsa OID_AUTO, num, CTLFLAG_RD, NULL, ""); 263188412Sthompsa SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx, 264188412Sthompsa SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO, 265217556Smdf "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0, 266188412Sthompsa ue_sysctl_parent, "A", "parent device"); 267188412Sthompsa 268188412Sthompsa UE_LOCK(ue); 269188412Sthompsa return; 270188412Sthompsa 271226709Syongarifail: 272262436Srodrigc CURVNET_RESTORE(); 273188412Sthompsa free_unr(ueunit, ue->ue_unit); 274188412Sthompsa if (ue->ue_ifp != NULL) { 275188412Sthompsa if_free(ue->ue_ifp); 276188412Sthompsa ue->ue_ifp = NULL; 277184610Salfred } 278188412Sthompsa UE_LOCK(ue); 279188412Sthompsa return; 280188412Sthompsa} 281184610Salfred 282188412Sthompsavoid 283194228Sthompsauether_ifdetach(struct usb_ether *ue) 284188412Sthompsa{ 285188412Sthompsa struct ifnet *ifp; 286184610Salfred 287188412Sthompsa /* wait for any post attach or other command to complete */ 288194228Sthompsa usb_proc_drain(&ue->ue_tq); 289184610Salfred 290188412Sthompsa /* read "ifnet" pointer after taskqueue drain */ 291188412Sthompsa ifp = ue->ue_ifp; 292184610Salfred 293188412Sthompsa if (ifp != NULL) { 294184610Salfred 295188412Sthompsa /* we are not running any more */ 296188412Sthompsa UE_LOCK(ue); 297188412Sthompsa ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 298188412Sthompsa UE_UNLOCK(ue); 299184610Salfred 300188412Sthompsa /* drain any callouts */ 301194228Sthompsa usb_callout_drain(&ue->ue_watchdog); 302188412Sthompsa 303188412Sthompsa /* detach miibus */ 304188412Sthompsa if (ue->ue_miibus != NULL) { 305196403Sjhb mtx_lock(&Giant); /* device_xxx() depends on this */ 306188412Sthompsa device_delete_child(ue->ue_dev, ue->ue_miibus); 307196403Sjhb mtx_unlock(&Giant); 308184610Salfred } 309184610Salfred 310188412Sthompsa /* detach ethernet */ 311188412Sthompsa ether_ifdetach(ifp); 312184610Salfred 313188412Sthompsa /* free interface instance */ 314188412Sthompsa if_free(ifp); 315188412Sthompsa 316188412Sthompsa /* free sysctl */ 317188412Sthompsa sysctl_ctx_free(&ue->ue_sysctl_ctx); 318188412Sthompsa 319188412Sthompsa /* free unit */ 320188412Sthompsa free_unr(ueunit, ue->ue_unit); 321188412Sthompsa } 322188412Sthompsa 323188412Sthompsa /* free taskqueue, if any */ 324194228Sthompsa usb_proc_free(&ue->ue_tq); 325188412Sthompsa} 326188412Sthompsa 327188412Sthompsauint8_t 328194228Sthompsauether_is_gone(struct usb_ether *ue) 329188412Sthompsa{ 330194228Sthompsa return (usb_proc_is_gone(&ue->ue_tq)); 331188412Sthompsa} 332188412Sthompsa 333226709Syongarivoid 334226709Syongariuether_init(void *arg) 335226709Syongari{ 336226709Syongari 337226709Syongari ue_init(arg); 338226709Syongari} 339226709Syongari 340188412Sthompsastatic void 341188412Sthompsaue_init(void *arg) 342188412Sthompsa{ 343192984Sthompsa struct usb_ether *ue = arg; 344188412Sthompsa 345188412Sthompsa UE_LOCK(ue); 346188412Sthompsa ue_queue_command(ue, ue_start_task, 347188412Sthompsa &ue->ue_sync_task[0].hdr, 348188412Sthompsa &ue->ue_sync_task[1].hdr); 349188412Sthompsa UE_UNLOCK(ue); 350188412Sthompsa} 351188412Sthompsa 352188412Sthompsastatic void 353192984Sthompsaue_start_task(struct usb_proc_msg *_task) 354188412Sthompsa{ 355192984Sthompsa struct usb_ether_cfg_task *task = 356192984Sthompsa (struct usb_ether_cfg_task *)_task; 357192984Sthompsa struct usb_ether *ue = task->ue; 358188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 359188412Sthompsa 360188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 361188412Sthompsa 362188412Sthompsa ue->ue_methods->ue_init(ue); 363188412Sthompsa 364188412Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 365188412Sthompsa return; 366188412Sthompsa 367188412Sthompsa if (ue->ue_methods->ue_tick != NULL) 368194228Sthompsa usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 369188412Sthompsa} 370188412Sthompsa 371188412Sthompsastatic void 372192984Sthompsaue_stop_task(struct usb_proc_msg *_task) 373188412Sthompsa{ 374192984Sthompsa struct usb_ether_cfg_task *task = 375192984Sthompsa (struct usb_ether_cfg_task *)_task; 376192984Sthompsa struct usb_ether *ue = task->ue; 377188412Sthompsa 378188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 379188412Sthompsa 380194228Sthompsa usb_callout_stop(&ue->ue_watchdog); 381188412Sthompsa 382188412Sthompsa ue->ue_methods->ue_stop(ue); 383188412Sthompsa} 384188412Sthompsa 385226709Syongarivoid 386226709Syongariuether_start(struct ifnet *ifp) 387226709Syongari{ 388226709Syongari 389226709Syongari ue_start(ifp); 390226709Syongari} 391226709Syongari 392188412Sthompsastatic void 393188412Sthompsaue_start(struct ifnet *ifp) 394188412Sthompsa{ 395192984Sthompsa struct usb_ether *ue = ifp->if_softc; 396188412Sthompsa 397188412Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 398188412Sthompsa return; 399188412Sthompsa 400188412Sthompsa UE_LOCK(ue); 401188412Sthompsa ue->ue_methods->ue_start(ue); 402188412Sthompsa UE_UNLOCK(ue); 403188412Sthompsa} 404188412Sthompsa 405188412Sthompsastatic void 406192984Sthompsaue_promisc_task(struct usb_proc_msg *_task) 407188412Sthompsa{ 408192984Sthompsa struct usb_ether_cfg_task *task = 409192984Sthompsa (struct usb_ether_cfg_task *)_task; 410192984Sthompsa struct usb_ether *ue = task->ue; 411188412Sthompsa 412188412Sthompsa ue->ue_methods->ue_setpromisc(ue); 413188412Sthompsa} 414188412Sthompsa 415188412Sthompsastatic void 416192984Sthompsaue_setmulti_task(struct usb_proc_msg *_task) 417188412Sthompsa{ 418192984Sthompsa struct usb_ether_cfg_task *task = 419192984Sthompsa (struct usb_ether_cfg_task *)_task; 420192984Sthompsa struct usb_ether *ue = task->ue; 421188412Sthompsa 422188412Sthompsa ue->ue_methods->ue_setmulti(ue); 423188412Sthompsa} 424188412Sthompsa 425226709Syongariint 426226709Syongariuether_ifmedia_upd(struct ifnet *ifp) 427226709Syongari{ 428226709Syongari 429226709Syongari return (ue_ifmedia_upd(ifp)); 430226709Syongari} 431226709Syongari 432188412Sthompsastatic int 433188412Sthompsaue_ifmedia_upd(struct ifnet *ifp) 434188412Sthompsa{ 435192984Sthompsa struct usb_ether *ue = ifp->if_softc; 436188412Sthompsa 437188412Sthompsa /* Defer to process context */ 438188412Sthompsa UE_LOCK(ue); 439188412Sthompsa ue_queue_command(ue, ue_ifmedia_task, 440188412Sthompsa &ue->ue_media_task[0].hdr, 441188412Sthompsa &ue->ue_media_task[1].hdr); 442188412Sthompsa UE_UNLOCK(ue); 443188412Sthompsa 444188412Sthompsa return (0); 445188412Sthompsa} 446188412Sthompsa 447188412Sthompsastatic void 448192984Sthompsaue_ifmedia_task(struct usb_proc_msg *_task) 449188412Sthompsa{ 450192984Sthompsa struct usb_ether_cfg_task *task = 451192984Sthompsa (struct usb_ether_cfg_task *)_task; 452192984Sthompsa struct usb_ether *ue = task->ue; 453188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 454188412Sthompsa 455188412Sthompsa ue->ue_methods->ue_mii_upd(ifp); 456188412Sthompsa} 457188412Sthompsa 458188412Sthompsastatic void 459188412Sthompsaue_watchdog(void *arg) 460188412Sthompsa{ 461192984Sthompsa struct usb_ether *ue = arg; 462188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 463188412Sthompsa 464188412Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 465188412Sthompsa return; 466188412Sthompsa 467188412Sthompsa ue_queue_command(ue, ue_tick_task, 468188412Sthompsa &ue->ue_tick_task[0].hdr, 469188412Sthompsa &ue->ue_tick_task[1].hdr); 470188412Sthompsa 471194228Sthompsa usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 472188412Sthompsa} 473188412Sthompsa 474188412Sthompsastatic void 475192984Sthompsaue_tick_task(struct usb_proc_msg *_task) 476188412Sthompsa{ 477192984Sthompsa struct usb_ether_cfg_task *task = 478192984Sthompsa (struct usb_ether_cfg_task *)_task; 479192984Sthompsa struct usb_ether *ue = task->ue; 480188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 481188412Sthompsa 482188412Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 483188412Sthompsa return; 484188412Sthompsa 485188412Sthompsa ue->ue_methods->ue_tick(ue); 486188412Sthompsa} 487188412Sthompsa 488188412Sthompsaint 489194228Sthompsauether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 490188412Sthompsa{ 491192984Sthompsa struct usb_ether *ue = ifp->if_softc; 492188412Sthompsa struct ifreq *ifr = (struct ifreq *)data; 493188412Sthompsa struct mii_data *mii; 494188412Sthompsa int error = 0; 495188412Sthompsa 496188412Sthompsa switch (command) { 497188412Sthompsa case SIOCSIFFLAGS: 498188412Sthompsa UE_LOCK(ue); 499188412Sthompsa if (ifp->if_flags & IFF_UP) { 500188412Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) 501188412Sthompsa ue_queue_command(ue, ue_promisc_task, 502188412Sthompsa &ue->ue_promisc_task[0].hdr, 503188412Sthompsa &ue->ue_promisc_task[1].hdr); 504188412Sthompsa else 505188412Sthompsa ue_queue_command(ue, ue_start_task, 506188412Sthompsa &ue->ue_sync_task[0].hdr, 507188412Sthompsa &ue->ue_sync_task[1].hdr); 508188412Sthompsa } else { 509188412Sthompsa ue_queue_command(ue, ue_stop_task, 510188412Sthompsa &ue->ue_sync_task[0].hdr, 511188412Sthompsa &ue->ue_sync_task[1].hdr); 512184610Salfred } 513188412Sthompsa UE_UNLOCK(ue); 514188412Sthompsa break; 515188412Sthompsa case SIOCADDMULTI: 516188412Sthompsa case SIOCDELMULTI: 517188412Sthompsa UE_LOCK(ue); 518188412Sthompsa ue_queue_command(ue, ue_setmulti_task, 519188412Sthompsa &ue->ue_multi_task[0].hdr, 520188412Sthompsa &ue->ue_multi_task[1].hdr); 521188412Sthompsa UE_UNLOCK(ue); 522188412Sthompsa break; 523188412Sthompsa case SIOCGIFMEDIA: 524188412Sthompsa case SIOCSIFMEDIA: 525188412Sthompsa if (ue->ue_miibus != NULL) { 526188412Sthompsa mii = device_get_softc(ue->ue_miibus); 527188412Sthompsa error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 528188412Sthompsa } else 529188412Sthompsa error = ether_ioctl(ifp, command, data); 530188412Sthompsa break; 531188412Sthompsa default: 532188412Sthompsa error = ether_ioctl(ifp, command, data); 533188412Sthompsa break; 534184610Salfred } 535188412Sthompsa return (error); 536184610Salfred} 537188412Sthompsa 538188412Sthompsastatic int 539194228Sthompsauether_modevent(module_t mod, int type, void *data) 540188412Sthompsa{ 541188412Sthompsa 542188412Sthompsa switch (type) { 543188412Sthompsa case MOD_LOAD: 544188412Sthompsa ueunit = new_unrhdr(0, INT_MAX, NULL); 545188412Sthompsa break; 546188412Sthompsa case MOD_UNLOAD: 547188412Sthompsa break; 548188412Sthompsa default: 549188412Sthompsa return (EOPNOTSUPP); 550188412Sthompsa } 551188412Sthompsa return (0); 552188412Sthompsa} 553194228Sthompsastatic moduledata_t uether_mod = { 554188942Sthompsa "uether", 555194228Sthompsa uether_modevent, 556241394Skevlo 0 557188412Sthompsa}; 558188412Sthompsa 559189528Sthompsastruct mbuf * 560194228Sthompsauether_newbuf(void) 561189528Sthompsa{ 562189528Sthompsa struct mbuf *m_new; 563189528Sthompsa 564243857Sglebius m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 565189528Sthompsa if (m_new == NULL) 566189528Sthompsa return (NULL); 567189528Sthompsa m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 568189528Sthompsa 569189528Sthompsa m_adj(m_new, ETHER_ALIGN); 570189528Sthompsa return (m_new); 571189528Sthompsa} 572189528Sthompsa 573188412Sthompsaint 574194228Sthompsauether_rxmbuf(struct usb_ether *ue, struct mbuf *m, 575188412Sthompsa unsigned int len) 576188412Sthompsa{ 577188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 578188412Sthompsa 579188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 580188412Sthompsa 581188412Sthompsa /* finalize mbuf */ 582188412Sthompsa ifp->if_ipackets++; 583188412Sthompsa m->m_pkthdr.rcvif = ifp; 584188412Sthompsa m->m_pkthdr.len = m->m_len = len; 585188412Sthompsa 586188412Sthompsa /* enqueue for later when the lock can be released */ 587188412Sthompsa _IF_ENQUEUE(&ue->ue_rxq, m); 588188412Sthompsa return (0); 589188412Sthompsa} 590188412Sthompsa 591188412Sthompsaint 592194228Sthompsauether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc, 593188412Sthompsa unsigned int offset, unsigned int len) 594188412Sthompsa{ 595188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 596188412Sthompsa struct mbuf *m; 597188412Sthompsa 598188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 599188412Sthompsa 600189528Sthompsa if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN) 601188412Sthompsa return (1); 602188412Sthompsa 603194228Sthompsa m = uether_newbuf(); 604188412Sthompsa if (m == NULL) { 605213438Syongari ifp->if_iqdrops++; 606188412Sthompsa return (ENOMEM); 607188412Sthompsa } 608188412Sthompsa 609194228Sthompsa usbd_copy_out(pc, offset, mtod(m, uint8_t *), len); 610188412Sthompsa 611188412Sthompsa /* finalize mbuf */ 612188412Sthompsa ifp->if_ipackets++; 613188412Sthompsa m->m_pkthdr.rcvif = ifp; 614188412Sthompsa m->m_pkthdr.len = m->m_len = len; 615188412Sthompsa 616188412Sthompsa /* enqueue for later when the lock can be released */ 617188412Sthompsa _IF_ENQUEUE(&ue->ue_rxq, m); 618188412Sthompsa return (0); 619188412Sthompsa} 620188412Sthompsa 621188412Sthompsavoid 622194228Sthompsauether_rxflush(struct usb_ether *ue) 623188412Sthompsa{ 624188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 625188412Sthompsa struct mbuf *m; 626188412Sthompsa 627188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 628188412Sthompsa 629188412Sthompsa for (;;) { 630188412Sthompsa _IF_DEQUEUE(&ue->ue_rxq, m); 631188412Sthompsa if (m == NULL) 632188412Sthompsa break; 633188412Sthompsa 634188412Sthompsa /* 635188412Sthompsa * The USB xfer has been resubmitted so its safe to unlock now. 636188412Sthompsa */ 637188412Sthompsa UE_UNLOCK(ue); 638188412Sthompsa ifp->if_input(ifp, m); 639188412Sthompsa UE_LOCK(ue); 640188412Sthompsa } 641188412Sthompsa} 642188412Sthompsa 643194228SthompsaDECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 644188942SthompsaMODULE_VERSION(uether, 1); 645