📄 hci_core.c
字号:
/* BlueZ - Bluetooth protocol stack for Linux Copyright (C) 2000-2001 Qualcomm Incorporated Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE IS DISCLAIMED.*//* Bluetooth HCI core. */#include <linux/config.h>#include <linux/module.h>#include <linux/kmod.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/major.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/poll.h>#include <linux/fcntl.h>#include <linux/init.h>#include <linux/skbuff.h>#include <linux/interrupt.h>#include <linux/notifier.h>#include <net/sock.h>#include <asm/system.h>#include <asm/uaccess.h>#include <asm/unaligned.h>#include <net/bluetooth/bluetooth.h>#include <net/bluetooth/hci_core.h>#ifndef CONFIG_BT_HCI_CORE_DEBUG#undef BT_DBG#define BT_DBG(D...)#endifstatic void hci_cmd_task(unsigned long arg);static void hci_rx_task(unsigned long arg);static void hci_tx_task(unsigned long arg);static void hci_notify(struct hci_dev *hdev, int event);rwlock_t hci_task_lock = RW_LOCK_UNLOCKED;/* HCI device list */LIST_HEAD(hci_dev_list);rwlock_t hci_dev_list_lock = RW_LOCK_UNLOCKED;/* HCI protocols */#define HCI_MAX_PROTO 2struct hci_proto *hci_proto[HCI_MAX_PROTO];/* HCI notifiers list */static struct notifier_block *hci_notifier;/* ---- HCI notifications ---- */int hci_register_notifier(struct notifier_block *nb){ return notifier_chain_register(&hci_notifier, nb);}int hci_unregister_notifier(struct notifier_block *nb){ return notifier_chain_unregister(&hci_notifier, nb);}void hci_notify(struct hci_dev *hdev, int event){ notifier_call_chain(&hci_notifier, event, hdev);}/* ---- HCI requests ---- */void hci_req_complete(struct hci_dev *hdev, int result){ BT_DBG("%s result 0x%2.2x", hdev->name, result); if (hdev->req_status == HCI_REQ_PEND) { hdev->req_result = result; hdev->req_status = HCI_REQ_DONE; wake_up_interruptible(&hdev->req_wait_q); }}void hci_req_cancel(struct hci_dev *hdev, int err){ BT_DBG("%s err 0x%2.2x", hdev->name, err); if (hdev->req_status == HCI_REQ_PEND) { hdev->req_result = err; hdev->req_status = HCI_REQ_CANCELED; wake_up_interruptible(&hdev->req_wait_q); }}/* Execute request and wait for completion. */static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), unsigned long opt, __u32 timeout){ DECLARE_WAITQUEUE(wait, current); int err = 0; BT_DBG("%s start", hdev->name); hdev->req_status = HCI_REQ_PEND; add_wait_queue(&hdev->req_wait_q, &wait); set_current_state(TASK_INTERRUPTIBLE); req(hdev, opt); schedule_timeout(timeout); remove_wait_queue(&hdev->req_wait_q, &wait); if (signal_pending(current)) return -EINTR; switch (hdev->req_status) { case HCI_REQ_DONE: err = -bt_err(hdev->req_result); break; case HCI_REQ_CANCELED: err = -hdev->req_result; break; default: err = -ETIMEDOUT; break; }; hdev->req_status = hdev->req_result = 0; BT_DBG("%s end: err %d", hdev->name, err); return err;}static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), unsigned long opt, __u32 timeout){ int ret; /* Serialize all requests */ hci_req_lock(hdev); ret = __hci_request(hdev, req, opt, timeout); hci_req_unlock(hdev); return ret;}static void hci_reset_req(struct hci_dev *hdev, unsigned long opt){ BT_DBG("%s %ld", hdev->name, opt); /* Reset device */ hci_send_cmd(hdev, OGF_HOST_CTL, OCF_RESET, 0, NULL);}static void hci_init_req(struct hci_dev *hdev, unsigned long opt){ __u16 param; BT_DBG("%s %ld", hdev->name, opt); /* Mandatory initialization */ /* Reset */ if (test_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks)) hci_send_cmd(hdev, OGF_HOST_CTL, OCF_RESET, 0, NULL); /* Read Local Supported Features */ hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_LOCAL_FEATURES, 0, NULL); /* Read Buffer Size (ACL mtu, max pkt, etc.) */ hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_BUFFER_SIZE, 0, NULL);#if 0 /* Host buffer size */ { struct hci_cp_host_buffer_size cp; cp.acl_mtu = __cpu_to_le16(HCI_MAX_ACL_SIZE); cp.sco_mtu = HCI_MAX_SCO_SIZE; cp.acl_max_pkt = __cpu_to_le16(0xffff); cp.sco_max_pkt = __cpu_to_le16(0xffff); hci_send_cmd(hdev, OGF_HOST_CTL, OCF_HOST_BUFFER_SIZE, sizeof(cp), &cp); }#endif /* Read BD Address */ hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_BD_ADDR, 0, NULL); /* Read Voice Setting */ hci_send_cmd(hdev, OGF_HOST_CTL, OCF_READ_VOICE_SETTING, 0, NULL); /* Optional initialization */ /* Clear Event Filters */ { struct hci_cp_set_event_flt cp; cp.flt_type = HCI_FLT_CLEAR_ALL; hci_send_cmd(hdev, OGF_HOST_CTL, OCF_SET_EVENT_FLT, sizeof(cp), &cp); } /* Page timeout ~20 secs */ param = __cpu_to_le16(0x8000); hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_PG_TIMEOUT, 2, ¶m); /* Connection accept timeout ~20 secs */ param = __cpu_to_le16(0x7d00); hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_CA_TIMEOUT, 2, ¶m);}static void hci_scan_req(struct hci_dev *hdev, unsigned long opt){ __u8 scan = opt; BT_DBG("%s %x", hdev->name, scan); /* Inquiry and Page scans */ hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE, 1, &scan);}static void hci_auth_req(struct hci_dev *hdev, unsigned long opt){ __u8 auth = opt; BT_DBG("%s %x", hdev->name, auth); /* Authentication */ hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE, 1, &auth);}static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt){ __u8 encrypt = opt; BT_DBG("%s %x", hdev->name, encrypt); /* Authentication */ hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE, 1, &encrypt);}/* Get HCI device by index. * Device is held on return. */struct hci_dev *hci_dev_get(int index){ struct hci_dev *hdev = NULL; struct list_head *p; BT_DBG("%d", index); if (index < 0) return NULL; read_lock(&hci_dev_list_lock); list_for_each(p, &hci_dev_list) { struct hci_dev *d = list_entry(p, struct hci_dev, list); if (d->id == index) { hdev = hci_dev_hold(d); break; } } read_unlock(&hci_dev_list_lock); return hdev;}EXPORT_SYMBOL(hci_dev_get);/* ---- Inquiry support ---- */static void inquiry_cache_flush(struct hci_dev *hdev){ struct inquiry_cache *cache = &hdev->inq_cache; struct inquiry_entry *next = cache->list, *e; BT_DBG("cache %p", cache); cache->list = NULL; while ((e = next)) { next = e->next; kfree(e); }}struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr){ struct inquiry_cache *cache = &hdev->inq_cache; struct inquiry_entry *e; BT_DBG("cache %p, %s", cache, batostr(bdaddr)); for (e = cache->list; e; e = e->next) if (!bacmp(&e->info.bdaddr, bdaddr)) break; return e;}void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_info *info){ struct inquiry_cache *cache = &hdev->inq_cache; struct inquiry_entry *e; BT_DBG("cache %p, %s", cache, batostr(&info->bdaddr)); if (!(e = hci_inquiry_cache_lookup(hdev, &info->bdaddr))) { /* Entry not in the cache. Add new one. */ if (!(e = kmalloc(sizeof(struct inquiry_entry), GFP_ATOMIC))) return; memset(e, 0, sizeof(struct inquiry_entry)); e->next = cache->list; cache->list = e; } memcpy(&e->info, info, sizeof(*info)); e->timestamp = jiffies; cache->timestamp = jiffies;}static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf){ struct inquiry_cache *cache = &hdev->inq_cache; struct inquiry_info *info = (struct inquiry_info *) buf; struct inquiry_entry *e; int copied = 0; for (e = cache->list; e && copied < num; e = e->next, copied++) memcpy(info++, &e->info, sizeof(*info)); BT_DBG("cache %p, copied %d", cache, copied); return copied;}static void hci_inq_req(struct hci_dev *hdev, unsigned long opt){ struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; struct hci_cp_inquiry cp; BT_DBG("%s", hdev->name); if (test_bit(HCI_INQUIRY, &hdev->flags)) return; /* Start Inquiry */ memcpy(&cp.lap, &ir->lap, 3); cp.length = ir->length; cp.num_rsp = ir->num_rsp; hci_send_cmd(hdev, OGF_LINK_CTL, OCF_INQUIRY, sizeof(cp), &cp);}int hci_inquiry(void __user *arg){ __u8 __user *ptr = arg; struct hci_inquiry_req ir; struct hci_dev *hdev; int err = 0, do_inquiry = 0, max_rsp; long timeo; __u8 *buf; if (copy_from_user(&ir, ptr, sizeof(ir))) return -EFAULT; if (!(hdev = hci_dev_get(ir.dev_id))) return -ENODEV; hci_dev_lock_bh(hdev); if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { inquiry_cache_flush(hdev); do_inquiry = 1; } hci_dev_unlock_bh(hdev); timeo = ir.length * 2 * HZ; if (do_inquiry && (err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo)) < 0) goto done; /* for unlimited number of responses we will use buffer with 255 entries */ max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; /* cache_dump can't sleep. Therefore we allocate temp buffer and then * copy it to the user space. */ if (!(buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL))) { err = -ENOMEM; goto done; } hci_dev_lock_bh(hdev); ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); hci_dev_unlock_bh(hdev); BT_DBG("num_rsp %d", ir.num_rsp); if (!copy_to_user(ptr, &ir, sizeof(ir))) { ptr += sizeof(ir); if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * ir.num_rsp)) err = -EFAULT; } else err = -EFAULT; kfree(buf);done: hci_dev_put(hdev); return err;}/* ---- HCI ioctl helpers ---- */int hci_dev_open(__u16 dev){ struct hci_dev *hdev; int ret = 0; if (!(hdev = hci_dev_get(dev))) return -ENODEV; BT_DBG("%s %p", hdev->name, hdev); hci_req_lock(hdev); if (test_bit(HCI_UP, &hdev->flags)) { ret = -EALREADY; goto done; } if (hdev->open(hdev)) { ret = -EIO; goto done; } if (!test_bit(HCI_RAW, &hdev->flags)) { atomic_set(&hdev->cmd_cnt, 1); set_bit(HCI_INIT, &hdev->flags); //__hci_request(hdev, hci_reset_req, 0, HZ); ret = __hci_request(hdev, hci_init_req, 0, HCI_INIT_TIMEOUT); clear_bit(HCI_INIT, &hdev->flags); } if (!ret) { hci_dev_hold(hdev); set_bit(HCI_UP, &hdev->flags); hci_notify(hdev, HCI_DEV_UP); } else { /* Init failed, cleanup */ tasklet_kill(&hdev->rx_task); tasklet_kill(&hdev->tx_task); tasklet_kill(&hdev->cmd_task); skb_queue_purge(&hdev->cmd_q); skb_queue_purge(&hdev->rx_q); if (hdev->flush) hdev->flush(hdev); if (hdev->sent_cmd) { kfree_skb(hdev->sent_cmd); hdev->sent_cmd = NULL; } hdev->close(hdev); hdev->flags = 0; }done: hci_req_unlock(hdev); hci_dev_put(hdev); return ret;}static int hci_dev_do_close(struct hci_dev *hdev){ BT_DBG("%s %p", hdev->name, hdev); hci_req_cancel(hdev, ENODEV); hci_req_lock(hdev); if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { hci_req_unlock(hdev); return 0; } /* Kill RX and TX tasks */ tasklet_kill(&hdev->rx_task); tasklet_kill(&hdev->tx_task); hci_dev_lock_bh(hdev); inquiry_cache_flush(hdev); hci_conn_hash_flush(hdev); hci_dev_unlock_bh(hdev); hci_notify(hdev, HCI_DEV_DOWN); if (hdev->flush) hdev->flush(hdev); /* Reset device */ skb_queue_purge(&hdev->cmd_q); atomic_set(&hdev->cmd_cnt, 1); set_bit(HCI_INIT, &hdev->flags); __hci_request(hdev, hci_reset_req, 0, HZ/4); clear_bit(HCI_INIT, &hdev->flags); /* Kill cmd task */ tasklet_kill(&hdev->cmd_task); /* Drop queues */ skb_queue_purge(&hdev->rx_q); skb_queue_purge(&hdev->cmd_q); skb_queue_purge(&hdev->raw_q); /* Drop last sent command */ if (hdev->sent_cmd) { kfree_skb(hdev->sent_cmd); hdev->sent_cmd = NULL; } /* After this point our queues are empty * and no tasks are scheduled. */ hdev->close(hdev); /* Clear flags */ hdev->flags = 0; hci_req_unlock(hdev); hci_dev_put(hdev); return 0;}int hci_dev_close(__u16 dev){ struct hci_dev *hdev; int err; if (!(hdev = hci_dev_get(dev))) return -ENODEV; err = hci_dev_do_close(hdev); hci_dev_put(hdev); return err;}int hci_dev_reset(__u16 dev){ struct hci_dev *hdev; int ret = 0; if (!(hdev = hci_dev_get(dev))) return -ENODEV; hci_req_lock(hdev); tasklet_disable(&hdev->tx_task); if (!test_bit(HCI_UP, &hdev->flags)) goto done; /* Drop queues */ skb_queue_purge(&hdev->rx_q); skb_queue_purge(&hdev->cmd_q); hci_dev_lock_bh(hdev); inquiry_cache_flush(hdev); hci_conn_hash_flush(hdev); hci_dev_unlock_bh(hdev); if (hdev->flush) hdev->flush(hdev); atomic_set(&hdev->cmd_cnt, 1); hdev->acl_cnt = 0; hdev->sco_cnt = 0; ret = __hci_request(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT);done: tasklet_enable(&hdev->tx_task); hci_req_unlock(hdev); hci_dev_put(hdev); return ret;}int hci_dev_reset_stat(__u16 dev){ struct hci_dev *hdev; int ret = 0; if (!(hdev = hci_dev_get(dev))) return -ENODEV; memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); hci_dev_put(hdev); return ret;}int hci_dev_cmd(unsigned int cmd, void __user *arg){ struct hci_dev *hdev; struct hci_dev_req dr; int err = 0; if (copy_from_user(&dr, arg, sizeof(dr))) return -EFAULT; if (!(hdev = hci_dev_get(dr.dev_id))) return -ENODEV; switch (cmd) { case HCISETAUTH: err = hci_request(hdev, hci_auth_req, dr.dev_opt, HCI_INIT_TIMEOUT); break; case HCISETENCRYPT: if (!lmp_encrypt_capable(hdev)) { err = -EOPNOTSUPP; break; } if (!test_bit(HCI_AUTH, &hdev->flags)) { /* Auth must be enabled first */ err = hci_request(hdev, hci_auth_req, dr.dev_opt, HCI_INIT_TIMEOUT); if (err) break; } err = hci_request(hdev, hci_encrypt_req, dr.dev_opt, HCI_INIT_TIMEOUT); break; case HCISETSCAN: err = hci_request(hdev, hci_scan_req, dr.dev_opt, HCI_INIT_TIMEOUT); break; case HCISETPTYPE: hdev->pkt_type = (__u16) dr.dev_opt; break; case HCISETLINKPOL: hdev->link_policy = (__u16) dr.dev_opt; break; case HCISETLINKMODE: hdev->link_mode = ((__u16) dr.dev_opt) & (HCI_LM_MASTER | HCI_LM_ACCEPT); break; case HCISETACLMTU: hdev->acl_mtu = *((__u16 *)&dr.dev_opt + 1); hdev->acl_pkts = *((__u16 *)&dr.dev_opt + 0); break; case HCISETSCOMTU: hdev->sco_mtu = *((__u16 *)&dr.dev_opt + 1); hdev->sco_pkts = *((__u16 *)&dr.dev_opt + 0); break; default: err = -EINVAL; break; } hci_dev_put(hdev); return err;}int hci_get_dev_list(void __user *arg)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -