2 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
22 #include <sys/param.h>
24 #include <sys/mutex.h>
26 #include <sys/kernel.h>
27 #include <sys/socket.h>
28 #include <sys/systm.h>
29 #include <sys/malloc.h>
30 #include <sys/queue.h>
31 #include <sys/taskqueue.h>
33 #include <sys/endian.h>
36 #include <net/ethernet.h>
37 #include <net/if_media.h>
39 #include <net80211/ieee80211_var.h>
40 #include <net80211/ieee80211_radiotap.h>
42 #include <dev/rtwn/if_rtwnvar.h>
44 #include <dev/rtwn/if_rtwn_task.h>
48 rtwn_cmdq_cb(void *arg, int pending)
50 struct rtwn_softc *sc = arg;
51 struct rtwn_cmdq *item;
54 * Device must be powered on (via rtwn_power_on())
55 * before any command may be sent.
58 if (!(sc->sc_flags & RTWN_RUNNING)) {
64 while (sc->cmdq[sc->cmdq_first].func != NULL) {
65 item = &sc->cmdq[sc->cmdq_first];
66 sc->cmdq_first = (sc->cmdq_first + 1) % RTWN_CMDQ_SIZE;
69 item->func(sc, &item->data);
72 memset(item, 0, sizeof (*item));
79 rtwn_cmdq_init(struct rtwn_softc *sc)
81 RTWN_CMDQ_LOCK_INIT(sc);
82 TASK_INIT(&sc->cmdq_task, 0, rtwn_cmdq_cb, sc);
86 rtwn_cmdq_destroy(struct rtwn_softc *sc)
88 if (RTWN_CMDQ_LOCK_INITIALIZED(sc))
89 RTWN_CMDQ_LOCK_DESTROY(sc);
93 rtwn_cmd_sleepable(struct rtwn_softc *sc, const void *ptr, size_t len,
96 struct ieee80211com *ic = &sc->sc_ic;
98 KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
101 if (sc->sc_detached) {
102 RTWN_CMDQ_UNLOCK(sc);
106 if (sc->cmdq[sc->cmdq_last].func != NULL) {
107 device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
108 RTWN_CMDQ_UNLOCK(sc);
114 memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
115 sc->cmdq[sc->cmdq_last].func = func;
116 sc->cmdq_last = (sc->cmdq_last + 1) % RTWN_CMDQ_SIZE;
117 RTWN_CMDQ_UNLOCK(sc);
119 ieee80211_runtask(ic, &sc->cmdq_task);