Commit | Line | Data |
---|---|---|
8e87d142 | 1 | /* |
1da177e4 LT |
2 | BlueZ - Bluetooth protocol stack for Linux |
3 | Copyright (C) 2000-2001 Qualcomm Incorporated | |
590051de | 4 | Copyright (C) 2011 ProFUSION Embedded Systems |
1da177e4 LT |
5 | |
6 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License version 2 as | |
10 | published by the Free Software Foundation; | |
11 | ||
12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
13 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. | |
15 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY | |
8e87d142 YH |
16 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
17 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
18 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
1da177e4 LT |
19 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
20 | ||
8e87d142 YH |
21 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
22 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS | |
1da177e4 LT |
23 | SOFTWARE IS DISCLAIMED. |
24 | */ | |
25 | ||
26 | /* Bluetooth HCI core. */ | |
27 | ||
82453021 | 28 | #include <linux/jiffies.h> |
1da177e4 LT |
29 | #include <linux/module.h> |
30 | #include <linux/kmod.h> | |
31 | ||
32 | #include <linux/types.h> | |
33 | #include <linux/errno.h> | |
34 | #include <linux/kernel.h> | |
1da177e4 LT |
35 | #include <linux/sched.h> |
36 | #include <linux/slab.h> | |
37 | #include <linux/poll.h> | |
38 | #include <linux/fcntl.h> | |
39 | #include <linux/init.h> | |
40 | #include <linux/skbuff.h> | |
f48fd9c8 | 41 | #include <linux/workqueue.h> |
1da177e4 | 42 | #include <linux/interrupt.h> |
611b30f7 | 43 | #include <linux/rfkill.h> |
6bd32326 | 44 | #include <linux/timer.h> |
3a0259bb | 45 | #include <linux/crypto.h> |
1da177e4 LT |
46 | #include <net/sock.h> |
47 | ||
70f23020 | 48 | #include <linux/uaccess.h> |
1da177e4 LT |
49 | #include <asm/unaligned.h> |
50 | ||
51 | #include <net/bluetooth/bluetooth.h> | |
52 | #include <net/bluetooth/hci_core.h> | |
53 | ||
ab81cbf9 JH |
54 | #define AUTO_OFF_TIMEOUT 2000 |
55 | ||
b78752cc | 56 | static void hci_rx_work(struct work_struct *work); |
c347b765 | 57 | static void hci_cmd_work(struct work_struct *work); |
3eff45ea | 58 | static void hci_tx_work(struct work_struct *work); |
1da177e4 | 59 | |
1da177e4 LT |
60 | /* HCI device list */ |
61 | LIST_HEAD(hci_dev_list); | |
62 | DEFINE_RWLOCK(hci_dev_list_lock); | |
63 | ||
64 | /* HCI callback list */ | |
65 | LIST_HEAD(hci_cb_list); | |
66 | DEFINE_RWLOCK(hci_cb_list_lock); | |
67 | ||
1da177e4 LT |
68 | /* ---- HCI notifications ---- */ |
69 | ||
6516455d | 70 | static void hci_notify(struct hci_dev *hdev, int event) |
1da177e4 | 71 | { |
040030ef | 72 | hci_sock_dev_event(hdev, event); |
1da177e4 LT |
73 | } |
74 | ||
75 | /* ---- HCI requests ---- */ | |
76 | ||
23bb5763 | 77 | void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result) |
1da177e4 | 78 | { |
23bb5763 JH |
79 | BT_DBG("%s command 0x%04x result 0x%2.2x", hdev->name, cmd, result); |
80 | ||
a5040efa JH |
81 | /* If this is the init phase check if the completed command matches |
82 | * the last init command, and if not just return. | |
83 | */ | |
75fb0e32 JH |
84 | if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd) { |
85 | struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data; | |
1036b890 | 86 | u16 opcode = __le16_to_cpu(sent->opcode); |
75fb0e32 JH |
87 | struct sk_buff *skb; |
88 | ||
89 | /* Some CSR based controllers generate a spontaneous | |
90 | * reset complete event during init and any pending | |
91 | * command will never be completed. In such a case we | |
92 | * need to resend whatever was the last sent | |
93 | * command. | |
94 | */ | |
95 | ||
1036b890 | 96 | if (cmd != HCI_OP_RESET || opcode == HCI_OP_RESET) |
75fb0e32 JH |
97 | return; |
98 | ||
99 | skb = skb_clone(hdev->sent_cmd, GFP_ATOMIC); | |
100 | if (skb) { | |
101 | skb_queue_head(&hdev->cmd_q, skb); | |
102 | queue_work(hdev->workqueue, &hdev->cmd_work); | |
103 | } | |
104 | ||
23bb5763 | 105 | return; |
75fb0e32 | 106 | } |
1da177e4 LT |
107 | |
108 | if (hdev->req_status == HCI_REQ_PEND) { | |
109 | hdev->req_result = result; | |
110 | hdev->req_status = HCI_REQ_DONE; | |
111 | wake_up_interruptible(&hdev->req_wait_q); | |
112 | } | |
113 | } | |
114 | ||
115 | static void hci_req_cancel(struct hci_dev *hdev, int err) | |
116 | { | |
117 | BT_DBG("%s err 0x%2.2x", hdev->name, err); | |
118 | ||
119 | if (hdev->req_status == HCI_REQ_PEND) { | |
120 | hdev->req_result = err; | |
121 | hdev->req_status = HCI_REQ_CANCELED; | |
122 | wake_up_interruptible(&hdev->req_wait_q); | |
123 | } | |
124 | } | |
125 | ||
126 | /* Execute request and wait for completion. */ | |
8e87d142 | 127 | static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), |
01df8c31 | 128 | unsigned long opt, __u32 timeout) |
1da177e4 LT |
129 | { |
130 | DECLARE_WAITQUEUE(wait, current); | |
131 | int err = 0; | |
132 | ||
133 | BT_DBG("%s start", hdev->name); | |
134 | ||
135 | hdev->req_status = HCI_REQ_PEND; | |
136 | ||
137 | add_wait_queue(&hdev->req_wait_q, &wait); | |
138 | set_current_state(TASK_INTERRUPTIBLE); | |
139 | ||
140 | req(hdev, opt); | |
141 | schedule_timeout(timeout); | |
142 | ||
143 | remove_wait_queue(&hdev->req_wait_q, &wait); | |
144 | ||
145 | if (signal_pending(current)) | |
146 | return -EINTR; | |
147 | ||
148 | switch (hdev->req_status) { | |
149 | case HCI_REQ_DONE: | |
e175072f | 150 | err = -bt_to_errno(hdev->req_result); |
1da177e4 LT |
151 | break; |
152 | ||
153 | case HCI_REQ_CANCELED: | |
154 | err = -hdev->req_result; | |
155 | break; | |
156 | ||
157 | default: | |
158 | err = -ETIMEDOUT; | |
159 | break; | |
3ff50b79 | 160 | } |
1da177e4 | 161 | |
a5040efa | 162 | hdev->req_status = hdev->req_result = 0; |
1da177e4 LT |
163 | |
164 | BT_DBG("%s end: err %d", hdev->name, err); | |
165 | ||
166 | return err; | |
167 | } | |
168 | ||
169 | static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), | |
01df8c31 | 170 | unsigned long opt, __u32 timeout) |
1da177e4 LT |
171 | { |
172 | int ret; | |
173 | ||
7c6a329e MH |
174 | if (!test_bit(HCI_UP, &hdev->flags)) |
175 | return -ENETDOWN; | |
176 | ||
1da177e4 LT |
177 | /* Serialize all requests */ |
178 | hci_req_lock(hdev); | |
179 | ret = __hci_request(hdev, req, opt, timeout); | |
180 | hci_req_unlock(hdev); | |
181 | ||
182 | return ret; | |
183 | } | |
184 | ||
185 | static void hci_reset_req(struct hci_dev *hdev, unsigned long opt) | |
186 | { | |
187 | BT_DBG("%s %ld", hdev->name, opt); | |
188 | ||
189 | /* Reset device */ | |
f630cf0d | 190 | set_bit(HCI_RESET, &hdev->flags); |
a9de9248 | 191 | hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL); |
1da177e4 LT |
192 | } |
193 | ||
e61ef499 | 194 | static void bredr_init(struct hci_dev *hdev) |
1da177e4 | 195 | { |
b0916ea0 | 196 | struct hci_cp_delete_stored_link_key cp; |
1ebb9252 | 197 | __le16 param; |
89f2783d | 198 | __u8 flt_type; |
1da177e4 | 199 | |
2455a3ea AE |
200 | hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; |
201 | ||
1da177e4 LT |
202 | /* Mandatory initialization */ |
203 | ||
204 | /* Reset */ | |
f630cf0d | 205 | if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) { |
e61ef499 AE |
206 | set_bit(HCI_RESET, &hdev->flags); |
207 | hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL); | |
f630cf0d | 208 | } |
1da177e4 LT |
209 | |
210 | /* Read Local Supported Features */ | |
a9de9248 | 211 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
1da177e4 | 212 | |
1143e5a6 | 213 | /* Read Local Version */ |
a9de9248 | 214 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
1143e5a6 | 215 | |
1da177e4 | 216 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ |
a9de9248 | 217 | hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL); |
1da177e4 | 218 | |
1da177e4 | 219 | /* Read BD Address */ |
a9de9248 MH |
220 | hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL); |
221 | ||
222 | /* Read Class of Device */ | |
223 | hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); | |
224 | ||
225 | /* Read Local Name */ | |
226 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL); | |
1da177e4 LT |
227 | |
228 | /* Read Voice Setting */ | |
a9de9248 | 229 | hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL); |
1da177e4 LT |
230 | |
231 | /* Optional initialization */ | |
232 | ||
233 | /* Clear Event Filters */ | |
89f2783d | 234 | flt_type = HCI_FLT_CLEAR_ALL; |
a9de9248 | 235 | hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type); |
1da177e4 | 236 | |
1da177e4 | 237 | /* Connection accept timeout ~20 secs */ |
aca3192c | 238 | param = cpu_to_le16(0x7d00); |
a9de9248 | 239 | hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); |
b0916ea0 JH |
240 | |
241 | bacpy(&cp.bdaddr, BDADDR_ANY); | |
242 | cp.delete_all = 1; | |
243 | hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp); | |
1da177e4 LT |
244 | } |
245 | ||
e61ef499 AE |
246 | static void amp_init(struct hci_dev *hdev) |
247 | { | |
2455a3ea AE |
248 | hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; |
249 | ||
e61ef499 AE |
250 | /* Reset */ |
251 | hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL); | |
252 | ||
253 | /* Read Local Version */ | |
254 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL); | |
6bcbc489 AE |
255 | |
256 | /* Read Local AMP Info */ | |
257 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); | |
e61ef499 AE |
258 | } |
259 | ||
260 | static void hci_init_req(struct hci_dev *hdev, unsigned long opt) | |
261 | { | |
262 | struct sk_buff *skb; | |
263 | ||
264 | BT_DBG("%s %ld", hdev->name, opt); | |
265 | ||
266 | /* Driver initialization */ | |
267 | ||
268 | /* Special commands */ | |
269 | while ((skb = skb_dequeue(&hdev->driver_init))) { | |
270 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; | |
271 | skb->dev = (void *) hdev; | |
272 | ||
273 | skb_queue_tail(&hdev->cmd_q, skb); | |
274 | queue_work(hdev->workqueue, &hdev->cmd_work); | |
275 | } | |
276 | skb_queue_purge(&hdev->driver_init); | |
277 | ||
278 | switch (hdev->dev_type) { | |
279 | case HCI_BREDR: | |
280 | bredr_init(hdev); | |
281 | break; | |
282 | ||
283 | case HCI_AMP: | |
284 | amp_init(hdev); | |
285 | break; | |
286 | ||
287 | default: | |
288 | BT_ERR("Unknown device type %d", hdev->dev_type); | |
289 | break; | |
290 | } | |
291 | ||
292 | } | |
293 | ||
6ed58ec5 VT |
294 | static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt) |
295 | { | |
296 | BT_DBG("%s", hdev->name); | |
297 | ||
298 | /* Read LE buffer size */ | |
299 | hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); | |
300 | } | |
301 | ||
1da177e4 LT |
302 | static void hci_scan_req(struct hci_dev *hdev, unsigned long opt) |
303 | { | |
304 | __u8 scan = opt; | |
305 | ||
306 | BT_DBG("%s %x", hdev->name, scan); | |
307 | ||
308 | /* Inquiry and Page scans */ | |
a9de9248 | 309 | hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
1da177e4 LT |
310 | } |
311 | ||
312 | static void hci_auth_req(struct hci_dev *hdev, unsigned long opt) | |
313 | { | |
314 | __u8 auth = opt; | |
315 | ||
316 | BT_DBG("%s %x", hdev->name, auth); | |
317 | ||
318 | /* Authentication */ | |
a9de9248 | 319 | hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); |
1da177e4 LT |
320 | } |
321 | ||
322 | static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt) | |
323 | { | |
324 | __u8 encrypt = opt; | |
325 | ||
326 | BT_DBG("%s %x", hdev->name, encrypt); | |
327 | ||
e4e8e37c | 328 | /* Encryption */ |
a9de9248 | 329 | hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); |
1da177e4 LT |
330 | } |
331 | ||
e4e8e37c MH |
332 | static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt) |
333 | { | |
334 | __le16 policy = cpu_to_le16(opt); | |
335 | ||
a418b893 | 336 | BT_DBG("%s %x", hdev->name, policy); |
e4e8e37c MH |
337 | |
338 | /* Default link policy */ | |
339 | hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); | |
340 | } | |
341 | ||
8e87d142 | 342 | /* Get HCI device by index. |
1da177e4 LT |
343 | * Device is held on return. */ |
344 | struct hci_dev *hci_dev_get(int index) | |
345 | { | |
8035ded4 | 346 | struct hci_dev *hdev = NULL, *d; |
1da177e4 LT |
347 | |
348 | BT_DBG("%d", index); | |
349 | ||
350 | if (index < 0) | |
351 | return NULL; | |
352 | ||
353 | read_lock(&hci_dev_list_lock); | |
8035ded4 | 354 | list_for_each_entry(d, &hci_dev_list, list) { |
1da177e4 LT |
355 | if (d->id == index) { |
356 | hdev = hci_dev_hold(d); | |
357 | break; | |
358 | } | |
359 | } | |
360 | read_unlock(&hci_dev_list_lock); | |
361 | return hdev; | |
362 | } | |
1da177e4 LT |
363 | |
364 | /* ---- Inquiry support ---- */ | |
ff9ef578 | 365 | |
30dc78e1 JH |
366 | bool hci_discovery_active(struct hci_dev *hdev) |
367 | { | |
368 | struct discovery_state *discov = &hdev->discovery; | |
369 | ||
6fbe195d | 370 | switch (discov->state) { |
343f935b | 371 | case DISCOVERY_FINDING: |
6fbe195d | 372 | case DISCOVERY_RESOLVING: |
30dc78e1 JH |
373 | return true; |
374 | ||
6fbe195d AG |
375 | default: |
376 | return false; | |
377 | } | |
30dc78e1 JH |
378 | } |
379 | ||
ff9ef578 JH |
380 | void hci_discovery_set_state(struct hci_dev *hdev, int state) |
381 | { | |
382 | BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state); | |
383 | ||
384 | if (hdev->discovery.state == state) | |
385 | return; | |
386 | ||
387 | switch (state) { | |
388 | case DISCOVERY_STOPPED: | |
7b99b659 AG |
389 | if (hdev->discovery.state != DISCOVERY_STARTING) |
390 | mgmt_discovering(hdev, 0); | |
ff9ef578 JH |
391 | break; |
392 | case DISCOVERY_STARTING: | |
393 | break; | |
343f935b | 394 | case DISCOVERY_FINDING: |
ff9ef578 JH |
395 | mgmt_discovering(hdev, 1); |
396 | break; | |
30dc78e1 JH |
397 | case DISCOVERY_RESOLVING: |
398 | break; | |
ff9ef578 JH |
399 | case DISCOVERY_STOPPING: |
400 | break; | |
401 | } | |
402 | ||
403 | hdev->discovery.state = state; | |
404 | } | |
405 | ||
1da177e4 LT |
406 | static void inquiry_cache_flush(struct hci_dev *hdev) |
407 | { | |
30883512 | 408 | struct discovery_state *cache = &hdev->discovery; |
b57c1a56 | 409 | struct inquiry_entry *p, *n; |
1da177e4 | 410 | |
561aafbc JH |
411 | list_for_each_entry_safe(p, n, &cache->all, all) { |
412 | list_del(&p->all); | |
b57c1a56 | 413 | kfree(p); |
1da177e4 | 414 | } |
561aafbc JH |
415 | |
416 | INIT_LIST_HEAD(&cache->unknown); | |
417 | INIT_LIST_HEAD(&cache->resolve); | |
1da177e4 LT |
418 | } |
419 | ||
420 | struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr) | |
421 | { | |
30883512 | 422 | struct discovery_state *cache = &hdev->discovery; |
1da177e4 LT |
423 | struct inquiry_entry *e; |
424 | ||
425 | BT_DBG("cache %p, %s", cache, batostr(bdaddr)); | |
426 | ||
561aafbc JH |
427 | list_for_each_entry(e, &cache->all, all) { |
428 | if (!bacmp(&e->data.bdaddr, bdaddr)) | |
429 | return e; | |
430 | } | |
431 | ||
432 | return NULL; | |
433 | } | |
434 | ||
435 | struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev, | |
04124681 | 436 | bdaddr_t *bdaddr) |
561aafbc | 437 | { |
30883512 | 438 | struct discovery_state *cache = &hdev->discovery; |
561aafbc JH |
439 | struct inquiry_entry *e; |
440 | ||
441 | BT_DBG("cache %p, %s", cache, batostr(bdaddr)); | |
442 | ||
443 | list_for_each_entry(e, &cache->unknown, list) { | |
1da177e4 | 444 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
b57c1a56 JH |
445 | return e; |
446 | } | |
447 | ||
448 | return NULL; | |
1da177e4 LT |
449 | } |
450 | ||
30dc78e1 | 451 | struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev, |
04124681 GP |
452 | bdaddr_t *bdaddr, |
453 | int state) | |
30dc78e1 JH |
454 | { |
455 | struct discovery_state *cache = &hdev->discovery; | |
456 | struct inquiry_entry *e; | |
457 | ||
458 | BT_DBG("cache %p bdaddr %s state %d", cache, batostr(bdaddr), state); | |
459 | ||
460 | list_for_each_entry(e, &cache->resolve, list) { | |
461 | if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state) | |
462 | return e; | |
463 | if (!bacmp(&e->data.bdaddr, bdaddr)) | |
464 | return e; | |
465 | } | |
466 | ||
467 | return NULL; | |
468 | } | |
469 | ||
a3d4e20a | 470 | void hci_inquiry_cache_update_resolve(struct hci_dev *hdev, |
04124681 | 471 | struct inquiry_entry *ie) |
a3d4e20a JH |
472 | { |
473 | struct discovery_state *cache = &hdev->discovery; | |
474 | struct list_head *pos = &cache->resolve; | |
475 | struct inquiry_entry *p; | |
476 | ||
477 | list_del(&ie->list); | |
478 | ||
479 | list_for_each_entry(p, &cache->resolve, list) { | |
480 | if (p->name_state != NAME_PENDING && | |
481 | abs(p->data.rssi) >= abs(ie->data.rssi)) | |
482 | break; | |
483 | pos = &p->list; | |
484 | } | |
485 | ||
486 | list_add(&ie->list, pos); | |
487 | } | |
488 | ||
3175405b | 489 | bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data, |
04124681 | 490 | bool name_known, bool *ssp) |
1da177e4 | 491 | { |
30883512 | 492 | struct discovery_state *cache = &hdev->discovery; |
70f23020 | 493 | struct inquiry_entry *ie; |
1da177e4 LT |
494 | |
495 | BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr)); | |
496 | ||
388fc8fa JH |
497 | if (ssp) |
498 | *ssp = data->ssp_mode; | |
499 | ||
70f23020 | 500 | ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); |
a3d4e20a | 501 | if (ie) { |
388fc8fa JH |
502 | if (ie->data.ssp_mode && ssp) |
503 | *ssp = true; | |
504 | ||
a3d4e20a JH |
505 | if (ie->name_state == NAME_NEEDED && |
506 | data->rssi != ie->data.rssi) { | |
507 | ie->data.rssi = data->rssi; | |
508 | hci_inquiry_cache_update_resolve(hdev, ie); | |
509 | } | |
510 | ||
561aafbc | 511 | goto update; |
a3d4e20a | 512 | } |
561aafbc JH |
513 | |
514 | /* Entry not in the cache. Add new one. */ | |
515 | ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC); | |
516 | if (!ie) | |
3175405b | 517 | return false; |
561aafbc JH |
518 | |
519 | list_add(&ie->all, &cache->all); | |
520 | ||
521 | if (name_known) { | |
522 | ie->name_state = NAME_KNOWN; | |
523 | } else { | |
524 | ie->name_state = NAME_NOT_KNOWN; | |
525 | list_add(&ie->list, &cache->unknown); | |
526 | } | |
70f23020 | 527 | |
561aafbc JH |
528 | update: |
529 | if (name_known && ie->name_state != NAME_KNOWN && | |
530 | ie->name_state != NAME_PENDING) { | |
531 | ie->name_state = NAME_KNOWN; | |
532 | list_del(&ie->list); | |
1da177e4 LT |
533 | } |
534 | ||
70f23020 AE |
535 | memcpy(&ie->data, data, sizeof(*data)); |
536 | ie->timestamp = jiffies; | |
1da177e4 | 537 | cache->timestamp = jiffies; |
3175405b JH |
538 | |
539 | if (ie->name_state == NAME_NOT_KNOWN) | |
540 | return false; | |
541 | ||
542 | return true; | |
1da177e4 LT |
543 | } |
544 | ||
545 | static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) | |
546 | { | |
30883512 | 547 | struct discovery_state *cache = &hdev->discovery; |
1da177e4 LT |
548 | struct inquiry_info *info = (struct inquiry_info *) buf; |
549 | struct inquiry_entry *e; | |
550 | int copied = 0; | |
551 | ||
561aafbc | 552 | list_for_each_entry(e, &cache->all, all) { |
1da177e4 | 553 | struct inquiry_data *data = &e->data; |
b57c1a56 JH |
554 | |
555 | if (copied >= num) | |
556 | break; | |
557 | ||
1da177e4 LT |
558 | bacpy(&info->bdaddr, &data->bdaddr); |
559 | info->pscan_rep_mode = data->pscan_rep_mode; | |
560 | info->pscan_period_mode = data->pscan_period_mode; | |
561 | info->pscan_mode = data->pscan_mode; | |
562 | memcpy(info->dev_class, data->dev_class, 3); | |
563 | info->clock_offset = data->clock_offset; | |
b57c1a56 | 564 | |
1da177e4 | 565 | info++; |
b57c1a56 | 566 | copied++; |
1da177e4 LT |
567 | } |
568 | ||
569 | BT_DBG("cache %p, copied %d", cache, copied); | |
570 | return copied; | |
571 | } | |
572 | ||
573 | static void hci_inq_req(struct hci_dev *hdev, unsigned long opt) | |
574 | { | |
575 | struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; | |
576 | struct hci_cp_inquiry cp; | |
577 | ||
578 | BT_DBG("%s", hdev->name); | |
579 | ||
580 | if (test_bit(HCI_INQUIRY, &hdev->flags)) | |
581 | return; | |
582 | ||
583 | /* Start Inquiry */ | |
584 | memcpy(&cp.lap, &ir->lap, 3); | |
585 | cp.length = ir->length; | |
586 | cp.num_rsp = ir->num_rsp; | |
a9de9248 | 587 | hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); |
1da177e4 LT |
588 | } |
589 | ||
590 | int hci_inquiry(void __user *arg) | |
591 | { | |
592 | __u8 __user *ptr = arg; | |
593 | struct hci_inquiry_req ir; | |
594 | struct hci_dev *hdev; | |
595 | int err = 0, do_inquiry = 0, max_rsp; | |
596 | long timeo; | |
597 | __u8 *buf; | |
598 | ||
599 | if (copy_from_user(&ir, ptr, sizeof(ir))) | |
600 | return -EFAULT; | |
601 | ||
5a08ecce AE |
602 | hdev = hci_dev_get(ir.dev_id); |
603 | if (!hdev) | |
1da177e4 LT |
604 | return -ENODEV; |
605 | ||
09fd0de5 | 606 | hci_dev_lock(hdev); |
8e87d142 | 607 | if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || |
70f23020 AE |
608 | inquiry_cache_empty(hdev) || |
609 | ir.flags & IREQ_CACHE_FLUSH) { | |
1da177e4 LT |
610 | inquiry_cache_flush(hdev); |
611 | do_inquiry = 1; | |
612 | } | |
09fd0de5 | 613 | hci_dev_unlock(hdev); |
1da177e4 | 614 | |
04837f64 | 615 | timeo = ir.length * msecs_to_jiffies(2000); |
70f23020 AE |
616 | |
617 | if (do_inquiry) { | |
618 | err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo); | |
619 | if (err < 0) | |
620 | goto done; | |
621 | } | |
1da177e4 LT |
622 | |
623 | /* for unlimited number of responses we will use buffer with 255 entries */ | |
624 | max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; | |
625 | ||
626 | /* cache_dump can't sleep. Therefore we allocate temp buffer and then | |
627 | * copy it to the user space. | |
628 | */ | |
01df8c31 | 629 | buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); |
70f23020 | 630 | if (!buf) { |
1da177e4 LT |
631 | err = -ENOMEM; |
632 | goto done; | |
633 | } | |
634 | ||
09fd0de5 | 635 | hci_dev_lock(hdev); |
1da177e4 | 636 | ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); |
09fd0de5 | 637 | hci_dev_unlock(hdev); |
1da177e4 LT |
638 | |
639 | BT_DBG("num_rsp %d", ir.num_rsp); | |
640 | ||
641 | if (!copy_to_user(ptr, &ir, sizeof(ir))) { | |
642 | ptr += sizeof(ir); | |
643 | if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * | |
644 | ir.num_rsp)) | |
645 | err = -EFAULT; | |
8e87d142 | 646 | } else |
1da177e4 LT |
647 | err = -EFAULT; |
648 | ||
649 | kfree(buf); | |
650 | ||
651 | done: | |
652 | hci_dev_put(hdev); | |
653 | return err; | |
654 | } | |
655 | ||
656 | /* ---- HCI ioctl helpers ---- */ | |
657 | ||
658 | int hci_dev_open(__u16 dev) | |
659 | { | |
660 | struct hci_dev *hdev; | |
661 | int ret = 0; | |
662 | ||
5a08ecce AE |
663 | hdev = hci_dev_get(dev); |
664 | if (!hdev) | |
1da177e4 LT |
665 | return -ENODEV; |
666 | ||
667 | BT_DBG("%s %p", hdev->name, hdev); | |
668 | ||
669 | hci_req_lock(hdev); | |
670 | ||
94324962 JH |
671 | if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) { |
672 | ret = -ENODEV; | |
673 | goto done; | |
674 | } | |
675 | ||
611b30f7 MH |
676 | if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) { |
677 | ret = -ERFKILL; | |
678 | goto done; | |
679 | } | |
680 | ||
1da177e4 LT |
681 | if (test_bit(HCI_UP, &hdev->flags)) { |
682 | ret = -EALREADY; | |
683 | goto done; | |
684 | } | |
685 | ||
686 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) | |
687 | set_bit(HCI_RAW, &hdev->flags); | |
688 | ||
07e3b94a AE |
689 | /* Treat all non BR/EDR controllers as raw devices if |
690 | enable_hs is not set */ | |
691 | if (hdev->dev_type != HCI_BREDR && !enable_hs) | |
943da25d MH |
692 | set_bit(HCI_RAW, &hdev->flags); |
693 | ||
1da177e4 LT |
694 | if (hdev->open(hdev)) { |
695 | ret = -EIO; | |
696 | goto done; | |
697 | } | |
698 | ||
699 | if (!test_bit(HCI_RAW, &hdev->flags)) { | |
700 | atomic_set(&hdev->cmd_cnt, 1); | |
701 | set_bit(HCI_INIT, &hdev->flags); | |
a5040efa | 702 | hdev->init_last_cmd = 0; |
1da177e4 | 703 | |
04837f64 MH |
704 | ret = __hci_request(hdev, hci_init_req, 0, |
705 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 | 706 | |
eead27da | 707 | if (lmp_host_le_capable(hdev)) |
6ed58ec5 VT |
708 | ret = __hci_request(hdev, hci_le_init_req, 0, |
709 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
710 | ||
1da177e4 LT |
711 | clear_bit(HCI_INIT, &hdev->flags); |
712 | } | |
713 | ||
714 | if (!ret) { | |
715 | hci_dev_hold(hdev); | |
716 | set_bit(HCI_UP, &hdev->flags); | |
717 | hci_notify(hdev, HCI_DEV_UP); | |
a8b2d5c2 | 718 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) { |
09fd0de5 | 719 | hci_dev_lock(hdev); |
744cf19e | 720 | mgmt_powered(hdev, 1); |
09fd0de5 | 721 | hci_dev_unlock(hdev); |
56e5cb86 | 722 | } |
8e87d142 | 723 | } else { |
1da177e4 | 724 | /* Init failed, cleanup */ |
3eff45ea | 725 | flush_work(&hdev->tx_work); |
c347b765 | 726 | flush_work(&hdev->cmd_work); |
b78752cc | 727 | flush_work(&hdev->rx_work); |
1da177e4 LT |
728 | |
729 | skb_queue_purge(&hdev->cmd_q); | |
730 | skb_queue_purge(&hdev->rx_q); | |
731 | ||
732 | if (hdev->flush) | |
733 | hdev->flush(hdev); | |
734 | ||
735 | if (hdev->sent_cmd) { | |
736 | kfree_skb(hdev->sent_cmd); | |
737 | hdev->sent_cmd = NULL; | |
738 | } | |
739 | ||
740 | hdev->close(hdev); | |
741 | hdev->flags = 0; | |
742 | } | |
743 | ||
744 | done: | |
745 | hci_req_unlock(hdev); | |
746 | hci_dev_put(hdev); | |
747 | return ret; | |
748 | } | |
749 | ||
750 | static int hci_dev_do_close(struct hci_dev *hdev) | |
751 | { | |
752 | BT_DBG("%s %p", hdev->name, hdev); | |
753 | ||
28b75a89 AG |
754 | cancel_work_sync(&hdev->le_scan); |
755 | ||
1da177e4 LT |
756 | hci_req_cancel(hdev, ENODEV); |
757 | hci_req_lock(hdev); | |
758 | ||
759 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { | |
b79f44c1 | 760 | del_timer_sync(&hdev->cmd_timer); |
1da177e4 LT |
761 | hci_req_unlock(hdev); |
762 | return 0; | |
763 | } | |
764 | ||
3eff45ea GP |
765 | /* Flush RX and TX works */ |
766 | flush_work(&hdev->tx_work); | |
b78752cc | 767 | flush_work(&hdev->rx_work); |
1da177e4 | 768 | |
16ab91ab | 769 | if (hdev->discov_timeout > 0) { |
e0f9309f | 770 | cancel_delayed_work(&hdev->discov_off); |
16ab91ab | 771 | hdev->discov_timeout = 0; |
5e5282bb | 772 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
16ab91ab JH |
773 | } |
774 | ||
a8b2d5c2 | 775 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
7d78525d JH |
776 | cancel_delayed_work(&hdev->service_cache); |
777 | ||
7ba8b4be AG |
778 | cancel_delayed_work_sync(&hdev->le_scan_disable); |
779 | ||
09fd0de5 | 780 | hci_dev_lock(hdev); |
1da177e4 LT |
781 | inquiry_cache_flush(hdev); |
782 | hci_conn_hash_flush(hdev); | |
09fd0de5 | 783 | hci_dev_unlock(hdev); |
1da177e4 LT |
784 | |
785 | hci_notify(hdev, HCI_DEV_DOWN); | |
786 | ||
787 | if (hdev->flush) | |
788 | hdev->flush(hdev); | |
789 | ||
790 | /* Reset device */ | |
791 | skb_queue_purge(&hdev->cmd_q); | |
792 | atomic_set(&hdev->cmd_cnt, 1); | |
8af59467 JH |
793 | if (!test_bit(HCI_RAW, &hdev->flags) && |
794 | test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) { | |
1da177e4 | 795 | set_bit(HCI_INIT, &hdev->flags); |
04837f64 | 796 | __hci_request(hdev, hci_reset_req, 0, |
cad44c2b | 797 | msecs_to_jiffies(250)); |
1da177e4 LT |
798 | clear_bit(HCI_INIT, &hdev->flags); |
799 | } | |
800 | ||
c347b765 GP |
801 | /* flush cmd work */ |
802 | flush_work(&hdev->cmd_work); | |
1da177e4 LT |
803 | |
804 | /* Drop queues */ | |
805 | skb_queue_purge(&hdev->rx_q); | |
806 | skb_queue_purge(&hdev->cmd_q); | |
807 | skb_queue_purge(&hdev->raw_q); | |
808 | ||
809 | /* Drop last sent command */ | |
810 | if (hdev->sent_cmd) { | |
b79f44c1 | 811 | del_timer_sync(&hdev->cmd_timer); |
1da177e4 LT |
812 | kfree_skb(hdev->sent_cmd); |
813 | hdev->sent_cmd = NULL; | |
814 | } | |
815 | ||
816 | /* After this point our queues are empty | |
817 | * and no tasks are scheduled. */ | |
818 | hdev->close(hdev); | |
819 | ||
8ee56540 MH |
820 | if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
821 | hci_dev_lock(hdev); | |
822 | mgmt_powered(hdev, 0); | |
823 | hci_dev_unlock(hdev); | |
824 | } | |
5add6af8 | 825 | |
1da177e4 LT |
826 | /* Clear flags */ |
827 | hdev->flags = 0; | |
828 | ||
e59fda8d | 829 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
09b3c3fb | 830 | memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); |
e59fda8d | 831 | |
1da177e4 LT |
832 | hci_req_unlock(hdev); |
833 | ||
834 | hci_dev_put(hdev); | |
835 | return 0; | |
836 | } | |
837 | ||
838 | int hci_dev_close(__u16 dev) | |
839 | { | |
840 | struct hci_dev *hdev; | |
841 | int err; | |
842 | ||
70f23020 AE |
843 | hdev = hci_dev_get(dev); |
844 | if (!hdev) | |
1da177e4 | 845 | return -ENODEV; |
8ee56540 MH |
846 | |
847 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) | |
848 | cancel_delayed_work(&hdev->power_off); | |
849 | ||
1da177e4 | 850 | err = hci_dev_do_close(hdev); |
8ee56540 | 851 | |
1da177e4 LT |
852 | hci_dev_put(hdev); |
853 | return err; | |
854 | } | |
855 | ||
856 | int hci_dev_reset(__u16 dev) | |
857 | { | |
858 | struct hci_dev *hdev; | |
859 | int ret = 0; | |
860 | ||
70f23020 AE |
861 | hdev = hci_dev_get(dev); |
862 | if (!hdev) | |
1da177e4 LT |
863 | return -ENODEV; |
864 | ||
865 | hci_req_lock(hdev); | |
1da177e4 LT |
866 | |
867 | if (!test_bit(HCI_UP, &hdev->flags)) | |
868 | goto done; | |
869 | ||
870 | /* Drop queues */ | |
871 | skb_queue_purge(&hdev->rx_q); | |
872 | skb_queue_purge(&hdev->cmd_q); | |
873 | ||
09fd0de5 | 874 | hci_dev_lock(hdev); |
1da177e4 LT |
875 | inquiry_cache_flush(hdev); |
876 | hci_conn_hash_flush(hdev); | |
09fd0de5 | 877 | hci_dev_unlock(hdev); |
1da177e4 LT |
878 | |
879 | if (hdev->flush) | |
880 | hdev->flush(hdev); | |
881 | ||
8e87d142 | 882 | atomic_set(&hdev->cmd_cnt, 1); |
6ed58ec5 | 883 | hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; |
1da177e4 LT |
884 | |
885 | if (!test_bit(HCI_RAW, &hdev->flags)) | |
04837f64 MH |
886 | ret = __hci_request(hdev, hci_reset_req, 0, |
887 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
888 | |
889 | done: | |
1da177e4 LT |
890 | hci_req_unlock(hdev); |
891 | hci_dev_put(hdev); | |
892 | return ret; | |
893 | } | |
894 | ||
895 | int hci_dev_reset_stat(__u16 dev) | |
896 | { | |
897 | struct hci_dev *hdev; | |
898 | int ret = 0; | |
899 | ||
70f23020 AE |
900 | hdev = hci_dev_get(dev); |
901 | if (!hdev) | |
1da177e4 LT |
902 | return -ENODEV; |
903 | ||
904 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); | |
905 | ||
906 | hci_dev_put(hdev); | |
907 | ||
908 | return ret; | |
909 | } | |
910 | ||
911 | int hci_dev_cmd(unsigned int cmd, void __user *arg) | |
912 | { | |
913 | struct hci_dev *hdev; | |
914 | struct hci_dev_req dr; | |
915 | int err = 0; | |
916 | ||
917 | if (copy_from_user(&dr, arg, sizeof(dr))) | |
918 | return -EFAULT; | |
919 | ||
70f23020 AE |
920 | hdev = hci_dev_get(dr.dev_id); |
921 | if (!hdev) | |
1da177e4 LT |
922 | return -ENODEV; |
923 | ||
924 | switch (cmd) { | |
925 | case HCISETAUTH: | |
04837f64 MH |
926 | err = hci_request(hdev, hci_auth_req, dr.dev_opt, |
927 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
928 | break; |
929 | ||
930 | case HCISETENCRYPT: | |
931 | if (!lmp_encrypt_capable(hdev)) { | |
932 | err = -EOPNOTSUPP; | |
933 | break; | |
934 | } | |
935 | ||
936 | if (!test_bit(HCI_AUTH, &hdev->flags)) { | |
937 | /* Auth must be enabled first */ | |
04837f64 MH |
938 | err = hci_request(hdev, hci_auth_req, dr.dev_opt, |
939 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
940 | if (err) |
941 | break; | |
942 | } | |
943 | ||
04837f64 MH |
944 | err = hci_request(hdev, hci_encrypt_req, dr.dev_opt, |
945 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
946 | break; |
947 | ||
948 | case HCISETSCAN: | |
04837f64 MH |
949 | err = hci_request(hdev, hci_scan_req, dr.dev_opt, |
950 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
951 | break; |
952 | ||
1da177e4 | 953 | case HCISETLINKPOL: |
e4e8e37c MH |
954 | err = hci_request(hdev, hci_linkpol_req, dr.dev_opt, |
955 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
956 | break; |
957 | ||
958 | case HCISETLINKMODE: | |
e4e8e37c MH |
959 | hdev->link_mode = ((__u16) dr.dev_opt) & |
960 | (HCI_LM_MASTER | HCI_LM_ACCEPT); | |
961 | break; | |
962 | ||
963 | case HCISETPTYPE: | |
964 | hdev->pkt_type = (__u16) dr.dev_opt; | |
1da177e4 LT |
965 | break; |
966 | ||
967 | case HCISETACLMTU: | |
e4e8e37c MH |
968 | hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); |
969 | hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); | |
1da177e4 LT |
970 | break; |
971 | ||
972 | case HCISETSCOMTU: | |
e4e8e37c MH |
973 | hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); |
974 | hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); | |
1da177e4 LT |
975 | break; |
976 | ||
977 | default: | |
978 | err = -EINVAL; | |
979 | break; | |
980 | } | |
e4e8e37c | 981 | |
1da177e4 LT |
982 | hci_dev_put(hdev); |
983 | return err; | |
984 | } | |
985 | ||
986 | int hci_get_dev_list(void __user *arg) | |
987 | { | |
8035ded4 | 988 | struct hci_dev *hdev; |
1da177e4 LT |
989 | struct hci_dev_list_req *dl; |
990 | struct hci_dev_req *dr; | |
1da177e4 LT |
991 | int n = 0, size, err; |
992 | __u16 dev_num; | |
993 | ||
994 | if (get_user(dev_num, (__u16 __user *) arg)) | |
995 | return -EFAULT; | |
996 | ||
997 | if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) | |
998 | return -EINVAL; | |
999 | ||
1000 | size = sizeof(*dl) + dev_num * sizeof(*dr); | |
1001 | ||
70f23020 AE |
1002 | dl = kzalloc(size, GFP_KERNEL); |
1003 | if (!dl) | |
1da177e4 LT |
1004 | return -ENOMEM; |
1005 | ||
1006 | dr = dl->dev_req; | |
1007 | ||
f20d09d5 | 1008 | read_lock(&hci_dev_list_lock); |
8035ded4 | 1009 | list_for_each_entry(hdev, &hci_dev_list, list) { |
a8b2d5c2 | 1010 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
e0f9309f | 1011 | cancel_delayed_work(&hdev->power_off); |
c542a06c | 1012 | |
a8b2d5c2 JH |
1013 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
1014 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); | |
c542a06c | 1015 | |
1da177e4 LT |
1016 | (dr + n)->dev_id = hdev->id; |
1017 | (dr + n)->dev_opt = hdev->flags; | |
c542a06c | 1018 | |
1da177e4 LT |
1019 | if (++n >= dev_num) |
1020 | break; | |
1021 | } | |
f20d09d5 | 1022 | read_unlock(&hci_dev_list_lock); |
1da177e4 LT |
1023 | |
1024 | dl->dev_num = n; | |
1025 | size = sizeof(*dl) + n * sizeof(*dr); | |
1026 | ||
1027 | err = copy_to_user(arg, dl, size); | |
1028 | kfree(dl); | |
1029 | ||
1030 | return err ? -EFAULT : 0; | |
1031 | } | |
1032 | ||
1033 | int hci_get_dev_info(void __user *arg) | |
1034 | { | |
1035 | struct hci_dev *hdev; | |
1036 | struct hci_dev_info di; | |
1037 | int err = 0; | |
1038 | ||
1039 | if (copy_from_user(&di, arg, sizeof(di))) | |
1040 | return -EFAULT; | |
1041 | ||
70f23020 AE |
1042 | hdev = hci_dev_get(di.dev_id); |
1043 | if (!hdev) | |
1da177e4 LT |
1044 | return -ENODEV; |
1045 | ||
a8b2d5c2 | 1046 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
3243553f | 1047 | cancel_delayed_work_sync(&hdev->power_off); |
ab81cbf9 | 1048 | |
a8b2d5c2 JH |
1049 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
1050 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); | |
c542a06c | 1051 | |
1da177e4 LT |
1052 | strcpy(di.name, hdev->name); |
1053 | di.bdaddr = hdev->bdaddr; | |
943da25d | 1054 | di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4); |
1da177e4 LT |
1055 | di.flags = hdev->flags; |
1056 | di.pkt_type = hdev->pkt_type; | |
1057 | di.acl_mtu = hdev->acl_mtu; | |
1058 | di.acl_pkts = hdev->acl_pkts; | |
1059 | di.sco_mtu = hdev->sco_mtu; | |
1060 | di.sco_pkts = hdev->sco_pkts; | |
1061 | di.link_policy = hdev->link_policy; | |
1062 | di.link_mode = hdev->link_mode; | |
1063 | ||
1064 | memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); | |
1065 | memcpy(&di.features, &hdev->features, sizeof(di.features)); | |
1066 | ||
1067 | if (copy_to_user(arg, &di, sizeof(di))) | |
1068 | err = -EFAULT; | |
1069 | ||
1070 | hci_dev_put(hdev); | |
1071 | ||
1072 | return err; | |
1073 | } | |
1074 | ||
1075 | /* ---- Interface to HCI drivers ---- */ | |
1076 | ||
611b30f7 MH |
1077 | static int hci_rfkill_set_block(void *data, bool blocked) |
1078 | { | |
1079 | struct hci_dev *hdev = data; | |
1080 | ||
1081 | BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); | |
1082 | ||
1083 | if (!blocked) | |
1084 | return 0; | |
1085 | ||
1086 | hci_dev_do_close(hdev); | |
1087 | ||
1088 | return 0; | |
1089 | } | |
1090 | ||
1091 | static const struct rfkill_ops hci_rfkill_ops = { | |
1092 | .set_block = hci_rfkill_set_block, | |
1093 | }; | |
1094 | ||
ab81cbf9 JH |
1095 | static void hci_power_on(struct work_struct *work) |
1096 | { | |
1097 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); | |
1098 | ||
1099 | BT_DBG("%s", hdev->name); | |
1100 | ||
1101 | if (hci_dev_open(hdev->id) < 0) | |
1102 | return; | |
1103 | ||
a8b2d5c2 | 1104 | if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
80b7ab33 | 1105 | schedule_delayed_work(&hdev->power_off, |
3243553f | 1106 | msecs_to_jiffies(AUTO_OFF_TIMEOUT)); |
ab81cbf9 | 1107 | |
a8b2d5c2 | 1108 | if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) |
744cf19e | 1109 | mgmt_index_added(hdev); |
ab81cbf9 JH |
1110 | } |
1111 | ||
1112 | static void hci_power_off(struct work_struct *work) | |
1113 | { | |
3243553f JH |
1114 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
1115 | power_off.work); | |
ab81cbf9 JH |
1116 | |
1117 | BT_DBG("%s", hdev->name); | |
1118 | ||
8ee56540 | 1119 | hci_dev_do_close(hdev); |
ab81cbf9 JH |
1120 | } |
1121 | ||
16ab91ab JH |
1122 | static void hci_discov_off(struct work_struct *work) |
1123 | { | |
1124 | struct hci_dev *hdev; | |
1125 | u8 scan = SCAN_PAGE; | |
1126 | ||
1127 | hdev = container_of(work, struct hci_dev, discov_off.work); | |
1128 | ||
1129 | BT_DBG("%s", hdev->name); | |
1130 | ||
09fd0de5 | 1131 | hci_dev_lock(hdev); |
16ab91ab JH |
1132 | |
1133 | hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); | |
1134 | ||
1135 | hdev->discov_timeout = 0; | |
1136 | ||
09fd0de5 | 1137 | hci_dev_unlock(hdev); |
16ab91ab JH |
1138 | } |
1139 | ||
2aeb9a1a JH |
1140 | int hci_uuids_clear(struct hci_dev *hdev) |
1141 | { | |
1142 | struct list_head *p, *n; | |
1143 | ||
1144 | list_for_each_safe(p, n, &hdev->uuids) { | |
1145 | struct bt_uuid *uuid; | |
1146 | ||
1147 | uuid = list_entry(p, struct bt_uuid, list); | |
1148 | ||
1149 | list_del(p); | |
1150 | kfree(uuid); | |
1151 | } | |
1152 | ||
1153 | return 0; | |
1154 | } | |
1155 | ||
55ed8ca1 JH |
1156 | int hci_link_keys_clear(struct hci_dev *hdev) |
1157 | { | |
1158 | struct list_head *p, *n; | |
1159 | ||
1160 | list_for_each_safe(p, n, &hdev->link_keys) { | |
1161 | struct link_key *key; | |
1162 | ||
1163 | key = list_entry(p, struct link_key, list); | |
1164 | ||
1165 | list_del(p); | |
1166 | kfree(key); | |
1167 | } | |
1168 | ||
1169 | return 0; | |
1170 | } | |
1171 | ||
b899efaf VCG |
1172 | int hci_smp_ltks_clear(struct hci_dev *hdev) |
1173 | { | |
1174 | struct smp_ltk *k, *tmp; | |
1175 | ||
1176 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { | |
1177 | list_del(&k->list); | |
1178 | kfree(k); | |
1179 | } | |
1180 | ||
1181 | return 0; | |
1182 | } | |
1183 | ||
55ed8ca1 JH |
1184 | struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
1185 | { | |
8035ded4 | 1186 | struct link_key *k; |
55ed8ca1 | 1187 | |
8035ded4 | 1188 | list_for_each_entry(k, &hdev->link_keys, list) |
55ed8ca1 JH |
1189 | if (bacmp(bdaddr, &k->bdaddr) == 0) |
1190 | return k; | |
55ed8ca1 JH |
1191 | |
1192 | return NULL; | |
1193 | } | |
1194 | ||
745c0ce3 | 1195 | static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn, |
d25e28ab JH |
1196 | u8 key_type, u8 old_key_type) |
1197 | { | |
1198 | /* Legacy key */ | |
1199 | if (key_type < 0x03) | |
745c0ce3 | 1200 | return true; |
d25e28ab JH |
1201 | |
1202 | /* Debug keys are insecure so don't store them persistently */ | |
1203 | if (key_type == HCI_LK_DEBUG_COMBINATION) | |
745c0ce3 | 1204 | return false; |
d25e28ab JH |
1205 | |
1206 | /* Changed combination key and there's no previous one */ | |
1207 | if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) | |
745c0ce3 | 1208 | return false; |
d25e28ab JH |
1209 | |
1210 | /* Security mode 3 case */ | |
1211 | if (!conn) | |
745c0ce3 | 1212 | return true; |
d25e28ab JH |
1213 | |
1214 | /* Neither local nor remote side had no-bonding as requirement */ | |
1215 | if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) | |
745c0ce3 | 1216 | return true; |
d25e28ab JH |
1217 | |
1218 | /* Local side had dedicated bonding as requirement */ | |
1219 | if (conn->auth_type == 0x02 || conn->auth_type == 0x03) | |
745c0ce3 | 1220 | return true; |
d25e28ab JH |
1221 | |
1222 | /* Remote side had dedicated bonding as requirement */ | |
1223 | if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) | |
745c0ce3 | 1224 | return true; |
d25e28ab JH |
1225 | |
1226 | /* If none of the above criteria match, then don't store the key | |
1227 | * persistently */ | |
745c0ce3 | 1228 | return false; |
d25e28ab JH |
1229 | } |
1230 | ||
c9839a11 | 1231 | struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]) |
75d262c2 | 1232 | { |
c9839a11 | 1233 | struct smp_ltk *k; |
75d262c2 | 1234 | |
c9839a11 VCG |
1235 | list_for_each_entry(k, &hdev->long_term_keys, list) { |
1236 | if (k->ediv != ediv || | |
1237 | memcmp(rand, k->rand, sizeof(k->rand))) | |
75d262c2 VCG |
1238 | continue; |
1239 | ||
c9839a11 | 1240 | return k; |
75d262c2 VCG |
1241 | } |
1242 | ||
1243 | return NULL; | |
1244 | } | |
1245 | EXPORT_SYMBOL(hci_find_ltk); | |
1246 | ||
c9839a11 | 1247 | struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, |
04124681 | 1248 | u8 addr_type) |
75d262c2 | 1249 | { |
c9839a11 | 1250 | struct smp_ltk *k; |
75d262c2 | 1251 | |
c9839a11 VCG |
1252 | list_for_each_entry(k, &hdev->long_term_keys, list) |
1253 | if (addr_type == k->bdaddr_type && | |
1254 | bacmp(bdaddr, &k->bdaddr) == 0) | |
75d262c2 VCG |
1255 | return k; |
1256 | ||
1257 | return NULL; | |
1258 | } | |
c9839a11 | 1259 | EXPORT_SYMBOL(hci_find_ltk_by_addr); |
75d262c2 | 1260 | |
d25e28ab | 1261 | int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key, |
04124681 | 1262 | bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len) |
55ed8ca1 JH |
1263 | { |
1264 | struct link_key *key, *old_key; | |
745c0ce3 VA |
1265 | u8 old_key_type; |
1266 | bool persistent; | |
55ed8ca1 JH |
1267 | |
1268 | old_key = hci_find_link_key(hdev, bdaddr); | |
1269 | if (old_key) { | |
1270 | old_key_type = old_key->type; | |
1271 | key = old_key; | |
1272 | } else { | |
12adcf3a | 1273 | old_key_type = conn ? conn->key_type : 0xff; |
55ed8ca1 JH |
1274 | key = kzalloc(sizeof(*key), GFP_ATOMIC); |
1275 | if (!key) | |
1276 | return -ENOMEM; | |
1277 | list_add(&key->list, &hdev->link_keys); | |
1278 | } | |
1279 | ||
1280 | BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type); | |
1281 | ||
d25e28ab JH |
1282 | /* Some buggy controller combinations generate a changed |
1283 | * combination key for legacy pairing even when there's no | |
1284 | * previous key */ | |
1285 | if (type == HCI_LK_CHANGED_COMBINATION && | |
1286 | (!conn || conn->remote_auth == 0xff) && | |
655fe6ec | 1287 | old_key_type == 0xff) { |
d25e28ab | 1288 | type = HCI_LK_COMBINATION; |
655fe6ec JH |
1289 | if (conn) |
1290 | conn->key_type = type; | |
1291 | } | |
d25e28ab | 1292 | |
55ed8ca1 JH |
1293 | bacpy(&key->bdaddr, bdaddr); |
1294 | memcpy(key->val, val, 16); | |
55ed8ca1 JH |
1295 | key->pin_len = pin_len; |
1296 | ||
b6020ba0 | 1297 | if (type == HCI_LK_CHANGED_COMBINATION) |
55ed8ca1 | 1298 | key->type = old_key_type; |
4748fed2 JH |
1299 | else |
1300 | key->type = type; | |
1301 | ||
4df378a1 JH |
1302 | if (!new_key) |
1303 | return 0; | |
1304 | ||
1305 | persistent = hci_persistent_key(hdev, conn, type, old_key_type); | |
1306 | ||
744cf19e | 1307 | mgmt_new_link_key(hdev, key, persistent); |
4df378a1 | 1308 | |
6ec5bcad VA |
1309 | if (conn) |
1310 | conn->flush_key = !persistent; | |
55ed8ca1 JH |
1311 | |
1312 | return 0; | |
1313 | } | |
1314 | ||
c9839a11 | 1315 | int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type, |
9a006657 | 1316 | int new_key, u8 authenticated, u8 tk[16], u8 enc_size, __le16 |
04124681 | 1317 | ediv, u8 rand[8]) |
75d262c2 | 1318 | { |
c9839a11 | 1319 | struct smp_ltk *key, *old_key; |
75d262c2 | 1320 | |
c9839a11 VCG |
1321 | if (!(type & HCI_SMP_STK) && !(type & HCI_SMP_LTK)) |
1322 | return 0; | |
75d262c2 | 1323 | |
c9839a11 VCG |
1324 | old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type); |
1325 | if (old_key) | |
75d262c2 | 1326 | key = old_key; |
c9839a11 VCG |
1327 | else { |
1328 | key = kzalloc(sizeof(*key), GFP_ATOMIC); | |
75d262c2 VCG |
1329 | if (!key) |
1330 | return -ENOMEM; | |
c9839a11 | 1331 | list_add(&key->list, &hdev->long_term_keys); |
75d262c2 VCG |
1332 | } |
1333 | ||
75d262c2 | 1334 | bacpy(&key->bdaddr, bdaddr); |
c9839a11 VCG |
1335 | key->bdaddr_type = addr_type; |
1336 | memcpy(key->val, tk, sizeof(key->val)); | |
1337 | key->authenticated = authenticated; | |
1338 | key->ediv = ediv; | |
1339 | key->enc_size = enc_size; | |
1340 | key->type = type; | |
1341 | memcpy(key->rand, rand, sizeof(key->rand)); | |
75d262c2 | 1342 | |
c9839a11 VCG |
1343 | if (!new_key) |
1344 | return 0; | |
75d262c2 | 1345 | |
261cc5aa VCG |
1346 | if (type & HCI_SMP_LTK) |
1347 | mgmt_new_ltk(hdev, key, 1); | |
1348 | ||
75d262c2 VCG |
1349 | return 0; |
1350 | } | |
1351 | ||
55ed8ca1 JH |
1352 | int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
1353 | { | |
1354 | struct link_key *key; | |
1355 | ||
1356 | key = hci_find_link_key(hdev, bdaddr); | |
1357 | if (!key) | |
1358 | return -ENOENT; | |
1359 | ||
1360 | BT_DBG("%s removing %s", hdev->name, batostr(bdaddr)); | |
1361 | ||
1362 | list_del(&key->list); | |
1363 | kfree(key); | |
1364 | ||
1365 | return 0; | |
1366 | } | |
1367 | ||
b899efaf VCG |
1368 | int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr) |
1369 | { | |
1370 | struct smp_ltk *k, *tmp; | |
1371 | ||
1372 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { | |
1373 | if (bacmp(bdaddr, &k->bdaddr)) | |
1374 | continue; | |
1375 | ||
1376 | BT_DBG("%s removing %s", hdev->name, batostr(bdaddr)); | |
1377 | ||
1378 | list_del(&k->list); | |
1379 | kfree(k); | |
1380 | } | |
1381 | ||
1382 | return 0; | |
1383 | } | |
1384 | ||
6bd32326 VT |
1385 | /* HCI command timer function */ |
1386 | static void hci_cmd_timer(unsigned long arg) | |
1387 | { | |
1388 | struct hci_dev *hdev = (void *) arg; | |
1389 | ||
1390 | BT_ERR("%s command tx timeout", hdev->name); | |
1391 | atomic_set(&hdev->cmd_cnt, 1); | |
c347b765 | 1392 | queue_work(hdev->workqueue, &hdev->cmd_work); |
6bd32326 VT |
1393 | } |
1394 | ||
2763eda6 | 1395 | struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, |
04124681 | 1396 | bdaddr_t *bdaddr) |
2763eda6 SJ |
1397 | { |
1398 | struct oob_data *data; | |
1399 | ||
1400 | list_for_each_entry(data, &hdev->remote_oob_data, list) | |
1401 | if (bacmp(bdaddr, &data->bdaddr) == 0) | |
1402 | return data; | |
1403 | ||
1404 | return NULL; | |
1405 | } | |
1406 | ||
1407 | int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr) | |
1408 | { | |
1409 | struct oob_data *data; | |
1410 | ||
1411 | data = hci_find_remote_oob_data(hdev, bdaddr); | |
1412 | if (!data) | |
1413 | return -ENOENT; | |
1414 | ||
1415 | BT_DBG("%s removing %s", hdev->name, batostr(bdaddr)); | |
1416 | ||
1417 | list_del(&data->list); | |
1418 | kfree(data); | |
1419 | ||
1420 | return 0; | |
1421 | } | |
1422 | ||
1423 | int hci_remote_oob_data_clear(struct hci_dev *hdev) | |
1424 | { | |
1425 | struct oob_data *data, *n; | |
1426 | ||
1427 | list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { | |
1428 | list_del(&data->list); | |
1429 | kfree(data); | |
1430 | } | |
1431 | ||
1432 | return 0; | |
1433 | } | |
1434 | ||
1435 | int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash, | |
04124681 | 1436 | u8 *randomizer) |
2763eda6 SJ |
1437 | { |
1438 | struct oob_data *data; | |
1439 | ||
1440 | data = hci_find_remote_oob_data(hdev, bdaddr); | |
1441 | ||
1442 | if (!data) { | |
1443 | data = kmalloc(sizeof(*data), GFP_ATOMIC); | |
1444 | if (!data) | |
1445 | return -ENOMEM; | |
1446 | ||
1447 | bacpy(&data->bdaddr, bdaddr); | |
1448 | list_add(&data->list, &hdev->remote_oob_data); | |
1449 | } | |
1450 | ||
1451 | memcpy(data->hash, hash, sizeof(data->hash)); | |
1452 | memcpy(data->randomizer, randomizer, sizeof(data->randomizer)); | |
1453 | ||
1454 | BT_DBG("%s for %s", hdev->name, batostr(bdaddr)); | |
1455 | ||
1456 | return 0; | |
1457 | } | |
1458 | ||
04124681 | 1459 | struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr) |
b2a66aad | 1460 | { |
8035ded4 | 1461 | struct bdaddr_list *b; |
b2a66aad | 1462 | |
8035ded4 | 1463 | list_for_each_entry(b, &hdev->blacklist, list) |
b2a66aad AJ |
1464 | if (bacmp(bdaddr, &b->bdaddr) == 0) |
1465 | return b; | |
b2a66aad AJ |
1466 | |
1467 | return NULL; | |
1468 | } | |
1469 | ||
1470 | int hci_blacklist_clear(struct hci_dev *hdev) | |
1471 | { | |
1472 | struct list_head *p, *n; | |
1473 | ||
1474 | list_for_each_safe(p, n, &hdev->blacklist) { | |
1475 | struct bdaddr_list *b; | |
1476 | ||
1477 | b = list_entry(p, struct bdaddr_list, list); | |
1478 | ||
1479 | list_del(p); | |
1480 | kfree(b); | |
1481 | } | |
1482 | ||
1483 | return 0; | |
1484 | } | |
1485 | ||
88c1fe4b | 1486 | int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
b2a66aad AJ |
1487 | { |
1488 | struct bdaddr_list *entry; | |
b2a66aad AJ |
1489 | |
1490 | if (bacmp(bdaddr, BDADDR_ANY) == 0) | |
1491 | return -EBADF; | |
1492 | ||
5e762444 AJ |
1493 | if (hci_blacklist_lookup(hdev, bdaddr)) |
1494 | return -EEXIST; | |
b2a66aad AJ |
1495 | |
1496 | entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); | |
5e762444 AJ |
1497 | if (!entry) |
1498 | return -ENOMEM; | |
b2a66aad AJ |
1499 | |
1500 | bacpy(&entry->bdaddr, bdaddr); | |
1501 | ||
1502 | list_add(&entry->list, &hdev->blacklist); | |
1503 | ||
88c1fe4b | 1504 | return mgmt_device_blocked(hdev, bdaddr, type); |
b2a66aad AJ |
1505 | } |
1506 | ||
88c1fe4b | 1507 | int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
b2a66aad AJ |
1508 | { |
1509 | struct bdaddr_list *entry; | |
b2a66aad | 1510 | |
1ec918ce | 1511 | if (bacmp(bdaddr, BDADDR_ANY) == 0) |
5e762444 | 1512 | return hci_blacklist_clear(hdev); |
b2a66aad AJ |
1513 | |
1514 | entry = hci_blacklist_lookup(hdev, bdaddr); | |
1ec918ce | 1515 | if (!entry) |
5e762444 | 1516 | return -ENOENT; |
b2a66aad AJ |
1517 | |
1518 | list_del(&entry->list); | |
1519 | kfree(entry); | |
1520 | ||
88c1fe4b | 1521 | return mgmt_device_unblocked(hdev, bdaddr, type); |
b2a66aad AJ |
1522 | } |
1523 | ||
db323f2f | 1524 | static void hci_clear_adv_cache(struct work_struct *work) |
35815085 | 1525 | { |
db323f2f | 1526 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
04124681 | 1527 | adv_work.work); |
35815085 AG |
1528 | |
1529 | hci_dev_lock(hdev); | |
1530 | ||
1531 | hci_adv_entries_clear(hdev); | |
1532 | ||
1533 | hci_dev_unlock(hdev); | |
1534 | } | |
1535 | ||
76c8686f AG |
1536 | int hci_adv_entries_clear(struct hci_dev *hdev) |
1537 | { | |
1538 | struct adv_entry *entry, *tmp; | |
1539 | ||
1540 | list_for_each_entry_safe(entry, tmp, &hdev->adv_entries, list) { | |
1541 | list_del(&entry->list); | |
1542 | kfree(entry); | |
1543 | } | |
1544 | ||
1545 | BT_DBG("%s adv cache cleared", hdev->name); | |
1546 | ||
1547 | return 0; | |
1548 | } | |
1549 | ||
1550 | struct adv_entry *hci_find_adv_entry(struct hci_dev *hdev, bdaddr_t *bdaddr) | |
1551 | { | |
1552 | struct adv_entry *entry; | |
1553 | ||
1554 | list_for_each_entry(entry, &hdev->adv_entries, list) | |
1555 | if (bacmp(bdaddr, &entry->bdaddr) == 0) | |
1556 | return entry; | |
1557 | ||
1558 | return NULL; | |
1559 | } | |
1560 | ||
1561 | static inline int is_connectable_adv(u8 evt_type) | |
1562 | { | |
1563 | if (evt_type == ADV_IND || evt_type == ADV_DIRECT_IND) | |
1564 | return 1; | |
1565 | ||
1566 | return 0; | |
1567 | } | |
1568 | ||
1569 | int hci_add_adv_entry(struct hci_dev *hdev, | |
04124681 | 1570 | struct hci_ev_le_advertising_info *ev) { struct adv_entry *entry; if (!is_connectable_adv(ev->evt_type)) |
76c8686f AG |
1571 | return -EINVAL; |
1572 | ||
1573 | /* Only new entries should be added to adv_entries. So, if | |
1574 | * bdaddr was found, don't add it. */ | |
1575 | if (hci_find_adv_entry(hdev, &ev->bdaddr)) | |
1576 | return 0; | |
1577 | ||
4777bfde | 1578 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
76c8686f AG |
1579 | if (!entry) |
1580 | return -ENOMEM; | |
1581 | ||
1582 | bacpy(&entry->bdaddr, &ev->bdaddr); | |
1583 | entry->bdaddr_type = ev->bdaddr_type; | |
1584 | ||
1585 | list_add(&entry->list, &hdev->adv_entries); | |
1586 | ||
1587 | BT_DBG("%s adv entry added: address %s type %u", hdev->name, | |
1588 | batostr(&entry->bdaddr), entry->bdaddr_type); | |
1589 | ||
1590 | return 0; | |
1591 | } | |
1592 | ||
7ba8b4be AG |
1593 | static void le_scan_param_req(struct hci_dev *hdev, unsigned long opt) |
1594 | { | |
1595 | struct le_scan_params *param = (struct le_scan_params *) opt; | |
1596 | struct hci_cp_le_set_scan_param cp; | |
1597 | ||
1598 | memset(&cp, 0, sizeof(cp)); | |
1599 | cp.type = param->type; | |
1600 | cp.interval = cpu_to_le16(param->interval); | |
1601 | cp.window = cpu_to_le16(param->window); | |
1602 | ||
1603 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_PARAM, sizeof(cp), &cp); | |
1604 | } | |
1605 | ||
1606 | static void le_scan_enable_req(struct hci_dev *hdev, unsigned long opt) | |
1607 | { | |
1608 | struct hci_cp_le_set_scan_enable cp; | |
1609 | ||
1610 | memset(&cp, 0, sizeof(cp)); | |
1611 | cp.enable = 1; | |
1612 | ||
1613 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); | |
1614 | } | |
1615 | ||
1616 | static int hci_do_le_scan(struct hci_dev *hdev, u8 type, u16 interval, | |
04124681 | 1617 | u16 window, int timeout) |
7ba8b4be AG |
1618 | { |
1619 | long timeo = msecs_to_jiffies(3000); | |
1620 | struct le_scan_params param; | |
1621 | int err; | |
1622 | ||
1623 | BT_DBG("%s", hdev->name); | |
1624 | ||
1625 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) | |
1626 | return -EINPROGRESS; | |
1627 | ||
1628 | param.type = type; | |
1629 | param.interval = interval; | |
1630 | param.window = window; | |
1631 | ||
1632 | hci_req_lock(hdev); | |
1633 | ||
1634 | err = __hci_request(hdev, le_scan_param_req, (unsigned long) ¶m, | |
04124681 | 1635 | timeo); |
7ba8b4be AG |
1636 | if (!err) |
1637 | err = __hci_request(hdev, le_scan_enable_req, 0, timeo); | |
1638 | ||
1639 | hci_req_unlock(hdev); | |
1640 | ||
1641 | if (err < 0) | |
1642 | return err; | |
1643 | ||
1644 | schedule_delayed_work(&hdev->le_scan_disable, | |
04124681 | 1645 | msecs_to_jiffies(timeout)); |
7ba8b4be AG |
1646 | |
1647 | return 0; | |
1648 | } | |
1649 | ||
7dbfac1d AG |
1650 | int hci_cancel_le_scan(struct hci_dev *hdev) |
1651 | { | |
1652 | BT_DBG("%s", hdev->name); | |
1653 | ||
1654 | if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags)) | |
1655 | return -EALREADY; | |
1656 | ||
1657 | if (cancel_delayed_work(&hdev->le_scan_disable)) { | |
1658 | struct hci_cp_le_set_scan_enable cp; | |
1659 | ||
1660 | /* Send HCI command to disable LE Scan */ | |
1661 | memset(&cp, 0, sizeof(cp)); | |
1662 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); | |
1663 | } | |
1664 | ||
1665 | return 0; | |
1666 | } | |
1667 | ||
7ba8b4be AG |
1668 | static void le_scan_disable_work(struct work_struct *work) |
1669 | { | |
1670 | struct hci_dev *hdev = container_of(work, struct hci_dev, | |
04124681 | 1671 | le_scan_disable.work); |
7ba8b4be AG |
1672 | struct hci_cp_le_set_scan_enable cp; |
1673 | ||
1674 | BT_DBG("%s", hdev->name); | |
1675 | ||
1676 | memset(&cp, 0, sizeof(cp)); | |
1677 | ||
1678 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); | |
1679 | } | |
1680 | ||
28b75a89 AG |
1681 | static void le_scan_work(struct work_struct *work) |
1682 | { | |
1683 | struct hci_dev *hdev = container_of(work, struct hci_dev, le_scan); | |
1684 | struct le_scan_params *param = &hdev->le_scan_params; | |
1685 | ||
1686 | BT_DBG("%s", hdev->name); | |
1687 | ||
04124681 GP |
1688 | hci_do_le_scan(hdev, param->type, param->interval, param->window, |
1689 | param->timeout); | |
28b75a89 AG |
1690 | } |
1691 | ||
1692 | int hci_le_scan(struct hci_dev *hdev, u8 type, u16 interval, u16 window, | |
04124681 | 1693 | int timeout) |
28b75a89 AG |
1694 | { |
1695 | struct le_scan_params *param = &hdev->le_scan_params; | |
1696 | ||
1697 | BT_DBG("%s", hdev->name); | |
1698 | ||
1699 | if (work_busy(&hdev->le_scan)) | |
1700 | return -EINPROGRESS; | |
1701 | ||
1702 | param->type = type; | |
1703 | param->interval = interval; | |
1704 | param->window = window; | |
1705 | param->timeout = timeout; | |
1706 | ||
1707 | queue_work(system_long_wq, &hdev->le_scan); | |
1708 | ||
1709 | return 0; | |
1710 | } | |
1711 | ||
9be0dab7 DH |
1712 | /* Alloc HCI device */ |
1713 | struct hci_dev *hci_alloc_dev(void) | |
1714 | { | |
1715 | struct hci_dev *hdev; | |
1716 | ||
1717 | hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); | |
1718 | if (!hdev) | |
1719 | return NULL; | |
1720 | ||
b1b813d4 DH |
1721 | hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); |
1722 | hdev->esco_type = (ESCO_HV1); | |
1723 | hdev->link_mode = (HCI_LM_ACCEPT); | |
1724 | hdev->io_capability = 0x03; /* No Input No Output */ | |
1725 | ||
b1b813d4 DH |
1726 | hdev->sniff_max_interval = 800; |
1727 | hdev->sniff_min_interval = 80; | |
1728 | ||
1729 | mutex_init(&hdev->lock); | |
1730 | mutex_init(&hdev->req_lock); | |
1731 | ||
1732 | INIT_LIST_HEAD(&hdev->mgmt_pending); | |
1733 | INIT_LIST_HEAD(&hdev->blacklist); | |
1734 | INIT_LIST_HEAD(&hdev->uuids); | |
1735 | INIT_LIST_HEAD(&hdev->link_keys); | |
1736 | INIT_LIST_HEAD(&hdev->long_term_keys); | |
1737 | INIT_LIST_HEAD(&hdev->remote_oob_data); | |
1738 | INIT_LIST_HEAD(&hdev->adv_entries); | |
1739 | ||
1740 | INIT_WORK(&hdev->rx_work, hci_rx_work); | |
1741 | INIT_WORK(&hdev->cmd_work, hci_cmd_work); | |
1742 | INIT_WORK(&hdev->tx_work, hci_tx_work); | |
1743 | INIT_WORK(&hdev->power_on, hci_power_on); | |
1744 | INIT_WORK(&hdev->le_scan, le_scan_work); | |
1745 | ||
1746 | INIT_DELAYED_WORK(&hdev->adv_work, hci_clear_adv_cache); | |
1747 | INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); | |
1748 | INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off); | |
1749 | INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work); | |
1750 | ||
9be0dab7 | 1751 | skb_queue_head_init(&hdev->driver_init); |
b1b813d4 DH |
1752 | skb_queue_head_init(&hdev->rx_q); |
1753 | skb_queue_head_init(&hdev->cmd_q); | |
1754 | skb_queue_head_init(&hdev->raw_q); | |
1755 | ||
1756 | init_waitqueue_head(&hdev->req_wait_q); | |
1757 | ||
1758 | setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev); | |
1759 | ||
b1b813d4 DH |
1760 | hci_init_sysfs(hdev); |
1761 | discovery_init(hdev); | |
1762 | hci_conn_hash_init(hdev); | |
9be0dab7 DH |
1763 | |
1764 | return hdev; | |
1765 | } | |
1766 | EXPORT_SYMBOL(hci_alloc_dev); | |
1767 | ||
1768 | /* Free HCI device */ | |
1769 | void hci_free_dev(struct hci_dev *hdev) | |
1770 | { | |
1771 | skb_queue_purge(&hdev->driver_init); | |
1772 | ||
1773 | /* will free via device release */ | |
1774 | put_device(&hdev->dev); | |
1775 | } | |
1776 | EXPORT_SYMBOL(hci_free_dev); | |
1777 | ||
1da177e4 LT |
1778 | /* Register HCI device */ |
1779 | int hci_register_dev(struct hci_dev *hdev) | |
1780 | { | |
fc50744c | 1781 | struct list_head *head, *p; |
b1b813d4 | 1782 | int id, error; |
1da177e4 | 1783 | |
010666a1 | 1784 | if (!hdev->open || !hdev->close) |
1da177e4 LT |
1785 | return -EINVAL; |
1786 | ||
fc50744c UF |
1787 | write_lock(&hci_dev_list_lock); |
1788 | ||
08add513 MM |
1789 | /* Do not allow HCI_AMP devices to register at index 0, |
1790 | * so the index can be used as the AMP controller ID. | |
1791 | */ | |
1792 | id = (hdev->dev_type == HCI_BREDR) ? 0 : 1; | |
fc50744c | 1793 | head = &hci_dev_list; |
1da177e4 LT |
1794 | |
1795 | /* Find first available device id */ | |
1796 | list_for_each(p, &hci_dev_list) { | |
fc50744c UF |
1797 | int nid = list_entry(p, struct hci_dev, list)->id; |
1798 | if (nid > id) | |
1da177e4 | 1799 | break; |
fc50744c UF |
1800 | if (nid == id) |
1801 | id++; | |
1802 | head = p; | |
1da177e4 | 1803 | } |
8e87d142 | 1804 | |
1da177e4 LT |
1805 | sprintf(hdev->name, "hci%d", id); |
1806 | hdev->id = id; | |
2d8b3a11 AE |
1807 | |
1808 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); | |
1809 | ||
fc50744c | 1810 | list_add(&hdev->list, head); |
1da177e4 | 1811 | |
f20d09d5 | 1812 | write_unlock(&hci_dev_list_lock); |
1da177e4 | 1813 | |
32845eb1 GP |
1814 | hdev->workqueue = alloc_workqueue(hdev->name, WQ_HIGHPRI | WQ_UNBOUND | |
1815 | WQ_MEM_RECLAIM, 1); | |
33ca954d DH |
1816 | if (!hdev->workqueue) { |
1817 | error = -ENOMEM; | |
1818 | goto err; | |
1819 | } | |
f48fd9c8 | 1820 | |
33ca954d DH |
1821 | error = hci_add_sysfs(hdev); |
1822 | if (error < 0) | |
1823 | goto err_wqueue; | |
1da177e4 | 1824 | |
611b30f7 MH |
1825 | hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, |
1826 | RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, hdev); | |
1827 | if (hdev->rfkill) { | |
1828 | if (rfkill_register(hdev->rfkill) < 0) { | |
1829 | rfkill_destroy(hdev->rfkill); | |
1830 | hdev->rfkill = NULL; | |
1831 | } | |
1832 | } | |
1833 | ||
a8b2d5c2 JH |
1834 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
1835 | set_bit(HCI_SETUP, &hdev->dev_flags); | |
7f971041 | 1836 | schedule_work(&hdev->power_on); |
ab81cbf9 | 1837 | |
1da177e4 | 1838 | hci_notify(hdev, HCI_DEV_REG); |
dc946bd8 | 1839 | hci_dev_hold(hdev); |
1da177e4 LT |
1840 | |
1841 | return id; | |
f48fd9c8 | 1842 | |
33ca954d DH |
1843 | err_wqueue: |
1844 | destroy_workqueue(hdev->workqueue); | |
1845 | err: | |
f20d09d5 | 1846 | write_lock(&hci_dev_list_lock); |
f48fd9c8 | 1847 | list_del(&hdev->list); |
f20d09d5 | 1848 | write_unlock(&hci_dev_list_lock); |
f48fd9c8 | 1849 | |
33ca954d | 1850 | return error; |
1da177e4 LT |
1851 | } |
1852 | EXPORT_SYMBOL(hci_register_dev); | |
1853 | ||
1854 | /* Unregister HCI device */ | |
59735631 | 1855 | void hci_unregister_dev(struct hci_dev *hdev) |
1da177e4 | 1856 | { |
ef222013 MH |
1857 | int i; |
1858 | ||
c13854ce | 1859 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
1da177e4 | 1860 | |
94324962 JH |
1861 | set_bit(HCI_UNREGISTER, &hdev->dev_flags); |
1862 | ||
f20d09d5 | 1863 | write_lock(&hci_dev_list_lock); |
1da177e4 | 1864 | list_del(&hdev->list); |
f20d09d5 | 1865 | write_unlock(&hci_dev_list_lock); |
1da177e4 LT |
1866 | |
1867 | hci_dev_do_close(hdev); | |
1868 | ||
cd4c5391 | 1869 | for (i = 0; i < NUM_REASSEMBLY; i++) |
ef222013 MH |
1870 | kfree_skb(hdev->reassembly[i]); |
1871 | ||
ab81cbf9 | 1872 | if (!test_bit(HCI_INIT, &hdev->flags) && |
a8b2d5c2 | 1873 | !test_bit(HCI_SETUP, &hdev->dev_flags)) { |
09fd0de5 | 1874 | hci_dev_lock(hdev); |
744cf19e | 1875 | mgmt_index_removed(hdev); |
09fd0de5 | 1876 | hci_dev_unlock(hdev); |
56e5cb86 | 1877 | } |
ab81cbf9 | 1878 | |
2e58ef3e JH |
1879 | /* mgmt_index_removed should take care of emptying the |
1880 | * pending list */ | |
1881 | BUG_ON(!list_empty(&hdev->mgmt_pending)); | |
1882 | ||
1da177e4 LT |
1883 | hci_notify(hdev, HCI_DEV_UNREG); |
1884 | ||
611b30f7 MH |
1885 | if (hdev->rfkill) { |
1886 | rfkill_unregister(hdev->rfkill); | |
1887 | rfkill_destroy(hdev->rfkill); | |
1888 | } | |
1889 | ||
ce242970 | 1890 | hci_del_sysfs(hdev); |
147e2d59 | 1891 | |
db323f2f | 1892 | cancel_delayed_work_sync(&hdev->adv_work); |
c6f3c5f7 | 1893 | |
f48fd9c8 MH |
1894 | destroy_workqueue(hdev->workqueue); |
1895 | ||
09fd0de5 | 1896 | hci_dev_lock(hdev); |
e2e0cacb | 1897 | hci_blacklist_clear(hdev); |
2aeb9a1a | 1898 | hci_uuids_clear(hdev); |
55ed8ca1 | 1899 | hci_link_keys_clear(hdev); |
b899efaf | 1900 | hci_smp_ltks_clear(hdev); |
2763eda6 | 1901 | hci_remote_oob_data_clear(hdev); |
76c8686f | 1902 | hci_adv_entries_clear(hdev); |
09fd0de5 | 1903 | hci_dev_unlock(hdev); |
e2e0cacb | 1904 | |
dc946bd8 | 1905 | hci_dev_put(hdev); |
1da177e4 LT |
1906 | } |
1907 | EXPORT_SYMBOL(hci_unregister_dev); | |
1908 | ||
1909 | /* Suspend HCI device */ | |
1910 | int hci_suspend_dev(struct hci_dev *hdev) | |
1911 | { | |
1912 | hci_notify(hdev, HCI_DEV_SUSPEND); | |
1913 | return 0; | |
1914 | } | |
1915 | EXPORT_SYMBOL(hci_suspend_dev); | |
1916 | ||
1917 | /* Resume HCI device */ | |
1918 | int hci_resume_dev(struct hci_dev *hdev) | |
1919 | { | |
1920 | hci_notify(hdev, HCI_DEV_RESUME); | |
1921 | return 0; | |
1922 | } | |
1923 | EXPORT_SYMBOL(hci_resume_dev); | |
1924 | ||
76bca880 MH |
1925 | /* Receive frame from HCI drivers */ |
1926 | int hci_recv_frame(struct sk_buff *skb) | |
1927 | { | |
1928 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
1929 | if (!hdev || (!test_bit(HCI_UP, &hdev->flags) | |
1930 | && !test_bit(HCI_INIT, &hdev->flags))) { | |
1931 | kfree_skb(skb); | |
1932 | return -ENXIO; | |
1933 | } | |
1934 | ||
1935 | /* Incomming skb */ | |
1936 | bt_cb(skb)->incoming = 1; | |
1937 | ||
1938 | /* Time stamp */ | |
1939 | __net_timestamp(skb); | |
1940 | ||
76bca880 | 1941 | skb_queue_tail(&hdev->rx_q, skb); |
b78752cc | 1942 | queue_work(hdev->workqueue, &hdev->rx_work); |
c78ae283 | 1943 | |
76bca880 MH |
1944 | return 0; |
1945 | } | |
1946 | EXPORT_SYMBOL(hci_recv_frame); | |
1947 | ||
33e882a5 | 1948 | static int hci_reassembly(struct hci_dev *hdev, int type, void *data, |
1e429f38 | 1949 | int count, __u8 index) |
33e882a5 SS |
1950 | { |
1951 | int len = 0; | |
1952 | int hlen = 0; | |
1953 | int remain = count; | |
1954 | struct sk_buff *skb; | |
1955 | struct bt_skb_cb *scb; | |
1956 | ||
1957 | if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || | |
1958 | index >= NUM_REASSEMBLY) | |
1959 | return -EILSEQ; | |
1960 | ||
1961 | skb = hdev->reassembly[index]; | |
1962 | ||
1963 | if (!skb) { | |
1964 | switch (type) { | |
1965 | case HCI_ACLDATA_PKT: | |
1966 | len = HCI_MAX_FRAME_SIZE; | |
1967 | hlen = HCI_ACL_HDR_SIZE; | |
1968 | break; | |
1969 | case HCI_EVENT_PKT: | |
1970 | len = HCI_MAX_EVENT_SIZE; | |
1971 | hlen = HCI_EVENT_HDR_SIZE; | |
1972 | break; | |
1973 | case HCI_SCODATA_PKT: | |
1974 | len = HCI_MAX_SCO_SIZE; | |
1975 | hlen = HCI_SCO_HDR_SIZE; | |
1976 | break; | |
1977 | } | |
1978 | ||
1e429f38 | 1979 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
33e882a5 SS |
1980 | if (!skb) |
1981 | return -ENOMEM; | |
1982 | ||
1983 | scb = (void *) skb->cb; | |
1984 | scb->expect = hlen; | |
1985 | scb->pkt_type = type; | |
1986 | ||
1987 | skb->dev = (void *) hdev; | |
1988 | hdev->reassembly[index] = skb; | |
1989 | } | |
1990 | ||
1991 | while (count) { | |
1992 | scb = (void *) skb->cb; | |
89bb46d0 | 1993 | len = min_t(uint, scb->expect, count); |
33e882a5 SS |
1994 | |
1995 | memcpy(skb_put(skb, len), data, len); | |
1996 | ||
1997 | count -= len; | |
1998 | data += len; | |
1999 | scb->expect -= len; | |
2000 | remain = count; | |
2001 | ||
2002 | switch (type) { | |
2003 | case HCI_EVENT_PKT: | |
2004 | if (skb->len == HCI_EVENT_HDR_SIZE) { | |
2005 | struct hci_event_hdr *h = hci_event_hdr(skb); | |
2006 | scb->expect = h->plen; | |
2007 | ||
2008 | if (skb_tailroom(skb) < scb->expect) { | |
2009 | kfree_skb(skb); | |
2010 | hdev->reassembly[index] = NULL; | |
2011 | return -ENOMEM; | |
2012 | } | |
2013 | } | |
2014 | break; | |
2015 | ||
2016 | case HCI_ACLDATA_PKT: | |
2017 | if (skb->len == HCI_ACL_HDR_SIZE) { | |
2018 | struct hci_acl_hdr *h = hci_acl_hdr(skb); | |
2019 | scb->expect = __le16_to_cpu(h->dlen); | |
2020 | ||
2021 | if (skb_tailroom(skb) < scb->expect) { | |
2022 | kfree_skb(skb); | |
2023 | hdev->reassembly[index] = NULL; | |
2024 | return -ENOMEM; | |
2025 | } | |
2026 | } | |
2027 | break; | |
2028 | ||
2029 | case HCI_SCODATA_PKT: | |
2030 | if (skb->len == HCI_SCO_HDR_SIZE) { | |
2031 | struct hci_sco_hdr *h = hci_sco_hdr(skb); | |
2032 | scb->expect = h->dlen; | |
2033 | ||
2034 | if (skb_tailroom(skb) < scb->expect) { | |
2035 | kfree_skb(skb); | |
2036 | hdev->reassembly[index] = NULL; | |
2037 | return -ENOMEM; | |
2038 | } | |
2039 | } | |
2040 | break; | |
2041 | } | |
2042 | ||
2043 | if (scb->expect == 0) { | |
2044 | /* Complete frame */ | |
2045 | ||
2046 | bt_cb(skb)->pkt_type = type; | |
2047 | hci_recv_frame(skb); | |
2048 | ||
2049 | hdev->reassembly[index] = NULL; | |
2050 | return remain; | |
2051 | } | |
2052 | } | |
2053 | ||
2054 | return remain; | |
2055 | } | |
2056 | ||
ef222013 MH |
2057 | int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) |
2058 | { | |
f39a3c06 SS |
2059 | int rem = 0; |
2060 | ||
ef222013 MH |
2061 | if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) |
2062 | return -EILSEQ; | |
2063 | ||
da5f6c37 | 2064 | while (count) { |
1e429f38 | 2065 | rem = hci_reassembly(hdev, type, data, count, type - 1); |
f39a3c06 SS |
2066 | if (rem < 0) |
2067 | return rem; | |
ef222013 | 2068 | |
f39a3c06 SS |
2069 | data += (count - rem); |
2070 | count = rem; | |
f81c6224 | 2071 | } |
ef222013 | 2072 | |
f39a3c06 | 2073 | return rem; |
ef222013 MH |
2074 | } |
2075 | EXPORT_SYMBOL(hci_recv_fragment); | |
2076 | ||
99811510 SS |
2077 | #define STREAM_REASSEMBLY 0 |
2078 | ||
2079 | int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count) | |
2080 | { | |
2081 | int type; | |
2082 | int rem = 0; | |
2083 | ||
da5f6c37 | 2084 | while (count) { |
99811510 SS |
2085 | struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY]; |
2086 | ||
2087 | if (!skb) { | |
2088 | struct { char type; } *pkt; | |
2089 | ||
2090 | /* Start of the frame */ | |
2091 | pkt = data; | |
2092 | type = pkt->type; | |
2093 | ||
2094 | data++; | |
2095 | count--; | |
2096 | } else | |
2097 | type = bt_cb(skb)->pkt_type; | |
2098 | ||
1e429f38 GP |
2099 | rem = hci_reassembly(hdev, type, data, count, |
2100 | STREAM_REASSEMBLY); | |
99811510 SS |
2101 | if (rem < 0) |
2102 | return rem; | |
2103 | ||
2104 | data += (count - rem); | |
2105 | count = rem; | |
f81c6224 | 2106 | } |
99811510 SS |
2107 | |
2108 | return rem; | |
2109 | } | |
2110 | EXPORT_SYMBOL(hci_recv_stream_fragment); | |
2111 | ||
1da177e4 LT |
2112 | /* ---- Interface to upper protocols ---- */ |
2113 | ||
1da177e4 LT |
2114 | int hci_register_cb(struct hci_cb *cb) |
2115 | { | |
2116 | BT_DBG("%p name %s", cb, cb->name); | |
2117 | ||
f20d09d5 | 2118 | write_lock(&hci_cb_list_lock); |
1da177e4 | 2119 | list_add(&cb->list, &hci_cb_list); |
f20d09d5 | 2120 | write_unlock(&hci_cb_list_lock); |
1da177e4 LT |
2121 | |
2122 | return 0; | |
2123 | } | |
2124 | EXPORT_SYMBOL(hci_register_cb); | |
2125 | ||
2126 | int hci_unregister_cb(struct hci_cb *cb) | |
2127 | { | |
2128 | BT_DBG("%p name %s", cb, cb->name); | |
2129 | ||
f20d09d5 | 2130 | write_lock(&hci_cb_list_lock); |
1da177e4 | 2131 | list_del(&cb->list); |
f20d09d5 | 2132 | write_unlock(&hci_cb_list_lock); |
1da177e4 LT |
2133 | |
2134 | return 0; | |
2135 | } | |
2136 | EXPORT_SYMBOL(hci_unregister_cb); | |
2137 | ||
2138 | static int hci_send_frame(struct sk_buff *skb) | |
2139 | { | |
2140 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
2141 | ||
2142 | if (!hdev) { | |
2143 | kfree_skb(skb); | |
2144 | return -ENODEV; | |
2145 | } | |
2146 | ||
0d48d939 | 2147 | BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); |
1da177e4 | 2148 | |
cd82e61c MH |
2149 | /* Time stamp */ |
2150 | __net_timestamp(skb); | |
1da177e4 | 2151 | |
cd82e61c MH |
2152 | /* Send copy to monitor */ |
2153 | hci_send_to_monitor(hdev, skb); | |
2154 | ||
2155 | if (atomic_read(&hdev->promisc)) { | |
2156 | /* Send copy to the sockets */ | |
470fe1b5 | 2157 | hci_send_to_sock(hdev, skb); |
1da177e4 LT |
2158 | } |
2159 | ||
2160 | /* Get rid of skb owner, prior to sending to the driver. */ | |
2161 | skb_orphan(skb); | |
2162 | ||
2163 | return hdev->send(skb); | |
2164 | } | |
2165 | ||
2166 | /* Send HCI command */ | |
a9de9248 | 2167 | int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param) |
1da177e4 LT |
2168 | { |
2169 | int len = HCI_COMMAND_HDR_SIZE + plen; | |
2170 | struct hci_command_hdr *hdr; | |
2171 | struct sk_buff *skb; | |
2172 | ||
a9de9248 | 2173 | BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen); |
1da177e4 LT |
2174 | |
2175 | skb = bt_skb_alloc(len, GFP_ATOMIC); | |
2176 | if (!skb) { | |
ef222013 | 2177 | BT_ERR("%s no memory for command", hdev->name); |
1da177e4 LT |
2178 | return -ENOMEM; |
2179 | } | |
2180 | ||
2181 | hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); | |
a9de9248 | 2182 | hdr->opcode = cpu_to_le16(opcode); |
1da177e4 LT |
2183 | hdr->plen = plen; |
2184 | ||
2185 | if (plen) | |
2186 | memcpy(skb_put(skb, plen), param, plen); | |
2187 | ||
2188 | BT_DBG("skb len %d", skb->len); | |
2189 | ||
0d48d939 | 2190 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
1da177e4 | 2191 | skb->dev = (void *) hdev; |
c78ae283 | 2192 | |
a5040efa JH |
2193 | if (test_bit(HCI_INIT, &hdev->flags)) |
2194 | hdev->init_last_cmd = opcode; | |
2195 | ||
1da177e4 | 2196 | skb_queue_tail(&hdev->cmd_q, skb); |
c347b765 | 2197 | queue_work(hdev->workqueue, &hdev->cmd_work); |
1da177e4 LT |
2198 | |
2199 | return 0; | |
2200 | } | |
1da177e4 LT |
2201 | |
2202 | /* Get data from the previously sent command */ | |
a9de9248 | 2203 | void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) |
1da177e4 LT |
2204 | { |
2205 | struct hci_command_hdr *hdr; | |
2206 | ||
2207 | if (!hdev->sent_cmd) | |
2208 | return NULL; | |
2209 | ||
2210 | hdr = (void *) hdev->sent_cmd->data; | |
2211 | ||
a9de9248 | 2212 | if (hdr->opcode != cpu_to_le16(opcode)) |
1da177e4 LT |
2213 | return NULL; |
2214 | ||
a9de9248 | 2215 | BT_DBG("%s opcode 0x%x", hdev->name, opcode); |
1da177e4 LT |
2216 | |
2217 | return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; | |
2218 | } | |
2219 | ||
2220 | /* Send ACL data */ | |
2221 | static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) | |
2222 | { | |
2223 | struct hci_acl_hdr *hdr; | |
2224 | int len = skb->len; | |
2225 | ||
badff6d0 ACM |
2226 | skb_push(skb, HCI_ACL_HDR_SIZE); |
2227 | skb_reset_transport_header(skb); | |
9c70220b | 2228 | hdr = (struct hci_acl_hdr *)skb_transport_header(skb); |
aca3192c YH |
2229 | hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); |
2230 | hdr->dlen = cpu_to_le16(len); | |
1da177e4 LT |
2231 | } |
2232 | ||
73d80deb LAD |
2233 | static void hci_queue_acl(struct hci_conn *conn, struct sk_buff_head *queue, |
2234 | struct sk_buff *skb, __u16 flags) | |
1da177e4 LT |
2235 | { |
2236 | struct hci_dev *hdev = conn->hdev; | |
2237 | struct sk_buff *list; | |
2238 | ||
70f23020 AE |
2239 | list = skb_shinfo(skb)->frag_list; |
2240 | if (!list) { | |
1da177e4 LT |
2241 | /* Non fragmented */ |
2242 | BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); | |
2243 | ||
73d80deb | 2244 | skb_queue_tail(queue, skb); |
1da177e4 LT |
2245 | } else { |
2246 | /* Fragmented */ | |
2247 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); | |
2248 | ||
2249 | skb_shinfo(skb)->frag_list = NULL; | |
2250 | ||
2251 | /* Queue all fragments atomically */ | |
af3e6359 | 2252 | spin_lock(&queue->lock); |
1da177e4 | 2253 | |
73d80deb | 2254 | __skb_queue_tail(queue, skb); |
e702112f AE |
2255 | |
2256 | flags &= ~ACL_START; | |
2257 | flags |= ACL_CONT; | |
1da177e4 LT |
2258 | do { |
2259 | skb = list; list = list->next; | |
8e87d142 | 2260 | |
1da177e4 | 2261 | skb->dev = (void *) hdev; |
0d48d939 | 2262 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
e702112f | 2263 | hci_add_acl_hdr(skb, conn->handle, flags); |
1da177e4 LT |
2264 | |
2265 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); | |
2266 | ||
73d80deb | 2267 | __skb_queue_tail(queue, skb); |
1da177e4 LT |
2268 | } while (list); |
2269 | ||
af3e6359 | 2270 | spin_unlock(&queue->lock); |
1da177e4 | 2271 | } |
73d80deb LAD |
2272 | } |
2273 | ||
2274 | void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) | |
2275 | { | |
2276 | struct hci_conn *conn = chan->conn; | |
2277 | struct hci_dev *hdev = conn->hdev; | |
2278 | ||
2279 | BT_DBG("%s chan %p flags 0x%x", hdev->name, chan, flags); | |
2280 | ||
2281 | skb->dev = (void *) hdev; | |
2282 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; | |
2283 | hci_add_acl_hdr(skb, conn->handle, flags); | |
2284 | ||
2285 | hci_queue_acl(conn, &chan->data_q, skb, flags); | |
1da177e4 | 2286 | |
3eff45ea | 2287 | queue_work(hdev->workqueue, &hdev->tx_work); |
1da177e4 LT |
2288 | } |
2289 | EXPORT_SYMBOL(hci_send_acl); | |
2290 | ||
2291 | /* Send SCO data */ | |
0d861d8b | 2292 | void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) |
1da177e4 LT |
2293 | { |
2294 | struct hci_dev *hdev = conn->hdev; | |
2295 | struct hci_sco_hdr hdr; | |
2296 | ||
2297 | BT_DBG("%s len %d", hdev->name, skb->len); | |
2298 | ||
aca3192c | 2299 | hdr.handle = cpu_to_le16(conn->handle); |
1da177e4 LT |
2300 | hdr.dlen = skb->len; |
2301 | ||
badff6d0 ACM |
2302 | skb_push(skb, HCI_SCO_HDR_SIZE); |
2303 | skb_reset_transport_header(skb); | |
9c70220b | 2304 | memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); |
1da177e4 LT |
2305 | |
2306 | skb->dev = (void *) hdev; | |
0d48d939 | 2307 | bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; |
c78ae283 | 2308 | |
1da177e4 | 2309 | skb_queue_tail(&conn->data_q, skb); |
3eff45ea | 2310 | queue_work(hdev->workqueue, &hdev->tx_work); |
1da177e4 LT |
2311 | } |
2312 | EXPORT_SYMBOL(hci_send_sco); | |
2313 | ||
2314 | /* ---- HCI TX task (outgoing data) ---- */ | |
2315 | ||
2316 | /* HCI Connection scheduler */ | |
2317 | static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote) | |
2318 | { | |
2319 | struct hci_conn_hash *h = &hdev->conn_hash; | |
8035ded4 | 2320 | struct hci_conn *conn = NULL, *c; |
abc5de8f | 2321 | unsigned int num = 0, min = ~0; |
1da177e4 | 2322 | |
8e87d142 | 2323 | /* We don't have to lock device here. Connections are always |
1da177e4 | 2324 | * added and removed with TX task disabled. */ |
bf4c6325 GP |
2325 | |
2326 | rcu_read_lock(); | |
2327 | ||
2328 | list_for_each_entry_rcu(c, &h->list, list) { | |
769be974 | 2329 | if (c->type != type || skb_queue_empty(&c->data_q)) |
1da177e4 | 2330 | continue; |
769be974 MH |
2331 | |
2332 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) | |
2333 | continue; | |
2334 | ||
1da177e4 LT |
2335 | num++; |
2336 | ||
2337 | if (c->sent < min) { | |
2338 | min = c->sent; | |
2339 | conn = c; | |
2340 | } | |
52087a79 LAD |
2341 | |
2342 | if (hci_conn_num(hdev, type) == num) | |
2343 | break; | |
1da177e4 LT |
2344 | } |
2345 | ||
bf4c6325 GP |
2346 | rcu_read_unlock(); |
2347 | ||
1da177e4 | 2348 | if (conn) { |
6ed58ec5 VT |
2349 | int cnt, q; |
2350 | ||
2351 | switch (conn->type) { | |
2352 | case ACL_LINK: | |
2353 | cnt = hdev->acl_cnt; | |
2354 | break; | |
2355 | case SCO_LINK: | |
2356 | case ESCO_LINK: | |
2357 | cnt = hdev->sco_cnt; | |
2358 | break; | |
2359 | case LE_LINK: | |
2360 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; | |
2361 | break; | |
2362 | default: | |
2363 | cnt = 0; | |
2364 | BT_ERR("Unknown link type"); | |
2365 | } | |
2366 | ||
2367 | q = cnt / num; | |
1da177e4 LT |
2368 | *quote = q ? q : 1; |
2369 | } else | |
2370 | *quote = 0; | |
2371 | ||
2372 | BT_DBG("conn %p quote %d", conn, *quote); | |
2373 | return conn; | |
2374 | } | |
2375 | ||
bae1f5d9 | 2376 | static inline void hci_link_tx_to(struct hci_dev *hdev, __u8 type) |
1da177e4 LT |
2377 | { |
2378 | struct hci_conn_hash *h = &hdev->conn_hash; | |
8035ded4 | 2379 | struct hci_conn *c; |
1da177e4 | 2380 | |
bae1f5d9 | 2381 | BT_ERR("%s link tx timeout", hdev->name); |
1da177e4 | 2382 | |
bf4c6325 GP |
2383 | rcu_read_lock(); |
2384 | ||
1da177e4 | 2385 | /* Kill stalled connections */ |
bf4c6325 | 2386 | list_for_each_entry_rcu(c, &h->list, list) { |
bae1f5d9 VT |
2387 | if (c->type == type && c->sent) { |
2388 | BT_ERR("%s killing stalled connection %s", | |
1da177e4 LT |
2389 | hdev->name, batostr(&c->dst)); |
2390 | hci_acl_disconn(c, 0x13); | |
2391 | } | |
2392 | } | |
bf4c6325 GP |
2393 | |
2394 | rcu_read_unlock(); | |
1da177e4 LT |
2395 | } |
2396 | ||
73d80deb LAD |
2397 | static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, |
2398 | int *quote) | |
1da177e4 | 2399 | { |
73d80deb LAD |
2400 | struct hci_conn_hash *h = &hdev->conn_hash; |
2401 | struct hci_chan *chan = NULL; | |
abc5de8f | 2402 | unsigned int num = 0, min = ~0, cur_prio = 0; |
1da177e4 | 2403 | struct hci_conn *conn; |
73d80deb LAD |
2404 | int cnt, q, conn_num = 0; |
2405 | ||
2406 | BT_DBG("%s", hdev->name); | |
2407 | ||
bf4c6325 GP |
2408 | rcu_read_lock(); |
2409 | ||
2410 | list_for_each_entry_rcu(conn, &h->list, list) { | |
73d80deb LAD |
2411 | struct hci_chan *tmp; |
2412 | ||
2413 | if (conn->type != type) | |
2414 | continue; | |
2415 | ||
2416 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) | |
2417 | continue; | |
2418 | ||
2419 | conn_num++; | |
2420 | ||
8192edef | 2421 | list_for_each_entry_rcu(tmp, &conn->chan_list, list) { |
73d80deb LAD |
2422 | struct sk_buff *skb; |
2423 | ||
2424 | if (skb_queue_empty(&tmp->data_q)) | |
2425 | continue; | |
2426 | ||
2427 | skb = skb_peek(&tmp->data_q); | |
2428 | if (skb->priority < cur_prio) | |
2429 | continue; | |
2430 | ||
2431 | if (skb->priority > cur_prio) { | |
2432 | num = 0; | |
2433 | min = ~0; | |
2434 | cur_prio = skb->priority; | |
2435 | } | |
2436 | ||
2437 | num++; | |
2438 | ||
2439 | if (conn->sent < min) { | |
2440 | min = conn->sent; | |
2441 | chan = tmp; | |
2442 | } | |
2443 | } | |
2444 | ||
2445 | if (hci_conn_num(hdev, type) == conn_num) | |
2446 | break; | |
2447 | } | |
2448 | ||
bf4c6325 GP |
2449 | rcu_read_unlock(); |
2450 | ||
73d80deb LAD |
2451 | if (!chan) |
2452 | return NULL; | |
2453 | ||
2454 | switch (chan->conn->type) { | |
2455 | case ACL_LINK: | |
2456 | cnt = hdev->acl_cnt; | |
2457 | break; | |
2458 | case SCO_LINK: | |
2459 | case ESCO_LINK: | |
2460 | cnt = hdev->sco_cnt; | |
2461 | break; | |
2462 | case LE_LINK: | |
2463 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; | |
2464 | break; | |
2465 | default: | |
2466 | cnt = 0; | |
2467 | BT_ERR("Unknown link type"); | |
2468 | } | |
2469 | ||
2470 | q = cnt / num; | |
2471 | *quote = q ? q : 1; | |
2472 | BT_DBG("chan %p quote %d", chan, *quote); | |
2473 | return chan; | |
2474 | } | |
2475 | ||
02b20f0b LAD |
2476 | static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) |
2477 | { | |
2478 | struct hci_conn_hash *h = &hdev->conn_hash; | |
2479 | struct hci_conn *conn; | |
2480 | int num = 0; | |
2481 | ||
2482 | BT_DBG("%s", hdev->name); | |
2483 | ||
bf4c6325 GP |
2484 | rcu_read_lock(); |
2485 | ||
2486 | list_for_each_entry_rcu(conn, &h->list, list) { | |
02b20f0b LAD |
2487 | struct hci_chan *chan; |
2488 | ||
2489 | if (conn->type != type) | |
2490 | continue; | |
2491 | ||
2492 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) | |
2493 | continue; | |
2494 | ||
2495 | num++; | |
2496 | ||
8192edef | 2497 | list_for_each_entry_rcu(chan, &conn->chan_list, list) { |
02b20f0b LAD |
2498 | struct sk_buff *skb; |
2499 | ||
2500 | if (chan->sent) { | |
2501 | chan->sent = 0; | |
2502 | continue; | |
2503 | } | |
2504 | ||
2505 | if (skb_queue_empty(&chan->data_q)) | |
2506 | continue; | |
2507 | ||
2508 | skb = skb_peek(&chan->data_q); | |
2509 | if (skb->priority >= HCI_PRIO_MAX - 1) | |
2510 | continue; | |
2511 | ||
2512 | skb->priority = HCI_PRIO_MAX - 1; | |
2513 | ||
2514 | BT_DBG("chan %p skb %p promoted to %d", chan, skb, | |
2515 | skb->priority); | |
2516 | } | |
2517 | ||
2518 | if (hci_conn_num(hdev, type) == num) | |
2519 | break; | |
2520 | } | |
bf4c6325 GP |
2521 | |
2522 | rcu_read_unlock(); | |
2523 | ||
02b20f0b LAD |
2524 | } |
2525 | ||
b71d385a AE |
2526 | static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb) |
2527 | { | |
2528 | /* Calculate count of blocks used by this packet */ | |
2529 | return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len); | |
2530 | } | |
2531 | ||
63d2bc1b | 2532 | static inline void __check_timeout(struct hci_dev *hdev, unsigned int cnt) |
73d80deb | 2533 | { |
1da177e4 LT |
2534 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
2535 | /* ACL tx timeout must be longer than maximum | |
2536 | * link supervision timeout (40.9 seconds) */ | |
63d2bc1b | 2537 | if (!cnt && time_after(jiffies, hdev->acl_last_tx + |
cc48dc0a | 2538 | msecs_to_jiffies(HCI_ACL_TX_TIMEOUT))) |
bae1f5d9 | 2539 | hci_link_tx_to(hdev, ACL_LINK); |
1da177e4 | 2540 | } |
63d2bc1b | 2541 | } |
1da177e4 | 2542 | |
63d2bc1b AE |
2543 | static inline void hci_sched_acl_pkt(struct hci_dev *hdev) |
2544 | { | |
2545 | unsigned int cnt = hdev->acl_cnt; | |
2546 | struct hci_chan *chan; | |
2547 | struct sk_buff *skb; | |
2548 | int quote; | |
2549 | ||
2550 | __check_timeout(hdev, cnt); | |
04837f64 | 2551 | |
73d80deb LAD |
2552 | while (hdev->acl_cnt && |
2553 | (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { | |
ec1cce24 LAD |
2554 | u32 priority = (skb_peek(&chan->data_q))->priority; |
2555 | while (quote-- && (skb = skb_peek(&chan->data_q))) { | |
73d80deb LAD |
2556 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
2557 | skb->len, skb->priority); | |
2558 | ||
ec1cce24 LAD |
2559 | /* Stop if priority has changed */ |
2560 | if (skb->priority < priority) | |
2561 | break; | |
2562 | ||
2563 | skb = skb_dequeue(&chan->data_q); | |
2564 | ||
73d80deb | 2565 | hci_conn_enter_active_mode(chan->conn, |
04124681 | 2566 | bt_cb(skb)->force_active); |
04837f64 | 2567 | |
1da177e4 LT |
2568 | hci_send_frame(skb); |
2569 | hdev->acl_last_tx = jiffies; | |
2570 | ||
2571 | hdev->acl_cnt--; | |
73d80deb LAD |
2572 | chan->sent++; |
2573 | chan->conn->sent++; | |
1da177e4 LT |
2574 | } |
2575 | } | |
02b20f0b LAD |
2576 | |
2577 | if (cnt != hdev->acl_cnt) | |
2578 | hci_prio_recalculate(hdev, ACL_LINK); | |
1da177e4 LT |
2579 | } |
2580 | ||
b71d385a AE |
2581 | static inline void hci_sched_acl_blk(struct hci_dev *hdev) |
2582 | { | |
63d2bc1b | 2583 | unsigned int cnt = hdev->block_cnt; |
b71d385a AE |
2584 | struct hci_chan *chan; |
2585 | struct sk_buff *skb; | |
2586 | int quote; | |
b71d385a | 2587 | |
63d2bc1b | 2588 | __check_timeout(hdev, cnt); |
b71d385a AE |
2589 | |
2590 | while (hdev->block_cnt > 0 && | |
2591 | (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { | |
2592 | u32 priority = (skb_peek(&chan->data_q))->priority; | |
2593 | while (quote > 0 && (skb = skb_peek(&chan->data_q))) { | |
2594 | int blocks; | |
2595 | ||
2596 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, | |
2597 | skb->len, skb->priority); | |
2598 | ||
2599 | /* Stop if priority has changed */ | |
2600 | if (skb->priority < priority) | |
2601 | break; | |
2602 | ||
2603 | skb = skb_dequeue(&chan->data_q); | |
2604 | ||
2605 | blocks = __get_blocks(hdev, skb); | |
2606 | if (blocks > hdev->block_cnt) | |
2607 | return; | |
2608 | ||
2609 | hci_conn_enter_active_mode(chan->conn, | |
2610 | bt_cb(skb)->force_active); | |
2611 | ||
2612 | hci_send_frame(skb); | |
2613 | hdev->acl_last_tx = jiffies; | |
2614 | ||
2615 | hdev->block_cnt -= blocks; | |
2616 | quote -= blocks; | |
2617 | ||
2618 | chan->sent += blocks; | |
2619 | chan->conn->sent += blocks; | |
2620 | } | |
2621 | } | |
2622 | ||
2623 | if (cnt != hdev->block_cnt) | |
2624 | hci_prio_recalculate(hdev, ACL_LINK); | |
2625 | } | |
2626 | ||
2627 | static inline void hci_sched_acl(struct hci_dev *hdev) | |
2628 | { | |
2629 | BT_DBG("%s", hdev->name); | |
2630 | ||
2631 | if (!hci_conn_num(hdev, ACL_LINK)) | |
2632 | return; | |
2633 | ||
2634 | switch (hdev->flow_ctl_mode) { | |
2635 | case HCI_FLOW_CTL_MODE_PACKET_BASED: | |
2636 | hci_sched_acl_pkt(hdev); | |
2637 | break; | |
2638 | ||
2639 | case HCI_FLOW_CTL_MODE_BLOCK_BASED: | |
2640 | hci_sched_acl_blk(hdev); | |
2641 | break; | |
2642 | } | |
2643 | } | |
2644 | ||
1da177e4 LT |
2645 | /* Schedule SCO */ |
2646 | static inline void hci_sched_sco(struct hci_dev *hdev) | |
2647 | { | |
2648 | struct hci_conn *conn; | |
2649 | struct sk_buff *skb; | |
2650 | int quote; | |
2651 | ||
2652 | BT_DBG("%s", hdev->name); | |
2653 | ||
52087a79 LAD |
2654 | if (!hci_conn_num(hdev, SCO_LINK)) |
2655 | return; | |
2656 | ||
1da177e4 LT |
2657 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { |
2658 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { | |
2659 | BT_DBG("skb %p len %d", skb, skb->len); | |
2660 | hci_send_frame(skb); | |
2661 | ||
2662 | conn->sent++; | |
2663 | if (conn->sent == ~0) | |
2664 | conn->sent = 0; | |
2665 | } | |
2666 | } | |
2667 | } | |
2668 | ||
b6a0dc82 MH |
2669 | static inline void hci_sched_esco(struct hci_dev *hdev) |
2670 | { | |
2671 | struct hci_conn *conn; | |
2672 | struct sk_buff *skb; | |
2673 | int quote; | |
2674 | ||
2675 | BT_DBG("%s", hdev->name); | |
2676 | ||
52087a79 LAD |
2677 | if (!hci_conn_num(hdev, ESCO_LINK)) |
2678 | return; | |
2679 | ||
b6a0dc82 MH |
2680 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, "e))) { |
2681 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { | |
2682 | BT_DBG("skb %p len %d", skb, skb->len); | |
2683 | hci_send_frame(skb); | |
2684 | ||
2685 | conn->sent++; | |
2686 | if (conn->sent == ~0) | |
2687 | conn->sent = 0; | |
2688 | } | |
2689 | } | |
2690 | } | |
2691 | ||
6ed58ec5 VT |
2692 | static inline void hci_sched_le(struct hci_dev *hdev) |
2693 | { | |
73d80deb | 2694 | struct hci_chan *chan; |
6ed58ec5 | 2695 | struct sk_buff *skb; |
02b20f0b | 2696 | int quote, cnt, tmp; |
6ed58ec5 VT |
2697 | |
2698 | BT_DBG("%s", hdev->name); | |
2699 | ||
52087a79 LAD |
2700 | if (!hci_conn_num(hdev, LE_LINK)) |
2701 | return; | |
2702 | ||
6ed58ec5 VT |
2703 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
2704 | /* LE tx timeout must be longer than maximum | |
2705 | * link supervision timeout (40.9 seconds) */ | |
bae1f5d9 | 2706 | if (!hdev->le_cnt && hdev->le_pkts && |
6ed58ec5 | 2707 | time_after(jiffies, hdev->le_last_tx + HZ * 45)) |
bae1f5d9 | 2708 | hci_link_tx_to(hdev, LE_LINK); |
6ed58ec5 VT |
2709 | } |
2710 | ||
2711 | cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; | |
02b20f0b | 2712 | tmp = cnt; |
73d80deb | 2713 | while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { |
ec1cce24 LAD |
2714 | u32 priority = (skb_peek(&chan->data_q))->priority; |
2715 | while (quote-- && (skb = skb_peek(&chan->data_q))) { | |
73d80deb LAD |
2716 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
2717 | skb->len, skb->priority); | |
6ed58ec5 | 2718 | |
ec1cce24 LAD |
2719 | /* Stop if priority has changed */ |
2720 | if (skb->priority < priority) | |
2721 | break; | |
2722 | ||
2723 | skb = skb_dequeue(&chan->data_q); | |
2724 | ||
6ed58ec5 VT |
2725 | hci_send_frame(skb); |
2726 | hdev->le_last_tx = jiffies; | |
2727 | ||
2728 | cnt--; | |
73d80deb LAD |
2729 | chan->sent++; |
2730 | chan->conn->sent++; | |
6ed58ec5 VT |
2731 | } |
2732 | } | |
73d80deb | 2733 | |
6ed58ec5 VT |
2734 | if (hdev->le_pkts) |
2735 | hdev->le_cnt = cnt; | |
2736 | else | |
2737 | hdev->acl_cnt = cnt; | |
02b20f0b LAD |
2738 | |
2739 | if (cnt != tmp) | |
2740 | hci_prio_recalculate(hdev, LE_LINK); | |
6ed58ec5 VT |
2741 | } |
2742 | ||
3eff45ea | 2743 | static void hci_tx_work(struct work_struct *work) |
1da177e4 | 2744 | { |
3eff45ea | 2745 | struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); |
1da177e4 LT |
2746 | struct sk_buff *skb; |
2747 | ||
6ed58ec5 VT |
2748 | BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, |
2749 | hdev->sco_cnt, hdev->le_cnt); | |
1da177e4 LT |
2750 | |
2751 | /* Schedule queues and send stuff to HCI driver */ | |
2752 | ||
2753 | hci_sched_acl(hdev); | |
2754 | ||
2755 | hci_sched_sco(hdev); | |
2756 | ||
b6a0dc82 MH |
2757 | hci_sched_esco(hdev); |
2758 | ||
6ed58ec5 VT |
2759 | hci_sched_le(hdev); |
2760 | ||
1da177e4 LT |
2761 | /* Send next queued raw (unknown type) packet */ |
2762 | while ((skb = skb_dequeue(&hdev->raw_q))) | |
2763 | hci_send_frame(skb); | |
1da177e4 LT |
2764 | } |
2765 | ||
25985edc | 2766 | /* ----- HCI RX task (incoming data processing) ----- */ |
1da177e4 LT |
2767 | |
2768 | /* ACL data packet */ | |
2769 | static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) | |
2770 | { | |
2771 | struct hci_acl_hdr *hdr = (void *) skb->data; | |
2772 | struct hci_conn *conn; | |
2773 | __u16 handle, flags; | |
2774 | ||
2775 | skb_pull(skb, HCI_ACL_HDR_SIZE); | |
2776 | ||
2777 | handle = __le16_to_cpu(hdr->handle); | |
2778 | flags = hci_flags(handle); | |
2779 | handle = hci_handle(handle); | |
2780 | ||
2781 | BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags); | |
2782 | ||
2783 | hdev->stat.acl_rx++; | |
2784 | ||
2785 | hci_dev_lock(hdev); | |
2786 | conn = hci_conn_hash_lookup_handle(hdev, handle); | |
2787 | hci_dev_unlock(hdev); | |
8e87d142 | 2788 | |
1da177e4 | 2789 | if (conn) { |
65983fc7 | 2790 | hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); |
04837f64 | 2791 | |
1da177e4 | 2792 | /* Send to upper protocol */ |
686ebf28 UF |
2793 | l2cap_recv_acldata(conn, skb, flags); |
2794 | return; | |
1da177e4 | 2795 | } else { |
8e87d142 | 2796 | BT_ERR("%s ACL packet for unknown connection handle %d", |
1da177e4 LT |
2797 | hdev->name, handle); |
2798 | } | |
2799 | ||
2800 | kfree_skb(skb); | |
2801 | } | |
2802 | ||
2803 | /* SCO data packet */ | |
2804 | static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) | |
2805 | { | |
2806 | struct hci_sco_hdr *hdr = (void *) skb->data; | |
2807 | struct hci_conn *conn; | |
2808 | __u16 handle; | |
2809 | ||
2810 | skb_pull(skb, HCI_SCO_HDR_SIZE); | |
2811 | ||
2812 | handle = __le16_to_cpu(hdr->handle); | |
2813 | ||
2814 | BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle); | |
2815 | ||
2816 | hdev->stat.sco_rx++; | |
2817 | ||
2818 | hci_dev_lock(hdev); | |
2819 | conn = hci_conn_hash_lookup_handle(hdev, handle); | |
2820 | hci_dev_unlock(hdev); | |
2821 | ||
2822 | if (conn) { | |
1da177e4 | 2823 | /* Send to upper protocol */ |
686ebf28 UF |
2824 | sco_recv_scodata(conn, skb); |
2825 | return; | |
1da177e4 | 2826 | } else { |
8e87d142 | 2827 | BT_ERR("%s SCO packet for unknown connection handle %d", |
1da177e4 LT |
2828 | hdev->name, handle); |
2829 | } | |
2830 | ||
2831 | kfree_skb(skb); | |
2832 | } | |
2833 | ||
b78752cc | 2834 | static void hci_rx_work(struct work_struct *work) |
1da177e4 | 2835 | { |
b78752cc | 2836 | struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); |
1da177e4 LT |
2837 | struct sk_buff *skb; |
2838 | ||
2839 | BT_DBG("%s", hdev->name); | |
2840 | ||
1da177e4 | 2841 | while ((skb = skb_dequeue(&hdev->rx_q))) { |
cd82e61c MH |
2842 | /* Send copy to monitor */ |
2843 | hci_send_to_monitor(hdev, skb); | |
2844 | ||
1da177e4 LT |
2845 | if (atomic_read(&hdev->promisc)) { |
2846 | /* Send copy to the sockets */ | |
470fe1b5 | 2847 | hci_send_to_sock(hdev, skb); |
1da177e4 LT |
2848 | } |
2849 | ||
2850 | if (test_bit(HCI_RAW, &hdev->flags)) { | |
2851 | kfree_skb(skb); | |
2852 | continue; | |
2853 | } | |
2854 | ||
2855 | if (test_bit(HCI_INIT, &hdev->flags)) { | |
2856 | /* Don't process data packets in this states. */ | |
0d48d939 | 2857 | switch (bt_cb(skb)->pkt_type) { |
1da177e4 LT |
2858 | case HCI_ACLDATA_PKT: |
2859 | case HCI_SCODATA_PKT: | |
2860 | kfree_skb(skb); | |
2861 | continue; | |
3ff50b79 | 2862 | } |
1da177e4 LT |
2863 | } |
2864 | ||
2865 | /* Process frame */ | |
0d48d939 | 2866 | switch (bt_cb(skb)->pkt_type) { |
1da177e4 | 2867 | case HCI_EVENT_PKT: |
b78752cc | 2868 | BT_DBG("%s Event packet", hdev->name); |
1da177e4 LT |
2869 | hci_event_packet(hdev, skb); |
2870 | break; | |
2871 | ||
2872 | case HCI_ACLDATA_PKT: | |
2873 | BT_DBG("%s ACL data packet", hdev->name); | |
2874 | hci_acldata_packet(hdev, skb); | |
2875 | break; | |
2876 | ||
2877 | case HCI_SCODATA_PKT: | |
2878 | BT_DBG("%s SCO data packet", hdev->name); | |
2879 | hci_scodata_packet(hdev, skb); | |
2880 | break; | |
2881 | ||
2882 | default: | |
2883 | kfree_skb(skb); | |
2884 | break; | |
2885 | } | |
2886 | } | |
1da177e4 LT |
2887 | } |
2888 | ||
c347b765 | 2889 | static void hci_cmd_work(struct work_struct *work) |
1da177e4 | 2890 | { |
c347b765 | 2891 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); |
1da177e4 LT |
2892 | struct sk_buff *skb; |
2893 | ||
2894 | BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt)); | |
2895 | ||
1da177e4 | 2896 | /* Send queued commands */ |
5a08ecce AE |
2897 | if (atomic_read(&hdev->cmd_cnt)) { |
2898 | skb = skb_dequeue(&hdev->cmd_q); | |
2899 | if (!skb) | |
2900 | return; | |
2901 | ||
7585b97a | 2902 | kfree_skb(hdev->sent_cmd); |
1da177e4 | 2903 | |
70f23020 AE |
2904 | hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC); |
2905 | if (hdev->sent_cmd) { | |
1da177e4 LT |
2906 | atomic_dec(&hdev->cmd_cnt); |
2907 | hci_send_frame(skb); | |
7bdb8a5c SJ |
2908 | if (test_bit(HCI_RESET, &hdev->flags)) |
2909 | del_timer(&hdev->cmd_timer); | |
2910 | else | |
2911 | mod_timer(&hdev->cmd_timer, | |
6bd32326 | 2912 | jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT)); |
1da177e4 LT |
2913 | } else { |
2914 | skb_queue_head(&hdev->cmd_q, skb); | |
c347b765 | 2915 | queue_work(hdev->workqueue, &hdev->cmd_work); |
1da177e4 LT |
2916 | } |
2917 | } | |
2918 | } | |
2519a1fc AG |
2919 | |
2920 | int hci_do_inquiry(struct hci_dev *hdev, u8 length) | |
2921 | { | |
2922 | /* General inquiry access code (GIAC) */ | |
2923 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; | |
2924 | struct hci_cp_inquiry cp; | |
2925 | ||
2926 | BT_DBG("%s", hdev->name); | |
2927 | ||
2928 | if (test_bit(HCI_INQUIRY, &hdev->flags)) | |
2929 | return -EINPROGRESS; | |
2930 | ||
4663262c JH |
2931 | inquiry_cache_flush(hdev); |
2932 | ||
2519a1fc AG |
2933 | memset(&cp, 0, sizeof(cp)); |
2934 | memcpy(&cp.lap, lap, sizeof(cp.lap)); | |
2935 | cp.length = length; | |
2936 | ||
2937 | return hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); | |
2938 | } | |
023d5049 AG |
2939 | |
2940 | int hci_cancel_inquiry(struct hci_dev *hdev) | |
2941 | { | |
2942 | BT_DBG("%s", hdev->name); | |
2943 | ||
2944 | if (!test_bit(HCI_INQUIRY, &hdev->flags)) | |
7537e5c3 | 2945 | return -EALREADY; |
023d5049 AG |
2946 | |
2947 | return hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL); | |
2948 | } |