aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSzymon Janc <szymon.janc@tieto.com>2011-02-25 13:05:48 -0500
committerGustavo F. Padovan <padovan@profusion.mobi>2011-02-27 14:56:41 -0500
commit4e51eae9cdda4bf096e73a4ebe23f8f96a17596a (patch)
treebcc268f23f386018b86ce7fe7094f59904e20b64
parent779cb85016587d9ffaea681c38691d5301a3fedc (diff)
Bluetooth: Move index to common header in management interface
Most mgmt commands and event are related to hci adapter. Moving index to common header allow to easily use it in command status while reporting errors. For those not related to adapter use MGMT_INDEX_NONE (0xFFFF) as index. Signed-off-by: Szymon Janc <szymon.janc@tieto.com> Acked-by: Johan Hedberg <johan.hedberg@nokia.com> Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
-rw-r--r--include/net/bluetooth/mgmt.h43
-rw-r--r--net/bluetooth/mgmt.c407
2 files changed, 183 insertions, 267 deletions
diff --git a/include/net/bluetooth/mgmt.h b/include/net/bluetooth/mgmt.h
index 1e63c3141a78..5fabfa886b3e 100644
--- a/include/net/bluetooth/mgmt.h
+++ b/include/net/bluetooth/mgmt.h
@@ -21,11 +21,13 @@
21 SOFTWARE IS DISCLAIMED. 21 SOFTWARE IS DISCLAIMED.
22*/ 22*/
23 23
24#define MGMT_INDEX_NONE 0xFFFF
25
24struct mgmt_hdr { 26struct mgmt_hdr {
25 __le16 opcode; 27 __le16 opcode;
28 __le16 index;
26 __le16 len; 29 __le16 len;
27} __packed; 30} __packed;
28#define MGMT_HDR_SIZE 4
29 31
30#define MGMT_OP_READ_VERSION 0x0001 32#define MGMT_OP_READ_VERSION 0x0001
31struct mgmt_rp_read_version { 33struct mgmt_rp_read_version {
@@ -40,11 +42,7 @@ struct mgmt_rp_read_index_list {
40} __packed; 42} __packed;
41 43
42#define MGMT_OP_READ_INFO 0x0004 44#define MGMT_OP_READ_INFO 0x0004
43struct mgmt_cp_read_info {
44 __le16 index;
45} __packed;
46struct mgmt_rp_read_info { 45struct mgmt_rp_read_info {
47 __le16 index;
48 __u8 type; 46 __u8 type;
49 __u8 powered; 47 __u8 powered;
50 __u8 connectable; 48 __u8 connectable;
@@ -60,7 +58,6 @@ struct mgmt_rp_read_info {
60} __packed; 58} __packed;
61 59
62struct mgmt_mode { 60struct mgmt_mode {
63 __le16 index;
64 __u8 val; 61 __u8 val;
65} __packed; 62} __packed;
66 63
@@ -74,27 +71,23 @@ struct mgmt_mode {
74 71
75#define MGMT_OP_ADD_UUID 0x0009 72#define MGMT_OP_ADD_UUID 0x0009
76struct mgmt_cp_add_uuid { 73struct mgmt_cp_add_uuid {
77 __le16 index;
78 __u8 uuid[16]; 74 __u8 uuid[16];
79 __u8 svc_hint; 75 __u8 svc_hint;
80} __packed; 76} __packed;
81 77
82#define MGMT_OP_REMOVE_UUID 0x000A 78#define MGMT_OP_REMOVE_UUID 0x000A
83struct mgmt_cp_remove_uuid { 79struct mgmt_cp_remove_uuid {
84 __le16 index;
85 __u8 uuid[16]; 80 __u8 uuid[16];
86} __packed; 81} __packed;
87 82
88#define MGMT_OP_SET_DEV_CLASS 0x000B 83#define MGMT_OP_SET_DEV_CLASS 0x000B
89struct mgmt_cp_set_dev_class { 84struct mgmt_cp_set_dev_class {
90 __le16 index;
91 __u8 major; 85 __u8 major;
92 __u8 minor; 86 __u8 minor;
93} __packed; 87} __packed;
94 88
95#define MGMT_OP_SET_SERVICE_CACHE 0x000C 89#define MGMT_OP_SET_SERVICE_CACHE 0x000C
96struct mgmt_cp_set_service_cache { 90struct mgmt_cp_set_service_cache {
97 __le16 index;
98 __u8 enable; 91 __u8 enable;
99} __packed; 92} __packed;
100 93
@@ -107,7 +100,6 @@ struct mgmt_key_info {
107 100
108#define MGMT_OP_LOAD_KEYS 0x000D 101#define MGMT_OP_LOAD_KEYS 0x000D
109struct mgmt_cp_load_keys { 102struct mgmt_cp_load_keys {
110 __le16 index;
111 __u8 debug_keys; 103 __u8 debug_keys;
112 __le16 key_count; 104 __le16 key_count;
113 struct mgmt_key_info keys[0]; 105 struct mgmt_key_info keys[0];
@@ -115,75 +107,60 @@ struct mgmt_cp_load_keys {
115 107
116#define MGMT_OP_REMOVE_KEY 0x000E 108#define MGMT_OP_REMOVE_KEY 0x000E
117struct mgmt_cp_remove_key { 109struct mgmt_cp_remove_key {
118 __le16 index;
119 bdaddr_t bdaddr; 110 bdaddr_t bdaddr;
120 __u8 disconnect; 111 __u8 disconnect;
121} __packed; 112} __packed;
122 113
123#define MGMT_OP_DISCONNECT 0x000F 114#define MGMT_OP_DISCONNECT 0x000F
124struct mgmt_cp_disconnect { 115struct mgmt_cp_disconnect {
125 __le16 index;
126 bdaddr_t bdaddr; 116 bdaddr_t bdaddr;
127} __packed; 117} __packed;
128struct mgmt_rp_disconnect { 118struct mgmt_rp_disconnect {
129 __le16 index;
130 bdaddr_t bdaddr; 119 bdaddr_t bdaddr;
131} __packed; 120} __packed;
132 121
133#define MGMT_OP_GET_CONNECTIONS 0x0010 122#define MGMT_OP_GET_CONNECTIONS 0x0010
134struct mgmt_cp_get_connections {
135 __le16 index;
136} __packed;
137struct mgmt_rp_get_connections { 123struct mgmt_rp_get_connections {
138 __le16 index;
139 __le16 conn_count; 124 __le16 conn_count;
140 bdaddr_t conn[0]; 125 bdaddr_t conn[0];
141} __packed; 126} __packed;
142 127
143#define MGMT_OP_PIN_CODE_REPLY 0x0011 128#define MGMT_OP_PIN_CODE_REPLY 0x0011
144struct mgmt_cp_pin_code_reply { 129struct mgmt_cp_pin_code_reply {
145 __le16 index;
146 bdaddr_t bdaddr; 130 bdaddr_t bdaddr;
147 __u8 pin_len; 131 __u8 pin_len;
148 __u8 pin_code[16]; 132 __u8 pin_code[16];
149} __packed; 133} __packed;
150struct mgmt_rp_pin_code_reply { 134struct mgmt_rp_pin_code_reply {
151 __le16 index;
152 bdaddr_t bdaddr; 135 bdaddr_t bdaddr;
153 uint8_t status; 136 uint8_t status;
154} __packed; 137} __packed;
155 138
156#define MGMT_OP_PIN_CODE_NEG_REPLY 0x0012 139#define MGMT_OP_PIN_CODE_NEG_REPLY 0x0012
157struct mgmt_cp_pin_code_neg_reply { 140struct mgmt_cp_pin_code_neg_reply {
158 __le16 index;
159 bdaddr_t bdaddr; 141 bdaddr_t bdaddr;
160} __packed; 142} __packed;
161 143
162#define MGMT_OP_SET_IO_CAPABILITY 0x0013 144#define MGMT_OP_SET_IO_CAPABILITY 0x0013
163struct mgmt_cp_set_io_capability { 145struct mgmt_cp_set_io_capability {
164 __le16 index;
165 __u8 io_capability; 146 __u8 io_capability;
166} __packed; 147} __packed;
167 148
168#define MGMT_OP_PAIR_DEVICE 0x0014 149#define MGMT_OP_PAIR_DEVICE 0x0014
169struct mgmt_cp_pair_device { 150struct mgmt_cp_pair_device {
170 __le16 index;
171 bdaddr_t bdaddr; 151 bdaddr_t bdaddr;
172 __u8 io_cap; 152 __u8 io_cap;
173} __packed; 153} __packed;
174struct mgmt_rp_pair_device { 154struct mgmt_rp_pair_device {
175 __le16 index;
176 bdaddr_t bdaddr; 155 bdaddr_t bdaddr;
177 __u8 status; 156 __u8 status;
178} __packed; 157} __packed;
179 158
180#define MGMT_OP_USER_CONFIRM_REPLY 0x0015 159#define MGMT_OP_USER_CONFIRM_REPLY 0x0015
181struct mgmt_cp_user_confirm_reply { 160struct mgmt_cp_user_confirm_reply {
182 __le16 index;
183 bdaddr_t bdaddr; 161 bdaddr_t bdaddr;
184} __packed; 162} __packed;
185struct mgmt_rp_user_confirm_reply { 163struct mgmt_rp_user_confirm_reply {
186 __le16 index;
187 bdaddr_t bdaddr; 164 bdaddr_t bdaddr;
188 __u8 status; 165 __u8 status;
189} __packed; 166} __packed;
@@ -204,19 +181,12 @@ struct mgmt_ev_cmd_status {
204 181
205#define MGMT_EV_CONTROLLER_ERROR 0x0003 182#define MGMT_EV_CONTROLLER_ERROR 0x0003
206struct mgmt_ev_controller_error { 183struct mgmt_ev_controller_error {
207 __le16 index;
208 __u8 error_code; 184 __u8 error_code;
209} __packed; 185} __packed;
210 186
211#define MGMT_EV_INDEX_ADDED 0x0004 187#define MGMT_EV_INDEX_ADDED 0x0004
212struct mgmt_ev_index_added {
213 __le16 index;
214} __packed;
215 188
216#define MGMT_EV_INDEX_REMOVED 0x0005 189#define MGMT_EV_INDEX_REMOVED 0x0005
217struct mgmt_ev_index_removed {
218 __le16 index;
219} __packed;
220 190
221#define MGMT_EV_POWERED 0x0006 191#define MGMT_EV_POWERED 0x0006
222 192
@@ -228,46 +198,39 @@ struct mgmt_ev_index_removed {
228 198
229#define MGMT_EV_NEW_KEY 0x000A 199#define MGMT_EV_NEW_KEY 0x000A
230struct mgmt_ev_new_key { 200struct mgmt_ev_new_key {
231 __le16 index;
232 struct mgmt_key_info key; 201 struct mgmt_key_info key;
233 __u8 old_key_type; 202 __u8 old_key_type;
234} __packed; 203} __packed;
235 204
236#define MGMT_EV_CONNECTED 0x000B 205#define MGMT_EV_CONNECTED 0x000B
237struct mgmt_ev_connected { 206struct mgmt_ev_connected {
238 __le16 index;
239 bdaddr_t bdaddr; 207 bdaddr_t bdaddr;
240} __packed; 208} __packed;
241 209
242#define MGMT_EV_DISCONNECTED 0x000C 210#define MGMT_EV_DISCONNECTED 0x000C
243struct mgmt_ev_disconnected { 211struct mgmt_ev_disconnected {
244 __le16 index;
245 bdaddr_t bdaddr; 212 bdaddr_t bdaddr;
246} __packed; 213} __packed;
247 214
248#define MGMT_EV_CONNECT_FAILED 0x000D 215#define MGMT_EV_CONNECT_FAILED 0x000D
249struct mgmt_ev_connect_failed { 216struct mgmt_ev_connect_failed {
250 __le16 index;
251 bdaddr_t bdaddr; 217 bdaddr_t bdaddr;
252 __u8 status; 218 __u8 status;
253} __packed; 219} __packed;
254 220
255#define MGMT_EV_PIN_CODE_REQUEST 0x000E 221#define MGMT_EV_PIN_CODE_REQUEST 0x000E
256struct mgmt_ev_pin_code_request { 222struct mgmt_ev_pin_code_request {
257 __le16 index;
258 bdaddr_t bdaddr; 223 bdaddr_t bdaddr;
259} __packed; 224} __packed;
260 225
261#define MGMT_EV_USER_CONFIRM_REQUEST 0x000F 226#define MGMT_EV_USER_CONFIRM_REQUEST 0x000F
262struct mgmt_ev_user_confirm_request { 227struct mgmt_ev_user_confirm_request {
263 __le16 index;
264 bdaddr_t bdaddr; 228 bdaddr_t bdaddr;
265 __le32 value; 229 __le32 value;
266} __packed; 230} __packed;
267 231
268#define MGMT_EV_AUTH_FAILED 0x0010 232#define MGMT_EV_AUTH_FAILED 0x0010
269struct mgmt_ev_auth_failed { 233struct mgmt_ev_auth_failed {
270 __le16 index;
271 bdaddr_t bdaddr; 234 bdaddr_t bdaddr;
272 __u8 status; 235 __u8 status;
273} __packed; 236} __packed;
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 4543ede4ddf3..98c92aee6239 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -43,7 +43,7 @@ struct pending_cmd {
43 43
44LIST_HEAD(cmd_list); 44LIST_HEAD(cmd_list);
45 45
46static int cmd_status(struct sock *sk, u16 cmd, u8 status) 46static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
47{ 47{
48 struct sk_buff *skb; 48 struct sk_buff *skb;
49 struct mgmt_hdr *hdr; 49 struct mgmt_hdr *hdr;
@@ -58,6 +58,7 @@ static int cmd_status(struct sock *sk, u16 cmd, u8 status)
58 hdr = (void *) skb_put(skb, sizeof(*hdr)); 58 hdr = (void *) skb_put(skb, sizeof(*hdr));
59 59
60 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); 60 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
61 hdr->index = cpu_to_le16(index);
61 hdr->len = cpu_to_le16(sizeof(*ev)); 62 hdr->len = cpu_to_le16(sizeof(*ev));
62 63
63 ev = (void *) skb_put(skb, sizeof(*ev)); 64 ev = (void *) skb_put(skb, sizeof(*ev));
@@ -70,7 +71,8 @@ static int cmd_status(struct sock *sk, u16 cmd, u8 status)
70 return 0; 71 return 0;
71} 72}
72 73
73static int cmd_complete(struct sock *sk, u16 cmd, void *rp, size_t rp_len) 74static int cmd_complete(struct sock *sk, u16 index, u16 cmd, void *rp,
75 size_t rp_len)
74{ 76{
75 struct sk_buff *skb; 77 struct sk_buff *skb;
76 struct mgmt_hdr *hdr; 78 struct mgmt_hdr *hdr;
@@ -85,6 +87,7 @@ static int cmd_complete(struct sock *sk, u16 cmd, void *rp, size_t rp_len)
85 hdr = (void *) skb_put(skb, sizeof(*hdr)); 87 hdr = (void *) skb_put(skb, sizeof(*hdr));
86 88
87 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); 89 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
90 hdr->index = cpu_to_le16(index);
88 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); 91 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
89 92
90 ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); 93 ev = (void *) skb_put(skb, sizeof(*ev) + rp_len);
@@ -106,7 +109,8 @@ static int read_version(struct sock *sk)
106 rp.version = MGMT_VERSION; 109 rp.version = MGMT_VERSION;
107 put_unaligned_le16(MGMT_REVISION, &rp.revision); 110 put_unaligned_le16(MGMT_REVISION, &rp.revision);
108 111
109 return cmd_complete(sk, MGMT_OP_READ_VERSION, &rp, sizeof(rp)); 112 return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, &rp,
113 sizeof(rp));
110} 114}
111 115
112static int read_index_list(struct sock *sk) 116static int read_index_list(struct sock *sk)
@@ -152,32 +156,24 @@ static int read_index_list(struct sock *sk)
152 156
153 read_unlock(&hci_dev_list_lock); 157 read_unlock(&hci_dev_list_lock);
154 158
155 err = cmd_complete(sk, MGMT_OP_READ_INDEX_LIST, rp, rp_len); 159 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, rp,
160 rp_len);
156 161
157 kfree(rp); 162 kfree(rp);
158 163
159 return err; 164 return err;
160} 165}
161 166
162static int read_controller_info(struct sock *sk, unsigned char *data, u16 len) 167static int read_controller_info(struct sock *sk, u16 index)
163{ 168{
164 struct mgmt_rp_read_info rp; 169 struct mgmt_rp_read_info rp;
165 struct mgmt_cp_read_info *cp = (void *) data;
166 struct hci_dev *hdev; 170 struct hci_dev *hdev;
167 u16 dev_id;
168 171
169 BT_DBG("sock %p", sk); 172 BT_DBG("sock %p hci%u", sk, index);
170
171 if (len != 2)
172 return cmd_status(sk, MGMT_OP_READ_INFO, EINVAL);
173
174 dev_id = get_unaligned_le16(&cp->index);
175 173
176 BT_DBG("request for hci%u", dev_id); 174 hdev = hci_dev_get(index);
177
178 hdev = hci_dev_get(dev_id);
179 if (!hdev) 175 if (!hdev)
180 return cmd_status(sk, MGMT_OP_READ_INFO, ENODEV); 176 return cmd_status(sk, index, MGMT_OP_READ_INFO, ENODEV);
181 177
182 hci_del_off_timer(hdev); 178 hci_del_off_timer(hdev);
183 179
@@ -185,7 +181,6 @@ static int read_controller_info(struct sock *sk, unsigned char *data, u16 len)
185 181
186 set_bit(HCI_MGMT, &hdev->flags); 182 set_bit(HCI_MGMT, &hdev->flags);
187 183
188 put_unaligned_le16(hdev->id, &rp.index);
189 rp.type = hdev->dev_type; 184 rp.type = hdev->dev_type;
190 185
191 rp.powered = test_bit(HCI_UP, &hdev->flags); 186 rp.powered = test_bit(HCI_UP, &hdev->flags);
@@ -210,7 +205,7 @@ static int read_controller_info(struct sock *sk, unsigned char *data, u16 len)
210 hci_dev_unlock_bh(hdev); 205 hci_dev_unlock_bh(hdev);
211 hci_dev_put(hdev); 206 hci_dev_put(hdev);
212 207
213 return cmd_complete(sk, MGMT_OP_READ_INFO, &rp, sizeof(rp)); 208 return cmd_complete(sk, index, MGMT_OP_READ_INFO, &rp, sizeof(rp));
214} 209}
215 210
216static void mgmt_pending_free(struct pending_cmd *cmd) 211static void mgmt_pending_free(struct pending_cmd *cmd)
@@ -296,37 +291,35 @@ static void mgmt_pending_remove(struct pending_cmd *cmd)
296 mgmt_pending_free(cmd); 291 mgmt_pending_free(cmd);
297} 292}
298 293
299static int set_powered(struct sock *sk, unsigned char *data, u16 len) 294static int set_powered(struct sock *sk, u16 index, unsigned char *data, u16 len)
300{ 295{
301 struct mgmt_mode *cp; 296 struct mgmt_mode *cp;
302 struct hci_dev *hdev; 297 struct hci_dev *hdev;
303 struct pending_cmd *cmd; 298 struct pending_cmd *cmd;
304 u16 dev_id;
305 int err, up; 299 int err, up;
306 300
307 cp = (void *) data; 301 cp = (void *) data;
308 dev_id = get_unaligned_le16(&cp->index);
309 302
310 BT_DBG("request for hci%u", dev_id); 303 BT_DBG("request for hci%u", index);
311 304
312 hdev = hci_dev_get(dev_id); 305 hdev = hci_dev_get(index);
313 if (!hdev) 306 if (!hdev)
314 return cmd_status(sk, MGMT_OP_SET_POWERED, ENODEV); 307 return cmd_status(sk, index, MGMT_OP_SET_POWERED, ENODEV);
315 308
316 hci_dev_lock_bh(hdev); 309 hci_dev_lock_bh(hdev);
317 310
318 up = test_bit(HCI_UP, &hdev->flags); 311 up = test_bit(HCI_UP, &hdev->flags);
319 if ((cp->val && up) || (!cp->val && !up)) { 312 if ((cp->val && up) || (!cp->val && !up)) {
320 err = cmd_status(sk, MGMT_OP_SET_POWERED, EALREADY); 313 err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EALREADY);
321 goto failed; 314 goto failed;
322 } 315 }
323 316
324 if (mgmt_pending_find(MGMT_OP_SET_POWERED, dev_id)) { 317 if (mgmt_pending_find(MGMT_OP_SET_POWERED, index)) {
325 err = cmd_status(sk, MGMT_OP_SET_POWERED, EBUSY); 318 err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EBUSY);
326 goto failed; 319 goto failed;
327 } 320 }
328 321
329 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, dev_id, data, len); 322 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, index, data, len);
330 if (!cmd) { 323 if (!cmd) {
331 err = -ENOMEM; 324 err = -ENOMEM;
332 goto failed; 325 goto failed;
@@ -345,44 +338,43 @@ failed:
345 return err; 338 return err;
346} 339}
347 340
348static int set_discoverable(struct sock *sk, unsigned char *data, u16 len) 341static int set_discoverable(struct sock *sk, u16 index, unsigned char *data,
342 u16 len)
349{ 343{
350 struct mgmt_mode *cp; 344 struct mgmt_mode *cp;
351 struct hci_dev *hdev; 345 struct hci_dev *hdev;
352 struct pending_cmd *cmd; 346 struct pending_cmd *cmd;
353 u16 dev_id;
354 u8 scan; 347 u8 scan;
355 int err; 348 int err;
356 349
357 cp = (void *) data; 350 cp = (void *) data;
358 dev_id = get_unaligned_le16(&cp->index);
359 351
360 BT_DBG("request for hci%u", dev_id); 352 BT_DBG("request for hci%u", index);
361 353
362 hdev = hci_dev_get(dev_id); 354 hdev = hci_dev_get(index);
363 if (!hdev) 355 if (!hdev)
364 return cmd_status(sk, MGMT_OP_SET_DISCOVERABLE, ENODEV); 356 return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENODEV);
365 357
366 hci_dev_lock_bh(hdev); 358 hci_dev_lock_bh(hdev);
367 359
368 if (!test_bit(HCI_UP, &hdev->flags)) { 360 if (!test_bit(HCI_UP, &hdev->flags)) {
369 err = cmd_status(sk, MGMT_OP_SET_DISCOVERABLE, ENETDOWN); 361 err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENETDOWN);
370 goto failed; 362 goto failed;
371 } 363 }
372 364
373 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, dev_id) || 365 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) ||
374 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, dev_id)) { 366 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) {
375 err = cmd_status(sk, MGMT_OP_SET_DISCOVERABLE, EBUSY); 367 err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EBUSY);
376 goto failed; 368 goto failed;
377 } 369 }
378 370
379 if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) && 371 if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) &&
380 test_bit(HCI_PSCAN, &hdev->flags)) { 372 test_bit(HCI_PSCAN, &hdev->flags)) {
381 err = cmd_status(sk, MGMT_OP_SET_DISCOVERABLE, EALREADY); 373 err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EALREADY);
382 goto failed; 374 goto failed;
383 } 375 }
384 376
385 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, dev_id, data, len); 377 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, index, data, len);
386 if (!cmd) { 378 if (!cmd) {
387 err = -ENOMEM; 379 err = -ENOMEM;
388 goto failed; 380 goto failed;
@@ -404,43 +396,42 @@ failed:
404 return err; 396 return err;
405} 397}
406 398
407static int set_connectable(struct sock *sk, unsigned char *data, u16 len) 399static int set_connectable(struct sock *sk, u16 index, unsigned char *data,
400 u16 len)
408{ 401{
409 struct mgmt_mode *cp; 402 struct mgmt_mode *cp;
410 struct hci_dev *hdev; 403 struct hci_dev *hdev;
411 struct pending_cmd *cmd; 404 struct pending_cmd *cmd;
412 u16 dev_id;
413 u8 scan; 405 u8 scan;
414 int err; 406 int err;
415 407
416 cp = (void *) data; 408 cp = (void *) data;
417 dev_id = get_unaligned_le16(&cp->index);
418 409
419 BT_DBG("request for hci%u", dev_id); 410 BT_DBG("request for hci%u", index);
420 411
421 hdev = hci_dev_get(dev_id); 412 hdev = hci_dev_get(index);
422 if (!hdev) 413 if (!hdev)
423 return cmd_status(sk, MGMT_OP_SET_CONNECTABLE, ENODEV); 414 return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENODEV);
424 415
425 hci_dev_lock_bh(hdev); 416 hci_dev_lock_bh(hdev);
426 417
427 if (!test_bit(HCI_UP, &hdev->flags)) { 418 if (!test_bit(HCI_UP, &hdev->flags)) {
428 err = cmd_status(sk, MGMT_OP_SET_CONNECTABLE, ENETDOWN); 419 err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENETDOWN);
429 goto failed; 420 goto failed;
430 } 421 }
431 422
432 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, dev_id) || 423 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) ||
433 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, dev_id)) { 424 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) {
434 err = cmd_status(sk, MGMT_OP_SET_CONNECTABLE, EBUSY); 425 err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EBUSY);
435 goto failed; 426 goto failed;
436 } 427 }
437 428
438 if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) { 429 if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) {
439 err = cmd_status(sk, MGMT_OP_SET_CONNECTABLE, EALREADY); 430 err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EALREADY);
440 goto failed; 431 goto failed;
441 } 432 }
442 433
443 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, dev_id, data, len); 434 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, index, data, len);
444 if (!cmd) { 435 if (!cmd) {
445 err = -ENOMEM; 436 err = -ENOMEM;
446 goto failed; 437 goto failed;
@@ -462,7 +453,8 @@ failed:
462 return err; 453 return err;
463} 454}
464 455
465static int mgmt_event(u16 event, void *data, u16 data_len, struct sock *skip_sk) 456static int mgmt_event(u16 event, u16 index, void *data, u16 data_len,
457 struct sock *skip_sk)
466{ 458{
467 struct sk_buff *skb; 459 struct sk_buff *skb;
468 struct mgmt_hdr *hdr; 460 struct mgmt_hdr *hdr;
@@ -475,9 +467,11 @@ static int mgmt_event(u16 event, void *data, u16 data_len, struct sock *skip_sk)
475 467
476 hdr = (void *) skb_put(skb, sizeof(*hdr)); 468 hdr = (void *) skb_put(skb, sizeof(*hdr));
477 hdr->opcode = cpu_to_le16(event); 469 hdr->opcode = cpu_to_le16(event);
470 hdr->index = cpu_to_le16(index);
478 hdr->len = cpu_to_le16(data_len); 471 hdr->len = cpu_to_le16(data_len);
479 472
480 memcpy(skb_put(skb, data_len), data, data_len); 473 if (data)
474 memcpy(skb_put(skb, data_len), data, data_len);
481 475
482 hci_send_to_sock(NULL, skb, skip_sk); 476 hci_send_to_sock(NULL, skb, skip_sk);
483 kfree_skb(skb); 477 kfree_skb(skb);
@@ -489,27 +483,25 @@ static int send_mode_rsp(struct sock *sk, u16 opcode, u16 index, u8 val)
489{ 483{
490 struct mgmt_mode rp; 484 struct mgmt_mode rp;
491 485
492 put_unaligned_le16(index, &rp.index);
493 rp.val = val; 486 rp.val = val;
494 487
495 return cmd_complete(sk, opcode, &rp, sizeof(rp)); 488 return cmd_complete(sk, index, opcode, &rp, sizeof(rp));
496} 489}
497 490
498static int set_pairable(struct sock *sk, unsigned char *data, u16 len) 491static int set_pairable(struct sock *sk, u16 index, unsigned char *data,
492 u16 len)
499{ 493{
500 struct mgmt_mode *cp, ev; 494 struct mgmt_mode *cp, ev;
501 struct hci_dev *hdev; 495 struct hci_dev *hdev;
502 u16 dev_id;
503 int err; 496 int err;
504 497
505 cp = (void *) data; 498 cp = (void *) data;
506 dev_id = get_unaligned_le16(&cp->index);
507 499
508 BT_DBG("request for hci%u", dev_id); 500 BT_DBG("request for hci%u", index);
509 501
510 hdev = hci_dev_get(dev_id); 502 hdev = hci_dev_get(index);
511 if (!hdev) 503 if (!hdev)
512 return cmd_status(sk, MGMT_OP_SET_PAIRABLE, ENODEV); 504 return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, ENODEV);
513 505
514 hci_dev_lock_bh(hdev); 506 hci_dev_lock_bh(hdev);
515 507
@@ -518,14 +510,13 @@ static int set_pairable(struct sock *sk, unsigned char *data, u16 len)
518 else 510 else
519 clear_bit(HCI_PAIRABLE, &hdev->flags); 511 clear_bit(HCI_PAIRABLE, &hdev->flags);
520 512
521 err = send_mode_rsp(sk, MGMT_OP_SET_PAIRABLE, dev_id, cp->val); 513 err = send_mode_rsp(sk, MGMT_OP_SET_PAIRABLE, index, cp->val);
522 if (err < 0) 514 if (err < 0)
523 goto failed; 515 goto failed;
524 516
525 put_unaligned_le16(dev_id, &ev.index);
526 ev.val = cp->val; 517 ev.val = cp->val;
527 518
528 err = mgmt_event(MGMT_EV_PAIRABLE, &ev, sizeof(ev), sk); 519 err = mgmt_event(MGMT_EV_PAIRABLE, index, &ev, sizeof(ev), sk);
529 520
530failed: 521failed:
531 hci_dev_unlock_bh(hdev); 522 hci_dev_unlock_bh(hdev);
@@ -567,22 +558,20 @@ static int update_class(struct hci_dev *hdev)
567 return hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); 558 return hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
568} 559}
569 560
570static int add_uuid(struct sock *sk, unsigned char *data, u16 len) 561static int add_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len)
571{ 562{
572 struct mgmt_cp_add_uuid *cp; 563 struct mgmt_cp_add_uuid *cp;
573 struct hci_dev *hdev; 564 struct hci_dev *hdev;
574 struct bt_uuid *uuid; 565 struct bt_uuid *uuid;
575 u16 dev_id;
576 int err; 566 int err;
577 567
578 cp = (void *) data; 568 cp = (void *) data;
579 dev_id = get_unaligned_le16(&cp->index);
580 569
581 BT_DBG("request for hci%u", dev_id); 570 BT_DBG("request for hci%u", index);
582 571
583 hdev = hci_dev_get(dev_id); 572 hdev = hci_dev_get(index);
584 if (!hdev) 573 if (!hdev)
585 return cmd_status(sk, MGMT_OP_ADD_UUID, ENODEV); 574 return cmd_status(sk, index, MGMT_OP_ADD_UUID, ENODEV);
586 575
587 hci_dev_lock_bh(hdev); 576 hci_dev_lock_bh(hdev);
588 577
@@ -601,7 +590,7 @@ static int add_uuid(struct sock *sk, unsigned char *data, u16 len)
601 if (err < 0) 590 if (err < 0)
602 goto failed; 591 goto failed;
603 592
604 err = cmd_complete(sk, MGMT_OP_ADD_UUID, &dev_id, sizeof(dev_id)); 593 err = cmd_complete(sk, index, MGMT_OP_ADD_UUID, NULL, 0);
605 594
606failed: 595failed:
607 hci_dev_unlock_bh(hdev); 596 hci_dev_unlock_bh(hdev);
@@ -610,23 +599,21 @@ failed:
610 return err; 599 return err;
611} 600}
612 601
613static int remove_uuid(struct sock *sk, unsigned char *data, u16 len) 602static int remove_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len)
614{ 603{
615 struct list_head *p, *n; 604 struct list_head *p, *n;
616 struct mgmt_cp_remove_uuid *cp; 605 struct mgmt_cp_remove_uuid *cp;
617 struct hci_dev *hdev; 606 struct hci_dev *hdev;
618 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 607 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
619 u16 dev_id;
620 int err, found; 608 int err, found;
621 609
622 cp = (void *) data; 610 cp = (void *) data;
623 dev_id = get_unaligned_le16(&cp->index);
624 611
625 BT_DBG("request for hci%u", dev_id); 612 BT_DBG("request for hci%u", index);
626 613
627 hdev = hci_dev_get(dev_id); 614 hdev = hci_dev_get(index);
628 if (!hdev) 615 if (!hdev)
629 return cmd_status(sk, MGMT_OP_REMOVE_UUID, ENODEV); 616 return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENODEV);
630 617
631 hci_dev_lock_bh(hdev); 618 hci_dev_lock_bh(hdev);
632 619
@@ -648,7 +635,7 @@ static int remove_uuid(struct sock *sk, unsigned char *data, u16 len)
648 } 635 }
649 636
650 if (found == 0) { 637 if (found == 0) {
651 err = cmd_status(sk, MGMT_OP_REMOVE_UUID, ENOENT); 638 err = cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENOENT);
652 goto unlock; 639 goto unlock;
653 } 640 }
654 641
@@ -656,7 +643,7 @@ static int remove_uuid(struct sock *sk, unsigned char *data, u16 len)
656 if (err < 0) 643 if (err < 0)
657 goto unlock; 644 goto unlock;
658 645
659 err = cmd_complete(sk, MGMT_OP_REMOVE_UUID, &dev_id, sizeof(dev_id)); 646 err = cmd_complete(sk, index, MGMT_OP_REMOVE_UUID, NULL, 0);
660 647
661unlock: 648unlock:
662 hci_dev_unlock_bh(hdev); 649 hci_dev_unlock_bh(hdev);
@@ -665,21 +652,20 @@ unlock:
665 return err; 652 return err;
666} 653}
667 654
668static int set_dev_class(struct sock *sk, unsigned char *data, u16 len) 655static int set_dev_class(struct sock *sk, u16 index, unsigned char *data,
656 u16 len)
669{ 657{
670 struct hci_dev *hdev; 658 struct hci_dev *hdev;
671 struct mgmt_cp_set_dev_class *cp; 659 struct mgmt_cp_set_dev_class *cp;
672 u16 dev_id;
673 int err; 660 int err;
674 661
675 cp = (void *) data; 662 cp = (void *) data;
676 dev_id = get_unaligned_le16(&cp->index);
677 663
678 BT_DBG("request for hci%u", dev_id); 664 BT_DBG("request for hci%u", index);
679 665
680 hdev = hci_dev_get(dev_id); 666 hdev = hci_dev_get(index);
681 if (!hdev) 667 if (!hdev)
682 return cmd_status(sk, MGMT_OP_SET_DEV_CLASS, ENODEV); 668 return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, ENODEV);
683 669
684 hci_dev_lock_bh(hdev); 670 hci_dev_lock_bh(hdev);
685 671
@@ -689,8 +675,7 @@ static int set_dev_class(struct sock *sk, unsigned char *data, u16 len)
689 err = update_class(hdev); 675 err = update_class(hdev);
690 676
691 if (err == 0) 677 if (err == 0)
692 err = cmd_complete(sk, MGMT_OP_SET_DEV_CLASS, &dev_id, 678 err = cmd_complete(sk, index, MGMT_OP_SET_DEV_CLASS, NULL, 0);
693 sizeof(dev_id));
694 679
695 hci_dev_unlock_bh(hdev); 680 hci_dev_unlock_bh(hdev);
696 hci_dev_put(hdev); 681 hci_dev_put(hdev);
@@ -698,23 +683,22 @@ static int set_dev_class(struct sock *sk, unsigned char *data, u16 len)
698 return err; 683 return err;
699} 684}
700 685
701static int set_service_cache(struct sock *sk, unsigned char *data, u16 len) 686static int set_service_cache(struct sock *sk, u16 index, unsigned char *data,
687 u16 len)
702{ 688{
703 struct hci_dev *hdev; 689 struct hci_dev *hdev;
704 struct mgmt_cp_set_service_cache *cp; 690 struct mgmt_cp_set_service_cache *cp;
705 u16 dev_id;
706 int err; 691 int err;
707 692
708 cp = (void *) data; 693 cp = (void *) data;
709 dev_id = get_unaligned_le16(&cp->index);
710 694
711 hdev = hci_dev_get(dev_id); 695 hdev = hci_dev_get(index);
712 if (!hdev) 696 if (!hdev)
713 return cmd_status(sk, MGMT_OP_SET_SERVICE_CACHE, ENODEV); 697 return cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, ENODEV);
714 698
715 hci_dev_lock_bh(hdev); 699 hci_dev_lock_bh(hdev);
716 700
717 BT_DBG("hci%u enable %d", dev_id, cp->enable); 701 BT_DBG("hci%u enable %d", index, cp->enable);
718 702
719 if (cp->enable) { 703 if (cp->enable) {
720 set_bit(HCI_SERVICE_CACHE, &hdev->flags); 704 set_bit(HCI_SERVICE_CACHE, &hdev->flags);
@@ -725,8 +709,8 @@ static int set_service_cache(struct sock *sk, unsigned char *data, u16 len)
725 } 709 }
726 710
727 if (err == 0) 711 if (err == 0)
728 err = cmd_complete(sk, MGMT_OP_SET_SERVICE_CACHE, &dev_id, 712 err = cmd_complete(sk, index, MGMT_OP_SET_SERVICE_CACHE, NULL,
729 sizeof(dev_id)); 713 0);
730 714
731 hci_dev_unlock_bh(hdev); 715 hci_dev_unlock_bh(hdev);
732 hci_dev_put(hdev); 716 hci_dev_put(hdev);
@@ -734,15 +718,14 @@ static int set_service_cache(struct sock *sk, unsigned char *data, u16 len)
734 return err; 718 return err;
735} 719}
736 720
737static int load_keys(struct sock *sk, unsigned char *data, u16 len) 721static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len)
738{ 722{
739 struct hci_dev *hdev; 723 struct hci_dev *hdev;
740 struct mgmt_cp_load_keys *cp; 724 struct mgmt_cp_load_keys *cp;
741 u16 dev_id, key_count, expected_len; 725 u16 key_count, expected_len;
742 int i; 726 int i;
743 727
744 cp = (void *) data; 728 cp = (void *) data;
745 dev_id = get_unaligned_le16(&cp->index);
746 key_count = get_unaligned_le16(&cp->key_count); 729 key_count = get_unaligned_le16(&cp->key_count);
747 730
748 expected_len = sizeof(*cp) + key_count * sizeof(struct mgmt_key_info); 731 expected_len = sizeof(*cp) + key_count * sizeof(struct mgmt_key_info);
@@ -752,11 +735,11 @@ static int load_keys(struct sock *sk, unsigned char *data, u16 len)
752 return -EINVAL; 735 return -EINVAL;
753 } 736 }
754 737
755 hdev = hci_dev_get(dev_id); 738 hdev = hci_dev_get(index);
756 if (!hdev) 739 if (!hdev)
757 return cmd_status(sk, MGMT_OP_LOAD_KEYS, ENODEV); 740 return cmd_status(sk, index, MGMT_OP_LOAD_KEYS, ENODEV);
758 741
759 BT_DBG("hci%u debug_keys %u key_count %u", dev_id, cp->debug_keys, 742 BT_DBG("hci%u debug_keys %u key_count %u", index, cp->debug_keys,
760 key_count); 743 key_count);
761 744
762 hci_dev_lock_bh(hdev); 745 hci_dev_lock_bh(hdev);
@@ -783,26 +766,24 @@ static int load_keys(struct sock *sk, unsigned char *data, u16 len)
783 return 0; 766 return 0;
784} 767}
785 768
786static int remove_key(struct sock *sk, unsigned char *data, u16 len) 769static int remove_key(struct sock *sk, u16 index, unsigned char *data, u16 len)
787{ 770{
788 struct hci_dev *hdev; 771 struct hci_dev *hdev;
789 struct mgmt_cp_remove_key *cp; 772 struct mgmt_cp_remove_key *cp;
790 struct hci_conn *conn; 773 struct hci_conn *conn;
791 u16 dev_id;
792 int err; 774 int err;
793 775
794 cp = (void *) data; 776 cp = (void *) data;
795 dev_id = get_unaligned_le16(&cp->index);
796 777
797 hdev = hci_dev_get(dev_id); 778 hdev = hci_dev_get(index);
798 if (!hdev) 779 if (!hdev)
799 return cmd_status(sk, MGMT_OP_REMOVE_KEY, ENODEV); 780 return cmd_status(sk, index, MGMT_OP_REMOVE_KEY, ENODEV);
800 781
801 hci_dev_lock_bh(hdev); 782 hci_dev_lock_bh(hdev);
802 783
803 err = hci_remove_link_key(hdev, &cp->bdaddr); 784 err = hci_remove_link_key(hdev, &cp->bdaddr);
804 if (err < 0) { 785 if (err < 0) {
805 err = cmd_status(sk, MGMT_OP_REMOVE_KEY, -err); 786 err = cmd_status(sk, index, MGMT_OP_REMOVE_KEY, -err);
806 goto unlock; 787 goto unlock;
807 } 788 }
808 789
@@ -827,44 +808,42 @@ unlock:
827 return err; 808 return err;
828} 809}
829 810
830static int disconnect(struct sock *sk, unsigned char *data, u16 len) 811static int disconnect(struct sock *sk, u16 index, unsigned char *data, u16 len)
831{ 812{
832 struct hci_dev *hdev; 813 struct hci_dev *hdev;
833 struct mgmt_cp_disconnect *cp; 814 struct mgmt_cp_disconnect *cp;
834 struct hci_cp_disconnect dc; 815 struct hci_cp_disconnect dc;
835 struct pending_cmd *cmd; 816 struct pending_cmd *cmd;
836 struct hci_conn *conn; 817 struct hci_conn *conn;
837 u16 dev_id;
838 int err; 818 int err;
839 819
840 BT_DBG(""); 820 BT_DBG("");
841 821
842 cp = (void *) data; 822 cp = (void *) data;
843 dev_id = get_unaligned_le16(&cp->index);
844 823
845 hdev = hci_dev_get(dev_id); 824 hdev = hci_dev_get(index);
846 if (!hdev) 825 if (!hdev)
847 return cmd_status(sk, MGMT_OP_DISCONNECT, ENODEV); 826 return cmd_status(sk, index, MGMT_OP_DISCONNECT, ENODEV);
848 827
849 hci_dev_lock_bh(hdev); 828 hci_dev_lock_bh(hdev);
850 829
851 if (!test_bit(HCI_UP, &hdev->flags)) { 830 if (!test_bit(HCI_UP, &hdev->flags)) {
852 err = cmd_status(sk, MGMT_OP_DISCONNECT, ENETDOWN); 831 err = cmd_status(sk, index, MGMT_OP_DISCONNECT, ENETDOWN);
853 goto failed; 832 goto failed;
854 } 833 }
855 834
856 if (mgmt_pending_find(MGMT_OP_DISCONNECT, dev_id)) { 835 if (mgmt_pending_find(MGMT_OP_DISCONNECT, index)) {
857 err = cmd_status(sk, MGMT_OP_DISCONNECT, EBUSY); 836 err = cmd_status(sk, index, MGMT_OP_DISCONNECT, EBUSY);
858 goto failed; 837 goto failed;
859 } 838 }
860 839
861 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); 840 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
862 if (!conn) { 841 if (!conn) {
863 err = cmd_status(sk, MGMT_OP_DISCONNECT, ENOTCONN); 842 err = cmd_status(sk, index, MGMT_OP_DISCONNECT, ENOTCONN);
864 goto failed; 843 goto failed;
865 } 844 }
866 845
867 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, dev_id, data, len); 846 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, index, data, len);
868 if (!cmd) { 847 if (!cmd) {
869 err = -ENOMEM; 848 err = -ENOMEM;
870 goto failed; 849 goto failed;
@@ -884,24 +863,24 @@ failed:
884 return err; 863 return err;
885} 864}
886 865
887static int get_connections(struct sock *sk, unsigned char *data, u16 len) 866static int get_connections(struct sock *sk, u16 index, unsigned char *data,
867 u16 len)
888{ 868{
889 struct mgmt_cp_get_connections *cp; 869 struct mgmt_cp_get_connections *cp;
890 struct mgmt_rp_get_connections *rp; 870 struct mgmt_rp_get_connections *rp;
891 struct hci_dev *hdev; 871 struct hci_dev *hdev;
892 struct list_head *p; 872 struct list_head *p;
893 size_t rp_len; 873 size_t rp_len;
894 u16 dev_id, count; 874 u16 count;
895 int i, err; 875 int i, err;
896 876
897 BT_DBG(""); 877 BT_DBG("");
898 878
899 cp = (void *) data; 879 cp = (void *) data;
900 dev_id = get_unaligned_le16(&cp->index);
901 880
902 hdev = hci_dev_get(dev_id); 881 hdev = hci_dev_get(index);
903 if (!hdev) 882 if (!hdev)
904 return cmd_status(sk, MGMT_OP_GET_CONNECTIONS, ENODEV); 883 return cmd_status(sk, index, MGMT_OP_GET_CONNECTIONS, ENODEV);
905 884
906 hci_dev_lock_bh(hdev); 885 hci_dev_lock_bh(hdev);
907 886
@@ -917,7 +896,6 @@ static int get_connections(struct sock *sk, unsigned char *data, u16 len)
917 goto unlock; 896 goto unlock;
918 } 897 }
919 898
920 put_unaligned_le16(dev_id, &rp->index);
921 put_unaligned_le16(count, &rp->conn_count); 899 put_unaligned_le16(count, &rp->conn_count);
922 900
923 read_lock(&hci_dev_list_lock); 901 read_lock(&hci_dev_list_lock);
@@ -931,7 +909,7 @@ static int get_connections(struct sock *sk, unsigned char *data, u16 len)
931 909
932 read_unlock(&hci_dev_list_lock); 910 read_unlock(&hci_dev_list_lock);
933 911
934 err = cmd_complete(sk, MGMT_OP_GET_CONNECTIONS, rp, rp_len); 912 err = cmd_complete(sk, index, MGMT_OP_GET_CONNECTIONS, rp, rp_len);
935 913
936unlock: 914unlock:
937 kfree(rp); 915 kfree(rp);
@@ -940,32 +918,31 @@ unlock:
940 return err; 918 return err;
941} 919}
942 920
943static int pin_code_reply(struct sock *sk, unsigned char *data, u16 len) 921static int pin_code_reply(struct sock *sk, u16 index, unsigned char *data,
922 u16 len)
944{ 923{
945 struct hci_dev *hdev; 924 struct hci_dev *hdev;
946 struct mgmt_cp_pin_code_reply *cp; 925 struct mgmt_cp_pin_code_reply *cp;
947 struct hci_cp_pin_code_reply reply; 926 struct hci_cp_pin_code_reply reply;
948 struct pending_cmd *cmd; 927 struct pending_cmd *cmd;
949 u16 dev_id;
950 int err; 928 int err;
951 929
952 BT_DBG(""); 930 BT_DBG("");
953 931
954 cp = (void *) data; 932 cp = (void *) data;
955 dev_id = get_unaligned_le16(&cp->index);
956 933
957 hdev = hci_dev_get(dev_id); 934 hdev = hci_dev_get(index);
958 if (!hdev) 935 if (!hdev)
959 return cmd_status(sk, MGMT_OP_PIN_CODE_REPLY, ENODEV); 936 return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENODEV);
960 937
961 hci_dev_lock_bh(hdev); 938 hci_dev_lock_bh(hdev);
962 939
963 if (!test_bit(HCI_UP, &hdev->flags)) { 940 if (!test_bit(HCI_UP, &hdev->flags)) {
964 err = cmd_status(sk, MGMT_OP_PIN_CODE_REPLY, ENETDOWN); 941 err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENETDOWN);
965 goto failed; 942 goto failed;
966 } 943 }
967 944
968 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, dev_id, data, len); 945 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, index, data, len);
969 if (!cmd) { 946 if (!cmd) {
970 err = -ENOMEM; 947 err = -ENOMEM;
971 goto failed; 948 goto failed;
@@ -986,31 +963,32 @@ failed:
986 return err; 963 return err;
987} 964}
988 965
989static int pin_code_neg_reply(struct sock *sk, unsigned char *data, u16 len) 966static int pin_code_neg_reply(struct sock *sk, u16 index, unsigned char *data,
967 u16 len)
990{ 968{
991 struct hci_dev *hdev; 969 struct hci_dev *hdev;
992 struct mgmt_cp_pin_code_neg_reply *cp; 970 struct mgmt_cp_pin_code_neg_reply *cp;
993 struct pending_cmd *cmd; 971 struct pending_cmd *cmd;
994 u16 dev_id;
995 int err; 972 int err;
996 973
997 BT_DBG(""); 974 BT_DBG("");
998 975
999 cp = (void *) data; 976 cp = (void *) data;
1000 dev_id = get_unaligned_le16(&cp->index);
1001 977
1002 hdev = hci_dev_get(dev_id); 978 hdev = hci_dev_get(index);
1003 if (!hdev) 979 if (!hdev)
1004 return cmd_status(sk, MGMT_OP_PIN_CODE_NEG_REPLY, ENODEV); 980 return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY,
981 ENODEV);
1005 982
1006 hci_dev_lock_bh(hdev); 983 hci_dev_lock_bh(hdev);
1007 984
1008 if (!test_bit(HCI_UP, &hdev->flags)) { 985 if (!test_bit(HCI_UP, &hdev->flags)) {
1009 err = cmd_status(sk, MGMT_OP_PIN_CODE_NEG_REPLY, ENETDOWN); 986 err = cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY,
987 ENETDOWN);
1010 goto failed; 988 goto failed;
1011 } 989 }
1012 990
1013 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, dev_id, 991 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, index,
1014 data, len); 992 data, len);
1015 if (!cmd) { 993 if (!cmd) {
1016 err = -ENOMEM; 994 err = -ENOMEM;
@@ -1029,20 +1007,19 @@ failed:
1029 return err; 1007 return err;
1030} 1008}
1031 1009
1032static int set_io_capability(struct sock *sk, unsigned char *data, u16 len) 1010static int set_io_capability(struct sock *sk, u16 index, unsigned char *data,
1011 u16 len)
1033{ 1012{
1034 struct hci_dev *hdev; 1013 struct hci_dev *hdev;
1035 struct mgmt_cp_set_io_capability *cp; 1014 struct mgmt_cp_set_io_capability *cp;
1036 u16 dev_id;
1037 1015
1038 BT_DBG(""); 1016 BT_DBG("");
1039 1017
1040 cp = (void *) data; 1018 cp = (void *) data;
1041 dev_id = get_unaligned_le16(&cp->index);
1042 1019
1043 hdev = hci_dev_get(dev_id); 1020 hdev = hci_dev_get(index);
1044 if (!hdev) 1021 if (!hdev)
1045 return cmd_status(sk, MGMT_OP_SET_IO_CAPABILITY, ENODEV); 1022 return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, ENODEV);
1046 1023
1047 hci_dev_lock_bh(hdev); 1024 hci_dev_lock_bh(hdev);
1048 1025
@@ -1054,8 +1031,7 @@ static int set_io_capability(struct sock *sk, unsigned char *data, u16 len)
1054 hci_dev_unlock_bh(hdev); 1031 hci_dev_unlock_bh(hdev);
1055 hci_dev_put(hdev); 1032 hci_dev_put(hdev);
1056 1033
1057 return cmd_complete(sk, MGMT_OP_SET_IO_CAPABILITY, 1034 return cmd_complete(sk, index, MGMT_OP_SET_IO_CAPABILITY, NULL, 0);
1058 &dev_id, sizeof(dev_id));
1059} 1035}
1060 1036
1061static inline struct pending_cmd *find_pairing(struct hci_conn *conn) 1037static inline struct pending_cmd *find_pairing(struct hci_conn *conn)
@@ -1088,11 +1064,10 @@ static void pairing_complete(struct pending_cmd *cmd, u8 status)
1088 struct mgmt_rp_pair_device rp; 1064 struct mgmt_rp_pair_device rp;
1089 struct hci_conn *conn = cmd->user_data; 1065 struct hci_conn *conn = cmd->user_data;
1090 1066
1091 rp.index = cmd->index;
1092 bacpy(&rp.bdaddr, &conn->dst); 1067 bacpy(&rp.bdaddr, &conn->dst);
1093 rp.status = status; 1068 rp.status = status;
1094 1069
1095 cmd_complete(cmd->sk, MGMT_OP_PAIR_DEVICE, &rp, sizeof(rp)); 1070 cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, &rp, sizeof(rp));
1096 1071
1097 /* So we don't get further callbacks for this connection */ 1072 /* So we don't get further callbacks for this connection */
1098 conn->connect_cfm_cb = NULL; 1073 conn->connect_cfm_cb = NULL;
@@ -1119,24 +1094,22 @@ static void pairing_complete_cb(struct hci_conn *conn, u8 status)
1119 pairing_complete(cmd, status); 1094 pairing_complete(cmd, status);
1120} 1095}
1121 1096
1122static int pair_device(struct sock *sk, unsigned char *data, u16 len) 1097static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len)
1123{ 1098{
1124 struct hci_dev *hdev; 1099 struct hci_dev *hdev;
1125 struct mgmt_cp_pair_device *cp; 1100 struct mgmt_cp_pair_device *cp;
1126 struct pending_cmd *cmd; 1101 struct pending_cmd *cmd;
1127 u8 sec_level, auth_type; 1102 u8 sec_level, auth_type;
1128 struct hci_conn *conn; 1103 struct hci_conn *conn;
1129 u16 dev_id;
1130 int err; 1104 int err;
1131 1105
1132 BT_DBG(""); 1106 BT_DBG("");
1133 1107
1134 cp = (void *) data; 1108 cp = (void *) data;
1135 dev_id = get_unaligned_le16(&cp->index);
1136 1109
1137 hdev = hci_dev_get(dev_id); 1110 hdev = hci_dev_get(index);
1138 if (!hdev) 1111 if (!hdev)
1139 return cmd_status(sk, MGMT_OP_PAIR_DEVICE, ENODEV); 1112 return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, ENODEV);
1140 1113
1141 hci_dev_lock_bh(hdev); 1114 hci_dev_lock_bh(hdev);
1142 1115
@@ -1156,11 +1129,11 @@ static int pair_device(struct sock *sk, unsigned char *data, u16 len)
1156 1129
1157 if (conn->connect_cfm_cb) { 1130 if (conn->connect_cfm_cb) {
1158 hci_conn_put(conn); 1131 hci_conn_put(conn);
1159 err = cmd_status(sk, MGMT_OP_PAIR_DEVICE, EBUSY); 1132 err = cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, EBUSY);
1160 goto unlock; 1133 goto unlock;
1161 } 1134 }
1162 1135
1163 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, dev_id, data, len); 1136 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, index, data, len);
1164 if (!cmd) { 1137 if (!cmd) {
1165 err = -ENOMEM; 1138 err = -ENOMEM;
1166 hci_conn_put(conn); 1139 hci_conn_put(conn);
@@ -1186,19 +1159,17 @@ unlock:
1186 return err; 1159 return err;
1187} 1160}
1188 1161
1189static int user_confirm_reply(struct sock *sk, unsigned char *data, u16 len, 1162static int user_confirm_reply(struct sock *sk, u16 index, unsigned char *data,
1190 int success) 1163 u16 len, int success)
1191{ 1164{
1192 struct mgmt_cp_user_confirm_reply *cp = (void *) data; 1165 struct mgmt_cp_user_confirm_reply *cp = (void *) data;
1193 u16 dev_id, mgmt_op, hci_op; 1166 u16 mgmt_op, hci_op;
1194 struct pending_cmd *cmd; 1167 struct pending_cmd *cmd;
1195 struct hci_dev *hdev; 1168 struct hci_dev *hdev;
1196 int err; 1169 int err;
1197 1170
1198 BT_DBG(""); 1171 BT_DBG("");
1199 1172
1200 dev_id = get_unaligned_le16(&cp->index);
1201
1202 if (success) { 1173 if (success) {
1203 mgmt_op = MGMT_OP_USER_CONFIRM_REPLY; 1174 mgmt_op = MGMT_OP_USER_CONFIRM_REPLY;
1204 hci_op = HCI_OP_USER_CONFIRM_REPLY; 1175 hci_op = HCI_OP_USER_CONFIRM_REPLY;
@@ -1207,16 +1178,16 @@ static int user_confirm_reply(struct sock *sk, unsigned char *data, u16 len,
1207 hci_op = HCI_OP_USER_CONFIRM_NEG_REPLY; 1178 hci_op = HCI_OP_USER_CONFIRM_NEG_REPLY;
1208 } 1179 }
1209 1180
1210 hdev = hci_dev_get(dev_id); 1181 hdev = hci_dev_get(index);
1211 if (!hdev) 1182 if (!hdev)
1212 return cmd_status(sk, mgmt_op, ENODEV); 1183 return cmd_status(sk, index, mgmt_op, ENODEV);
1213 1184
1214 if (!test_bit(HCI_UP, &hdev->flags)) { 1185 if (!test_bit(HCI_UP, &hdev->flags)) {
1215 err = cmd_status(sk, mgmt_op, ENETDOWN); 1186 err = cmd_status(sk, index, mgmt_op, ENETDOWN);
1216 goto failed; 1187 goto failed;
1217 } 1188 }
1218 1189
1219 cmd = mgmt_pending_add(sk, mgmt_op, dev_id, data, len); 1190 cmd = mgmt_pending_add(sk, mgmt_op, index, data, len);
1220 if (!cmd) { 1191 if (!cmd) {
1221 err = -ENOMEM; 1192 err = -ENOMEM;
1222 goto failed; 1193 goto failed;
@@ -1237,7 +1208,7 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
1237{ 1208{
1238 unsigned char *buf; 1209 unsigned char *buf;
1239 struct mgmt_hdr *hdr; 1210 struct mgmt_hdr *hdr;
1240 u16 opcode, len; 1211 u16 opcode, index, len;
1241 int err; 1212 int err;
1242 1213
1243 BT_DBG("got %zu bytes", msglen); 1214 BT_DBG("got %zu bytes", msglen);
@@ -1256,6 +1227,7 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
1256 1227
1257 hdr = (struct mgmt_hdr *) buf; 1228 hdr = (struct mgmt_hdr *) buf;
1258 opcode = get_unaligned_le16(&hdr->opcode); 1229 opcode = get_unaligned_le16(&hdr->opcode);
1230 index = get_unaligned_le16(&hdr->index);
1259 len = get_unaligned_le16(&hdr->len); 1231 len = get_unaligned_le16(&hdr->len);
1260 1232
1261 if (len != msglen - sizeof(*hdr)) { 1233 if (len != msglen - sizeof(*hdr)) {
@@ -1271,65 +1243,65 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
1271 err = read_index_list(sk); 1243 err = read_index_list(sk);
1272 break; 1244 break;
1273 case MGMT_OP_READ_INFO: 1245 case MGMT_OP_READ_INFO:
1274 err = read_controller_info(sk, buf + sizeof(*hdr), len); 1246 err = read_controller_info(sk, index);
1275 break; 1247 break;
1276 case MGMT_OP_SET_POWERED: 1248 case MGMT_OP_SET_POWERED:
1277 err = set_powered(sk, buf + sizeof(*hdr), len); 1249 err = set_powered(sk, index, buf + sizeof(*hdr), len);
1278 break; 1250 break;
1279 case MGMT_OP_SET_DISCOVERABLE: 1251 case MGMT_OP_SET_DISCOVERABLE:
1280 err = set_discoverable(sk, buf + sizeof(*hdr), len); 1252 err = set_discoverable(sk, index, buf + sizeof(*hdr), len);
1281 break; 1253 break;
1282 case MGMT_OP_SET_CONNECTABLE: 1254 case MGMT_OP_SET_CONNECTABLE:
1283 err = set_connectable(sk, buf + sizeof(*hdr), len); 1255 err = set_connectable(sk, index, buf + sizeof(*hdr), len);
1284 break; 1256 break;
1285 case MGMT_OP_SET_PAIRABLE: 1257 case MGMT_OP_SET_PAIRABLE:
1286 err = set_pairable(sk, buf + sizeof(*hdr), len); 1258 err = set_pairable(sk, index, buf + sizeof(*hdr), len);
1287 break; 1259 break;
1288 case MGMT_OP_ADD_UUID: 1260 case MGMT_OP_ADD_UUID:
1289 err = add_uuid(sk, buf + sizeof(*hdr), len); 1261 err = add_uuid(sk, index, buf + sizeof(*hdr), len);
1290 break; 1262 break;
1291 case MGMT_OP_REMOVE_UUID: 1263 case MGMT_OP_REMOVE_UUID:
1292 err = remove_uuid(sk, buf + sizeof(*hdr), len); 1264 err = remove_uuid(sk, index, buf + sizeof(*hdr), len);
1293 break; 1265 break;
1294 case MGMT_OP_SET_DEV_CLASS: 1266 case MGMT_OP_SET_DEV_CLASS:
1295 err = set_dev_class(sk, buf + sizeof(*hdr), len); 1267 err = set_dev_class(sk, index, buf + sizeof(*hdr), len);
1296 break; 1268 break;
1297 case MGMT_OP_SET_SERVICE_CACHE: 1269 case MGMT_OP_SET_SERVICE_CACHE:
1298 err = set_service_cache(sk, buf + sizeof(*hdr), len); 1270 err = set_service_cache(sk, index, buf + sizeof(*hdr), len);
1299 break; 1271 break;
1300 case MGMT_OP_LOAD_KEYS: 1272 case MGMT_OP_LOAD_KEYS:
1301 err = load_keys(sk, buf + sizeof(*hdr), len); 1273 err = load_keys(sk, index, buf + sizeof(*hdr), len);
1302 break; 1274 break;
1303 case MGMT_OP_REMOVE_KEY: 1275 case MGMT_OP_REMOVE_KEY:
1304 err = remove_key(sk, buf + sizeof(*hdr), len); 1276 err = remove_key(sk, index, buf + sizeof(*hdr), len);
1305 break; 1277 break;
1306 case MGMT_OP_DISCONNECT: 1278 case MGMT_OP_DISCONNECT:
1307 err = disconnect(sk, buf + sizeof(*hdr), len); 1279 err = disconnect(sk, index, buf + sizeof(*hdr), len);
1308 break; 1280 break;
1309 case MGMT_OP_GET_CONNECTIONS: 1281 case MGMT_OP_GET_CONNECTIONS:
1310 err = get_connections(sk, buf + sizeof(*hdr), len); 1282 err = get_connections(sk, index, buf + sizeof(*hdr), len);
1311 break; 1283 break;
1312 case MGMT_OP_PIN_CODE_REPLY: 1284 case MGMT_OP_PIN_CODE_REPLY:
1313 err = pin_code_reply(sk, buf + sizeof(*hdr), len); 1285 err = pin_code_reply(sk, index, buf + sizeof(*hdr), len);
1314 break; 1286 break;
1315 case MGMT_OP_PIN_CODE_NEG_REPLY: 1287 case MGMT_OP_PIN_CODE_NEG_REPLY:
1316 err = pin_code_neg_reply(sk, buf + sizeof(*hdr), len); 1288 err = pin_code_neg_reply(sk, index, buf + sizeof(*hdr), len);
1317 break; 1289 break;
1318 case MGMT_OP_SET_IO_CAPABILITY: 1290 case MGMT_OP_SET_IO_CAPABILITY:
1319 err = set_io_capability(sk, buf + sizeof(*hdr), len); 1291 err = set_io_capability(sk, index, buf + sizeof(*hdr), len);
1320 break; 1292 break;
1321 case MGMT_OP_PAIR_DEVICE: 1293 case MGMT_OP_PAIR_DEVICE:
1322 err = pair_device(sk, buf + sizeof(*hdr), len); 1294 err = pair_device(sk, index, buf + sizeof(*hdr), len);
1323 break; 1295 break;
1324 case MGMT_OP_USER_CONFIRM_REPLY: 1296 case MGMT_OP_USER_CONFIRM_REPLY:
1325 err = user_confirm_reply(sk, buf + sizeof(*hdr), len, 1); 1297 err = user_confirm_reply(sk, index, buf + sizeof(*hdr), len, 1);
1326 break; 1298 break;
1327 case MGMT_OP_USER_CONFIRM_NEG_REPLY: 1299 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1328 err = user_confirm_reply(sk, buf + sizeof(*hdr), len, 0); 1300 err = user_confirm_reply(sk, index, buf + sizeof(*hdr), len, 0);
1329 break; 1301 break;
1330 default: 1302 default:
1331 BT_DBG("Unknown op %u", opcode); 1303 BT_DBG("Unknown op %u", opcode);
1332 err = cmd_status(sk, opcode, 0x01); 1304 err = cmd_status(sk, index, opcode, 0x01);
1333 break; 1305 break;
1334 } 1306 }
1335 1307
@@ -1345,20 +1317,12 @@ done:
1345 1317
1346int mgmt_index_added(u16 index) 1318int mgmt_index_added(u16 index)
1347{ 1319{
1348 struct mgmt_ev_index_added ev; 1320 return mgmt_event(MGMT_EV_INDEX_ADDED, index, NULL, 0, NULL);
1349
1350 put_unaligned_le16(index, &ev.index);
1351
1352 return mgmt_event(MGMT_EV_INDEX_ADDED, &ev, sizeof(ev), NULL);
1353} 1321}
1354 1322
1355int mgmt_index_removed(u16 index) 1323int mgmt_index_removed(u16 index)
1356{ 1324{
1357 struct mgmt_ev_index_added ev; 1325 return mgmt_event(MGMT_EV_INDEX_REMOVED, index, NULL, 0, NULL);
1358
1359 put_unaligned_le16(index, &ev.index);
1360
1361 return mgmt_event(MGMT_EV_INDEX_REMOVED, &ev, sizeof(ev), NULL);
1362} 1326}
1363 1327
1364struct cmd_lookup { 1328struct cmd_lookup {
@@ -1394,10 +1358,9 @@ int mgmt_powered(u16 index, u8 powered)
1394 1358
1395 mgmt_pending_foreach(MGMT_OP_SET_POWERED, index, mode_rsp, &match); 1359 mgmt_pending_foreach(MGMT_OP_SET_POWERED, index, mode_rsp, &match);
1396 1360
1397 put_unaligned_le16(index, &ev.index);
1398 ev.val = powered; 1361 ev.val = powered;
1399 1362
1400 ret = mgmt_event(MGMT_EV_POWERED, &ev, sizeof(ev), match.sk); 1363 ret = mgmt_event(MGMT_EV_POWERED, index, &ev, sizeof(ev), match.sk);
1401 1364
1402 if (match.sk) 1365 if (match.sk)
1403 sock_put(match.sk); 1366 sock_put(match.sk);
@@ -1414,10 +1377,10 @@ int mgmt_discoverable(u16 index, u8 discoverable)
1414 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, index, 1377 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, index,
1415 mode_rsp, &match); 1378 mode_rsp, &match);
1416 1379
1417 put_unaligned_le16(index, &ev.index);
1418 ev.val = discoverable; 1380 ev.val = discoverable;
1419 1381
1420 ret = mgmt_event(MGMT_EV_DISCOVERABLE, &ev, sizeof(ev), match.sk); 1382 ret = mgmt_event(MGMT_EV_DISCOVERABLE, index, &ev, sizeof(ev),
1383 match.sk);
1421 1384
1422 if (match.sk) 1385 if (match.sk)
1423 sock_put(match.sk); 1386 sock_put(match.sk);
@@ -1433,10 +1396,9 @@ int mgmt_connectable(u16 index, u8 connectable)
1433 1396
1434 mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, index, mode_rsp, &match); 1397 mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, index, mode_rsp, &match);
1435 1398
1436 put_unaligned_le16(index, &ev.index);
1437 ev.val = connectable; 1399 ev.val = connectable;
1438 1400
1439 ret = mgmt_event(MGMT_EV_CONNECTABLE, &ev, sizeof(ev), match.sk); 1401 ret = mgmt_event(MGMT_EV_CONNECTABLE, index, &ev, sizeof(ev), match.sk);
1440 1402
1441 if (match.sk) 1403 if (match.sk)
1442 sock_put(match.sk); 1404 sock_put(match.sk);
@@ -1450,25 +1412,22 @@ int mgmt_new_key(u16 index, struct link_key *key, u8 old_key_type)
1450 1412
1451 memset(&ev, 0, sizeof(ev)); 1413 memset(&ev, 0, sizeof(ev));
1452 1414
1453 put_unaligned_le16(index, &ev.index);
1454
1455 bacpy(&ev.key.bdaddr, &key->bdaddr); 1415 bacpy(&ev.key.bdaddr, &key->bdaddr);
1456 ev.key.type = key->type; 1416 ev.key.type = key->type;
1457 memcpy(ev.key.val, key->val, 16); 1417 memcpy(ev.key.val, key->val, 16);
1458 ev.key.pin_len = key->pin_len; 1418 ev.key.pin_len = key->pin_len;
1459 ev.old_key_type = old_key_type; 1419 ev.old_key_type = old_key_type;
1460 1420
1461 return mgmt_event(MGMT_EV_NEW_KEY, &ev, sizeof(ev), NULL); 1421 return mgmt_event(MGMT_EV_NEW_KEY, index, &ev, sizeof(ev), NULL);
1462} 1422}
1463 1423
1464int mgmt_connected(u16 index, bdaddr_t *bdaddr) 1424int mgmt_connected(u16 index, bdaddr_t *bdaddr)
1465{ 1425{
1466 struct mgmt_ev_connected ev; 1426 struct mgmt_ev_connected ev;
1467 1427
1468 put_unaligned_le16(index, &ev.index);
1469 bacpy(&ev.bdaddr, bdaddr); 1428 bacpy(&ev.bdaddr, bdaddr);
1470 1429
1471 return mgmt_event(MGMT_EV_CONNECTED, &ev, sizeof(ev), NULL); 1430 return mgmt_event(MGMT_EV_CONNECTED, index, &ev, sizeof(ev), NULL);
1472} 1431}
1473 1432
1474static void disconnect_rsp(struct pending_cmd *cmd, void *data) 1433static void disconnect_rsp(struct pending_cmd *cmd, void *data)
@@ -1477,10 +1436,9 @@ static void disconnect_rsp(struct pending_cmd *cmd, void *data)
1477 struct sock **sk = data; 1436 struct sock **sk = data;
1478 struct mgmt_rp_disconnect rp; 1437 struct mgmt_rp_disconnect rp;
1479 1438
1480 put_unaligned_le16(cmd->index, &rp.index);
1481 bacpy(&rp.bdaddr, &cp->bdaddr); 1439 bacpy(&rp.bdaddr, &cp->bdaddr);
1482 1440
1483 cmd_complete(cmd->sk, MGMT_OP_DISCONNECT, &rp, sizeof(rp)); 1441 cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, &rp, sizeof(rp));
1484 1442
1485 *sk = cmd->sk; 1443 *sk = cmd->sk;
1486 sock_hold(*sk); 1444 sock_hold(*sk);
@@ -1496,10 +1454,9 @@ int mgmt_disconnected(u16 index, bdaddr_t *bdaddr)
1496 1454
1497 mgmt_pending_foreach(MGMT_OP_DISCONNECT, index, disconnect_rsp, &sk); 1455 mgmt_pending_foreach(MGMT_OP_DISCONNECT, index, disconnect_rsp, &sk);
1498 1456
1499 put_unaligned_le16(index, &ev.index);
1500 bacpy(&ev.bdaddr, bdaddr); 1457 bacpy(&ev.bdaddr, bdaddr);
1501 1458
1502 err = mgmt_event(MGMT_EV_DISCONNECTED, &ev, sizeof(ev), sk); 1459 err = mgmt_event(MGMT_EV_DISCONNECTED, index, &ev, sizeof(ev), sk);
1503 1460
1504 if (sk) 1461 if (sk)
1505 sock_put(sk); 1462 sock_put(sk);
@@ -1516,7 +1473,7 @@ int mgmt_disconnect_failed(u16 index)
1516 if (!cmd) 1473 if (!cmd)
1517 return -ENOENT; 1474 return -ENOENT;
1518 1475
1519 err = cmd_status(cmd->sk, MGMT_OP_DISCONNECT, EIO); 1476 err = cmd_status(cmd->sk, index, MGMT_OP_DISCONNECT, EIO);
1520 1477
1521 mgmt_pending_remove(cmd); 1478 mgmt_pending_remove(cmd);
1522 1479
@@ -1527,21 +1484,20 @@ int mgmt_connect_failed(u16 index, bdaddr_t *bdaddr, u8 status)
1527{ 1484{
1528 struct mgmt_ev_connect_failed ev; 1485 struct mgmt_ev_connect_failed ev;
1529 1486
1530 put_unaligned_le16(index, &ev.index);
1531 bacpy(&ev.bdaddr, bdaddr); 1487 bacpy(&ev.bdaddr, bdaddr);
1532 ev.status = status; 1488 ev.status = status;
1533 1489
1534 return mgmt_event(MGMT_EV_CONNECT_FAILED, &ev, sizeof(ev), NULL); 1490 return mgmt_event(MGMT_EV_CONNECT_FAILED, index, &ev, sizeof(ev), NULL);
1535} 1491}
1536 1492
1537int mgmt_pin_code_request(u16 index, bdaddr_t *bdaddr) 1493int mgmt_pin_code_request(u16 index, bdaddr_t *bdaddr)
1538{ 1494{
1539 struct mgmt_ev_pin_code_request ev; 1495 struct mgmt_ev_pin_code_request ev;
1540 1496
1541 put_unaligned_le16(index, &ev.index);
1542 bacpy(&ev.bdaddr, bdaddr); 1497 bacpy(&ev.bdaddr, bdaddr);
1543 1498
1544 return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, &ev, sizeof(ev), NULL); 1499 return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, index, &ev, sizeof(ev),
1500 NULL);
1545} 1501}
1546 1502
1547int mgmt_pin_code_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) 1503int mgmt_pin_code_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status)
@@ -1554,11 +1510,11 @@ int mgmt_pin_code_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status)
1554 if (!cmd) 1510 if (!cmd)
1555 return -ENOENT; 1511 return -ENOENT;
1556 1512
1557 put_unaligned_le16(index, &rp.index);
1558 bacpy(&rp.bdaddr, bdaddr); 1513 bacpy(&rp.bdaddr, bdaddr);
1559 rp.status = status; 1514 rp.status = status;
1560 1515
1561 err = cmd_complete(cmd->sk, MGMT_OP_PIN_CODE_REPLY, &rp, sizeof(rp)); 1516 err = cmd_complete(cmd->sk, index, MGMT_OP_PIN_CODE_REPLY, &rp,
1517 sizeof(rp));
1562 1518
1563 mgmt_pending_remove(cmd); 1519 mgmt_pending_remove(cmd);
1564 1520
@@ -1575,12 +1531,11 @@ int mgmt_pin_code_neg_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status)
1575 if (!cmd) 1531 if (!cmd)
1576 return -ENOENT; 1532 return -ENOENT;
1577 1533
1578 put_unaligned_le16(index, &rp.index);
1579 bacpy(&rp.bdaddr, bdaddr); 1534 bacpy(&rp.bdaddr, bdaddr);
1580 rp.status = status; 1535 rp.status = status;
1581 1536
1582 err = cmd_complete(cmd->sk, MGMT_OP_PIN_CODE_NEG_REPLY, 1537 err = cmd_complete(cmd->sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, &rp,
1583 &rp, sizeof(rp)); 1538 sizeof(rp));
1584 1539
1585 mgmt_pending_remove(cmd); 1540 mgmt_pending_remove(cmd);
1586 1541
@@ -1593,11 +1548,11 @@ int mgmt_user_confirm_request(u16 index, bdaddr_t *bdaddr, __le32 value)
1593 1548
1594 BT_DBG("hci%u", index); 1549 BT_DBG("hci%u", index);
1595 1550
1596 put_unaligned_le16(index, &ev.index);
1597 bacpy(&ev.bdaddr, bdaddr); 1551 bacpy(&ev.bdaddr, bdaddr);
1598 put_unaligned_le32(value, &ev.value); 1552 put_unaligned_le32(value, &ev.value);
1599 1553
1600 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, &ev, sizeof(ev), NULL); 1554 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, index, &ev, sizeof(ev),
1555 NULL);
1601} 1556}
1602 1557
1603static int confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status, 1558static int confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status,
@@ -1611,10 +1566,9 @@ static int confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status,
1611 if (!cmd) 1566 if (!cmd)
1612 return -ENOENT; 1567 return -ENOENT;
1613 1568
1614 put_unaligned_le16(index, &rp.index);
1615 bacpy(&rp.bdaddr, bdaddr); 1569 bacpy(&rp.bdaddr, bdaddr);
1616 rp.status = status; 1570 rp.status = status;
1617 err = cmd_complete(cmd->sk, opcode, &rp, sizeof(rp)); 1571 err = cmd_complete(cmd->sk, index, opcode, &rp, sizeof(rp));
1618 1572
1619 mgmt_pending_remove(cmd); 1573 mgmt_pending_remove(cmd);
1620 1574
@@ -1638,9 +1592,8 @@ int mgmt_auth_failed(u16 index, bdaddr_t *bdaddr, u8 status)
1638{ 1592{
1639 struct mgmt_ev_auth_failed ev; 1593 struct mgmt_ev_auth_failed ev;
1640 1594
1641 put_unaligned_le16(index, &ev.index);
1642 bacpy(&ev.bdaddr, bdaddr); 1595 bacpy(&ev.bdaddr, bdaddr);
1643 ev.status = status; 1596 ev.status = status;
1644 1597
1645 return mgmt_event(MGMT_EV_AUTH_FAILED, &ev, sizeof(ev), NULL); 1598 return mgmt_event(MGMT_EV_AUTH_FAILED, index, &ev, sizeof(ev), NULL);
1646} 1599}