3 * Copyright (c) 2009 Andrew Thompson (thompsa@FreeBSD.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/param.h>
28 #include <sys/systm.h>
30 #include <sys/condvar.h>
31 #include <sys/kernel.h>
33 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/sysctl.h>
41 #include <net/ifq_var.h>
42 #include <net/ethernet.h>
43 #include <net/if_types.h>
44 #include <net/if_media.h>
45 #include <net/vlan/if_vlan_var.h>
47 #include <sys/devfs.h>
49 #include <dev/netif/mii_layer/mii.h>
50 #include <dev/netif/mii_layer/miivar.h>
52 #include <bus/u4b/usb.h>
53 #include <bus/u4b/usbdi.h>
55 #include <bus/u4b/usb_process.h>
56 #include <bus/u4b/net/usb_ethernet.h>
58 static SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0,
59 "USB Ethernet parameters");
61 #define UE_LOCK(_ue) lockmgr((_ue)->ue_lock, LK_EXCLUSIVE)
62 #define UE_UNLOCK(_ue) lockmgr((_ue)->ue_lock, LK_RELEASE)
63 #define UE_LOCK_ASSERT(_ue) KKASSERT(lockowned((_ue)->ue_lock))
65 MODULE_DEPEND(uether, usb, 1, 1, 1);
66 MODULE_DEPEND(uether, miibus, 1, 1, 1);
69 static struct unrhdr *ueunit;
71 DEVFS_DECLARE_CLONE_BITMAP(ue);
73 static usb_proc_callback_t ue_attach_post_task;
74 static usb_proc_callback_t ue_promisc_task;
75 static usb_proc_callback_t ue_setmulti_task;
76 static usb_proc_callback_t ue_ifmedia_task;
77 static usb_proc_callback_t ue_tick_task;
78 static usb_proc_callback_t ue_start_task;
79 static usb_proc_callback_t ue_stop_task;
81 static void ue_init(void *);
82 static void ue_start(struct ifnet *, struct ifaltq_subque *);
83 static int ue_ifmedia_upd(struct ifnet *);
84 static void ue_watchdog(void *);
89 * Else: device has been detached
92 uether_pause(struct usb_ether *ue, unsigned int _ticks)
94 if (usb_proc_is_gone(&ue->ue_tq)) {
98 usb_pause_mtx(ue->ue_lock, _ticks);
103 ue_queue_command(struct usb_ether *ue,
104 usb_proc_callback_t *fn,
105 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
107 struct usb_ether_cfg_task *task;
111 if (usb_proc_is_gone(&ue->ue_tq)) {
112 return; /* nothing to do */
115 * NOTE: The task cannot get executed before we drop the
116 * "sc_mtx" mutex. It is safe to update fields in the message
117 * structure after that the message got queued.
119 task = (struct usb_ether_cfg_task *)
120 usb_proc_msignal(&ue->ue_tq, t0, t1);
122 /* Setup callback and self pointers */
123 task->hdr.pm_callback = fn;
127 * Start and stop must be synchronous!
129 if ((fn == ue_start_task) || (fn == ue_stop_task))
130 usb_proc_mwait(&ue->ue_tq, t0, t1);
134 uether_getifp(struct usb_ether *ue)
140 uether_getmii(struct usb_ether *ue)
142 return (device_get_softc(ue->ue_miibus));
146 uether_getsc(struct usb_ether *ue)
152 ue_sysctl_parent(SYSCTL_HANDLER_ARGS)
154 struct usb_ether *ue = arg1;
157 name = device_get_nameunit(ue->ue_dev);
158 return SYSCTL_OUT(req, name, strlen(name));
162 uether_ifattach(struct usb_ether *ue)
166 /* check some critical parameters */
167 if ((ue->ue_dev == NULL) ||
168 (ue->ue_udev == NULL) ||
169 (ue->ue_lock == NULL) ||
170 (ue->ue_methods == NULL))
173 error = usb_proc_create(&ue->ue_tq, ue->ue_lock,
174 device_get_nameunit(ue->ue_dev), USB_PRI_MED);
176 device_printf(ue->ue_dev, "could not setup taskqueue\n");
180 /* fork rest of the attach code */
182 ue_queue_command(ue, ue_attach_post_task,
183 &ue->ue_sync_task[0].hdr,
184 &ue->ue_sync_task[1].hdr);
192 ue_attach_post_task(struct usb_proc_msg *_task)
194 struct usb_ether_cfg_task *task =
195 (struct usb_ether_cfg_task *)_task;
196 struct usb_ether *ue = task->ue;
199 char num[14]; /* sufficient for 32 bits */
201 /* first call driver's post attach routine */
202 ue->ue_methods->ue_attach_post(ue);
206 KKASSERT(!lockowned(ue->ue_lock));
208 ue->ue_unit = alloc_unr(ueunit);
210 ue->ue_unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ue), 0);
211 usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_lock, 0);
212 sysctl_ctx_init(&ue->ue_sysctl_ctx);
214 KKASSERT(!lockowned(ue->ue_lock));
216 ifp = if_alloc(IFT_ETHER);
218 device_printf(ue->ue_dev, "could not allocate ifnet\n");
222 KKASSERT(!lockowned(ue->ue_lock));
224 if_initname(ifp, "ue", ue->ue_unit);
225 if (ue->ue_methods->ue_attach_post_sub != NULL) {
227 error = ue->ue_methods->ue_attach_post_sub(ue);
228 KKASSERT(!lockowned(ue->ue_lock));
230 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
231 if (ue->ue_methods->ue_ioctl != NULL)
232 ifp->if_ioctl = ue->ue_methods->ue_ioctl;
234 ifp->if_ioctl = uether_ioctl;
235 ifp->if_start = ue_start;
236 ifp->if_init = ue_init;
237 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
239 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
241 ifq_set_ready(&ifp->if_snd);
244 if (ue->ue_methods->ue_mii_upd != NULL &&
245 ue->ue_methods->ue_mii_sts != NULL) {
246 /* device_xxx() depends on this */
247 /* mtx_lock(&Giant);*/
248 error = mii_phy_probe(ue->ue_dev, &ue->ue_miibus,
249 ue_ifmedia_upd, ue->ue_methods->ue_mii_sts);
251 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
252 ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
253 BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
254 */ /* mtx_unlock(&Giant);*/
259 device_printf(ue->ue_dev, "attaching PHYs failed\n");
263 if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
264 ether_ifattach(ifp, ue->ue_eaddr, NULL);
265 /* Tell upper layer we support VLAN oversized frames. */
266 if (ifp->if_capabilities & IFCAP_VLAN_MTU)
267 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
269 ksnprintf(num, sizeof(num), "%u", ue->ue_unit);
270 ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
271 &SYSCTL_NODE_CHILDREN(_net, ue),
272 OID_AUTO, num, CTLFLAG_RD, NULL, "");
273 SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
274 SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
275 "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0,
276 ue_sysctl_parent, "A", "parent device");
278 KKASSERT(!lockowned(ue->ue_lock));
284 free_unr(ueunit, ue->ue_unit);
286 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ue), ue->ue_unit);
287 if (ue->ue_ifp != NULL) {
296 uether_ifdetach(struct usb_ether *ue)
300 /* wait for any post attach or other command to complete */
301 usb_proc_drain(&ue->ue_tq);
303 /* read "ifnet" pointer after taskqueue drain */
308 /* we are not running any more */
310 ifp->if_flags &= ~IFF_RUNNING;
313 /* drain any callouts */
314 usb_callout_drain(&ue->ue_watchdog);
317 if (ue->ue_miibus != NULL) {
318 /*mtx_lock(&Giant);*/ /* device_xxx() depends on this */
319 device_delete_child(ue->ue_dev, ue->ue_miibus);
320 /*mtx_unlock(&Giant);*/
323 /* detach ethernet */
326 /* free interface instance */
330 sysctl_ctx_free(&ue->ue_sysctl_ctx);
333 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ue), ue->ue_unit);
335 free_unr(ueunit, ue->ue_unit);
339 /* free taskqueue, if any */
340 usb_proc_free(&ue->ue_tq);
344 uether_is_gone(struct usb_ether *ue)
346 return (usb_proc_is_gone(&ue->ue_tq));
350 uether_init(void *arg)
358 struct usb_ether *ue = arg;
361 ue_queue_command(ue, ue_start_task,
362 &ue->ue_sync_task[0].hdr,
363 &ue->ue_sync_task[1].hdr);
368 ue_start_task(struct usb_proc_msg *_task)
370 struct usb_ether_cfg_task *task =
371 (struct usb_ether_cfg_task *)_task;
372 struct usb_ether *ue = task->ue;
373 struct ifnet *ifp = ue->ue_ifp;
377 ue->ue_methods->ue_init(ue);
379 if ((ifp->if_flags & IFF_RUNNING) == 0)
381 if (ue->ue_methods->ue_tick != NULL)
382 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
386 ue_stop_task(struct usb_proc_msg *_task)
388 struct usb_ether_cfg_task *task =
389 (struct usb_ether_cfg_task *)_task;
390 struct usb_ether *ue = task->ue;
394 usb_callout_stop(&ue->ue_watchdog);
396 ue->ue_methods->ue_stop(ue);
400 uether_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
407 ue_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
409 struct usb_ether *ue = ifp->if_softc;
411 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
413 if ((ifp->if_flags & IFF_RUNNING) == 0)
416 ue->ue_methods->ue_start(ue);
421 ue_promisc_task(struct usb_proc_msg *_task)
423 struct usb_ether_cfg_task *task =
424 (struct usb_ether_cfg_task *)_task;
425 struct usb_ether *ue = task->ue;
427 ue->ue_methods->ue_setpromisc(ue);
431 ue_setmulti_task(struct usb_proc_msg *_task)
433 struct usb_ether_cfg_task *task =
434 (struct usb_ether_cfg_task *)_task;
435 struct usb_ether *ue = task->ue;
437 ue->ue_methods->ue_setmulti(ue);
441 uether_ifmedia_upd(struct ifnet *ifp)
444 return (ue_ifmedia_upd(ifp));
448 ue_ifmedia_upd(struct ifnet *ifp)
450 struct usb_ether *ue = ifp->if_softc;
452 /* Defer to process context */
454 ue_queue_command(ue, ue_ifmedia_task,
455 &ue->ue_media_task[0].hdr,
456 &ue->ue_media_task[1].hdr);
463 ue_ifmedia_task(struct usb_proc_msg *_task)
465 struct usb_ether_cfg_task *task =
466 (struct usb_ether_cfg_task *)_task;
467 struct usb_ether *ue = task->ue;
468 struct ifnet *ifp = ue->ue_ifp;
470 ue->ue_methods->ue_mii_upd(ifp);
474 ue_watchdog(void *arg)
476 struct usb_ether *ue = arg;
477 struct ifnet *ifp = ue->ue_ifp;
479 if ((ifp->if_flags & IFF_RUNNING) == 0)
482 ue_queue_command(ue, ue_tick_task,
483 &ue->ue_tick_task[0].hdr,
484 &ue->ue_tick_task[1].hdr);
486 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
490 ue_tick_task(struct usb_proc_msg *_task)
492 struct usb_ether_cfg_task *task =
493 (struct usb_ether_cfg_task *)_task;
494 struct usb_ether *ue = task->ue;
495 struct ifnet *ifp = ue->ue_ifp;
497 if ((ifp->if_flags & IFF_RUNNING) == 0)
500 ue->ue_methods->ue_tick(ue);
504 uether_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred* uc)
506 struct usb_ether *ue = ifp->if_softc;
507 struct ifreq *ifr = (struct ifreq *)data;
508 struct mii_data *mii;
514 if (ifp->if_flags & IFF_UP) {
515 if (ifp->if_flags & IFF_RUNNING)
516 ue_queue_command(ue, ue_promisc_task,
517 &ue->ue_promisc_task[0].hdr,
518 &ue->ue_promisc_task[1].hdr);
520 ue_queue_command(ue, ue_start_task,
521 &ue->ue_sync_task[0].hdr,
522 &ue->ue_sync_task[1].hdr);
524 ue_queue_command(ue, ue_stop_task,
525 &ue->ue_sync_task[0].hdr,
526 &ue->ue_sync_task[1].hdr);
533 ue_queue_command(ue, ue_setmulti_task,
534 &ue->ue_multi_task[0].hdr,
535 &ue->ue_multi_task[1].hdr);
540 if (ue->ue_miibus != NULL) {
541 mii = device_get_softc(ue->ue_miibus);
542 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
544 error = ether_ioctl(ifp, command, data);
547 error = ether_ioctl(ifp, command, data);
554 uether_modevent(module_t mod, int type, void *data)
556 static int attached = 0;
563 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ue));
568 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ue));
575 static moduledata_t uether_mod = {
586 m_new = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
589 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
591 m_adj(m_new, ETHER_ALIGN);
596 uether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
599 struct ifnet *ifp = ue->ue_ifp;
604 IFNET_STAT_INC(ifp, ipackets, 1);
605 m->m_pkthdr.rcvif = ifp;
606 m->m_pkthdr.len = m->m_len = len;
608 /* enqueue for later when the lock can be released */
609 IF_ENQUEUE(&ue->ue_rxq, m);
614 uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
615 unsigned int offset, unsigned int len)
617 struct ifnet *ifp = ue->ue_ifp;
622 if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
627 IFNET_STAT_INC(ifp, iqdrops, 1);
631 usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
634 IFNET_STAT_INC(ifp, ipackets, 1);
635 m->m_pkthdr.rcvif = ifp;
636 m->m_pkthdr.len = m->m_len = len;
638 /* enqueue for later when the lock can be released */
639 IF_ENQUEUE(&ue->ue_rxq, m);
644 uether_rxflush(struct usb_ether *ue)
646 struct ifnet *ifp = ue->ue_ifp;
652 IF_DEQUEUE(&ue->ue_rxq, m);
657 * The USB xfer has been resubmitted so its safe to unlock now.
660 ifp->if_input(ifp, m);
665 DECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
666 MODULE_VERSION(uether, 1);