blob: a28e637152fe1d81e8ea1fe1829bcd0800df73fd [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
Gustavo F. Padovan590051d2011-12-18 13:39:33 -02004 Copyright (C) 2011 ProFUSION Embedded Systems
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation;
11
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090016 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090021 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 SOFTWARE IS DISCLAIMED.
24*/
25
26/* Bluetooth HCI core. */
27
S.Çağlar Onur824530212008-02-17 23:25:57 -080028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/kmod.h>
31
32#include <linux/types.h>
33#include <linux/errno.h>
34#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/sched.h>
36#include <linux/slab.h>
37#include <linux/poll.h>
38#include <linux/fcntl.h>
39#include <linux/init.h>
40#include <linux/skbuff.h>
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +010041#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/interrupt.h>
43#include <linux/notifier.h>
Marcel Holtmann611b30f2009-06-08 14:41:38 +020044#include <linux/rfkill.h>
Ville Tervo6bd32322011-02-16 16:32:41 +020045#include <linux/timer.h>
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -030046#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/sock.h>
48
49#include <asm/system.h>
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020050#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/unaligned.h>
52
53#include <net/bluetooth/bluetooth.h>
54#include <net/bluetooth/hci_core.h>
55
Johan Hedbergab81cbf2010-12-15 13:53:18 +020056#define AUTO_OFF_TIMEOUT 2000
57
Fabio Estevam8b281b92012-01-10 18:33:50 -020058bool enable_hs;
Andrei Emeltchenko7784d782011-11-18 13:35:42 +020059
Marcel Holtmannb78752c2010-08-08 23:06:53 -040060static void hci_rx_work(struct work_struct *work);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -020061static void hci_cmd_work(struct work_struct *work);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -020062static void hci_tx_work(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* HCI device list */
65LIST_HEAD(hci_dev_list);
66DEFINE_RWLOCK(hci_dev_list_lock);
67
68/* HCI callback list */
69LIST_HEAD(hci_cb_list);
70DEFINE_RWLOCK(hci_cb_list_lock);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* HCI notifiers list */
Alan Sterne041c682006-03-27 01:16:30 -080073static ATOMIC_NOTIFIER_HEAD(hci_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/* ---- HCI notifications ---- */
76
77int hci_register_notifier(struct notifier_block *nb)
78{
Alan Sterne041c682006-03-27 01:16:30 -080079 return atomic_notifier_chain_register(&hci_notifier, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82int hci_unregister_notifier(struct notifier_block *nb)
83{
Alan Sterne041c682006-03-27 01:16:30 -080084 return atomic_notifier_chain_unregister(&hci_notifier, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Marcel Holtmann65164552005-10-28 19:20:48 +020087static void hci_notify(struct hci_dev *hdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Alan Sterne041c682006-03-27 01:16:30 -080089 atomic_notifier_call_chain(&hci_notifier, event, hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92/* ---- HCI requests ---- */
93
Johan Hedberg23bb5762010-12-21 23:01:27 +020094void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Johan Hedberg23bb5762010-12-21 23:01:27 +020096 BT_DBG("%s command 0x%04x result 0x%2.2x", hdev->name, cmd, result);
97
Johan Hedberga5040ef2011-01-10 13:28:59 +020098 /* If this is the init phase check if the completed command matches
99 * the last init command, and if not just return.
100 */
101 if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd)
Johan Hedberg23bb5762010-12-21 23:01:27 +0200102 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (hdev->req_status == HCI_REQ_PEND) {
105 hdev->req_result = result;
106 hdev->req_status = HCI_REQ_DONE;
107 wake_up_interruptible(&hdev->req_wait_q);
108 }
109}
110
111static void hci_req_cancel(struct hci_dev *hdev, int err)
112{
113 BT_DBG("%s err 0x%2.2x", hdev->name, err);
114
115 if (hdev->req_status == HCI_REQ_PEND) {
116 hdev->req_result = err;
117 hdev->req_status = HCI_REQ_CANCELED;
118 wake_up_interruptible(&hdev->req_wait_q);
119 }
120}
121
122/* Execute request and wait for completion. */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900123static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
Szymon Janc01df8c32011-02-17 16:46:47 +0100124 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 DECLARE_WAITQUEUE(wait, current);
127 int err = 0;
128
129 BT_DBG("%s start", hdev->name);
130
131 hdev->req_status = HCI_REQ_PEND;
132
133 add_wait_queue(&hdev->req_wait_q, &wait);
134 set_current_state(TASK_INTERRUPTIBLE);
135
136 req(hdev, opt);
137 schedule_timeout(timeout);
138
139 remove_wait_queue(&hdev->req_wait_q, &wait);
140
141 if (signal_pending(current))
142 return -EINTR;
143
144 switch (hdev->req_status) {
145 case HCI_REQ_DONE:
Joe Perchese1750722011-06-29 18:18:29 -0700146 err = -bt_to_errno(hdev->req_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 break;
148
149 case HCI_REQ_CANCELED:
150 err = -hdev->req_result;
151 break;
152
153 default:
154 err = -ETIMEDOUT;
155 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Johan Hedberga5040ef2011-01-10 13:28:59 +0200158 hdev->req_status = hdev->req_result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 BT_DBG("%s end: err %d", hdev->name, err);
161
162 return err;
163}
164
165static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
Szymon Janc01df8c32011-02-17 16:46:47 +0100166 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 int ret;
169
Marcel Holtmann7c6a3292008-09-12 03:11:54 +0200170 if (!test_bit(HCI_UP, &hdev->flags))
171 return -ENETDOWN;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* Serialize all requests */
174 hci_req_lock(hdev);
175 ret = __hci_request(hdev, req, opt, timeout);
176 hci_req_unlock(hdev);
177
178 return ret;
179}
180
181static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
182{
183 BT_DBG("%s %ld", hdev->name, opt);
184
185 /* Reset device */
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300186 set_bit(HCI_RESET, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200187 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200190static void bredr_init(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200192 struct hci_cp_delete_stored_link_key cp;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -0800193 __le16 param;
Marcel Holtmann89f27832007-09-09 08:39:49 +0200194 __u8 flt_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Andrei Emeltchenko2455a3e2011-12-19 16:31:28 +0200196 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Mandatory initialization */
199
200 /* Reset */
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300201 if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) {
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200202 set_bit(HCI_RESET, &hdev->flags);
203 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Read Local Supported Features */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200207 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200209 /* Read Local Version */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200210 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200213 hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Read BD Address */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200216 hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL);
217
218 /* Read Class of Device */
219 hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
220
221 /* Read Local Name */
222 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Read Voice Setting */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200225 hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* Optional initialization */
228
229 /* Clear Event Filters */
Marcel Holtmann89f27832007-09-09 08:39:49 +0200230 flt_type = HCI_FLT_CLEAR_ALL;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200231 hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* Connection accept timeout ~20 secs */
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700234 param = cpu_to_le16(0x7d00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200235 hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200236
237 bacpy(&cp.bdaddr, BDADDR_ANY);
238 cp.delete_all = 1;
239 hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200242static void amp_init(struct hci_dev *hdev)
243{
Andrei Emeltchenko2455a3e2011-12-19 16:31:28 +0200244 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
245
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200246 /* Reset */
247 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
248
249 /* Read Local Version */
250 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
251}
252
253static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
254{
255 struct sk_buff *skb;
256
257 BT_DBG("%s %ld", hdev->name, opt);
258
259 /* Driver initialization */
260
261 /* Special commands */
262 while ((skb = skb_dequeue(&hdev->driver_init))) {
263 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
264 skb->dev = (void *) hdev;
265
266 skb_queue_tail(&hdev->cmd_q, skb);
267 queue_work(hdev->workqueue, &hdev->cmd_work);
268 }
269 skb_queue_purge(&hdev->driver_init);
270
271 switch (hdev->dev_type) {
272 case HCI_BREDR:
273 bredr_init(hdev);
274 break;
275
276 case HCI_AMP:
277 amp_init(hdev);
278 break;
279
280 default:
281 BT_ERR("Unknown device type %d", hdev->dev_type);
282 break;
283 }
284
285}
286
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300287static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt)
288{
289 BT_DBG("%s", hdev->name);
290
291 /* Read LE buffer size */
292 hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static void hci_scan_req(struct hci_dev *hdev, unsigned long opt)
296{
297 __u8 scan = opt;
298
299 BT_DBG("%s %x", hdev->name, scan);
300
301 /* Inquiry and Page scans */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200302 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static void hci_auth_req(struct hci_dev *hdev, unsigned long opt)
306{
307 __u8 auth = opt;
308
309 BT_DBG("%s %x", hdev->name, auth);
310
311 /* Authentication */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200312 hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
316{
317 __u8 encrypt = opt;
318
319 BT_DBG("%s %x", hdev->name, encrypt);
320
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200321 /* Encryption */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200322 hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200325static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt)
326{
327 __le16 policy = cpu_to_le16(opt);
328
Marcel Holtmanna418b892008-11-30 12:17:28 +0100329 BT_DBG("%s %x", hdev->name, policy);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200330
331 /* Default link policy */
332 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
333}
334
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900335/* Get HCI device by index.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * Device is held on return. */
337struct hci_dev *hci_dev_get(int index)
338{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200339 struct hci_dev *hdev = NULL, *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 BT_DBG("%d", index);
342
343 if (index < 0)
344 return NULL;
345
346 read_lock(&hci_dev_list_lock);
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200347 list_for_each_entry(d, &hci_dev_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (d->id == index) {
349 hdev = hci_dev_hold(d);
350 break;
351 }
352 }
353 read_unlock(&hci_dev_list_lock);
354 return hdev;
355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357/* ---- Inquiry support ---- */
Johan Hedbergff9ef572012-01-04 14:23:45 +0200358
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200359bool hci_discovery_active(struct hci_dev *hdev)
360{
361 struct discovery_state *discov = &hdev->discovery;
362
363 if (discov->state == DISCOVERY_INQUIRY ||
364 discov->state == DISCOVERY_RESOLVING)
365 return true;
366
367 return false;
368}
369
Johan Hedbergff9ef572012-01-04 14:23:45 +0200370void hci_discovery_set_state(struct hci_dev *hdev, int state)
371{
372 BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
373
374 if (hdev->discovery.state == state)
375 return;
376
377 switch (state) {
378 case DISCOVERY_STOPPED:
379 mgmt_discovering(hdev, 0);
380 break;
381 case DISCOVERY_STARTING:
382 break;
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200383 case DISCOVERY_INQUIRY:
Johan Hedbergff9ef572012-01-04 14:23:45 +0200384 mgmt_discovering(hdev, 1);
385 break;
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200386 case DISCOVERY_RESOLVING:
387 break;
Johan Hedbergff9ef572012-01-04 14:23:45 +0200388 case DISCOVERY_STOPPING:
389 break;
390 }
391
392 hdev->discovery.state = state;
393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static void inquiry_cache_flush(struct hci_dev *hdev)
396{
Johan Hedberg30883512012-01-04 14:16:21 +0200397 struct discovery_state *cache = &hdev->discovery;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200398 struct inquiry_entry *p, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Johan Hedberg561aafb2012-01-04 13:31:59 +0200400 list_for_each_entry_safe(p, n, &cache->all, all) {
401 list_del(&p->all);
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200402 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Johan Hedberg561aafb2012-01-04 13:31:59 +0200404
405 INIT_LIST_HEAD(&cache->unknown);
406 INIT_LIST_HEAD(&cache->resolve);
Johan Hedbergff9ef572012-01-04 14:23:45 +0200407 cache->state = DISCOVERY_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
411{
Johan Hedberg30883512012-01-04 14:16:21 +0200412 struct discovery_state *cache = &hdev->discovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct inquiry_entry *e;
414
415 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
416
Johan Hedberg561aafb2012-01-04 13:31:59 +0200417 list_for_each_entry(e, &cache->all, all) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (!bacmp(&e->data.bdaddr, bdaddr))
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200419 return e;
420 }
421
422 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Johan Hedberg561aafb2012-01-04 13:31:59 +0200425struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
426 bdaddr_t *bdaddr)
427{
Johan Hedberg30883512012-01-04 14:16:21 +0200428 struct discovery_state *cache = &hdev->discovery;
Johan Hedberg561aafb2012-01-04 13:31:59 +0200429 struct inquiry_entry *e;
430
431 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
432
433 list_for_each_entry(e, &cache->unknown, list) {
434 if (!bacmp(&e->data.bdaddr, bdaddr))
435 return e;
436 }
437
438 return NULL;
439}
440
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200441struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
442 bdaddr_t *bdaddr,
443 int state)
444{
445 struct discovery_state *cache = &hdev->discovery;
446 struct inquiry_entry *e;
447
448 BT_DBG("cache %p bdaddr %s state %d", cache, batostr(bdaddr), state);
449
450 list_for_each_entry(e, &cache->resolve, list) {
451 if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state)
452 return e;
453 if (!bacmp(&e->data.bdaddr, bdaddr))
454 return e;
455 }
456
457 return NULL;
458}
459
Johan Hedberga3d4e202012-01-09 00:53:02 +0200460void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
461 struct inquiry_entry *ie)
462{
463 struct discovery_state *cache = &hdev->discovery;
464 struct list_head *pos = &cache->resolve;
465 struct inquiry_entry *p;
466
467 list_del(&ie->list);
468
469 list_for_each_entry(p, &cache->resolve, list) {
470 if (p->name_state != NAME_PENDING &&
471 abs(p->data.rssi) >= abs(ie->data.rssi))
472 break;
473 pos = &p->list;
474 }
475
476 list_add(&ie->list, pos);
477}
478
Johan Hedberg31754052012-01-04 13:39:52 +0200479bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
Johan Hedberg561aafb2012-01-04 13:31:59 +0200480 bool name_known)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Johan Hedberg30883512012-01-04 14:16:21 +0200482 struct discovery_state *cache = &hdev->discovery;
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200483 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr));
486
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200487 ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
Johan Hedberga3d4e202012-01-09 00:53:02 +0200488 if (ie) {
489 if (ie->name_state == NAME_NEEDED &&
490 data->rssi != ie->data.rssi) {
491 ie->data.rssi = data->rssi;
492 hci_inquiry_cache_update_resolve(hdev, ie);
493 }
494
Johan Hedberg561aafb2012-01-04 13:31:59 +0200495 goto update;
Johan Hedberga3d4e202012-01-09 00:53:02 +0200496 }
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200497
Johan Hedberg561aafb2012-01-04 13:31:59 +0200498 /* Entry not in the cache. Add new one. */
499 ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
500 if (!ie)
Johan Hedberg31754052012-01-04 13:39:52 +0200501 return false;
Johan Hedberg561aafb2012-01-04 13:31:59 +0200502
503 list_add(&ie->all, &cache->all);
504
505 if (name_known) {
506 ie->name_state = NAME_KNOWN;
507 } else {
508 ie->name_state = NAME_NOT_KNOWN;
509 list_add(&ie->list, &cache->unknown);
510 }
511
512update:
513 if (name_known && ie->name_state != NAME_KNOWN &&
514 ie->name_state != NAME_PENDING) {
515 ie->name_state = NAME_KNOWN;
516 list_del(&ie->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200519 memcpy(&ie->data, data, sizeof(*data));
520 ie->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 cache->timestamp = jiffies;
Johan Hedberg31754052012-01-04 13:39:52 +0200522
523 if (ie->name_state == NAME_NOT_KNOWN)
524 return false;
525
526 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
530{
Johan Hedberg30883512012-01-04 14:16:21 +0200531 struct discovery_state *cache = &hdev->discovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct inquiry_info *info = (struct inquiry_info *) buf;
533 struct inquiry_entry *e;
534 int copied = 0;
535
Johan Hedberg561aafb2012-01-04 13:31:59 +0200536 list_for_each_entry(e, &cache->all, all) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct inquiry_data *data = &e->data;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200538
539 if (copied >= num)
540 break;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 bacpy(&info->bdaddr, &data->bdaddr);
543 info->pscan_rep_mode = data->pscan_rep_mode;
544 info->pscan_period_mode = data->pscan_period_mode;
545 info->pscan_mode = data->pscan_mode;
546 memcpy(info->dev_class, data->dev_class, 3);
547 info->clock_offset = data->clock_offset;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 info++;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200550 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
553 BT_DBG("cache %p, copied %d", cache, copied);
554 return copied;
555}
556
557static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
558{
559 struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
560 struct hci_cp_inquiry cp;
561
562 BT_DBG("%s", hdev->name);
563
564 if (test_bit(HCI_INQUIRY, &hdev->flags))
565 return;
566
567 /* Start Inquiry */
568 memcpy(&cp.lap, &ir->lap, 3);
569 cp.length = ir->length;
570 cp.num_rsp = ir->num_rsp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200571 hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574int hci_inquiry(void __user *arg)
575{
576 __u8 __user *ptr = arg;
577 struct hci_inquiry_req ir;
578 struct hci_dev *hdev;
579 int err = 0, do_inquiry = 0, max_rsp;
580 long timeo;
581 __u8 *buf;
582
583 if (copy_from_user(&ir, ptr, sizeof(ir)))
584 return -EFAULT;
585
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200586 hdev = hci_dev_get(ir.dev_id);
587 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return -ENODEV;
589
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300590 hci_dev_lock(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900591 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200592 inquiry_cache_empty(hdev) ||
593 ir.flags & IREQ_CACHE_FLUSH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 inquiry_cache_flush(hdev);
595 do_inquiry = 1;
596 }
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300597 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Marcel Holtmann04837f62006-07-03 10:02:33 +0200599 timeo = ir.length * msecs_to_jiffies(2000);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200600
601 if (do_inquiry) {
602 err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo);
603 if (err < 0)
604 goto done;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* for unlimited number of responses we will use buffer with 255 entries */
608 max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
609
610 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
611 * copy it to the user space.
612 */
Szymon Janc01df8c32011-02-17 16:46:47 +0100613 buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200614 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 err = -ENOMEM;
616 goto done;
617 }
618
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300619 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300621 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 BT_DBG("num_rsp %d", ir.num_rsp);
624
625 if (!copy_to_user(ptr, &ir, sizeof(ir))) {
626 ptr += sizeof(ir);
627 if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
628 ir.num_rsp))
629 err = -EFAULT;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900630 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 err = -EFAULT;
632
633 kfree(buf);
634
635done:
636 hci_dev_put(hdev);
637 return err;
638}
639
640/* ---- HCI ioctl helpers ---- */
641
642int hci_dev_open(__u16 dev)
643{
644 struct hci_dev *hdev;
645 int ret = 0;
646
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200647 hdev = hci_dev_get(dev);
648 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -ENODEV;
650
651 BT_DBG("%s %p", hdev->name, hdev);
652
653 hci_req_lock(hdev);
654
Marcel Holtmann611b30f2009-06-08 14:41:38 +0200655 if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) {
656 ret = -ERFKILL;
657 goto done;
658 }
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (test_bit(HCI_UP, &hdev->flags)) {
661 ret = -EALREADY;
662 goto done;
663 }
664
665 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
666 set_bit(HCI_RAW, &hdev->flags);
667
Andrei Emeltchenko07e3b942011-11-11 17:02:15 +0200668 /* Treat all non BR/EDR controllers as raw devices if
669 enable_hs is not set */
670 if (hdev->dev_type != HCI_BREDR && !enable_hs)
Marcel Holtmann943da252010-02-13 02:28:41 +0100671 set_bit(HCI_RAW, &hdev->flags);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (hdev->open(hdev)) {
674 ret = -EIO;
675 goto done;
676 }
677
678 if (!test_bit(HCI_RAW, &hdev->flags)) {
679 atomic_set(&hdev->cmd_cnt, 1);
680 set_bit(HCI_INIT, &hdev->flags);
Johan Hedberga5040ef2011-01-10 13:28:59 +0200681 hdev->init_last_cmd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Marcel Holtmann04837f62006-07-03 10:02:33 +0200683 ret = __hci_request(hdev, hci_init_req, 0,
684 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Andre Guedeseead27d2011-06-30 19:20:55 -0300686 if (lmp_host_le_capable(hdev))
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300687 ret = __hci_request(hdev, hci_le_init_req, 0,
688 msecs_to_jiffies(HCI_INIT_TIMEOUT));
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 clear_bit(HCI_INIT, &hdev->flags);
691 }
692
693 if (!ret) {
694 hci_dev_hold(hdev);
695 set_bit(HCI_UP, &hdev->flags);
696 hci_notify(hdev, HCI_DEV_UP);
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200697 if (!test_bit(HCI_SETUP, &hdev->dev_flags)) {
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300698 hci_dev_lock(hdev);
Johan Hedberg744cf192011-11-08 20:40:14 +0200699 mgmt_powered(hdev, 1);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300700 hci_dev_unlock(hdev);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200701 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900702 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* Init failed, cleanup */
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -0200704 flush_work(&hdev->tx_work);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -0200705 flush_work(&hdev->cmd_work);
Marcel Holtmannb78752c2010-08-08 23:06:53 -0400706 flush_work(&hdev->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 skb_queue_purge(&hdev->cmd_q);
709 skb_queue_purge(&hdev->rx_q);
710
711 if (hdev->flush)
712 hdev->flush(hdev);
713
714 if (hdev->sent_cmd) {
715 kfree_skb(hdev->sent_cmd);
716 hdev->sent_cmd = NULL;
717 }
718
719 hdev->close(hdev);
720 hdev->flags = 0;
721 }
722
723done:
724 hci_req_unlock(hdev);
725 hci_dev_put(hdev);
726 return ret;
727}
728
729static int hci_dev_do_close(struct hci_dev *hdev)
730{
731 BT_DBG("%s %p", hdev->name, hdev);
732
733 hci_req_cancel(hdev, ENODEV);
734 hci_req_lock(hdev);
735
736 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
Vinicius Costa Gomesb79f44c2011-04-11 18:46:55 -0300737 del_timer_sync(&hdev->cmd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 hci_req_unlock(hdev);
739 return 0;
740 }
741
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -0200742 /* Flush RX and TX works */
743 flush_work(&hdev->tx_work);
Marcel Holtmannb78752c2010-08-08 23:06:53 -0400744 flush_work(&hdev->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Johan Hedberg16ab91a2011-11-07 22:16:02 +0200746 if (hdev->discov_timeout > 0) {
Johan Hedberge0f93092011-11-09 01:44:22 +0200747 cancel_delayed_work(&hdev->discov_off);
Johan Hedberg16ab91a2011-11-07 22:16:02 +0200748 hdev->discov_timeout = 0;
749 }
750
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200751 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Johan Hedberge0f93092011-11-09 01:44:22 +0200752 cancel_delayed_work(&hdev->power_off);
Johan Hedberg32435532011-11-07 22:16:04 +0200753
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200754 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg7d785252011-12-15 00:47:39 +0200755 cancel_delayed_work(&hdev->service_cache);
756
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300757 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 inquiry_cache_flush(hdev);
759 hci_conn_hash_flush(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300760 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 hci_notify(hdev, HCI_DEV_DOWN);
763
764 if (hdev->flush)
765 hdev->flush(hdev);
766
767 /* Reset device */
768 skb_queue_purge(&hdev->cmd_q);
769 atomic_set(&hdev->cmd_cnt, 1);
770 if (!test_bit(HCI_RAW, &hdev->flags)) {
771 set_bit(HCI_INIT, &hdev->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200772 __hci_request(hdev, hci_reset_req, 0,
Gustavo F. Padovancad44c22011-12-23 18:59:13 -0200773 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 clear_bit(HCI_INIT, &hdev->flags);
775 }
776
Gustavo F. Padovanc347b762011-12-14 23:53:47 -0200777 /* flush cmd work */
778 flush_work(&hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* Drop queues */
781 skb_queue_purge(&hdev->rx_q);
782 skb_queue_purge(&hdev->cmd_q);
783 skb_queue_purge(&hdev->raw_q);
784
785 /* Drop last sent command */
786 if (hdev->sent_cmd) {
Vinicius Costa Gomesb79f44c2011-04-11 18:46:55 -0300787 del_timer_sync(&hdev->cmd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 kfree_skb(hdev->sent_cmd);
789 hdev->sent_cmd = NULL;
790 }
791
792 /* After this point our queues are empty
793 * and no tasks are scheduled. */
794 hdev->close(hdev);
795
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300796 hci_dev_lock(hdev);
Johan Hedberg744cf192011-11-08 20:40:14 +0200797 mgmt_powered(hdev, 0);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300798 hci_dev_unlock(hdev);
Johan Hedberg5add6af2010-12-16 10:00:37 +0200799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Clear flags */
801 hdev->flags = 0;
802
803 hci_req_unlock(hdev);
804
805 hci_dev_put(hdev);
806 return 0;
807}
808
809int hci_dev_close(__u16 dev)
810{
811 struct hci_dev *hdev;
812 int err;
813
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200814 hdev = hci_dev_get(dev);
815 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -ENODEV;
817 err = hci_dev_do_close(hdev);
818 hci_dev_put(hdev);
819 return err;
820}
821
822int hci_dev_reset(__u16 dev)
823{
824 struct hci_dev *hdev;
825 int ret = 0;
826
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200827 hdev = hci_dev_get(dev);
828 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -ENODEV;
830
831 hci_req_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (!test_bit(HCI_UP, &hdev->flags))
834 goto done;
835
836 /* Drop queues */
837 skb_queue_purge(&hdev->rx_q);
838 skb_queue_purge(&hdev->cmd_q);
839
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300840 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 inquiry_cache_flush(hdev);
842 hci_conn_hash_flush(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300843 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if (hdev->flush)
846 hdev->flush(hdev);
847
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900848 atomic_set(&hdev->cmd_cnt, 1);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300849 hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 if (!test_bit(HCI_RAW, &hdev->flags))
Marcel Holtmann04837f62006-07-03 10:02:33 +0200852 ret = __hci_request(hdev, hci_reset_req, 0,
853 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 hci_req_unlock(hdev);
857 hci_dev_put(hdev);
858 return ret;
859}
860
861int hci_dev_reset_stat(__u16 dev)
862{
863 struct hci_dev *hdev;
864 int ret = 0;
865
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200866 hdev = hci_dev_get(dev);
867 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return -ENODEV;
869
870 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
871
872 hci_dev_put(hdev);
873
874 return ret;
875}
876
877int hci_dev_cmd(unsigned int cmd, void __user *arg)
878{
879 struct hci_dev *hdev;
880 struct hci_dev_req dr;
881 int err = 0;
882
883 if (copy_from_user(&dr, arg, sizeof(dr)))
884 return -EFAULT;
885
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200886 hdev = hci_dev_get(dr.dev_id);
887 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -ENODEV;
889
890 switch (cmd) {
891 case HCISETAUTH:
Marcel Holtmann04837f62006-07-03 10:02:33 +0200892 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
893 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 break;
895
896 case HCISETENCRYPT:
897 if (!lmp_encrypt_capable(hdev)) {
898 err = -EOPNOTSUPP;
899 break;
900 }
901
902 if (!test_bit(HCI_AUTH, &hdev->flags)) {
903 /* Auth must be enabled first */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200904 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
905 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (err)
907 break;
908 }
909
Marcel Holtmann04837f62006-07-03 10:02:33 +0200910 err = hci_request(hdev, hci_encrypt_req, dr.dev_opt,
911 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
913
914 case HCISETSCAN:
Marcel Holtmann04837f62006-07-03 10:02:33 +0200915 err = hci_request(hdev, hci_scan_req, dr.dev_opt,
916 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 break;
918
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200919 case HCISETLINKPOL:
920 err = hci_request(hdev, hci_linkpol_req, dr.dev_opt,
921 msecs_to_jiffies(HCI_INIT_TIMEOUT));
922 break;
923
924 case HCISETLINKMODE:
925 hdev->link_mode = ((__u16) dr.dev_opt) &
926 (HCI_LM_MASTER | HCI_LM_ACCEPT);
927 break;
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 case HCISETPTYPE:
930 hdev->pkt_type = (__u16) dr.dev_opt;
931 break;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 case HCISETACLMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200934 hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1);
935 hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 break;
937
938 case HCISETSCOMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200939 hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1);
940 hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942
943 default:
944 err = -EINVAL;
945 break;
946 }
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 hci_dev_put(hdev);
949 return err;
950}
951
952int hci_get_dev_list(void __user *arg)
953{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200954 struct hci_dev *hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct hci_dev_list_req *dl;
956 struct hci_dev_req *dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int n = 0, size, err;
958 __u16 dev_num;
959
960 if (get_user(dev_num, (__u16 __user *) arg))
961 return -EFAULT;
962
963 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
964 return -EINVAL;
965
966 size = sizeof(*dl) + dev_num * sizeof(*dr);
967
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200968 dl = kzalloc(size, GFP_KERNEL);
969 if (!dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return -ENOMEM;
971
972 dr = dl->dev_req;
973
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -0200974 read_lock(&hci_dev_list_lock);
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200975 list_for_each_entry(hdev, &hci_dev_list, list) {
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200976 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Johan Hedberge0f93092011-11-09 01:44:22 +0200977 cancel_delayed_work(&hdev->power_off);
Johan Hedbergc542a062011-01-26 13:11:03 +0200978
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200979 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
980 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergc542a062011-01-26 13:11:03 +0200981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 (dr + n)->dev_id = hdev->id;
983 (dr + n)->dev_opt = hdev->flags;
Johan Hedbergc542a062011-01-26 13:11:03 +0200984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (++n >= dev_num)
986 break;
987 }
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -0200988 read_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 dl->dev_num = n;
991 size = sizeof(*dl) + n * sizeof(*dr);
992
993 err = copy_to_user(arg, dl, size);
994 kfree(dl);
995
996 return err ? -EFAULT : 0;
997}
998
999int hci_get_dev_info(void __user *arg)
1000{
1001 struct hci_dev *hdev;
1002 struct hci_dev_info di;
1003 int err = 0;
1004
1005 if (copy_from_user(&di, arg, sizeof(di)))
1006 return -EFAULT;
1007
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001008 hdev = hci_dev_get(di.dev_id);
1009 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -ENODEV;
1011
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001012 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Johan Hedberg32435532011-11-07 22:16:04 +02001013 cancel_delayed_work_sync(&hdev->power_off);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001014
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001015 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
1016 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergc542a062011-01-26 13:11:03 +02001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 strcpy(di.name, hdev->name);
1019 di.bdaddr = hdev->bdaddr;
Marcel Holtmann943da252010-02-13 02:28:41 +01001020 di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 di.flags = hdev->flags;
1022 di.pkt_type = hdev->pkt_type;
1023 di.acl_mtu = hdev->acl_mtu;
1024 di.acl_pkts = hdev->acl_pkts;
1025 di.sco_mtu = hdev->sco_mtu;
1026 di.sco_pkts = hdev->sco_pkts;
1027 di.link_policy = hdev->link_policy;
1028 di.link_mode = hdev->link_mode;
1029
1030 memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
1031 memcpy(&di.features, &hdev->features, sizeof(di.features));
1032
1033 if (copy_to_user(arg, &di, sizeof(di)))
1034 err = -EFAULT;
1035
1036 hci_dev_put(hdev);
1037
1038 return err;
1039}
1040
1041/* ---- Interface to HCI drivers ---- */
1042
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001043static int hci_rfkill_set_block(void *data, bool blocked)
1044{
1045 struct hci_dev *hdev = data;
1046
1047 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
1048
1049 if (!blocked)
1050 return 0;
1051
1052 hci_dev_do_close(hdev);
1053
1054 return 0;
1055}
1056
1057static const struct rfkill_ops hci_rfkill_ops = {
1058 .set_block = hci_rfkill_set_block,
1059};
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/* Alloc HCI device */
1062struct hci_dev *hci_alloc_dev(void)
1063{
1064 struct hci_dev *hdev;
1065
Marcel Holtmann25ea6db2006-07-06 15:40:09 +02001066 hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!hdev)
1068 return NULL;
1069
David Herrmann0ac7e702011-10-08 14:58:47 +02001070 hci_init_sysfs(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 skb_queue_head_init(&hdev->driver_init);
1072
1073 return hdev;
1074}
1075EXPORT_SYMBOL(hci_alloc_dev);
1076
1077/* Free HCI device */
1078void hci_free_dev(struct hci_dev *hdev)
1079{
1080 skb_queue_purge(&hdev->driver_init);
1081
Marcel Holtmanna91f2e32006-07-03 10:02:41 +02001082 /* will free via device release */
1083 put_device(&hdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085EXPORT_SYMBOL(hci_free_dev);
1086
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001087static void hci_power_on(struct work_struct *work)
1088{
1089 struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
1090
1091 BT_DBG("%s", hdev->name);
1092
1093 if (hci_dev_open(hdev->id) < 0)
1094 return;
1095
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001096 if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Gustavo F. Padovan80b7ab32011-12-17 14:52:27 -02001097 schedule_delayed_work(&hdev->power_off,
Johan Hedberg32435532011-11-07 22:16:04 +02001098 msecs_to_jiffies(AUTO_OFF_TIMEOUT));
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001099
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001100 if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +02001101 mgmt_index_added(hdev);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001102}
1103
1104static void hci_power_off(struct work_struct *work)
1105{
Johan Hedberg32435532011-11-07 22:16:04 +02001106 struct hci_dev *hdev = container_of(work, struct hci_dev,
1107 power_off.work);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001108
1109 BT_DBG("%s", hdev->name);
1110
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001111 clear_bit(HCI_AUTO_OFF, &hdev->dev_flags);
Johan Hedberg32435532011-11-07 22:16:04 +02001112
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001113 hci_dev_close(hdev->id);
1114}
1115
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001116static void hci_discov_off(struct work_struct *work)
1117{
1118 struct hci_dev *hdev;
1119 u8 scan = SCAN_PAGE;
1120
1121 hdev = container_of(work, struct hci_dev, discov_off.work);
1122
1123 BT_DBG("%s", hdev->name);
1124
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001125 hci_dev_lock(hdev);
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001126
1127 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
1128
1129 hdev->discov_timeout = 0;
1130
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001131 hci_dev_unlock(hdev);
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001132}
1133
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001134int hci_uuids_clear(struct hci_dev *hdev)
1135{
1136 struct list_head *p, *n;
1137
1138 list_for_each_safe(p, n, &hdev->uuids) {
1139 struct bt_uuid *uuid;
1140
1141 uuid = list_entry(p, struct bt_uuid, list);
1142
1143 list_del(p);
1144 kfree(uuid);
1145 }
1146
1147 return 0;
1148}
1149
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001150int hci_link_keys_clear(struct hci_dev *hdev)
1151{
1152 struct list_head *p, *n;
1153
1154 list_for_each_safe(p, n, &hdev->link_keys) {
1155 struct link_key *key;
1156
1157 key = list_entry(p, struct link_key, list);
1158
1159 list_del(p);
1160 kfree(key);
1161 }
1162
1163 return 0;
1164}
1165
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001166int hci_smp_ltks_clear(struct hci_dev *hdev)
1167{
1168 struct smp_ltk *k, *tmp;
1169
1170 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1171 list_del(&k->list);
1172 kfree(k);
1173 }
1174
1175 return 0;
1176}
1177
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001178struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1179{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001180 struct link_key *k;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001181
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001182 list_for_each_entry(k, &hdev->link_keys, list)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001183 if (bacmp(bdaddr, &k->bdaddr) == 0)
1184 return k;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001185
1186 return NULL;
1187}
1188
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001189static int hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,
1190 u8 key_type, u8 old_key_type)
1191{
1192 /* Legacy key */
1193 if (key_type < 0x03)
1194 return 1;
1195
1196 /* Debug keys are insecure so don't store them persistently */
1197 if (key_type == HCI_LK_DEBUG_COMBINATION)
1198 return 0;
1199
1200 /* Changed combination key and there's no previous one */
1201 if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff)
1202 return 0;
1203
1204 /* Security mode 3 case */
1205 if (!conn)
1206 return 1;
1207
1208 /* Neither local nor remote side had no-bonding as requirement */
1209 if (conn->auth_type > 0x01 && conn->remote_auth > 0x01)
1210 return 1;
1211
1212 /* Local side had dedicated bonding as requirement */
1213 if (conn->auth_type == 0x02 || conn->auth_type == 0x03)
1214 return 1;
1215
1216 /* Remote side had dedicated bonding as requirement */
1217 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03)
1218 return 1;
1219
1220 /* If none of the above criteria match, then don't store the key
1221 * persistently */
1222 return 0;
1223}
1224
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001225struct link_key *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8])
1226{
1227 struct link_key *k;
1228
1229 list_for_each_entry(k, &hdev->link_keys, list) {
1230 struct key_master_id *id;
1231
1232 if (k->type != HCI_LK_SMP_LTK)
1233 continue;
1234
1235 if (k->dlen != sizeof(*id))
1236 continue;
1237
1238 id = (void *) &k->data;
1239 if (id->ediv == ediv &&
1240 (memcmp(rand, id->rand, sizeof(id->rand)) == 0))
1241 return k;
1242 }
1243
1244 return NULL;
1245}
1246EXPORT_SYMBOL(hci_find_ltk);
1247
1248struct link_key *hci_find_link_key_type(struct hci_dev *hdev,
1249 bdaddr_t *bdaddr, u8 type)
1250{
1251 struct link_key *k;
1252
1253 list_for_each_entry(k, &hdev->link_keys, list)
1254 if (k->type == type && bacmp(bdaddr, &k->bdaddr) == 0)
1255 return k;
1256
1257 return NULL;
1258}
1259EXPORT_SYMBOL(hci_find_link_key_type);
1260
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001261int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
1262 bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001263{
1264 struct link_key *key, *old_key;
Johan Hedberg4df378a2011-04-28 11:29:03 -07001265 u8 old_key_type, persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001266
1267 old_key = hci_find_link_key(hdev, bdaddr);
1268 if (old_key) {
1269 old_key_type = old_key->type;
1270 key = old_key;
1271 } else {
Johan Hedberg12adcf32011-04-28 11:29:00 -07001272 old_key_type = conn ? conn->key_type : 0xff;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001273 key = kzalloc(sizeof(*key), GFP_ATOMIC);
1274 if (!key)
1275 return -ENOMEM;
1276 list_add(&key->list, &hdev->link_keys);
1277 }
1278
1279 BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type);
1280
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001281 /* Some buggy controller combinations generate a changed
1282 * combination key for legacy pairing even when there's no
1283 * previous key */
1284 if (type == HCI_LK_CHANGED_COMBINATION &&
1285 (!conn || conn->remote_auth == 0xff) &&
Johan Hedberg655fe6e2011-04-28 11:29:01 -07001286 old_key_type == 0xff) {
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001287 type = HCI_LK_COMBINATION;
Johan Hedberg655fe6e2011-04-28 11:29:01 -07001288 if (conn)
1289 conn->key_type = type;
1290 }
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001291
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001292 bacpy(&key->bdaddr, bdaddr);
1293 memcpy(key->val, val, 16);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001294 key->pin_len = pin_len;
1295
Waldemar Rymarkiewiczb6020ba2011-04-28 12:07:53 +02001296 if (type == HCI_LK_CHANGED_COMBINATION)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001297 key->type = old_key_type;
Johan Hedberg4748fed2011-04-28 11:29:02 -07001298 else
1299 key->type = type;
1300
Johan Hedberg4df378a2011-04-28 11:29:03 -07001301 if (!new_key)
1302 return 0;
1303
1304 persistent = hci_persistent_key(hdev, conn, type, old_key_type);
1305
Johan Hedberg744cf192011-11-08 20:40:14 +02001306 mgmt_new_link_key(hdev, key, persistent);
Johan Hedberg4df378a2011-04-28 11:29:03 -07001307
1308 if (!persistent) {
1309 list_del(&key->list);
1310 kfree(key);
1311 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001312
1313 return 0;
1314}
1315
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001316int hci_add_ltk(struct hci_dev *hdev, int new_key, bdaddr_t *bdaddr,
Vinicius Costa Gomes726b4ff2011-07-08 18:31:45 -03001317 u8 key_size, __le16 ediv, u8 rand[8], u8 ltk[16])
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001318{
1319 struct link_key *key, *old_key;
1320 struct key_master_id *id;
1321 u8 old_key_type;
1322
1323 BT_DBG("%s addr %s", hdev->name, batostr(bdaddr));
1324
1325 old_key = hci_find_link_key_type(hdev, bdaddr, HCI_LK_SMP_LTK);
1326 if (old_key) {
1327 key = old_key;
1328 old_key_type = old_key->type;
1329 } else {
1330 key = kzalloc(sizeof(*key) + sizeof(*id), GFP_ATOMIC);
1331 if (!key)
1332 return -ENOMEM;
1333 list_add(&key->list, &hdev->link_keys);
1334 old_key_type = 0xff;
1335 }
1336
1337 key->dlen = sizeof(*id);
1338
1339 bacpy(&key->bdaddr, bdaddr);
1340 memcpy(key->val, ltk, sizeof(key->val));
1341 key->type = HCI_LK_SMP_LTK;
Vinicius Costa Gomes726b4ff2011-07-08 18:31:45 -03001342 key->pin_len = key_size;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001343
1344 id = (void *) &key->data;
1345 id->ediv = ediv;
1346 memcpy(id->rand, rand, sizeof(id->rand));
1347
1348 if (new_key)
Johan Hedberg744cf192011-11-08 20:40:14 +02001349 mgmt_new_link_key(hdev, key, old_key_type);
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001350
1351 return 0;
1352}
1353
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001354int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1355{
1356 struct link_key *key;
1357
1358 key = hci_find_link_key(hdev, bdaddr);
1359 if (!key)
1360 return -ENOENT;
1361
1362 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1363
1364 list_del(&key->list);
1365 kfree(key);
1366
1367 return 0;
1368}
1369
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001370int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr)
1371{
1372 struct smp_ltk *k, *tmp;
1373
1374 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1375 if (bacmp(bdaddr, &k->bdaddr))
1376 continue;
1377
1378 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1379
1380 list_del(&k->list);
1381 kfree(k);
1382 }
1383
1384 return 0;
1385}
1386
Ville Tervo6bd32322011-02-16 16:32:41 +02001387/* HCI command timer function */
1388static void hci_cmd_timer(unsigned long arg)
1389{
1390 struct hci_dev *hdev = (void *) arg;
1391
1392 BT_ERR("%s command tx timeout", hdev->name);
1393 atomic_set(&hdev->cmd_cnt, 1);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02001394 queue_work(hdev->workqueue, &hdev->cmd_work);
Ville Tervo6bd32322011-02-16 16:32:41 +02001395}
1396
Szymon Janc2763eda2011-03-22 13:12:22 +01001397struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
1398 bdaddr_t *bdaddr)
1399{
1400 struct oob_data *data;
1401
1402 list_for_each_entry(data, &hdev->remote_oob_data, list)
1403 if (bacmp(bdaddr, &data->bdaddr) == 0)
1404 return data;
1405
1406 return NULL;
1407}
1408
1409int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr)
1410{
1411 struct oob_data *data;
1412
1413 data = hci_find_remote_oob_data(hdev, bdaddr);
1414 if (!data)
1415 return -ENOENT;
1416
1417 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1418
1419 list_del(&data->list);
1420 kfree(data);
1421
1422 return 0;
1423}
1424
1425int hci_remote_oob_data_clear(struct hci_dev *hdev)
1426{
1427 struct oob_data *data, *n;
1428
1429 list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) {
1430 list_del(&data->list);
1431 kfree(data);
1432 }
1433
1434 return 0;
1435}
1436
1437int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
1438 u8 *randomizer)
1439{
1440 struct oob_data *data;
1441
1442 data = hci_find_remote_oob_data(hdev, bdaddr);
1443
1444 if (!data) {
1445 data = kmalloc(sizeof(*data), GFP_ATOMIC);
1446 if (!data)
1447 return -ENOMEM;
1448
1449 bacpy(&data->bdaddr, bdaddr);
1450 list_add(&data->list, &hdev->remote_oob_data);
1451 }
1452
1453 memcpy(data->hash, hash, sizeof(data->hash));
1454 memcpy(data->randomizer, randomizer, sizeof(data->randomizer));
1455
1456 BT_DBG("%s for %s", hdev->name, batostr(bdaddr));
1457
1458 return 0;
1459}
1460
Antti Julkub2a66aa2011-06-15 12:01:14 +03001461struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
1462 bdaddr_t *bdaddr)
1463{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001464 struct bdaddr_list *b;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001465
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001466 list_for_each_entry(b, &hdev->blacklist, list)
Antti Julkub2a66aa2011-06-15 12:01:14 +03001467 if (bacmp(bdaddr, &b->bdaddr) == 0)
1468 return b;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001469
1470 return NULL;
1471}
1472
1473int hci_blacklist_clear(struct hci_dev *hdev)
1474{
1475 struct list_head *p, *n;
1476
1477 list_for_each_safe(p, n, &hdev->blacklist) {
1478 struct bdaddr_list *b;
1479
1480 b = list_entry(p, struct bdaddr_list, list);
1481
1482 list_del(p);
1483 kfree(b);
1484 }
1485
1486 return 0;
1487}
1488
1489int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr)
1490{
1491 struct bdaddr_list *entry;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001492
1493 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1494 return -EBADF;
1495
Antti Julku5e762442011-08-25 16:48:02 +03001496 if (hci_blacklist_lookup(hdev, bdaddr))
1497 return -EEXIST;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001498
1499 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
Antti Julku5e762442011-08-25 16:48:02 +03001500 if (!entry)
1501 return -ENOMEM;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001502
1503 bacpy(&entry->bdaddr, bdaddr);
1504
1505 list_add(&entry->list, &hdev->blacklist);
1506
Johan Hedberg744cf192011-11-08 20:40:14 +02001507 return mgmt_device_blocked(hdev, bdaddr);
Antti Julkub2a66aa2011-06-15 12:01:14 +03001508}
1509
1510int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr)
1511{
1512 struct bdaddr_list *entry;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001513
Szymon Janc1ec918c2011-11-16 09:32:21 +01001514 if (bacmp(bdaddr, BDADDR_ANY) == 0)
Antti Julku5e762442011-08-25 16:48:02 +03001515 return hci_blacklist_clear(hdev);
Antti Julkub2a66aa2011-06-15 12:01:14 +03001516
1517 entry = hci_blacklist_lookup(hdev, bdaddr);
Szymon Janc1ec918c2011-11-16 09:32:21 +01001518 if (!entry)
Antti Julku5e762442011-08-25 16:48:02 +03001519 return -ENOENT;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001520
1521 list_del(&entry->list);
1522 kfree(entry);
1523
Johan Hedberg744cf192011-11-08 20:40:14 +02001524 return mgmt_device_unblocked(hdev, bdaddr);
Antti Julkub2a66aa2011-06-15 12:01:14 +03001525}
1526
Gustavo F. Padovandb323f22011-06-20 16:39:29 -03001527static void hci_clear_adv_cache(struct work_struct *work)
Andre Guedes35815082011-05-26 16:23:53 -03001528{
Gustavo F. Padovandb323f22011-06-20 16:39:29 -03001529 struct hci_dev *hdev = container_of(work, struct hci_dev,
1530 adv_work.work);
Andre Guedes35815082011-05-26 16:23:53 -03001531
1532 hci_dev_lock(hdev);
1533
1534 hci_adv_entries_clear(hdev);
1535
1536 hci_dev_unlock(hdev);
1537}
1538
Andre Guedes76c86862011-05-26 16:23:50 -03001539int hci_adv_entries_clear(struct hci_dev *hdev)
1540{
1541 struct adv_entry *entry, *tmp;
1542
1543 list_for_each_entry_safe(entry, tmp, &hdev->adv_entries, list) {
1544 list_del(&entry->list);
1545 kfree(entry);
1546 }
1547
1548 BT_DBG("%s adv cache cleared", hdev->name);
1549
1550 return 0;
1551}
1552
1553struct adv_entry *hci_find_adv_entry(struct hci_dev *hdev, bdaddr_t *bdaddr)
1554{
1555 struct adv_entry *entry;
1556
1557 list_for_each_entry(entry, &hdev->adv_entries, list)
1558 if (bacmp(bdaddr, &entry->bdaddr) == 0)
1559 return entry;
1560
1561 return NULL;
1562}
1563
1564static inline int is_connectable_adv(u8 evt_type)
1565{
1566 if (evt_type == ADV_IND || evt_type == ADV_DIRECT_IND)
1567 return 1;
1568
1569 return 0;
1570}
1571
1572int hci_add_adv_entry(struct hci_dev *hdev,
1573 struct hci_ev_le_advertising_info *ev)
1574{
1575 struct adv_entry *entry;
1576
1577 if (!is_connectable_adv(ev->evt_type))
1578 return -EINVAL;
1579
1580 /* Only new entries should be added to adv_entries. So, if
1581 * bdaddr was found, don't add it. */
1582 if (hci_find_adv_entry(hdev, &ev->bdaddr))
1583 return 0;
1584
Andre Guedes4777bfd2012-01-30 23:31:28 -03001585 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Andre Guedes76c86862011-05-26 16:23:50 -03001586 if (!entry)
1587 return -ENOMEM;
1588
1589 bacpy(&entry->bdaddr, &ev->bdaddr);
1590 entry->bdaddr_type = ev->bdaddr_type;
1591
1592 list_add(&entry->list, &hdev->adv_entries);
1593
1594 BT_DBG("%s adv entry added: address %s type %u", hdev->name,
1595 batostr(&entry->bdaddr), entry->bdaddr_type);
1596
1597 return 0;
1598}
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600/* Register HCI device */
1601int hci_register_dev(struct hci_dev *hdev)
1602{
1603 struct list_head *head = &hci_dev_list, *p;
Mat Martineau08add512011-11-02 16:18:36 -07001604 int i, id, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
David Herrmanne9b9cfa2012-01-07 15:47:22 +01001606 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
David Herrmann010666a2012-01-07 15:47:07 +01001608 if (!hdev->open || !hdev->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return -EINVAL;
1610
Mat Martineau08add512011-11-02 16:18:36 -07001611 /* Do not allow HCI_AMP devices to register at index 0,
1612 * so the index can be used as the AMP controller ID.
1613 */
1614 id = (hdev->dev_type == HCI_BREDR) ? 0 : 1;
1615
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001616 write_lock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 /* Find first available device id */
1619 list_for_each(p, &hci_dev_list) {
1620 if (list_entry(p, struct hci_dev, list)->id != id)
1621 break;
1622 head = p; id++;
1623 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 sprintf(hdev->name, "hci%d", id);
1626 hdev->id = id;
Andrei Emeltchenkoc6feeb22011-11-16 17:30:20 +02001627 list_add_tail(&hdev->list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001629 mutex_init(&hdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 hdev->flags = 0;
Andre Guedesd23264a2011-11-25 20:53:38 -03001632 hdev->dev_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001634 hdev->esco_type = (ESCO_HV1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 hdev->link_mode = (HCI_LM_ACCEPT);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001636 hdev->io_capability = 0x03; /* No Input No Output */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Marcel Holtmann04837f62006-07-03 10:02:33 +02001638 hdev->idle_timeout = 0;
1639 hdev->sniff_max_interval = 800;
1640 hdev->sniff_min_interval = 80;
1641
Marcel Holtmannb78752c2010-08-08 23:06:53 -04001642 INIT_WORK(&hdev->rx_work, hci_rx_work);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02001643 INIT_WORK(&hdev->cmd_work, hci_cmd_work);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02001644 INIT_WORK(&hdev->tx_work, hci_tx_work);
Marcel Holtmannb78752c2010-08-08 23:06:53 -04001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 skb_queue_head_init(&hdev->rx_q);
1648 skb_queue_head_init(&hdev->cmd_q);
1649 skb_queue_head_init(&hdev->raw_q);
1650
Ville Tervo6bd32322011-02-16 16:32:41 +02001651 setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev);
1652
Suraj Sumangalacd4c5392010-07-14 13:02:16 +05301653 for (i = 0; i < NUM_REASSEMBLY; i++)
Marcel Holtmannef222012007-07-11 06:42:04 +02001654 hdev->reassembly[i] = NULL;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 init_waitqueue_head(&hdev->req_wait_q);
Thomas Gleixnera6a67ef2009-07-26 08:18:19 +00001657 mutex_init(&hdev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Johan Hedberg30883512012-01-04 14:16:21 +02001659 discovery_init(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 hci_conn_hash_init(hdev);
1662
Johan Hedberg2e58ef32011-11-08 20:40:15 +02001663 INIT_LIST_HEAD(&hdev->mgmt_pending);
1664
David Millerea4bd8b2010-07-30 21:54:49 -07001665 INIT_LIST_HEAD(&hdev->blacklist);
Johan Hedbergf0358562010-05-18 13:20:32 +02001666
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001667 INIT_LIST_HEAD(&hdev->uuids);
1668
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001669 INIT_LIST_HEAD(&hdev->link_keys);
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001670 INIT_LIST_HEAD(&hdev->long_term_keys);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001671
Szymon Janc2763eda2011-03-22 13:12:22 +01001672 INIT_LIST_HEAD(&hdev->remote_oob_data);
1673
Andre Guedes76c86862011-05-26 16:23:50 -03001674 INIT_LIST_HEAD(&hdev->adv_entries);
1675
Gustavo F. Padovandb323f22011-06-20 16:39:29 -03001676 INIT_DELAYED_WORK(&hdev->adv_work, hci_clear_adv_cache);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001677 INIT_WORK(&hdev->power_on, hci_power_on);
Johan Hedberg32435532011-11-07 22:16:04 +02001678 INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001679
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001680 INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
1683
1684 atomic_set(&hdev->promisc, 0);
1685
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001686 write_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Gustavo F. Padovan32845eb2011-12-17 17:47:30 -02001688 hdev->workqueue = alloc_workqueue(hdev->name, WQ_HIGHPRI | WQ_UNBOUND |
1689 WQ_MEM_RECLAIM, 1);
David Herrmann33ca9542011-10-08 14:58:49 +02001690 if (!hdev->workqueue) {
1691 error = -ENOMEM;
1692 goto err;
1693 }
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001694
David Herrmann33ca9542011-10-08 14:58:49 +02001695 error = hci_add_sysfs(hdev);
1696 if (error < 0)
1697 goto err_wqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001699 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
1700 RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, hdev);
1701 if (hdev->rfkill) {
1702 if (rfkill_register(hdev->rfkill) < 0) {
1703 rfkill_destroy(hdev->rfkill);
1704 hdev->rfkill = NULL;
1705 }
1706 }
1707
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001708 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
1709 set_bit(HCI_SETUP, &hdev->dev_flags);
Gustavo F. Padovan7f971042011-12-18 12:40:32 -02001710 schedule_work(&hdev->power_on);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 hci_notify(hdev, HCI_DEV_REG);
David Herrmanndc946bd2012-01-07 15:47:24 +01001713 hci_dev_hold(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 return id;
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001716
David Herrmann33ca9542011-10-08 14:58:49 +02001717err_wqueue:
1718 destroy_workqueue(hdev->workqueue);
1719err:
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001720 write_lock(&hci_dev_list_lock);
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001721 list_del(&hdev->list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001722 write_unlock(&hci_dev_list_lock);
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001723
David Herrmann33ca9542011-10-08 14:58:49 +02001724 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726EXPORT_SYMBOL(hci_register_dev);
1727
1728/* Unregister HCI device */
David Herrmann59735632011-10-26 10:43:19 +02001729void hci_unregister_dev(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Marcel Holtmannef222012007-07-11 06:42:04 +02001731 int i;
1732
Marcel Holtmannc13854c2010-02-08 15:27:07 +01001733 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001735 write_lock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 list_del(&hdev->list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001737 write_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 hci_dev_do_close(hdev);
1740
Suraj Sumangalacd4c5392010-07-14 13:02:16 +05301741 for (i = 0; i < NUM_REASSEMBLY; i++)
Marcel Holtmannef222012007-07-11 06:42:04 +02001742 kfree_skb(hdev->reassembly[i]);
1743
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001744 if (!test_bit(HCI_INIT, &hdev->flags) &&
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001745 !test_bit(HCI_SETUP, &hdev->dev_flags)) {
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001746 hci_dev_lock(hdev);
Johan Hedberg744cf192011-11-08 20:40:14 +02001747 mgmt_index_removed(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001748 hci_dev_unlock(hdev);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001749 }
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001750
Johan Hedberg2e58ef32011-11-08 20:40:15 +02001751 /* mgmt_index_removed should take care of emptying the
1752 * pending list */
1753 BUG_ON(!list_empty(&hdev->mgmt_pending));
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 hci_notify(hdev, HCI_DEV_UNREG);
1756
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001757 if (hdev->rfkill) {
1758 rfkill_unregister(hdev->rfkill);
1759 rfkill_destroy(hdev->rfkill);
1760 }
1761
David Herrmannce242972011-10-08 14:58:48 +02001762 hci_del_sysfs(hdev);
Dave Young147e2d52008-03-05 18:45:59 -08001763
Gustavo F. Padovandb323f22011-06-20 16:39:29 -03001764 cancel_delayed_work_sync(&hdev->adv_work);
Gustavo F. Padovanc6f3c5f2011-02-15 20:22:03 -03001765
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001766 destroy_workqueue(hdev->workqueue);
1767
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001768 hci_dev_lock(hdev);
Johan Hedberge2e0cac2011-01-04 12:08:50 +02001769 hci_blacklist_clear(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001770 hci_uuids_clear(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001771 hci_link_keys_clear(hdev);
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001772 hci_smp_ltks_clear(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01001773 hci_remote_oob_data_clear(hdev);
Andre Guedes76c86862011-05-26 16:23:50 -03001774 hci_adv_entries_clear(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001775 hci_dev_unlock(hdev);
Johan Hedberge2e0cac2011-01-04 12:08:50 +02001776
David Herrmanndc946bd2012-01-07 15:47:24 +01001777 hci_dev_put(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779EXPORT_SYMBOL(hci_unregister_dev);
1780
1781/* Suspend HCI device */
1782int hci_suspend_dev(struct hci_dev *hdev)
1783{
1784 hci_notify(hdev, HCI_DEV_SUSPEND);
1785 return 0;
1786}
1787EXPORT_SYMBOL(hci_suspend_dev);
1788
1789/* Resume HCI device */
1790int hci_resume_dev(struct hci_dev *hdev)
1791{
1792 hci_notify(hdev, HCI_DEV_RESUME);
1793 return 0;
1794}
1795EXPORT_SYMBOL(hci_resume_dev);
1796
Marcel Holtmann76bca882009-11-18 00:40:39 +01001797/* Receive frame from HCI drivers */
1798int hci_recv_frame(struct sk_buff *skb)
1799{
1800 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
1801 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
1802 && !test_bit(HCI_INIT, &hdev->flags))) {
1803 kfree_skb(skb);
1804 return -ENXIO;
1805 }
1806
1807 /* Incomming skb */
1808 bt_cb(skb)->incoming = 1;
1809
1810 /* Time stamp */
1811 __net_timestamp(skb);
1812
Marcel Holtmann76bca882009-11-18 00:40:39 +01001813 skb_queue_tail(&hdev->rx_q, skb);
Marcel Holtmannb78752c2010-08-08 23:06:53 -04001814 queue_work(hdev->workqueue, &hdev->rx_work);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001815
Marcel Holtmann76bca882009-11-18 00:40:39 +01001816 return 0;
1817}
1818EXPORT_SYMBOL(hci_recv_frame);
1819
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301820static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001821 int count, __u8 index)
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301822{
1823 int len = 0;
1824 int hlen = 0;
1825 int remain = count;
1826 struct sk_buff *skb;
1827 struct bt_skb_cb *scb;
1828
1829 if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
1830 index >= NUM_REASSEMBLY)
1831 return -EILSEQ;
1832
1833 skb = hdev->reassembly[index];
1834
1835 if (!skb) {
1836 switch (type) {
1837 case HCI_ACLDATA_PKT:
1838 len = HCI_MAX_FRAME_SIZE;
1839 hlen = HCI_ACL_HDR_SIZE;
1840 break;
1841 case HCI_EVENT_PKT:
1842 len = HCI_MAX_EVENT_SIZE;
1843 hlen = HCI_EVENT_HDR_SIZE;
1844 break;
1845 case HCI_SCODATA_PKT:
1846 len = HCI_MAX_SCO_SIZE;
1847 hlen = HCI_SCO_HDR_SIZE;
1848 break;
1849 }
1850
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001851 skb = bt_skb_alloc(len, GFP_ATOMIC);
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301852 if (!skb)
1853 return -ENOMEM;
1854
1855 scb = (void *) skb->cb;
1856 scb->expect = hlen;
1857 scb->pkt_type = type;
1858
1859 skb->dev = (void *) hdev;
1860 hdev->reassembly[index] = skb;
1861 }
1862
1863 while (count) {
1864 scb = (void *) skb->cb;
1865 len = min(scb->expect, (__u16)count);
1866
1867 memcpy(skb_put(skb, len), data, len);
1868
1869 count -= len;
1870 data += len;
1871 scb->expect -= len;
1872 remain = count;
1873
1874 switch (type) {
1875 case HCI_EVENT_PKT:
1876 if (skb->len == HCI_EVENT_HDR_SIZE) {
1877 struct hci_event_hdr *h = hci_event_hdr(skb);
1878 scb->expect = h->plen;
1879
1880 if (skb_tailroom(skb) < scb->expect) {
1881 kfree_skb(skb);
1882 hdev->reassembly[index] = NULL;
1883 return -ENOMEM;
1884 }
1885 }
1886 break;
1887
1888 case HCI_ACLDATA_PKT:
1889 if (skb->len == HCI_ACL_HDR_SIZE) {
1890 struct hci_acl_hdr *h = hci_acl_hdr(skb);
1891 scb->expect = __le16_to_cpu(h->dlen);
1892
1893 if (skb_tailroom(skb) < scb->expect) {
1894 kfree_skb(skb);
1895 hdev->reassembly[index] = NULL;
1896 return -ENOMEM;
1897 }
1898 }
1899 break;
1900
1901 case HCI_SCODATA_PKT:
1902 if (skb->len == HCI_SCO_HDR_SIZE) {
1903 struct hci_sco_hdr *h = hci_sco_hdr(skb);
1904 scb->expect = h->dlen;
1905
1906 if (skb_tailroom(skb) < scb->expect) {
1907 kfree_skb(skb);
1908 hdev->reassembly[index] = NULL;
1909 return -ENOMEM;
1910 }
1911 }
1912 break;
1913 }
1914
1915 if (scb->expect == 0) {
1916 /* Complete frame */
1917
1918 bt_cb(skb)->pkt_type = type;
1919 hci_recv_frame(skb);
1920
1921 hdev->reassembly[index] = NULL;
1922 return remain;
1923 }
1924 }
1925
1926 return remain;
1927}
1928
Marcel Holtmannef222012007-07-11 06:42:04 +02001929int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
1930{
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301931 int rem = 0;
1932
Marcel Holtmannef222012007-07-11 06:42:04 +02001933 if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
1934 return -EILSEQ;
1935
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001936 while (count) {
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001937 rem = hci_reassembly(hdev, type, data, count, type - 1);
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301938 if (rem < 0)
1939 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02001940
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301941 data += (count - rem);
1942 count = rem;
Joe Perchesf81c6222011-06-03 11:51:19 +00001943 }
Marcel Holtmannef222012007-07-11 06:42:04 +02001944
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05301945 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02001946}
1947EXPORT_SYMBOL(hci_recv_fragment);
1948
Suraj Sumangala99811512010-07-14 13:02:19 +05301949#define STREAM_REASSEMBLY 0
1950
1951int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
1952{
1953 int type;
1954 int rem = 0;
1955
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03001956 while (count) {
Suraj Sumangala99811512010-07-14 13:02:19 +05301957 struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
1958
1959 if (!skb) {
1960 struct { char type; } *pkt;
1961
1962 /* Start of the frame */
1963 pkt = data;
1964 type = pkt->type;
1965
1966 data++;
1967 count--;
1968 } else
1969 type = bt_cb(skb)->pkt_type;
1970
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001971 rem = hci_reassembly(hdev, type, data, count,
1972 STREAM_REASSEMBLY);
Suraj Sumangala99811512010-07-14 13:02:19 +05301973 if (rem < 0)
1974 return rem;
1975
1976 data += (count - rem);
1977 count = rem;
Joe Perchesf81c6222011-06-03 11:51:19 +00001978 }
Suraj Sumangala99811512010-07-14 13:02:19 +05301979
1980 return rem;
1981}
1982EXPORT_SYMBOL(hci_recv_stream_fragment);
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984/* ---- Interface to upper protocols ---- */
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986int hci_register_cb(struct hci_cb *cb)
1987{
1988 BT_DBG("%p name %s", cb, cb->name);
1989
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001990 write_lock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 list_add(&cb->list, &hci_cb_list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001992 write_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 return 0;
1995}
1996EXPORT_SYMBOL(hci_register_cb);
1997
1998int hci_unregister_cb(struct hci_cb *cb)
1999{
2000 BT_DBG("%p name %s", cb, cb->name);
2001
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002002 write_lock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 list_del(&cb->list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002004 write_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 return 0;
2007}
2008EXPORT_SYMBOL(hci_unregister_cb);
2009
2010static int hci_send_frame(struct sk_buff *skb)
2011{
2012 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2013
2014 if (!hdev) {
2015 kfree_skb(skb);
2016 return -ENODEV;
2017 }
2018
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002019 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 if (atomic_read(&hdev->promisc)) {
2022 /* Time stamp */
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002023 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002025 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027
2028 /* Get rid of skb owner, prior to sending to the driver. */
2029 skb_orphan(skb);
2030
2031 return hdev->send(skb);
2032}
2033
2034/* Send HCI command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002035int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 int len = HCI_COMMAND_HDR_SIZE + plen;
2038 struct hci_command_hdr *hdr;
2039 struct sk_buff *skb;
2040
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002041 BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 skb = bt_skb_alloc(len, GFP_ATOMIC);
2044 if (!skb) {
Marcel Holtmannef222012007-07-11 06:42:04 +02002045 BT_ERR("%s no memory for command", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return -ENOMEM;
2047 }
2048
2049 hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002050 hdr->opcode = cpu_to_le16(opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 hdr->plen = plen;
2052
2053 if (plen)
2054 memcpy(skb_put(skb, plen), param, plen);
2055
2056 BT_DBG("skb len %d", skb->len);
2057
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002058 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 skb->dev = (void *) hdev;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002060
Johan Hedberga5040ef2011-01-10 13:28:59 +02002061 if (test_bit(HCI_INIT, &hdev->flags))
2062 hdev->init_last_cmd = opcode;
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 skb_queue_tail(&hdev->cmd_q, skb);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002065 queue_work(hdev->workqueue, &hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 return 0;
2068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070/* Get data from the previously sent command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002071void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 struct hci_command_hdr *hdr;
2074
2075 if (!hdev->sent_cmd)
2076 return NULL;
2077
2078 hdr = (void *) hdev->sent_cmd->data;
2079
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002080 if (hdr->opcode != cpu_to_le16(opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return NULL;
2082
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002083 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
2086}
2087
2088/* Send ACL data */
2089static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
2090{
2091 struct hci_acl_hdr *hdr;
2092 int len = skb->len;
2093
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03002094 skb_push(skb, HCI_ACL_HDR_SIZE);
2095 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002096 hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002097 hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
2098 hdr->dlen = cpu_to_le16(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002101static void hci_queue_acl(struct hci_conn *conn, struct sk_buff_head *queue,
2102 struct sk_buff *skb, __u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 struct hci_dev *hdev = conn->hdev;
2105 struct sk_buff *list;
2106
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002107 list = skb_shinfo(skb)->frag_list;
2108 if (!list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 /* Non fragmented */
2110 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
2111
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002112 skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 } else {
2114 /* Fragmented */
2115 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2116
2117 skb_shinfo(skb)->frag_list = NULL;
2118
2119 /* Queue all fragments atomically */
Gustavo F. Padovanaf3e6352011-12-22 16:35:05 -02002120 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002122 __skb_queue_tail(queue, skb);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02002123
2124 flags &= ~ACL_START;
2125 flags |= ACL_CONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 do {
2127 skb = list; list = list->next;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002130 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02002131 hci_add_acl_hdr(skb, conn->handle, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2134
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002135 __skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 } while (list);
2137
Gustavo F. Padovanaf3e6352011-12-22 16:35:05 -02002138 spin_unlock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002140}
2141
2142void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
2143{
2144 struct hci_conn *conn = chan->conn;
2145 struct hci_dev *hdev = conn->hdev;
2146
2147 BT_DBG("%s chan %p flags 0x%x", hdev->name, chan, flags);
2148
2149 skb->dev = (void *) hdev;
2150 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
2151 hci_add_acl_hdr(skb, conn->handle, flags);
2152
2153 hci_queue_acl(conn, &chan->data_q, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002155 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157EXPORT_SYMBOL(hci_send_acl);
2158
2159/* Send SCO data */
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -03002160void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 struct hci_dev *hdev = conn->hdev;
2163 struct hci_sco_hdr hdr;
2164
2165 BT_DBG("%s len %d", hdev->name, skb->len);
2166
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002167 hdr.handle = cpu_to_le16(conn->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 hdr.dlen = skb->len;
2169
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03002170 skb_push(skb, HCI_SCO_HDR_SIZE);
2171 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002172 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002175 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 skb_queue_tail(&conn->data_q, skb);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002178 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180EXPORT_SYMBOL(hci_send_sco);
2181
2182/* ---- HCI TX task (outgoing data) ---- */
2183
2184/* HCI Connection scheduler */
2185static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote)
2186{
2187 struct hci_conn_hash *h = &hdev->conn_hash;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002188 struct hci_conn *conn = NULL, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 int num = 0, min = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002191 /* We don't have to lock device here. Connections are always
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 * added and removed with TX task disabled. */
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002193
2194 rcu_read_lock();
2195
2196 list_for_each_entry_rcu(c, &h->list, list) {
Marcel Holtmann769be972008-07-14 20:13:49 +02002197 if (c->type != type || skb_queue_empty(&c->data_q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 continue;
Marcel Holtmann769be972008-07-14 20:13:49 +02002199
2200 if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
2201 continue;
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 num++;
2204
2205 if (c->sent < min) {
2206 min = c->sent;
2207 conn = c;
2208 }
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002209
2210 if (hci_conn_num(hdev, type) == num)
2211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002214 rcu_read_unlock();
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (conn) {
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002217 int cnt, q;
2218
2219 switch (conn->type) {
2220 case ACL_LINK:
2221 cnt = hdev->acl_cnt;
2222 break;
2223 case SCO_LINK:
2224 case ESCO_LINK:
2225 cnt = hdev->sco_cnt;
2226 break;
2227 case LE_LINK:
2228 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2229 break;
2230 default:
2231 cnt = 0;
2232 BT_ERR("Unknown link type");
2233 }
2234
2235 q = cnt / num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 *quote = q ? q : 1;
2237 } else
2238 *quote = 0;
2239
2240 BT_DBG("conn %p quote %d", conn, *quote);
2241 return conn;
2242}
2243
Ville Tervobae1f5d92011-02-10 22:38:53 -03002244static inline void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
2246 struct hci_conn_hash *h = &hdev->conn_hash;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002247 struct hci_conn *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Ville Tervobae1f5d92011-02-10 22:38:53 -03002249 BT_ERR("%s link tx timeout", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002251 rcu_read_lock();
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 /* Kill stalled connections */
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002254 list_for_each_entry_rcu(c, &h->list, list) {
Ville Tervobae1f5d92011-02-10 22:38:53 -03002255 if (c->type == type && c->sent) {
2256 BT_ERR("%s killing stalled connection %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 hdev->name, batostr(&c->dst));
2258 hci_acl_disconn(c, 0x13);
2259 }
2260 }
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002261
2262 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002265static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
2266 int *quote)
2267{
2268 struct hci_conn_hash *h = &hdev->conn_hash;
2269 struct hci_chan *chan = NULL;
2270 int num = 0, min = ~0, cur_prio = 0;
2271 struct hci_conn *conn;
2272 int cnt, q, conn_num = 0;
2273
2274 BT_DBG("%s", hdev->name);
2275
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002276 rcu_read_lock();
2277
2278 list_for_each_entry_rcu(conn, &h->list, list) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002279 struct hci_chan *tmp;
2280
2281 if (conn->type != type)
2282 continue;
2283
2284 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2285 continue;
2286
2287 conn_num++;
2288
Gustavo F. Padovan8192ede2011-12-14 15:08:48 -02002289 list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002290 struct sk_buff *skb;
2291
2292 if (skb_queue_empty(&tmp->data_q))
2293 continue;
2294
2295 skb = skb_peek(&tmp->data_q);
2296 if (skb->priority < cur_prio)
2297 continue;
2298
2299 if (skb->priority > cur_prio) {
2300 num = 0;
2301 min = ~0;
2302 cur_prio = skb->priority;
2303 }
2304
2305 num++;
2306
2307 if (conn->sent < min) {
2308 min = conn->sent;
2309 chan = tmp;
2310 }
2311 }
2312
2313 if (hci_conn_num(hdev, type) == conn_num)
2314 break;
2315 }
2316
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002317 rcu_read_unlock();
2318
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002319 if (!chan)
2320 return NULL;
2321
2322 switch (chan->conn->type) {
2323 case ACL_LINK:
2324 cnt = hdev->acl_cnt;
2325 break;
2326 case SCO_LINK:
2327 case ESCO_LINK:
2328 cnt = hdev->sco_cnt;
2329 break;
2330 case LE_LINK:
2331 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2332 break;
2333 default:
2334 cnt = 0;
2335 BT_ERR("Unknown link type");
2336 }
2337
2338 q = cnt / num;
2339 *quote = q ? q : 1;
2340 BT_DBG("chan %p quote %d", chan, *quote);
2341 return chan;
2342}
2343
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002344static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
2345{
2346 struct hci_conn_hash *h = &hdev->conn_hash;
2347 struct hci_conn *conn;
2348 int num = 0;
2349
2350 BT_DBG("%s", hdev->name);
2351
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002352 rcu_read_lock();
2353
2354 list_for_each_entry_rcu(conn, &h->list, list) {
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002355 struct hci_chan *chan;
2356
2357 if (conn->type != type)
2358 continue;
2359
2360 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2361 continue;
2362
2363 num++;
2364
Gustavo F. Padovan8192ede2011-12-14 15:08:48 -02002365 list_for_each_entry_rcu(chan, &conn->chan_list, list) {
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002366 struct sk_buff *skb;
2367
2368 if (chan->sent) {
2369 chan->sent = 0;
2370 continue;
2371 }
2372
2373 if (skb_queue_empty(&chan->data_q))
2374 continue;
2375
2376 skb = skb_peek(&chan->data_q);
2377 if (skb->priority >= HCI_PRIO_MAX - 1)
2378 continue;
2379
2380 skb->priority = HCI_PRIO_MAX - 1;
2381
2382 BT_DBG("chan %p skb %p promoted to %d", chan, skb,
2383 skb->priority);
2384 }
2385
2386 if (hci_conn_num(hdev, type) == num)
2387 break;
2388 }
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002389
2390 rcu_read_unlock();
2391
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002392}
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394static inline void hci_sched_acl(struct hci_dev *hdev)
2395{
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002396 struct hci_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 struct sk_buff *skb;
2398 int quote;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002399 unsigned int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 BT_DBG("%s", hdev->name);
2402
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002403 if (!hci_conn_num(hdev, ACL_LINK))
2404 return;
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (!test_bit(HCI_RAW, &hdev->flags)) {
2407 /* ACL tx timeout must be longer than maximum
2408 * link supervision timeout (40.9 seconds) */
Andrei Emeltchenkocc48dc02012-01-04 16:42:26 +02002409 if (!hdev->acl_cnt && time_after(jiffies, hdev->acl_last_tx +
2410 msecs_to_jiffies(HCI_ACL_TX_TIMEOUT)))
Ville Tervobae1f5d92011-02-10 22:38:53 -03002411 hci_link_tx_to(hdev, ACL_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002414 cnt = hdev->acl_cnt;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002415
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002416 while (hdev->acl_cnt &&
2417 (chan = hci_chan_sent(hdev, ACL_LINK, &quote))) {
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002418 u32 priority = (skb_peek(&chan->data_q))->priority;
2419 while (quote-- && (skb = skb_peek(&chan->data_q))) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002420 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2421 skb->len, skb->priority);
2422
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002423 /* Stop if priority has changed */
2424 if (skb->priority < priority)
2425 break;
2426
2427 skb = skb_dequeue(&chan->data_q);
2428
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002429 hci_conn_enter_active_mode(chan->conn,
2430 bt_cb(skb)->force_active);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 hci_send_frame(skb);
2433 hdev->acl_last_tx = jiffies;
2434
2435 hdev->acl_cnt--;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002436 chan->sent++;
2437 chan->conn->sent++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439 }
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002440
2441 if (cnt != hdev->acl_cnt)
2442 hci_prio_recalculate(hdev, ACL_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
2445/* Schedule SCO */
2446static inline void hci_sched_sco(struct hci_dev *hdev)
2447{
2448 struct hci_conn *conn;
2449 struct sk_buff *skb;
2450 int quote;
2451
2452 BT_DBG("%s", hdev->name);
2453
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002454 if (!hci_conn_num(hdev, SCO_LINK))
2455 return;
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
2458 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2459 BT_DBG("skb %p len %d", skb, skb->len);
2460 hci_send_frame(skb);
2461
2462 conn->sent++;
2463 if (conn->sent == ~0)
2464 conn->sent = 0;
2465 }
2466 }
2467}
2468
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002469static inline void hci_sched_esco(struct hci_dev *hdev)
2470{
2471 struct hci_conn *conn;
2472 struct sk_buff *skb;
2473 int quote;
2474
2475 BT_DBG("%s", hdev->name);
2476
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002477 if (!hci_conn_num(hdev, ESCO_LINK))
2478 return;
2479
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002480 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, &quote))) {
2481 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2482 BT_DBG("skb %p len %d", skb, skb->len);
2483 hci_send_frame(skb);
2484
2485 conn->sent++;
2486 if (conn->sent == ~0)
2487 conn->sent = 0;
2488 }
2489 }
2490}
2491
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002492static inline void hci_sched_le(struct hci_dev *hdev)
2493{
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002494 struct hci_chan *chan;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002495 struct sk_buff *skb;
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002496 int quote, cnt, tmp;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002497
2498 BT_DBG("%s", hdev->name);
2499
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002500 if (!hci_conn_num(hdev, LE_LINK))
2501 return;
2502
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002503 if (!test_bit(HCI_RAW, &hdev->flags)) {
2504 /* LE tx timeout must be longer than maximum
2505 * link supervision timeout (40.9 seconds) */
Ville Tervobae1f5d92011-02-10 22:38:53 -03002506 if (!hdev->le_cnt && hdev->le_pkts &&
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002507 time_after(jiffies, hdev->le_last_tx + HZ * 45))
Ville Tervobae1f5d92011-02-10 22:38:53 -03002508 hci_link_tx_to(hdev, LE_LINK);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002509 }
2510
2511 cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002512 tmp = cnt;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002513 while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, &quote))) {
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002514 u32 priority = (skb_peek(&chan->data_q))->priority;
2515 while (quote-- && (skb = skb_peek(&chan->data_q))) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002516 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2517 skb->len, skb->priority);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002518
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002519 /* Stop if priority has changed */
2520 if (skb->priority < priority)
2521 break;
2522
2523 skb = skb_dequeue(&chan->data_q);
2524
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002525 hci_send_frame(skb);
2526 hdev->le_last_tx = jiffies;
2527
2528 cnt--;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002529 chan->sent++;
2530 chan->conn->sent++;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002531 }
2532 }
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002533
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002534 if (hdev->le_pkts)
2535 hdev->le_cnt = cnt;
2536 else
2537 hdev->acl_cnt = cnt;
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002538
2539 if (cnt != tmp)
2540 hci_prio_recalculate(hdev, LE_LINK);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002541}
2542
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002543static void hci_tx_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002545 struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 struct sk_buff *skb;
2547
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002548 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
2549 hdev->sco_cnt, hdev->le_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 /* Schedule queues and send stuff to HCI driver */
2552
2553 hci_sched_acl(hdev);
2554
2555 hci_sched_sco(hdev);
2556
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002557 hci_sched_esco(hdev);
2558
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002559 hci_sched_le(hdev);
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /* Send next queued raw (unknown type) packet */
2562 while ((skb = skb_dequeue(&hdev->raw_q)))
2563 hci_send_frame(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002566/* ----- HCI RX task (incoming data processing) ----- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568/* ACL data packet */
2569static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2570{
2571 struct hci_acl_hdr *hdr = (void *) skb->data;
2572 struct hci_conn *conn;
2573 __u16 handle, flags;
2574
2575 skb_pull(skb, HCI_ACL_HDR_SIZE);
2576
2577 handle = __le16_to_cpu(hdr->handle);
2578 flags = hci_flags(handle);
2579 handle = hci_handle(handle);
2580
2581 BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags);
2582
2583 hdev->stat.acl_rx++;
2584
2585 hci_dev_lock(hdev);
2586 conn = hci_conn_hash_lookup_handle(hdev, handle);
2587 hci_dev_unlock(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (conn) {
Mat Martineau65983fc2011-12-13 15:06:02 -08002590 hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 /* Send to upper protocol */
Ulisses Furquim686ebf22011-12-21 10:11:33 -02002593 l2cap_recv_acldata(conn, skb, flags);
2594 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002596 BT_ERR("%s ACL packet for unknown connection handle %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 hdev->name, handle);
2598 }
2599
2600 kfree_skb(skb);
2601}
2602
2603/* SCO data packet */
2604static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2605{
2606 struct hci_sco_hdr *hdr = (void *) skb->data;
2607 struct hci_conn *conn;
2608 __u16 handle;
2609
2610 skb_pull(skb, HCI_SCO_HDR_SIZE);
2611
2612 handle = __le16_to_cpu(hdr->handle);
2613
2614 BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle);
2615
2616 hdev->stat.sco_rx++;
2617
2618 hci_dev_lock(hdev);
2619 conn = hci_conn_hash_lookup_handle(hdev, handle);
2620 hci_dev_unlock(hdev);
2621
2622 if (conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 /* Send to upper protocol */
Ulisses Furquim686ebf22011-12-21 10:11:33 -02002624 sco_recv_scodata(conn, skb);
2625 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002627 BT_ERR("%s SCO packet for unknown connection handle %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 hdev->name, handle);
2629 }
2630
2631 kfree_skb(skb);
2632}
2633
Marcel Holtmannb78752c2010-08-08 23:06:53 -04002634static void hci_rx_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Marcel Holtmannb78752c2010-08-08 23:06:53 -04002636 struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 struct sk_buff *skb;
2638
2639 BT_DBG("%s", hdev->name);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 while ((skb = skb_dequeue(&hdev->rx_q))) {
2642 if (atomic_read(&hdev->promisc)) {
2643 /* Send copy to the sockets */
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002644 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
2646
2647 if (test_bit(HCI_RAW, &hdev->flags)) {
2648 kfree_skb(skb);
2649 continue;
2650 }
2651
2652 if (test_bit(HCI_INIT, &hdev->flags)) {
2653 /* Don't process data packets in this states. */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002654 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 case HCI_ACLDATA_PKT:
2656 case HCI_SCODATA_PKT:
2657 kfree_skb(skb);
2658 continue;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
2661
2662 /* Process frame */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002663 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 case HCI_EVENT_PKT:
Marcel Holtmannb78752c2010-08-08 23:06:53 -04002665 BT_DBG("%s Event packet", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 hci_event_packet(hdev, skb);
2667 break;
2668
2669 case HCI_ACLDATA_PKT:
2670 BT_DBG("%s ACL data packet", hdev->name);
2671 hci_acldata_packet(hdev, skb);
2672 break;
2673
2674 case HCI_SCODATA_PKT:
2675 BT_DBG("%s SCO data packet", hdev->name);
2676 hci_scodata_packet(hdev, skb);
2677 break;
2678
2679 default:
2680 kfree_skb(skb);
2681 break;
2682 }
2683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002686static void hci_cmd_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002688 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 struct sk_buff *skb;
2690
2691 BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /* Send queued commands */
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002694 if (atomic_read(&hdev->cmd_cnt)) {
2695 skb = skb_dequeue(&hdev->cmd_q);
2696 if (!skb)
2697 return;
2698
Wei Yongjun7585b972009-02-25 18:29:52 +08002699 kfree_skb(hdev->sent_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002701 hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC);
2702 if (hdev->sent_cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 atomic_dec(&hdev->cmd_cnt);
2704 hci_send_frame(skb);
Szymon Janc7bdb8a52011-07-26 22:46:54 +02002705 if (test_bit(HCI_RESET, &hdev->flags))
2706 del_timer(&hdev->cmd_timer);
2707 else
2708 mod_timer(&hdev->cmd_timer,
Ville Tervo6bd32322011-02-16 16:32:41 +02002709 jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 } else {
2711 skb_queue_head(&hdev->cmd_q, skb);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002712 queue_work(hdev->workqueue, &hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
2714 }
2715}
Andre Guedes2519a1f2011-11-07 11:45:24 -03002716
2717int hci_do_inquiry(struct hci_dev *hdev, u8 length)
2718{
2719 /* General inquiry access code (GIAC) */
2720 u8 lap[3] = { 0x33, 0x8b, 0x9e };
2721 struct hci_cp_inquiry cp;
2722
2723 BT_DBG("%s", hdev->name);
2724
2725 if (test_bit(HCI_INQUIRY, &hdev->flags))
2726 return -EINPROGRESS;
2727
Johan Hedberg46632622012-01-02 16:06:08 +02002728 inquiry_cache_flush(hdev);
2729
Andre Guedes2519a1f2011-11-07 11:45:24 -03002730 memset(&cp, 0, sizeof(cp));
2731 memcpy(&cp.lap, lap, sizeof(cp.lap));
2732 cp.length = length;
2733
2734 return hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
2735}
Andre Guedes023d50492011-11-04 14:16:52 -03002736
2737int hci_cancel_inquiry(struct hci_dev *hdev)
2738{
2739 BT_DBG("%s", hdev->name);
2740
2741 if (!test_bit(HCI_INQUIRY, &hdev->flags))
2742 return -EPERM;
2743
2744 return hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL);
2745}
Andrei Emeltchenko7784d782011-11-18 13:35:42 +02002746
2747module_param(enable_hs, bool, 0644);
2748MODULE_PARM_DESC(enable_hs, "Enable High Speed");