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