#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/caps.h>
#include <sys/socket.h>
#include <sys/tty.h>
#include <sys/ttycom.h>
#include <net/if.h>
#include <net/if_var.h>
#include <netgraph7/ng_message.h>
#include <netgraph7/netgraph.h>
#include <netgraph7/ng_parse.h>
#include <netgraph7/bluetooth/include/ng_bluetooth.h>
#include <netgraph7/bluetooth/include/ng_hci.h>
#include <netgraph7/bluetooth/include/ng_h4.h>
#include <netgraph7/bluetooth/drivers/h4/ng_h4_var.h>
#include <netgraph7/bluetooth/drivers/h4/ng_h4_prse.h>
#ifndef NG_SEPARATE_MALLOC
MALLOC_DEFINE(M_NETGRAPH_H4, "netgraph_h4", "Netgraph Bluetooth H4 node");
#else
#define M_NETGRAPH_H4 M_NETGRAPH
#endif
static int ng_h4_open (struct cdev *, struct tty *);
static int ng_h4_close (struct tty *, int);
static int ng_h4_read (struct tty *, struct uio *, int);
static int ng_h4_write (struct tty *, struct uio *, int);
static int ng_h4_input (int, struct tty *);
static int ng_h4_start (struct tty *);
static int ng_h4_ioctl (struct tty *, u_long, caddr_t,
int, struct ucred *);
static struct linesw ng_h4_disc = {
ng_h4_open,
ng_h4_close,
ng_h4_read,
ng_h4_write,
ng_h4_ioctl,
ng_h4_input,
ng_h4_start,
ttymodem
};
static ng_constructor_t ng_h4_constructor;
static ng_rcvmsg_t ng_h4_rcvmsg;
static ng_shutdown_t ng_h4_shutdown;
static ng_newhook_t ng_h4_newhook;
static ng_connect_t ng_h4_connect;
static ng_rcvdata_t ng_h4_rcvdata;
static ng_disconnect_t ng_h4_disconnect;
static void ng_h4_process_timeout (node_p, hook_p, void *, int);
static int ng_h4_mod_event (module_t, int, void *);
static struct ng_type typestruct = {
.version = NG_ABI_VERSION,
.name = NG_H4_NODE_TYPE,
.mod_event = ng_h4_mod_event,
.constructor = ng_h4_constructor,
.rcvmsg = ng_h4_rcvmsg,
.shutdown = ng_h4_shutdown,
.newhook = ng_h4_newhook,
.connect = ng_h4_connect,
.rcvdata = ng_h4_rcvdata,
.disconnect = ng_h4_disconnect,
.cmdlist = ng_h4_cmdlist
};
NETGRAPH_INIT(h4, &typestruct);
MODULE_VERSION(ng_h4, NG_BLUETOOTH_VERSION);
static int ng_h4_node = 0;
static int
ng_h4_open(struct cdev *dev, struct tty *tp)
{
char name[NG_NODESIZ];
ng_h4_info_p sc = NULL;
int error;
error = caps_priv_check_self(SYSCAP_NONET_NETGRAPH);
if (error != 0)
return (error);
sc = kmalloc(sizeof(*sc), M_NETGRAPH_H4, M_WAITOK | M_NULLOK | M_ZERO);
if (sc == NULL)
return (ENOMEM);
lwkt_gettoken(&tp->t_token);
sc->tp = tp;
sc->debug = NG_H4_WARN_LEVEL;
sc->state = NG_H4_W4_PKT_IND;
sc->want = 1;
sc->got = 0;
sc->outq.ifq_maxlen = NG_H4_DEFAULTQLEN;
ng_callout_init(&sc->timo);
NG_H4_LOCK(sc);
error = ng_make_node_common(&typestruct, &sc->node);
if (error != 0) {
NG_H4_UNLOCK(sc);
kprintf("%s: Unable to create new node!\n", __func__);
bzero(sc, sizeof(*sc));
kfree(sc, M_NETGRAPH_H4);
lwkt_reltoken(&tp->t_token);
return (error);
}
ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ng_h4_node ++);
error = ng_name_node(sc->node, name);
if (error != 0) {
NG_H4_UNLOCK(sc);
kprintf("%s: %s - node name exists?\n", __func__, name);
NG_NODE_UNREF(sc->node);
bzero(sc, sizeof(*sc));
kfree(sc, M_NETGRAPH_H4);
lwkt_reltoken(&tp->t_token);
return (error);
}
NG_NODE_SET_PRIVATE(sc->node, sc);
tp->t_sc = (caddr_t) sc;
NG_NODE_FORCE_WRITER(sc->node);
ttyflush(tp, FREAD | FWRITE);
clist_alloc_cblocks(&tp->t_canq, 0);
clist_alloc_cblocks(&tp->t_rawq, 0);
clist_alloc_cblocks(&tp->t_outq, MLEN + NG_H4_HIWATER);
NG_H4_UNLOCK(sc);
lwkt_reltoken(&tp->t_token);
return (error);
}
static int
ng_h4_close(struct tty *tp, int flag)
{
ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc;
lwkt_gettoken(&tp->t_token);
ttyflush(tp, FREAD | FWRITE);
clist_free_cblocks(&tp->t_outq);
if (sc != NULL) {
NG_H4_LOCK(sc);
if (callout_pending(&sc->timo))
ng_uncallout(&sc->timo, sc->node);
tp->t_sc = NULL;
sc->dying = 1;
NG_H4_UNLOCK(sc);
ng_rmnode_self(sc->node);
}
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ng_h4_read(struct tty *tp, struct uio *uio, int flag)
{
return (EIO);
}
static int
ng_h4_write(struct tty *tp, struct uio *uio, int flag)
{
return (EIO);
}
static int
ng_h4_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
struct ucred *cred)
{
ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc;
int error = 0;
if (sc == NULL)
return (ENXIO);
lwkt_gettoken(&tp->t_token);
NG_H4_LOCK(sc);
switch (cmd) {
case NGIOCGINFO:
#undef NI
#define NI(x) ((struct nodeinfo *)(x))
bzero(data, sizeof(*NI(data)));
if (NG_NODE_HAS_NAME(sc->node))
strncpy(NI(data)->name, NG_NODE_NAME(sc->node),
sizeof(NI(data)->name) - 1);
strncpy(NI(data)->type, sc->node->nd_type->name,
sizeof(NI(data)->type) - 1);
NI(data)->id = (u_int32_t) ng_node2ID(sc->node);
NI(data)->hooks = NG_NODE_NUMHOOKS(sc->node);
break;
default:
error = ENOIOCTL;
break;
}
NG_H4_UNLOCK(sc);
lwkt_reltoken(&tp->t_token);
return (error);
}
static int
ng_h4_input(int c, struct tty *tp)
{
ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc;
lwkt_gettoken(&tp->t_token);
if (sc == NULL || tp != sc->tp ||
sc->node == NULL || NG_NODE_NOT_VALID(sc->node)) {
lwkt_reltoken(&tp->t_token);
return (0);
}
NG_H4_LOCK(sc);
if ((tp->t_state & TS_CONNECTED) == 0) {
NG_H4_INFO("%s: %s - no carrier\n", __func__,
NG_NODE_NAME(sc->node));
sc->state = NG_H4_W4_PKT_IND;
sc->want = 1;
sc->got = 0;
NG_H4_UNLOCK(sc);
lwkt_reltoken(&tp->t_token);
return (0);
}
if (c & TTY_ERRORMASK) {
NG_H4_ERR("%s: %s - line error %#x, c=%#x\n", __func__,
NG_NODE_NAME(sc->node), c & TTY_ERRORMASK,
c & TTY_CHARMASK);
NG_H4_STAT_IERROR(sc->stat);
sc->state = NG_H4_W4_PKT_IND;
sc->want = 1;
sc->got = 0;
NG_H4_UNLOCK(sc);
lwkt_reltoken(&tp->t_token);
return (0);
}
NG_H4_STAT_BYTES_RECV(sc->stat, 1);
if (sc->got >= sizeof(sc->ibuf)) {
NG_H4_ALERT("%s: %s - input buffer overflow, c=%#x, got=%d\n",
__func__, NG_NODE_NAME(sc->node), c & TTY_CHARMASK,
sc->got);
NG_H4_STAT_IERROR(sc->stat);
sc->state = NG_H4_W4_PKT_IND;
sc->want = 1;
sc->got = 0;
NG_H4_UNLOCK(sc);
lwkt_reltoken(&tp->t_token);
return (0);
}
sc->ibuf[sc->got ++] = (c & TTY_CHARMASK);
NG_H4_INFO("%s: %s - got char %#x, want=%d, got=%d\n", __func__,
NG_NODE_NAME(sc->node), c, sc->want, sc->got);
if (sc->got < sc->want) {
NG_H4_UNLOCK(sc);
lwkt_reltoken(&tp->t_token);
return (0);
}
switch (sc->state) {
case NG_H4_W4_PKT_IND:
NG_H4_INFO("%s: %s - got packet indicator %#x\n", __func__,
NG_NODE_NAME(sc->node), sc->ibuf[0]);
sc->state = NG_H4_W4_PKT_HDR;
switch (sc->ibuf[0]) {
case NG_HCI_ACL_DATA_PKT:
sc->want = sizeof(ng_hci_acldata_pkt_t);
break;
case NG_HCI_SCO_DATA_PKT:
sc->want = sizeof(ng_hci_scodata_pkt_t);
break;
case NG_HCI_EVENT_PKT:
sc->want = sizeof(ng_hci_event_pkt_t);
break;
default:
NG_H4_WARN("%s: %s - ignoring unknown packet " \
"type=%#x\n", __func__, NG_NODE_NAME(sc->node),
sc->ibuf[0]);
NG_H4_STAT_IERROR(sc->stat);
sc->state = NG_H4_W4_PKT_IND;
sc->want = 1;
sc->got = 0;
break;
}
break;
case NG_H4_W4_PKT_HDR:
sc->state = NG_H4_W4_PKT_DATA;
switch (sc->ibuf[0]) {
case NG_HCI_ACL_DATA_PKT:
c = le16toh(((ng_hci_acldata_pkt_t *)
(sc->ibuf))->length);
break;
case NG_HCI_SCO_DATA_PKT:
c = ((ng_hci_scodata_pkt_t *)(sc->ibuf))->length;
break;
case NG_HCI_EVENT_PKT:
c = ((ng_hci_event_pkt_t *)(sc->ibuf))->length;
break;
default:
KASSERT((0), ("Invalid packet type=%#x",
sc->ibuf[0]));
break;
}
NG_H4_INFO("%s: %s - got packet header, packet type=%#x, " \
"packet size=%d, payload size=%d\n", __func__,
NG_NODE_NAME(sc->node), sc->ibuf[0], sc->got, c);
if (c > 0) {
sc->want += c;
if (sc->want >= sizeof(sc->ibuf)) {
int b;
NG_H4_ALERT("%s: %s - packet too big for " \
"buffer, type=%#x, got=%d, want=%d, " \
"length=%d\n", __func__,
NG_NODE_NAME(sc->node), sc->ibuf[0],
sc->got, sc->want, c);
NG_H4_ALERT("Packet header:\n");
for (b = 0; b < sc->got; b++)
NG_H4_ALERT("%#x ", sc->ibuf[b]);
NG_H4_ALERT("\n");
NG_H4_STAT_IERROR(sc->stat);
sc->state = NG_H4_W4_PKT_IND;
sc->want = 1;
sc->got = 0;
}
break;
}
case NG_H4_W4_PKT_DATA:
NG_H4_INFO("%s: %s - got full packet, packet type=%#x, " \
"packet size=%d\n", __func__,
NG_NODE_NAME(sc->node), sc->ibuf[0], sc->got);
if (sc->hook != NULL && NG_HOOK_IS_VALID(sc->hook)) {
struct mbuf *m = NULL;
MGETHDR(m, M_NOWAIT, MT_DATA);
if (m != NULL && m_copyback2(m, 0, sc->got, sc->ibuf,
M_NOWAIT) == 0) {
NG_SEND_DATA_ONLY(c, sc->hook, m);
} else {
NG_H4_ERR("%s: %s - could not get mbuf\n",
__func__, NG_NODE_NAME(sc->node));
NG_H4_STAT_IERROR(sc->stat);
m_freem(m);
}
}
sc->state = NG_H4_W4_PKT_IND;
sc->want = 1;
sc->got = 0;
NG_H4_STAT_PCKTS_RECV(sc->stat);
break;
default:
KASSERT((0), ("Invalid H4 node state=%d", sc->state));
break;
}
NG_H4_UNLOCK(sc);
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ng_h4_start(struct tty *tp)
{
ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc;
struct mbuf *m = NULL;
int size;
lwkt_gettoken(&tp->t_token);
if (sc == NULL || tp != sc->tp ||
sc->node == NULL || NG_NODE_NOT_VALID(sc->node)) {
lwkt_reltoken(&tp->t_token);
return (0);
}
#if 0
while (tp->t_outq.c_cc < NG_H4_HIWATER) {
#else
while (1) {
#endif
IF_DEQUEUE(&sc->outq, m);
if (m == NULL)
break;
while (m != NULL) {
size = m->m_len - clist_btoq(mtod(m, u_char *),
m->m_len, &tp->t_outq);
NG_H4_LOCK(sc);
NG_H4_STAT_BYTES_SENT(sc->stat, size);
NG_H4_UNLOCK(sc);
m->m_data += size;
m->m_len -= size;
if (m->m_len > 0)
break;
m = m_free(m);
}
if (m != NULL) {
IF_PREPEND(&sc->outq, m);
break;
}
NG_H4_LOCK(sc);
NG_H4_STAT_PCKTS_SENT(sc->stat);
NG_H4_UNLOCK(sc);
}
if (tp->t_oproc != NULL)
(*tp->t_oproc) (tp);
NG_H4_LOCK(sc);
if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->timo))
ng_callout(&sc->timo, sc->node, NULL, 1,
ng_h4_process_timeout, NULL, 0);
NG_H4_UNLOCK(sc);
lwkt_reltoken(&tp->t_token);
return (0);
}
static int
ng_h4_constructor(node_p node)
{
return (EOPNOTSUPP);
}
static int
ng_h4_newhook(node_p node, hook_p hook, const char *name)
{
ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node);
if (strcmp(name, NG_H4_HOOK) != 0)
return (EINVAL);
NG_H4_LOCK(sc);
if (sc->hook != NULL) {
NG_H4_UNLOCK(sc);
return (EISCONN);
}
sc->hook = hook;
NG_H4_UNLOCK(sc);
return (0);
}
static int
ng_h4_connect(hook_p hook)
{
ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
if (hook != sc->hook)
panic("%s: hook != sc->hook", __func__);
NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
NG_HOOK_FORCE_QUEUE(hook);
return (0);
}
static int
ng_h4_disconnect(hook_p hook)
{
ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
if (sc != NULL) {
if (hook != sc->hook)
panic("%s: hook != sc->hook", __func__);
NG_H4_LOCK(sc);
if (callout_pending(&sc->timo))
ng_uncallout(&sc->timo, sc->node);
IF_DRAIN(&sc->outq);
sc->state = NG_H4_W4_PKT_IND;
sc->want = 1;
sc->got = 0;
sc->hook = NULL;
NG_H4_UNLOCK(sc);
}
return (0);
}
static int
ng_h4_shutdown(node_p node)
{
ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node);
NG_H4_LOCK(sc);
if (!sc->dying) {
NG_H4_UNLOCK(sc);
NG_NODE_REVIVE(node);
return (EOPNOTSUPP);
}
NG_H4_UNLOCK(sc);
NG_NODE_SET_PRIVATE(node, NULL);
IF_DRAIN(&sc->outq);
NG_NODE_UNREF(node);
bzero(sc, sizeof(*sc));
kfree(sc, M_NETGRAPH_H4);
return (0);
}
static int
ng_h4_rcvdata(hook_p hook, item_p item)
{
ng_h4_info_p sc = (ng_h4_info_p)NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
struct mbuf *m = NULL;
int qlen;
if (sc == NULL)
return (EHOSTDOWN);
if (hook != sc->hook)
panic("%s: hook != sc->hook", __func__);
NGI_GET_M(item, m);
NG_FREE_ITEM(item);
NG_H4_LOCK(sc);
if (IF_QFULL(&sc->outq)) {
NG_H4_ERR("%s: %s - dropping mbuf, len=%d\n", __func__,
NG_NODE_NAME(sc->node), m->m_pkthdr.len);
NG_H4_STAT_OERROR(sc->stat);
IF_DROP(&sc->outq);
NG_H4_UNLOCK(sc);
NG_FREE_M(m);
return (ENOBUFS);
}
NG_H4_INFO("%s: %s - queue mbuf, len=%d\n", __func__,
NG_NODE_NAME(sc->node), m->m_pkthdr.len);
IF_ENQUEUE(&sc->outq, m);
qlen = IF_QLEN(&sc->outq);
NG_H4_UNLOCK(sc);
if (qlen == 1)
ng_h4_start(sc->tp);
return (0);
}
static int
ng_h4_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node);
struct ng_mesg *msg = NULL, *resp = NULL;
int error = 0;
if (sc == NULL)
return (EHOSTDOWN);
NGI_GET_MSG(item, msg);
NG_H4_LOCK(sc);
switch (msg->header.typecookie) {
case NGM_GENERIC_COOKIE:
switch (msg->header.cmd) {
case NGM_TEXT_STATUS:
NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK);
if (resp == NULL)
error = ENOMEM;
else
ksnprintf(resp->data, NG_TEXTRESPONSE,
"Hook: %s\n" \
"Debug: %d\n" \
"State: %d\n" \
"Queue: [have:%d,max:%d]\n" \
"Input: [got:%d,want:%d]",
(sc->hook != NULL)? NG_H4_HOOK : "",
sc->debug,
sc->state,
IF_QLEN(&sc->outq),
sc->outq.ifq_maxlen,
sc->got,
sc->want);
break;
default:
error = EINVAL;
break;
}
break;
case NGM_H4_COOKIE:
switch (msg->header.cmd) {
case NGM_H4_NODE_RESET:
IF_DRAIN(&sc->outq);
sc->state = NG_H4_W4_PKT_IND;
sc->want = 1;
sc->got = 0;
break;
case NGM_H4_NODE_GET_STATE:
NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_state_ep),
M_WAITOK | M_NULLOK);
if (resp == NULL)
error = ENOMEM;
else
*((ng_h4_node_state_ep *)(resp->data)) =
sc->state;
break;
case NGM_H4_NODE_GET_DEBUG:
NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_debug_ep),
M_WAITOK | M_NULLOK);
if (resp == NULL)
error = ENOMEM;
else
*((ng_h4_node_debug_ep *)(resp->data)) =
sc->debug;
break;
case NGM_H4_NODE_SET_DEBUG:
if (msg->header.arglen != sizeof(ng_h4_node_debug_ep))
error = EMSGSIZE;
else
sc->debug =
*((ng_h4_node_debug_ep *)(msg->data));
break;
case NGM_H4_NODE_GET_QLEN:
NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_qlen_ep),
M_WAITOK | M_NULLOK);
if (resp == NULL)
error = ENOMEM;
else
*((ng_h4_node_qlen_ep *)(resp->data)) =
sc->outq.ifq_maxlen;
break;
case NGM_H4_NODE_SET_QLEN:
if (msg->header.arglen != sizeof(ng_h4_node_qlen_ep))
error = EMSGSIZE;
else if (*((ng_h4_node_qlen_ep *)(msg->data)) <= 0)
error = EINVAL;
else
sc->outq.ifq_maxlen =
*((ng_h4_node_qlen_ep *)(msg->data));
break;
case NGM_H4_NODE_GET_STAT:
NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_stat_ep),
M_WAITOK | M_NULLOK);
if (resp == NULL)
error = ENOMEM;
else
bcopy(&sc->stat, resp->data,
sizeof(ng_h4_node_stat_ep));
break;
case NGM_H4_NODE_RESET_STAT:
NG_H4_STAT_RESET(sc->stat);
break;
default:
error = EINVAL;
break;
}
break;
default:
error = EINVAL;
break;
}
NG_H4_UNLOCK(sc);
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
return (error);
}
static void
ng_h4_process_timeout(node_p node, hook_p hook, void *arg1, int arg2)
{
ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node);
ng_h4_start(sc->tp);
}
static int
ng_h4_mod_event(module_t mod, int event, void *data)
{
static int ng_h4_ldisc;
int error = 0;
switch (event) {
case MOD_LOAD:
crit_enter();
ng_h4_ldisc = ldisc_register(BTUARTDISC, &ng_h4_disc);
crit_exit();
if (ng_h4_ldisc < 0) {
kprintf("%s: can't register H4 line discipline\n",
__func__);
error = EIO;
}
break;
case MOD_UNLOAD:
crit_enter();
ldisc_deregister(ng_h4_ldisc);
crit_exit();
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}