[PATCH] Fix sighand->siglock usage in kernel/acct.c
[linux-2.6/kmemtrace.git] / net / bluetooth / hci_core.c
blob5ed47427790385fc335168857d02082deb2ab498
1 /*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
25 /* Bluetooth HCI core. */
27 #include <linux/module.h>
28 #include <linux/kmod.h>
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/skbuff.h>
39 #include <linux/interrupt.h>
40 #include <linux/notifier.h>
41 #include <net/sock.h>
43 #include <asm/system.h>
44 #include <asm/uaccess.h>
45 #include <asm/unaligned.h>
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
50 #ifndef CONFIG_BT_HCI_CORE_DEBUG
51 #undef BT_DBG
52 #define BT_DBG(D...)
53 #endif
55 static void hci_cmd_task(unsigned long arg);
56 static void hci_rx_task(unsigned long arg);
57 static void hci_tx_task(unsigned long arg);
58 static void hci_notify(struct hci_dev *hdev, int event);
60 static DEFINE_RWLOCK(hci_task_lock);
62 /* HCI device list */
63 LIST_HEAD(hci_dev_list);
64 DEFINE_RWLOCK(hci_dev_list_lock);
66 /* HCI callback list */
67 LIST_HEAD(hci_cb_list);
68 DEFINE_RWLOCK(hci_cb_list_lock);
70 /* HCI protocols */
71 #define HCI_MAX_PROTO 2
72 struct hci_proto *hci_proto[HCI_MAX_PROTO];
74 /* HCI notifiers list */
75 static ATOMIC_NOTIFIER_HEAD(hci_notifier);
77 /* ---- HCI notifications ---- */
79 int hci_register_notifier(struct notifier_block *nb)
81 return atomic_notifier_chain_register(&hci_notifier, nb);
84 int hci_unregister_notifier(struct notifier_block *nb)
86 return atomic_notifier_chain_unregister(&hci_notifier, nb);
89 static void hci_notify(struct hci_dev *hdev, int event)
91 atomic_notifier_call_chain(&hci_notifier, event, hdev);
94 /* ---- HCI requests ---- */
96 void hci_req_complete(struct hci_dev *hdev, int result)
98 BT_DBG("%s result 0x%2.2x", hdev->name, result);
100 if (hdev->req_status == HCI_REQ_PEND) {
101 hdev->req_result = result;
102 hdev->req_status = HCI_REQ_DONE;
103 wake_up_interruptible(&hdev->req_wait_q);
107 static void hci_req_cancel(struct hci_dev *hdev, int err)
109 BT_DBG("%s err 0x%2.2x", hdev->name, err);
111 if (hdev->req_status == HCI_REQ_PEND) {
112 hdev->req_result = err;
113 hdev->req_status = HCI_REQ_CANCELED;
114 wake_up_interruptible(&hdev->req_wait_q);
118 /* Execute request and wait for completion. */
119 static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
120 unsigned long opt, __u32 timeout)
122 DECLARE_WAITQUEUE(wait, current);
123 int err = 0;
125 BT_DBG("%s start", hdev->name);
127 hdev->req_status = HCI_REQ_PEND;
129 add_wait_queue(&hdev->req_wait_q, &wait);
130 set_current_state(TASK_INTERRUPTIBLE);
132 req(hdev, opt);
133 schedule_timeout(timeout);
135 remove_wait_queue(&hdev->req_wait_q, &wait);
137 if (signal_pending(current))
138 return -EINTR;
140 switch (hdev->req_status) {
141 case HCI_REQ_DONE:
142 err = -bt_err(hdev->req_result);
143 break;
145 case HCI_REQ_CANCELED:
146 err = -hdev->req_result;
147 break;
149 default:
150 err = -ETIMEDOUT;
151 break;
154 hdev->req_status = hdev->req_result = 0;
156 BT_DBG("%s end: err %d", hdev->name, err);
158 return err;
161 static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
162 unsigned long opt, __u32 timeout)
164 int ret;
166 /* Serialize all requests */
167 hci_req_lock(hdev);
168 ret = __hci_request(hdev, req, opt, timeout);
169 hci_req_unlock(hdev);
171 return ret;
174 static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
176 BT_DBG("%s %ld", hdev->name, opt);
178 /* Reset device */
179 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_RESET, 0, NULL);
182 static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
184 struct sk_buff *skb;
185 __le16 param;
187 BT_DBG("%s %ld", hdev->name, opt);
189 /* Driver initialization */
191 /* Special commands */
192 while ((skb = skb_dequeue(&hdev->driver_init))) {
193 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
194 skb->dev = (void *) hdev;
195 skb_queue_tail(&hdev->cmd_q, skb);
196 hci_sched_cmd(hdev);
198 skb_queue_purge(&hdev->driver_init);
200 /* Mandatory initialization */
202 /* Reset */
203 if (test_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks))
204 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_RESET, 0, NULL);
206 /* Read Local Supported Features */
207 hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_LOCAL_FEATURES, 0, NULL);
209 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
210 hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_BUFFER_SIZE, 0, NULL);
212 #if 0
213 /* Host buffer size */
215 struct hci_cp_host_buffer_size cp;
216 cp.acl_mtu = __cpu_to_le16(HCI_MAX_ACL_SIZE);
217 cp.sco_mtu = HCI_MAX_SCO_SIZE;
218 cp.acl_max_pkt = __cpu_to_le16(0xffff);
219 cp.sco_max_pkt = __cpu_to_le16(0xffff);
220 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_HOST_BUFFER_SIZE, sizeof(cp), &cp);
222 #endif
224 /* Read BD Address */
225 hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_BD_ADDR, 0, NULL);
227 /* Read Voice Setting */
228 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_READ_VOICE_SETTING, 0, NULL);
230 /* Optional initialization */
232 /* Clear Event Filters */
234 struct hci_cp_set_event_flt cp;
235 cp.flt_type = HCI_FLT_CLEAR_ALL;
236 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_SET_EVENT_FLT, sizeof(cp), &cp);
239 /* Page timeout ~20 secs */
240 param = __cpu_to_le16(0x8000);
241 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_PG_TIMEOUT, 2, &param);
243 /* Connection accept timeout ~20 secs */
244 param = __cpu_to_le16(0x7d00);
245 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_CA_TIMEOUT, 2, &param);
248 static void hci_scan_req(struct hci_dev *hdev, unsigned long opt)
250 __u8 scan = opt;
252 BT_DBG("%s %x", hdev->name, scan);
254 /* Inquiry and Page scans */
255 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE, 1, &scan);
258 static void hci_auth_req(struct hci_dev *hdev, unsigned long opt)
260 __u8 auth = opt;
262 BT_DBG("%s %x", hdev->name, auth);
264 /* Authentication */
265 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE, 1, &auth);
268 static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
270 __u8 encrypt = opt;
272 BT_DBG("%s %x", hdev->name, encrypt);
274 /* Authentication */
275 hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE, 1, &encrypt);
278 /* Get HCI device by index.
279 * Device is held on return. */
280 struct hci_dev *hci_dev_get(int index)
282 struct hci_dev *hdev = NULL;
283 struct list_head *p;
285 BT_DBG("%d", index);
287 if (index < 0)
288 return NULL;
290 read_lock(&hci_dev_list_lock);
291 list_for_each(p, &hci_dev_list) {
292 struct hci_dev *d = list_entry(p, struct hci_dev, list);
293 if (d->id == index) {
294 hdev = hci_dev_hold(d);
295 break;
298 read_unlock(&hci_dev_list_lock);
299 return hdev;
302 /* ---- Inquiry support ---- */
303 static void inquiry_cache_flush(struct hci_dev *hdev)
305 struct inquiry_cache *cache = &hdev->inq_cache;
306 struct inquiry_entry *next = cache->list, *e;
308 BT_DBG("cache %p", cache);
310 cache->list = NULL;
311 while ((e = next)) {
312 next = e->next;
313 kfree(e);
317 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
319 struct inquiry_cache *cache = &hdev->inq_cache;
320 struct inquiry_entry *e;
322 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
324 for (e = cache->list; e; e = e->next)
325 if (!bacmp(&e->data.bdaddr, bdaddr))
326 break;
327 return e;
330 void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data)
332 struct inquiry_cache *cache = &hdev->inq_cache;
333 struct inquiry_entry *e;
335 BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr));
337 if (!(e = hci_inquiry_cache_lookup(hdev, &data->bdaddr))) {
338 /* Entry not in the cache. Add new one. */
339 if (!(e = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC)))
340 return;
341 e->next = cache->list;
342 cache->list = e;
345 memcpy(&e->data, data, sizeof(*data));
346 e->timestamp = jiffies;
347 cache->timestamp = jiffies;
350 static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
352 struct inquiry_cache *cache = &hdev->inq_cache;
353 struct inquiry_info *info = (struct inquiry_info *) buf;
354 struct inquiry_entry *e;
355 int copied = 0;
357 for (e = cache->list; e && copied < num; e = e->next, copied++) {
358 struct inquiry_data *data = &e->data;
359 bacpy(&info->bdaddr, &data->bdaddr);
360 info->pscan_rep_mode = data->pscan_rep_mode;
361 info->pscan_period_mode = data->pscan_period_mode;
362 info->pscan_mode = data->pscan_mode;
363 memcpy(info->dev_class, data->dev_class, 3);
364 info->clock_offset = data->clock_offset;
365 info++;
368 BT_DBG("cache %p, copied %d", cache, copied);
369 return copied;
372 static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
374 struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
375 struct hci_cp_inquiry cp;
377 BT_DBG("%s", hdev->name);
379 if (test_bit(HCI_INQUIRY, &hdev->flags))
380 return;
382 /* Start Inquiry */
383 memcpy(&cp.lap, &ir->lap, 3);
384 cp.length = ir->length;
385 cp.num_rsp = ir->num_rsp;
386 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_INQUIRY, sizeof(cp), &cp);
389 int hci_inquiry(void __user *arg)
391 __u8 __user *ptr = arg;
392 struct hci_inquiry_req ir;
393 struct hci_dev *hdev;
394 int err = 0, do_inquiry = 0, max_rsp;
395 long timeo;
396 __u8 *buf;
398 if (copy_from_user(&ir, ptr, sizeof(ir)))
399 return -EFAULT;
401 if (!(hdev = hci_dev_get(ir.dev_id)))
402 return -ENODEV;
404 hci_dev_lock_bh(hdev);
405 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
406 inquiry_cache_empty(hdev) ||
407 ir.flags & IREQ_CACHE_FLUSH) {
408 inquiry_cache_flush(hdev);
409 do_inquiry = 1;
411 hci_dev_unlock_bh(hdev);
413 timeo = ir.length * msecs_to_jiffies(2000);
414 if (do_inquiry && (err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo)) < 0)
415 goto done;
417 /* for unlimited number of responses we will use buffer with 255 entries */
418 max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
420 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
421 * copy it to the user space.
423 if (!(buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL))) {
424 err = -ENOMEM;
425 goto done;
428 hci_dev_lock_bh(hdev);
429 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
430 hci_dev_unlock_bh(hdev);
432 BT_DBG("num_rsp %d", ir.num_rsp);
434 if (!copy_to_user(ptr, &ir, sizeof(ir))) {
435 ptr += sizeof(ir);
436 if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
437 ir.num_rsp))
438 err = -EFAULT;
439 } else
440 err = -EFAULT;
442 kfree(buf);
444 done:
445 hci_dev_put(hdev);
446 return err;
449 /* ---- HCI ioctl helpers ---- */
451 int hci_dev_open(__u16 dev)
453 struct hci_dev *hdev;
454 int ret = 0;
456 if (!(hdev = hci_dev_get(dev)))
457 return -ENODEV;
459 BT_DBG("%s %p", hdev->name, hdev);
461 hci_req_lock(hdev);
463 if (test_bit(HCI_UP, &hdev->flags)) {
464 ret = -EALREADY;
465 goto done;
468 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
469 set_bit(HCI_RAW, &hdev->flags);
471 if (hdev->open(hdev)) {
472 ret = -EIO;
473 goto done;
476 if (!test_bit(HCI_RAW, &hdev->flags)) {
477 atomic_set(&hdev->cmd_cnt, 1);
478 set_bit(HCI_INIT, &hdev->flags);
480 //__hci_request(hdev, hci_reset_req, 0, HZ);
481 ret = __hci_request(hdev, hci_init_req, 0,
482 msecs_to_jiffies(HCI_INIT_TIMEOUT));
484 clear_bit(HCI_INIT, &hdev->flags);
487 if (!ret) {
488 hci_dev_hold(hdev);
489 set_bit(HCI_UP, &hdev->flags);
490 hci_notify(hdev, HCI_DEV_UP);
491 } else {
492 /* Init failed, cleanup */
493 tasklet_kill(&hdev->rx_task);
494 tasklet_kill(&hdev->tx_task);
495 tasklet_kill(&hdev->cmd_task);
497 skb_queue_purge(&hdev->cmd_q);
498 skb_queue_purge(&hdev->rx_q);
500 if (hdev->flush)
501 hdev->flush(hdev);
503 if (hdev->sent_cmd) {
504 kfree_skb(hdev->sent_cmd);
505 hdev->sent_cmd = NULL;
508 hdev->close(hdev);
509 hdev->flags = 0;
512 done:
513 hci_req_unlock(hdev);
514 hci_dev_put(hdev);
515 return ret;
518 static int hci_dev_do_close(struct hci_dev *hdev)
520 BT_DBG("%s %p", hdev->name, hdev);
522 hci_req_cancel(hdev, ENODEV);
523 hci_req_lock(hdev);
525 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
526 hci_req_unlock(hdev);
527 return 0;
530 /* Kill RX and TX tasks */
531 tasklet_kill(&hdev->rx_task);
532 tasklet_kill(&hdev->tx_task);
534 hci_dev_lock_bh(hdev);
535 inquiry_cache_flush(hdev);
536 hci_conn_hash_flush(hdev);
537 hci_dev_unlock_bh(hdev);
539 hci_notify(hdev, HCI_DEV_DOWN);
541 if (hdev->flush)
542 hdev->flush(hdev);
544 /* Reset device */
545 skb_queue_purge(&hdev->cmd_q);
546 atomic_set(&hdev->cmd_cnt, 1);
547 if (!test_bit(HCI_RAW, &hdev->flags)) {
548 set_bit(HCI_INIT, &hdev->flags);
549 __hci_request(hdev, hci_reset_req, 0,
550 msecs_to_jiffies(250));
551 clear_bit(HCI_INIT, &hdev->flags);
554 /* Kill cmd task */
555 tasklet_kill(&hdev->cmd_task);
557 /* Drop queues */
558 skb_queue_purge(&hdev->rx_q);
559 skb_queue_purge(&hdev->cmd_q);
560 skb_queue_purge(&hdev->raw_q);
562 /* Drop last sent command */
563 if (hdev->sent_cmd) {
564 kfree_skb(hdev->sent_cmd);
565 hdev->sent_cmd = NULL;
568 /* After this point our queues are empty
569 * and no tasks are scheduled. */
570 hdev->close(hdev);
572 /* Clear flags */
573 hdev->flags = 0;
575 hci_req_unlock(hdev);
577 hci_dev_put(hdev);
578 return 0;
581 int hci_dev_close(__u16 dev)
583 struct hci_dev *hdev;
584 int err;
586 if (!(hdev = hci_dev_get(dev)))
587 return -ENODEV;
588 err = hci_dev_do_close(hdev);
589 hci_dev_put(hdev);
590 return err;
593 int hci_dev_reset(__u16 dev)
595 struct hci_dev *hdev;
596 int ret = 0;
598 if (!(hdev = hci_dev_get(dev)))
599 return -ENODEV;
601 hci_req_lock(hdev);
602 tasklet_disable(&hdev->tx_task);
604 if (!test_bit(HCI_UP, &hdev->flags))
605 goto done;
607 /* Drop queues */
608 skb_queue_purge(&hdev->rx_q);
609 skb_queue_purge(&hdev->cmd_q);
611 hci_dev_lock_bh(hdev);
612 inquiry_cache_flush(hdev);
613 hci_conn_hash_flush(hdev);
614 hci_dev_unlock_bh(hdev);
616 if (hdev->flush)
617 hdev->flush(hdev);
619 atomic_set(&hdev->cmd_cnt, 1);
620 hdev->acl_cnt = 0; hdev->sco_cnt = 0;
622 if (!test_bit(HCI_RAW, &hdev->flags))
623 ret = __hci_request(hdev, hci_reset_req, 0,
624 msecs_to_jiffies(HCI_INIT_TIMEOUT));
626 done:
627 tasklet_enable(&hdev->tx_task);
628 hci_req_unlock(hdev);
629 hci_dev_put(hdev);
630 return ret;
633 int hci_dev_reset_stat(__u16 dev)
635 struct hci_dev *hdev;
636 int ret = 0;
638 if (!(hdev = hci_dev_get(dev)))
639 return -ENODEV;
641 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
643 hci_dev_put(hdev);
645 return ret;
648 int hci_dev_cmd(unsigned int cmd, void __user *arg)
650 struct hci_dev *hdev;
651 struct hci_dev_req dr;
652 int err = 0;
654 if (copy_from_user(&dr, arg, sizeof(dr)))
655 return -EFAULT;
657 if (!(hdev = hci_dev_get(dr.dev_id)))
658 return -ENODEV;
660 switch (cmd) {
661 case HCISETAUTH:
662 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
663 msecs_to_jiffies(HCI_INIT_TIMEOUT));
664 break;
666 case HCISETENCRYPT:
667 if (!lmp_encrypt_capable(hdev)) {
668 err = -EOPNOTSUPP;
669 break;
672 if (!test_bit(HCI_AUTH, &hdev->flags)) {
673 /* Auth must be enabled first */
674 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
675 msecs_to_jiffies(HCI_INIT_TIMEOUT));
676 if (err)
677 break;
680 err = hci_request(hdev, hci_encrypt_req, dr.dev_opt,
681 msecs_to_jiffies(HCI_INIT_TIMEOUT));
682 break;
684 case HCISETSCAN:
685 err = hci_request(hdev, hci_scan_req, dr.dev_opt,
686 msecs_to_jiffies(HCI_INIT_TIMEOUT));
687 break;
689 case HCISETPTYPE:
690 hdev->pkt_type = (__u16) dr.dev_opt;
691 break;
693 case HCISETLINKPOL:
694 hdev->link_policy = (__u16) dr.dev_opt;
695 break;
697 case HCISETLINKMODE:
698 hdev->link_mode = ((__u16) dr.dev_opt) & (HCI_LM_MASTER | HCI_LM_ACCEPT);
699 break;
701 case HCISETACLMTU:
702 hdev->acl_mtu = *((__u16 *)&dr.dev_opt + 1);
703 hdev->acl_pkts = *((__u16 *)&dr.dev_opt + 0);
704 break;
706 case HCISETSCOMTU:
707 hdev->sco_mtu = *((__u16 *)&dr.dev_opt + 1);
708 hdev->sco_pkts = *((__u16 *)&dr.dev_opt + 0);
709 break;
711 default:
712 err = -EINVAL;
713 break;
715 hci_dev_put(hdev);
716 return err;
719 int hci_get_dev_list(void __user *arg)
721 struct hci_dev_list_req *dl;
722 struct hci_dev_req *dr;
723 struct list_head *p;
724 int n = 0, size, err;
725 __u16 dev_num;
727 if (get_user(dev_num, (__u16 __user *) arg))
728 return -EFAULT;
730 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
731 return -EINVAL;
733 size = sizeof(*dl) + dev_num * sizeof(*dr);
735 if (!(dl = kmalloc(size, GFP_KERNEL)))
736 return -ENOMEM;
738 dr = dl->dev_req;
740 read_lock_bh(&hci_dev_list_lock);
741 list_for_each(p, &hci_dev_list) {
742 struct hci_dev *hdev;
743 hdev = list_entry(p, struct hci_dev, list);
744 (dr + n)->dev_id = hdev->id;
745 (dr + n)->dev_opt = hdev->flags;
746 if (++n >= dev_num)
747 break;
749 read_unlock_bh(&hci_dev_list_lock);
751 dl->dev_num = n;
752 size = sizeof(*dl) + n * sizeof(*dr);
754 err = copy_to_user(arg, dl, size);
755 kfree(dl);
757 return err ? -EFAULT : 0;
760 int hci_get_dev_info(void __user *arg)
762 struct hci_dev *hdev;
763 struct hci_dev_info di;
764 int err = 0;
766 if (copy_from_user(&di, arg, sizeof(di)))
767 return -EFAULT;
769 if (!(hdev = hci_dev_get(di.dev_id)))
770 return -ENODEV;
772 strcpy(di.name, hdev->name);
773 di.bdaddr = hdev->bdaddr;
774 di.type = hdev->type;
775 di.flags = hdev->flags;
776 di.pkt_type = hdev->pkt_type;
777 di.acl_mtu = hdev->acl_mtu;
778 di.acl_pkts = hdev->acl_pkts;
779 di.sco_mtu = hdev->sco_mtu;
780 di.sco_pkts = hdev->sco_pkts;
781 di.link_policy = hdev->link_policy;
782 di.link_mode = hdev->link_mode;
784 memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
785 memcpy(&di.features, &hdev->features, sizeof(di.features));
787 if (copy_to_user(arg, &di, sizeof(di)))
788 err = -EFAULT;
790 hci_dev_put(hdev);
792 return err;
795 /* ---- Interface to HCI drivers ---- */
797 /* Alloc HCI device */
798 struct hci_dev *hci_alloc_dev(void)
800 struct hci_dev *hdev;
802 hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
803 if (!hdev)
804 return NULL;
806 skb_queue_head_init(&hdev->driver_init);
808 return hdev;
810 EXPORT_SYMBOL(hci_alloc_dev);
812 /* Free HCI device */
813 void hci_free_dev(struct hci_dev *hdev)
815 skb_queue_purge(&hdev->driver_init);
817 /* will free via device release */
818 put_device(&hdev->dev);
820 EXPORT_SYMBOL(hci_free_dev);
822 /* Register HCI device */
823 int hci_register_dev(struct hci_dev *hdev)
825 struct list_head *head = &hci_dev_list, *p;
826 int id = 0;
828 BT_DBG("%p name %s type %d owner %p", hdev, hdev->name, hdev->type, hdev->owner);
830 if (!hdev->open || !hdev->close || !hdev->destruct)
831 return -EINVAL;
833 write_lock_bh(&hci_dev_list_lock);
835 /* Find first available device id */
836 list_for_each(p, &hci_dev_list) {
837 if (list_entry(p, struct hci_dev, list)->id != id)
838 break;
839 head = p; id++;
842 sprintf(hdev->name, "hci%d", id);
843 hdev->id = id;
844 list_add(&hdev->list, head);
846 atomic_set(&hdev->refcnt, 1);
847 spin_lock_init(&hdev->lock);
849 hdev->flags = 0;
850 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
851 hdev->link_mode = (HCI_LM_ACCEPT);
853 hdev->idle_timeout = 0;
854 hdev->sniff_max_interval = 800;
855 hdev->sniff_min_interval = 80;
857 tasklet_init(&hdev->cmd_task, hci_cmd_task,(unsigned long) hdev);
858 tasklet_init(&hdev->rx_task, hci_rx_task, (unsigned long) hdev);
859 tasklet_init(&hdev->tx_task, hci_tx_task, (unsigned long) hdev);
861 skb_queue_head_init(&hdev->rx_q);
862 skb_queue_head_init(&hdev->cmd_q);
863 skb_queue_head_init(&hdev->raw_q);
865 init_waitqueue_head(&hdev->req_wait_q);
866 init_MUTEX(&hdev->req_lock);
868 inquiry_cache_init(hdev);
870 hci_conn_hash_init(hdev);
872 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
874 atomic_set(&hdev->promisc, 0);
876 write_unlock_bh(&hci_dev_list_lock);
878 hci_register_sysfs(hdev);
880 hci_notify(hdev, HCI_DEV_REG);
882 return id;
884 EXPORT_SYMBOL(hci_register_dev);
886 /* Unregister HCI device */
887 int hci_unregister_dev(struct hci_dev *hdev)
889 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
891 hci_unregister_sysfs(hdev);
893 write_lock_bh(&hci_dev_list_lock);
894 list_del(&hdev->list);
895 write_unlock_bh(&hci_dev_list_lock);
897 hci_dev_do_close(hdev);
899 hci_notify(hdev, HCI_DEV_UNREG);
901 __hci_dev_put(hdev);
902 return 0;
904 EXPORT_SYMBOL(hci_unregister_dev);
906 /* Suspend HCI device */
907 int hci_suspend_dev(struct hci_dev *hdev)
909 hci_notify(hdev, HCI_DEV_SUSPEND);
910 return 0;
912 EXPORT_SYMBOL(hci_suspend_dev);
914 /* Resume HCI device */
915 int hci_resume_dev(struct hci_dev *hdev)
917 hci_notify(hdev, HCI_DEV_RESUME);
918 return 0;
920 EXPORT_SYMBOL(hci_resume_dev);
922 /* ---- Interface to upper protocols ---- */
924 /* Register/Unregister protocols.
925 * hci_task_lock is used to ensure that no tasks are running. */
926 int hci_register_proto(struct hci_proto *hp)
928 int err = 0;
930 BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
932 if (hp->id >= HCI_MAX_PROTO)
933 return -EINVAL;
935 write_lock_bh(&hci_task_lock);
937 if (!hci_proto[hp->id])
938 hci_proto[hp->id] = hp;
939 else
940 err = -EEXIST;
942 write_unlock_bh(&hci_task_lock);
944 return err;
946 EXPORT_SYMBOL(hci_register_proto);
948 int hci_unregister_proto(struct hci_proto *hp)
950 int err = 0;
952 BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
954 if (hp->id >= HCI_MAX_PROTO)
955 return -EINVAL;
957 write_lock_bh(&hci_task_lock);
959 if (hci_proto[hp->id])
960 hci_proto[hp->id] = NULL;
961 else
962 err = -ENOENT;
964 write_unlock_bh(&hci_task_lock);
966 return err;
968 EXPORT_SYMBOL(hci_unregister_proto);
970 int hci_register_cb(struct hci_cb *cb)
972 BT_DBG("%p name %s", cb, cb->name);
974 write_lock_bh(&hci_cb_list_lock);
975 list_add(&cb->list, &hci_cb_list);
976 write_unlock_bh(&hci_cb_list_lock);
978 return 0;
980 EXPORT_SYMBOL(hci_register_cb);
982 int hci_unregister_cb(struct hci_cb *cb)
984 BT_DBG("%p name %s", cb, cb->name);
986 write_lock_bh(&hci_cb_list_lock);
987 list_del(&cb->list);
988 write_unlock_bh(&hci_cb_list_lock);
990 return 0;
992 EXPORT_SYMBOL(hci_unregister_cb);
994 static int hci_send_frame(struct sk_buff *skb)
996 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
998 if (!hdev) {
999 kfree_skb(skb);
1000 return -ENODEV;
1003 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
1005 if (atomic_read(&hdev->promisc)) {
1006 /* Time stamp */
1007 __net_timestamp(skb);
1009 hci_send_to_sock(hdev, skb);
1012 /* Get rid of skb owner, prior to sending to the driver. */
1013 skb_orphan(skb);
1015 return hdev->send(skb);
1018 /* Send HCI command */
1019 int hci_send_cmd(struct hci_dev *hdev, __u16 ogf, __u16 ocf, __u32 plen, void *param)
1021 int len = HCI_COMMAND_HDR_SIZE + plen;
1022 struct hci_command_hdr *hdr;
1023 struct sk_buff *skb;
1025 BT_DBG("%s ogf 0x%x ocf 0x%x plen %d", hdev->name, ogf, ocf, plen);
1027 skb = bt_skb_alloc(len, GFP_ATOMIC);
1028 if (!skb) {
1029 BT_ERR("%s Can't allocate memory for HCI command", hdev->name);
1030 return -ENOMEM;
1033 hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
1034 hdr->opcode = __cpu_to_le16(hci_opcode_pack(ogf, ocf));
1035 hdr->plen = plen;
1037 if (plen)
1038 memcpy(skb_put(skb, plen), param, plen);
1040 BT_DBG("skb len %d", skb->len);
1042 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
1043 skb->dev = (void *) hdev;
1044 skb_queue_tail(&hdev->cmd_q, skb);
1045 hci_sched_cmd(hdev);
1047 return 0;
1050 /* Get data from the previously sent command */
1051 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 ogf, __u16 ocf)
1053 struct hci_command_hdr *hdr;
1055 if (!hdev->sent_cmd)
1056 return NULL;
1058 hdr = (void *) hdev->sent_cmd->data;
1060 if (hdr->opcode != __cpu_to_le16(hci_opcode_pack(ogf, ocf)))
1061 return NULL;
1063 BT_DBG("%s ogf 0x%x ocf 0x%x", hdev->name, ogf, ocf);
1065 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
1068 /* Send ACL data */
1069 static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
1071 struct hci_acl_hdr *hdr;
1072 int len = skb->len;
1074 hdr = (struct hci_acl_hdr *) skb_push(skb, HCI_ACL_HDR_SIZE);
1075 hdr->handle = __cpu_to_le16(hci_handle_pack(handle, flags));
1076 hdr->dlen = __cpu_to_le16(len);
1078 skb->h.raw = (void *) hdr;
1081 int hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags)
1083 struct hci_dev *hdev = conn->hdev;
1084 struct sk_buff *list;
1086 BT_DBG("%s conn %p flags 0x%x", hdev->name, conn, flags);
1088 skb->dev = (void *) hdev;
1089 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
1090 hci_add_acl_hdr(skb, conn->handle, flags | ACL_START);
1092 if (!(list = skb_shinfo(skb)->frag_list)) {
1093 /* Non fragmented */
1094 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
1096 skb_queue_tail(&conn->data_q, skb);
1097 } else {
1098 /* Fragmented */
1099 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
1101 skb_shinfo(skb)->frag_list = NULL;
1103 /* Queue all fragments atomically */
1104 spin_lock_bh(&conn->data_q.lock);
1106 __skb_queue_tail(&conn->data_q, skb);
1107 do {
1108 skb = list; list = list->next;
1110 skb->dev = (void *) hdev;
1111 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
1112 hci_add_acl_hdr(skb, conn->handle, flags | ACL_CONT);
1114 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
1116 __skb_queue_tail(&conn->data_q, skb);
1117 } while (list);
1119 spin_unlock_bh(&conn->data_q.lock);
1122 hci_sched_tx(hdev);
1123 return 0;
1125 EXPORT_SYMBOL(hci_send_acl);
1127 /* Send SCO data */
1128 int hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
1130 struct hci_dev *hdev = conn->hdev;
1131 struct hci_sco_hdr hdr;
1133 BT_DBG("%s len %d", hdev->name, skb->len);
1135 if (skb->len > hdev->sco_mtu) {
1136 kfree_skb(skb);
1137 return -EINVAL;
1140 hdr.handle = __cpu_to_le16(conn->handle);
1141 hdr.dlen = skb->len;
1143 skb->h.raw = skb_push(skb, HCI_SCO_HDR_SIZE);
1144 memcpy(skb->h.raw, &hdr, HCI_SCO_HDR_SIZE);
1146 skb->dev = (void *) hdev;
1147 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
1148 skb_queue_tail(&conn->data_q, skb);
1149 hci_sched_tx(hdev);
1150 return 0;
1152 EXPORT_SYMBOL(hci_send_sco);
1154 /* ---- HCI TX task (outgoing data) ---- */
1156 /* HCI Connection scheduler */
1157 static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote)
1159 struct hci_conn_hash *h = &hdev->conn_hash;
1160 struct hci_conn *conn = NULL;
1161 int num = 0, min = ~0;
1162 struct list_head *p;
1164 /* We don't have to lock device here. Connections are always
1165 * added and removed with TX task disabled. */
1166 list_for_each(p, &h->list) {
1167 struct hci_conn *c;
1168 c = list_entry(p, struct hci_conn, list);
1170 if (c->type != type || c->state != BT_CONNECTED
1171 || skb_queue_empty(&c->data_q))
1172 continue;
1173 num++;
1175 if (c->sent < min) {
1176 min = c->sent;
1177 conn = c;
1181 if (conn) {
1182 int cnt = (type == ACL_LINK ? hdev->acl_cnt : hdev->sco_cnt);
1183 int q = cnt / num;
1184 *quote = q ? q : 1;
1185 } else
1186 *quote = 0;
1188 BT_DBG("conn %p quote %d", conn, *quote);
1189 return conn;
1192 static inline void hci_acl_tx_to(struct hci_dev *hdev)
1194 struct hci_conn_hash *h = &hdev->conn_hash;
1195 struct list_head *p;
1196 struct hci_conn *c;
1198 BT_ERR("%s ACL tx timeout", hdev->name);
1200 /* Kill stalled connections */
1201 list_for_each(p, &h->list) {
1202 c = list_entry(p, struct hci_conn, list);
1203 if (c->type == ACL_LINK && c->sent) {
1204 BT_ERR("%s killing stalled ACL connection %s",
1205 hdev->name, batostr(&c->dst));
1206 hci_acl_disconn(c, 0x13);
1211 static inline void hci_sched_acl(struct hci_dev *hdev)
1213 struct hci_conn *conn;
1214 struct sk_buff *skb;
1215 int quote;
1217 BT_DBG("%s", hdev->name);
1219 if (!test_bit(HCI_RAW, &hdev->flags)) {
1220 /* ACL tx timeout must be longer than maximum
1221 * link supervision timeout (40.9 seconds) */
1222 if (!hdev->acl_cnt && (jiffies - hdev->acl_last_tx) > (HZ * 45))
1223 hci_acl_tx_to(hdev);
1226 while (hdev->acl_cnt && (conn = hci_low_sent(hdev, ACL_LINK, &quote))) {
1227 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
1228 BT_DBG("skb %p len %d", skb, skb->len);
1230 hci_conn_enter_active_mode(conn);
1232 hci_send_frame(skb);
1233 hdev->acl_last_tx = jiffies;
1235 hdev->acl_cnt--;
1236 conn->sent++;
1241 /* Schedule SCO */
1242 static inline void hci_sched_sco(struct hci_dev *hdev)
1244 struct hci_conn *conn;
1245 struct sk_buff *skb;
1246 int quote;
1248 BT_DBG("%s", hdev->name);
1250 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
1251 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
1252 BT_DBG("skb %p len %d", skb, skb->len);
1253 hci_send_frame(skb);
1255 conn->sent++;
1256 if (conn->sent == ~0)
1257 conn->sent = 0;
1262 static void hci_tx_task(unsigned long arg)
1264 struct hci_dev *hdev = (struct hci_dev *) arg;
1265 struct sk_buff *skb;
1267 read_lock(&hci_task_lock);
1269 BT_DBG("%s acl %d sco %d", hdev->name, hdev->acl_cnt, hdev->sco_cnt);
1271 /* Schedule queues and send stuff to HCI driver */
1273 hci_sched_acl(hdev);
1275 hci_sched_sco(hdev);
1277 /* Send next queued raw (unknown type) packet */
1278 while ((skb = skb_dequeue(&hdev->raw_q)))
1279 hci_send_frame(skb);
1281 read_unlock(&hci_task_lock);
1284 /* ----- HCI RX task (incoming data proccessing) ----- */
1286 /* ACL data packet */
1287 static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
1289 struct hci_acl_hdr *hdr = (void *) skb->data;
1290 struct hci_conn *conn;
1291 __u16 handle, flags;
1293 skb_pull(skb, HCI_ACL_HDR_SIZE);
1295 handle = __le16_to_cpu(hdr->handle);
1296 flags = hci_flags(handle);
1297 handle = hci_handle(handle);
1299 BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags);
1301 hdev->stat.acl_rx++;
1303 hci_dev_lock(hdev);
1304 conn = hci_conn_hash_lookup_handle(hdev, handle);
1305 hci_dev_unlock(hdev);
1307 if (conn) {
1308 register struct hci_proto *hp;
1310 hci_conn_enter_active_mode(conn);
1312 /* Send to upper protocol */
1313 if ((hp = hci_proto[HCI_PROTO_L2CAP]) && hp->recv_acldata) {
1314 hp->recv_acldata(conn, skb, flags);
1315 return;
1317 } else {
1318 BT_ERR("%s ACL packet for unknown connection handle %d",
1319 hdev->name, handle);
1322 kfree_skb(skb);
1325 /* SCO data packet */
1326 static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
1328 struct hci_sco_hdr *hdr = (void *) skb->data;
1329 struct hci_conn *conn;
1330 __u16 handle;
1332 skb_pull(skb, HCI_SCO_HDR_SIZE);
1334 handle = __le16_to_cpu(hdr->handle);
1336 BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle);
1338 hdev->stat.sco_rx++;
1340 hci_dev_lock(hdev);
1341 conn = hci_conn_hash_lookup_handle(hdev, handle);
1342 hci_dev_unlock(hdev);
1344 if (conn) {
1345 register struct hci_proto *hp;
1347 /* Send to upper protocol */
1348 if ((hp = hci_proto[HCI_PROTO_SCO]) && hp->recv_scodata) {
1349 hp->recv_scodata(conn, skb);
1350 return;
1352 } else {
1353 BT_ERR("%s SCO packet for unknown connection handle %d",
1354 hdev->name, handle);
1357 kfree_skb(skb);
1360 static void hci_rx_task(unsigned long arg)
1362 struct hci_dev *hdev = (struct hci_dev *) arg;
1363 struct sk_buff *skb;
1365 BT_DBG("%s", hdev->name);
1367 read_lock(&hci_task_lock);
1369 while ((skb = skb_dequeue(&hdev->rx_q))) {
1370 if (atomic_read(&hdev->promisc)) {
1371 /* Send copy to the sockets */
1372 hci_send_to_sock(hdev, skb);
1375 if (test_bit(HCI_RAW, &hdev->flags)) {
1376 kfree_skb(skb);
1377 continue;
1380 if (test_bit(HCI_INIT, &hdev->flags)) {
1381 /* Don't process data packets in this states. */
1382 switch (bt_cb(skb)->pkt_type) {
1383 case HCI_ACLDATA_PKT:
1384 case HCI_SCODATA_PKT:
1385 kfree_skb(skb);
1386 continue;
1390 /* Process frame */
1391 switch (bt_cb(skb)->pkt_type) {
1392 case HCI_EVENT_PKT:
1393 hci_event_packet(hdev, skb);
1394 break;
1396 case HCI_ACLDATA_PKT:
1397 BT_DBG("%s ACL data packet", hdev->name);
1398 hci_acldata_packet(hdev, skb);
1399 break;
1401 case HCI_SCODATA_PKT:
1402 BT_DBG("%s SCO data packet", hdev->name);
1403 hci_scodata_packet(hdev, skb);
1404 break;
1406 default:
1407 kfree_skb(skb);
1408 break;
1412 read_unlock(&hci_task_lock);
1415 static void hci_cmd_task(unsigned long arg)
1417 struct hci_dev *hdev = (struct hci_dev *) arg;
1418 struct sk_buff *skb;
1420 BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
1422 if (!atomic_read(&hdev->cmd_cnt) && (jiffies - hdev->cmd_last_tx) > HZ) {
1423 BT_ERR("%s command tx timeout", hdev->name);
1424 atomic_set(&hdev->cmd_cnt, 1);
1427 /* Send queued commands */
1428 if (atomic_read(&hdev->cmd_cnt) && (skb = skb_dequeue(&hdev->cmd_q))) {
1429 if (hdev->sent_cmd)
1430 kfree_skb(hdev->sent_cmd);
1432 if ((hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC))) {
1433 atomic_dec(&hdev->cmd_cnt);
1434 hci_send_frame(skb);
1435 hdev->cmd_last_tx = jiffies;
1436 } else {
1437 skb_queue_head(&hdev->cmd_q, skb);
1438 hci_sched_cmd(hdev);