blob: b372fb8bcdcfdbbcbb7c190ce9b12baff125c12d [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
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;
10
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
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI core. */
26
S.Çağlar Onur824530212008-02-17 23:25:57 -080027#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/kmod.h>
30
31#include <linux/types.h>
32#include <linux/errno.h>
33#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sched.h>
35#include <linux/slab.h>
36#include <linux/poll.h>
37#include <linux/fcntl.h>
38#include <linux/init.h>
39#include <linux/skbuff.h>
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +010040#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/interrupt.h>
42#include <linux/notifier.h>
Marcel Holtmann611b30f2009-06-08 14:41:38 +020043#include <linux/rfkill.h>
Ville Tervo6bd32322011-02-16 16:32:41 +020044#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/sock.h>
46
47#include <asm/system.h>
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020048#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/unaligned.h>
50
51#include <net/bluetooth/bluetooth.h>
52#include <net/bluetooth/hci_core.h>
53
Johan Hedbergab81cbf2010-12-15 13:53:18 +020054#define AUTO_OFF_TIMEOUT 2000
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static void hci_cmd_task(unsigned long arg);
57static void hci_rx_task(unsigned long arg);
58static void hci_tx_task(unsigned long arg);
59static void hci_notify(struct hci_dev *hdev, int event);
60
61static DEFINE_RWLOCK(hci_task_lock);
62
63/* HCI device list */
64LIST_HEAD(hci_dev_list);
65DEFINE_RWLOCK(hci_dev_list_lock);
66
67/* HCI callback list */
68LIST_HEAD(hci_cb_list);
69DEFINE_RWLOCK(hci_cb_list_lock);
70
71/* HCI protocols */
72#define HCI_MAX_PROTO 2
73struct hci_proto *hci_proto[HCI_MAX_PROTO];
74
75/* HCI notifiers list */
Alan Sterne041c682006-03-27 01:16:30 -080076static ATOMIC_NOTIFIER_HEAD(hci_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/* ---- HCI notifications ---- */
79
80int hci_register_notifier(struct notifier_block *nb)
81{
Alan Sterne041c682006-03-27 01:16:30 -080082 return atomic_notifier_chain_register(&hci_notifier, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85int hci_unregister_notifier(struct notifier_block *nb)
86{
Alan Sterne041c682006-03-27 01:16:30 -080087 return atomic_notifier_chain_unregister(&hci_notifier, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Marcel Holtmann65164552005-10-28 19:20:48 +020090static void hci_notify(struct hci_dev *hdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Alan Sterne041c682006-03-27 01:16:30 -080092 atomic_notifier_call_chain(&hci_notifier, event, hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95/* ---- HCI requests ---- */
96
Johan Hedberg23bb5762010-12-21 23:01:27 +020097void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Johan Hedberg23bb5762010-12-21 23:01:27 +020099 BT_DBG("%s command 0x%04x result 0x%2.2x", hdev->name, cmd, result);
100
Johan Hedberga5040ef2011-01-10 13:28:59 +0200101 /* If this is the init phase check if the completed command matches
102 * the last init command, and if not just return.
103 */
104 if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd)
Johan Hedberg23bb5762010-12-21 23:01:27 +0200105 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (hdev->req_status == HCI_REQ_PEND) {
108 hdev->req_result = result;
109 hdev->req_status = HCI_REQ_DONE;
110 wake_up_interruptible(&hdev->req_wait_q);
111 }
112}
113
114static void hci_req_cancel(struct hci_dev *hdev, int err)
115{
116 BT_DBG("%s err 0x%2.2x", hdev->name, err);
117
118 if (hdev->req_status == HCI_REQ_PEND) {
119 hdev->req_result = err;
120 hdev->req_status = HCI_REQ_CANCELED;
121 wake_up_interruptible(&hdev->req_wait_q);
122 }
123}
124
125/* Execute request and wait for completion. */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900126static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
Szymon Janc01df8c32011-02-17 16:46:47 +0100127 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 DECLARE_WAITQUEUE(wait, current);
130 int err = 0;
131
132 BT_DBG("%s start", hdev->name);
133
134 hdev->req_status = HCI_REQ_PEND;
135
136 add_wait_queue(&hdev->req_wait_q, &wait);
137 set_current_state(TASK_INTERRUPTIBLE);
138
139 req(hdev, opt);
140 schedule_timeout(timeout);
141
142 remove_wait_queue(&hdev->req_wait_q, &wait);
143
144 if (signal_pending(current))
145 return -EINTR;
146
147 switch (hdev->req_status) {
148 case HCI_REQ_DONE:
149 err = -bt_err(hdev->req_result);
150 break;
151
152 case HCI_REQ_CANCELED:
153 err = -hdev->req_result;
154 break;
155
156 default:
157 err = -ETIMEDOUT;
158 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Johan Hedberga5040ef2011-01-10 13:28:59 +0200161 hdev->req_status = hdev->req_result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 BT_DBG("%s end: err %d", hdev->name, err);
164
165 return err;
166}
167
168static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
Szymon Janc01df8c32011-02-17 16:46:47 +0100169 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 int ret;
172
Marcel Holtmann7c6a3292008-09-12 03:11:54 +0200173 if (!test_bit(HCI_UP, &hdev->flags))
174 return -ENETDOWN;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* Serialize all requests */
177 hci_req_lock(hdev);
178 ret = __hci_request(hdev, req, opt, timeout);
179 hci_req_unlock(hdev);
180
181 return ret;
182}
183
184static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
185{
186 BT_DBG("%s %ld", hdev->name, opt);
187
188 /* Reset device */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200189 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
193{
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200194 struct hci_cp_delete_stored_link_key cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 struct sk_buff *skb;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -0800196 __le16 param;
Marcel Holtmann89f27832007-09-09 08:39:49 +0200197 __u8 flt_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 BT_DBG("%s %ld", hdev->name, opt);
200
201 /* Driver initialization */
202
203 /* Special commands */
204 while ((skb = skb_dequeue(&hdev->driver_init))) {
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700205 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 skb->dev = (void *) hdev;
Marcel Holtmannc78ae282009-11-18 01:02:54 +0100207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 skb_queue_tail(&hdev->cmd_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +0100209 tasklet_schedule(&hdev->cmd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 skb_queue_purge(&hdev->driver_init);
212
213 /* Mandatory initialization */
214
215 /* Reset */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100216 if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200217 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* Read Local Supported Features */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200220 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200222 /* Read Local Version */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200223 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200226 hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228#if 0
229 /* Host buffer size */
230 {
231 struct hci_cp_host_buffer_size cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700232 cp.acl_mtu = cpu_to_le16(HCI_MAX_ACL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 cp.sco_mtu = HCI_MAX_SCO_SIZE;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700234 cp.acl_max_pkt = cpu_to_le16(0xffff);
235 cp.sco_max_pkt = cpu_to_le16(0xffff);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200236 hci_send_cmd(hdev, HCI_OP_HOST_BUFFER_SIZE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238#endif
239
240 /* Read BD Address */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200241 hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL);
242
243 /* Read Class of Device */
244 hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
245
246 /* Read Local Name */
247 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* Read Voice Setting */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200250 hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Optional initialization */
253
254 /* Clear Event Filters */
Marcel Holtmann89f27832007-09-09 08:39:49 +0200255 flt_type = HCI_FLT_CLEAR_ALL;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200256 hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* Connection accept timeout ~20 secs */
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700259 param = cpu_to_le16(0x7d00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200260 hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200261
262 bacpy(&cp.bdaddr, BDADDR_ANY);
263 cp.delete_all = 1;
264 hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300267static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt)
268{
269 BT_DBG("%s", hdev->name);
270
271 /* Read LE buffer size */
272 hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static void hci_scan_req(struct hci_dev *hdev, unsigned long opt)
276{
277 __u8 scan = opt;
278
279 BT_DBG("%s %x", hdev->name, scan);
280
281 /* Inquiry and Page scans */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200282 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285static void hci_auth_req(struct hci_dev *hdev, unsigned long opt)
286{
287 __u8 auth = opt;
288
289 BT_DBG("%s %x", hdev->name, auth);
290
291 /* Authentication */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200292 hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
296{
297 __u8 encrypt = opt;
298
299 BT_DBG("%s %x", hdev->name, encrypt);
300
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200301 /* Encryption */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200302 hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200305static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt)
306{
307 __le16 policy = cpu_to_le16(opt);
308
Marcel Holtmanna418b892008-11-30 12:17:28 +0100309 BT_DBG("%s %x", hdev->name, policy);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200310
311 /* Default link policy */
312 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
313}
314
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900315/* Get HCI device by index.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * Device is held on return. */
317struct hci_dev *hci_dev_get(int index)
318{
319 struct hci_dev *hdev = NULL;
320 struct list_head *p;
321
322 BT_DBG("%d", index);
323
324 if (index < 0)
325 return NULL;
326
327 read_lock(&hci_dev_list_lock);
328 list_for_each(p, &hci_dev_list) {
329 struct hci_dev *d = list_entry(p, struct hci_dev, list);
330 if (d->id == index) {
331 hdev = hci_dev_hold(d);
332 break;
333 }
334 }
335 read_unlock(&hci_dev_list_lock);
336 return hdev;
337}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/* ---- Inquiry support ---- */
340static void inquiry_cache_flush(struct hci_dev *hdev)
341{
342 struct inquiry_cache *cache = &hdev->inq_cache;
343 struct inquiry_entry *next = cache->list, *e;
344
345 BT_DBG("cache %p", cache);
346
347 cache->list = NULL;
348 while ((e = next)) {
349 next = e->next;
350 kfree(e);
351 }
352}
353
354struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
355{
356 struct inquiry_cache *cache = &hdev->inq_cache;
357 struct inquiry_entry *e;
358
359 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
360
361 for (e = cache->list; e; e = e->next)
362 if (!bacmp(&e->data.bdaddr, bdaddr))
363 break;
364 return e;
365}
366
367void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data)
368{
369 struct inquiry_cache *cache = &hdev->inq_cache;
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200370 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr));
373
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200374 ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
375 if (!ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* Entry not in the cache. Add new one. */
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200377 ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
378 if (!ie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return;
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200380
381 ie->next = cache->list;
382 cache->list = ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200385 memcpy(&ie->data, data, sizeof(*data));
386 ie->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 cache->timestamp = jiffies;
388}
389
390static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
391{
392 struct inquiry_cache *cache = &hdev->inq_cache;
393 struct inquiry_info *info = (struct inquiry_info *) buf;
394 struct inquiry_entry *e;
395 int copied = 0;
396
397 for (e = cache->list; e && copied < num; e = e->next, copied++) {
398 struct inquiry_data *data = &e->data;
399 bacpy(&info->bdaddr, &data->bdaddr);
400 info->pscan_rep_mode = data->pscan_rep_mode;
401 info->pscan_period_mode = data->pscan_period_mode;
402 info->pscan_mode = data->pscan_mode;
403 memcpy(info->dev_class, data->dev_class, 3);
404 info->clock_offset = data->clock_offset;
405 info++;
406 }
407
408 BT_DBG("cache %p, copied %d", cache, copied);
409 return copied;
410}
411
412static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
413{
414 struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
415 struct hci_cp_inquiry cp;
416
417 BT_DBG("%s", hdev->name);
418
419 if (test_bit(HCI_INQUIRY, &hdev->flags))
420 return;
421
422 /* Start Inquiry */
423 memcpy(&cp.lap, &ir->lap, 3);
424 cp.length = ir->length;
425 cp.num_rsp = ir->num_rsp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200426 hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429int hci_inquiry(void __user *arg)
430{
431 __u8 __user *ptr = arg;
432 struct hci_inquiry_req ir;
433 struct hci_dev *hdev;
434 int err = 0, do_inquiry = 0, max_rsp;
435 long timeo;
436 __u8 *buf;
437
438 if (copy_from_user(&ir, ptr, sizeof(ir)))
439 return -EFAULT;
440
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200441 hdev = hci_dev_get(ir.dev_id);
442 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return -ENODEV;
444
445 hci_dev_lock_bh(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900446 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200447 inquiry_cache_empty(hdev) ||
448 ir.flags & IREQ_CACHE_FLUSH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 inquiry_cache_flush(hdev);
450 do_inquiry = 1;
451 }
452 hci_dev_unlock_bh(hdev);
453
Marcel Holtmann04837f62006-07-03 10:02:33 +0200454 timeo = ir.length * msecs_to_jiffies(2000);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200455
456 if (do_inquiry) {
457 err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo);
458 if (err < 0)
459 goto done;
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* for unlimited number of responses we will use buffer with 255 entries */
463 max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
464
465 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
466 * copy it to the user space.
467 */
Szymon Janc01df8c32011-02-17 16:46:47 +0100468 buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200469 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 err = -ENOMEM;
471 goto done;
472 }
473
474 hci_dev_lock_bh(hdev);
475 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
476 hci_dev_unlock_bh(hdev);
477
478 BT_DBG("num_rsp %d", ir.num_rsp);
479
480 if (!copy_to_user(ptr, &ir, sizeof(ir))) {
481 ptr += sizeof(ir);
482 if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
483 ir.num_rsp))
484 err = -EFAULT;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900485 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 err = -EFAULT;
487
488 kfree(buf);
489
490done:
491 hci_dev_put(hdev);
492 return err;
493}
494
495/* ---- HCI ioctl helpers ---- */
496
497int hci_dev_open(__u16 dev)
498{
499 struct hci_dev *hdev;
500 int ret = 0;
501
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200502 hdev = hci_dev_get(dev);
503 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -ENODEV;
505
506 BT_DBG("%s %p", hdev->name, hdev);
507
508 hci_req_lock(hdev);
509
Marcel Holtmann611b30f2009-06-08 14:41:38 +0200510 if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) {
511 ret = -ERFKILL;
512 goto done;
513 }
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (test_bit(HCI_UP, &hdev->flags)) {
516 ret = -EALREADY;
517 goto done;
518 }
519
520 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
521 set_bit(HCI_RAW, &hdev->flags);
522
Marcel Holtmann943da252010-02-13 02:28:41 +0100523 /* Treat all non BR/EDR controllers as raw devices for now */
524 if (hdev->dev_type != HCI_BREDR)
525 set_bit(HCI_RAW, &hdev->flags);
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (hdev->open(hdev)) {
528 ret = -EIO;
529 goto done;
530 }
531
532 if (!test_bit(HCI_RAW, &hdev->flags)) {
533 atomic_set(&hdev->cmd_cnt, 1);
534 set_bit(HCI_INIT, &hdev->flags);
Johan Hedberga5040ef2011-01-10 13:28:59 +0200535 hdev->init_last_cmd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Marcel Holtmann04837f62006-07-03 10:02:33 +0200537 ret = __hci_request(hdev, hci_init_req, 0,
538 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300540 if (lmp_le_capable(hdev))
541 ret = __hci_request(hdev, hci_le_init_req, 0,
542 msecs_to_jiffies(HCI_INIT_TIMEOUT));
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 clear_bit(HCI_INIT, &hdev->flags);
545 }
546
547 if (!ret) {
548 hci_dev_hold(hdev);
549 set_bit(HCI_UP, &hdev->flags);
550 hci_notify(hdev, HCI_DEV_UP);
Johan Hedberg5add6af2010-12-16 10:00:37 +0200551 if (!test_bit(HCI_SETUP, &hdev->flags))
552 mgmt_powered(hdev->id, 1);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900553 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* Init failed, cleanup */
555 tasklet_kill(&hdev->rx_task);
556 tasklet_kill(&hdev->tx_task);
557 tasklet_kill(&hdev->cmd_task);
558
559 skb_queue_purge(&hdev->cmd_q);
560 skb_queue_purge(&hdev->rx_q);
561
562 if (hdev->flush)
563 hdev->flush(hdev);
564
565 if (hdev->sent_cmd) {
566 kfree_skb(hdev->sent_cmd);
567 hdev->sent_cmd = NULL;
568 }
569
570 hdev->close(hdev);
571 hdev->flags = 0;
572 }
573
574done:
575 hci_req_unlock(hdev);
576 hci_dev_put(hdev);
577 return ret;
578}
579
580static int hci_dev_do_close(struct hci_dev *hdev)
581{
582 BT_DBG("%s %p", hdev->name, hdev);
583
584 hci_req_cancel(hdev, ENODEV);
585 hci_req_lock(hdev);
586
587 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
588 hci_req_unlock(hdev);
589 return 0;
590 }
591
592 /* Kill RX and TX tasks */
593 tasklet_kill(&hdev->rx_task);
594 tasklet_kill(&hdev->tx_task);
595
596 hci_dev_lock_bh(hdev);
597 inquiry_cache_flush(hdev);
598 hci_conn_hash_flush(hdev);
599 hci_dev_unlock_bh(hdev);
600
601 hci_notify(hdev, HCI_DEV_DOWN);
602
603 if (hdev->flush)
604 hdev->flush(hdev);
605
606 /* Reset device */
607 skb_queue_purge(&hdev->cmd_q);
608 atomic_set(&hdev->cmd_cnt, 1);
609 if (!test_bit(HCI_RAW, &hdev->flags)) {
610 set_bit(HCI_INIT, &hdev->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200611 __hci_request(hdev, hci_reset_req, 0,
612 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 clear_bit(HCI_INIT, &hdev->flags);
614 }
615
616 /* Kill cmd task */
617 tasklet_kill(&hdev->cmd_task);
618
619 /* Drop queues */
620 skb_queue_purge(&hdev->rx_q);
621 skb_queue_purge(&hdev->cmd_q);
622 skb_queue_purge(&hdev->raw_q);
623
624 /* Drop last sent command */
625 if (hdev->sent_cmd) {
Ville Tervo6bd32322011-02-16 16:32:41 +0200626 del_timer_sync(&hdev->cmd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 kfree_skb(hdev->sent_cmd);
628 hdev->sent_cmd = NULL;
629 }
630
631 /* After this point our queues are empty
632 * and no tasks are scheduled. */
633 hdev->close(hdev);
634
Johan Hedberg5add6af2010-12-16 10:00:37 +0200635 mgmt_powered(hdev->id, 0);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* Clear flags */
638 hdev->flags = 0;
639
640 hci_req_unlock(hdev);
641
642 hci_dev_put(hdev);
643 return 0;
644}
645
646int hci_dev_close(__u16 dev)
647{
648 struct hci_dev *hdev;
649 int err;
650
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200651 hdev = hci_dev_get(dev);
652 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -ENODEV;
654 err = hci_dev_do_close(hdev);
655 hci_dev_put(hdev);
656 return err;
657}
658
659int hci_dev_reset(__u16 dev)
660{
661 struct hci_dev *hdev;
662 int ret = 0;
663
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200664 hdev = hci_dev_get(dev);
665 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -ENODEV;
667
668 hci_req_lock(hdev);
669 tasklet_disable(&hdev->tx_task);
670
671 if (!test_bit(HCI_UP, &hdev->flags))
672 goto done;
673
674 /* Drop queues */
675 skb_queue_purge(&hdev->rx_q);
676 skb_queue_purge(&hdev->cmd_q);
677
678 hci_dev_lock_bh(hdev);
679 inquiry_cache_flush(hdev);
680 hci_conn_hash_flush(hdev);
681 hci_dev_unlock_bh(hdev);
682
683 if (hdev->flush)
684 hdev->flush(hdev);
685
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900686 atomic_set(&hdev->cmd_cnt, 1);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300687 hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (!test_bit(HCI_RAW, &hdev->flags))
Marcel Holtmann04837f62006-07-03 10:02:33 +0200690 ret = __hci_request(hdev, hci_reset_req, 0,
691 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693done:
694 tasklet_enable(&hdev->tx_task);
695 hci_req_unlock(hdev);
696 hci_dev_put(hdev);
697 return ret;
698}
699
700int hci_dev_reset_stat(__u16 dev)
701{
702 struct hci_dev *hdev;
703 int ret = 0;
704
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200705 hdev = hci_dev_get(dev);
706 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -ENODEV;
708
709 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
710
711 hci_dev_put(hdev);
712
713 return ret;
714}
715
716int hci_dev_cmd(unsigned int cmd, void __user *arg)
717{
718 struct hci_dev *hdev;
719 struct hci_dev_req dr;
720 int err = 0;
721
722 if (copy_from_user(&dr, arg, sizeof(dr)))
723 return -EFAULT;
724
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200725 hdev = hci_dev_get(dr.dev_id);
726 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -ENODEV;
728
729 switch (cmd) {
730 case HCISETAUTH:
Marcel Holtmann04837f62006-07-03 10:02:33 +0200731 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
732 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
734
735 case HCISETENCRYPT:
736 if (!lmp_encrypt_capable(hdev)) {
737 err = -EOPNOTSUPP;
738 break;
739 }
740
741 if (!test_bit(HCI_AUTH, &hdev->flags)) {
742 /* Auth must be enabled first */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200743 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
744 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (err)
746 break;
747 }
748
Marcel Holtmann04837f62006-07-03 10:02:33 +0200749 err = hci_request(hdev, hci_encrypt_req, dr.dev_opt,
750 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
752
753 case HCISETSCAN:
Marcel Holtmann04837f62006-07-03 10:02:33 +0200754 err = hci_request(hdev, hci_scan_req, dr.dev_opt,
755 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
757
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200758 case HCISETLINKPOL:
759 err = hci_request(hdev, hci_linkpol_req, dr.dev_opt,
760 msecs_to_jiffies(HCI_INIT_TIMEOUT));
761 break;
762
763 case HCISETLINKMODE:
764 hdev->link_mode = ((__u16) dr.dev_opt) &
765 (HCI_LM_MASTER | HCI_LM_ACCEPT);
766 break;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 case HCISETPTYPE:
769 hdev->pkt_type = (__u16) dr.dev_opt;
770 break;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 case HCISETACLMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200773 hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1);
774 hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
776
777 case HCISETSCOMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200778 hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1);
779 hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 break;
781
782 default:
783 err = -EINVAL;
784 break;
785 }
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 hci_dev_put(hdev);
788 return err;
789}
790
791int hci_get_dev_list(void __user *arg)
792{
793 struct hci_dev_list_req *dl;
794 struct hci_dev_req *dr;
795 struct list_head *p;
796 int n = 0, size, err;
797 __u16 dev_num;
798
799 if (get_user(dev_num, (__u16 __user *) arg))
800 return -EFAULT;
801
802 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
803 return -EINVAL;
804
805 size = sizeof(*dl) + dev_num * sizeof(*dr);
806
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200807 dl = kzalloc(size, GFP_KERNEL);
808 if (!dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return -ENOMEM;
810
811 dr = dl->dev_req;
812
813 read_lock_bh(&hci_dev_list_lock);
814 list_for_each(p, &hci_dev_list) {
815 struct hci_dev *hdev;
Johan Hedbergc542a062011-01-26 13:11:03 +0200816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 hdev = list_entry(p, struct hci_dev, list);
Johan Hedbergc542a062011-01-26 13:11:03 +0200818
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200819 hci_del_off_timer(hdev);
Johan Hedbergc542a062011-01-26 13:11:03 +0200820
821 if (!test_bit(HCI_MGMT, &hdev->flags))
822 set_bit(HCI_PAIRABLE, &hdev->flags);
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 (dr + n)->dev_id = hdev->id;
825 (dr + n)->dev_opt = hdev->flags;
Johan Hedbergc542a062011-01-26 13:11:03 +0200826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (++n >= dev_num)
828 break;
829 }
830 read_unlock_bh(&hci_dev_list_lock);
831
832 dl->dev_num = n;
833 size = sizeof(*dl) + n * sizeof(*dr);
834
835 err = copy_to_user(arg, dl, size);
836 kfree(dl);
837
838 return err ? -EFAULT : 0;
839}
840
841int hci_get_dev_info(void __user *arg)
842{
843 struct hci_dev *hdev;
844 struct hci_dev_info di;
845 int err = 0;
846
847 if (copy_from_user(&di, arg, sizeof(di)))
848 return -EFAULT;
849
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200850 hdev = hci_dev_get(di.dev_id);
851 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return -ENODEV;
853
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200854 hci_del_off_timer(hdev);
855
Johan Hedbergc542a062011-01-26 13:11:03 +0200856 if (!test_bit(HCI_MGMT, &hdev->flags))
857 set_bit(HCI_PAIRABLE, &hdev->flags);
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 strcpy(di.name, hdev->name);
860 di.bdaddr = hdev->bdaddr;
Marcel Holtmann943da252010-02-13 02:28:41 +0100861 di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 di.flags = hdev->flags;
863 di.pkt_type = hdev->pkt_type;
864 di.acl_mtu = hdev->acl_mtu;
865 di.acl_pkts = hdev->acl_pkts;
866 di.sco_mtu = hdev->sco_mtu;
867 di.sco_pkts = hdev->sco_pkts;
868 di.link_policy = hdev->link_policy;
869 di.link_mode = hdev->link_mode;
870
871 memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
872 memcpy(&di.features, &hdev->features, sizeof(di.features));
873
874 if (copy_to_user(arg, &di, sizeof(di)))
875 err = -EFAULT;
876
877 hci_dev_put(hdev);
878
879 return err;
880}
881
882/* ---- Interface to HCI drivers ---- */
883
Marcel Holtmann611b30f2009-06-08 14:41:38 +0200884static int hci_rfkill_set_block(void *data, bool blocked)
885{
886 struct hci_dev *hdev = data;
887
888 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
889
890 if (!blocked)
891 return 0;
892
893 hci_dev_do_close(hdev);
894
895 return 0;
896}
897
898static const struct rfkill_ops hci_rfkill_ops = {
899 .set_block = hci_rfkill_set_block,
900};
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/* Alloc HCI device */
903struct hci_dev *hci_alloc_dev(void)
904{
905 struct hci_dev *hdev;
906
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200907 hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (!hdev)
909 return NULL;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 skb_queue_head_init(&hdev->driver_init);
912
913 return hdev;
914}
915EXPORT_SYMBOL(hci_alloc_dev);
916
917/* Free HCI device */
918void hci_free_dev(struct hci_dev *hdev)
919{
920 skb_queue_purge(&hdev->driver_init);
921
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200922 /* will free via device release */
923 put_device(&hdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925EXPORT_SYMBOL(hci_free_dev);
926
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200927static void hci_power_on(struct work_struct *work)
928{
929 struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
930
931 BT_DBG("%s", hdev->name);
932
933 if (hci_dev_open(hdev->id) < 0)
934 return;
935
936 if (test_bit(HCI_AUTO_OFF, &hdev->flags))
937 mod_timer(&hdev->off_timer,
938 jiffies + msecs_to_jiffies(AUTO_OFF_TIMEOUT));
939
940 if (test_and_clear_bit(HCI_SETUP, &hdev->flags))
941 mgmt_index_added(hdev->id);
942}
943
944static void hci_power_off(struct work_struct *work)
945{
946 struct hci_dev *hdev = container_of(work, struct hci_dev, power_off);
947
948 BT_DBG("%s", hdev->name);
949
950 hci_dev_close(hdev->id);
951}
952
953static void hci_auto_off(unsigned long data)
954{
955 struct hci_dev *hdev = (struct hci_dev *) data;
956
957 BT_DBG("%s", hdev->name);
958
959 clear_bit(HCI_AUTO_OFF, &hdev->flags);
960
961 queue_work(hdev->workqueue, &hdev->power_off);
962}
963
964void hci_del_off_timer(struct hci_dev *hdev)
965{
966 BT_DBG("%s", hdev->name);
967
968 clear_bit(HCI_AUTO_OFF, &hdev->flags);
969 del_timer(&hdev->off_timer);
970}
971
Johan Hedberg2aeb9a12011-01-04 12:08:51 +0200972int hci_uuids_clear(struct hci_dev *hdev)
973{
974 struct list_head *p, *n;
975
976 list_for_each_safe(p, n, &hdev->uuids) {
977 struct bt_uuid *uuid;
978
979 uuid = list_entry(p, struct bt_uuid, list);
980
981 list_del(p);
982 kfree(uuid);
983 }
984
985 return 0;
986}
987
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200988int hci_link_keys_clear(struct hci_dev *hdev)
989{
990 struct list_head *p, *n;
991
992 list_for_each_safe(p, n, &hdev->link_keys) {
993 struct link_key *key;
994
995 key = list_entry(p, struct link_key, list);
996
997 list_del(p);
998 kfree(key);
999 }
1000
1001 return 0;
1002}
1003
1004struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1005{
1006 struct list_head *p;
1007
1008 list_for_each(p, &hdev->link_keys) {
1009 struct link_key *k;
1010
1011 k = list_entry(p, struct link_key, list);
1012
1013 if (bacmp(bdaddr, &k->bdaddr) == 0)
1014 return k;
1015 }
1016
1017 return NULL;
1018}
1019
1020int hci_add_link_key(struct hci_dev *hdev, int new_key, bdaddr_t *bdaddr,
1021 u8 *val, u8 type, u8 pin_len)
1022{
1023 struct link_key *key, *old_key;
1024 u8 old_key_type;
1025
1026 old_key = hci_find_link_key(hdev, bdaddr);
1027 if (old_key) {
1028 old_key_type = old_key->type;
1029 key = old_key;
1030 } else {
1031 old_key_type = 0xff;
1032 key = kzalloc(sizeof(*key), GFP_ATOMIC);
1033 if (!key)
1034 return -ENOMEM;
1035 list_add(&key->list, &hdev->link_keys);
1036 }
1037
1038 BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type);
1039
1040 bacpy(&key->bdaddr, bdaddr);
1041 memcpy(key->val, val, 16);
1042 key->type = type;
1043 key->pin_len = pin_len;
1044
1045 if (new_key)
1046 mgmt_new_key(hdev->id, key, old_key_type);
1047
1048 if (type == 0x06)
1049 key->type = old_key_type;
1050
1051 return 0;
1052}
1053
1054int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1055{
1056 struct link_key *key;
1057
1058 key = hci_find_link_key(hdev, bdaddr);
1059 if (!key)
1060 return -ENOENT;
1061
1062 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1063
1064 list_del(&key->list);
1065 kfree(key);
1066
1067 return 0;
1068}
1069
Ville Tervo6bd32322011-02-16 16:32:41 +02001070/* HCI command timer function */
1071static void hci_cmd_timer(unsigned long arg)
1072{
1073 struct hci_dev *hdev = (void *) arg;
1074
1075 BT_ERR("%s command tx timeout", hdev->name);
1076 atomic_set(&hdev->cmd_cnt, 1);
1077 tasklet_schedule(&hdev->cmd_task);
1078}
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080/* Register HCI device */
1081int hci_register_dev(struct hci_dev *hdev)
1082{
1083 struct list_head *head = &hci_dev_list, *p;
Marcel Holtmannef222012007-07-11 06:42:04 +02001084 int i, id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Marcel Holtmannc13854c2010-02-08 15:27:07 +01001086 BT_DBG("%p name %s bus %d owner %p", hdev, hdev->name,
1087 hdev->bus, hdev->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (!hdev->open || !hdev->close || !hdev->destruct)
1090 return -EINVAL;
1091
1092 write_lock_bh(&hci_dev_list_lock);
1093
1094 /* Find first available device id */
1095 list_for_each(p, &hci_dev_list) {
1096 if (list_entry(p, struct hci_dev, list)->id != id)
1097 break;
1098 head = p; id++;
1099 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 sprintf(hdev->name, "hci%d", id);
1102 hdev->id = id;
1103 list_add(&hdev->list, head);
1104
1105 atomic_set(&hdev->refcnt, 1);
1106 spin_lock_init(&hdev->lock);
1107
1108 hdev->flags = 0;
1109 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001110 hdev->esco_type = (ESCO_HV1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 hdev->link_mode = (HCI_LM_ACCEPT);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001112 hdev->io_capability = 0x03; /* No Input No Output */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Marcel Holtmann04837f62006-07-03 10:02:33 +02001114 hdev->idle_timeout = 0;
1115 hdev->sniff_max_interval = 800;
1116 hdev->sniff_min_interval = 80;
1117
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001118 tasklet_init(&hdev->cmd_task, hci_cmd_task, (unsigned long) hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 tasklet_init(&hdev->rx_task, hci_rx_task, (unsigned long) hdev);
1120 tasklet_init(&hdev->tx_task, hci_tx_task, (unsigned long) hdev);
1121
1122 skb_queue_head_init(&hdev->rx_q);
1123 skb_queue_head_init(&hdev->cmd_q);
1124 skb_queue_head_init(&hdev->raw_q);
1125
Ville Tervo6bd32322011-02-16 16:32:41 +02001126 setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev);
1127
Suraj Sumangalacd4c5392010-07-14 13:02:16 +05301128 for (i = 0; i < NUM_REASSEMBLY; i++)
Marcel Holtmannef222012007-07-11 06:42:04 +02001129 hdev->reassembly[i] = NULL;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 init_waitqueue_head(&hdev->req_wait_q);
Thomas Gleixnera6a67ef2009-07-26 08:18:19 +00001132 mutex_init(&hdev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 inquiry_cache_init(hdev);
1135
1136 hci_conn_hash_init(hdev);
1137
David Millerea4bd8b2010-07-30 21:54:49 -07001138 INIT_LIST_HEAD(&hdev->blacklist);
Johan Hedbergf0358562010-05-18 13:20:32 +02001139
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001140 INIT_LIST_HEAD(&hdev->uuids);
1141
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001142 INIT_LIST_HEAD(&hdev->link_keys);
1143
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001144 INIT_WORK(&hdev->power_on, hci_power_on);
1145 INIT_WORK(&hdev->power_off, hci_power_off);
1146 setup_timer(&hdev->off_timer, hci_auto_off, (unsigned long) hdev);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
1149
1150 atomic_set(&hdev->promisc, 0);
1151
1152 write_unlock_bh(&hci_dev_list_lock);
1153
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001154 hdev->workqueue = create_singlethread_workqueue(hdev->name);
1155 if (!hdev->workqueue)
1156 goto nomem;
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 hci_register_sysfs(hdev);
1159
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001160 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
1161 RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, hdev);
1162 if (hdev->rfkill) {
1163 if (rfkill_register(hdev->rfkill) < 0) {
1164 rfkill_destroy(hdev->rfkill);
1165 hdev->rfkill = NULL;
1166 }
1167 }
1168
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001169 set_bit(HCI_AUTO_OFF, &hdev->flags);
1170 set_bit(HCI_SETUP, &hdev->flags);
1171 queue_work(hdev->workqueue, &hdev->power_on);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 hci_notify(hdev, HCI_DEV_REG);
1174
1175 return id;
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001176
1177nomem:
1178 write_lock_bh(&hci_dev_list_lock);
1179 list_del(&hdev->list);
1180 write_unlock_bh(&hci_dev_list_lock);
1181
1182 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184EXPORT_SYMBOL(hci_register_dev);
1185
1186/* Unregister HCI device */
1187int hci_unregister_dev(struct hci_dev *hdev)
1188{
Marcel Holtmannef222012007-07-11 06:42:04 +02001189 int i;
1190
Marcel Holtmannc13854c2010-02-08 15:27:07 +01001191 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 write_lock_bh(&hci_dev_list_lock);
1194 list_del(&hdev->list);
1195 write_unlock_bh(&hci_dev_list_lock);
1196
1197 hci_dev_do_close(hdev);
1198
Suraj Sumangalacd4c5392010-07-14 13:02:16 +05301199 for (i = 0; i < NUM_REASSEMBLY; i++)
Marcel Holtmannef222012007-07-11 06:42:04 +02001200 kfree_skb(hdev->reassembly[i]);
1201
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001202 if (!test_bit(HCI_INIT, &hdev->flags) &&
1203 !test_bit(HCI_SETUP, &hdev->flags))
1204 mgmt_index_removed(hdev->id);
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 hci_notify(hdev, HCI_DEV_UNREG);
1207
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001208 if (hdev->rfkill) {
1209 rfkill_unregister(hdev->rfkill);
1210 rfkill_destroy(hdev->rfkill);
1211 }
1212
Dave Young147e2d52008-03-05 18:45:59 -08001213 hci_unregister_sysfs(hdev);
1214
Gustavo F. Padovanc6f3c5f2011-02-15 20:22:03 -03001215 hci_del_off_timer(hdev);
1216
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001217 destroy_workqueue(hdev->workqueue);
1218
Johan Hedberge2e0cac2011-01-04 12:08:50 +02001219 hci_dev_lock_bh(hdev);
1220 hci_blacklist_clear(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001221 hci_uuids_clear(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001222 hci_link_keys_clear(hdev);
Johan Hedberge2e0cac2011-01-04 12:08:50 +02001223 hci_dev_unlock_bh(hdev);
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 __hci_dev_put(hdev);
Marcel Holtmannef222012007-07-11 06:42:04 +02001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return 0;
1228}
1229EXPORT_SYMBOL(hci_unregister_dev);
1230
1231/* Suspend HCI device */
1232int hci_suspend_dev(struct hci_dev *hdev)
1233{
1234 hci_notify(hdev, HCI_DEV_SUSPEND);
1235 return 0;
1236}
1237EXPORT_SYMBOL(hci_suspend_dev);
1238
1239/* Resume HCI device */
1240int hci_resume_dev(struct hci_dev *hdev)
1241{
1242 hci_notify(hdev, HCI_DEV_RESUME);
1243 return 0;
1244}
1245EXPORT_SYMBOL(hci_resume_dev);
1246
Marcel Holtmann76bca882009-11-18 00:40:39 +01001247/* Receive frame from HCI drivers */
1248int hci_recv_frame(struct sk_buff *skb)
1249{
1250 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
1251 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
1252 && !test_bit(HCI_INIT, &hdev->flags))) {
1253 kfree_skb(skb);
1254 return -ENXIO;
1255 }
1256
1257 /* Incomming skb */
1258 bt_cb(skb)->incoming = 1;
1259
1260 /* Time stamp */
1261 __net_timestamp(skb);
1262
1263 /* Queue frame for rx task */
1264 skb_queue_tail(&hdev->rx_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001265 tasklet_schedule(&hdev->rx_task);
1266
Marcel Holtmann76bca882009-11-18 00:40:39 +01001267 return 0;
1268}
1269EXPORT_SYMBOL(hci_recv_frame);
1270
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301271static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
1272 int count, __u8 index, gfp_t gfp_mask)
1273{
1274 int len = 0;
1275 int hlen = 0;
1276 int remain = count;
1277 struct sk_buff *skb;
1278 struct bt_skb_cb *scb;
1279
1280 if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
1281 index >= NUM_REASSEMBLY)
1282 return -EILSEQ;
1283
1284 skb = hdev->reassembly[index];
1285
1286 if (!skb) {
1287 switch (type) {
1288 case HCI_ACLDATA_PKT:
1289 len = HCI_MAX_FRAME_SIZE;
1290 hlen = HCI_ACL_HDR_SIZE;
1291 break;
1292 case HCI_EVENT_PKT:
1293 len = HCI_MAX_EVENT_SIZE;
1294 hlen = HCI_EVENT_HDR_SIZE;
1295 break;
1296 case HCI_SCODATA_PKT:
1297 len = HCI_MAX_SCO_SIZE;
1298 hlen = HCI_SCO_HDR_SIZE;
1299 break;
1300 }
1301
1302 skb = bt_skb_alloc(len, gfp_mask);
1303 if (!skb)
1304 return -ENOMEM;
1305
1306 scb = (void *) skb->cb;
1307 scb->expect = hlen;
1308 scb->pkt_type = type;
1309
1310 skb->dev = (void *) hdev;
1311 hdev->reassembly[index] = skb;
1312 }
1313
1314 while (count) {
1315 scb = (void *) skb->cb;
1316 len = min(scb->expect, (__u16)count);
1317
1318 memcpy(skb_put(skb, len), data, len);
1319
1320 count -= len;
1321 data += len;
1322 scb->expect -= len;
1323 remain = count;
1324
1325 switch (type) {
1326 case HCI_EVENT_PKT:
1327 if (skb->len == HCI_EVENT_HDR_SIZE) {
1328 struct hci_event_hdr *h = hci_event_hdr(skb);
1329 scb->expect = h->plen;
1330
1331 if (skb_tailroom(skb) < scb->expect) {
1332 kfree_skb(skb);
1333 hdev->reassembly[index] = NULL;
1334 return -ENOMEM;
1335 }
1336 }
1337 break;
1338
1339 case HCI_ACLDATA_PKT:
1340 if (skb->len == HCI_ACL_HDR_SIZE) {
1341 struct hci_acl_hdr *h = hci_acl_hdr(skb);
1342 scb->expect = __le16_to_cpu(h->dlen);
1343
1344 if (skb_tailroom(skb) < scb->expect) {
1345 kfree_skb(skb);
1346 hdev->reassembly[index] = NULL;
1347 return -ENOMEM;
1348 }
1349 }
1350 break;
1351
1352 case HCI_SCODATA_PKT:
1353 if (skb->len == HCI_SCO_HDR_SIZE) {
1354 struct hci_sco_hdr *h = hci_sco_hdr(skb);
1355 scb->expect = h->dlen;
1356
1357 if (skb_tailroom(skb) < scb->expect) {
1358 kfree_skb(skb);
1359 hdev->reassembly[index] = NULL;
1360 return -ENOMEM;
1361 }
1362 }
1363 break;
1364 }
1365
1366 if (scb->expect == 0) {
1367 /* Complete frame */
1368
1369 bt_cb(skb)->pkt_type = type;
1370 hci_recv_frame(skb);
1371
1372 hdev->reassembly[index] = NULL;
1373 return remain;
1374 }
1375 }
1376
1377 return remain;
1378}
1379
Marcel Holtmannef222012007-07-11 06:42:04 +02001380int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
1381{
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301382 int rem = 0;
1383
Marcel Holtmannef222012007-07-11 06:42:04 +02001384 if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
1385 return -EILSEQ;
1386
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001387 while (count) {
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301388 rem = hci_reassembly(hdev, type, data, count,
1389 type - 1, GFP_ATOMIC);
1390 if (rem < 0)
1391 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02001392
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301393 data += (count - rem);
1394 count = rem;
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001395 };
Marcel Holtmannef222012007-07-11 06:42:04 +02001396
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301397 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02001398}
1399EXPORT_SYMBOL(hci_recv_fragment);
1400
Suraj Sumangala99811512010-07-14 13:02:19 +05301401#define STREAM_REASSEMBLY 0
1402
1403int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
1404{
1405 int type;
1406 int rem = 0;
1407
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001408 while (count) {
Suraj Sumangala99811512010-07-14 13:02:19 +05301409 struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
1410
1411 if (!skb) {
1412 struct { char type; } *pkt;
1413
1414 /* Start of the frame */
1415 pkt = data;
1416 type = pkt->type;
1417
1418 data++;
1419 count--;
1420 } else
1421 type = bt_cb(skb)->pkt_type;
1422
1423 rem = hci_reassembly(hdev, type, data,
1424 count, STREAM_REASSEMBLY, GFP_ATOMIC);
1425 if (rem < 0)
1426 return rem;
1427
1428 data += (count - rem);
1429 count = rem;
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001430 };
Suraj Sumangala99811512010-07-14 13:02:19 +05301431
1432 return rem;
1433}
1434EXPORT_SYMBOL(hci_recv_stream_fragment);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/* ---- Interface to upper protocols ---- */
1437
1438/* Register/Unregister protocols.
1439 * hci_task_lock is used to ensure that no tasks are running. */
1440int hci_register_proto(struct hci_proto *hp)
1441{
1442 int err = 0;
1443
1444 BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
1445
1446 if (hp->id >= HCI_MAX_PROTO)
1447 return -EINVAL;
1448
1449 write_lock_bh(&hci_task_lock);
1450
1451 if (!hci_proto[hp->id])
1452 hci_proto[hp->id] = hp;
1453 else
1454 err = -EEXIST;
1455
1456 write_unlock_bh(&hci_task_lock);
1457
1458 return err;
1459}
1460EXPORT_SYMBOL(hci_register_proto);
1461
1462int hci_unregister_proto(struct hci_proto *hp)
1463{
1464 int err = 0;
1465
1466 BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
1467
1468 if (hp->id >= HCI_MAX_PROTO)
1469 return -EINVAL;
1470
1471 write_lock_bh(&hci_task_lock);
1472
1473 if (hci_proto[hp->id])
1474 hci_proto[hp->id] = NULL;
1475 else
1476 err = -ENOENT;
1477
1478 write_unlock_bh(&hci_task_lock);
1479
1480 return err;
1481}
1482EXPORT_SYMBOL(hci_unregister_proto);
1483
1484int hci_register_cb(struct hci_cb *cb)
1485{
1486 BT_DBG("%p name %s", cb, cb->name);
1487
1488 write_lock_bh(&hci_cb_list_lock);
1489 list_add(&cb->list, &hci_cb_list);
1490 write_unlock_bh(&hci_cb_list_lock);
1491
1492 return 0;
1493}
1494EXPORT_SYMBOL(hci_register_cb);
1495
1496int hci_unregister_cb(struct hci_cb *cb)
1497{
1498 BT_DBG("%p name %s", cb, cb->name);
1499
1500 write_lock_bh(&hci_cb_list_lock);
1501 list_del(&cb->list);
1502 write_unlock_bh(&hci_cb_list_lock);
1503
1504 return 0;
1505}
1506EXPORT_SYMBOL(hci_unregister_cb);
1507
1508static int hci_send_frame(struct sk_buff *skb)
1509{
1510 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
1511
1512 if (!hdev) {
1513 kfree_skb(skb);
1514 return -ENODEV;
1515 }
1516
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001517 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (atomic_read(&hdev->promisc)) {
1520 /* Time stamp */
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001521 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001523 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
1526 /* Get rid of skb owner, prior to sending to the driver. */
1527 skb_orphan(skb);
1528
1529 return hdev->send(skb);
1530}
1531
1532/* Send HCI command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001533int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 int len = HCI_COMMAND_HDR_SIZE + plen;
1536 struct hci_command_hdr *hdr;
1537 struct sk_buff *skb;
1538
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001539 BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 skb = bt_skb_alloc(len, GFP_ATOMIC);
1542 if (!skb) {
Marcel Holtmannef222012007-07-11 06:42:04 +02001543 BT_ERR("%s no memory for command", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return -ENOMEM;
1545 }
1546
1547 hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001548 hdr->opcode = cpu_to_le16(opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 hdr->plen = plen;
1550
1551 if (plen)
1552 memcpy(skb_put(skb, plen), param, plen);
1553
1554 BT_DBG("skb len %d", skb->len);
1555
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001556 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 skb->dev = (void *) hdev;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001558
Johan Hedberga5040ef2011-01-10 13:28:59 +02001559 if (test_bit(HCI_INIT, &hdev->flags))
1560 hdev->init_last_cmd = opcode;
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 skb_queue_tail(&hdev->cmd_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001563 tasklet_schedule(&hdev->cmd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 return 0;
1566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568/* Get data from the previously sent command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001569void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 struct hci_command_hdr *hdr;
1572
1573 if (!hdev->sent_cmd)
1574 return NULL;
1575
1576 hdr = (void *) hdev->sent_cmd->data;
1577
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001578 if (hdr->opcode != cpu_to_le16(opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return NULL;
1580
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001581 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
1584}
1585
1586/* Send ACL data */
1587static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
1588{
1589 struct hci_acl_hdr *hdr;
1590 int len = skb->len;
1591
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001592 skb_push(skb, HCI_ACL_HDR_SIZE);
1593 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001594 hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001595 hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
1596 hdr->dlen = cpu_to_le16(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -03001599void hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct hci_dev *hdev = conn->hdev;
1602 struct sk_buff *list;
1603
1604 BT_DBG("%s conn %p flags 0x%x", hdev->name, conn, flags);
1605
1606 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001607 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02001608 hci_add_acl_hdr(skb, conn->handle, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001610 list = skb_shinfo(skb)->frag_list;
1611 if (!list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* Non fragmented */
1613 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
1614
1615 skb_queue_tail(&conn->data_q, skb);
1616 } else {
1617 /* Fragmented */
1618 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
1619
1620 skb_shinfo(skb)->frag_list = NULL;
1621
1622 /* Queue all fragments atomically */
1623 spin_lock_bh(&conn->data_q.lock);
1624
1625 __skb_queue_tail(&conn->data_q, skb);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02001626
1627 flags &= ~ACL_START;
1628 flags |= ACL_CONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 do {
1630 skb = list; list = list->next;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001633 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02001634 hci_add_acl_hdr(skb, conn->handle, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
1637
1638 __skb_queue_tail(&conn->data_q, skb);
1639 } while (list);
1640
1641 spin_unlock_bh(&conn->data_q.lock);
1642 }
1643
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001644 tasklet_schedule(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646EXPORT_SYMBOL(hci_send_acl);
1647
1648/* Send SCO data */
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -03001649void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct hci_dev *hdev = conn->hdev;
1652 struct hci_sco_hdr hdr;
1653
1654 BT_DBG("%s len %d", hdev->name, skb->len);
1655
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001656 hdr.handle = cpu_to_le16(conn->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 hdr.dlen = skb->len;
1658
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001659 skb_push(skb, HCI_SCO_HDR_SIZE);
1660 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001661 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001664 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 skb_queue_tail(&conn->data_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001667 tasklet_schedule(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669EXPORT_SYMBOL(hci_send_sco);
1670
1671/* ---- HCI TX task (outgoing data) ---- */
1672
1673/* HCI Connection scheduler */
1674static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote)
1675{
1676 struct hci_conn_hash *h = &hdev->conn_hash;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001677 struct hci_conn *conn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 int num = 0, min = ~0;
1679 struct list_head *p;
1680
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001681 /* We don't have to lock device here. Connections are always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * added and removed with TX task disabled. */
1683 list_for_each(p, &h->list) {
1684 struct hci_conn *c;
1685 c = list_entry(p, struct hci_conn, list);
1686
Marcel Holtmann769be972008-07-14 20:13:49 +02001687 if (c->type != type || skb_queue_empty(&c->data_q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 continue;
Marcel Holtmann769be972008-07-14 20:13:49 +02001689
1690 if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
1691 continue;
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 num++;
1694
1695 if (c->sent < min) {
1696 min = c->sent;
1697 conn = c;
1698 }
1699 }
1700
1701 if (conn) {
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001702 int cnt, q;
1703
1704 switch (conn->type) {
1705 case ACL_LINK:
1706 cnt = hdev->acl_cnt;
1707 break;
1708 case SCO_LINK:
1709 case ESCO_LINK:
1710 cnt = hdev->sco_cnt;
1711 break;
1712 case LE_LINK:
1713 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
1714 break;
1715 default:
1716 cnt = 0;
1717 BT_ERR("Unknown link type");
1718 }
1719
1720 q = cnt / num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 *quote = q ? q : 1;
1722 } else
1723 *quote = 0;
1724
1725 BT_DBG("conn %p quote %d", conn, *quote);
1726 return conn;
1727}
1728
Ville Tervobae1f5d92011-02-10 22:38:53 -03001729static inline void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 struct hci_conn_hash *h = &hdev->conn_hash;
1732 struct list_head *p;
1733 struct hci_conn *c;
1734
Ville Tervobae1f5d92011-02-10 22:38:53 -03001735 BT_ERR("%s link tx timeout", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /* Kill stalled connections */
1738 list_for_each(p, &h->list) {
1739 c = list_entry(p, struct hci_conn, list);
Ville Tervobae1f5d92011-02-10 22:38:53 -03001740 if (c->type == type && c->sent) {
1741 BT_ERR("%s killing stalled connection %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 hdev->name, batostr(&c->dst));
1743 hci_acl_disconn(c, 0x13);
1744 }
1745 }
1746}
1747
1748static inline void hci_sched_acl(struct hci_dev *hdev)
1749{
1750 struct hci_conn *conn;
1751 struct sk_buff *skb;
1752 int quote;
1753
1754 BT_DBG("%s", hdev->name);
1755
1756 if (!test_bit(HCI_RAW, &hdev->flags)) {
1757 /* ACL tx timeout must be longer than maximum
1758 * link supervision timeout (40.9 seconds) */
S.Çağlar Onur824530212008-02-17 23:25:57 -08001759 if (!hdev->acl_cnt && time_after(jiffies, hdev->acl_last_tx + HZ * 45))
Ville Tervobae1f5d92011-02-10 22:38:53 -03001760 hci_link_tx_to(hdev, ACL_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762
1763 while (hdev->acl_cnt && (conn = hci_low_sent(hdev, ACL_LINK, &quote))) {
1764 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
1765 BT_DBG("skb %p len %d", skb, skb->len);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001766
1767 hci_conn_enter_active_mode(conn);
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 hci_send_frame(skb);
1770 hdev->acl_last_tx = jiffies;
1771
1772 hdev->acl_cnt--;
1773 conn->sent++;
1774 }
1775 }
1776}
1777
1778/* Schedule SCO */
1779static inline void hci_sched_sco(struct hci_dev *hdev)
1780{
1781 struct hci_conn *conn;
1782 struct sk_buff *skb;
1783 int quote;
1784
1785 BT_DBG("%s", hdev->name);
1786
1787 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
1788 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
1789 BT_DBG("skb %p len %d", skb, skb->len);
1790 hci_send_frame(skb);
1791
1792 conn->sent++;
1793 if (conn->sent == ~0)
1794 conn->sent = 0;
1795 }
1796 }
1797}
1798
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001799static inline void hci_sched_esco(struct hci_dev *hdev)
1800{
1801 struct hci_conn *conn;
1802 struct sk_buff *skb;
1803 int quote;
1804
1805 BT_DBG("%s", hdev->name);
1806
1807 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, &quote))) {
1808 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
1809 BT_DBG("skb %p len %d", skb, skb->len);
1810 hci_send_frame(skb);
1811
1812 conn->sent++;
1813 if (conn->sent == ~0)
1814 conn->sent = 0;
1815 }
1816 }
1817}
1818
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001819static inline void hci_sched_le(struct hci_dev *hdev)
1820{
1821 struct hci_conn *conn;
1822 struct sk_buff *skb;
1823 int quote, cnt;
1824
1825 BT_DBG("%s", hdev->name);
1826
1827 if (!test_bit(HCI_RAW, &hdev->flags)) {
1828 /* LE tx timeout must be longer than maximum
1829 * link supervision timeout (40.9 seconds) */
Ville Tervobae1f5d92011-02-10 22:38:53 -03001830 if (!hdev->le_cnt && hdev->le_pkts &&
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001831 time_after(jiffies, hdev->le_last_tx + HZ * 45))
Ville Tervobae1f5d92011-02-10 22:38:53 -03001832 hci_link_tx_to(hdev, LE_LINK);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001833 }
1834
1835 cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
1836 while (cnt && (conn = hci_low_sent(hdev, LE_LINK, &quote))) {
1837 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
1838 BT_DBG("skb %p len %d", skb, skb->len);
1839
1840 hci_send_frame(skb);
1841 hdev->le_last_tx = jiffies;
1842
1843 cnt--;
1844 conn->sent++;
1845 }
1846 }
1847 if (hdev->le_pkts)
1848 hdev->le_cnt = cnt;
1849 else
1850 hdev->acl_cnt = cnt;
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853static void hci_tx_task(unsigned long arg)
1854{
1855 struct hci_dev *hdev = (struct hci_dev *) arg;
1856 struct sk_buff *skb;
1857
1858 read_lock(&hci_task_lock);
1859
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001860 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
1861 hdev->sco_cnt, hdev->le_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 /* Schedule queues and send stuff to HCI driver */
1864
1865 hci_sched_acl(hdev);
1866
1867 hci_sched_sco(hdev);
1868
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001869 hci_sched_esco(hdev);
1870
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001871 hci_sched_le(hdev);
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /* Send next queued raw (unknown type) packet */
1874 while ((skb = skb_dequeue(&hdev->raw_q)))
1875 hci_send_frame(skb);
1876
1877 read_unlock(&hci_task_lock);
1878}
1879
1880/* ----- HCI RX task (incoming data proccessing) ----- */
1881
1882/* ACL data packet */
1883static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
1884{
1885 struct hci_acl_hdr *hdr = (void *) skb->data;
1886 struct hci_conn *conn;
1887 __u16 handle, flags;
1888
1889 skb_pull(skb, HCI_ACL_HDR_SIZE);
1890
1891 handle = __le16_to_cpu(hdr->handle);
1892 flags = hci_flags(handle);
1893 handle = hci_handle(handle);
1894
1895 BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags);
1896
1897 hdev->stat.acl_rx++;
1898
1899 hci_dev_lock(hdev);
1900 conn = hci_conn_hash_lookup_handle(hdev, handle);
1901 hci_dev_unlock(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (conn) {
1904 register struct hci_proto *hp;
1905
Marcel Holtmann04837f62006-07-03 10:02:33 +02001906 hci_conn_enter_active_mode(conn);
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /* Send to upper protocol */
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001909 hp = hci_proto[HCI_PROTO_L2CAP];
1910 if (hp && hp->recv_acldata) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 hp->recv_acldata(conn, skb, flags);
1912 return;
1913 }
1914 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001915 BT_ERR("%s ACL packet for unknown connection handle %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 hdev->name, handle);
1917 }
1918
1919 kfree_skb(skb);
1920}
1921
1922/* SCO data packet */
1923static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
1924{
1925 struct hci_sco_hdr *hdr = (void *) skb->data;
1926 struct hci_conn *conn;
1927 __u16 handle;
1928
1929 skb_pull(skb, HCI_SCO_HDR_SIZE);
1930
1931 handle = __le16_to_cpu(hdr->handle);
1932
1933 BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle);
1934
1935 hdev->stat.sco_rx++;
1936
1937 hci_dev_lock(hdev);
1938 conn = hci_conn_hash_lookup_handle(hdev, handle);
1939 hci_dev_unlock(hdev);
1940
1941 if (conn) {
1942 register struct hci_proto *hp;
1943
1944 /* Send to upper protocol */
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001945 hp = hci_proto[HCI_PROTO_SCO];
1946 if (hp && hp->recv_scodata) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 hp->recv_scodata(conn, skb);
1948 return;
1949 }
1950 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001951 BT_ERR("%s SCO packet for unknown connection handle %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 hdev->name, handle);
1953 }
1954
1955 kfree_skb(skb);
1956}
1957
Marcel Holtmann65164552005-10-28 19:20:48 +02001958static void hci_rx_task(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 struct hci_dev *hdev = (struct hci_dev *) arg;
1961 struct sk_buff *skb;
1962
1963 BT_DBG("%s", hdev->name);
1964
1965 read_lock(&hci_task_lock);
1966
1967 while ((skb = skb_dequeue(&hdev->rx_q))) {
1968 if (atomic_read(&hdev->promisc)) {
1969 /* Send copy to the sockets */
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001970 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
1973 if (test_bit(HCI_RAW, &hdev->flags)) {
1974 kfree_skb(skb);
1975 continue;
1976 }
1977
1978 if (test_bit(HCI_INIT, &hdev->flags)) {
1979 /* Don't process data packets in this states. */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001980 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 case HCI_ACLDATA_PKT:
1982 case HCI_SCODATA_PKT:
1983 kfree_skb(skb);
1984 continue;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987
1988 /* Process frame */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001989 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 case HCI_EVENT_PKT:
1991 hci_event_packet(hdev, skb);
1992 break;
1993
1994 case HCI_ACLDATA_PKT:
1995 BT_DBG("%s ACL data packet", hdev->name);
1996 hci_acldata_packet(hdev, skb);
1997 break;
1998
1999 case HCI_SCODATA_PKT:
2000 BT_DBG("%s SCO data packet", hdev->name);
2001 hci_scodata_packet(hdev, skb);
2002 break;
2003
2004 default:
2005 kfree_skb(skb);
2006 break;
2007 }
2008 }
2009
2010 read_unlock(&hci_task_lock);
2011}
2012
2013static void hci_cmd_task(unsigned long arg)
2014{
2015 struct hci_dev *hdev = (struct hci_dev *) arg;
2016 struct sk_buff *skb;
2017
2018 BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 /* Send queued commands */
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002021 if (atomic_read(&hdev->cmd_cnt)) {
2022 skb = skb_dequeue(&hdev->cmd_q);
2023 if (!skb)
2024 return;
2025
Wei Yongjun7585b972009-02-25 18:29:52 +08002026 kfree_skb(hdev->sent_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002028 hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC);
2029 if (hdev->sent_cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 atomic_dec(&hdev->cmd_cnt);
2031 hci_send_frame(skb);
Ville Tervo6bd32322011-02-16 16:32:41 +02002032 mod_timer(&hdev->cmd_timer,
2033 jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 } else {
2035 skb_queue_head(&hdev->cmd_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002036 tasklet_schedule(&hdev->cmd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038 }
2039}