2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2010 Nokia Corporation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
23 /* Bluetooth HCI Management interface */
25 #include <asm/uaccess.h>
26 #include <asm/unaligned.h>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
30 #include <net/bluetooth/mgmt.h>
32 #define MGMT_VERSION 0
33 #define MGMT_REVISION 1
35 static int read_version(struct sock *sk)
39 struct mgmt_ev_cmd_complete *ev;
40 struct mgmt_rp_read_version *rp;
42 BT_DBG("sock %p", sk);
44 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + sizeof(*rp), GFP_ATOMIC);
48 hdr = (void *) skb_put(skb, sizeof(*hdr));
49 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
50 hdr->len = cpu_to_le16(sizeof(*ev) + sizeof(*rp));
52 ev = (void *) skb_put(skb, sizeof(*ev));
53 put_unaligned_le16(MGMT_OP_READ_VERSION, &ev->opcode);
55 rp = (void *) skb_put(skb, sizeof(*rp));
56 rp->version = MGMT_VERSION;
57 put_unaligned_le16(MGMT_REVISION, &rp->revision);
59 if (sock_queue_rcv_skb(sk, skb) < 0)
65 static int cmd_status(struct sock *sk, u16 cmd, u8 status)
69 struct mgmt_ev_cmd_status *ev;
71 BT_DBG("sock %p", sk);
73 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC);
77 hdr = (void *) skb_put(skb, sizeof(*hdr));
79 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
80 hdr->len = cpu_to_le16(sizeof(*ev));
82 ev = (void *) skb_put(skb, sizeof(*ev));
84 put_unaligned_le16(cmd, &ev->opcode);
86 if (sock_queue_rcv_skb(sk, skb) < 0)
92 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
99 BT_DBG("got %zu bytes", msglen);
101 if (msglen < sizeof(*hdr))
104 buf = kmalloc(msglen, GFP_ATOMIC);
108 if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) {
113 hdr = (struct mgmt_hdr *) buf;
114 opcode = get_unaligned_le16(&hdr->opcode);
115 len = get_unaligned_le16(&hdr->len);
117 if (len != msglen - sizeof(*hdr)) {
123 case MGMT_OP_READ_VERSION:
124 err = read_version(sk);
127 BT_DBG("Unknown op %u", opcode);
128 err = cmd_status(sk, opcode, 0x01);