#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: l2cap_misc.c,v 1.7 2009/09/13 18:45:11 pooka Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/systm.h>
#include <netbt/bluetooth.h>
#include <netbt/hci.h>
#include <netbt/l2cap.h>
struct l2cap_channel_list
l2cap_active_list = LIST_HEAD_INITIALIZER(l2cap_active_list);
struct l2cap_channel_list
l2cap_listen_list = LIST_HEAD_INITIALIZER(l2cap_listen_list);
struct pool l2cap_req_pool, l2cap_pdu_pool;
const l2cap_qos_t l2cap_default_qos = {
0,
L2CAP_QOS_BEST_EFFORT,
0x00000000,
0x00000000,
0x00000000,
0xffffffff,
0xffffffff
};
int l2cap_response_timeout = 30;
int l2cap_response_extended_timeout = 180;
void
l2cap_init(void)
{
pool_init(&l2cap_req_pool, sizeof(struct l2cap_req), 0, 0, 0,
"l2cap_req", NULL, IPL_SOFTNET);
pool_init(&l2cap_pdu_pool, sizeof(struct l2cap_pdu), 0, 0, 0,
"l2cap_pdu", NULL, IPL_SOFTNET);
}
int
l2cap_setmode(struct l2cap_channel *chan)
{
KASSERT(chan != NULL);
KASSERT(chan->lc_link != NULL);
DPRINTF("CID #%d, auth %s, encrypt %s, secure %s\n", chan->lc_lcid,
(chan->lc_mode & L2CAP_LM_AUTH ? "yes" : "no"),
(chan->lc_mode & L2CAP_LM_ENCRYPT ? "yes" : "no"),
(chan->lc_mode & L2CAP_LM_SECURE ? "yes" : "no"));
if (chan->lc_mode & L2CAP_LM_AUTH)
chan->lc_link->hl_flags |= HCI_LINK_AUTH_REQ;
if (chan->lc_mode & L2CAP_LM_ENCRYPT)
chan->lc_link->hl_flags |= HCI_LINK_ENCRYPT_REQ;
if (chan->lc_mode & L2CAP_LM_SECURE)
chan->lc_link->hl_flags |= HCI_LINK_SECURE_REQ;
return hci_acl_setmode(chan->lc_link);
}
int
l2cap_request_alloc(struct l2cap_channel *chan, uint8_t code)
{
struct hci_link *link = chan->lc_link;
struct l2cap_req *req;
int next_id;
if (link == NULL)
return ENETDOWN;
next_id = link->hl_lastid + 1;
if (next_id > 0xff)
next_id = 1;
req = TAILQ_FIRST(&link->hl_reqs);
if (req && req->lr_id == next_id)
return ENFILE;
req = pool_get(&l2cap_req_pool, PR_NOWAIT);
if (req == NULL)
return ENOMEM;
req->lr_id = link->hl_lastid = next_id;
req->lr_code = code;
req->lr_chan = chan;
req->lr_link = link;
callout_init(&req->lr_rtx, 0);
callout_setfunc(&req->lr_rtx, l2cap_rtx, req);
callout_schedule(&req->lr_rtx, l2cap_response_timeout * hz);
TAILQ_INSERT_TAIL(&link->hl_reqs, req, lr_next);
return 0;
}
struct l2cap_req *
l2cap_request_lookup(struct hci_link *link, uint8_t id)
{
struct l2cap_req *req;
TAILQ_FOREACH(req, &link->hl_reqs, lr_next) {
if (req->lr_id == id)
return req;
}
return NULL;
}
void
l2cap_request_free(struct l2cap_req *req)
{
struct hci_link *link = req->lr_link;
callout_stop(&req->lr_rtx);
if (callout_invoking(&req->lr_rtx))
return;
callout_destroy(&req->lr_rtx);
TAILQ_REMOVE(&link->hl_reqs, req, lr_next);
pool_put(&l2cap_req_pool, req);
}
void
l2cap_rtx(void *arg)
{
struct l2cap_req *req = arg;
struct l2cap_channel *chan;
mutex_enter(bt_lock);
callout_ack(&req->lr_rtx);
chan = req->lr_chan;
l2cap_request_free(req);
DPRINTF("cid %d, ident %d\n", (chan ? chan->lc_lcid : 0), req->lr_id);
if (chan && chan->lc_state != L2CAP_CLOSED)
l2cap_close(chan, ETIMEDOUT);
mutex_exit(bt_lock);
}
int
l2cap_cid_alloc(struct l2cap_channel *chan)
{
struct l2cap_channel *used, *prev = NULL;
uint16_t cid = L2CAP_FIRST_CID;
if (chan->lc_lcid != L2CAP_NULL_CID || chan->lc_state != L2CAP_CLOSED)
return EISCONN;
LIST_FOREACH(used, &l2cap_active_list, lc_ncid) {
if (used->lc_lcid > cid)
break;
KASSERT(used->lc_lcid == cid);
cid++;
if (cid == L2CAP_LAST_CID)
return ENFILE;
prev = used;
}
chan->lc_lcid = cid;
if (prev)
LIST_INSERT_AFTER(prev, chan, lc_ncid);
else
LIST_INSERT_HEAD(&l2cap_active_list, chan, lc_ncid);
return 0;
}
struct l2cap_channel *
l2cap_cid_lookup(uint16_t cid)
{
struct l2cap_channel *chan;
LIST_FOREACH(chan, &l2cap_active_list, lc_ncid) {
if (chan->lc_lcid == cid)
return chan;
if (chan->lc_lcid > cid)
return NULL;
}
return NULL;
}