3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2009 Andrew Thompson (thompsa@FreeBSD.org)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/condvar.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/sysctl.h>
48 #include <net/if_var.h>
49 #include <net/ethernet.h>
50 #include <net/if_types.h>
51 #include <net/if_media.h>
52 #include <net/if_vlan_var.h>
54 #include <dev/mii/mii.h>
55 #include <dev/mii/miivar.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usb_process.h>
61 #include <dev/usb/net/usb_ethernet.h>
63 static SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0,
64 "USB Ethernet parameters");
66 #define UE_LOCK(_ue) mtx_lock((_ue)->ue_mtx)
67 #define UE_UNLOCK(_ue) mtx_unlock((_ue)->ue_mtx)
68 #define UE_LOCK_ASSERT(_ue, t) mtx_assert((_ue)->ue_mtx, t)
70 MODULE_DEPEND(uether, usb, 1, 1, 1);
71 MODULE_DEPEND(uether, miibus, 1, 1, 1);
73 static struct unrhdr *ueunit;
75 static usb_proc_callback_t ue_attach_post_task;
76 static usb_proc_callback_t ue_promisc_task;
77 static usb_proc_callback_t ue_setmulti_task;
78 static usb_proc_callback_t ue_ifmedia_task;
79 static usb_proc_callback_t ue_tick_task;
80 static usb_proc_callback_t ue_start_task;
81 static usb_proc_callback_t ue_stop_task;
83 static void ue_init(void *);
84 static void ue_start(struct ifnet *);
85 static int ue_ifmedia_upd(struct ifnet *);
86 static void ue_watchdog(void *);
91 * Else: device has been detached
94 uether_pause(struct usb_ether *ue, unsigned int _ticks)
96 if (usb_proc_is_gone(&ue->ue_tq)) {
100 usb_pause_mtx(ue->ue_mtx, _ticks);
105 ue_queue_command(struct usb_ether *ue,
106 usb_proc_callback_t *fn,
107 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
109 struct usb_ether_cfg_task *task;
111 UE_LOCK_ASSERT(ue, MA_OWNED);
113 if (usb_proc_is_gone(&ue->ue_tq)) {
114 return; /* nothing to do */
117 * NOTE: The task cannot get executed before we drop the
118 * "sc_mtx" mutex. It is safe to update fields in the message
119 * structure after that the message got queued.
121 task = (struct usb_ether_cfg_task *)
122 usb_proc_msignal(&ue->ue_tq, t0, t1);
124 /* Setup callback and self pointers */
125 task->hdr.pm_callback = fn;
129 * Start and stop must be synchronous!
131 if ((fn == ue_start_task) || (fn == ue_stop_task))
132 usb_proc_mwait(&ue->ue_tq, t0, t1);
136 uether_getifp(struct usb_ether *ue)
142 uether_getmii(struct usb_ether *ue)
144 return (device_get_softc(ue->ue_miibus));
148 uether_getsc(struct usb_ether *ue)
154 ue_sysctl_parent(SYSCTL_HANDLER_ARGS)
156 struct usb_ether *ue = arg1;
159 name = device_get_nameunit(ue->ue_dev);
160 return SYSCTL_OUT_STR(req, name);
164 uether_ifattach(struct usb_ether *ue)
168 /* check some critical parameters */
169 if ((ue->ue_dev == NULL) ||
170 (ue->ue_udev == NULL) ||
171 (ue->ue_mtx == NULL) ||
172 (ue->ue_methods == NULL))
175 error = usb_proc_create(&ue->ue_tq, ue->ue_mtx,
176 device_get_nameunit(ue->ue_dev), USB_PRI_MED);
178 device_printf(ue->ue_dev, "could not setup taskqueue\n");
182 /* fork rest of the attach code */
184 ue_queue_command(ue, ue_attach_post_task,
185 &ue->ue_sync_task[0].hdr,
186 &ue->ue_sync_task[1].hdr);
194 uether_ifattach_wait(struct usb_ether *ue)
198 usb_proc_mwait(&ue->ue_tq,
199 &ue->ue_sync_task[0].hdr,
200 &ue->ue_sync_task[1].hdr);
205 ue_attach_post_task(struct usb_proc_msg *_task)
207 struct usb_ether_cfg_task *task =
208 (struct usb_ether_cfg_task *)_task;
209 struct usb_ether *ue = task->ue;
212 char num[14]; /* sufficient for 32 bits */
214 /* first call driver's post attach routine */
215 ue->ue_methods->ue_attach_post(ue);
219 ue->ue_unit = alloc_unr(ueunit);
220 usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
221 sysctl_ctx_init(&ue->ue_sysctl_ctx);
222 mbufq_init(&ue->ue_rxq, 0 /* unlimited length */);
225 CURVNET_SET_QUIET(vnet0);
226 ifp = if_alloc(IFT_ETHER);
228 device_printf(ue->ue_dev, "could not allocate ifnet\n");
233 if_initname(ifp, "ue", ue->ue_unit);
234 if (ue->ue_methods->ue_attach_post_sub != NULL) {
236 error = ue->ue_methods->ue_attach_post_sub(ue);
238 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
239 if (ue->ue_methods->ue_ioctl != NULL)
240 ifp->if_ioctl = ue->ue_methods->ue_ioctl;
242 ifp->if_ioctl = uether_ioctl;
243 ifp->if_start = ue_start;
244 ifp->if_init = ue_init;
245 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
246 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
247 IFQ_SET_READY(&ifp->if_snd);
250 if (ue->ue_methods->ue_mii_upd != NULL &&
251 ue->ue_methods->ue_mii_sts != NULL) {
252 /* device_xxx() depends on this */
254 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
255 ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
256 BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
262 device_printf(ue->ue_dev, "attaching PHYs failed\n");
266 if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
267 ether_ifattach(ifp, ue->ue_eaddr);
268 /* Tell upper layer we support VLAN oversized frames. */
269 if (ifp->if_capabilities & IFCAP_VLAN_MTU)
270 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
274 snprintf(num, sizeof(num), "%u", ue->ue_unit);
275 ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
276 &SYSCTL_NODE_CHILDREN(_net, ue),
277 OID_AUTO, num, CTLFLAG_RD, NULL, "");
278 SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
279 SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
280 "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0,
281 ue_sysctl_parent, "A", "parent device");
289 /* drain mbuf queue */
290 mbufq_drain(&ue->ue_rxq);
293 free_unr(ueunit, ue->ue_unit);
294 if (ue->ue_ifp != NULL) {
303 uether_ifdetach(struct usb_ether *ue)
307 /* wait for any post attach or other command to complete */
308 usb_proc_drain(&ue->ue_tq);
310 /* read "ifnet" pointer after taskqueue drain */
315 /* we are not running any more */
317 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
320 /* drain any callouts */
321 usb_callout_drain(&ue->ue_watchdog);
324 if (ue->ue_miibus != NULL) {
325 mtx_lock(&Giant); /* device_xxx() depends on this */
326 device_delete_child(ue->ue_dev, ue->ue_miibus);
330 /* detach ethernet */
333 /* free interface instance */
337 sysctl_ctx_free(&ue->ue_sysctl_ctx);
339 /* drain mbuf queue */
340 mbufq_drain(&ue->ue_rxq);
343 free_unr(ueunit, ue->ue_unit);
346 /* free taskqueue, if any */
347 usb_proc_free(&ue->ue_tq);
351 uether_is_gone(struct usb_ether *ue)
353 return (usb_proc_is_gone(&ue->ue_tq));
357 uether_init(void *arg)
366 struct usb_ether *ue = arg;
369 ue_queue_command(ue, ue_start_task,
370 &ue->ue_sync_task[0].hdr,
371 &ue->ue_sync_task[1].hdr);
376 ue_start_task(struct usb_proc_msg *_task)
378 struct usb_ether_cfg_task *task =
379 (struct usb_ether_cfg_task *)_task;
380 struct usb_ether *ue = task->ue;
381 struct ifnet *ifp = ue->ue_ifp;
383 UE_LOCK_ASSERT(ue, MA_OWNED);
385 ue->ue_methods->ue_init(ue);
387 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
390 if (ue->ue_methods->ue_tick != NULL)
391 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
395 ue_stop_task(struct usb_proc_msg *_task)
397 struct usb_ether_cfg_task *task =
398 (struct usb_ether_cfg_task *)_task;
399 struct usb_ether *ue = task->ue;
401 UE_LOCK_ASSERT(ue, MA_OWNED);
403 usb_callout_stop(&ue->ue_watchdog);
405 ue->ue_methods->ue_stop(ue);
409 uether_start(struct ifnet *ifp)
416 ue_start(struct ifnet *ifp)
418 struct usb_ether *ue = ifp->if_softc;
420 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
424 ue->ue_methods->ue_start(ue);
429 ue_promisc_task(struct usb_proc_msg *_task)
431 struct usb_ether_cfg_task *task =
432 (struct usb_ether_cfg_task *)_task;
433 struct usb_ether *ue = task->ue;
435 ue->ue_methods->ue_setpromisc(ue);
439 ue_setmulti_task(struct usb_proc_msg *_task)
441 struct usb_ether_cfg_task *task =
442 (struct usb_ether_cfg_task *)_task;
443 struct usb_ether *ue = task->ue;
445 ue->ue_methods->ue_setmulti(ue);
449 uether_ifmedia_upd(struct ifnet *ifp)
452 return (ue_ifmedia_upd(ifp));
456 ue_ifmedia_upd(struct ifnet *ifp)
458 struct usb_ether *ue = ifp->if_softc;
460 /* Defer to process context */
462 ue_queue_command(ue, ue_ifmedia_task,
463 &ue->ue_media_task[0].hdr,
464 &ue->ue_media_task[1].hdr);
471 ue_ifmedia_task(struct usb_proc_msg *_task)
473 struct usb_ether_cfg_task *task =
474 (struct usb_ether_cfg_task *)_task;
475 struct usb_ether *ue = task->ue;
476 struct ifnet *ifp = ue->ue_ifp;
478 ue->ue_methods->ue_mii_upd(ifp);
482 ue_watchdog(void *arg)
484 struct usb_ether *ue = arg;
485 struct ifnet *ifp = ue->ue_ifp;
487 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
490 ue_queue_command(ue, ue_tick_task,
491 &ue->ue_tick_task[0].hdr,
492 &ue->ue_tick_task[1].hdr);
494 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
498 ue_tick_task(struct usb_proc_msg *_task)
500 struct usb_ether_cfg_task *task =
501 (struct usb_ether_cfg_task *)_task;
502 struct usb_ether *ue = task->ue;
503 struct ifnet *ifp = ue->ue_ifp;
505 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
508 ue->ue_methods->ue_tick(ue);
512 uether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
514 struct usb_ether *ue = ifp->if_softc;
515 struct ifreq *ifr = (struct ifreq *)data;
516 struct mii_data *mii;
522 if (ifp->if_flags & IFF_UP) {
523 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
524 ue_queue_command(ue, ue_promisc_task,
525 &ue->ue_promisc_task[0].hdr,
526 &ue->ue_promisc_task[1].hdr);
528 ue_queue_command(ue, ue_start_task,
529 &ue->ue_sync_task[0].hdr,
530 &ue->ue_sync_task[1].hdr);
532 ue_queue_command(ue, ue_stop_task,
533 &ue->ue_sync_task[0].hdr,
534 &ue->ue_sync_task[1].hdr);
541 ue_queue_command(ue, ue_setmulti_task,
542 &ue->ue_multi_task[0].hdr,
543 &ue->ue_multi_task[1].hdr);
548 if (ue->ue_miibus != NULL) {
549 mii = device_get_softc(ue->ue_miibus);
550 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
552 error = ether_ioctl(ifp, command, data);
555 error = ether_ioctl(ifp, command, data);
562 uether_modevent(module_t mod, int type, void *data)
567 ueunit = new_unrhdr(0, INT_MAX, NULL);
576 static moduledata_t uether_mod = {
587 m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
590 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
592 m_adj(m_new, ETHER_ALIGN);
597 uether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
600 struct ifnet *ifp = ue->ue_ifp;
602 UE_LOCK_ASSERT(ue, MA_OWNED);
605 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
606 m->m_pkthdr.rcvif = ifp;
607 m->m_pkthdr.len = m->m_len = len;
609 /* enqueue for later when the lock can be released */
610 (void)mbufq_enqueue(&ue->ue_rxq, m);
615 uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
616 unsigned int offset, unsigned int len)
618 struct ifnet *ifp = ue->ue_ifp;
621 UE_LOCK_ASSERT(ue, MA_OWNED);
623 if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
628 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
632 usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
635 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
636 m->m_pkthdr.rcvif = ifp;
637 m->m_pkthdr.len = m->m_len = len;
639 /* enqueue for later when the lock can be released */
640 (void)mbufq_enqueue(&ue->ue_rxq, m);
645 uether_rxflush(struct usb_ether *ue)
647 struct ifnet *ifp = ue->ue_ifp;
648 struct epoch_tracker et;
651 UE_LOCK_ASSERT(ue, MA_OWNED);
653 n = mbufq_flush(&ue->ue_rxq);
656 while ((m = n) != NULL) {
657 n = STAILQ_NEXT(m, m_stailqpkt);
659 ifp->if_input(ifp, m);
666 * USB net drivers are run by DRIVER_MODULE() thus SI_SUB_DRIVERS,
667 * SI_ORDER_MIDDLE. Run uether after that.
669 DECLARE_MODULE(uether, uether_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
670 MODULE_VERSION(uether, 1);