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 read_index_list(struct sock *sk)
69 struct mgmt_ev_cmd_complete *ev;
70 struct mgmt_rp_read_index_list *rp;
76 BT_DBG("sock %p", sk);
78 read_lock(&hci_dev_list_lock);
81 list_for_each(p, &hci_dev_list) {
85 body_len = sizeof(*ev) + sizeof(*rp) + (2 * count);
86 skb = alloc_skb(sizeof(*hdr) + body_len, GFP_ATOMIC);
90 hdr = (void *) skb_put(skb, sizeof(*hdr));
91 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
92 hdr->len = cpu_to_le16(body_len);
94 ev = (void *) skb_put(skb, sizeof(*ev));
95 put_unaligned_le16(MGMT_OP_READ_INDEX_LIST, &ev->opcode);
97 rp = (void *) skb_put(skb, sizeof(*rp) + (2 * count));
98 put_unaligned_le16(count, &rp->num_controllers);
101 list_for_each(p, &hci_dev_list) {
102 struct hci_dev *d = list_entry(p, struct hci_dev, list);
103 put_unaligned_le16(d->id, &rp->index[i++]);
104 BT_DBG("Added hci%u", d->id);
107 read_unlock(&hci_dev_list_lock);
109 if (sock_queue_rcv_skb(sk, skb) < 0)
115 static int cmd_status(struct sock *sk, u16 cmd, u8 status)
118 struct mgmt_hdr *hdr;
119 struct mgmt_ev_cmd_status *ev;
121 BT_DBG("sock %p", sk);
123 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC);
127 hdr = (void *) skb_put(skb, sizeof(*hdr));
129 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
130 hdr->len = cpu_to_le16(sizeof(*ev));
132 ev = (void *) skb_put(skb, sizeof(*ev));
134 put_unaligned_le16(cmd, &ev->opcode);
136 if (sock_queue_rcv_skb(sk, skb) < 0)
142 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
145 struct mgmt_hdr *hdr;
149 BT_DBG("got %zu bytes", msglen);
151 if (msglen < sizeof(*hdr))
154 buf = kmalloc(msglen, GFP_ATOMIC);
158 if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) {
163 hdr = (struct mgmt_hdr *) buf;
164 opcode = get_unaligned_le16(&hdr->opcode);
165 len = get_unaligned_le16(&hdr->len);
167 if (len != msglen - sizeof(*hdr)) {
173 case MGMT_OP_READ_VERSION:
174 err = read_version(sk);
176 case MGMT_OP_READ_INDEX_LIST:
177 err = read_index_list(sk);
180 BT_DBG("Unknown op %u", opcode);
181 err = cmd_status(sk, opcode, 0x01);