2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Copyright (C) 2011 ProFUSION Embedded Systems
6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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
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
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
23 SOFTWARE IS DISCLAIMED.
26 /* Bluetooth HCI core. */
28 #include <linux/export.h>
29 #include <linux/idr.h>
31 #include <linux/rfkill.h>
33 #include <net/bluetooth/bluetooth.h>
34 #include <net/bluetooth/hci_core.h>
36 #define AUTO_OFF_TIMEOUT 2000
38 static void hci_rx_work(struct work_struct *work);
39 static void hci_cmd_work(struct work_struct *work);
40 static void hci_tx_work(struct work_struct *work);
43 LIST_HEAD(hci_dev_list);
44 DEFINE_RWLOCK(hci_dev_list_lock);
46 /* HCI callback list */
47 LIST_HEAD(hci_cb_list);
48 DEFINE_RWLOCK(hci_cb_list_lock);
50 /* HCI ID Numbering */
51 static DEFINE_IDA(hci_index_ida);
53 /* ---- HCI notifications ---- */
55 static void hci_notify(struct hci_dev *hdev, int event)
57 hci_sock_dev_event(hdev, event);
60 /* ---- HCI requests ---- */
62 void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result)
64 BT_DBG("%s command 0x%04x result 0x%2.2x", hdev->name, cmd, result);
66 /* If this is the init phase check if the completed command matches
67 * the last init command, and if not just return.
69 if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd) {
70 struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data;
71 u16 opcode = __le16_to_cpu(sent->opcode);
74 /* Some CSR based controllers generate a spontaneous
75 * reset complete event during init and any pending
76 * command will never be completed. In such a case we
77 * need to resend whatever was the last sent
81 if (cmd != HCI_OP_RESET || opcode == HCI_OP_RESET)
84 skb = skb_clone(hdev->sent_cmd, GFP_ATOMIC);
86 skb_queue_head(&hdev->cmd_q, skb);
87 queue_work(hdev->workqueue, &hdev->cmd_work);
93 if (hdev->req_status == HCI_REQ_PEND) {
94 hdev->req_result = result;
95 hdev->req_status = HCI_REQ_DONE;
96 wake_up_interruptible(&hdev->req_wait_q);
100 static void hci_req_cancel(struct hci_dev *hdev, int err)
102 BT_DBG("%s err 0x%2.2x", hdev->name, err);
104 if (hdev->req_status == HCI_REQ_PEND) {
105 hdev->req_result = err;
106 hdev->req_status = HCI_REQ_CANCELED;
107 wake_up_interruptible(&hdev->req_wait_q);
111 /* Execute request and wait for completion. */
112 static int __hci_request(struct hci_dev *hdev,
113 void (*req)(struct hci_dev *hdev, unsigned long opt),
114 unsigned long opt, __u32 timeout)
116 DECLARE_WAITQUEUE(wait, current);
119 BT_DBG("%s start", hdev->name);
121 hdev->req_status = HCI_REQ_PEND;
123 add_wait_queue(&hdev->req_wait_q, &wait);
124 set_current_state(TASK_INTERRUPTIBLE);
127 schedule_timeout(timeout);
129 remove_wait_queue(&hdev->req_wait_q, &wait);
131 if (signal_pending(current))
134 switch (hdev->req_status) {
136 err = -bt_to_errno(hdev->req_result);
139 case HCI_REQ_CANCELED:
140 err = -hdev->req_result;
148 hdev->req_status = hdev->req_result = 0;
150 BT_DBG("%s end: err %d", hdev->name, err);
155 static int hci_request(struct hci_dev *hdev,
156 void (*req)(struct hci_dev *hdev, unsigned long opt),
157 unsigned long opt, __u32 timeout)
161 if (!test_bit(HCI_UP, &hdev->flags))
164 /* Serialize all requests */
166 ret = __hci_request(hdev, req, opt, timeout);
167 hci_req_unlock(hdev);
172 static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
174 BT_DBG("%s %ld", hdev->name, opt);
177 set_bit(HCI_RESET, &hdev->flags);
178 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
181 static void bredr_init(struct hci_dev *hdev)
183 struct hci_cp_delete_stored_link_key cp;
187 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
189 /* Mandatory initialization */
192 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
193 set_bit(HCI_RESET, &hdev->flags);
194 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
197 /* Read Local Supported Features */
198 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
200 /* Read Local Version */
201 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
203 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
204 hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
206 /* Read BD Address */
207 hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL);
209 /* Read Class of Device */
210 hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
212 /* Read Local Name */
213 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL);
215 /* Read Voice Setting */
216 hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL);
218 /* Optional initialization */
220 /* Clear Event Filters */
221 flt_type = HCI_FLT_CLEAR_ALL;
222 hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
224 /* Connection accept timeout ~20 secs */
225 param = __constant_cpu_to_le16(0x7d00);
226 hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m);
228 bacpy(&cp.bdaddr, BDADDR_ANY);
230 hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp);
233 static void amp_init(struct hci_dev *hdev)
235 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
238 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
240 /* Read Local Version */
241 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
243 /* Read Local AMP Info */
244 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
247 static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
251 BT_DBG("%s %ld", hdev->name, opt);
253 /* Driver initialization */
255 /* Special commands */
256 while ((skb = skb_dequeue(&hdev->driver_init))) {
257 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
258 skb->dev = (void *) hdev;
260 skb_queue_tail(&hdev->cmd_q, skb);
261 queue_work(hdev->workqueue, &hdev->cmd_work);
263 skb_queue_purge(&hdev->driver_init);
265 switch (hdev->dev_type) {
275 BT_ERR("Unknown device type %d", hdev->dev_type);
281 static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt)
283 BT_DBG("%s", hdev->name);
285 /* Read LE buffer size */
286 hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
289 static void hci_scan_req(struct hci_dev *hdev, unsigned long opt)
293 BT_DBG("%s %x", hdev->name, scan);
295 /* Inquiry and Page scans */
296 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
299 static void hci_auth_req(struct hci_dev *hdev, unsigned long opt)
303 BT_DBG("%s %x", hdev->name, auth);
306 hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
309 static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
313 BT_DBG("%s %x", hdev->name, encrypt);
316 hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
319 static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt)
321 __le16 policy = cpu_to_le16(opt);
323 BT_DBG("%s %x", hdev->name, policy);
325 /* Default link policy */
326 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
329 /* Get HCI device by index.
330 * Device is held on return. */
331 struct hci_dev *hci_dev_get(int index)
333 struct hci_dev *hdev = NULL, *d;
340 read_lock(&hci_dev_list_lock);
341 list_for_each_entry(d, &hci_dev_list, list) {
342 if (d->id == index) {
343 hdev = hci_dev_hold(d);
347 read_unlock(&hci_dev_list_lock);
351 /* ---- Inquiry support ---- */
353 bool hci_discovery_active(struct hci_dev *hdev)
355 struct discovery_state *discov = &hdev->discovery;
357 switch (discov->state) {
358 case DISCOVERY_FINDING:
359 case DISCOVERY_RESOLVING:
367 void hci_discovery_set_state(struct hci_dev *hdev, int state)
369 BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
371 if (hdev->discovery.state == state)
375 case DISCOVERY_STOPPED:
376 if (hdev->discovery.state != DISCOVERY_STARTING)
377 mgmt_discovering(hdev, 0);
379 case DISCOVERY_STARTING:
381 case DISCOVERY_FINDING:
382 mgmt_discovering(hdev, 1);
384 case DISCOVERY_RESOLVING:
386 case DISCOVERY_STOPPING:
390 hdev->discovery.state = state;
393 static void inquiry_cache_flush(struct hci_dev *hdev)
395 struct discovery_state *cache = &hdev->discovery;
396 struct inquiry_entry *p, *n;
398 list_for_each_entry_safe(p, n, &cache->all, all) {
403 INIT_LIST_HEAD(&cache->unknown);
404 INIT_LIST_HEAD(&cache->resolve);
407 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
410 struct discovery_state *cache = &hdev->discovery;
411 struct inquiry_entry *e;
413 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
415 list_for_each_entry(e, &cache->all, all) {
416 if (!bacmp(&e->data.bdaddr, bdaddr))
423 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
426 struct discovery_state *cache = &hdev->discovery;
427 struct inquiry_entry *e;
429 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
431 list_for_each_entry(e, &cache->unknown, list) {
432 if (!bacmp(&e->data.bdaddr, bdaddr))
439 struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
443 struct discovery_state *cache = &hdev->discovery;
444 struct inquiry_entry *e;
446 BT_DBG("cache %p bdaddr %s state %d", cache, batostr(bdaddr), state);
448 list_for_each_entry(e, &cache->resolve, list) {
449 if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state)
451 if (!bacmp(&e->data.bdaddr, bdaddr))
458 void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
459 struct inquiry_entry *ie)
461 struct discovery_state *cache = &hdev->discovery;
462 struct list_head *pos = &cache->resolve;
463 struct inquiry_entry *p;
467 list_for_each_entry(p, &cache->resolve, list) {
468 if (p->name_state != NAME_PENDING &&
469 abs(p->data.rssi) >= abs(ie->data.rssi))
474 list_add(&ie->list, pos);
477 bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
478 bool name_known, bool *ssp)
480 struct discovery_state *cache = &hdev->discovery;
481 struct inquiry_entry *ie;
483 BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr));
486 *ssp = data->ssp_mode;
488 ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
490 if (ie->data.ssp_mode && ssp)
493 if (ie->name_state == NAME_NEEDED &&
494 data->rssi != ie->data.rssi) {
495 ie->data.rssi = data->rssi;
496 hci_inquiry_cache_update_resolve(hdev, ie);
502 /* Entry not in the cache. Add new one. */
503 ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
507 list_add(&ie->all, &cache->all);
510 ie->name_state = NAME_KNOWN;
512 ie->name_state = NAME_NOT_KNOWN;
513 list_add(&ie->list, &cache->unknown);
517 if (name_known && ie->name_state != NAME_KNOWN &&
518 ie->name_state != NAME_PENDING) {
519 ie->name_state = NAME_KNOWN;
523 memcpy(&ie->data, data, sizeof(*data));
524 ie->timestamp = jiffies;
525 cache->timestamp = jiffies;
527 if (ie->name_state == NAME_NOT_KNOWN)
533 static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
535 struct discovery_state *cache = &hdev->discovery;
536 struct inquiry_info *info = (struct inquiry_info *) buf;
537 struct inquiry_entry *e;
540 list_for_each_entry(e, &cache->all, all) {
541 struct inquiry_data *data = &e->data;
546 bacpy(&info->bdaddr, &data->bdaddr);
547 info->pscan_rep_mode = data->pscan_rep_mode;
548 info->pscan_period_mode = data->pscan_period_mode;
549 info->pscan_mode = data->pscan_mode;
550 memcpy(info->dev_class, data->dev_class, 3);
551 info->clock_offset = data->clock_offset;
557 BT_DBG("cache %p, copied %d", cache, copied);
561 static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
563 struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
564 struct hci_cp_inquiry cp;
566 BT_DBG("%s", hdev->name);
568 if (test_bit(HCI_INQUIRY, &hdev->flags))
572 memcpy(&cp.lap, &ir->lap, 3);
573 cp.length = ir->length;
574 cp.num_rsp = ir->num_rsp;
575 hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
578 int hci_inquiry(void __user *arg)
580 __u8 __user *ptr = arg;
581 struct hci_inquiry_req ir;
582 struct hci_dev *hdev;
583 int err = 0, do_inquiry = 0, max_rsp;
587 if (copy_from_user(&ir, ptr, sizeof(ir)))
590 hdev = hci_dev_get(ir.dev_id);
595 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
596 inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) {
597 inquiry_cache_flush(hdev);
600 hci_dev_unlock(hdev);
602 timeo = ir.length * msecs_to_jiffies(2000);
605 err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo);
610 /* for unlimited number of responses we will use buffer with
613 max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
615 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
616 * copy it to the user space.
618 buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL);
625 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
626 hci_dev_unlock(hdev);
628 BT_DBG("num_rsp %d", ir.num_rsp);
630 if (!copy_to_user(ptr, &ir, sizeof(ir))) {
632 if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
645 /* ---- HCI ioctl helpers ---- */
647 int hci_dev_open(__u16 dev)
649 struct hci_dev *hdev;
652 hdev = hci_dev_get(dev);
656 BT_DBG("%s %p", hdev->name, hdev);
660 if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) {
665 if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) {
670 if (test_bit(HCI_UP, &hdev->flags)) {
675 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
676 set_bit(HCI_RAW, &hdev->flags);
678 /* Treat all non BR/EDR controllers as raw devices if
679 enable_hs is not set */
680 if (hdev->dev_type != HCI_BREDR && !enable_hs)
681 set_bit(HCI_RAW, &hdev->flags);
683 if (hdev->open(hdev)) {
688 if (!test_bit(HCI_RAW, &hdev->flags)) {
689 atomic_set(&hdev->cmd_cnt, 1);
690 set_bit(HCI_INIT, &hdev->flags);
691 hdev->init_last_cmd = 0;
693 ret = __hci_request(hdev, hci_init_req, 0,
694 msecs_to_jiffies(HCI_INIT_TIMEOUT));
696 if (lmp_host_le_capable(hdev))
697 ret = __hci_request(hdev, hci_le_init_req, 0,
698 msecs_to_jiffies(HCI_INIT_TIMEOUT));
700 clear_bit(HCI_INIT, &hdev->flags);
705 set_bit(HCI_UP, &hdev->flags);
706 hci_notify(hdev, HCI_DEV_UP);
707 if (!test_bit(HCI_SETUP, &hdev->dev_flags)) {
709 mgmt_powered(hdev, 1);
710 hci_dev_unlock(hdev);
713 /* Init failed, cleanup */
714 flush_work(&hdev->tx_work);
715 flush_work(&hdev->cmd_work);
716 flush_work(&hdev->rx_work);
718 skb_queue_purge(&hdev->cmd_q);
719 skb_queue_purge(&hdev->rx_q);
724 if (hdev->sent_cmd) {
725 kfree_skb(hdev->sent_cmd);
726 hdev->sent_cmd = NULL;
734 hci_req_unlock(hdev);
739 static int hci_dev_do_close(struct hci_dev *hdev)
741 BT_DBG("%s %p", hdev->name, hdev);
743 cancel_work_sync(&hdev->le_scan);
745 hci_req_cancel(hdev, ENODEV);
748 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
749 del_timer_sync(&hdev->cmd_timer);
750 hci_req_unlock(hdev);
754 /* Flush RX and TX works */
755 flush_work(&hdev->tx_work);
756 flush_work(&hdev->rx_work);
758 if (hdev->discov_timeout > 0) {
759 cancel_delayed_work(&hdev->discov_off);
760 hdev->discov_timeout = 0;
761 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
764 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
765 cancel_delayed_work(&hdev->service_cache);
767 cancel_delayed_work_sync(&hdev->le_scan_disable);
770 inquiry_cache_flush(hdev);
771 hci_conn_hash_flush(hdev);
772 hci_dev_unlock(hdev);
774 hci_notify(hdev, HCI_DEV_DOWN);
780 skb_queue_purge(&hdev->cmd_q);
781 atomic_set(&hdev->cmd_cnt, 1);
782 if (!test_bit(HCI_RAW, &hdev->flags) &&
783 test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
784 set_bit(HCI_INIT, &hdev->flags);
785 __hci_request(hdev, hci_reset_req, 0,
786 msecs_to_jiffies(250));
787 clear_bit(HCI_INIT, &hdev->flags);
791 flush_work(&hdev->cmd_work);
794 skb_queue_purge(&hdev->rx_q);
795 skb_queue_purge(&hdev->cmd_q);
796 skb_queue_purge(&hdev->raw_q);
798 /* Drop last sent command */
799 if (hdev->sent_cmd) {
800 del_timer_sync(&hdev->cmd_timer);
801 kfree_skb(hdev->sent_cmd);
802 hdev->sent_cmd = NULL;
805 /* After this point our queues are empty
806 * and no tasks are scheduled. */
809 if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
811 mgmt_powered(hdev, 0);
812 hci_dev_unlock(hdev);
818 memset(hdev->eir, 0, sizeof(hdev->eir));
819 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
821 hci_req_unlock(hdev);
827 int hci_dev_close(__u16 dev)
829 struct hci_dev *hdev;
832 hdev = hci_dev_get(dev);
836 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
837 cancel_delayed_work(&hdev->power_off);
839 err = hci_dev_do_close(hdev);
845 int hci_dev_reset(__u16 dev)
847 struct hci_dev *hdev;
850 hdev = hci_dev_get(dev);
856 if (!test_bit(HCI_UP, &hdev->flags))
860 skb_queue_purge(&hdev->rx_q);
861 skb_queue_purge(&hdev->cmd_q);
864 inquiry_cache_flush(hdev);
865 hci_conn_hash_flush(hdev);
866 hci_dev_unlock(hdev);
871 atomic_set(&hdev->cmd_cnt, 1);
872 hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
874 if (!test_bit(HCI_RAW, &hdev->flags))
875 ret = __hci_request(hdev, hci_reset_req, 0,
876 msecs_to_jiffies(HCI_INIT_TIMEOUT));
879 hci_req_unlock(hdev);
884 int hci_dev_reset_stat(__u16 dev)
886 struct hci_dev *hdev;
889 hdev = hci_dev_get(dev);
893 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
900 int hci_dev_cmd(unsigned int cmd, void __user *arg)
902 struct hci_dev *hdev;
903 struct hci_dev_req dr;
906 if (copy_from_user(&dr, arg, sizeof(dr)))
909 hdev = hci_dev_get(dr.dev_id);
915 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
916 msecs_to_jiffies(HCI_INIT_TIMEOUT));
920 if (!lmp_encrypt_capable(hdev)) {
925 if (!test_bit(HCI_AUTH, &hdev->flags)) {
926 /* Auth must be enabled first */
927 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
928 msecs_to_jiffies(HCI_INIT_TIMEOUT));
933 err = hci_request(hdev, hci_encrypt_req, dr.dev_opt,
934 msecs_to_jiffies(HCI_INIT_TIMEOUT));
938 err = hci_request(hdev, hci_scan_req, dr.dev_opt,
939 msecs_to_jiffies(HCI_INIT_TIMEOUT));
943 err = hci_request(hdev, hci_linkpol_req, dr.dev_opt,
944 msecs_to_jiffies(HCI_INIT_TIMEOUT));
948 hdev->link_mode = ((__u16) dr.dev_opt) &
949 (HCI_LM_MASTER | HCI_LM_ACCEPT);
953 hdev->pkt_type = (__u16) dr.dev_opt;
957 hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1);
958 hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
962 hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1);
963 hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
975 int hci_get_dev_list(void __user *arg)
977 struct hci_dev *hdev;
978 struct hci_dev_list_req *dl;
979 struct hci_dev_req *dr;
980 int n = 0, size, err;
983 if (get_user(dev_num, (__u16 __user *) arg))
986 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
989 size = sizeof(*dl) + dev_num * sizeof(*dr);
991 dl = kzalloc(size, GFP_KERNEL);
997 read_lock(&hci_dev_list_lock);
998 list_for_each_entry(hdev, &hci_dev_list, list) {
999 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
1000 cancel_delayed_work(&hdev->power_off);
1002 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
1003 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
1005 (dr + n)->dev_id = hdev->id;
1006 (dr + n)->dev_opt = hdev->flags;
1011 read_unlock(&hci_dev_list_lock);
1014 size = sizeof(*dl) + n * sizeof(*dr);
1016 err = copy_to_user(arg, dl, size);
1019 return err ? -EFAULT : 0;
1022 int hci_get_dev_info(void __user *arg)
1024 struct hci_dev *hdev;
1025 struct hci_dev_info di;
1028 if (copy_from_user(&di, arg, sizeof(di)))
1031 hdev = hci_dev_get(di.dev_id);
1035 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
1036 cancel_delayed_work_sync(&hdev->power_off);
1038 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
1039 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
1041 strcpy(di.name, hdev->name);
1042 di.bdaddr = hdev->bdaddr;
1043 di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4);
1044 di.flags = hdev->flags;
1045 di.pkt_type = hdev->pkt_type;
1046 di.acl_mtu = hdev->acl_mtu;
1047 di.acl_pkts = hdev->acl_pkts;
1048 di.sco_mtu = hdev->sco_mtu;
1049 di.sco_pkts = hdev->sco_pkts;
1050 di.link_policy = hdev->link_policy;
1051 di.link_mode = hdev->link_mode;
1053 memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
1054 memcpy(&di.features, &hdev->features, sizeof(di.features));
1056 if (copy_to_user(arg, &di, sizeof(di)))
1064 /* ---- Interface to HCI drivers ---- */
1066 static int hci_rfkill_set_block(void *data, bool blocked)
1068 struct hci_dev *hdev = data;
1070 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
1075 hci_dev_do_close(hdev);
1080 static const struct rfkill_ops hci_rfkill_ops = {
1081 .set_block = hci_rfkill_set_block,
1084 static void hci_power_on(struct work_struct *work)
1086 struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
1088 BT_DBG("%s", hdev->name);
1090 if (hci_dev_open(hdev->id) < 0)
1093 if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
1094 schedule_delayed_work(&hdev->power_off,
1095 msecs_to_jiffies(AUTO_OFF_TIMEOUT));
1097 if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags))
1098 mgmt_index_added(hdev);
1101 static void hci_power_off(struct work_struct *work)
1103 struct hci_dev *hdev = container_of(work, struct hci_dev,
1106 BT_DBG("%s", hdev->name);
1108 hci_dev_do_close(hdev);
1111 static void hci_discov_off(struct work_struct *work)
1113 struct hci_dev *hdev;
1114 u8 scan = SCAN_PAGE;
1116 hdev = container_of(work, struct hci_dev, discov_off.work);
1118 BT_DBG("%s", hdev->name);
1122 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
1124 hdev->discov_timeout = 0;
1126 hci_dev_unlock(hdev);
1129 int hci_uuids_clear(struct hci_dev *hdev)
1131 struct list_head *p, *n;
1133 list_for_each_safe(p, n, &hdev->uuids) {
1134 struct bt_uuid *uuid;
1136 uuid = list_entry(p, struct bt_uuid, list);
1145 int hci_link_keys_clear(struct hci_dev *hdev)
1147 struct list_head *p, *n;
1149 list_for_each_safe(p, n, &hdev->link_keys) {
1150 struct link_key *key;
1152 key = list_entry(p, struct link_key, list);
1161 int hci_smp_ltks_clear(struct hci_dev *hdev)
1163 struct smp_ltk *k, *tmp;
1165 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1173 struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1177 list_for_each_entry(k, &hdev->link_keys, list)
1178 if (bacmp(bdaddr, &k->bdaddr) == 0)
1184 static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,
1185 u8 key_type, u8 old_key_type)
1188 if (key_type < 0x03)
1191 /* Debug keys are insecure so don't store them persistently */
1192 if (key_type == HCI_LK_DEBUG_COMBINATION)
1195 /* Changed combination key and there's no previous one */
1196 if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff)
1199 /* Security mode 3 case */
1203 /* Neither local nor remote side had no-bonding as requirement */
1204 if (conn->auth_type > 0x01 && conn->remote_auth > 0x01)
1207 /* Local side had dedicated bonding as requirement */
1208 if (conn->auth_type == 0x02 || conn->auth_type == 0x03)
1211 /* Remote side had dedicated bonding as requirement */
1212 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03)
1215 /* If none of the above criteria match, then don't store the key
1220 struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8])
1224 list_for_each_entry(k, &hdev->long_term_keys, list) {
1225 if (k->ediv != ediv ||
1226 memcmp(rand, k->rand, sizeof(k->rand)))
1235 struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
1240 list_for_each_entry(k, &hdev->long_term_keys, list)
1241 if (addr_type == k->bdaddr_type &&
1242 bacmp(bdaddr, &k->bdaddr) == 0)
1248 int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
1249 bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len)
1251 struct link_key *key, *old_key;
1255 old_key = hci_find_link_key(hdev, bdaddr);
1257 old_key_type = old_key->type;
1260 old_key_type = conn ? conn->key_type : 0xff;
1261 key = kzalloc(sizeof(*key), GFP_ATOMIC);
1264 list_add(&key->list, &hdev->link_keys);
1267 BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type);
1269 /* Some buggy controller combinations generate a changed
1270 * combination key for legacy pairing even when there's no
1272 if (type == HCI_LK_CHANGED_COMBINATION &&
1273 (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) {
1274 type = HCI_LK_COMBINATION;
1276 conn->key_type = type;
1279 bacpy(&key->bdaddr, bdaddr);
1280 memcpy(key->val, val, HCI_LINK_KEY_SIZE);
1281 key->pin_len = pin_len;
1283 if (type == HCI_LK_CHANGED_COMBINATION)
1284 key->type = old_key_type;
1291 persistent = hci_persistent_key(hdev, conn, type, old_key_type);
1293 mgmt_new_link_key(hdev, key, persistent);
1296 conn->flush_key = !persistent;
1301 int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type,
1302 int new_key, u8 authenticated, u8 tk[16], u8 enc_size, __le16
1305 struct smp_ltk *key, *old_key;
1307 if (!(type & HCI_SMP_STK) && !(type & HCI_SMP_LTK))
1310 old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type);
1314 key = kzalloc(sizeof(*key), GFP_ATOMIC);
1317 list_add(&key->list, &hdev->long_term_keys);
1320 bacpy(&key->bdaddr, bdaddr);
1321 key->bdaddr_type = addr_type;
1322 memcpy(key->val, tk, sizeof(key->val));
1323 key->authenticated = authenticated;
1325 key->enc_size = enc_size;
1327 memcpy(key->rand, rand, sizeof(key->rand));
1332 if (type & HCI_SMP_LTK)
1333 mgmt_new_ltk(hdev, key, 1);
1338 int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1340 struct link_key *key;
1342 key = hci_find_link_key(hdev, bdaddr);
1346 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1348 list_del(&key->list);
1354 int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr)
1356 struct smp_ltk *k, *tmp;
1358 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1359 if (bacmp(bdaddr, &k->bdaddr))
1362 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1371 /* HCI command timer function */
1372 static void hci_cmd_timer(unsigned long arg)
1374 struct hci_dev *hdev = (void *) arg;
1376 BT_ERR("%s command tx timeout", hdev->name);
1377 atomic_set(&hdev->cmd_cnt, 1);
1378 queue_work(hdev->workqueue, &hdev->cmd_work);
1381 struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
1384 struct oob_data *data;
1386 list_for_each_entry(data, &hdev->remote_oob_data, list)
1387 if (bacmp(bdaddr, &data->bdaddr) == 0)
1393 int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr)
1395 struct oob_data *data;
1397 data = hci_find_remote_oob_data(hdev, bdaddr);
1401 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1403 list_del(&data->list);
1409 int hci_remote_oob_data_clear(struct hci_dev *hdev)
1411 struct oob_data *data, *n;
1413 list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) {
1414 list_del(&data->list);
1421 int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
1424 struct oob_data *data;
1426 data = hci_find_remote_oob_data(hdev, bdaddr);
1429 data = kmalloc(sizeof(*data), GFP_ATOMIC);
1433 bacpy(&data->bdaddr, bdaddr);
1434 list_add(&data->list, &hdev->remote_oob_data);
1437 memcpy(data->hash, hash, sizeof(data->hash));
1438 memcpy(data->randomizer, randomizer, sizeof(data->randomizer));
1440 BT_DBG("%s for %s", hdev->name, batostr(bdaddr));
1445 struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
1447 struct bdaddr_list *b;
1449 list_for_each_entry(b, &hdev->blacklist, list)
1450 if (bacmp(bdaddr, &b->bdaddr) == 0)
1456 int hci_blacklist_clear(struct hci_dev *hdev)
1458 struct list_head *p, *n;
1460 list_for_each_safe(p, n, &hdev->blacklist) {
1461 struct bdaddr_list *b;
1463 b = list_entry(p, struct bdaddr_list, list);
1472 int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
1474 struct bdaddr_list *entry;
1476 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1479 if (hci_blacklist_lookup(hdev, bdaddr))
1482 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
1486 bacpy(&entry->bdaddr, bdaddr);
1488 list_add(&entry->list, &hdev->blacklist);
1490 return mgmt_device_blocked(hdev, bdaddr, type);
1493 int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
1495 struct bdaddr_list *entry;
1497 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1498 return hci_blacklist_clear(hdev);
1500 entry = hci_blacklist_lookup(hdev, bdaddr);
1504 list_del(&entry->list);
1507 return mgmt_device_unblocked(hdev, bdaddr, type);
1510 static void le_scan_param_req(struct hci_dev *hdev, unsigned long opt)
1512 struct le_scan_params *param = (struct le_scan_params *) opt;
1513 struct hci_cp_le_set_scan_param cp;
1515 memset(&cp, 0, sizeof(cp));
1516 cp.type = param->type;
1517 cp.interval = cpu_to_le16(param->interval);
1518 cp.window = cpu_to_le16(param->window);
1520 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_PARAM, sizeof(cp), &cp);
1523 static void le_scan_enable_req(struct hci_dev *hdev, unsigned long opt)
1525 struct hci_cp_le_set_scan_enable cp;
1527 memset(&cp, 0, sizeof(cp));
1530 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
1533 static int hci_do_le_scan(struct hci_dev *hdev, u8 type, u16 interval,
1534 u16 window, int timeout)
1536 long timeo = msecs_to_jiffies(3000);
1537 struct le_scan_params param;
1540 BT_DBG("%s", hdev->name);
1542 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))
1543 return -EINPROGRESS;
1546 param.interval = interval;
1547 param.window = window;
1551 err = __hci_request(hdev, le_scan_param_req, (unsigned long) ¶m,
1554 err = __hci_request(hdev, le_scan_enable_req, 0, timeo);
1556 hci_req_unlock(hdev);
1561 schedule_delayed_work(&hdev->le_scan_disable,
1562 msecs_to_jiffies(timeout));
1567 int hci_cancel_le_scan(struct hci_dev *hdev)
1569 BT_DBG("%s", hdev->name);
1571 if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
1574 if (cancel_delayed_work(&hdev->le_scan_disable)) {
1575 struct hci_cp_le_set_scan_enable cp;
1577 /* Send HCI command to disable LE Scan */
1578 memset(&cp, 0, sizeof(cp));
1579 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
1585 static void le_scan_disable_work(struct work_struct *work)
1587 struct hci_dev *hdev = container_of(work, struct hci_dev,
1588 le_scan_disable.work);
1589 struct hci_cp_le_set_scan_enable cp;
1591 BT_DBG("%s", hdev->name);
1593 memset(&cp, 0, sizeof(cp));
1595 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
1598 static void le_scan_work(struct work_struct *work)
1600 struct hci_dev *hdev = container_of(work, struct hci_dev, le_scan);
1601 struct le_scan_params *param = &hdev->le_scan_params;
1603 BT_DBG("%s", hdev->name);
1605 hci_do_le_scan(hdev, param->type, param->interval, param->window,
1609 int hci_le_scan(struct hci_dev *hdev, u8 type, u16 interval, u16 window,
1612 struct le_scan_params *param = &hdev->le_scan_params;
1614 BT_DBG("%s", hdev->name);
1616 if (work_busy(&hdev->le_scan))
1617 return -EINPROGRESS;
1620 param->interval = interval;
1621 param->window = window;
1622 param->timeout = timeout;
1624 queue_work(system_long_wq, &hdev->le_scan);
1629 /* Alloc HCI device */
1630 struct hci_dev *hci_alloc_dev(void)
1632 struct hci_dev *hdev;
1634 hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
1638 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
1639 hdev->esco_type = (ESCO_HV1);
1640 hdev->link_mode = (HCI_LM_ACCEPT);
1641 hdev->io_capability = 0x03; /* No Input No Output */
1643 hdev->sniff_max_interval = 800;
1644 hdev->sniff_min_interval = 80;
1646 mutex_init(&hdev->lock);
1647 mutex_init(&hdev->req_lock);
1649 INIT_LIST_HEAD(&hdev->mgmt_pending);
1650 INIT_LIST_HEAD(&hdev->blacklist);
1651 INIT_LIST_HEAD(&hdev->uuids);
1652 INIT_LIST_HEAD(&hdev->link_keys);
1653 INIT_LIST_HEAD(&hdev->long_term_keys);
1654 INIT_LIST_HEAD(&hdev->remote_oob_data);
1656 INIT_WORK(&hdev->rx_work, hci_rx_work);
1657 INIT_WORK(&hdev->cmd_work, hci_cmd_work);
1658 INIT_WORK(&hdev->tx_work, hci_tx_work);
1659 INIT_WORK(&hdev->power_on, hci_power_on);
1660 INIT_WORK(&hdev->le_scan, le_scan_work);
1662 INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
1663 INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
1664 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
1666 skb_queue_head_init(&hdev->driver_init);
1667 skb_queue_head_init(&hdev->rx_q);
1668 skb_queue_head_init(&hdev->cmd_q);
1669 skb_queue_head_init(&hdev->raw_q);
1671 init_waitqueue_head(&hdev->req_wait_q);
1673 setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev);
1675 hci_init_sysfs(hdev);
1676 discovery_init(hdev);
1677 hci_conn_hash_init(hdev);
1681 EXPORT_SYMBOL(hci_alloc_dev);
1683 /* Free HCI device */
1684 void hci_free_dev(struct hci_dev *hdev)
1686 skb_queue_purge(&hdev->driver_init);
1688 /* will free via device release */
1689 put_device(&hdev->dev);
1691 EXPORT_SYMBOL(hci_free_dev);
1693 /* Register HCI device */
1694 int hci_register_dev(struct hci_dev *hdev)
1698 if (!hdev->open || !hdev->close)
1701 /* Do not allow HCI_AMP devices to register at index 0,
1702 * so the index can be used as the AMP controller ID.
1704 switch (hdev->dev_type) {
1706 id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL);
1709 id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL);
1718 sprintf(hdev->name, "hci%d", id);
1721 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
1723 write_lock(&hci_dev_list_lock);
1724 list_add(&hdev->list, &hci_dev_list);
1725 write_unlock(&hci_dev_list_lock);
1727 hdev->workqueue = alloc_workqueue(hdev->name, WQ_HIGHPRI | WQ_UNBOUND |
1729 if (!hdev->workqueue) {
1734 error = hci_add_sysfs(hdev);
1738 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
1739 RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops,
1742 if (rfkill_register(hdev->rfkill) < 0) {
1743 rfkill_destroy(hdev->rfkill);
1744 hdev->rfkill = NULL;
1748 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
1749 set_bit(HCI_SETUP, &hdev->dev_flags);
1750 schedule_work(&hdev->power_on);
1752 hci_notify(hdev, HCI_DEV_REG);
1758 destroy_workqueue(hdev->workqueue);
1760 ida_simple_remove(&hci_index_ida, hdev->id);
1761 write_lock(&hci_dev_list_lock);
1762 list_del(&hdev->list);
1763 write_unlock(&hci_dev_list_lock);
1767 EXPORT_SYMBOL(hci_register_dev);
1769 /* Unregister HCI device */
1770 void hci_unregister_dev(struct hci_dev *hdev)
1774 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
1776 set_bit(HCI_UNREGISTER, &hdev->dev_flags);
1780 write_lock(&hci_dev_list_lock);
1781 list_del(&hdev->list);
1782 write_unlock(&hci_dev_list_lock);
1784 hci_dev_do_close(hdev);
1786 for (i = 0; i < NUM_REASSEMBLY; i++)
1787 kfree_skb(hdev->reassembly[i]);
1789 if (!test_bit(HCI_INIT, &hdev->flags) &&
1790 !test_bit(HCI_SETUP, &hdev->dev_flags)) {
1792 mgmt_index_removed(hdev);
1793 hci_dev_unlock(hdev);
1796 /* mgmt_index_removed should take care of emptying the
1798 BUG_ON(!list_empty(&hdev->mgmt_pending));
1800 hci_notify(hdev, HCI_DEV_UNREG);
1803 rfkill_unregister(hdev->rfkill);
1804 rfkill_destroy(hdev->rfkill);
1807 hci_del_sysfs(hdev);
1809 destroy_workqueue(hdev->workqueue);
1812 hci_blacklist_clear(hdev);
1813 hci_uuids_clear(hdev);
1814 hci_link_keys_clear(hdev);
1815 hci_smp_ltks_clear(hdev);
1816 hci_remote_oob_data_clear(hdev);
1817 hci_dev_unlock(hdev);
1821 ida_simple_remove(&hci_index_ida, id);
1823 EXPORT_SYMBOL(hci_unregister_dev);
1825 /* Suspend HCI device */
1826 int hci_suspend_dev(struct hci_dev *hdev)
1828 hci_notify(hdev, HCI_DEV_SUSPEND);
1831 EXPORT_SYMBOL(hci_suspend_dev);
1833 /* Resume HCI device */
1834 int hci_resume_dev(struct hci_dev *hdev)
1836 hci_notify(hdev, HCI_DEV_RESUME);
1839 EXPORT_SYMBOL(hci_resume_dev);
1841 /* Receive frame from HCI drivers */
1842 int hci_recv_frame(struct sk_buff *skb)
1844 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
1845 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
1846 && !test_bit(HCI_INIT, &hdev->flags))) {
1852 bt_cb(skb)->incoming = 1;
1855 __net_timestamp(skb);
1857 skb_queue_tail(&hdev->rx_q, skb);
1858 queue_work(hdev->workqueue, &hdev->rx_work);
1862 EXPORT_SYMBOL(hci_recv_frame);
1864 static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
1865 int count, __u8 index)
1870 struct sk_buff *skb;
1871 struct bt_skb_cb *scb;
1873 if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
1874 index >= NUM_REASSEMBLY)
1877 skb = hdev->reassembly[index];
1881 case HCI_ACLDATA_PKT:
1882 len = HCI_MAX_FRAME_SIZE;
1883 hlen = HCI_ACL_HDR_SIZE;
1886 len = HCI_MAX_EVENT_SIZE;
1887 hlen = HCI_EVENT_HDR_SIZE;
1889 case HCI_SCODATA_PKT:
1890 len = HCI_MAX_SCO_SIZE;
1891 hlen = HCI_SCO_HDR_SIZE;
1895 skb = bt_skb_alloc(len, GFP_ATOMIC);
1899 scb = (void *) skb->cb;
1901 scb->pkt_type = type;
1903 skb->dev = (void *) hdev;
1904 hdev->reassembly[index] = skb;
1908 scb = (void *) skb->cb;
1909 len = min_t(uint, scb->expect, count);
1911 memcpy(skb_put(skb, len), data, len);
1920 if (skb->len == HCI_EVENT_HDR_SIZE) {
1921 struct hci_event_hdr *h = hci_event_hdr(skb);
1922 scb->expect = h->plen;
1924 if (skb_tailroom(skb) < scb->expect) {
1926 hdev->reassembly[index] = NULL;
1932 case HCI_ACLDATA_PKT:
1933 if (skb->len == HCI_ACL_HDR_SIZE) {
1934 struct hci_acl_hdr *h = hci_acl_hdr(skb);
1935 scb->expect = __le16_to_cpu(h->dlen);
1937 if (skb_tailroom(skb) < scb->expect) {
1939 hdev->reassembly[index] = NULL;
1945 case HCI_SCODATA_PKT:
1946 if (skb->len == HCI_SCO_HDR_SIZE) {
1947 struct hci_sco_hdr *h = hci_sco_hdr(skb);
1948 scb->expect = h->dlen;
1950 if (skb_tailroom(skb) < scb->expect) {
1952 hdev->reassembly[index] = NULL;
1959 if (scb->expect == 0) {
1960 /* Complete frame */
1962 bt_cb(skb)->pkt_type = type;
1963 hci_recv_frame(skb);
1965 hdev->reassembly[index] = NULL;
1973 int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
1977 if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
1981 rem = hci_reassembly(hdev, type, data, count, type - 1);
1985 data += (count - rem);
1991 EXPORT_SYMBOL(hci_recv_fragment);
1993 #define STREAM_REASSEMBLY 0
1995 int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
2001 struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
2004 struct { char type; } *pkt;
2006 /* Start of the frame */
2013 type = bt_cb(skb)->pkt_type;
2015 rem = hci_reassembly(hdev, type, data, count,
2020 data += (count - rem);
2026 EXPORT_SYMBOL(hci_recv_stream_fragment);
2028 /* ---- Interface to upper protocols ---- */
2030 int hci_register_cb(struct hci_cb *cb)
2032 BT_DBG("%p name %s", cb, cb->name);
2034 write_lock(&hci_cb_list_lock);
2035 list_add(&cb->list, &hci_cb_list);
2036 write_unlock(&hci_cb_list_lock);
2040 EXPORT_SYMBOL(hci_register_cb);
2042 int hci_unregister_cb(struct hci_cb *cb)
2044 BT_DBG("%p name %s", cb, cb->name);
2046 write_lock(&hci_cb_list_lock);
2047 list_del(&cb->list);
2048 write_unlock(&hci_cb_list_lock);
2052 EXPORT_SYMBOL(hci_unregister_cb);
2054 static int hci_send_frame(struct sk_buff *skb)
2056 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2063 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
2066 __net_timestamp(skb);
2068 /* Send copy to monitor */
2069 hci_send_to_monitor(hdev, skb);
2071 if (atomic_read(&hdev->promisc)) {
2072 /* Send copy to the sockets */
2073 hci_send_to_sock(hdev, skb);
2076 /* Get rid of skb owner, prior to sending to the driver. */
2079 return hdev->send(skb);
2082 /* Send HCI command */
2083 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
2085 int len = HCI_COMMAND_HDR_SIZE + plen;
2086 struct hci_command_hdr *hdr;
2087 struct sk_buff *skb;
2089 BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen);
2091 skb = bt_skb_alloc(len, GFP_ATOMIC);
2093 BT_ERR("%s no memory for command", hdev->name);
2097 hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
2098 hdr->opcode = cpu_to_le16(opcode);
2102 memcpy(skb_put(skb, plen), param, plen);
2104 BT_DBG("skb len %d", skb->len);
2106 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
2107 skb->dev = (void *) hdev;
2109 if (test_bit(HCI_INIT, &hdev->flags))
2110 hdev->init_last_cmd = opcode;
2112 skb_queue_tail(&hdev->cmd_q, skb);
2113 queue_work(hdev->workqueue, &hdev->cmd_work);
2118 /* Get data from the previously sent command */
2119 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
2121 struct hci_command_hdr *hdr;
2123 if (!hdev->sent_cmd)
2126 hdr = (void *) hdev->sent_cmd->data;
2128 if (hdr->opcode != cpu_to_le16(opcode))
2131 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
2133 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
2137 static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
2139 struct hci_acl_hdr *hdr;
2142 skb_push(skb, HCI_ACL_HDR_SIZE);
2143 skb_reset_transport_header(skb);
2144 hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
2145 hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
2146 hdr->dlen = cpu_to_le16(len);
2149 static void hci_queue_acl(struct hci_conn *conn, struct sk_buff_head *queue,
2150 struct sk_buff *skb, __u16 flags)
2152 struct hci_dev *hdev = conn->hdev;
2153 struct sk_buff *list;
2155 skb->len = skb_headlen(skb);
2158 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
2159 hci_add_acl_hdr(skb, conn->handle, flags);
2161 list = skb_shinfo(skb)->frag_list;
2163 /* Non fragmented */
2164 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
2166 skb_queue_tail(queue, skb);
2169 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2171 skb_shinfo(skb)->frag_list = NULL;
2173 /* Queue all fragments atomically */
2174 spin_lock(&queue->lock);
2176 __skb_queue_tail(queue, skb);
2178 flags &= ~ACL_START;
2181 skb = list; list = list->next;
2183 skb->dev = (void *) hdev;
2184 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
2185 hci_add_acl_hdr(skb, conn->handle, flags);
2187 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2189 __skb_queue_tail(queue, skb);
2192 spin_unlock(&queue->lock);
2196 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
2198 struct hci_conn *conn = chan->conn;
2199 struct hci_dev *hdev = conn->hdev;
2201 BT_DBG("%s chan %p flags 0x%x", hdev->name, chan, flags);
2203 skb->dev = (void *) hdev;
2205 hci_queue_acl(conn, &chan->data_q, skb, flags);
2207 queue_work(hdev->workqueue, &hdev->tx_work);
2211 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
2213 struct hci_dev *hdev = conn->hdev;
2214 struct hci_sco_hdr hdr;
2216 BT_DBG("%s len %d", hdev->name, skb->len);
2218 hdr.handle = cpu_to_le16(conn->handle);
2219 hdr.dlen = skb->len;
2221 skb_push(skb, HCI_SCO_HDR_SIZE);
2222 skb_reset_transport_header(skb);
2223 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
2225 skb->dev = (void *) hdev;
2226 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
2228 skb_queue_tail(&conn->data_q, skb);
2229 queue_work(hdev->workqueue, &hdev->tx_work);
2232 /* ---- HCI TX task (outgoing data) ---- */
2234 /* HCI Connection scheduler */
2235 static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type,
2238 struct hci_conn_hash *h = &hdev->conn_hash;
2239 struct hci_conn *conn = NULL, *c;
2240 unsigned int num = 0, min = ~0;
2242 /* We don't have to lock device here. Connections are always
2243 * added and removed with TX task disabled. */
2247 list_for_each_entry_rcu(c, &h->list, list) {
2248 if (c->type != type || skb_queue_empty(&c->data_q))
2251 if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
2256 if (c->sent < min) {
2261 if (hci_conn_num(hdev, type) == num)
2270 switch (conn->type) {
2272 cnt = hdev->acl_cnt;
2276 cnt = hdev->sco_cnt;
2279 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2283 BT_ERR("Unknown link type");
2291 BT_DBG("conn %p quote %d", conn, *quote);
2295 static void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
2297 struct hci_conn_hash *h = &hdev->conn_hash;
2300 BT_ERR("%s link tx timeout", hdev->name);
2304 /* Kill stalled connections */
2305 list_for_each_entry_rcu(c, &h->list, list) {
2306 if (c->type == type && c->sent) {
2307 BT_ERR("%s killing stalled connection %s",
2308 hdev->name, batostr(&c->dst));
2309 hci_acl_disconn(c, 0x13);
2316 static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
2319 struct hci_conn_hash *h = &hdev->conn_hash;
2320 struct hci_chan *chan = NULL;
2321 unsigned int num = 0, min = ~0, cur_prio = 0;
2322 struct hci_conn *conn;
2323 int cnt, q, conn_num = 0;
2325 BT_DBG("%s", hdev->name);
2329 list_for_each_entry_rcu(conn, &h->list, list) {
2330 struct hci_chan *tmp;
2332 if (conn->type != type)
2335 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2340 list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
2341 struct sk_buff *skb;
2343 if (skb_queue_empty(&tmp->data_q))
2346 skb = skb_peek(&tmp->data_q);
2347 if (skb->priority < cur_prio)
2350 if (skb->priority > cur_prio) {
2353 cur_prio = skb->priority;
2358 if (conn->sent < min) {
2364 if (hci_conn_num(hdev, type) == conn_num)
2373 switch (chan->conn->type) {
2375 cnt = hdev->acl_cnt;
2379 cnt = hdev->sco_cnt;
2382 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2386 BT_ERR("Unknown link type");
2391 BT_DBG("chan %p quote %d", chan, *quote);
2395 static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
2397 struct hci_conn_hash *h = &hdev->conn_hash;
2398 struct hci_conn *conn;
2401 BT_DBG("%s", hdev->name);
2405 list_for_each_entry_rcu(conn, &h->list, list) {
2406 struct hci_chan *chan;
2408 if (conn->type != type)
2411 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2416 list_for_each_entry_rcu(chan, &conn->chan_list, list) {
2417 struct sk_buff *skb;
2424 if (skb_queue_empty(&chan->data_q))
2427 skb = skb_peek(&chan->data_q);
2428 if (skb->priority >= HCI_PRIO_MAX - 1)
2431 skb->priority = HCI_PRIO_MAX - 1;
2433 BT_DBG("chan %p skb %p promoted to %d", chan, skb,
2437 if (hci_conn_num(hdev, type) == num)
2445 static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb)
2447 /* Calculate count of blocks used by this packet */
2448 return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len);
2451 static void __check_timeout(struct hci_dev *hdev, unsigned int cnt)
2453 if (!test_bit(HCI_RAW, &hdev->flags)) {
2454 /* ACL tx timeout must be longer than maximum
2455 * link supervision timeout (40.9 seconds) */
2456 if (!cnt && time_after(jiffies, hdev->acl_last_tx +
2457 msecs_to_jiffies(HCI_ACL_TX_TIMEOUT)))
2458 hci_link_tx_to(hdev, ACL_LINK);
2462 static void hci_sched_acl_pkt(struct hci_dev *hdev)
2464 unsigned int cnt = hdev->acl_cnt;
2465 struct hci_chan *chan;
2466 struct sk_buff *skb;
2469 __check_timeout(hdev, cnt);
2471 while (hdev->acl_cnt &&
2472 (chan = hci_chan_sent(hdev, ACL_LINK, "e))) {
2473 u32 priority = (skb_peek(&chan->data_q))->priority;
2474 while (quote-- && (skb = skb_peek(&chan->data_q))) {
2475 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2476 skb->len, skb->priority);
2478 /* Stop if priority has changed */
2479 if (skb->priority < priority)
2482 skb = skb_dequeue(&chan->data_q);
2484 hci_conn_enter_active_mode(chan->conn,
2485 bt_cb(skb)->force_active);
2487 hci_send_frame(skb);
2488 hdev->acl_last_tx = jiffies;
2496 if (cnt != hdev->acl_cnt)
2497 hci_prio_recalculate(hdev, ACL_LINK);
2500 static void hci_sched_acl_blk(struct hci_dev *hdev)
2502 unsigned int cnt = hdev->block_cnt;
2503 struct hci_chan *chan;
2504 struct sk_buff *skb;
2507 __check_timeout(hdev, cnt);
2509 while (hdev->block_cnt > 0 &&
2510 (chan = hci_chan_sent(hdev, ACL_LINK, "e))) {
2511 u32 priority = (skb_peek(&chan->data_q))->priority;
2512 while (quote > 0 && (skb = skb_peek(&chan->data_q))) {
2515 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2516 skb->len, skb->priority);
2518 /* Stop if priority has changed */
2519 if (skb->priority < priority)
2522 skb = skb_dequeue(&chan->data_q);
2524 blocks = __get_blocks(hdev, skb);
2525 if (blocks > hdev->block_cnt)
2528 hci_conn_enter_active_mode(chan->conn,
2529 bt_cb(skb)->force_active);
2531 hci_send_frame(skb);
2532 hdev->acl_last_tx = jiffies;
2534 hdev->block_cnt -= blocks;
2537 chan->sent += blocks;
2538 chan->conn->sent += blocks;
2542 if (cnt != hdev->block_cnt)
2543 hci_prio_recalculate(hdev, ACL_LINK);
2546 static void hci_sched_acl(struct hci_dev *hdev)
2548 BT_DBG("%s", hdev->name);
2550 if (!hci_conn_num(hdev, ACL_LINK))
2553 switch (hdev->flow_ctl_mode) {
2554 case HCI_FLOW_CTL_MODE_PACKET_BASED:
2555 hci_sched_acl_pkt(hdev);
2558 case HCI_FLOW_CTL_MODE_BLOCK_BASED:
2559 hci_sched_acl_blk(hdev);
2565 static void hci_sched_sco(struct hci_dev *hdev)
2567 struct hci_conn *conn;
2568 struct sk_buff *skb;
2571 BT_DBG("%s", hdev->name);
2573 if (!hci_conn_num(hdev, SCO_LINK))
2576 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) {
2577 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2578 BT_DBG("skb %p len %d", skb, skb->len);
2579 hci_send_frame(skb);
2582 if (conn->sent == ~0)
2588 static void hci_sched_esco(struct hci_dev *hdev)
2590 struct hci_conn *conn;
2591 struct sk_buff *skb;
2594 BT_DBG("%s", hdev->name);
2596 if (!hci_conn_num(hdev, ESCO_LINK))
2599 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK,
2601 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2602 BT_DBG("skb %p len %d", skb, skb->len);
2603 hci_send_frame(skb);
2606 if (conn->sent == ~0)
2612 static void hci_sched_le(struct hci_dev *hdev)
2614 struct hci_chan *chan;
2615 struct sk_buff *skb;
2616 int quote, cnt, tmp;
2618 BT_DBG("%s", hdev->name);
2620 if (!hci_conn_num(hdev, LE_LINK))
2623 if (!test_bit(HCI_RAW, &hdev->flags)) {
2624 /* LE tx timeout must be longer than maximum
2625 * link supervision timeout (40.9 seconds) */
2626 if (!hdev->le_cnt && hdev->le_pkts &&
2627 time_after(jiffies, hdev->le_last_tx + HZ * 45))
2628 hci_link_tx_to(hdev, LE_LINK);
2631 cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
2633 while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) {
2634 u32 priority = (skb_peek(&chan->data_q))->priority;
2635 while (quote-- && (skb = skb_peek(&chan->data_q))) {
2636 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2637 skb->len, skb->priority);
2639 /* Stop if priority has changed */
2640 if (skb->priority < priority)
2643 skb = skb_dequeue(&chan->data_q);
2645 hci_send_frame(skb);
2646 hdev->le_last_tx = jiffies;
2657 hdev->acl_cnt = cnt;
2660 hci_prio_recalculate(hdev, LE_LINK);
2663 static void hci_tx_work(struct work_struct *work)
2665 struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
2666 struct sk_buff *skb;
2668 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
2669 hdev->sco_cnt, hdev->le_cnt);
2671 /* Schedule queues and send stuff to HCI driver */
2673 hci_sched_acl(hdev);
2675 hci_sched_sco(hdev);
2677 hci_sched_esco(hdev);
2681 /* Send next queued raw (unknown type) packet */
2682 while ((skb = skb_dequeue(&hdev->raw_q)))
2683 hci_send_frame(skb);
2686 /* ----- HCI RX task (incoming data processing) ----- */
2688 /* ACL data packet */
2689 static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2691 struct hci_acl_hdr *hdr = (void *) skb->data;
2692 struct hci_conn *conn;
2693 __u16 handle, flags;
2695 skb_pull(skb, HCI_ACL_HDR_SIZE);
2697 handle = __le16_to_cpu(hdr->handle);
2698 flags = hci_flags(handle);
2699 handle = hci_handle(handle);
2701 BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len,
2704 hdev->stat.acl_rx++;
2707 conn = hci_conn_hash_lookup_handle(hdev, handle);
2708 hci_dev_unlock(hdev);
2711 hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
2714 if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
2715 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
2716 mgmt_device_connected(hdev, &conn->dst, conn->type,
2717 conn->dst_type, 0, NULL, 0,
2719 hci_dev_unlock(hdev);
2721 /* Send to upper protocol */
2722 l2cap_recv_acldata(conn, skb, flags);
2725 BT_ERR("%s ACL packet for unknown connection handle %d",
2726 hdev->name, handle);
2732 /* SCO data packet */
2733 static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2735 struct hci_sco_hdr *hdr = (void *) skb->data;
2736 struct hci_conn *conn;
2739 skb_pull(skb, HCI_SCO_HDR_SIZE);
2741 handle = __le16_to_cpu(hdr->handle);
2743 BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle);
2745 hdev->stat.sco_rx++;
2748 conn = hci_conn_hash_lookup_handle(hdev, handle);
2749 hci_dev_unlock(hdev);
2752 /* Send to upper protocol */
2753 sco_recv_scodata(conn, skb);
2756 BT_ERR("%s SCO packet for unknown connection handle %d",
2757 hdev->name, handle);
2763 static void hci_rx_work(struct work_struct *work)
2765 struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
2766 struct sk_buff *skb;
2768 BT_DBG("%s", hdev->name);
2770 while ((skb = skb_dequeue(&hdev->rx_q))) {
2771 /* Send copy to monitor */
2772 hci_send_to_monitor(hdev, skb);
2774 if (atomic_read(&hdev->promisc)) {
2775 /* Send copy to the sockets */
2776 hci_send_to_sock(hdev, skb);
2779 if (test_bit(HCI_RAW, &hdev->flags)) {
2784 if (test_bit(HCI_INIT, &hdev->flags)) {
2785 /* Don't process data packets in this states. */
2786 switch (bt_cb(skb)->pkt_type) {
2787 case HCI_ACLDATA_PKT:
2788 case HCI_SCODATA_PKT:
2795 switch (bt_cb(skb)->pkt_type) {
2797 BT_DBG("%s Event packet", hdev->name);
2798 hci_event_packet(hdev, skb);
2801 case HCI_ACLDATA_PKT:
2802 BT_DBG("%s ACL data packet", hdev->name);
2803 hci_acldata_packet(hdev, skb);
2806 case HCI_SCODATA_PKT:
2807 BT_DBG("%s SCO data packet", hdev->name);
2808 hci_scodata_packet(hdev, skb);
2818 static void hci_cmd_work(struct work_struct *work)
2820 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
2821 struct sk_buff *skb;
2823 BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
2825 /* Send queued commands */
2826 if (atomic_read(&hdev->cmd_cnt)) {
2827 skb = skb_dequeue(&hdev->cmd_q);
2831 kfree_skb(hdev->sent_cmd);
2833 hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC);
2834 if (hdev->sent_cmd) {
2835 atomic_dec(&hdev->cmd_cnt);
2836 hci_send_frame(skb);
2837 if (test_bit(HCI_RESET, &hdev->flags))
2838 del_timer(&hdev->cmd_timer);
2840 mod_timer(&hdev->cmd_timer,
2841 jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT));
2843 skb_queue_head(&hdev->cmd_q, skb);
2844 queue_work(hdev->workqueue, &hdev->cmd_work);
2849 int hci_do_inquiry(struct hci_dev *hdev, u8 length)
2851 /* General inquiry access code (GIAC) */
2852 u8 lap[3] = { 0x33, 0x8b, 0x9e };
2853 struct hci_cp_inquiry cp;
2855 BT_DBG("%s", hdev->name);
2857 if (test_bit(HCI_INQUIRY, &hdev->flags))
2858 return -EINPROGRESS;
2860 inquiry_cache_flush(hdev);
2862 memset(&cp, 0, sizeof(cp));
2863 memcpy(&cp.lap, lap, sizeof(cp.lap));
2866 return hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
2869 int hci_cancel_inquiry(struct hci_dev *hdev)
2871 BT_DBG("%s", hdev->name);
2873 if (!test_bit(HCI_INQUIRY, &hdev->flags))
2876 return hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL);
2879 u8 bdaddr_to_le(u8 bdaddr_type)
2881 switch (bdaddr_type) {
2882 case BDADDR_LE_PUBLIC:
2883 return ADDR_LE_DEV_PUBLIC;
2886 /* Fallback to LE Random address type */
2887 return ADDR_LE_DEV_RANDOM;