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 27229105Shselasky#include <sys/cdefs.h> 28229105Shselasky__FBSDID("$FreeBSD$"); 29229105Shselasky 30194677Sthompsa#include <sys/param.h> 31194677Sthompsa#include <sys/systm.h> 32229105Shselasky#include <sys/bus.h> 33229105Shselasky#include <sys/condvar.h> 34194677Sthompsa#include <sys/kernel.h> 35229105Shselasky#include <sys/lock.h> 36229105Shselasky#include <sys/malloc.h> 37229105Shselasky#include <sys/mbuf.h> 38194677Sthompsa#include <sys/module.h> 39194677Sthompsa#include <sys/mutex.h> 40229105Shselasky#include <sys/socket.h> 41229105Shselasky#include <sys/sockio.h> 42194677Sthompsa#include <sys/sysctl.h> 43194677Sthompsa#include <sys/sx.h> 44194677Sthompsa 45229105Shselasky#include <net/if.h> 46229105Shselasky#include <net/ethernet.h> 47229105Shselasky#include <net/if_types.h> 48229105Shselasky#include <net/if_media.h> 49229105Shselasky#include <net/if_vlan_var.h> 50229105Shselasky 51229105Shselasky#include <dev/mii/mii.h> 52229105Shselasky#include <dev/mii/miivar.h> 53229105Shselasky 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 60248085Smariusstatic SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0, 61248085Smarius "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 209229105Shselasky error = 0; 210262594Srodrigc 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"); 214229105Shselasky goto fail; 215184610Salfred } 216184610Salfred 217188412Sthompsa ifp->if_softc = ue; 218188412Sthompsa if_initname(ifp, "ue", ue->ue_unit); 219229105Shselasky if (ue->ue_methods->ue_attach_post_sub != NULL) { 220229105Shselasky ue->ue_ifp = ifp; 221229105Shselasky error = ue->ue_methods->ue_attach_post_sub(ue); 222229105Shselasky } else { 223229105Shselasky ifp->if_mtu = ETHERMTU; 224229105Shselasky ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 225229105Shselasky if (ue->ue_methods->ue_ioctl != NULL) 226229105Shselasky ifp->if_ioctl = ue->ue_methods->ue_ioctl; 227229105Shselasky else 228229105Shselasky ifp->if_ioctl = uether_ioctl; 229229105Shselasky ifp->if_start = ue_start; 230229105Shselasky ifp->if_init = ue_init; 231229105Shselasky IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 232229105Shselasky ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 233229105Shselasky IFQ_SET_READY(&ifp->if_snd); 234229105Shselasky ue->ue_ifp = ifp; 235184610Salfred 236229105Shselasky if (ue->ue_methods->ue_mii_upd != NULL && 237229105Shselasky ue->ue_methods->ue_mii_sts != NULL) { 238229105Shselasky /* device_xxx() depends on this */ 239229105Shselasky mtx_lock(&Giant); 240229105Shselasky error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, 241229105Shselasky ue_ifmedia_upd, ue->ue_methods->ue_mii_sts, 242229105Shselasky BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); 243229105Shselasky mtx_unlock(&Giant); 244188412Sthompsa } 245188412Sthompsa } 246184610Salfred 247229105Shselasky if (error) { 248229105Shselasky device_printf(ue->ue_dev, "attaching PHYs failed\n"); 249229105Shselasky goto fail; 250229105Shselasky } 251229105Shselasky 252188412Sthompsa if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev)); 253188412Sthompsa ether_ifattach(ifp, ue->ue_eaddr); 254229105Shselasky /* Tell upper layer we support VLAN oversized frames. */ 255229105Shselasky if (ifp->if_capabilities & IFCAP_VLAN_MTU) 256229105Shselasky ifp->if_hdrlen = sizeof(struct ether_vlan_header); 257188412Sthompsa 258262594Srodrigc CURVNET_RESTORE(); 259262594Srodrigc 260188412Sthompsa snprintf(num, sizeof(num), "%u", ue->ue_unit); 261188412Sthompsa ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx, 262188412Sthompsa &SYSCTL_NODE_CHILDREN(_net, ue), 263188412Sthompsa OID_AUTO, num, CTLFLAG_RD, NULL, ""); 264188412Sthompsa SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx, 265188412Sthompsa SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO, 266217556Smdf "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0, 267188412Sthompsa ue_sysctl_parent, "A", "parent device"); 268188412Sthompsa 269188412Sthompsa UE_LOCK(ue); 270188412Sthompsa return; 271188412Sthompsa 272229105Shselaskyfail: 273262594Srodrigc CURVNET_RESTORE(); 274188412Sthompsa free_unr(ueunit, ue->ue_unit); 275188412Sthompsa if (ue->ue_ifp != NULL) { 276188412Sthompsa if_free(ue->ue_ifp); 277188412Sthompsa ue->ue_ifp = NULL; 278184610Salfred } 279188412Sthompsa UE_LOCK(ue); 280188412Sthompsa return; 281188412Sthompsa} 282184610Salfred 283188412Sthompsavoid 284194228Sthompsauether_ifdetach(struct usb_ether *ue) 285188412Sthompsa{ 286188412Sthompsa struct ifnet *ifp; 287184610Salfred 288188412Sthompsa /* wait for any post attach or other command to complete */ 289194228Sthompsa usb_proc_drain(&ue->ue_tq); 290184610Salfred 291188412Sthompsa /* read "ifnet" pointer after taskqueue drain */ 292188412Sthompsa ifp = ue->ue_ifp; 293184610Salfred 294188412Sthompsa if (ifp != NULL) { 295184610Salfred 296188412Sthompsa /* we are not running any more */ 297188412Sthompsa UE_LOCK(ue); 298188412Sthompsa ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 299188412Sthompsa UE_UNLOCK(ue); 300184610Salfred 301188412Sthompsa /* drain any callouts */ 302194228Sthompsa usb_callout_drain(&ue->ue_watchdog); 303188412Sthompsa 304188412Sthompsa /* detach miibus */ 305188412Sthompsa if (ue->ue_miibus != NULL) { 306196403Sjhb mtx_lock(&Giant); /* device_xxx() depends on this */ 307188412Sthompsa device_delete_child(ue->ue_dev, ue->ue_miibus); 308196403Sjhb mtx_unlock(&Giant); 309184610Salfred } 310184610Salfred 311188412Sthompsa /* detach ethernet */ 312188412Sthompsa ether_ifdetach(ifp); 313184610Salfred 314188412Sthompsa /* free interface instance */ 315188412Sthompsa if_free(ifp); 316188412Sthompsa 317188412Sthompsa /* free sysctl */ 318188412Sthompsa sysctl_ctx_free(&ue->ue_sysctl_ctx); 319188412Sthompsa 320188412Sthompsa /* free unit */ 321188412Sthompsa free_unr(ueunit, ue->ue_unit); 322188412Sthompsa } 323188412Sthompsa 324188412Sthompsa /* free taskqueue, if any */ 325194228Sthompsa usb_proc_free(&ue->ue_tq); 326188412Sthompsa} 327188412Sthompsa 328188412Sthompsauint8_t 329194228Sthompsauether_is_gone(struct usb_ether *ue) 330188412Sthompsa{ 331194228Sthompsa return (usb_proc_is_gone(&ue->ue_tq)); 332188412Sthompsa} 333188412Sthompsa 334229105Shselaskyvoid 335229105Shselaskyuether_init(void *arg) 336229105Shselasky{ 337229105Shselasky 338229105Shselasky ue_init(arg); 339229105Shselasky} 340229105Shselasky 341188412Sthompsastatic void 342188412Sthompsaue_init(void *arg) 343188412Sthompsa{ 344192984Sthompsa struct usb_ether *ue = arg; 345188412Sthompsa 346188412Sthompsa UE_LOCK(ue); 347188412Sthompsa ue_queue_command(ue, ue_start_task, 348188412Sthompsa &ue->ue_sync_task[0].hdr, 349188412Sthompsa &ue->ue_sync_task[1].hdr); 350188412Sthompsa UE_UNLOCK(ue); 351188412Sthompsa} 352188412Sthompsa 353188412Sthompsastatic void 354192984Sthompsaue_start_task(struct usb_proc_msg *_task) 355188412Sthompsa{ 356192984Sthompsa struct usb_ether_cfg_task *task = 357192984Sthompsa (struct usb_ether_cfg_task *)_task; 358192984Sthompsa struct usb_ether *ue = task->ue; 359188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 360188412Sthompsa 361188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 362188412Sthompsa 363188412Sthompsa ue->ue_methods->ue_init(ue); 364188412Sthompsa 365188412Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 366188412Sthompsa return; 367188412Sthompsa 368188412Sthompsa if (ue->ue_methods->ue_tick != NULL) 369194228Sthompsa usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 370188412Sthompsa} 371188412Sthompsa 372188412Sthompsastatic void 373192984Sthompsaue_stop_task(struct usb_proc_msg *_task) 374188412Sthompsa{ 375192984Sthompsa struct usb_ether_cfg_task *task = 376192984Sthompsa (struct usb_ether_cfg_task *)_task; 377192984Sthompsa struct usb_ether *ue = task->ue; 378188412Sthompsa 379188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 380188412Sthompsa 381194228Sthompsa usb_callout_stop(&ue->ue_watchdog); 382188412Sthompsa 383188412Sthompsa ue->ue_methods->ue_stop(ue); 384188412Sthompsa} 385188412Sthompsa 386229105Shselaskyvoid 387229105Shselaskyuether_start(struct ifnet *ifp) 388229105Shselasky{ 389229105Shselasky 390229105Shselasky ue_start(ifp); 391229105Shselasky} 392229105Shselasky 393188412Sthompsastatic void 394188412Sthompsaue_start(struct ifnet *ifp) 395188412Sthompsa{ 396192984Sthompsa struct usb_ether *ue = ifp->if_softc; 397188412Sthompsa 398188412Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 399188412Sthompsa return; 400188412Sthompsa 401188412Sthompsa UE_LOCK(ue); 402188412Sthompsa ue->ue_methods->ue_start(ue); 403188412Sthompsa UE_UNLOCK(ue); 404188412Sthompsa} 405188412Sthompsa 406188412Sthompsastatic void 407192984Sthompsaue_promisc_task(struct usb_proc_msg *_task) 408188412Sthompsa{ 409192984Sthompsa struct usb_ether_cfg_task *task = 410192984Sthompsa (struct usb_ether_cfg_task *)_task; 411192984Sthompsa struct usb_ether *ue = task->ue; 412188412Sthompsa 413188412Sthompsa ue->ue_methods->ue_setpromisc(ue); 414188412Sthompsa} 415188412Sthompsa 416188412Sthompsastatic void 417192984Sthompsaue_setmulti_task(struct usb_proc_msg *_task) 418188412Sthompsa{ 419192984Sthompsa struct usb_ether_cfg_task *task = 420192984Sthompsa (struct usb_ether_cfg_task *)_task; 421192984Sthompsa struct usb_ether *ue = task->ue; 422188412Sthompsa 423188412Sthompsa ue->ue_methods->ue_setmulti(ue); 424188412Sthompsa} 425188412Sthompsa 426229105Shselaskyint 427229105Shselaskyuether_ifmedia_upd(struct ifnet *ifp) 428229105Shselasky{ 429229105Shselasky 430229105Shselasky return (ue_ifmedia_upd(ifp)); 431229105Shselasky} 432229105Shselasky 433188412Sthompsastatic int 434188412Sthompsaue_ifmedia_upd(struct ifnet *ifp) 435188412Sthompsa{ 436192984Sthompsa struct usb_ether *ue = ifp->if_softc; 437188412Sthompsa 438188412Sthompsa /* Defer to process context */ 439188412Sthompsa UE_LOCK(ue); 440188412Sthompsa ue_queue_command(ue, ue_ifmedia_task, 441188412Sthompsa &ue->ue_media_task[0].hdr, 442188412Sthompsa &ue->ue_media_task[1].hdr); 443188412Sthompsa UE_UNLOCK(ue); 444188412Sthompsa 445188412Sthompsa return (0); 446188412Sthompsa} 447188412Sthompsa 448188412Sthompsastatic void 449192984Sthompsaue_ifmedia_task(struct usb_proc_msg *_task) 450188412Sthompsa{ 451192984Sthompsa struct usb_ether_cfg_task *task = 452192984Sthompsa (struct usb_ether_cfg_task *)_task; 453192984Sthompsa struct usb_ether *ue = task->ue; 454188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 455188412Sthompsa 456188412Sthompsa ue->ue_methods->ue_mii_upd(ifp); 457188412Sthompsa} 458188412Sthompsa 459188412Sthompsastatic void 460188412Sthompsaue_watchdog(void *arg) 461188412Sthompsa{ 462192984Sthompsa struct usb_ether *ue = arg; 463188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 464188412Sthompsa 465188412Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 466188412Sthompsa return; 467188412Sthompsa 468188412Sthompsa ue_queue_command(ue, ue_tick_task, 469188412Sthompsa &ue->ue_tick_task[0].hdr, 470188412Sthompsa &ue->ue_tick_task[1].hdr); 471188412Sthompsa 472194228Sthompsa usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 473188412Sthompsa} 474188412Sthompsa 475188412Sthompsastatic void 476192984Sthompsaue_tick_task(struct usb_proc_msg *_task) 477188412Sthompsa{ 478192984Sthompsa struct usb_ether_cfg_task *task = 479192984Sthompsa (struct usb_ether_cfg_task *)_task; 480192984Sthompsa struct usb_ether *ue = task->ue; 481188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 482188412Sthompsa 483188412Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 484188412Sthompsa return; 485188412Sthompsa 486188412Sthompsa ue->ue_methods->ue_tick(ue); 487188412Sthompsa} 488188412Sthompsa 489188412Sthompsaint 490194228Sthompsauether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 491188412Sthompsa{ 492192984Sthompsa struct usb_ether *ue = ifp->if_softc; 493188412Sthompsa struct ifreq *ifr = (struct ifreq *)data; 494188412Sthompsa struct mii_data *mii; 495188412Sthompsa int error = 0; 496188412Sthompsa 497188412Sthompsa switch (command) { 498188412Sthompsa case SIOCSIFFLAGS: 499188412Sthompsa UE_LOCK(ue); 500188412Sthompsa if (ifp->if_flags & IFF_UP) { 501188412Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) 502188412Sthompsa ue_queue_command(ue, ue_promisc_task, 503188412Sthompsa &ue->ue_promisc_task[0].hdr, 504188412Sthompsa &ue->ue_promisc_task[1].hdr); 505188412Sthompsa else 506188412Sthompsa ue_queue_command(ue, ue_start_task, 507188412Sthompsa &ue->ue_sync_task[0].hdr, 508188412Sthompsa &ue->ue_sync_task[1].hdr); 509188412Sthompsa } else { 510188412Sthompsa ue_queue_command(ue, ue_stop_task, 511188412Sthompsa &ue->ue_sync_task[0].hdr, 512188412Sthompsa &ue->ue_sync_task[1].hdr); 513184610Salfred } 514188412Sthompsa UE_UNLOCK(ue); 515188412Sthompsa break; 516188412Sthompsa case SIOCADDMULTI: 517188412Sthompsa case SIOCDELMULTI: 518188412Sthompsa UE_LOCK(ue); 519188412Sthompsa ue_queue_command(ue, ue_setmulti_task, 520188412Sthompsa &ue->ue_multi_task[0].hdr, 521188412Sthompsa &ue->ue_multi_task[1].hdr); 522188412Sthompsa UE_UNLOCK(ue); 523188412Sthompsa break; 524188412Sthompsa case SIOCGIFMEDIA: 525188412Sthompsa case SIOCSIFMEDIA: 526188412Sthompsa if (ue->ue_miibus != NULL) { 527188412Sthompsa mii = device_get_softc(ue->ue_miibus); 528188412Sthompsa error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 529188412Sthompsa } else 530188412Sthompsa error = ether_ioctl(ifp, command, data); 531188412Sthompsa break; 532188412Sthompsa default: 533188412Sthompsa error = ether_ioctl(ifp, command, data); 534188412Sthompsa break; 535184610Salfred } 536188412Sthompsa return (error); 537184610Salfred} 538188412Sthompsa 539188412Sthompsastatic int 540194228Sthompsauether_modevent(module_t mod, int type, void *data) 541188412Sthompsa{ 542188412Sthompsa 543188412Sthompsa switch (type) { 544188412Sthompsa case MOD_LOAD: 545188412Sthompsa ueunit = new_unrhdr(0, INT_MAX, NULL); 546188412Sthompsa break; 547188412Sthompsa case MOD_UNLOAD: 548188412Sthompsa break; 549188412Sthompsa default: 550188412Sthompsa return (EOPNOTSUPP); 551188412Sthompsa } 552188412Sthompsa return (0); 553188412Sthompsa} 554194228Sthompsastatic moduledata_t uether_mod = { 555188942Sthompsa "uether", 556194228Sthompsa uether_modevent, 557188412Sthompsa 0 558188412Sthompsa}; 559188412Sthompsa 560189528Sthompsastruct mbuf * 561194228Sthompsauether_newbuf(void) 562189528Sthompsa{ 563189528Sthompsa struct mbuf *m_new; 564189528Sthompsa 565248078Smarius m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 566189528Sthompsa if (m_new == NULL) 567189528Sthompsa return (NULL); 568189528Sthompsa m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 569189528Sthompsa 570189528Sthompsa m_adj(m_new, ETHER_ALIGN); 571189528Sthompsa return (m_new); 572189528Sthompsa} 573189528Sthompsa 574188412Sthompsaint 575194228Sthompsauether_rxmbuf(struct usb_ether *ue, struct mbuf *m, 576188412Sthompsa unsigned int len) 577188412Sthompsa{ 578188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 579188412Sthompsa 580188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 581188412Sthompsa 582188412Sthompsa /* finalize mbuf */ 583188412Sthompsa ifp->if_ipackets++; 584188412Sthompsa m->m_pkthdr.rcvif = ifp; 585188412Sthompsa m->m_pkthdr.len = m->m_len = len; 586188412Sthompsa 587188412Sthompsa /* enqueue for later when the lock can be released */ 588188412Sthompsa _IF_ENQUEUE(&ue->ue_rxq, m); 589188412Sthompsa return (0); 590188412Sthompsa} 591188412Sthompsa 592188412Sthompsaint 593194228Sthompsauether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc, 594188412Sthompsa unsigned int offset, unsigned int len) 595188412Sthompsa{ 596188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 597188412Sthompsa struct mbuf *m; 598188412Sthompsa 599188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 600188412Sthompsa 601189528Sthompsa if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN) 602188412Sthompsa return (1); 603188412Sthompsa 604194228Sthompsa m = uether_newbuf(); 605188412Sthompsa if (m == NULL) { 606213438Syongari ifp->if_iqdrops++; 607188412Sthompsa return (ENOMEM); 608188412Sthompsa } 609188412Sthompsa 610194228Sthompsa usbd_copy_out(pc, offset, mtod(m, uint8_t *), len); 611188412Sthompsa 612188412Sthompsa /* finalize mbuf */ 613188412Sthompsa ifp->if_ipackets++; 614188412Sthompsa m->m_pkthdr.rcvif = ifp; 615188412Sthompsa m->m_pkthdr.len = m->m_len = len; 616188412Sthompsa 617188412Sthompsa /* enqueue for later when the lock can be released */ 618188412Sthompsa _IF_ENQUEUE(&ue->ue_rxq, m); 619188412Sthompsa return (0); 620188412Sthompsa} 621188412Sthompsa 622188412Sthompsavoid 623194228Sthompsauether_rxflush(struct usb_ether *ue) 624188412Sthompsa{ 625188412Sthompsa struct ifnet *ifp = ue->ue_ifp; 626188412Sthompsa struct mbuf *m; 627188412Sthompsa 628188412Sthompsa UE_LOCK_ASSERT(ue, MA_OWNED); 629188412Sthompsa 630188412Sthompsa for (;;) { 631188412Sthompsa _IF_DEQUEUE(&ue->ue_rxq, m); 632188412Sthompsa if (m == NULL) 633188412Sthompsa break; 634188412Sthompsa 635188412Sthompsa /* 636188412Sthompsa * The USB xfer has been resubmitted so its safe to unlock now. 637188412Sthompsa */ 638188412Sthompsa UE_UNLOCK(ue); 639188412Sthompsa ifp->if_input(ifp, m); 640188412Sthompsa UE_LOCK(ue); 641188412Sthompsa } 642188412Sthompsa} 643188412Sthompsa 644194228SthompsaDECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 645188942SthompsaMODULE_VERSION(uether, 1); 646