/*
 * QEMU Bluetooth L2CAP logic.
 *
 * Copyright (C) 2008 Andrzej Zaborowski  <balrog@zabor.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu-common.h"
#include "qemu/timer.h"
#include "bt.h"

#define L2CAP_CID_MAX	0x100	/* Between 0x40 and 0x10000 */

struct l2cap_instance_s {
    struct bt_link_s *link;
    struct bt_l2cap_device_s *dev;
    int role;

    uint8_t frame_in[65535 + L2CAP_HDR_SIZE] __attribute__ ((aligned (4)));
    int frame_in_len;

    uint8_t frame_out[65535 + L2CAP_HDR_SIZE] __attribute__ ((aligned (4)));
    int frame_out_len;

    /* Signalling channel timers.  They exist per-request but we can make
     * sure we have no more than one outstanding request at any time.  */
    QEMUTimer *rtx;
    QEMUTimer *ertx;

    int last_id;
    int next_id;

    struct l2cap_chan_s {
        struct bt_l2cap_conn_params_s params;

        void (*frame_in)(struct l2cap_chan_s *chan, uint16_t cid,
                        const l2cap_hdr *hdr, int len);
        int mps;
        int min_mtu;

        struct l2cap_instance_s *l2cap;

        /* Only allocated channels */
        uint16_t remote_cid;
#define L2CAP_CFG_INIT	2
#define L2CAP_CFG_ACC	1
        int config_req_id; /* TODO: handle outgoing requests generically */
        int config;

        /* Only connection-oriented channels.  Note: if we allow the tx and
         * rx traffic to be in different modes at any time, we need two.  */
        int mode;

        /* Only flow-controlled, connection-oriented channels */
        uint8_t sdu[65536]; /* TODO: dynamically allocate */
        int len_cur, len_total;
        int rexmit;
        int monitor_timeout;
        QEMUTimer *monitor_timer;
        QEMUTimer *retransmission_timer;
    } *cid[L2CAP_CID_MAX];
    /* The channel state machine states map as following:
     * CLOSED           -> !cid[N]
     * WAIT_CONNECT     -> never occurs
     * WAIT_CONNECT_RSP -> never occurs
     * CONFIG           -> cid[N] && config < 3
     *   WAIT_CONFIG         -> never occurs, cid[N] && config == 0 && !config_r
     *   WAIT_SEND_CONFIG    -> never occurs, cid[N] && config == 1 && !config_r
     *   WAIT_CONFIG_REQ_RSP -> cid[N] && config == 0 && config_req_id
     *   WAIT_CONFIG_RSP     -> cid[N] && config == 1 && config_req_id
     *   WAIT_CONFIG_REQ     -> cid[N] && config == 2
     * OPEN             -> cid[N] && config == 3
     * WAIT_DISCONNECT  -> never occurs
     */

    struct l2cap_chan_s signalling_ch;
    struct l2cap_chan_s group_ch;
};

struct slave_l2cap_instance_s {
    struct bt_link_s link;	/* Underlying logical link (ACL) */
    struct l2cap_instance_s l2cap;
};

struct bt_l2cap_psm_s {
    int psm;
    int min_mtu;
    int (*new_channel)(struct bt_l2cap_device_s *device,
                    struct bt_l2cap_conn_params_s *params);
    struct bt_l2cap_psm_s *next;
};

static const uint16_t l2cap_fcs16_table[256] = {
    0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
    0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
    0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
    0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
    0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
    0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
    0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
    0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
    0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
    0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
    0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
    0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
    0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
    0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
    0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
    0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
    0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
    0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
    0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
    0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
    0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
    0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
    0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
    0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
    0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
    0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
    0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
    0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
    0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
    0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
    0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
    0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
};

static uint16_t l2cap_fcs16(const uint8_t *message, int len)
{
    uint16_t fcs = 0x0000;

    while (len --)
#if 0
    {
        int i;

        fcs ^= *message ++;
        for (i = 8; i; -- i)
            if (fcs & 1)
                fcs = (fcs >> 1) ^ 0xa001;
            else
                fcs = (fcs >> 1);
    }
#else
        fcs = (fcs >> 8) ^ l2cap_fcs16_table[(fcs ^ *message ++) & 0xff];
#endif

    return fcs;
}

/* L2CAP layer logic (protocol) */

static void l2cap_retransmission_timer_update(struct l2cap_chan_s *ch)
{
#if 0
    if (ch->mode != L2CAP_MODE_BASIC && ch->rexmit)
        qemu_mod_timer(ch->retransmission_timer);
    else
        qemu_del_timer(ch->retransmission_timer);
#endif
}

static void l2cap_monitor_timer_update(struct l2cap_chan_s *ch)
{
#if 0
    if (ch->mode != L2CAP_MODE_BASIC && !ch->rexmit)
        qemu_mod_timer(ch->monitor_timer);
    else
        qemu_del_timer(ch->monitor_timer);
#endif
}

static void l2cap_command_reject(struct l2cap_instance_s *l2cap, int id,
                uint16_t reason, const void *data, int plen)
{
    uint8_t *pkt;
    l2cap_cmd_hdr *hdr;
    l2cap_cmd_rej *params;
    uint16_t len;

    reason = cpu_to_le16(reason);
    len = cpu_to_le16(L2CAP_CMD_REJ_SIZE + plen);

    pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
                    L2CAP_CMD_HDR_SIZE + L2CAP_CMD_REJ_SIZE + plen);
    hdr = (void *) (pkt + 0);
    params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);

    hdr->code = L2CAP_COMMAND_REJ;
    hdr->ident = id;
    memcpy(&hdr->len, &len, sizeof(hdr->len));
    memcpy(&params->reason, &reason, sizeof(reason));
    if (plen)
       memcpy(pkt + L2CAP_CMD_HDR_SIZE + L2CAP_CMD_REJ_SIZE, data, plen);

    l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
}

static void l2cap_command_reject_cid(struct l2cap_instance_s *l2cap, int id,
                uint16_t reason, uint16_t dcid, uint16_t scid)
{
    l2cap_cmd_rej_cid params = {
        .dcid = dcid,
        .scid = scid,
    };

    l2cap_command_reject(l2cap, id, reason, &params, L2CAP_CMD_REJ_CID_SIZE);
}

static void l2cap_connection_response(struct l2cap_instance_s *l2cap,
                int dcid, int scid, int result, int status)
{
    uint8_t *pkt;
    l2cap_cmd_hdr *hdr;
    l2cap_conn_rsp *params;

    pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
                    L2CAP_CMD_HDR_SIZE + L2CAP_CONN_RSP_SIZE);
    hdr = (void *) (pkt + 0);
    params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);

    hdr->code = L2CAP_CONN_RSP;
    hdr->ident = l2cap->last_id;
    hdr->len = cpu_to_le16(L2CAP_CONN_RSP_SIZE);

    params->dcid = cpu_to_le16(dcid);
    params->scid = cpu_to_le16(scid);
    params->result = cpu_to_le16(result);
    params->status = cpu_to_le16(status);

    l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
}

static void l2cap_configuration_request(struct l2cap_instance_s *l2cap,
                int dcid, int flag, const uint8_t *data, int len)
{
    uint8_t *pkt;
    l2cap_cmd_hdr *hdr;
    l2cap_conf_req *params;

    pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
                    L2CAP_CMD_HDR_SIZE + L2CAP_CONF_REQ_SIZE(len));
    hdr = (void *) (pkt + 0);
    params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);

    /* TODO: unify the id sequencing */
    l2cap->last_id = l2cap->next_id;
    l2cap->next_id = l2cap->next_id == 255 ? 1 : l2cap->next_id + 1;

    hdr->code = L2CAP_CONF_REQ;
    hdr->ident = l2cap->last_id;
    hdr->len = cpu_to_le16(L2CAP_CONF_REQ_SIZE(len));

    params->dcid = cpu_to_le16(dcid);
    params->flags = cpu_to_le16(flag);
    if (len)
        memcpy(params->data, data, len);

    l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
}

static void l2cap_configuration_response(struct l2cap_instance_s *l2cap,
                int scid, int flag, int result, const uint8_t *data, int len)
{
    uint8_t *pkt;
    l2cap_cmd_hdr *hdr;
    l2cap_conf_rsp *params;

    pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
                    L2CAP_CMD_HDR_SIZE + L2CAP_CONF_RSP_SIZE(len));
    hdr = (void *) (pkt + 0);
    params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);

    hdr->code = L2CAP_CONF_RSP;
    hdr->ident = l2cap->last_id;
    hdr->len = cpu_to_le16(L2CAP_CONF_RSP_SIZE(len));

    params->scid = cpu_to_le16(scid);
    params->flags = cpu_to_le16(flag);
    params->result = cpu_to_le16(result);
    if (len)
        memcpy(params->data, data, len);

    l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
}

static void l2cap_disconnection_response(struct l2cap_instance_s *l2cap,
                int dcid, int scid)
{
    uint8_t *pkt;
    l2cap_cmd_hdr *hdr;
    l2cap_disconn_rsp *params;

    pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
                    L2CAP_CMD_HDR_SIZE + L2CAP_DISCONN_RSP_SIZE);
    hdr = (void *) (pkt + 0);
    params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);

    hdr->code = L2CAP_DISCONN_RSP;
    hdr->ident = l2cap->last_id;
    hdr->len = cpu_to_le16(L2CAP_DISCONN_RSP_SIZE);

    params->dcid = cpu_to_le16(dcid);
    params->scid = cpu_to_le16(scid);

    l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
}

static void l2cap_echo_response(struct l2cap_instance_s *l2cap,
                const uint8_t *data, int len)
{
    uint8_t *pkt;
    l2cap_cmd_hdr *hdr;
    uint8_t *params;

    pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
                    L2CAP_CMD_HDR_SIZE + len);
    hdr = (void *) (pkt + 0);
    params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);

    hdr->code = L2CAP_ECHO_RSP;
    hdr->ident = l2cap->last_id;
    hdr->len = cpu_to_le16(len);

    memcpy(params, data, len);

    l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
}

static void l2cap_info_response(struct l2cap_instance_s *l2cap, int type,
                int result, const uint8_t *data, int len)
{
    uint8_t *pkt;
    l2cap_cmd_hdr *hdr;
    l2cap_info_rsp *params;

    pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
                    L2CAP_CMD_HDR_SIZE + L2CAP_INFO_RSP_SIZE + len);
    hdr = (void *) (pkt + 0);
    params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);

    hdr->code = L2CAP_INFO_RSP;
    hdr->ident = l2cap->last_id;
    hdr->len = cpu_to_le16(L2CAP_INFO_RSP_SIZE + len);

    params->type = cpu_to_le16(type);
    params->result = cpu_to_le16(result);
    if (len)
       memcpy(params->data, data, len);

    l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
}

static uint8_t *l2cap_bframe_out(struct bt_l2cap_conn_params_s *parm, int len);
static void l2cap_bframe_submit(struct bt_l2cap_conn_params_s *parms);
#if 0
static uint8_t *l2cap_iframe_out(struct bt_l2cap_conn_params_s *parm, int len);
static void l2cap_iframe_submit(struct bt_l2cap_conn_params_s *parm);
#endif
static void l2cap_bframe_in(struct l2cap_chan_s *ch, uint16_t cid,
                const l2cap_hdr *hdr, int len);
static void l2cap_iframe_in(struct l2cap_chan_s *ch, uint16_t cid,
                const l2cap_hdr *hdr, int len);

static int l2cap_cid_new(struct l2cap_instance_s *l2cap)
{
    int i;

    for (i = L2CAP_CID_ALLOC; i < L2CAP_CID_MAX; i ++)
        if (!l2cap->cid[i])
            return i;

    return L2CAP_CID_INVALID;
}

static inline struct bt_l2cap_psm_s *l2cap_psm(
                struct bt_l2cap_device_s *device, int psm)
{
    struct bt_l2cap_psm_s *ret = device->first_psm;

    while (ret && ret->psm != psm)
        ret = ret->next;

    return ret;
}

static struct l2cap_chan_s *l2cap_channel_open(struct l2cap_instance_s *l2cap,
                int psm, int source_cid)
{
    struct l2cap_chan_s *ch = NULL;
    struct bt_l2cap_psm_s *psm_info;
    int result, status;
    int cid = l2cap_cid_new(l2cap);

    if (cid) {
        /* See what the channel is to be used for.. */
        psm_info = l2cap_psm(l2cap->dev, psm);

        if (psm_info) {
            /* Device supports this use-case.  */
            ch = g_malloc0(sizeof(*ch));
            ch->params.sdu_out = l2cap_bframe_out;
            ch->params.sdu_submit = l2cap_bframe_submit;
            ch->frame_in = l2cap_bframe_in;
            ch->mps = 65536;
            ch->min_mtu = MAX(48, psm_info->min_mtu);
            ch->params.remote_mtu = MAX(672, ch->min_mtu);
            ch->remote_cid = source_cid;
            ch->mode = L2CAP_MODE_BASIC;
            ch->l2cap = l2cap;

            /* Does it feel like opening yet another channel though?  */
            if (!psm_info->new_channel(l2cap->dev, &ch->params)) {
                l2cap->cid[cid] = ch;

                result = L2CAP_CR_SUCCESS;
                status = L2CAP_CS_NO_INFO;
            } else {
                g_free(ch);

                result = L2CAP_CR_NO_MEM;
                status = L2CAP_CS_NO_INFO;
            }
        } else {
            result = L2CAP_CR_BAD_PSM;
            status = L2CAP_CS_NO_INFO;
        }
    } else {
        result = L2CAP_CR_NO_MEM;
        status = L2CAP_CS_NO_INFO;
    }

    l2cap_connection_response(l2cap, cid, source_cid, result, status);

    return ch;
}

static void l2cap_channel_close(struct l2cap_instance_s *l2cap,
                int cid, int source_cid)
{
    struct l2cap_chan_s *ch = NULL;

    /* According to Volume 3, section 6.1.1, pg 1048 of BT Core V2.0, a
     * connection in CLOSED state still responds with a L2CAP_DisconnectRsp
     * message on an L2CAP_DisconnectReq event.  */
    if (unlikely(cid < L2CAP_CID_ALLOC)) {
        l2cap_command_reject_cid(l2cap, l2cap->last_id, L2CAP_REJ_CID_INVAL,
                        cid, source_cid);
        return;
    }
    if (likely(cid >= L2CAP_CID_ALLOC && cid < L2CAP_CID_MAX))
        ch = l2cap->cid[cid];

    if (likely(ch)) {
        if (ch->remote_cid != source_cid) {
            fprintf(stderr, "%s: Ignoring a Disconnection Request with the "
                            "invalid SCID %04x.\n", __FUNCTION__, source_cid);
            return;
        }

        l2cap->cid[cid] = NULL;

        ch->params.close(ch->params.opaque);
        g_free(ch);
    }

    l2cap_disconnection_response(l2cap, cid, source_cid);
}

static void l2cap_channel_config_null(struct l2cap_instance_s *l2cap,
                struct l2cap_chan_s *ch)
{
    l2cap_configuration_request(l2cap, ch->remote_cid, 0, NULL, 0);
    ch->config_req_id = l2cap->last_id;
    ch->config &= ~L2CAP_CFG_INIT;
}

static void l2cap_channel_config_req_event(struct l2cap_instance_s *l2cap,
                struct l2cap_chan_s *ch)
{
    /* Use all default channel options and terminate negotiation.  */
    l2cap_channel_config_null(l2cap, ch);
}

static int l2cap_channel_config(struct l2cap_instance_s *l2cap,
                struct l2cap_chan_s *ch, int flag,
                const uint8_t *data, int len)
{
    l2cap_conf_opt *opt;
    l2cap_conf_opt_qos *qos;
    uint32_t val;
    uint8_t rsp[len];
    int result = L2CAP_CONF_SUCCESS;

    data = memcpy(rsp, data, len);
    while (len) {
        opt = (void *) data;

        if (len < L2CAP_CONF_OPT_SIZE ||
                        len < L2CAP_CONF_OPT_SIZE + opt->len) {
            result = L2CAP_CONF_REJECT;
            break;
        }
        data += L2CAP_CONF_OPT_SIZE + opt->len;
        len -= L2CAP_CONF_OPT_SIZE + opt->len;

        switch (opt->type & 0x7f) {
        case L2CAP_CONF_MTU:
            if (opt->len != 2) {
                result = L2CAP_CONF_REJECT;
                break;
            }

            /* MTU */
            val = le16_to_cpup((void *) opt->val);
            if (val < ch->min_mtu) {
                cpu_to_le16w((void *) opt->val, ch->min_mtu);
                result = L2CAP_CONF_UNACCEPT;
                break;
            }

            ch->params.remote_mtu = val;
            break;

        case L2CAP_CONF_FLUSH_TO:
            if (opt->len != 2) {
                result = L2CAP_CONF_REJECT;
                break;
            }

            /* Flush Timeout */
            val = le16_to_cpup((void *) opt->val);
            if (val < 0x0001) {
                opt->val[0] = 0xff;
                opt->val[1] = 0xff;
                result = L2CAP_CONF_UNACCEPT;
                break;
            }
            break;

        case L2CAP_CONF_QOS:
            if (opt->len != L2CAP_CONF_OPT_QOS_SIZE) {
                result = L2CAP_CONF_REJECT;
                break;
            }
            qos = (void *) opt->val;

            /* Flags */
            val = qos->flags;
            if (val) {
                qos->flags = 0;
                result = L2CAP_CONF_UNACCEPT;
            }

            /* Service type */
            val = qos->service_type;
            if (val != L2CAP_CONF_QOS_BEST_EFFORT &&
                            val != L2CAP_CONF_QOS_NO_TRAFFIC) {
                qos->service_type = L2CAP_CONF_QOS_BEST_EFFORT;
                result = L2CAP_CONF_UNACCEPT;
            }

            if (val != L2CAP_CONF_QOS_NO_TRAFFIC) {
                /* XXX: These values should possibly be calculated
                 * based on LM / baseband properties also.  */

                /* Token rate */
                val = le32_to_cpu(qos->token_rate);
                if (val == L2CAP_CONF_QOS_WILDCARD)
                    qos->token_rate = cpu_to_le32(0x100000);

                /* Token bucket size */
                val = le32_to_cpu(qos->token_bucket_size);
                if (val == L2CAP_CONF_QOS_WILDCARD)
                    qos->token_bucket_size = cpu_to_le32(65500);

                /* Any Peak bandwidth value is correct to return as-is */
                /* Any Access latency value is correct to return as-is */
                /* Any Delay variation value is correct to return as-is */
            }
            break;

        case L2CAP_CONF_RFC:
            if (opt->len != 9) {
                result = L2CAP_CONF_REJECT;
                break;
            }

            /* Mode */
            val = opt->val[0];
            switch (val) {
            case L2CAP_MODE_BASIC:
                ch->mode = val;
                ch->frame_in = l2cap_bframe_in;

                /* All other parameters shall be ignored */
                break;

            case L2CAP_MODE_RETRANS:
            case L2CAP_MODE_FLOWCTL:
                ch->mode = val;
                ch->frame_in = l2cap_iframe_in;
                /* Note: most of these parameters refer to incoming traffic
                 * so we don't need to save them as long as we can accept
                 * incoming PDUs at any values of the parameters.  */

                /* TxWindow size */
                val = opt->val[1];
                if (val < 1 || val > 32) {
                    opt->val[1] = 32;
                    result = L2CAP_CONF_UNACCEPT;
                    break;
                }

                /* MaxTransmit */
                val = opt->val[2];
                if (val < 1) {
                    opt->val[2] = 1;
                    result = L2CAP_CONF_UNACCEPT;
                    break;
                }

                /* Remote Retransmission time-out shouldn't affect local
                 * operation (?) */

                /* The Monitor time-out drives the local Monitor timer (?),
                 * so save the value.  */
                val = (opt->val[6] << 8) | opt->val[5];
                if (val < 30) {
                    opt->val[5] = 100 & 0xff;
                    opt->val[6] = 100 >> 8;
                    result = L2CAP_CONF_UNACCEPT;
                    break;
                }
                ch->monitor_timeout = val;
                l2cap_monitor_timer_update(ch);

                /* MPS */
                val = (opt->val[8] << 8) | opt->val[7];
                if (val < ch->min_mtu) {
                    opt->val[7] = ch->min_mtu & 0xff;
                    opt->val[8] = ch->min_mtu >> 8;
                    result = L2CAP_CONF_UNACCEPT;
                    break;
                }
                ch->mps = val;
                break;

            default:
                result = L2CAP_CONF_UNACCEPT;
                break;
            }
            break;

        default:
            if (!(opt->type >> 7))
                result = L2CAP_CONF_UNKNOWN;
            break;
        }

        if (result != L2CAP_CONF_SUCCESS)
            break;	/* XXX: should continue? */
    }

    l2cap_configuration_response(l2cap, ch->remote_cid,
                    flag, result, rsp, len);

    return result == L2CAP_CONF_SUCCESS && !flag;
}

static void l2cap_channel_config_req_msg(struct l2cap_instance_s *l2cap,
                int flag, int cid, const uint8_t *data, int len)
{
    struct l2cap_chan_s *ch;

    if (unlikely(cid >= L2CAP_CID_MAX || !l2cap->cid[cid])) {
        l2cap_command_reject_cid(l2cap, l2cap->last_id, L2CAP_REJ_CID_INVAL,
                        cid, 0x0000);
        return;
    }
    ch = l2cap->cid[cid];

    /* From OPEN go to WAIT_CONFIG_REQ and from WAIT_CONFIG_REQ_RSP to
     * WAIT_CONFIG_REQ_RSP.  This is assuming the transition chart for OPEN
     * on pg 1053, section 6.1.5, volume 3 of BT Core V2.0 has a mistake
     * and on options-acceptable we go back to OPEN and otherwise to
     * WAIT_CONFIG_REQ and not the other way.  */
    ch->config &= ~L2CAP_CFG_ACC;

    if (l2cap_channel_config(l2cap, ch, flag, data, len))
        /* Go to OPEN or WAIT_CONFIG_RSP */
        ch->config |= L2CAP_CFG_ACC;

    /* TODO: if the incoming traffic flow control or retransmission mode
     * changed then we probably need to also generate the
     * ConfigureChannel_Req event and set the outgoing traffic to the same
     * mode.  */
    if (!(ch->config & L2CAP_CFG_INIT) && (ch->config & L2CAP_CFG_ACC) &&
                    !ch->config_req_id)
        l2cap_channel_config_req_event(l2cap, ch);
}

static int l2cap_channel_config_rsp_msg(struct l2cap_instance_s *l2cap,
                int result, int flag, int cid, const uint8_t *data, int len)
{
    struct l2cap_chan_s *ch;

    if (unlikely(cid >= L2CAP_CID_MAX || !l2cap->cid[cid])) {
        l2cap_command_reject_cid(l2cap, l2cap->last_id, L2CAP_REJ_CID_INVAL,
                        cid, 0x0000);
        return 0;
    }
    ch = l2cap->cid[cid];

    if (ch->config_req_id != l2cap->last_id)
        return 1;
    ch->config_req_id = 0;

    if (result == L2CAP_CONF_SUCCESS) {
        if (!flag)
            ch->config |= L2CAP_CFG_INIT;
        else
            l2cap_channel_config_null(l2cap, ch);
    } else
        /* Retry until we succeed */
        l2cap_channel_config_req_event(l2cap, ch);

    return 0;
}

static void l2cap_channel_open_req_msg(struct l2cap_instance_s *l2cap,
                int psm, int source_cid)
{
    struct l2cap_chan_s *ch = l2cap_channel_open(l2cap, psm, source_cid);

    if (!ch)
        return;

    /* Optional */
    if (!(ch->config & L2CAP_CFG_INIT) && !ch->config_req_id)
        l2cap_channel_config_req_event(l2cap, ch);
}

static void l2cap_info(struct l2cap_instance_s *l2cap, int type)
{
    uint8_t data[4];
    int len = 0;
    int result = L2CAP_IR_SUCCESS;

    switch (type) {
    case L2CAP_IT_CL_MTU:
        data[len ++] = l2cap->group_ch.mps & 0xff;
        data[len ++] = l2cap->group_ch.mps >> 8;
        break;

    case L2CAP_IT_FEAT_MASK:
        /* (Prematurely) report Flow control and Retransmission modes.  */
        data[len ++] = 0x03;
        data[len ++] = 0x00;
        data[len ++] = 0x00;
        data[len ++] = 0x00;
        break;

    default:
        result = L2CAP_IR_NOTSUPP;
    }

    l2cap_info_response(l2cap, type, result, data, len);
}

static void l2cap_command(struct l2cap_instance_s *l2cap, int code, int id,
                const uint8_t *params, int len)
{
    int err;

#if 0
    /* TODO: do the IDs really have to be in sequence?  */
    if (!id || (id != l2cap->last_id && id != l2cap->next_id)) {
        fprintf(stderr, "%s: out of sequence command packet ignored.\n",
                        __FUNCTION__);
        return;
    }
#else
    l2cap->next_id = id;
#endif
    if (id == l2cap->next_id) {
        l2cap->last_id = l2cap->next_id;
        l2cap->next_id = l2cap->next_id == 255 ? 1 : l2cap->next_id + 1;
    } else {
        /* TODO: Need to re-send the same response, without re-executing
         * the corresponding command!  */
    }

    switch (code) {
    case L2CAP_COMMAND_REJ:
        if (unlikely(len != 2 && len != 4 && len != 6)) {
            err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
            goto reject;
        }

        /* We never issue commands other than Command Reject currently.  */
        fprintf(stderr, "%s: stray Command Reject (%02x, %04x) "
                        "packet, ignoring.\n", __FUNCTION__, id,
                        le16_to_cpu(((l2cap_cmd_rej *) params)->reason));
        break;

    case L2CAP_CONN_REQ:
        if (unlikely(len != L2CAP_CONN_REQ_SIZE)) {
            err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
            goto reject;
        }

        l2cap_channel_open_req_msg(l2cap,
                        le16_to_cpu(((l2cap_conn_req *) params)->psm),
                        le16_to_cpu(((l2cap_conn_req *) params)->scid));
        break;

    case L2CAP_CONN_RSP:
        if (unlikely(len != L2CAP_CONN_RSP_SIZE)) {
            err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
            goto reject;
        }

        /* We never issue Connection Requests currently. TODO  */
        fprintf(stderr, "%s: unexpected Connection Response (%02x) "
                        "packet, ignoring.\n", __FUNCTION__, id);
        break;

    case L2CAP_CONF_REQ:
        if (unlikely(len < L2CAP_CONF_REQ_SIZE(0))) {
            err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
            goto reject;
        }

        l2cap_channel_config_req_msg(l2cap,
                        le16_to_cpu(((l2cap_conf_req *) params)->flags) & 1,
                        le16_to_cpu(((l2cap_conf_req *) params)->dcid),
                        ((l2cap_conf_req *) params)->data,
                        len - L2CAP_CONF_REQ_SIZE(0));
        break;

    case L2CAP_CONF_RSP:
        if (unlikely(len < L2CAP_CONF_RSP_SIZE(0))) {
            err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
            goto reject;
        }

        if (l2cap_channel_config_rsp_msg(l2cap,
                        le16_to_cpu(((l2cap_conf_rsp *) params)->result),
                        le16_to_cpu(((l2cap_conf_rsp *) params)->flags) & 1,
                        le16_to_cpu(((l2cap_conf_rsp *) params)->scid),
                        ((l2cap_conf_rsp *) params)->data,
                        len - L2CAP_CONF_RSP_SIZE(0)))
            fprintf(stderr, "%s: unexpected Configure Response (%02x) "
                            "packet, ignoring.\n", __FUNCTION__, id);
        break;

    case L2CAP_DISCONN_REQ:
        if (unlikely(len != L2CAP_DISCONN_REQ_SIZE)) {
            err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
            goto reject;
        }

        l2cap_channel_close(l2cap,
                        le16_to_cpu(((l2cap_disconn_req *) params)->dcid),
                        le16_to_cpu(((l2cap_disconn_req *) params)->scid));
        break;

    case L2CAP_DISCONN_RSP:
        if (unlikely(len != L2CAP_DISCONN_RSP_SIZE)) {
            err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
            goto reject;
        }

        /* We never issue Disconnection Requests currently. TODO  */
        fprintf(stderr, "%s: unexpected Disconnection Response (%02x) "
                        "packet, ignoring.\n", __FUNCTION__, id);
        break;

    case L2CAP_ECHO_REQ:
        l2cap_echo_response(l2cap, params, len);
        break;

    case L2CAP_ECHO_RSP:
        /* We never issue Echo Requests currently. TODO  */
        fprintf(stderr, "%s: unexpected Echo Response (%02x) "
                        "packet, ignoring.\n", __FUNCTION__, id);
        break;

    case L2CAP_INFO_REQ:
        if (unlikely(len != L2CAP_INFO_REQ_SIZE)) {
            err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
            goto reject;
        }

        l2cap_info(l2cap, le16_to_cpu(((l2cap_info_req *) params)->type));
        break;

    case L2CAP_INFO_RSP:
        if (unlikely(len != L2CAP_INFO_RSP_SIZE)) {
            err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
            goto reject;
        }

        /* We never issue Information Requests currently. TODO  */
        fprintf(stderr, "%s: unexpected Information Response (%02x) "
                        "packet, ignoring.\n", __FUNCTION__, id);
        break;

    default:
        err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
    reject:
        l2cap_command_reject(l2cap, id, err, 0, 0);
        break;
    }
}

static void l2cap_rexmit_enable(struct l2cap_chan_s *ch, int enable)
{
    ch->rexmit = enable;

    l2cap_retransmission_timer_update(ch);
    l2cap_monitor_timer_update(ch);
}

/* Command frame SDU */
static void l2cap_cframe_in(void *opaque, const uint8_t *data, int len)
{
    struct l2cap_instance_s *l2cap = opaque;
    const l2cap_cmd_hdr *hdr;
    int clen;

    while (len) {
        hdr = (void *) data;
        if (len < L2CAP_CMD_HDR_SIZE)
            /* TODO: signal an error */
            return;
        len -= L2CAP_CMD_HDR_SIZE;
        data += L2CAP_CMD_HDR_SIZE;

        clen = le16_to_cpu(hdr->len);
        if (len < clen) {
            l2cap_command_reject(l2cap, hdr->ident,
                            L2CAP_REJ_CMD_NOT_UNDERSTOOD, 0, 0);
            break;
        }

        l2cap_command(l2cap, hdr->code, hdr->ident, data, clen);
        len -= clen;
        data += clen;
    }
}

/* Group frame SDU */
static void l2cap_gframe_in(void *opaque, const uint8_t *data, int len)
{
}

/* Supervisory frame */
static void l2cap_sframe_in(struct l2cap_chan_s *ch, uint16_t ctrl)
{
}

/* Basic L2CAP mode Information frame */
static void l2cap_bframe_in(struct l2cap_chan_s *ch, uint16_t cid,
                const l2cap_hdr *hdr, int len)
{
    /* We have a full SDU, no further processing */
    ch->params.sdu_in(ch->params.opaque, hdr->data, len);
}

/* Flow Control and Retransmission mode frame */
static void l2cap_iframe_in(struct l2cap_chan_s *ch, uint16_t cid,
                const l2cap_hdr *hdr, int len)
{
    uint16_t fcs = le16_to_cpup((void *) (hdr->data + len - 2));

    if (len < 4)
        goto len_error;
    if (l2cap_fcs16((const uint8_t *) hdr, L2CAP_HDR_SIZE + len - 2) != fcs)
        goto fcs_error;

    if ((hdr->data[0] >> 7) == ch->rexmit)
        l2cap_rexmit_enable(ch, !(hdr->data[0] >> 7));

    if (hdr->data[0] & 1) {
        if (len != 4) {
            /* TODO: Signal an error? */
            return;
        }
        l2cap_sframe_in(ch, le16_to_cpup((void *) hdr->data));
        return;
    }

    switch (hdr->data[1] >> 6) {	/* SAR */
    case L2CAP_SAR_NO_SEG:
        if (ch->len_total)
            goto seg_error;
        if (len - 4 > ch->mps)
            goto len_error;

        ch->params.sdu_in(ch->params.opaque, hdr->data + 2, len - 4);
        break;

    case L2CAP_SAR_START:
        if (ch->len_total || len < 6)
            goto seg_error;
        if (len - 6 > ch->mps)
            goto len_error;

        ch->len_total = le16_to_cpup((void *) (hdr->data + 2));
        if (len >= 6 + ch->len_total)
            goto seg_error;

        ch->len_cur = len - 6;
        memcpy(ch->sdu, hdr->data + 4, ch->len_cur);
        break;

    case L2CAP_SAR_END:
        if (!ch->len_total || ch->len_cur + len - 4 < ch->len_total)
            goto seg_error;
        if (len - 4 > ch->mps)
            goto len_error;

        memcpy(ch->sdu + ch->len_cur, hdr->data + 2, len - 4);
        ch->params.sdu_in(ch->params.opaque, ch->sdu, ch->len_total);
        break;

    case L2CAP_SAR_CONT:
        if (!ch->len_total || ch->len_cur + len - 4 >= ch->len_total)
            goto seg_error;
        if (len - 4 > ch->mps)
            goto len_error;

        memcpy(ch->sdu + ch->len_cur, hdr->data + 2, len - 4);
        ch->len_cur += len - 4;
        break;

    seg_error:
    len_error:	/* TODO */
    fcs_error:	/* TODO */
        ch->len_cur = 0;
        ch->len_total = 0;
        break;
    }
}

static void l2cap_frame_in(struct l2cap_instance_s *l2cap,
                const l2cap_hdr *frame)
{
    uint16_t cid = le16_to_cpu(frame->cid);
    uint16_t len = le16_to_cpu(frame->len);

    if (unlikely(cid >= L2CAP_CID_MAX || !l2cap->cid[cid])) {
        fprintf(stderr, "%s: frame addressed to a non-existent L2CAP "
                        "channel %04x received.\n", __FUNCTION__, cid);
        return;
    }

    l2cap->cid[cid]->frame_in(l2cap->cid[cid], cid, frame, len);
}

/* "Recombination" */
static void l2cap_pdu_in(struct l2cap_instance_s *l2cap,
                const uint8_t *data, int len)
{
    const l2cap_hdr *hdr = (void *) l2cap->frame_in;

    if (unlikely(len + l2cap->frame_in_len > sizeof(l2cap->frame_in))) {
        if (l2cap->frame_in_len < sizeof(l2cap->frame_in)) {
            memcpy(l2cap->frame_in + l2cap->frame_in_len, data,
                            sizeof(l2cap->frame_in) - l2cap->frame_in_len);
            l2cap->frame_in_len = sizeof(l2cap->frame_in);
            /* TODO: truncate */
            l2cap_frame_in(l2cap, hdr);
        }

        return;
    }

    memcpy(l2cap->frame_in + l2cap->frame_in_len, data, len);
    l2cap->frame_in_len += len;

    if (len >= L2CAP_HDR_SIZE)
        if (len >= L2CAP_HDR_SIZE + le16_to_cpu(hdr->len))
            l2cap_frame_in(l2cap, hdr);
            /* There is never a start of a new PDU in the same ACL packet, so
             * no need to memmove the remaining payload and loop.  */
}

static inline uint8_t *l2cap_pdu_out(struct l2cap_instance_s *l2cap,
                uint16_t cid, uint16_t len)
{
    l2cap_hdr *hdr = (void *) l2cap->frame_out;

    l2cap->frame_out_len = len + L2CAP_HDR_SIZE;

    hdr->cid = cpu_to_le16(cid);
    hdr->len = cpu_to_le16(len);

    return l2cap->frame_out + L2CAP_HDR_SIZE;
}

static inline void l2cap_pdu_submit(struct l2cap_instance_s *l2cap)
{
    /* TODO: Fragmentation */
    (l2cap->role ?
     l2cap->link->slave->lmp_acl_data : l2cap->link->host->lmp_acl_resp)
            (l2cap->link, l2cap->frame_out, 1, l2cap->frame_out_len);
}

static uint8_t *l2cap_bframe_out(struct bt_l2cap_conn_params_s *parm, int len)
{
    struct l2cap_chan_s *chan = (struct l2cap_chan_s *) parm;

    if (len > chan->params.remote_mtu) {
        fprintf(stderr, "%s: B-Frame for CID %04x longer than %i octets.\n",
                        __FUNCTION__,
                        chan->remote_cid, chan->params.remote_mtu);
        exit(-1);
    }

    return l2cap_pdu_out(chan->l2cap, chan->remote_cid, len);
}

static void l2cap_bframe_submit(struct bt_l2cap_conn_params_s *parms)
{
    struct l2cap_chan_s *chan = (struct l2cap_chan_s *) parms;

    l2cap_pdu_submit(chan->l2cap);
}

#if 0
/* Stub: Only used if an emulated device requests outgoing flow control */
static uint8_t *l2cap_iframe_out(struct bt_l2cap_conn_params_s *parm, int len)
{
    struct l2cap_chan_s *chan = (struct l2cap_chan_s *) parm;

    if (len > chan->params.remote_mtu) {
        /* TODO: slice into segments and queue each segment as a separate
         * I-Frame in a FIFO of I-Frames, local to the CID.  */
    } else {
        /* TODO: add to the FIFO of I-Frames, local to the CID.  */
        /* Possibly we need to return a pointer to a contiguous buffer
         * for now and then memcpy from it into FIFOs in l2cap_iframe_submit
         * while segmenting at the same time.  */
    }
    return 0;
}

static void l2cap_iframe_submit(struct bt_l2cap_conn_params_s *parm)
{
    /* TODO: If flow control indicates clear to send, start submitting the
     * invidual I-Frames from the FIFO, but don't remove them from there.
     * Kick the appropriate timer until we get an S-Frame, and only then
     * remove from FIFO or resubmit and re-kick the timer if the timer
     * expired.  */
}
#endif

static void l2cap_init(struct l2cap_instance_s *l2cap,
                struct bt_link_s *link, int role)
{
    l2cap->link = link;
    l2cap->role = role;
    l2cap->dev = (struct bt_l2cap_device_s *)
            (role ? link->host : link->slave);

    l2cap->next_id = 1;

    /* Establish the signalling channel */
    l2cap->signalling_ch.params.sdu_in = l2cap_cframe_in;
    l2cap->signalling_ch.params.sdu_out = l2cap_bframe_out;
    l2cap->signalling_ch.params.sdu_submit = l2cap_bframe_submit;
    l2cap->signalling_ch.params.opaque = l2cap;
    l2cap->signalling_ch.params.remote_mtu = 48;
    l2cap->signalling_ch.remote_cid = L2CAP_CID_SIGNALLING;
    l2cap->signalling_ch.frame_in = l2cap_bframe_in;
    l2cap->signalling_ch.mps = 65536;
    l2cap->signalling_ch.min_mtu = 48;
    l2cap->signalling_ch.mode = L2CAP_MODE_BASIC;
    l2cap->signalling_ch.l2cap = l2cap;
    l2cap->cid[L2CAP_CID_SIGNALLING] = &l2cap->signalling_ch;

    /* Establish the connection-less data channel */
    l2cap->group_ch.params.sdu_in = l2cap_gframe_in;
    l2cap->group_ch.params.opaque = l2cap;
    l2cap->group_ch.frame_in = l2cap_bframe_in;
    l2cap->group_ch.mps = 65533;
    l2cap->group_ch.l2cap = l2cap;
    l2cap->group_ch.remote_cid = L2CAP_CID_INVALID;
    l2cap->cid[L2CAP_CID_GROUP] = &l2cap->group_ch;
}

static void l2cap_teardown(struct l2cap_instance_s *l2cap, int send_disconnect)
{
    int cid;

    /* Don't send DISCONNECT if we are currently handling a DISCONNECT
     * sent from the other side.  */
    if (send_disconnect) {
        if (l2cap->role)
            l2cap->dev->device.lmp_disconnect_slave(l2cap->link);
            /* l2cap->link is invalid from now on.  */
        else
            l2cap->dev->device.lmp_disconnect_master(l2cap->link);
    }

    for (cid = L2CAP_CID_ALLOC; cid < L2CAP_CID_MAX; cid ++)
        if (l2cap->cid[cid]) {
            l2cap->cid[cid]->params.close(l2cap->cid[cid]->params.opaque);
            g_free(l2cap->cid[cid]);
        }

    if (l2cap->role)
        g_free(l2cap);
    else
        g_free(l2cap->link);
}

/* L2CAP glue to lower layers in bluetooth stack (LMP) */

static void l2cap_lmp_connection_request(struct bt_link_s *link)
{
    struct bt_l2cap_device_s *dev = (struct bt_l2cap_device_s *) link->slave;
    struct slave_l2cap_instance_s *l2cap;

    /* Always accept - we only get called if (dev->device->page_scan).  */

    l2cap = g_malloc0(sizeof(struct slave_l2cap_instance_s));
    l2cap->link.slave = &dev->device;
    l2cap->link.host = link->host;
    l2cap_init(&l2cap->l2cap, &l2cap->link, 0);

    /* Always at the end */
    link->host->reject_reason = 0;
    link->host->lmp_connection_complete(&l2cap->link);
}

/* Stub */
static void l2cap_lmp_connection_complete(struct bt_link_s *link)
{
    struct bt_l2cap_device_s *dev = (struct bt_l2cap_device_s *) link->host;
    struct l2cap_instance_s *l2cap;

    if (dev->device.reject_reason) {
        /* Signal to upper layer */
        return;
    }

    l2cap = g_malloc0(sizeof(struct l2cap_instance_s));
    l2cap_init(l2cap, link, 1);

    link->acl_mode = acl_active;

    /* Signal to upper layer */
}

/* Stub */
static void l2cap_lmp_disconnect_host(struct bt_link_s *link)
{
    struct bt_l2cap_device_s *dev = (struct bt_l2cap_device_s *) link->host;
    struct l2cap_instance_s *l2cap =
            /* TODO: Retrieve from upper layer */ (void *) dev;

    /* Signal to upper layer */

    l2cap_teardown(l2cap, 0);
}

static void l2cap_lmp_disconnect_slave(struct bt_link_s *link)
{
    struct slave_l2cap_instance_s *l2cap =
            (struct slave_l2cap_instance_s *) link;

    l2cap_teardown(&l2cap->l2cap, 0);
}

static void l2cap_lmp_acl_data_slave(struct bt_link_s *link,
                const uint8_t *data, int start, int len)
{
    struct slave_l2cap_instance_s *l2cap =
            (struct slave_l2cap_instance_s *) link;

    if (start)
        l2cap->l2cap.frame_in_len = 0;

    l2cap_pdu_in(&l2cap->l2cap, data, len);
}

/* Stub */
static void l2cap_lmp_acl_data_host(struct bt_link_s *link,
                const uint8_t *data, int start, int len)
{
    struct bt_l2cap_device_s *dev = (struct bt_l2cap_device_s *) link->host;
    struct l2cap_instance_s *l2cap =
            /* TODO: Retrieve from upper layer */ (void *) dev;

    if (start)
        l2cap->frame_in_len = 0;

    l2cap_pdu_in(l2cap, data, len);
}

static void l2cap_dummy_destroy(struct bt_device_s *dev)
{
    struct bt_l2cap_device_s *l2cap_dev = (struct bt_l2cap_device_s *) dev;

    bt_l2cap_device_done(l2cap_dev);
}

void bt_l2cap_device_init(struct bt_l2cap_device_s *dev,
                struct bt_scatternet_s *net)
{
    bt_device_init(&dev->device, net);

    dev->device.lmp_connection_request = l2cap_lmp_connection_request;
    dev->device.lmp_connection_complete = l2cap_lmp_connection_complete;
    dev->device.lmp_disconnect_master = l2cap_lmp_disconnect_host;
    dev->device.lmp_disconnect_slave = l2cap_lmp_disconnect_slave;
    dev->device.lmp_acl_data = l2cap_lmp_acl_data_slave;
    dev->device.lmp_acl_resp = l2cap_lmp_acl_data_host;

    dev->device.handle_destroy = l2cap_dummy_destroy;
}

void bt_l2cap_device_done(struct bt_l2cap_device_s *dev)
{
    bt_device_done(&dev->device);

    /* Should keep a list of all instances and go through it and
     * invoke l2cap_teardown() for each.  */
}

void bt_l2cap_psm_register(struct bt_l2cap_device_s *dev, int psm, int min_mtu,
                int (*new_channel)(struct bt_l2cap_device_s *dev,
                        struct bt_l2cap_conn_params_s *params))
{
    struct bt_l2cap_psm_s *new_psm = l2cap_psm(dev, psm);

    if (new_psm) {
        fprintf(stderr, "%s: PSM %04x already registered for device `%s'.\n",
                        __FUNCTION__, psm, dev->device.lmp_name);
        exit(-1);
    }

    new_psm = g_malloc0(sizeof(*new_psm));
    new_psm->psm = psm;
    new_psm->min_mtu = min_mtu;
    new_psm->new_channel = new_channel;
    new_psm->next = dev->first_psm;
    dev->first_psm = new_psm;
}
