usb_ethernet.c revision 226709
1184610Salfred/* $FreeBSD: head/sys/dev/usb/net/usb_ethernet.c 226709 2011-10-24 23:38:11Z yongari $ */
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: head/sys/dev/usb/net/usb_ethernet.c 226709 2011-10-24 23:38:11Z yongari $");
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
60188412SthompsaSYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0, "USB Ethernet parameters");
61184610Salfred
62188412Sthompsa#define	UE_LOCK(_ue)		mtx_lock((_ue)->ue_mtx)
63188412Sthompsa#define	UE_UNLOCK(_ue)		mtx_unlock((_ue)->ue_mtx)
64188412Sthompsa#define	UE_LOCK_ASSERT(_ue, t)	mtx_assert((_ue)->ue_mtx, t)
65188412Sthompsa
66188942SthompsaMODULE_DEPEND(uether, usb, 1, 1, 1);
67188942SthompsaMODULE_DEPEND(uether, miibus, 1, 1, 1);
68188552Sthompsa
69188412Sthompsastatic struct unrhdr *ueunit;
70188412Sthompsa
71193045Sthompsastatic usb_proc_callback_t ue_attach_post_task;
72193045Sthompsastatic usb_proc_callback_t ue_promisc_task;
73193045Sthompsastatic usb_proc_callback_t ue_setmulti_task;
74193045Sthompsastatic usb_proc_callback_t ue_ifmedia_task;
75193045Sthompsastatic usb_proc_callback_t ue_tick_task;
76193045Sthompsastatic usb_proc_callback_t ue_start_task;
77193045Sthompsastatic usb_proc_callback_t ue_stop_task;
78188412Sthompsa
79188412Sthompsastatic void	ue_init(void *);
80188412Sthompsastatic void	ue_start(struct ifnet *);
81188412Sthompsastatic int	ue_ifmedia_upd(struct ifnet *);
82188412Sthompsastatic void	ue_watchdog(void *);
83188412Sthompsa
84188412Sthompsa/*
85188412Sthompsa * Return values:
86188412Sthompsa *    0: success
87188412Sthompsa * Else: device has been detached
88188412Sthompsa */
89188412Sthompsauint8_t
90194228Sthompsauether_pause(struct usb_ether *ue, unsigned int _ticks)
91184610Salfred{
92194228Sthompsa	if (usb_proc_is_gone(&ue->ue_tq)) {
93188412Sthompsa		/* nothing to do */
94188412Sthompsa		return (1);
95188412Sthompsa	}
96194228Sthompsa	usb_pause_mtx(ue->ue_mtx, _ticks);
97188412Sthompsa	return (0);
98188412Sthompsa}
99184610Salfred
100188412Sthompsastatic void
101192984Sthompsaue_queue_command(struct usb_ether *ue,
102193045Sthompsa    usb_proc_callback_t *fn,
103192984Sthompsa    struct usb_proc_msg *t0, struct usb_proc_msg *t1)
104188412Sthompsa{
105192984Sthompsa	struct usb_ether_cfg_task *task;
106188412Sthompsa
107188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
108188412Sthompsa
109194228Sthompsa	if (usb_proc_is_gone(&ue->ue_tq)) {
110188412Sthompsa		return;         /* nothing to do */
111184610Salfred	}
112188412Sthompsa	/*
113188412Sthompsa	 * NOTE: The task cannot get executed before we drop the
114188412Sthompsa	 * "sc_mtx" mutex. It is safe to update fields in the message
115188412Sthompsa	 * structure after that the message got queued.
116188412Sthompsa	 */
117192984Sthompsa	task = (struct usb_ether_cfg_task *)
118194228Sthompsa	  usb_proc_msignal(&ue->ue_tq, t0, t1);
119188412Sthompsa
120188412Sthompsa	/* Setup callback and self pointers */
121188412Sthompsa	task->hdr.pm_callback = fn;
122188412Sthompsa	task->ue = ue;
123188412Sthompsa
124188412Sthompsa	/*
125188412Sthompsa	 * Start and stop must be synchronous!
126188412Sthompsa	 */
127188412Sthompsa	if ((fn == ue_start_task) || (fn == ue_stop_task))
128194228Sthompsa		usb_proc_mwait(&ue->ue_tq, t0, t1);
129184610Salfred}
130184610Salfred
131188412Sthompsastruct ifnet *
132194228Sthompsauether_getifp(struct usb_ether *ue)
133184610Salfred{
134188412Sthompsa	return (ue->ue_ifp);
135188412Sthompsa}
136184610Salfred
137188412Sthompsastruct mii_data *
138194228Sthompsauether_getmii(struct usb_ether *ue)
139188412Sthompsa{
140188412Sthompsa	return (device_get_softc(ue->ue_miibus));
141188412Sthompsa}
142188412Sthompsa
143188412Sthompsavoid *
144194228Sthompsauether_getsc(struct usb_ether *ue)
145188412Sthompsa{
146188412Sthompsa	return (ue->ue_sc);
147188412Sthompsa}
148188412Sthompsa
149188412Sthompsastatic int
150188412Sthompsaue_sysctl_parent(SYSCTL_HANDLER_ARGS)
151188412Sthompsa{
152192984Sthompsa	struct usb_ether *ue = arg1;
153188412Sthompsa	const char *name;
154188412Sthompsa
155188412Sthompsa	name = device_get_nameunit(ue->ue_dev);
156188412Sthompsa	return SYSCTL_OUT(req, name, strlen(name));
157188412Sthompsa}
158188412Sthompsa
159188412Sthompsaint
160194228Sthompsauether_ifattach(struct usb_ether *ue)
161188412Sthompsa{
162188412Sthompsa	int error;
163188412Sthompsa
164188412Sthompsa	/* check some critical parameters */
165188412Sthompsa	if ((ue->ue_dev == NULL) ||
166188412Sthompsa	    (ue->ue_udev == NULL) ||
167188412Sthompsa	    (ue->ue_mtx == NULL) ||
168188412Sthompsa	    (ue->ue_methods == NULL))
169188412Sthompsa		return (EINVAL);
170188412Sthompsa
171194228Sthompsa	error = usb_proc_create(&ue->ue_tq, ue->ue_mtx,
172188412Sthompsa	    device_get_nameunit(ue->ue_dev), USB_PRI_MED);
173188412Sthompsa	if (error) {
174188412Sthompsa		device_printf(ue->ue_dev, "could not setup taskqueue\n");
175188412Sthompsa		goto error;
176188412Sthompsa	}
177188412Sthompsa
178188412Sthompsa	/* fork rest of the attach code */
179188412Sthompsa	UE_LOCK(ue);
180188412Sthompsa	ue_queue_command(ue, ue_attach_post_task,
181188412Sthompsa	    &ue->ue_sync_task[0].hdr,
182188412Sthompsa	    &ue->ue_sync_task[1].hdr);
183188412Sthompsa	UE_UNLOCK(ue);
184188412Sthompsa
185188412Sthompsaerror:
186188412Sthompsa	return (error);
187188412Sthompsa}
188188412Sthompsa
189188412Sthompsastatic void
190192984Sthompsaue_attach_post_task(struct usb_proc_msg *_task)
191188412Sthompsa{
192192984Sthompsa	struct usb_ether_cfg_task *task =
193192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
194192984Sthompsa	struct usb_ether *ue = task->ue;
195188412Sthompsa	struct ifnet *ifp;
196188412Sthompsa	int error;
197188412Sthompsa	char num[14];			/* sufficient for 32 bits */
198188412Sthompsa
199188412Sthompsa	/* first call driver's post attach routine */
200188412Sthompsa	ue->ue_methods->ue_attach_post(ue);
201188412Sthompsa
202188412Sthompsa	UE_UNLOCK(ue);
203188412Sthompsa
204188412Sthompsa	ue->ue_unit = alloc_unr(ueunit);
205194228Sthompsa	usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
206188412Sthompsa	sysctl_ctx_init(&ue->ue_sysctl_ctx);
207188412Sthompsa
208226709Syongari	error = 0;
209188412Sthompsa	ifp = if_alloc(IFT_ETHER);
210184610Salfred	if (ifp == NULL) {
211188412Sthompsa		device_printf(ue->ue_dev, "could not allocate ifnet\n");
212226709Syongari		goto fail;
213184610Salfred	}
214184610Salfred
215188412Sthompsa	ifp->if_softc = ue;
216188412Sthompsa	if_initname(ifp, "ue", ue->ue_unit);
217226709Syongari	if (ue->ue_methods->ue_attach_post_sub != NULL) {
218226709Syongari		ue->ue_ifp = ifp;
219226709Syongari		error = ue->ue_methods->ue_attach_post_sub(ue);
220226709Syongari	} else {
221226709Syongari		ifp->if_mtu = ETHERMTU;
222226709Syongari		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
223226709Syongari		if (ue->ue_methods->ue_ioctl != NULL)
224226709Syongari			ifp->if_ioctl = ue->ue_methods->ue_ioctl;
225226709Syongari		else
226226709Syongari			ifp->if_ioctl = uether_ioctl;
227226709Syongari		ifp->if_start = ue_start;
228226709Syongari		ifp->if_init = ue_init;
229226709Syongari		IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
230226709Syongari		ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
231226709Syongari		IFQ_SET_READY(&ifp->if_snd);
232226709Syongari		ue->ue_ifp = ifp;
233184610Salfred
234226709Syongari		if (ue->ue_methods->ue_mii_upd != NULL &&
235226709Syongari		    ue->ue_methods->ue_mii_sts != NULL) {
236226709Syongari			/* device_xxx() depends on this */
237226709Syongari			mtx_lock(&Giant);
238226709Syongari			error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
239226709Syongari			    ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
240226709Syongari			    BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
241226709Syongari			mtx_unlock(&Giant);
242188412Sthompsa		}
243188412Sthompsa	}
244184610Salfred
245226709Syongari	if (error) {
246226709Syongari		device_printf(ue->ue_dev, "attaching PHYs failed\n");
247226709Syongari		goto fail;
248226709Syongari	}
249226709Syongari
250188412Sthompsa	if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
251188412Sthompsa	ether_ifattach(ifp, ue->ue_eaddr);
252226709Syongari	/* Tell upper layer we support VLAN oversized frames. */
253226709Syongari	if (ifp->if_capabilities & IFCAP_VLAN_MTU)
254226709Syongari		ifp->if_hdrlen = sizeof(struct ether_vlan_header);
255188412Sthompsa
256188412Sthompsa	snprintf(num, sizeof(num), "%u", ue->ue_unit);
257188412Sthompsa	ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
258188412Sthompsa	    &SYSCTL_NODE_CHILDREN(_net, ue),
259188412Sthompsa	    OID_AUTO, num, CTLFLAG_RD, NULL, "");
260188412Sthompsa	SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
261188412Sthompsa	    SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
262217556Smdf	    "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0,
263188412Sthompsa	    ue_sysctl_parent, "A", "parent device");
264188412Sthompsa
265188412Sthompsa	UE_LOCK(ue);
266188412Sthompsa	return;
267188412Sthompsa
268226709Syongarifail:
269188412Sthompsa	free_unr(ueunit, ue->ue_unit);
270188412Sthompsa	if (ue->ue_ifp != NULL) {
271188412Sthompsa		if_free(ue->ue_ifp);
272188412Sthompsa		ue->ue_ifp = NULL;
273184610Salfred	}
274188412Sthompsa	UE_LOCK(ue);
275188412Sthompsa	return;
276188412Sthompsa}
277184610Salfred
278188412Sthompsavoid
279194228Sthompsauether_ifdetach(struct usb_ether *ue)
280188412Sthompsa{
281188412Sthompsa	struct ifnet *ifp;
282184610Salfred
283188412Sthompsa	/* wait for any post attach or other command to complete */
284194228Sthompsa	usb_proc_drain(&ue->ue_tq);
285184610Salfred
286188412Sthompsa	/* read "ifnet" pointer after taskqueue drain */
287188412Sthompsa	ifp = ue->ue_ifp;
288184610Salfred
289188412Sthompsa	if (ifp != NULL) {
290184610Salfred
291188412Sthompsa		/* we are not running any more */
292188412Sthompsa		UE_LOCK(ue);
293188412Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
294188412Sthompsa		UE_UNLOCK(ue);
295184610Salfred
296188412Sthompsa		/* drain any callouts */
297194228Sthompsa		usb_callout_drain(&ue->ue_watchdog);
298188412Sthompsa
299188412Sthompsa		/* detach miibus */
300188412Sthompsa		if (ue->ue_miibus != NULL) {
301196403Sjhb			mtx_lock(&Giant);	/* device_xxx() depends on this */
302188412Sthompsa			device_delete_child(ue->ue_dev, ue->ue_miibus);
303196403Sjhb			mtx_unlock(&Giant);
304184610Salfred		}
305184610Salfred
306188412Sthompsa		/* detach ethernet */
307188412Sthompsa		ether_ifdetach(ifp);
308184610Salfred
309188412Sthompsa		/* free interface instance */
310188412Sthompsa		if_free(ifp);
311188412Sthompsa
312188412Sthompsa		/* free sysctl */
313188412Sthompsa		sysctl_ctx_free(&ue->ue_sysctl_ctx);
314188412Sthompsa
315188412Sthompsa		/* free unit */
316188412Sthompsa		free_unr(ueunit, ue->ue_unit);
317188412Sthompsa	}
318188412Sthompsa
319188412Sthompsa	/* free taskqueue, if any */
320194228Sthompsa	usb_proc_free(&ue->ue_tq);
321188412Sthompsa}
322188412Sthompsa
323188412Sthompsauint8_t
324194228Sthompsauether_is_gone(struct usb_ether *ue)
325188412Sthompsa{
326194228Sthompsa	return (usb_proc_is_gone(&ue->ue_tq));
327188412Sthompsa}
328188412Sthompsa
329226709Syongarivoid
330226709Syongariuether_init(void *arg)
331226709Syongari{
332226709Syongari
333226709Syongari	ue_init(arg);
334226709Syongari}
335226709Syongari
336188412Sthompsastatic void
337188412Sthompsaue_init(void *arg)
338188412Sthompsa{
339192984Sthompsa	struct usb_ether *ue = arg;
340188412Sthompsa
341188412Sthompsa	UE_LOCK(ue);
342188412Sthompsa	ue_queue_command(ue, ue_start_task,
343188412Sthompsa	    &ue->ue_sync_task[0].hdr,
344188412Sthompsa	    &ue->ue_sync_task[1].hdr);
345188412Sthompsa	UE_UNLOCK(ue);
346188412Sthompsa}
347188412Sthompsa
348188412Sthompsastatic void
349192984Sthompsaue_start_task(struct usb_proc_msg *_task)
350188412Sthompsa{
351192984Sthompsa	struct usb_ether_cfg_task *task =
352192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
353192984Sthompsa	struct usb_ether *ue = task->ue;
354188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
355188412Sthompsa
356188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
357188412Sthompsa
358188412Sthompsa	ue->ue_methods->ue_init(ue);
359188412Sthompsa
360188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
361188412Sthompsa		return;
362188412Sthompsa
363188412Sthompsa	if (ue->ue_methods->ue_tick != NULL)
364194228Sthompsa		usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
365188412Sthompsa}
366188412Sthompsa
367188412Sthompsastatic void
368192984Sthompsaue_stop_task(struct usb_proc_msg *_task)
369188412Sthompsa{
370192984Sthompsa	struct usb_ether_cfg_task *task =
371192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
372192984Sthompsa	struct usb_ether *ue = task->ue;
373188412Sthompsa
374188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
375188412Sthompsa
376194228Sthompsa	usb_callout_stop(&ue->ue_watchdog);
377188412Sthompsa
378188412Sthompsa	ue->ue_methods->ue_stop(ue);
379188412Sthompsa}
380188412Sthompsa
381226709Syongarivoid
382226709Syongariuether_start(struct ifnet *ifp)
383226709Syongari{
384226709Syongari
385226709Syongari	ue_start(ifp);
386226709Syongari}
387226709Syongari
388188412Sthompsastatic void
389188412Sthompsaue_start(struct ifnet *ifp)
390188412Sthompsa{
391192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
392188412Sthompsa
393188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
394188412Sthompsa		return;
395188412Sthompsa
396188412Sthompsa	UE_LOCK(ue);
397188412Sthompsa	ue->ue_methods->ue_start(ue);
398188412Sthompsa	UE_UNLOCK(ue);
399188412Sthompsa}
400188412Sthompsa
401188412Sthompsastatic void
402192984Sthompsaue_promisc_task(struct usb_proc_msg *_task)
403188412Sthompsa{
404192984Sthompsa	struct usb_ether_cfg_task *task =
405192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
406192984Sthompsa	struct usb_ether *ue = task->ue;
407188412Sthompsa
408188412Sthompsa	ue->ue_methods->ue_setpromisc(ue);
409188412Sthompsa}
410188412Sthompsa
411188412Sthompsastatic void
412192984Sthompsaue_setmulti_task(struct usb_proc_msg *_task)
413188412Sthompsa{
414192984Sthompsa	struct usb_ether_cfg_task *task =
415192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
416192984Sthompsa	struct usb_ether *ue = task->ue;
417188412Sthompsa
418188412Sthompsa	ue->ue_methods->ue_setmulti(ue);
419188412Sthompsa}
420188412Sthompsa
421226709Syongariint
422226709Syongariuether_ifmedia_upd(struct ifnet *ifp)
423226709Syongari{
424226709Syongari
425226709Syongari	return (ue_ifmedia_upd(ifp));
426226709Syongari}
427226709Syongari
428188412Sthompsastatic int
429188412Sthompsaue_ifmedia_upd(struct ifnet *ifp)
430188412Sthompsa{
431192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
432188412Sthompsa
433188412Sthompsa	/* Defer to process context */
434188412Sthompsa	UE_LOCK(ue);
435188412Sthompsa	ue_queue_command(ue, ue_ifmedia_task,
436188412Sthompsa	    &ue->ue_media_task[0].hdr,
437188412Sthompsa	    &ue->ue_media_task[1].hdr);
438188412Sthompsa	UE_UNLOCK(ue);
439188412Sthompsa
440188412Sthompsa	return (0);
441188412Sthompsa}
442188412Sthompsa
443188412Sthompsastatic void
444192984Sthompsaue_ifmedia_task(struct usb_proc_msg *_task)
445188412Sthompsa{
446192984Sthompsa	struct usb_ether_cfg_task *task =
447192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
448192984Sthompsa	struct usb_ether *ue = task->ue;
449188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
450188412Sthompsa
451188412Sthompsa	ue->ue_methods->ue_mii_upd(ifp);
452188412Sthompsa}
453188412Sthompsa
454188412Sthompsastatic void
455188412Sthompsaue_watchdog(void *arg)
456188412Sthompsa{
457192984Sthompsa	struct usb_ether *ue = arg;
458188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
459188412Sthompsa
460188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
461188412Sthompsa		return;
462188412Sthompsa
463188412Sthompsa	ue_queue_command(ue, ue_tick_task,
464188412Sthompsa	    &ue->ue_tick_task[0].hdr,
465188412Sthompsa	    &ue->ue_tick_task[1].hdr);
466188412Sthompsa
467194228Sthompsa	usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
468188412Sthompsa}
469188412Sthompsa
470188412Sthompsastatic void
471192984Sthompsaue_tick_task(struct usb_proc_msg *_task)
472188412Sthompsa{
473192984Sthompsa	struct usb_ether_cfg_task *task =
474192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
475192984Sthompsa	struct usb_ether *ue = task->ue;
476188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
477188412Sthompsa
478188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
479188412Sthompsa		return;
480188412Sthompsa
481188412Sthompsa	ue->ue_methods->ue_tick(ue);
482188412Sthompsa}
483188412Sthompsa
484188412Sthompsaint
485194228Sthompsauether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
486188412Sthompsa{
487192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
488188412Sthompsa	struct ifreq *ifr = (struct ifreq *)data;
489188412Sthompsa	struct mii_data *mii;
490188412Sthompsa	int error = 0;
491188412Sthompsa
492188412Sthompsa	switch (command) {
493188412Sthompsa	case SIOCSIFFLAGS:
494188412Sthompsa		UE_LOCK(ue);
495188412Sthompsa		if (ifp->if_flags & IFF_UP) {
496188412Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
497188412Sthompsa				ue_queue_command(ue, ue_promisc_task,
498188412Sthompsa				    &ue->ue_promisc_task[0].hdr,
499188412Sthompsa				    &ue->ue_promisc_task[1].hdr);
500188412Sthompsa			else
501188412Sthompsa				ue_queue_command(ue, ue_start_task,
502188412Sthompsa				    &ue->ue_sync_task[0].hdr,
503188412Sthompsa				    &ue->ue_sync_task[1].hdr);
504188412Sthompsa		} else {
505188412Sthompsa			ue_queue_command(ue, ue_stop_task,
506188412Sthompsa			    &ue->ue_sync_task[0].hdr,
507188412Sthompsa			    &ue->ue_sync_task[1].hdr);
508184610Salfred		}
509188412Sthompsa		UE_UNLOCK(ue);
510188412Sthompsa		break;
511188412Sthompsa	case SIOCADDMULTI:
512188412Sthompsa	case SIOCDELMULTI:
513188412Sthompsa		UE_LOCK(ue);
514188412Sthompsa		ue_queue_command(ue, ue_setmulti_task,
515188412Sthompsa		    &ue->ue_multi_task[0].hdr,
516188412Sthompsa		    &ue->ue_multi_task[1].hdr);
517188412Sthompsa		UE_UNLOCK(ue);
518188412Sthompsa		break;
519188412Sthompsa	case SIOCGIFMEDIA:
520188412Sthompsa	case SIOCSIFMEDIA:
521188412Sthompsa		if (ue->ue_miibus != NULL) {
522188412Sthompsa			mii = device_get_softc(ue->ue_miibus);
523188412Sthompsa			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
524188412Sthompsa		} else
525188412Sthompsa			error = ether_ioctl(ifp, command, data);
526188412Sthompsa		break;
527188412Sthompsa	default:
528188412Sthompsa		error = ether_ioctl(ifp, command, data);
529188412Sthompsa		break;
530184610Salfred	}
531188412Sthompsa	return (error);
532184610Salfred}
533188412Sthompsa
534188412Sthompsastatic int
535194228Sthompsauether_modevent(module_t mod, int type, void *data)
536188412Sthompsa{
537188412Sthompsa
538188412Sthompsa	switch (type) {
539188412Sthompsa	case MOD_LOAD:
540188412Sthompsa		ueunit = new_unrhdr(0, INT_MAX, NULL);
541188412Sthompsa		break;
542188412Sthompsa	case MOD_UNLOAD:
543188412Sthompsa		break;
544188412Sthompsa	default:
545188412Sthompsa		return (EOPNOTSUPP);
546188412Sthompsa	}
547188412Sthompsa	return (0);
548188412Sthompsa}
549194228Sthompsastatic moduledata_t uether_mod = {
550188942Sthompsa	"uether",
551194228Sthompsa	uether_modevent,
552188412Sthompsa	0
553188412Sthompsa};
554188412Sthompsa
555189528Sthompsastruct mbuf *
556194228Sthompsauether_newbuf(void)
557189528Sthompsa{
558189528Sthompsa	struct mbuf *m_new;
559189528Sthompsa
560189528Sthompsa	m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
561189528Sthompsa	if (m_new == NULL)
562189528Sthompsa		return (NULL);
563189528Sthompsa	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
564189528Sthompsa
565189528Sthompsa	m_adj(m_new, ETHER_ALIGN);
566189528Sthompsa	return (m_new);
567189528Sthompsa}
568189528Sthompsa
569188412Sthompsaint
570194228Sthompsauether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
571188412Sthompsa    unsigned int len)
572188412Sthompsa{
573188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
574188412Sthompsa
575188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
576188412Sthompsa
577188412Sthompsa	/* finalize mbuf */
578188412Sthompsa	ifp->if_ipackets++;
579188412Sthompsa	m->m_pkthdr.rcvif = ifp;
580188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
581188412Sthompsa
582188412Sthompsa	/* enqueue for later when the lock can be released */
583188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
584188412Sthompsa	return (0);
585188412Sthompsa}
586188412Sthompsa
587188412Sthompsaint
588194228Sthompsauether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
589188412Sthompsa    unsigned int offset, unsigned int len)
590188412Sthompsa{
591188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
592188412Sthompsa	struct mbuf *m;
593188412Sthompsa
594188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
595188412Sthompsa
596189528Sthompsa	if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
597188412Sthompsa		return (1);
598188412Sthompsa
599194228Sthompsa	m = uether_newbuf();
600188412Sthompsa	if (m == NULL) {
601213438Syongari		ifp->if_iqdrops++;
602188412Sthompsa		return (ENOMEM);
603188412Sthompsa	}
604188412Sthompsa
605194228Sthompsa	usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
606188412Sthompsa
607188412Sthompsa	/* finalize mbuf */
608188412Sthompsa	ifp->if_ipackets++;
609188412Sthompsa	m->m_pkthdr.rcvif = ifp;
610188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
611188412Sthompsa
612188412Sthompsa	/* enqueue for later when the lock can be released */
613188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
614188412Sthompsa	return (0);
615188412Sthompsa}
616188412Sthompsa
617188412Sthompsavoid
618194228Sthompsauether_rxflush(struct usb_ether *ue)
619188412Sthompsa{
620188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
621188412Sthompsa	struct mbuf *m;
622188412Sthompsa
623188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
624188412Sthompsa
625188412Sthompsa	for (;;) {
626188412Sthompsa		_IF_DEQUEUE(&ue->ue_rxq, m);
627188412Sthompsa		if (m == NULL)
628188412Sthompsa			break;
629188412Sthompsa
630188412Sthompsa		/*
631188412Sthompsa		 * The USB xfer has been resubmitted so its safe to unlock now.
632188412Sthompsa		 */
633188412Sthompsa		UE_UNLOCK(ue);
634188412Sthompsa		ifp->if_input(ifp, m);
635188412Sthompsa		UE_LOCK(ue);
636188412Sthompsa	}
637188412Sthompsa}
638188412Sthompsa
639194228SthompsaDECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
640188942SthompsaMODULE_VERSION(uether, 1);
641