blob: 8f5bee15e872f702334c1edaa27bbc52ef17ccd7 [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>
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -030045#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/sock.h>
47
48#include <asm/system.h>
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020049#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/unaligned.h>
51
52#include <net/bluetooth/bluetooth.h>
53#include <net/bluetooth/hci_core.h>
54
Johan Hedbergab81cbf2010-12-15 13:53:18 +020055#define AUTO_OFF_TIMEOUT 2000
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void hci_cmd_task(unsigned long arg);
58static void hci_rx_task(unsigned long arg);
59static void hci_tx_task(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static DEFINE_RWLOCK(hci_task_lock);
62
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -030063static int enable_smp;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* HCI device list */
66LIST_HEAD(hci_dev_list);
67DEFINE_RWLOCK(hci_dev_list_lock);
68
69/* HCI callback list */
70LIST_HEAD(hci_cb_list);
71DEFINE_RWLOCK(hci_cb_list_lock);
72
73/* HCI protocols */
74#define HCI_MAX_PROTO 2
75struct hci_proto *hci_proto[HCI_MAX_PROTO];
76
77/* HCI notifiers list */
Alan Sterne041c682006-03-27 01:16:30 -080078static ATOMIC_NOTIFIER_HEAD(hci_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* ---- HCI notifications ---- */
81
82int hci_register_notifier(struct notifier_block *nb)
83{
Alan Sterne041c682006-03-27 01:16:30 -080084 return atomic_notifier_chain_register(&hci_notifier, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87int hci_unregister_notifier(struct notifier_block *nb)
88{
Alan Sterne041c682006-03-27 01:16:30 -080089 return atomic_notifier_chain_unregister(&hci_notifier, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Marcel Holtmann65164552005-10-28 19:20:48 +020092static void hci_notify(struct hci_dev *hdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Alan Sterne041c682006-03-27 01:16:30 -080094 atomic_notifier_call_chain(&hci_notifier, event, hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/* ---- HCI requests ---- */
98
Johan Hedberg23bb5762010-12-21 23:01:27 +020099void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Johan Hedberg23bb5762010-12-21 23:01:27 +0200101 BT_DBG("%s command 0x%04x result 0x%2.2x", hdev->name, cmd, result);
102
Johan Hedberga5040ef2011-01-10 13:28:59 +0200103 /* If this is the init phase check if the completed command matches
104 * the last init command, and if not just return.
105 */
106 if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd)
Johan Hedberg23bb5762010-12-21 23:01:27 +0200107 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if (hdev->req_status == HCI_REQ_PEND) {
110 hdev->req_result = result;
111 hdev->req_status = HCI_REQ_DONE;
112 wake_up_interruptible(&hdev->req_wait_q);
113 }
114}
115
116static void hci_req_cancel(struct hci_dev *hdev, int err)
117{
118 BT_DBG("%s err 0x%2.2x", hdev->name, err);
119
120 if (hdev->req_status == HCI_REQ_PEND) {
121 hdev->req_result = err;
122 hdev->req_status = HCI_REQ_CANCELED;
123 wake_up_interruptible(&hdev->req_wait_q);
124 }
125}
126
127/* Execute request and wait for completion. */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900128static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
Szymon Janc01df8c32011-02-17 16:46:47 +0100129 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 DECLARE_WAITQUEUE(wait, current);
132 int err = 0;
133
134 BT_DBG("%s start", hdev->name);
135
136 hdev->req_status = HCI_REQ_PEND;
137
138 add_wait_queue(&hdev->req_wait_q, &wait);
139 set_current_state(TASK_INTERRUPTIBLE);
140
141 req(hdev, opt);
142 schedule_timeout(timeout);
143
144 remove_wait_queue(&hdev->req_wait_q, &wait);
145
146 if (signal_pending(current))
147 return -EINTR;
148
149 switch (hdev->req_status) {
150 case HCI_REQ_DONE:
151 err = -bt_err(hdev->req_result);
152 break;
153
154 case HCI_REQ_CANCELED:
155 err = -hdev->req_result;
156 break;
157
158 default:
159 err = -ETIMEDOUT;
160 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Johan Hedberga5040ef2011-01-10 13:28:59 +0200163 hdev->req_status = hdev->req_result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 BT_DBG("%s end: err %d", hdev->name, err);
166
167 return err;
168}
169
170static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
Szymon Janc01df8c32011-02-17 16:46:47 +0100171 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int ret;
174
Marcel Holtmann7c6a3292008-09-12 03:11:54 +0200175 if (!test_bit(HCI_UP, &hdev->flags))
176 return -ENETDOWN;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* Serialize all requests */
179 hci_req_lock(hdev);
180 ret = __hci_request(hdev, req, opt, timeout);
181 hci_req_unlock(hdev);
182
183 return ret;
184}
185
186static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
187{
188 BT_DBG("%s %ld", hdev->name, opt);
189
190 /* Reset device */
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300191 set_bit(HCI_RESET, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200192 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
196{
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200197 struct hci_cp_delete_stored_link_key cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct sk_buff *skb;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -0800199 __le16 param;
Marcel Holtmann89f27832007-09-09 08:39:49 +0200200 __u8 flt_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 BT_DBG("%s %ld", hdev->name, opt);
203
204 /* Driver initialization */
205
206 /* Special commands */
207 while ((skb = skb_dequeue(&hdev->driver_init))) {
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700208 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 skb->dev = (void *) hdev;
Marcel Holtmannc78ae282009-11-18 01:02:54 +0100210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 skb_queue_tail(&hdev->cmd_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +0100212 tasklet_schedule(&hdev->cmd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 skb_queue_purge(&hdev->driver_init);
215
216 /* Mandatory initialization */
217
218 /* Reset */
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300219 if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) {
220 set_bit(HCI_RESET, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200221 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Read Local Supported Features */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200225 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200227 /* Read Local Version */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200228 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200231 hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233#if 0
234 /* Host buffer size */
235 {
236 struct hci_cp_host_buffer_size cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700237 cp.acl_mtu = cpu_to_le16(HCI_MAX_ACL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 cp.sco_mtu = HCI_MAX_SCO_SIZE;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700239 cp.acl_max_pkt = cpu_to_le16(0xffff);
240 cp.sco_max_pkt = cpu_to_le16(0xffff);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200241 hci_send_cmd(hdev, HCI_OP_HOST_BUFFER_SIZE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243#endif
244
245 /* Read BD Address */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200246 hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL);
247
248 /* Read Class of Device */
249 hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
250
251 /* Read Local Name */
252 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* Read Voice Setting */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200255 hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* Optional initialization */
258
259 /* Clear Event Filters */
Marcel Holtmann89f27832007-09-09 08:39:49 +0200260 flt_type = HCI_FLT_CLEAR_ALL;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200261 hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* Connection accept timeout ~20 secs */
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700264 param = cpu_to_le16(0x7d00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200265 hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200266
267 bacpy(&cp.bdaddr, BDADDR_ANY);
268 cp.delete_all = 1;
269 hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300272static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt)
273{
274 BT_DBG("%s", hdev->name);
275
276 /* Read LE buffer size */
277 hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static void hci_scan_req(struct hci_dev *hdev, unsigned long opt)
281{
282 __u8 scan = opt;
283
284 BT_DBG("%s %x", hdev->name, scan);
285
286 /* Inquiry and Page scans */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200287 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static void hci_auth_req(struct hci_dev *hdev, unsigned long opt)
291{
292 __u8 auth = opt;
293
294 BT_DBG("%s %x", hdev->name, auth);
295
296 /* Authentication */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200297 hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
301{
302 __u8 encrypt = opt;
303
304 BT_DBG("%s %x", hdev->name, encrypt);
305
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200306 /* Encryption */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200307 hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200310static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt)
311{
312 __le16 policy = cpu_to_le16(opt);
313
Marcel Holtmanna418b892008-11-30 12:17:28 +0100314 BT_DBG("%s %x", hdev->name, policy);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200315
316 /* Default link policy */
317 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
318}
319
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900320/* Get HCI device by index.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * Device is held on return. */
322struct hci_dev *hci_dev_get(int index)
323{
324 struct hci_dev *hdev = NULL;
325 struct list_head *p;
326
327 BT_DBG("%d", index);
328
329 if (index < 0)
330 return NULL;
331
332 read_lock(&hci_dev_list_lock);
333 list_for_each(p, &hci_dev_list) {
334 struct hci_dev *d = list_entry(p, struct hci_dev, list);
335 if (d->id == index) {
336 hdev = hci_dev_hold(d);
337 break;
338 }
339 }
340 read_unlock(&hci_dev_list_lock);
341 return hdev;
342}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344/* ---- Inquiry support ---- */
345static void inquiry_cache_flush(struct hci_dev *hdev)
346{
347 struct inquiry_cache *cache = &hdev->inq_cache;
348 struct inquiry_entry *next = cache->list, *e;
349
350 BT_DBG("cache %p", cache);
351
352 cache->list = NULL;
353 while ((e = next)) {
354 next = e->next;
355 kfree(e);
356 }
357}
358
359struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
360{
361 struct inquiry_cache *cache = &hdev->inq_cache;
362 struct inquiry_entry *e;
363
364 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
365
366 for (e = cache->list; e; e = e->next)
367 if (!bacmp(&e->data.bdaddr, bdaddr))
368 break;
369 return e;
370}
371
372void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data)
373{
374 struct inquiry_cache *cache = &hdev->inq_cache;
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200375 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr));
378
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200379 ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
380 if (!ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /* Entry not in the cache. Add new one. */
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200382 ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
383 if (!ie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return;
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200385
386 ie->next = cache->list;
387 cache->list = ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200390 memcpy(&ie->data, data, sizeof(*data));
391 ie->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 cache->timestamp = jiffies;
393}
394
395static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
396{
397 struct inquiry_cache *cache = &hdev->inq_cache;
398 struct inquiry_info *info = (struct inquiry_info *) buf;
399 struct inquiry_entry *e;
400 int copied = 0;
401
402 for (e = cache->list; e && copied < num; e = e->next, copied++) {
403 struct inquiry_data *data = &e->data;
404 bacpy(&info->bdaddr, &data->bdaddr);
405 info->pscan_rep_mode = data->pscan_rep_mode;
406 info->pscan_period_mode = data->pscan_period_mode;
407 info->pscan_mode = data->pscan_mode;
408 memcpy(info->dev_class, data->dev_class, 3);
409 info->clock_offset = data->clock_offset;
410 info++;
411 }
412
413 BT_DBG("cache %p, copied %d", cache, copied);
414 return copied;
415}
416
417static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
418{
419 struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
420 struct hci_cp_inquiry cp;
421
422 BT_DBG("%s", hdev->name);
423
424 if (test_bit(HCI_INQUIRY, &hdev->flags))
425 return;
426
427 /* Start Inquiry */
428 memcpy(&cp.lap, &ir->lap, 3);
429 cp.length = ir->length;
430 cp.num_rsp = ir->num_rsp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200431 hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434int hci_inquiry(void __user *arg)
435{
436 __u8 __user *ptr = arg;
437 struct hci_inquiry_req ir;
438 struct hci_dev *hdev;
439 int err = 0, do_inquiry = 0, max_rsp;
440 long timeo;
441 __u8 *buf;
442
443 if (copy_from_user(&ir, ptr, sizeof(ir)))
444 return -EFAULT;
445
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200446 hdev = hci_dev_get(ir.dev_id);
447 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -ENODEV;
449
450 hci_dev_lock_bh(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900451 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200452 inquiry_cache_empty(hdev) ||
453 ir.flags & IREQ_CACHE_FLUSH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 inquiry_cache_flush(hdev);
455 do_inquiry = 1;
456 }
457 hci_dev_unlock_bh(hdev);
458
Marcel Holtmann04837f62006-07-03 10:02:33 +0200459 timeo = ir.length * msecs_to_jiffies(2000);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200460
461 if (do_inquiry) {
462 err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo);
463 if (err < 0)
464 goto done;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /* for unlimited number of responses we will use buffer with 255 entries */
468 max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
469
470 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
471 * copy it to the user space.
472 */
Szymon Janc01df8c32011-02-17 16:46:47 +0100473 buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200474 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 err = -ENOMEM;
476 goto done;
477 }
478
479 hci_dev_lock_bh(hdev);
480 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
481 hci_dev_unlock_bh(hdev);
482
483 BT_DBG("num_rsp %d", ir.num_rsp);
484
485 if (!copy_to_user(ptr, &ir, sizeof(ir))) {
486 ptr += sizeof(ir);
487 if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
488 ir.num_rsp))
489 err = -EFAULT;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900490 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 err = -EFAULT;
492
493 kfree(buf);
494
495done:
496 hci_dev_put(hdev);
497 return err;
498}
499
500/* ---- HCI ioctl helpers ---- */
501
502int hci_dev_open(__u16 dev)
503{
504 struct hci_dev *hdev;
505 int ret = 0;
506
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200507 hdev = hci_dev_get(dev);
508 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return -ENODEV;
510
511 BT_DBG("%s %p", hdev->name, hdev);
512
513 hci_req_lock(hdev);
514
Marcel Holtmann611b30f2009-06-08 14:41:38 +0200515 if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) {
516 ret = -ERFKILL;
517 goto done;
518 }
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (test_bit(HCI_UP, &hdev->flags)) {
521 ret = -EALREADY;
522 goto done;
523 }
524
525 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
526 set_bit(HCI_RAW, &hdev->flags);
527
Marcel Holtmann943da252010-02-13 02:28:41 +0100528 /* Treat all non BR/EDR controllers as raw devices for now */
529 if (hdev->dev_type != HCI_BREDR)
530 set_bit(HCI_RAW, &hdev->flags);
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (hdev->open(hdev)) {
533 ret = -EIO;
534 goto done;
535 }
536
537 if (!test_bit(HCI_RAW, &hdev->flags)) {
538 atomic_set(&hdev->cmd_cnt, 1);
539 set_bit(HCI_INIT, &hdev->flags);
Johan Hedberga5040ef2011-01-10 13:28:59 +0200540 hdev->init_last_cmd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Marcel Holtmann04837f62006-07-03 10:02:33 +0200542 ret = __hci_request(hdev, hci_init_req, 0,
543 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300545 if (lmp_le_capable(hdev))
546 ret = __hci_request(hdev, hci_le_init_req, 0,
547 msecs_to_jiffies(HCI_INIT_TIMEOUT));
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 clear_bit(HCI_INIT, &hdev->flags);
550 }
551
552 if (!ret) {
553 hci_dev_hold(hdev);
554 set_bit(HCI_UP, &hdev->flags);
555 hci_notify(hdev, HCI_DEV_UP);
Johan Hedberg5add6af2010-12-16 10:00:37 +0200556 if (!test_bit(HCI_SETUP, &hdev->flags))
557 mgmt_powered(hdev->id, 1);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900558 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* Init failed, cleanup */
560 tasklet_kill(&hdev->rx_task);
561 tasklet_kill(&hdev->tx_task);
562 tasklet_kill(&hdev->cmd_task);
563
564 skb_queue_purge(&hdev->cmd_q);
565 skb_queue_purge(&hdev->rx_q);
566
567 if (hdev->flush)
568 hdev->flush(hdev);
569
570 if (hdev->sent_cmd) {
571 kfree_skb(hdev->sent_cmd);
572 hdev->sent_cmd = NULL;
573 }
574
575 hdev->close(hdev);
576 hdev->flags = 0;
577 }
578
579done:
580 hci_req_unlock(hdev);
581 hci_dev_put(hdev);
582 return ret;
583}
584
585static int hci_dev_do_close(struct hci_dev *hdev)
586{
587 BT_DBG("%s %p", hdev->name, hdev);
588
589 hci_req_cancel(hdev, ENODEV);
590 hci_req_lock(hdev);
591
592 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
Vinicius Costa Gomesb79f44c2011-04-11 18:46:55 -0300593 del_timer_sync(&hdev->cmd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 hci_req_unlock(hdev);
595 return 0;
596 }
597
598 /* Kill RX and TX tasks */
599 tasklet_kill(&hdev->rx_task);
600 tasklet_kill(&hdev->tx_task);
601
602 hci_dev_lock_bh(hdev);
603 inquiry_cache_flush(hdev);
604 hci_conn_hash_flush(hdev);
605 hci_dev_unlock_bh(hdev);
606
607 hci_notify(hdev, HCI_DEV_DOWN);
608
609 if (hdev->flush)
610 hdev->flush(hdev);
611
612 /* Reset device */
613 skb_queue_purge(&hdev->cmd_q);
614 atomic_set(&hdev->cmd_cnt, 1);
615 if (!test_bit(HCI_RAW, &hdev->flags)) {
616 set_bit(HCI_INIT, &hdev->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200617 __hci_request(hdev, hci_reset_req, 0,
618 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 clear_bit(HCI_INIT, &hdev->flags);
620 }
621
622 /* Kill cmd task */
623 tasklet_kill(&hdev->cmd_task);
624
625 /* Drop queues */
626 skb_queue_purge(&hdev->rx_q);
627 skb_queue_purge(&hdev->cmd_q);
628 skb_queue_purge(&hdev->raw_q);
629
630 /* Drop last sent command */
631 if (hdev->sent_cmd) {
Vinicius Costa Gomesb79f44c2011-04-11 18:46:55 -0300632 del_timer_sync(&hdev->cmd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 kfree_skb(hdev->sent_cmd);
634 hdev->sent_cmd = NULL;
635 }
636
637 /* After this point our queues are empty
638 * and no tasks are scheduled. */
639 hdev->close(hdev);
640
Johan Hedberg5add6af2010-12-16 10:00:37 +0200641 mgmt_powered(hdev->id, 0);
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* Clear flags */
644 hdev->flags = 0;
645
646 hci_req_unlock(hdev);
647
648 hci_dev_put(hdev);
649 return 0;
650}
651
652int hci_dev_close(__u16 dev)
653{
654 struct hci_dev *hdev;
655 int err;
656
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200657 hdev = hci_dev_get(dev);
658 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -ENODEV;
660 err = hci_dev_do_close(hdev);
661 hci_dev_put(hdev);
662 return err;
663}
664
665int hci_dev_reset(__u16 dev)
666{
667 struct hci_dev *hdev;
668 int ret = 0;
669
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200670 hdev = hci_dev_get(dev);
671 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -ENODEV;
673
674 hci_req_lock(hdev);
675 tasklet_disable(&hdev->tx_task);
676
677 if (!test_bit(HCI_UP, &hdev->flags))
678 goto done;
679
680 /* Drop queues */
681 skb_queue_purge(&hdev->rx_q);
682 skb_queue_purge(&hdev->cmd_q);
683
684 hci_dev_lock_bh(hdev);
685 inquiry_cache_flush(hdev);
686 hci_conn_hash_flush(hdev);
687 hci_dev_unlock_bh(hdev);
688
689 if (hdev->flush)
690 hdev->flush(hdev);
691
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900692 atomic_set(&hdev->cmd_cnt, 1);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300693 hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (!test_bit(HCI_RAW, &hdev->flags))
Marcel Holtmann04837f62006-07-03 10:02:33 +0200696 ret = __hci_request(hdev, hci_reset_req, 0,
697 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699done:
700 tasklet_enable(&hdev->tx_task);
701 hci_req_unlock(hdev);
702 hci_dev_put(hdev);
703 return ret;
704}
705
706int hci_dev_reset_stat(__u16 dev)
707{
708 struct hci_dev *hdev;
709 int ret = 0;
710
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200711 hdev = hci_dev_get(dev);
712 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return -ENODEV;
714
715 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
716
717 hci_dev_put(hdev);
718
719 return ret;
720}
721
722int hci_dev_cmd(unsigned int cmd, void __user *arg)
723{
724 struct hci_dev *hdev;
725 struct hci_dev_req dr;
726 int err = 0;
727
728 if (copy_from_user(&dr, arg, sizeof(dr)))
729 return -EFAULT;
730
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200731 hdev = hci_dev_get(dr.dev_id);
732 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -ENODEV;
734
735 switch (cmd) {
736 case HCISETAUTH:
Marcel Holtmann04837f62006-07-03 10:02:33 +0200737 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
738 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740
741 case HCISETENCRYPT:
742 if (!lmp_encrypt_capable(hdev)) {
743 err = -EOPNOTSUPP;
744 break;
745 }
746
747 if (!test_bit(HCI_AUTH, &hdev->flags)) {
748 /* Auth must be enabled first */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200749 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
750 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (err)
752 break;
753 }
754
Marcel Holtmann04837f62006-07-03 10:02:33 +0200755 err = hci_request(hdev, hci_encrypt_req, dr.dev_opt,
756 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758
759 case HCISETSCAN:
Marcel Holtmann04837f62006-07-03 10:02:33 +0200760 err = hci_request(hdev, hci_scan_req, dr.dev_opt,
761 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200764 case HCISETLINKPOL:
765 err = hci_request(hdev, hci_linkpol_req, dr.dev_opt,
766 msecs_to_jiffies(HCI_INIT_TIMEOUT));
767 break;
768
769 case HCISETLINKMODE:
770 hdev->link_mode = ((__u16) dr.dev_opt) &
771 (HCI_LM_MASTER | HCI_LM_ACCEPT);
772 break;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case HCISETPTYPE:
775 hdev->pkt_type = (__u16) dr.dev_opt;
776 break;
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 case HCISETACLMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200779 hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1);
780 hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 break;
782
783 case HCISETSCOMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200784 hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1);
785 hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 break;
787
788 default:
789 err = -EINVAL;
790 break;
791 }
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 hci_dev_put(hdev);
794 return err;
795}
796
797int hci_get_dev_list(void __user *arg)
798{
799 struct hci_dev_list_req *dl;
800 struct hci_dev_req *dr;
801 struct list_head *p;
802 int n = 0, size, err;
803 __u16 dev_num;
804
805 if (get_user(dev_num, (__u16 __user *) arg))
806 return -EFAULT;
807
808 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
809 return -EINVAL;
810
811 size = sizeof(*dl) + dev_num * sizeof(*dr);
812
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200813 dl = kzalloc(size, GFP_KERNEL);
814 if (!dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return -ENOMEM;
816
817 dr = dl->dev_req;
818
819 read_lock_bh(&hci_dev_list_lock);
820 list_for_each(p, &hci_dev_list) {
821 struct hci_dev *hdev;
Johan Hedbergc542a062011-01-26 13:11:03 +0200822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 hdev = list_entry(p, struct hci_dev, list);
Johan Hedbergc542a062011-01-26 13:11:03 +0200824
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200825 hci_del_off_timer(hdev);
Johan Hedbergc542a062011-01-26 13:11:03 +0200826
827 if (!test_bit(HCI_MGMT, &hdev->flags))
828 set_bit(HCI_PAIRABLE, &hdev->flags);
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 (dr + n)->dev_id = hdev->id;
831 (dr + n)->dev_opt = hdev->flags;
Johan Hedbergc542a062011-01-26 13:11:03 +0200832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (++n >= dev_num)
834 break;
835 }
836 read_unlock_bh(&hci_dev_list_lock);
837
838 dl->dev_num = n;
839 size = sizeof(*dl) + n * sizeof(*dr);
840
841 err = copy_to_user(arg, dl, size);
842 kfree(dl);
843
844 return err ? -EFAULT : 0;
845}
846
847int hci_get_dev_info(void __user *arg)
848{
849 struct hci_dev *hdev;
850 struct hci_dev_info di;
851 int err = 0;
852
853 if (copy_from_user(&di, arg, sizeof(di)))
854 return -EFAULT;
855
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200856 hdev = hci_dev_get(di.dev_id);
857 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -ENODEV;
859
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200860 hci_del_off_timer(hdev);
861
Johan Hedbergc542a062011-01-26 13:11:03 +0200862 if (!test_bit(HCI_MGMT, &hdev->flags))
863 set_bit(HCI_PAIRABLE, &hdev->flags);
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 strcpy(di.name, hdev->name);
866 di.bdaddr = hdev->bdaddr;
Marcel Holtmann943da252010-02-13 02:28:41 +0100867 di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 di.flags = hdev->flags;
869 di.pkt_type = hdev->pkt_type;
870 di.acl_mtu = hdev->acl_mtu;
871 di.acl_pkts = hdev->acl_pkts;
872 di.sco_mtu = hdev->sco_mtu;
873 di.sco_pkts = hdev->sco_pkts;
874 di.link_policy = hdev->link_policy;
875 di.link_mode = hdev->link_mode;
876
877 memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
878 memcpy(&di.features, &hdev->features, sizeof(di.features));
879
880 if (copy_to_user(arg, &di, sizeof(di)))
881 err = -EFAULT;
882
883 hci_dev_put(hdev);
884
885 return err;
886}
887
888/* ---- Interface to HCI drivers ---- */
889
Marcel Holtmann611b30f2009-06-08 14:41:38 +0200890static int hci_rfkill_set_block(void *data, bool blocked)
891{
892 struct hci_dev *hdev = data;
893
894 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
895
896 if (!blocked)
897 return 0;
898
899 hci_dev_do_close(hdev);
900
901 return 0;
902}
903
904static const struct rfkill_ops hci_rfkill_ops = {
905 .set_block = hci_rfkill_set_block,
906};
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908/* Alloc HCI device */
909struct hci_dev *hci_alloc_dev(void)
910{
911 struct hci_dev *hdev;
912
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200913 hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (!hdev)
915 return NULL;
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 skb_queue_head_init(&hdev->driver_init);
918
919 return hdev;
920}
921EXPORT_SYMBOL(hci_alloc_dev);
922
923/* Free HCI device */
924void hci_free_dev(struct hci_dev *hdev)
925{
926 skb_queue_purge(&hdev->driver_init);
927
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200928 /* will free via device release */
929 put_device(&hdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931EXPORT_SYMBOL(hci_free_dev);
932
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200933static void hci_power_on(struct work_struct *work)
934{
935 struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
936
937 BT_DBG("%s", hdev->name);
938
939 if (hci_dev_open(hdev->id) < 0)
940 return;
941
942 if (test_bit(HCI_AUTO_OFF, &hdev->flags))
943 mod_timer(&hdev->off_timer,
944 jiffies + msecs_to_jiffies(AUTO_OFF_TIMEOUT));
945
946 if (test_and_clear_bit(HCI_SETUP, &hdev->flags))
947 mgmt_index_added(hdev->id);
948}
949
950static void hci_power_off(struct work_struct *work)
951{
952 struct hci_dev *hdev = container_of(work, struct hci_dev, power_off);
953
954 BT_DBG("%s", hdev->name);
955
956 hci_dev_close(hdev->id);
957}
958
959static void hci_auto_off(unsigned long data)
960{
961 struct hci_dev *hdev = (struct hci_dev *) data;
962
963 BT_DBG("%s", hdev->name);
964
965 clear_bit(HCI_AUTO_OFF, &hdev->flags);
966
967 queue_work(hdev->workqueue, &hdev->power_off);
968}
969
970void hci_del_off_timer(struct hci_dev *hdev)
971{
972 BT_DBG("%s", hdev->name);
973
974 clear_bit(HCI_AUTO_OFF, &hdev->flags);
975 del_timer(&hdev->off_timer);
976}
977
Johan Hedberg2aeb9a12011-01-04 12:08:51 +0200978int hci_uuids_clear(struct hci_dev *hdev)
979{
980 struct list_head *p, *n;
981
982 list_for_each_safe(p, n, &hdev->uuids) {
983 struct bt_uuid *uuid;
984
985 uuid = list_entry(p, struct bt_uuid, list);
986
987 list_del(p);
988 kfree(uuid);
989 }
990
991 return 0;
992}
993
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200994int hci_link_keys_clear(struct hci_dev *hdev)
995{
996 struct list_head *p, *n;
997
998 list_for_each_safe(p, n, &hdev->link_keys) {
999 struct link_key *key;
1000
1001 key = list_entry(p, struct link_key, list);
1002
1003 list_del(p);
1004 kfree(key);
1005 }
1006
1007 return 0;
1008}
1009
1010struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1011{
1012 struct list_head *p;
1013
1014 list_for_each(p, &hdev->link_keys) {
1015 struct link_key *k;
1016
1017 k = list_entry(p, struct link_key, list);
1018
1019 if (bacmp(bdaddr, &k->bdaddr) == 0)
1020 return k;
1021 }
1022
1023 return NULL;
1024}
1025
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001026static int hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,
1027 u8 key_type, u8 old_key_type)
1028{
1029 /* Legacy key */
1030 if (key_type < 0x03)
1031 return 1;
1032
1033 /* Debug keys are insecure so don't store them persistently */
1034 if (key_type == HCI_LK_DEBUG_COMBINATION)
1035 return 0;
1036
1037 /* Changed combination key and there's no previous one */
1038 if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff)
1039 return 0;
1040
1041 /* Security mode 3 case */
1042 if (!conn)
1043 return 1;
1044
1045 /* Neither local nor remote side had no-bonding as requirement */
1046 if (conn->auth_type > 0x01 && conn->remote_auth > 0x01)
1047 return 1;
1048
1049 /* Local side had dedicated bonding as requirement */
1050 if (conn->auth_type == 0x02 || conn->auth_type == 0x03)
1051 return 1;
1052
1053 /* Remote side had dedicated bonding as requirement */
1054 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03)
1055 return 1;
1056
1057 /* If none of the above criteria match, then don't store the key
1058 * persistently */
1059 return 0;
1060}
1061
1062int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
1063 bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001064{
1065 struct link_key *key, *old_key;
Johan Hedberg4df378a2011-04-28 11:29:03 -07001066 u8 old_key_type, persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001067
1068 old_key = hci_find_link_key(hdev, bdaddr);
1069 if (old_key) {
1070 old_key_type = old_key->type;
1071 key = old_key;
1072 } else {
Johan Hedberg12adcf32011-04-28 11:29:00 -07001073 old_key_type = conn ? conn->key_type : 0xff;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001074 key = kzalloc(sizeof(*key), GFP_ATOMIC);
1075 if (!key)
1076 return -ENOMEM;
1077 list_add(&key->list, &hdev->link_keys);
1078 }
1079
1080 BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type);
1081
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001082 /* Some buggy controller combinations generate a changed
1083 * combination key for legacy pairing even when there's no
1084 * previous key */
1085 if (type == HCI_LK_CHANGED_COMBINATION &&
1086 (!conn || conn->remote_auth == 0xff) &&
Johan Hedberg655fe6e2011-04-28 11:29:01 -07001087 old_key_type == 0xff) {
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001088 type = HCI_LK_COMBINATION;
Johan Hedberg655fe6e2011-04-28 11:29:01 -07001089 if (conn)
1090 conn->key_type = type;
1091 }
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001092
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001093 bacpy(&key->bdaddr, bdaddr);
1094 memcpy(key->val, val, 16);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001095 key->pin_len = pin_len;
1096
Waldemar Rymarkiewiczb6020ba2011-04-28 12:07:53 +02001097 if (type == HCI_LK_CHANGED_COMBINATION)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001098 key->type = old_key_type;
Johan Hedberg4748fed2011-04-28 11:29:02 -07001099 else
1100 key->type = type;
1101
Johan Hedberg4df378a2011-04-28 11:29:03 -07001102 if (!new_key)
1103 return 0;
1104
1105 persistent = hci_persistent_key(hdev, conn, type, old_key_type);
1106
1107 mgmt_new_key(hdev->id, key, persistent);
1108
1109 if (!persistent) {
1110 list_del(&key->list);
1111 kfree(key);
1112 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001113
1114 return 0;
1115}
1116
1117int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1118{
1119 struct link_key *key;
1120
1121 key = hci_find_link_key(hdev, bdaddr);
1122 if (!key)
1123 return -ENOENT;
1124
1125 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1126
1127 list_del(&key->list);
1128 kfree(key);
1129
1130 return 0;
1131}
1132
Ville Tervo6bd32322011-02-16 16:32:41 +02001133/* HCI command timer function */
1134static void hci_cmd_timer(unsigned long arg)
1135{
1136 struct hci_dev *hdev = (void *) arg;
1137
1138 BT_ERR("%s command tx timeout", hdev->name);
1139 atomic_set(&hdev->cmd_cnt, 1);
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -03001140 clear_bit(HCI_RESET, &hdev->flags);
Ville Tervo6bd32322011-02-16 16:32:41 +02001141 tasklet_schedule(&hdev->cmd_task);
1142}
1143
Szymon Janc2763eda2011-03-22 13:12:22 +01001144struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
1145 bdaddr_t *bdaddr)
1146{
1147 struct oob_data *data;
1148
1149 list_for_each_entry(data, &hdev->remote_oob_data, list)
1150 if (bacmp(bdaddr, &data->bdaddr) == 0)
1151 return data;
1152
1153 return NULL;
1154}
1155
1156int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr)
1157{
1158 struct oob_data *data;
1159
1160 data = hci_find_remote_oob_data(hdev, bdaddr);
1161 if (!data)
1162 return -ENOENT;
1163
1164 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1165
1166 list_del(&data->list);
1167 kfree(data);
1168
1169 return 0;
1170}
1171
1172int hci_remote_oob_data_clear(struct hci_dev *hdev)
1173{
1174 struct oob_data *data, *n;
1175
1176 list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) {
1177 list_del(&data->list);
1178 kfree(data);
1179 }
1180
1181 return 0;
1182}
1183
1184int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
1185 u8 *randomizer)
1186{
1187 struct oob_data *data;
1188
1189 data = hci_find_remote_oob_data(hdev, bdaddr);
1190
1191 if (!data) {
1192 data = kmalloc(sizeof(*data), GFP_ATOMIC);
1193 if (!data)
1194 return -ENOMEM;
1195
1196 bacpy(&data->bdaddr, bdaddr);
1197 list_add(&data->list, &hdev->remote_oob_data);
1198 }
1199
1200 memcpy(data->hash, hash, sizeof(data->hash));
1201 memcpy(data->randomizer, randomizer, sizeof(data->randomizer));
1202
1203 BT_DBG("%s for %s", hdev->name, batostr(bdaddr));
1204
1205 return 0;
1206}
1207
Antti Julkub2a66aa2011-06-15 12:01:14 +03001208struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
1209 bdaddr_t *bdaddr)
1210{
1211 struct list_head *p;
1212
1213 list_for_each(p, &hdev->blacklist) {
1214 struct bdaddr_list *b;
1215
1216 b = list_entry(p, struct bdaddr_list, list);
1217
1218 if (bacmp(bdaddr, &b->bdaddr) == 0)
1219 return b;
1220 }
1221
1222 return NULL;
1223}
1224
1225int hci_blacklist_clear(struct hci_dev *hdev)
1226{
1227 struct list_head *p, *n;
1228
1229 list_for_each_safe(p, n, &hdev->blacklist) {
1230 struct bdaddr_list *b;
1231
1232 b = list_entry(p, struct bdaddr_list, list);
1233
1234 list_del(p);
1235 kfree(b);
1236 }
1237
1238 return 0;
1239}
1240
1241int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr)
1242{
1243 struct bdaddr_list *entry;
1244
1245 hci_dev_lock(hdev);
1246
1247 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1248 return -EBADF;
1249
1250 if (hci_blacklist_lookup(hdev, bdaddr))
1251 return -EEXIST;
1252
1253 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
1254 if (!entry)
1255 return -ENOMEM;
1256
1257 bacpy(&entry->bdaddr, bdaddr);
1258
1259 list_add(&entry->list, &hdev->blacklist);
1260
1261 hci_dev_unlock(hdev);
1262
1263 return 0;
1264}
1265
1266int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr)
1267{
1268 struct bdaddr_list *entry;
1269
1270 hci_dev_lock(hdev);
1271
1272 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1273 return hci_blacklist_clear(hdev);
1274
1275 entry = hci_blacklist_lookup(hdev, bdaddr);
1276 if (!entry)
1277 return -ENOENT;
1278
1279 list_del(&entry->list);
1280 kfree(entry);
1281
1282 hci_dev_unlock(hdev);
1283
1284 return 0;
1285}
1286
Andre Guedes35815082011-05-26 16:23:53 -03001287static void hci_clear_adv_cache(unsigned long arg)
1288{
1289 struct hci_dev *hdev = (void *) arg;
1290
1291 hci_dev_lock(hdev);
1292
1293 hci_adv_entries_clear(hdev);
1294
1295 hci_dev_unlock(hdev);
1296}
1297
Andre Guedes76c86862011-05-26 16:23:50 -03001298int hci_adv_entries_clear(struct hci_dev *hdev)
1299{
1300 struct adv_entry *entry, *tmp;
1301
1302 list_for_each_entry_safe(entry, tmp, &hdev->adv_entries, list) {
1303 list_del(&entry->list);
1304 kfree(entry);
1305 }
1306
1307 BT_DBG("%s adv cache cleared", hdev->name);
1308
1309 return 0;
1310}
1311
1312struct adv_entry *hci_find_adv_entry(struct hci_dev *hdev, bdaddr_t *bdaddr)
1313{
1314 struct adv_entry *entry;
1315
1316 list_for_each_entry(entry, &hdev->adv_entries, list)
1317 if (bacmp(bdaddr, &entry->bdaddr) == 0)
1318 return entry;
1319
1320 return NULL;
1321}
1322
1323static inline int is_connectable_adv(u8 evt_type)
1324{
1325 if (evt_type == ADV_IND || evt_type == ADV_DIRECT_IND)
1326 return 1;
1327
1328 return 0;
1329}
1330
1331int hci_add_adv_entry(struct hci_dev *hdev,
1332 struct hci_ev_le_advertising_info *ev)
1333{
1334 struct adv_entry *entry;
1335
1336 if (!is_connectable_adv(ev->evt_type))
1337 return -EINVAL;
1338
1339 /* Only new entries should be added to adv_entries. So, if
1340 * bdaddr was found, don't add it. */
1341 if (hci_find_adv_entry(hdev, &ev->bdaddr))
1342 return 0;
1343
1344 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1345 if (!entry)
1346 return -ENOMEM;
1347
1348 bacpy(&entry->bdaddr, &ev->bdaddr);
1349 entry->bdaddr_type = ev->bdaddr_type;
1350
1351 list_add(&entry->list, &hdev->adv_entries);
1352
1353 BT_DBG("%s adv entry added: address %s type %u", hdev->name,
1354 batostr(&entry->bdaddr), entry->bdaddr_type);
1355
1356 return 0;
1357}
1358
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001359static struct crypto_blkcipher *alloc_cypher(void)
1360{
1361 if (enable_smp)
1362 return crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1363
1364 return ERR_PTR(-ENOTSUPP);
1365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367/* Register HCI device */
1368int hci_register_dev(struct hci_dev *hdev)
1369{
1370 struct list_head *head = &hci_dev_list, *p;
Marcel Holtmannef222012007-07-11 06:42:04 +02001371 int i, id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Marcel Holtmannc13854c2010-02-08 15:27:07 +01001373 BT_DBG("%p name %s bus %d owner %p", hdev, hdev->name,
1374 hdev->bus, hdev->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (!hdev->open || !hdev->close || !hdev->destruct)
1377 return -EINVAL;
1378
1379 write_lock_bh(&hci_dev_list_lock);
1380
1381 /* Find first available device id */
1382 list_for_each(p, &hci_dev_list) {
1383 if (list_entry(p, struct hci_dev, list)->id != id)
1384 break;
1385 head = p; id++;
1386 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 sprintf(hdev->name, "hci%d", id);
1389 hdev->id = id;
1390 list_add(&hdev->list, head);
1391
1392 atomic_set(&hdev->refcnt, 1);
1393 spin_lock_init(&hdev->lock);
1394
1395 hdev->flags = 0;
1396 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001397 hdev->esco_type = (ESCO_HV1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 hdev->link_mode = (HCI_LM_ACCEPT);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001399 hdev->io_capability = 0x03; /* No Input No Output */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Marcel Holtmann04837f62006-07-03 10:02:33 +02001401 hdev->idle_timeout = 0;
1402 hdev->sniff_max_interval = 800;
1403 hdev->sniff_min_interval = 80;
1404
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001405 tasklet_init(&hdev->cmd_task, hci_cmd_task, (unsigned long) hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 tasklet_init(&hdev->rx_task, hci_rx_task, (unsigned long) hdev);
1407 tasklet_init(&hdev->tx_task, hci_tx_task, (unsigned long) hdev);
1408
1409 skb_queue_head_init(&hdev->rx_q);
1410 skb_queue_head_init(&hdev->cmd_q);
1411 skb_queue_head_init(&hdev->raw_q);
1412
Ville Tervo6bd32322011-02-16 16:32:41 +02001413 setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev);
1414
Suraj Sumangalacd4c5392010-07-14 13:02:16 +05301415 for (i = 0; i < NUM_REASSEMBLY; i++)
Marcel Holtmannef222012007-07-11 06:42:04 +02001416 hdev->reassembly[i] = NULL;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 init_waitqueue_head(&hdev->req_wait_q);
Thomas Gleixnera6a67ef2009-07-26 08:18:19 +00001419 mutex_init(&hdev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 inquiry_cache_init(hdev);
1422
1423 hci_conn_hash_init(hdev);
1424
David Millerea4bd8b2010-07-30 21:54:49 -07001425 INIT_LIST_HEAD(&hdev->blacklist);
Johan Hedbergf0358562010-05-18 13:20:32 +02001426
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001427 INIT_LIST_HEAD(&hdev->uuids);
1428
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001429 INIT_LIST_HEAD(&hdev->link_keys);
1430
Szymon Janc2763eda2011-03-22 13:12:22 +01001431 INIT_LIST_HEAD(&hdev->remote_oob_data);
1432
Andre Guedes76c86862011-05-26 16:23:50 -03001433 INIT_LIST_HEAD(&hdev->adv_entries);
Andre Guedes35815082011-05-26 16:23:53 -03001434 setup_timer(&hdev->adv_timer, hci_clear_adv_cache,
1435 (unsigned long) hdev);
Andre Guedes76c86862011-05-26 16:23:50 -03001436
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001437 INIT_WORK(&hdev->power_on, hci_power_on);
1438 INIT_WORK(&hdev->power_off, hci_power_off);
1439 setup_timer(&hdev->off_timer, hci_auto_off, (unsigned long) hdev);
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
1442
1443 atomic_set(&hdev->promisc, 0);
1444
1445 write_unlock_bh(&hci_dev_list_lock);
1446
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001447 hdev->workqueue = create_singlethread_workqueue(hdev->name);
1448 if (!hdev->workqueue)
1449 goto nomem;
1450
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001451 hdev->tfm = alloc_cypher();
1452 if (IS_ERR(hdev->tfm))
1453 BT_INFO("Failed to load transform for ecb(aes): %ld",
1454 PTR_ERR(hdev->tfm));
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 hci_register_sysfs(hdev);
1457
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001458 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
1459 RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, hdev);
1460 if (hdev->rfkill) {
1461 if (rfkill_register(hdev->rfkill) < 0) {
1462 rfkill_destroy(hdev->rfkill);
1463 hdev->rfkill = NULL;
1464 }
1465 }
1466
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001467 set_bit(HCI_AUTO_OFF, &hdev->flags);
1468 set_bit(HCI_SETUP, &hdev->flags);
1469 queue_work(hdev->workqueue, &hdev->power_on);
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 hci_notify(hdev, HCI_DEV_REG);
1472
1473 return id;
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001474
1475nomem:
1476 write_lock_bh(&hci_dev_list_lock);
1477 list_del(&hdev->list);
1478 write_unlock_bh(&hci_dev_list_lock);
1479
1480 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482EXPORT_SYMBOL(hci_register_dev);
1483
1484/* Unregister HCI device */
1485int hci_unregister_dev(struct hci_dev *hdev)
1486{
Marcel Holtmannef222012007-07-11 06:42:04 +02001487 int i;
1488
Marcel Holtmannc13854c2010-02-08 15:27:07 +01001489 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 write_lock_bh(&hci_dev_list_lock);
1492 list_del(&hdev->list);
1493 write_unlock_bh(&hci_dev_list_lock);
1494
1495 hci_dev_do_close(hdev);
1496
Suraj Sumangalacd4c5392010-07-14 13:02:16 +05301497 for (i = 0; i < NUM_REASSEMBLY; i++)
Marcel Holtmannef222012007-07-11 06:42:04 +02001498 kfree_skb(hdev->reassembly[i]);
1499
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001500 if (!test_bit(HCI_INIT, &hdev->flags) &&
1501 !test_bit(HCI_SETUP, &hdev->flags))
1502 mgmt_index_removed(hdev->id);
1503
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03001504 if (!IS_ERR(hdev->tfm))
1505 crypto_free_blkcipher(hdev->tfm);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 hci_notify(hdev, HCI_DEV_UNREG);
1508
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001509 if (hdev->rfkill) {
1510 rfkill_unregister(hdev->rfkill);
1511 rfkill_destroy(hdev->rfkill);
1512 }
1513
Dave Young147e2d52008-03-05 18:45:59 -08001514 hci_unregister_sysfs(hdev);
1515
Gustavo F. Padovanc6f3c5f2011-02-15 20:22:03 -03001516 hci_del_off_timer(hdev);
Andre Guedes35815082011-05-26 16:23:53 -03001517 del_timer(&hdev->adv_timer);
Gustavo F. Padovanc6f3c5f2011-02-15 20:22:03 -03001518
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001519 destroy_workqueue(hdev->workqueue);
1520
Johan Hedberge2e0cac2011-01-04 12:08:50 +02001521 hci_dev_lock_bh(hdev);
1522 hci_blacklist_clear(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001523 hci_uuids_clear(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001524 hci_link_keys_clear(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01001525 hci_remote_oob_data_clear(hdev);
Andre Guedes76c86862011-05-26 16:23:50 -03001526 hci_adv_entries_clear(hdev);
Johan Hedberge2e0cac2011-01-04 12:08:50 +02001527 hci_dev_unlock_bh(hdev);
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 __hci_dev_put(hdev);
Marcel Holtmannef222012007-07-11 06:42:04 +02001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return 0;
1532}
1533EXPORT_SYMBOL(hci_unregister_dev);
1534
1535/* Suspend HCI device */
1536int hci_suspend_dev(struct hci_dev *hdev)
1537{
1538 hci_notify(hdev, HCI_DEV_SUSPEND);
1539 return 0;
1540}
1541EXPORT_SYMBOL(hci_suspend_dev);
1542
1543/* Resume HCI device */
1544int hci_resume_dev(struct hci_dev *hdev)
1545{
1546 hci_notify(hdev, HCI_DEV_RESUME);
1547 return 0;
1548}
1549EXPORT_SYMBOL(hci_resume_dev);
1550
Marcel Holtmann76bca882009-11-18 00:40:39 +01001551/* Receive frame from HCI drivers */
1552int hci_recv_frame(struct sk_buff *skb)
1553{
1554 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
1555 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
1556 && !test_bit(HCI_INIT, &hdev->flags))) {
1557 kfree_skb(skb);
1558 return -ENXIO;
1559 }
1560
1561 /* Incomming skb */
1562 bt_cb(skb)->incoming = 1;
1563
1564 /* Time stamp */
1565 __net_timestamp(skb);
1566
1567 /* Queue frame for rx task */
1568 skb_queue_tail(&hdev->rx_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001569 tasklet_schedule(&hdev->rx_task);
1570
Marcel Holtmann76bca882009-11-18 00:40:39 +01001571 return 0;
1572}
1573EXPORT_SYMBOL(hci_recv_frame);
1574
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301575static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001576 int count, __u8 index)
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301577{
1578 int len = 0;
1579 int hlen = 0;
1580 int remain = count;
1581 struct sk_buff *skb;
1582 struct bt_skb_cb *scb;
1583
1584 if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
1585 index >= NUM_REASSEMBLY)
1586 return -EILSEQ;
1587
1588 skb = hdev->reassembly[index];
1589
1590 if (!skb) {
1591 switch (type) {
1592 case HCI_ACLDATA_PKT:
1593 len = HCI_MAX_FRAME_SIZE;
1594 hlen = HCI_ACL_HDR_SIZE;
1595 break;
1596 case HCI_EVENT_PKT:
1597 len = HCI_MAX_EVENT_SIZE;
1598 hlen = HCI_EVENT_HDR_SIZE;
1599 break;
1600 case HCI_SCODATA_PKT:
1601 len = HCI_MAX_SCO_SIZE;
1602 hlen = HCI_SCO_HDR_SIZE;
1603 break;
1604 }
1605
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001606 skb = bt_skb_alloc(len, GFP_ATOMIC);
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301607 if (!skb)
1608 return -ENOMEM;
1609
1610 scb = (void *) skb->cb;
1611 scb->expect = hlen;
1612 scb->pkt_type = type;
1613
1614 skb->dev = (void *) hdev;
1615 hdev->reassembly[index] = skb;
1616 }
1617
1618 while (count) {
1619 scb = (void *) skb->cb;
1620 len = min(scb->expect, (__u16)count);
1621
1622 memcpy(skb_put(skb, len), data, len);
1623
1624 count -= len;
1625 data += len;
1626 scb->expect -= len;
1627 remain = count;
1628
1629 switch (type) {
1630 case HCI_EVENT_PKT:
1631 if (skb->len == HCI_EVENT_HDR_SIZE) {
1632 struct hci_event_hdr *h = hci_event_hdr(skb);
1633 scb->expect = h->plen;
1634
1635 if (skb_tailroom(skb) < scb->expect) {
1636 kfree_skb(skb);
1637 hdev->reassembly[index] = NULL;
1638 return -ENOMEM;
1639 }
1640 }
1641 break;
1642
1643 case HCI_ACLDATA_PKT:
1644 if (skb->len == HCI_ACL_HDR_SIZE) {
1645 struct hci_acl_hdr *h = hci_acl_hdr(skb);
1646 scb->expect = __le16_to_cpu(h->dlen);
1647
1648 if (skb_tailroom(skb) < scb->expect) {
1649 kfree_skb(skb);
1650 hdev->reassembly[index] = NULL;
1651 return -ENOMEM;
1652 }
1653 }
1654 break;
1655
1656 case HCI_SCODATA_PKT:
1657 if (skb->len == HCI_SCO_HDR_SIZE) {
1658 struct hci_sco_hdr *h = hci_sco_hdr(skb);
1659 scb->expect = h->dlen;
1660
1661 if (skb_tailroom(skb) < scb->expect) {
1662 kfree_skb(skb);
1663 hdev->reassembly[index] = NULL;
1664 return -ENOMEM;
1665 }
1666 }
1667 break;
1668 }
1669
1670 if (scb->expect == 0) {
1671 /* Complete frame */
1672
1673 bt_cb(skb)->pkt_type = type;
1674 hci_recv_frame(skb);
1675
1676 hdev->reassembly[index] = NULL;
1677 return remain;
1678 }
1679 }
1680
1681 return remain;
1682}
1683
Marcel Holtmannef222012007-07-11 06:42:04 +02001684int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
1685{
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301686 int rem = 0;
1687
Marcel Holtmannef222012007-07-11 06:42:04 +02001688 if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
1689 return -EILSEQ;
1690
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001691 while (count) {
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001692 rem = hci_reassembly(hdev, type, data, count, type - 1);
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301693 if (rem < 0)
1694 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02001695
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301696 data += (count - rem);
1697 count = rem;
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001698 };
Marcel Holtmannef222012007-07-11 06:42:04 +02001699
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301700 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02001701}
1702EXPORT_SYMBOL(hci_recv_fragment);
1703
Suraj Sumangala99811512010-07-14 13:02:19 +05301704#define STREAM_REASSEMBLY 0
1705
1706int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
1707{
1708 int type;
1709 int rem = 0;
1710
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001711 while (count) {
Suraj Sumangala99811512010-07-14 13:02:19 +05301712 struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
1713
1714 if (!skb) {
1715 struct { char type; } *pkt;
1716
1717 /* Start of the frame */
1718 pkt = data;
1719 type = pkt->type;
1720
1721 data++;
1722 count--;
1723 } else
1724 type = bt_cb(skb)->pkt_type;
1725
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001726 rem = hci_reassembly(hdev, type, data, count,
1727 STREAM_REASSEMBLY);
Suraj Sumangala99811512010-07-14 13:02:19 +05301728 if (rem < 0)
1729 return rem;
1730
1731 data += (count - rem);
1732 count = rem;
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001733 };
Suraj Sumangala99811512010-07-14 13:02:19 +05301734
1735 return rem;
1736}
1737EXPORT_SYMBOL(hci_recv_stream_fragment);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739/* ---- Interface to upper protocols ---- */
1740
1741/* Register/Unregister protocols.
1742 * hci_task_lock is used to ensure that no tasks are running. */
1743int hci_register_proto(struct hci_proto *hp)
1744{
1745 int err = 0;
1746
1747 BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
1748
1749 if (hp->id >= HCI_MAX_PROTO)
1750 return -EINVAL;
1751
1752 write_lock_bh(&hci_task_lock);
1753
1754 if (!hci_proto[hp->id])
1755 hci_proto[hp->id] = hp;
1756 else
1757 err = -EEXIST;
1758
1759 write_unlock_bh(&hci_task_lock);
1760
1761 return err;
1762}
1763EXPORT_SYMBOL(hci_register_proto);
1764
1765int hci_unregister_proto(struct hci_proto *hp)
1766{
1767 int err = 0;
1768
1769 BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
1770
1771 if (hp->id >= HCI_MAX_PROTO)
1772 return -EINVAL;
1773
1774 write_lock_bh(&hci_task_lock);
1775
1776 if (hci_proto[hp->id])
1777 hci_proto[hp->id] = NULL;
1778 else
1779 err = -ENOENT;
1780
1781 write_unlock_bh(&hci_task_lock);
1782
1783 return err;
1784}
1785EXPORT_SYMBOL(hci_unregister_proto);
1786
1787int hci_register_cb(struct hci_cb *cb)
1788{
1789 BT_DBG("%p name %s", cb, cb->name);
1790
1791 write_lock_bh(&hci_cb_list_lock);
1792 list_add(&cb->list, &hci_cb_list);
1793 write_unlock_bh(&hci_cb_list_lock);
1794
1795 return 0;
1796}
1797EXPORT_SYMBOL(hci_register_cb);
1798
1799int hci_unregister_cb(struct hci_cb *cb)
1800{
1801 BT_DBG("%p name %s", cb, cb->name);
1802
1803 write_lock_bh(&hci_cb_list_lock);
1804 list_del(&cb->list);
1805 write_unlock_bh(&hci_cb_list_lock);
1806
1807 return 0;
1808}
1809EXPORT_SYMBOL(hci_unregister_cb);
1810
1811static int hci_send_frame(struct sk_buff *skb)
1812{
1813 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
1814
1815 if (!hdev) {
1816 kfree_skb(skb);
1817 return -ENODEV;
1818 }
1819
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001820 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 if (atomic_read(&hdev->promisc)) {
1823 /* Time stamp */
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001824 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001826 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828
1829 /* Get rid of skb owner, prior to sending to the driver. */
1830 skb_orphan(skb);
1831
1832 return hdev->send(skb);
1833}
1834
1835/* Send HCI command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001836int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
1838 int len = HCI_COMMAND_HDR_SIZE + plen;
1839 struct hci_command_hdr *hdr;
1840 struct sk_buff *skb;
1841
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001842 BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 skb = bt_skb_alloc(len, GFP_ATOMIC);
1845 if (!skb) {
Marcel Holtmannef222012007-07-11 06:42:04 +02001846 BT_ERR("%s no memory for command", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return -ENOMEM;
1848 }
1849
1850 hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001851 hdr->opcode = cpu_to_le16(opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 hdr->plen = plen;
1853
1854 if (plen)
1855 memcpy(skb_put(skb, plen), param, plen);
1856
1857 BT_DBG("skb len %d", skb->len);
1858
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001859 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 skb->dev = (void *) hdev;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001861
Johan Hedberga5040ef2011-01-10 13:28:59 +02001862 if (test_bit(HCI_INIT, &hdev->flags))
1863 hdev->init_last_cmd = opcode;
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 skb_queue_tail(&hdev->cmd_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001866 tasklet_schedule(&hdev->cmd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 return 0;
1869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871/* Get data from the previously sent command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001872void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 struct hci_command_hdr *hdr;
1875
1876 if (!hdev->sent_cmd)
1877 return NULL;
1878
1879 hdr = (void *) hdev->sent_cmd->data;
1880
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001881 if (hdr->opcode != cpu_to_le16(opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return NULL;
1883
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001884 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
1887}
1888
1889/* Send ACL data */
1890static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
1891{
1892 struct hci_acl_hdr *hdr;
1893 int len = skb->len;
1894
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001895 skb_push(skb, HCI_ACL_HDR_SIZE);
1896 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001897 hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001898 hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
1899 hdr->dlen = cpu_to_le16(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -03001902void hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct hci_dev *hdev = conn->hdev;
1905 struct sk_buff *list;
1906
1907 BT_DBG("%s conn %p flags 0x%x", hdev->name, conn, flags);
1908
1909 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001910 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02001911 hci_add_acl_hdr(skb, conn->handle, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001913 list = skb_shinfo(skb)->frag_list;
1914 if (!list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /* Non fragmented */
1916 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
1917
1918 skb_queue_tail(&conn->data_q, skb);
1919 } else {
1920 /* Fragmented */
1921 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
1922
1923 skb_shinfo(skb)->frag_list = NULL;
1924
1925 /* Queue all fragments atomically */
1926 spin_lock_bh(&conn->data_q.lock);
1927
1928 __skb_queue_tail(&conn->data_q, skb);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02001929
1930 flags &= ~ACL_START;
1931 flags |= ACL_CONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 do {
1933 skb = list; list = list->next;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001936 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02001937 hci_add_acl_hdr(skb, conn->handle, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
1940
1941 __skb_queue_tail(&conn->data_q, skb);
1942 } while (list);
1943
1944 spin_unlock_bh(&conn->data_q.lock);
1945 }
1946
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001947 tasklet_schedule(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949EXPORT_SYMBOL(hci_send_acl);
1950
1951/* Send SCO data */
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -03001952void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 struct hci_dev *hdev = conn->hdev;
1955 struct hci_sco_hdr hdr;
1956
1957 BT_DBG("%s len %d", hdev->name, skb->len);
1958
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001959 hdr.handle = cpu_to_le16(conn->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 hdr.dlen = skb->len;
1961
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001962 skb_push(skb, HCI_SCO_HDR_SIZE);
1963 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001964 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001967 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 skb_queue_tail(&conn->data_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001970 tasklet_schedule(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972EXPORT_SYMBOL(hci_send_sco);
1973
1974/* ---- HCI TX task (outgoing data) ---- */
1975
1976/* HCI Connection scheduler */
1977static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote)
1978{
1979 struct hci_conn_hash *h = &hdev->conn_hash;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001980 struct hci_conn *conn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 int num = 0, min = ~0;
1982 struct list_head *p;
1983
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001984 /* We don't have to lock device here. Connections are always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 * added and removed with TX task disabled. */
1986 list_for_each(p, &h->list) {
1987 struct hci_conn *c;
1988 c = list_entry(p, struct hci_conn, list);
1989
Marcel Holtmann769be972008-07-14 20:13:49 +02001990 if (c->type != type || skb_queue_empty(&c->data_q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 continue;
Marcel Holtmann769be972008-07-14 20:13:49 +02001992
1993 if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
1994 continue;
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 num++;
1997
1998 if (c->sent < min) {
1999 min = c->sent;
2000 conn = c;
2001 }
2002 }
2003
2004 if (conn) {
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002005 int cnt, q;
2006
2007 switch (conn->type) {
2008 case ACL_LINK:
2009 cnt = hdev->acl_cnt;
2010 break;
2011 case SCO_LINK:
2012 case ESCO_LINK:
2013 cnt = hdev->sco_cnt;
2014 break;
2015 case LE_LINK:
2016 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2017 break;
2018 default:
2019 cnt = 0;
2020 BT_ERR("Unknown link type");
2021 }
2022
2023 q = cnt / num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 *quote = q ? q : 1;
2025 } else
2026 *quote = 0;
2027
2028 BT_DBG("conn %p quote %d", conn, *quote);
2029 return conn;
2030}
2031
Ville Tervobae1f5d92011-02-10 22:38:53 -03002032static inline void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 struct hci_conn_hash *h = &hdev->conn_hash;
2035 struct list_head *p;
2036 struct hci_conn *c;
2037
Ville Tervobae1f5d92011-02-10 22:38:53 -03002038 BT_ERR("%s link tx timeout", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 /* Kill stalled connections */
2041 list_for_each(p, &h->list) {
2042 c = list_entry(p, struct hci_conn, list);
Ville Tervobae1f5d92011-02-10 22:38:53 -03002043 if (c->type == type && c->sent) {
2044 BT_ERR("%s killing stalled connection %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 hdev->name, batostr(&c->dst));
2046 hci_acl_disconn(c, 0x13);
2047 }
2048 }
2049}
2050
2051static inline void hci_sched_acl(struct hci_dev *hdev)
2052{
2053 struct hci_conn *conn;
2054 struct sk_buff *skb;
2055 int quote;
2056
2057 BT_DBG("%s", hdev->name);
2058
2059 if (!test_bit(HCI_RAW, &hdev->flags)) {
2060 /* ACL tx timeout must be longer than maximum
2061 * link supervision timeout (40.9 seconds) */
S.Çağlar Onur824530212008-02-17 23:25:57 -08002062 if (!hdev->acl_cnt && time_after(jiffies, hdev->acl_last_tx + HZ * 45))
Ville Tervobae1f5d92011-02-10 22:38:53 -03002063 hci_link_tx_to(hdev, ACL_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
2066 while (hdev->acl_cnt && (conn = hci_low_sent(hdev, ACL_LINK, &quote))) {
2067 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2068 BT_DBG("skb %p len %d", skb, skb->len);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002069
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -07002070 hci_conn_enter_active_mode(conn, bt_cb(skb)->force_active);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 hci_send_frame(skb);
2073 hdev->acl_last_tx = jiffies;
2074
2075 hdev->acl_cnt--;
2076 conn->sent++;
2077 }
2078 }
2079}
2080
2081/* Schedule SCO */
2082static inline void hci_sched_sco(struct hci_dev *hdev)
2083{
2084 struct hci_conn *conn;
2085 struct sk_buff *skb;
2086 int quote;
2087
2088 BT_DBG("%s", hdev->name);
2089
2090 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
2091 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2092 BT_DBG("skb %p len %d", skb, skb->len);
2093 hci_send_frame(skb);
2094
2095 conn->sent++;
2096 if (conn->sent == ~0)
2097 conn->sent = 0;
2098 }
2099 }
2100}
2101
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002102static inline void hci_sched_esco(struct hci_dev *hdev)
2103{
2104 struct hci_conn *conn;
2105 struct sk_buff *skb;
2106 int quote;
2107
2108 BT_DBG("%s", hdev->name);
2109
2110 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, &quote))) {
2111 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2112 BT_DBG("skb %p len %d", skb, skb->len);
2113 hci_send_frame(skb);
2114
2115 conn->sent++;
2116 if (conn->sent == ~0)
2117 conn->sent = 0;
2118 }
2119 }
2120}
2121
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002122static inline void hci_sched_le(struct hci_dev *hdev)
2123{
2124 struct hci_conn *conn;
2125 struct sk_buff *skb;
2126 int quote, cnt;
2127
2128 BT_DBG("%s", hdev->name);
2129
2130 if (!test_bit(HCI_RAW, &hdev->flags)) {
2131 /* LE tx timeout must be longer than maximum
2132 * link supervision timeout (40.9 seconds) */
Ville Tervobae1f5d92011-02-10 22:38:53 -03002133 if (!hdev->le_cnt && hdev->le_pkts &&
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002134 time_after(jiffies, hdev->le_last_tx + HZ * 45))
Ville Tervobae1f5d92011-02-10 22:38:53 -03002135 hci_link_tx_to(hdev, LE_LINK);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002136 }
2137
2138 cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
2139 while (cnt && (conn = hci_low_sent(hdev, LE_LINK, &quote))) {
2140 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2141 BT_DBG("skb %p len %d", skb, skb->len);
2142
2143 hci_send_frame(skb);
2144 hdev->le_last_tx = jiffies;
2145
2146 cnt--;
2147 conn->sent++;
2148 }
2149 }
2150 if (hdev->le_pkts)
2151 hdev->le_cnt = cnt;
2152 else
2153 hdev->acl_cnt = cnt;
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156static void hci_tx_task(unsigned long arg)
2157{
2158 struct hci_dev *hdev = (struct hci_dev *) arg;
2159 struct sk_buff *skb;
2160
2161 read_lock(&hci_task_lock);
2162
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002163 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
2164 hdev->sco_cnt, hdev->le_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 /* Schedule queues and send stuff to HCI driver */
2167
2168 hci_sched_acl(hdev);
2169
2170 hci_sched_sco(hdev);
2171
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002172 hci_sched_esco(hdev);
2173
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002174 hci_sched_le(hdev);
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /* Send next queued raw (unknown type) packet */
2177 while ((skb = skb_dequeue(&hdev->raw_q)))
2178 hci_send_frame(skb);
2179
2180 read_unlock(&hci_task_lock);
2181}
2182
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002183/* ----- HCI RX task (incoming data processing) ----- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185/* ACL data packet */
2186static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2187{
2188 struct hci_acl_hdr *hdr = (void *) skb->data;
2189 struct hci_conn *conn;
2190 __u16 handle, flags;
2191
2192 skb_pull(skb, HCI_ACL_HDR_SIZE);
2193
2194 handle = __le16_to_cpu(hdr->handle);
2195 flags = hci_flags(handle);
2196 handle = hci_handle(handle);
2197
2198 BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags);
2199
2200 hdev->stat.acl_rx++;
2201
2202 hci_dev_lock(hdev);
2203 conn = hci_conn_hash_lookup_handle(hdev, handle);
2204 hci_dev_unlock(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (conn) {
2207 register struct hci_proto *hp;
2208
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -07002209 hci_conn_enter_active_mode(conn, bt_cb(skb)->force_active);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /* Send to upper protocol */
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002212 hp = hci_proto[HCI_PROTO_L2CAP];
2213 if (hp && hp->recv_acldata) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 hp->recv_acldata(conn, skb, flags);
2215 return;
2216 }
2217 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002218 BT_ERR("%s ACL packet for unknown connection handle %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 hdev->name, handle);
2220 }
2221
2222 kfree_skb(skb);
2223}
2224
2225/* SCO data packet */
2226static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2227{
2228 struct hci_sco_hdr *hdr = (void *) skb->data;
2229 struct hci_conn *conn;
2230 __u16 handle;
2231
2232 skb_pull(skb, HCI_SCO_HDR_SIZE);
2233
2234 handle = __le16_to_cpu(hdr->handle);
2235
2236 BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle);
2237
2238 hdev->stat.sco_rx++;
2239
2240 hci_dev_lock(hdev);
2241 conn = hci_conn_hash_lookup_handle(hdev, handle);
2242 hci_dev_unlock(hdev);
2243
2244 if (conn) {
2245 register struct hci_proto *hp;
2246
2247 /* Send to upper protocol */
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002248 hp = hci_proto[HCI_PROTO_SCO];
2249 if (hp && hp->recv_scodata) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 hp->recv_scodata(conn, skb);
2251 return;
2252 }
2253 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002254 BT_ERR("%s SCO packet for unknown connection handle %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 hdev->name, handle);
2256 }
2257
2258 kfree_skb(skb);
2259}
2260
Marcel Holtmann65164552005-10-28 19:20:48 +02002261static void hci_rx_task(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 struct hci_dev *hdev = (struct hci_dev *) arg;
2264 struct sk_buff *skb;
2265
2266 BT_DBG("%s", hdev->name);
2267
2268 read_lock(&hci_task_lock);
2269
2270 while ((skb = skb_dequeue(&hdev->rx_q))) {
2271 if (atomic_read(&hdev->promisc)) {
2272 /* Send copy to the sockets */
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002273 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275
2276 if (test_bit(HCI_RAW, &hdev->flags)) {
2277 kfree_skb(skb);
2278 continue;
2279 }
2280
2281 if (test_bit(HCI_INIT, &hdev->flags)) {
2282 /* Don't process data packets in this states. */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002283 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 case HCI_ACLDATA_PKT:
2285 case HCI_SCODATA_PKT:
2286 kfree_skb(skb);
2287 continue;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290
2291 /* Process frame */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002292 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 case HCI_EVENT_PKT:
2294 hci_event_packet(hdev, skb);
2295 break;
2296
2297 case HCI_ACLDATA_PKT:
2298 BT_DBG("%s ACL data packet", hdev->name);
2299 hci_acldata_packet(hdev, skb);
2300 break;
2301
2302 case HCI_SCODATA_PKT:
2303 BT_DBG("%s SCO data packet", hdev->name);
2304 hci_scodata_packet(hdev, skb);
2305 break;
2306
2307 default:
2308 kfree_skb(skb);
2309 break;
2310 }
2311 }
2312
2313 read_unlock(&hci_task_lock);
2314}
2315
2316static void hci_cmd_task(unsigned long arg)
2317{
2318 struct hci_dev *hdev = (struct hci_dev *) arg;
2319 struct sk_buff *skb;
2320
2321 BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /* Send queued commands */
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002324 if (atomic_read(&hdev->cmd_cnt)) {
2325 skb = skb_dequeue(&hdev->cmd_q);
2326 if (!skb)
2327 return;
2328
Wei Yongjun7585b972009-02-25 18:29:52 +08002329 kfree_skb(hdev->sent_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002331 hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC);
2332 if (hdev->sent_cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 atomic_dec(&hdev->cmd_cnt);
2334 hci_send_frame(skb);
Ville Tervo6bd32322011-02-16 16:32:41 +02002335 mod_timer(&hdev->cmd_timer,
2336 jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 } else {
2338 skb_queue_head(&hdev->cmd_q, skb);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002339 tasklet_schedule(&hdev->cmd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341 }
2342}
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002343
2344module_param(enable_smp, bool, 0644);
2345MODULE_PARM_DESC(enable_smp, "Enable SMP support (LE only)");