blob: a787c9c9d4cd36a861406d7e0948343ba0a631bb [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>
Marcel Holtmann611b30f2009-06-08 14:41:38 +020043#include <linux/rfkill.h>
Ville Tervo6bd32322011-02-16 16:32:41 +020044#include <linux/timer.h>
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -030045#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/sock.h>
47
48#include <asm/system.h>
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020049#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/unaligned.h>
51
52#include <net/bluetooth/bluetooth.h>
53#include <net/bluetooth/hci_core.h>
54
Johan Hedbergab81cbf2010-12-15 13:53:18 +020055#define AUTO_OFF_TIMEOUT 2000
56
Marcel Holtmannb78752c2010-08-08 23:06:53 -040057static void hci_rx_work(struct work_struct *work);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -020058static void hci_cmd_work(struct work_struct *work);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -020059static void hci_tx_work(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* HCI device list */
62LIST_HEAD(hci_dev_list);
63DEFINE_RWLOCK(hci_dev_list_lock);
64
65/* HCI callback list */
66LIST_HEAD(hci_cb_list);
67DEFINE_RWLOCK(hci_cb_list_lock);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* ---- HCI notifications ---- */
70
Marcel Holtmann65164552005-10-28 19:20:48 +020071static void hci_notify(struct hci_dev *hdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Marcel Holtmann040030e2012-02-20 14:50:37 +010073 hci_sock_dev_event(hdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76/* ---- HCI requests ---- */
77
Johan Hedberg23bb5762010-12-21 23:01:27 +020078void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Johan Hedberg23bb5762010-12-21 23:01:27 +020080 BT_DBG("%s command 0x%04x result 0x%2.2x", hdev->name, cmd, result);
81
Johan Hedberga5040ef2011-01-10 13:28:59 +020082 /* If this is the init phase check if the completed command matches
83 * the last init command, and if not just return.
84 */
85 if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd)
Johan Hedberg23bb5762010-12-21 23:01:27 +020086 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (hdev->req_status == HCI_REQ_PEND) {
89 hdev->req_result = result;
90 hdev->req_status = HCI_REQ_DONE;
91 wake_up_interruptible(&hdev->req_wait_q);
92 }
93}
94
95static void hci_req_cancel(struct hci_dev *hdev, int err)
96{
97 BT_DBG("%s err 0x%2.2x", hdev->name, err);
98
99 if (hdev->req_status == HCI_REQ_PEND) {
100 hdev->req_result = err;
101 hdev->req_status = HCI_REQ_CANCELED;
102 wake_up_interruptible(&hdev->req_wait_q);
103 }
104}
105
106/* Execute request and wait for completion. */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900107static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
Szymon Janc01df8c32011-02-17 16:46:47 +0100108 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 DECLARE_WAITQUEUE(wait, current);
111 int err = 0;
112
113 BT_DBG("%s start", hdev->name);
114
115 hdev->req_status = HCI_REQ_PEND;
116
117 add_wait_queue(&hdev->req_wait_q, &wait);
118 set_current_state(TASK_INTERRUPTIBLE);
119
120 req(hdev, opt);
121 schedule_timeout(timeout);
122
123 remove_wait_queue(&hdev->req_wait_q, &wait);
124
125 if (signal_pending(current))
126 return -EINTR;
127
128 switch (hdev->req_status) {
129 case HCI_REQ_DONE:
Joe Perchese1750722011-06-29 18:18:29 -0700130 err = -bt_to_errno(hdev->req_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 break;
132
133 case HCI_REQ_CANCELED:
134 err = -hdev->req_result;
135 break;
136
137 default:
138 err = -ETIMEDOUT;
139 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Johan Hedberga5040ef2011-01-10 13:28:59 +0200142 hdev->req_status = hdev->req_result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 BT_DBG("%s end: err %d", hdev->name, err);
145
146 return err;
147}
148
149static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
Szymon Janc01df8c32011-02-17 16:46:47 +0100150 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int ret;
153
Marcel Holtmann7c6a3292008-09-12 03:11:54 +0200154 if (!test_bit(HCI_UP, &hdev->flags))
155 return -ENETDOWN;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Serialize all requests */
158 hci_req_lock(hdev);
159 ret = __hci_request(hdev, req, opt, timeout);
160 hci_req_unlock(hdev);
161
162 return ret;
163}
164
165static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
166{
167 BT_DBG("%s %ld", hdev->name, opt);
168
169 /* Reset device */
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300170 set_bit(HCI_RESET, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200171 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200174static void bredr_init(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200176 struct hci_cp_delete_stored_link_key cp;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -0800177 __le16 param;
Marcel Holtmann89f27832007-09-09 08:39:49 +0200178 __u8 flt_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Andrei Emeltchenko2455a3e2011-12-19 16:31:28 +0200180 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* Mandatory initialization */
183
184 /* Reset */
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300185 if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) {
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200186 set_bit(HCI_RESET, &hdev->flags);
187 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* Read Local Supported Features */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200191 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200193 /* Read Local Version */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200194 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200197 hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Read BD Address */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200200 hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL);
201
202 /* Read Class of Device */
203 hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
204
205 /* Read Local Name */
206 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* Read Voice Setting */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200209 hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* Optional initialization */
212
213 /* Clear Event Filters */
Marcel Holtmann89f27832007-09-09 08:39:49 +0200214 flt_type = HCI_FLT_CLEAR_ALL;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200215 hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* Connection accept timeout ~20 secs */
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700218 param = cpu_to_le16(0x7d00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200219 hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200220
221 bacpy(&cp.bdaddr, BDADDR_ANY);
222 cp.delete_all = 1;
223 hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200226static void amp_init(struct hci_dev *hdev)
227{
Andrei Emeltchenko2455a3e2011-12-19 16:31:28 +0200228 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
229
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200230 /* Reset */
231 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
232
233 /* Read Local Version */
234 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
235}
236
237static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
238{
239 struct sk_buff *skb;
240
241 BT_DBG("%s %ld", hdev->name, opt);
242
243 /* Driver initialization */
244
245 /* Special commands */
246 while ((skb = skb_dequeue(&hdev->driver_init))) {
247 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
248 skb->dev = (void *) hdev;
249
250 skb_queue_tail(&hdev->cmd_q, skb);
251 queue_work(hdev->workqueue, &hdev->cmd_work);
252 }
253 skb_queue_purge(&hdev->driver_init);
254
255 switch (hdev->dev_type) {
256 case HCI_BREDR:
257 bredr_init(hdev);
258 break;
259
260 case HCI_AMP:
261 amp_init(hdev);
262 break;
263
264 default:
265 BT_ERR("Unknown device type %d", hdev->dev_type);
266 break;
267 }
268
269}
270
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300271static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt)
272{
273 BT_DBG("%s", hdev->name);
274
275 /* Read LE buffer size */
276 hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static void hci_scan_req(struct hci_dev *hdev, unsigned long opt)
280{
281 __u8 scan = opt;
282
283 BT_DBG("%s %x", hdev->name, scan);
284
285 /* Inquiry and Page scans */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200286 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289static void hci_auth_req(struct hci_dev *hdev, unsigned long opt)
290{
291 __u8 auth = opt;
292
293 BT_DBG("%s %x", hdev->name, auth);
294
295 /* Authentication */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200296 hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
300{
301 __u8 encrypt = opt;
302
303 BT_DBG("%s %x", hdev->name, encrypt);
304
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200305 /* Encryption */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200306 hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200309static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt)
310{
311 __le16 policy = cpu_to_le16(opt);
312
Marcel Holtmanna418b892008-11-30 12:17:28 +0100313 BT_DBG("%s %x", hdev->name, policy);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200314
315 /* Default link policy */
316 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
317}
318
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900319/* Get HCI device by index.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * Device is held on return. */
321struct hci_dev *hci_dev_get(int index)
322{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200323 struct hci_dev *hdev = NULL, *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 BT_DBG("%d", index);
326
327 if (index < 0)
328 return NULL;
329
330 read_lock(&hci_dev_list_lock);
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200331 list_for_each_entry(d, &hci_dev_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (d->id == index) {
333 hdev = hci_dev_hold(d);
334 break;
335 }
336 }
337 read_unlock(&hci_dev_list_lock);
338 return hdev;
339}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/* ---- Inquiry support ---- */
Johan Hedbergff9ef572012-01-04 14:23:45 +0200342
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200343bool hci_discovery_active(struct hci_dev *hdev)
344{
345 struct discovery_state *discov = &hdev->discovery;
346
Andre Guedes6fbe1952012-02-03 17:47:58 -0300347 switch (discov->state) {
Andre Guedes343f9352012-02-17 20:39:37 -0300348 case DISCOVERY_FINDING:
Andre Guedes6fbe1952012-02-03 17:47:58 -0300349 case DISCOVERY_RESOLVING:
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200350 return true;
351
Andre Guedes6fbe1952012-02-03 17:47:58 -0300352 default:
353 return false;
354 }
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200355}
356
Johan Hedbergff9ef572012-01-04 14:23:45 +0200357void hci_discovery_set_state(struct hci_dev *hdev, int state)
358{
359 BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
360
361 if (hdev->discovery.state == state)
362 return;
363
364 switch (state) {
365 case DISCOVERY_STOPPED:
Andre Guedes7b99b652012-02-13 15:41:02 -0300366 if (hdev->discovery.state != DISCOVERY_STARTING)
367 mgmt_discovering(hdev, 0);
Johan Hedbergf963e8e2012-02-20 23:30:44 +0200368 hdev->discovery.type = 0;
Johan Hedbergff9ef572012-01-04 14:23:45 +0200369 break;
370 case DISCOVERY_STARTING:
371 break;
Andre Guedes343f9352012-02-17 20:39:37 -0300372 case DISCOVERY_FINDING:
Johan Hedbergff9ef572012-01-04 14:23:45 +0200373 mgmt_discovering(hdev, 1);
374 break;
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200375 case DISCOVERY_RESOLVING:
376 break;
Johan Hedbergff9ef572012-01-04 14:23:45 +0200377 case DISCOVERY_STOPPING:
378 break;
379 }
380
381 hdev->discovery.state = state;
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void inquiry_cache_flush(struct hci_dev *hdev)
385{
Johan Hedberg30883512012-01-04 14:16:21 +0200386 struct discovery_state *cache = &hdev->discovery;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200387 struct inquiry_entry *p, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Johan Hedberg561aafb2012-01-04 13:31:59 +0200389 list_for_each_entry_safe(p, n, &cache->all, all) {
390 list_del(&p->all);
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200391 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Johan Hedberg561aafb2012-01-04 13:31:59 +0200393
394 INIT_LIST_HEAD(&cache->unknown);
395 INIT_LIST_HEAD(&cache->resolve);
Johan Hedbergff9ef572012-01-04 14:23:45 +0200396 cache->state = DISCOVERY_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
400{
Johan Hedberg30883512012-01-04 14:16:21 +0200401 struct discovery_state *cache = &hdev->discovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct inquiry_entry *e;
403
404 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
405
Johan Hedberg561aafb2012-01-04 13:31:59 +0200406 list_for_each_entry(e, &cache->all, all) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!bacmp(&e->data.bdaddr, bdaddr))
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200408 return e;
409 }
410
411 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Johan Hedberg561aafb2012-01-04 13:31:59 +0200414struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
415 bdaddr_t *bdaddr)
416{
Johan Hedberg30883512012-01-04 14:16:21 +0200417 struct discovery_state *cache = &hdev->discovery;
Johan Hedberg561aafb2012-01-04 13:31:59 +0200418 struct inquiry_entry *e;
419
420 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
421
422 list_for_each_entry(e, &cache->unknown, list) {
423 if (!bacmp(&e->data.bdaddr, bdaddr))
424 return e;
425 }
426
427 return NULL;
428}
429
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200430struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
431 bdaddr_t *bdaddr,
432 int state)
433{
434 struct discovery_state *cache = &hdev->discovery;
435 struct inquiry_entry *e;
436
437 BT_DBG("cache %p bdaddr %s state %d", cache, batostr(bdaddr), state);
438
439 list_for_each_entry(e, &cache->resolve, list) {
440 if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state)
441 return e;
442 if (!bacmp(&e->data.bdaddr, bdaddr))
443 return e;
444 }
445
446 return NULL;
447}
448
Johan Hedberga3d4e202012-01-09 00:53:02 +0200449void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
450 struct inquiry_entry *ie)
451{
452 struct discovery_state *cache = &hdev->discovery;
453 struct list_head *pos = &cache->resolve;
454 struct inquiry_entry *p;
455
456 list_del(&ie->list);
457
458 list_for_each_entry(p, &cache->resolve, list) {
459 if (p->name_state != NAME_PENDING &&
460 abs(p->data.rssi) >= abs(ie->data.rssi))
461 break;
462 pos = &p->list;
463 }
464
465 list_add(&ie->list, pos);
466}
467
Johan Hedberg31754052012-01-04 13:39:52 +0200468bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
Johan Hedberg561aafb2012-01-04 13:31:59 +0200469 bool name_known)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Johan Hedberg30883512012-01-04 14:16:21 +0200471 struct discovery_state *cache = &hdev->discovery;
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200472 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr));
475
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200476 ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
Johan Hedberga3d4e202012-01-09 00:53:02 +0200477 if (ie) {
478 if (ie->name_state == NAME_NEEDED &&
479 data->rssi != ie->data.rssi) {
480 ie->data.rssi = data->rssi;
481 hci_inquiry_cache_update_resolve(hdev, ie);
482 }
483
Johan Hedberg561aafb2012-01-04 13:31:59 +0200484 goto update;
Johan Hedberga3d4e202012-01-09 00:53:02 +0200485 }
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200486
Johan Hedberg561aafb2012-01-04 13:31:59 +0200487 /* Entry not in the cache. Add new one. */
488 ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
489 if (!ie)
Johan Hedberg31754052012-01-04 13:39:52 +0200490 return false;
Johan Hedberg561aafb2012-01-04 13:31:59 +0200491
492 list_add(&ie->all, &cache->all);
493
494 if (name_known) {
495 ie->name_state = NAME_KNOWN;
496 } else {
497 ie->name_state = NAME_NOT_KNOWN;
498 list_add(&ie->list, &cache->unknown);
499 }
500
501update:
502 if (name_known && ie->name_state != NAME_KNOWN &&
503 ie->name_state != NAME_PENDING) {
504 ie->name_state = NAME_KNOWN;
505 list_del(&ie->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200508 memcpy(&ie->data, data, sizeof(*data));
509 ie->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 cache->timestamp = jiffies;
Johan Hedberg31754052012-01-04 13:39:52 +0200511
512 if (ie->name_state == NAME_NOT_KNOWN)
513 return false;
514
515 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
519{
Johan Hedberg30883512012-01-04 14:16:21 +0200520 struct discovery_state *cache = &hdev->discovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct inquiry_info *info = (struct inquiry_info *) buf;
522 struct inquiry_entry *e;
523 int copied = 0;
524
Johan Hedberg561aafb2012-01-04 13:31:59 +0200525 list_for_each_entry(e, &cache->all, all) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct inquiry_data *data = &e->data;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200527
528 if (copied >= num)
529 break;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 bacpy(&info->bdaddr, &data->bdaddr);
532 info->pscan_rep_mode = data->pscan_rep_mode;
533 info->pscan_period_mode = data->pscan_period_mode;
534 info->pscan_mode = data->pscan_mode;
535 memcpy(info->dev_class, data->dev_class, 3);
536 info->clock_offset = data->clock_offset;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 info++;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200539 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541
542 BT_DBG("cache %p, copied %d", cache, copied);
543 return copied;
544}
545
546static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
547{
548 struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
549 struct hci_cp_inquiry cp;
550
551 BT_DBG("%s", hdev->name);
552
553 if (test_bit(HCI_INQUIRY, &hdev->flags))
554 return;
555
556 /* Start Inquiry */
557 memcpy(&cp.lap, &ir->lap, 3);
558 cp.length = ir->length;
559 cp.num_rsp = ir->num_rsp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200560 hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563int hci_inquiry(void __user *arg)
564{
565 __u8 __user *ptr = arg;
566 struct hci_inquiry_req ir;
567 struct hci_dev *hdev;
568 int err = 0, do_inquiry = 0, max_rsp;
569 long timeo;
570 __u8 *buf;
571
572 if (copy_from_user(&ir, ptr, sizeof(ir)))
573 return -EFAULT;
574
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200575 hdev = hci_dev_get(ir.dev_id);
576 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return -ENODEV;
578
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300579 hci_dev_lock(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900580 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200581 inquiry_cache_empty(hdev) ||
582 ir.flags & IREQ_CACHE_FLUSH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 inquiry_cache_flush(hdev);
584 do_inquiry = 1;
585 }
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300586 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Marcel Holtmann04837f62006-07-03 10:02:33 +0200588 timeo = ir.length * msecs_to_jiffies(2000);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200589
590 if (do_inquiry) {
591 err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo);
592 if (err < 0)
593 goto done;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* for unlimited number of responses we will use buffer with 255 entries */
597 max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
598
599 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
600 * copy it to the user space.
601 */
Szymon Janc01df8c32011-02-17 16:46:47 +0100602 buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200603 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 err = -ENOMEM;
605 goto done;
606 }
607
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300608 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300610 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 BT_DBG("num_rsp %d", ir.num_rsp);
613
614 if (!copy_to_user(ptr, &ir, sizeof(ir))) {
615 ptr += sizeof(ir);
616 if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
617 ir.num_rsp))
618 err = -EFAULT;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900619 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 err = -EFAULT;
621
622 kfree(buf);
623
624done:
625 hci_dev_put(hdev);
626 return err;
627}
628
629/* ---- HCI ioctl helpers ---- */
630
631int hci_dev_open(__u16 dev)
632{
633 struct hci_dev *hdev;
634 int ret = 0;
635
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200636 hdev = hci_dev_get(dev);
637 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -ENODEV;
639
640 BT_DBG("%s %p", hdev->name, hdev);
641
642 hci_req_lock(hdev);
643
Marcel Holtmann611b30f2009-06-08 14:41:38 +0200644 if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) {
645 ret = -ERFKILL;
646 goto done;
647 }
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (test_bit(HCI_UP, &hdev->flags)) {
650 ret = -EALREADY;
651 goto done;
652 }
653
654 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
655 set_bit(HCI_RAW, &hdev->flags);
656
Andrei Emeltchenko07e3b942011-11-11 17:02:15 +0200657 /* Treat all non BR/EDR controllers as raw devices if
658 enable_hs is not set */
659 if (hdev->dev_type != HCI_BREDR && !enable_hs)
Marcel Holtmann943da252010-02-13 02:28:41 +0100660 set_bit(HCI_RAW, &hdev->flags);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (hdev->open(hdev)) {
663 ret = -EIO;
664 goto done;
665 }
666
667 if (!test_bit(HCI_RAW, &hdev->flags)) {
668 atomic_set(&hdev->cmd_cnt, 1);
669 set_bit(HCI_INIT, &hdev->flags);
Johan Hedberga5040ef2011-01-10 13:28:59 +0200670 hdev->init_last_cmd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Marcel Holtmann04837f62006-07-03 10:02:33 +0200672 ret = __hci_request(hdev, hci_init_req, 0,
673 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Andre Guedeseead27d2011-06-30 19:20:55 -0300675 if (lmp_host_le_capable(hdev))
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300676 ret = __hci_request(hdev, hci_le_init_req, 0,
677 msecs_to_jiffies(HCI_INIT_TIMEOUT));
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 clear_bit(HCI_INIT, &hdev->flags);
680 }
681
682 if (!ret) {
683 hci_dev_hold(hdev);
684 set_bit(HCI_UP, &hdev->flags);
685 hci_notify(hdev, HCI_DEV_UP);
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200686 if (!test_bit(HCI_SETUP, &hdev->dev_flags)) {
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300687 hci_dev_lock(hdev);
Johan Hedberg744cf192011-11-08 20:40:14 +0200688 mgmt_powered(hdev, 1);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300689 hci_dev_unlock(hdev);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200690 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900691 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* Init failed, cleanup */
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -0200693 flush_work(&hdev->tx_work);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -0200694 flush_work(&hdev->cmd_work);
Marcel Holtmannb78752c2010-08-08 23:06:53 -0400695 flush_work(&hdev->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 skb_queue_purge(&hdev->cmd_q);
698 skb_queue_purge(&hdev->rx_q);
699
700 if (hdev->flush)
701 hdev->flush(hdev);
702
703 if (hdev->sent_cmd) {
704 kfree_skb(hdev->sent_cmd);
705 hdev->sent_cmd = NULL;
706 }
707
708 hdev->close(hdev);
709 hdev->flags = 0;
710 }
711
712done:
713 hci_req_unlock(hdev);
714 hci_dev_put(hdev);
715 return ret;
716}
717
718static int hci_dev_do_close(struct hci_dev *hdev)
719{
720 BT_DBG("%s %p", hdev->name, hdev);
721
Andre Guedes28b75a82012-02-03 17:48:00 -0300722 cancel_work_sync(&hdev->le_scan);
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 hci_req_cancel(hdev, ENODEV);
725 hci_req_lock(hdev);
726
727 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
Vinicius Costa Gomesb79f44c2011-04-11 18:46:55 -0300728 del_timer_sync(&hdev->cmd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 hci_req_unlock(hdev);
730 return 0;
731 }
732
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -0200733 /* Flush RX and TX works */
734 flush_work(&hdev->tx_work);
Marcel Holtmannb78752c2010-08-08 23:06:53 -0400735 flush_work(&hdev->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Johan Hedberg16ab91a2011-11-07 22:16:02 +0200737 if (hdev->discov_timeout > 0) {
Johan Hedberge0f93092011-11-09 01:44:22 +0200738 cancel_delayed_work(&hdev->discov_off);
Johan Hedberg16ab91a2011-11-07 22:16:02 +0200739 hdev->discov_timeout = 0;
740 }
741
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200742 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg7d785252011-12-15 00:47:39 +0200743 cancel_delayed_work(&hdev->service_cache);
744
Andre Guedes7ba8b4b2012-02-03 17:47:59 -0300745 cancel_delayed_work_sync(&hdev->le_scan_disable);
746
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300747 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 inquiry_cache_flush(hdev);
749 hci_conn_hash_flush(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300750 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 hci_notify(hdev, HCI_DEV_DOWN);
753
754 if (hdev->flush)
755 hdev->flush(hdev);
756
757 /* Reset device */
758 skb_queue_purge(&hdev->cmd_q);
759 atomic_set(&hdev->cmd_cnt, 1);
Johan Hedberg8af59462012-02-03 21:29:40 +0200760 if (!test_bit(HCI_RAW, &hdev->flags) &&
761 test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 set_bit(HCI_INIT, &hdev->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200763 __hci_request(hdev, hci_reset_req, 0,
Gustavo F. Padovancad44c22011-12-23 18:59:13 -0200764 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 clear_bit(HCI_INIT, &hdev->flags);
766 }
767
Gustavo F. Padovanc347b762011-12-14 23:53:47 -0200768 /* flush cmd work */
769 flush_work(&hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* Drop queues */
772 skb_queue_purge(&hdev->rx_q);
773 skb_queue_purge(&hdev->cmd_q);
774 skb_queue_purge(&hdev->raw_q);
775
776 /* Drop last sent command */
777 if (hdev->sent_cmd) {
Vinicius Costa Gomesb79f44c2011-04-11 18:46:55 -0300778 del_timer_sync(&hdev->cmd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 kfree_skb(hdev->sent_cmd);
780 hdev->sent_cmd = NULL;
781 }
782
783 /* After this point our queues are empty
784 * and no tasks are scheduled. */
785 hdev->close(hdev);
786
Marcel Holtmann8ee56542012-02-21 12:33:48 +0100787 if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
788 hci_dev_lock(hdev);
789 mgmt_powered(hdev, 0);
790 hci_dev_unlock(hdev);
791 }
Johan Hedberg5add6af2010-12-16 10:00:37 +0200792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* Clear flags */
794 hdev->flags = 0;
795
796 hci_req_unlock(hdev);
797
798 hci_dev_put(hdev);
799 return 0;
800}
801
802int hci_dev_close(__u16 dev)
803{
804 struct hci_dev *hdev;
805 int err;
806
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200807 hdev = hci_dev_get(dev);
808 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return -ENODEV;
Marcel Holtmann8ee56542012-02-21 12:33:48 +0100810
811 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
812 cancel_delayed_work(&hdev->power_off);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 err = hci_dev_do_close(hdev);
Marcel Holtmann8ee56542012-02-21 12:33:48 +0100815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 hci_dev_put(hdev);
817 return err;
818}
819
820int hci_dev_reset(__u16 dev)
821{
822 struct hci_dev *hdev;
823 int ret = 0;
824
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200825 hdev = hci_dev_get(dev);
826 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return -ENODEV;
828
829 hci_req_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (!test_bit(HCI_UP, &hdev->flags))
832 goto done;
833
834 /* Drop queues */
835 skb_queue_purge(&hdev->rx_q);
836 skb_queue_purge(&hdev->cmd_q);
837
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300838 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 inquiry_cache_flush(hdev);
840 hci_conn_hash_flush(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300841 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (hdev->flush)
844 hdev->flush(hdev);
845
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900846 atomic_set(&hdev->cmd_cnt, 1);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300847 hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (!test_bit(HCI_RAW, &hdev->flags))
Marcel Holtmann04837f62006-07-03 10:02:33 +0200850 ret = __hci_request(hdev, hci_reset_req, 0,
851 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 hci_req_unlock(hdev);
855 hci_dev_put(hdev);
856 return ret;
857}
858
859int hci_dev_reset_stat(__u16 dev)
860{
861 struct hci_dev *hdev;
862 int ret = 0;
863
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200864 hdev = hci_dev_get(dev);
865 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return -ENODEV;
867
868 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
869
870 hci_dev_put(hdev);
871
872 return ret;
873}
874
875int hci_dev_cmd(unsigned int cmd, void __user *arg)
876{
877 struct hci_dev *hdev;
878 struct hci_dev_req dr;
879 int err = 0;
880
881 if (copy_from_user(&dr, arg, sizeof(dr)))
882 return -EFAULT;
883
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200884 hdev = hci_dev_get(dr.dev_id);
885 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -ENODEV;
887
888 switch (cmd) {
889 case HCISETAUTH:
Marcel Holtmann04837f62006-07-03 10:02:33 +0200890 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
891 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 break;
893
894 case HCISETENCRYPT:
895 if (!lmp_encrypt_capable(hdev)) {
896 err = -EOPNOTSUPP;
897 break;
898 }
899
900 if (!test_bit(HCI_AUTH, &hdev->flags)) {
901 /* Auth must be enabled first */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200902 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
903 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (err)
905 break;
906 }
907
Marcel Holtmann04837f62006-07-03 10:02:33 +0200908 err = hci_request(hdev, hci_encrypt_req, dr.dev_opt,
909 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 break;
911
912 case HCISETSCAN:
Marcel Holtmann04837f62006-07-03 10:02:33 +0200913 err = hci_request(hdev, hci_scan_req, dr.dev_opt,
914 msecs_to_jiffies(HCI_INIT_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 break;
916
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200917 case HCISETLINKPOL:
918 err = hci_request(hdev, hci_linkpol_req, dr.dev_opt,
919 msecs_to_jiffies(HCI_INIT_TIMEOUT));
920 break;
921
922 case HCISETLINKMODE:
923 hdev->link_mode = ((__u16) dr.dev_opt) &
924 (HCI_LM_MASTER | HCI_LM_ACCEPT);
925 break;
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 case HCISETPTYPE:
928 hdev->pkt_type = (__u16) dr.dev_opt;
929 break;
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 case HCISETACLMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200932 hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1);
933 hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
935
936 case HCISETSCOMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200937 hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1);
938 hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 break;
940
941 default:
942 err = -EINVAL;
943 break;
944 }
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 hci_dev_put(hdev);
947 return err;
948}
949
950int hci_get_dev_list(void __user *arg)
951{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200952 struct hci_dev *hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct hci_dev_list_req *dl;
954 struct hci_dev_req *dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int n = 0, size, err;
956 __u16 dev_num;
957
958 if (get_user(dev_num, (__u16 __user *) arg))
959 return -EFAULT;
960
961 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
962 return -EINVAL;
963
964 size = sizeof(*dl) + dev_num * sizeof(*dr);
965
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200966 dl = kzalloc(size, GFP_KERNEL);
967 if (!dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return -ENOMEM;
969
970 dr = dl->dev_req;
971
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -0200972 read_lock(&hci_dev_list_lock);
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200973 list_for_each_entry(hdev, &hci_dev_list, list) {
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200974 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Johan Hedberge0f93092011-11-09 01:44:22 +0200975 cancel_delayed_work(&hdev->power_off);
Johan Hedbergc542a062011-01-26 13:11:03 +0200976
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200977 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
978 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergc542a062011-01-26 13:11:03 +0200979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 (dr + n)->dev_id = hdev->id;
981 (dr + n)->dev_opt = hdev->flags;
Johan Hedbergc542a062011-01-26 13:11:03 +0200982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (++n >= dev_num)
984 break;
985 }
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -0200986 read_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 dl->dev_num = n;
989 size = sizeof(*dl) + n * sizeof(*dr);
990
991 err = copy_to_user(arg, dl, size);
992 kfree(dl);
993
994 return err ? -EFAULT : 0;
995}
996
997int hci_get_dev_info(void __user *arg)
998{
999 struct hci_dev *hdev;
1000 struct hci_dev_info di;
1001 int err = 0;
1002
1003 if (copy_from_user(&di, arg, sizeof(di)))
1004 return -EFAULT;
1005
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001006 hdev = hci_dev_get(di.dev_id);
1007 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -ENODEV;
1009
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001010 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Johan Hedberg32435532011-11-07 22:16:04 +02001011 cancel_delayed_work_sync(&hdev->power_off);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001012
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001013 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
1014 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergc542a062011-01-26 13:11:03 +02001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 strcpy(di.name, hdev->name);
1017 di.bdaddr = hdev->bdaddr;
Marcel Holtmann943da252010-02-13 02:28:41 +01001018 di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 di.flags = hdev->flags;
1020 di.pkt_type = hdev->pkt_type;
1021 di.acl_mtu = hdev->acl_mtu;
1022 di.acl_pkts = hdev->acl_pkts;
1023 di.sco_mtu = hdev->sco_mtu;
1024 di.sco_pkts = hdev->sco_pkts;
1025 di.link_policy = hdev->link_policy;
1026 di.link_mode = hdev->link_mode;
1027
1028 memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
1029 memcpy(&di.features, &hdev->features, sizeof(di.features));
1030
1031 if (copy_to_user(arg, &di, sizeof(di)))
1032 err = -EFAULT;
1033
1034 hci_dev_put(hdev);
1035
1036 return err;
1037}
1038
1039/* ---- Interface to HCI drivers ---- */
1040
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001041static int hci_rfkill_set_block(void *data, bool blocked)
1042{
1043 struct hci_dev *hdev = data;
1044
1045 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
1046
1047 if (!blocked)
1048 return 0;
1049
1050 hci_dev_do_close(hdev);
1051
1052 return 0;
1053}
1054
1055static const struct rfkill_ops hci_rfkill_ops = {
1056 .set_block = hci_rfkill_set_block,
1057};
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059/* Alloc HCI device */
1060struct hci_dev *hci_alloc_dev(void)
1061{
1062 struct hci_dev *hdev;
1063
Marcel Holtmann25ea6db2006-07-06 15:40:09 +02001064 hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (!hdev)
1066 return NULL;
1067
David Herrmann0ac7e702011-10-08 14:58:47 +02001068 hci_init_sysfs(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 skb_queue_head_init(&hdev->driver_init);
1070
1071 return hdev;
1072}
1073EXPORT_SYMBOL(hci_alloc_dev);
1074
1075/* Free HCI device */
1076void hci_free_dev(struct hci_dev *hdev)
1077{
1078 skb_queue_purge(&hdev->driver_init);
1079
Marcel Holtmanna91f2e32006-07-03 10:02:41 +02001080 /* will free via device release */
1081 put_device(&hdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083EXPORT_SYMBOL(hci_free_dev);
1084
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001085static void hci_power_on(struct work_struct *work)
1086{
1087 struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
1088
1089 BT_DBG("%s", hdev->name);
1090
1091 if (hci_dev_open(hdev->id) < 0)
1092 return;
1093
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001094 if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Gustavo F. Padovan80b7ab32011-12-17 14:52:27 -02001095 schedule_delayed_work(&hdev->power_off,
Johan Hedberg32435532011-11-07 22:16:04 +02001096 msecs_to_jiffies(AUTO_OFF_TIMEOUT));
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001097
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001098 if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +02001099 mgmt_index_added(hdev);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001100}
1101
1102static void hci_power_off(struct work_struct *work)
1103{
Johan Hedberg32435532011-11-07 22:16:04 +02001104 struct hci_dev *hdev = container_of(work, struct hci_dev,
1105 power_off.work);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001106
1107 BT_DBG("%s", hdev->name);
1108
Marcel Holtmann8ee56542012-02-21 12:33:48 +01001109 hci_dev_do_close(hdev);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001110}
1111
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001112static void hci_discov_off(struct work_struct *work)
1113{
1114 struct hci_dev *hdev;
1115 u8 scan = SCAN_PAGE;
1116
1117 hdev = container_of(work, struct hci_dev, discov_off.work);
1118
1119 BT_DBG("%s", hdev->name);
1120
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001121 hci_dev_lock(hdev);
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001122
1123 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
1124
1125 hdev->discov_timeout = 0;
1126
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001127 hci_dev_unlock(hdev);
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001128}
1129
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001130int hci_uuids_clear(struct hci_dev *hdev)
1131{
1132 struct list_head *p, *n;
1133
1134 list_for_each_safe(p, n, &hdev->uuids) {
1135 struct bt_uuid *uuid;
1136
1137 uuid = list_entry(p, struct bt_uuid, list);
1138
1139 list_del(p);
1140 kfree(uuid);
1141 }
1142
1143 return 0;
1144}
1145
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001146int hci_link_keys_clear(struct hci_dev *hdev)
1147{
1148 struct list_head *p, *n;
1149
1150 list_for_each_safe(p, n, &hdev->link_keys) {
1151 struct link_key *key;
1152
1153 key = list_entry(p, struct link_key, list);
1154
1155 list_del(p);
1156 kfree(key);
1157 }
1158
1159 return 0;
1160}
1161
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001162int hci_smp_ltks_clear(struct hci_dev *hdev)
1163{
1164 struct smp_ltk *k, *tmp;
1165
1166 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1167 list_del(&k->list);
1168 kfree(k);
1169 }
1170
1171 return 0;
1172}
1173
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001174struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1175{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001176 struct link_key *k;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001177
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001178 list_for_each_entry(k, &hdev->link_keys, list)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001179 if (bacmp(bdaddr, &k->bdaddr) == 0)
1180 return k;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001181
1182 return NULL;
1183}
1184
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001185static int hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,
1186 u8 key_type, u8 old_key_type)
1187{
1188 /* Legacy key */
1189 if (key_type < 0x03)
1190 return 1;
1191
1192 /* Debug keys are insecure so don't store them persistently */
1193 if (key_type == HCI_LK_DEBUG_COMBINATION)
1194 return 0;
1195
1196 /* Changed combination key and there's no previous one */
1197 if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff)
1198 return 0;
1199
1200 /* Security mode 3 case */
1201 if (!conn)
1202 return 1;
1203
1204 /* Neither local nor remote side had no-bonding as requirement */
1205 if (conn->auth_type > 0x01 && conn->remote_auth > 0x01)
1206 return 1;
1207
1208 /* Local side had dedicated bonding as requirement */
1209 if (conn->auth_type == 0x02 || conn->auth_type == 0x03)
1210 return 1;
1211
1212 /* Remote side had dedicated bonding as requirement */
1213 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03)
1214 return 1;
1215
1216 /* If none of the above criteria match, then don't store the key
1217 * persistently */
1218 return 0;
1219}
1220
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001221struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8])
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001222{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001223 struct smp_ltk *k;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001224
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001225 list_for_each_entry(k, &hdev->long_term_keys, list) {
1226 if (k->ediv != ediv ||
1227 memcmp(rand, k->rand, sizeof(k->rand)))
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001228 continue;
1229
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001230 return k;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001231 }
1232
1233 return NULL;
1234}
1235EXPORT_SYMBOL(hci_find_ltk);
1236
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001237struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
1238 u8 addr_type)
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001239{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001240 struct smp_ltk *k;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001241
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001242 list_for_each_entry(k, &hdev->long_term_keys, list)
1243 if (addr_type == k->bdaddr_type &&
1244 bacmp(bdaddr, &k->bdaddr) == 0)
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001245 return k;
1246
1247 return NULL;
1248}
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001249EXPORT_SYMBOL(hci_find_ltk_by_addr);
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001250
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001251int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
1252 bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001253{
1254 struct link_key *key, *old_key;
Johan Hedberg4df378a2011-04-28 11:29:03 -07001255 u8 old_key_type, persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001256
1257 old_key = hci_find_link_key(hdev, bdaddr);
1258 if (old_key) {
1259 old_key_type = old_key->type;
1260 key = old_key;
1261 } else {
Johan Hedberg12adcf32011-04-28 11:29:00 -07001262 old_key_type = conn ? conn->key_type : 0xff;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001263 key = kzalloc(sizeof(*key), GFP_ATOMIC);
1264 if (!key)
1265 return -ENOMEM;
1266 list_add(&key->list, &hdev->link_keys);
1267 }
1268
1269 BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type);
1270
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001271 /* Some buggy controller combinations generate a changed
1272 * combination key for legacy pairing even when there's no
1273 * previous key */
1274 if (type == HCI_LK_CHANGED_COMBINATION &&
1275 (!conn || conn->remote_auth == 0xff) &&
Johan Hedberg655fe6e2011-04-28 11:29:01 -07001276 old_key_type == 0xff) {
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001277 type = HCI_LK_COMBINATION;
Johan Hedberg655fe6e2011-04-28 11:29:01 -07001278 if (conn)
1279 conn->key_type = type;
1280 }
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001281
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001282 bacpy(&key->bdaddr, bdaddr);
1283 memcpy(key->val, val, 16);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001284 key->pin_len = pin_len;
1285
Waldemar Rymarkiewiczb6020ba2011-04-28 12:07:53 +02001286 if (type == HCI_LK_CHANGED_COMBINATION)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001287 key->type = old_key_type;
Johan Hedberg4748fed2011-04-28 11:29:02 -07001288 else
1289 key->type = type;
1290
Johan Hedberg4df378a2011-04-28 11:29:03 -07001291 if (!new_key)
1292 return 0;
1293
1294 persistent = hci_persistent_key(hdev, conn, type, old_key_type);
1295
Johan Hedberg744cf192011-11-08 20:40:14 +02001296 mgmt_new_link_key(hdev, key, persistent);
Johan Hedberg4df378a2011-04-28 11:29:03 -07001297
1298 if (!persistent) {
1299 list_del(&key->list);
1300 kfree(key);
1301 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001302
1303 return 0;
1304}
1305
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001306int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type,
1307 int new_key, u8 authenticated, u8 tk[16],
1308 u8 enc_size, u16 ediv, u8 rand[8])
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001309{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001310 struct smp_ltk *key, *old_key;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001311
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001312 if (!(type & HCI_SMP_STK) && !(type & HCI_SMP_LTK))
1313 return 0;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001314
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001315 old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type);
1316 if (old_key)
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001317 key = old_key;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001318 else {
1319 key = kzalloc(sizeof(*key), GFP_ATOMIC);
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001320 if (!key)
1321 return -ENOMEM;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001322 list_add(&key->list, &hdev->long_term_keys);
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001323 }
1324
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001325 bacpy(&key->bdaddr, bdaddr);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001326 key->bdaddr_type = addr_type;
1327 memcpy(key->val, tk, sizeof(key->val));
1328 key->authenticated = authenticated;
1329 key->ediv = ediv;
1330 key->enc_size = enc_size;
1331 key->type = type;
1332 memcpy(key->rand, rand, sizeof(key->rand));
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001333
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001334 if (!new_key)
1335 return 0;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001336
Vinicius Costa Gomes261cc5a2012-02-02 21:08:05 -03001337 if (type & HCI_SMP_LTK)
1338 mgmt_new_ltk(hdev, key, 1);
1339
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001340 return 0;
1341}
1342
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001343int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1344{
1345 struct link_key *key;
1346
1347 key = hci_find_link_key(hdev, bdaddr);
1348 if (!key)
1349 return -ENOENT;
1350
1351 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1352
1353 list_del(&key->list);
1354 kfree(key);
1355
1356 return 0;
1357}
1358
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001359int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr)
1360{
1361 struct smp_ltk *k, *tmp;
1362
1363 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1364 if (bacmp(bdaddr, &k->bdaddr))
1365 continue;
1366
1367 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1368
1369 list_del(&k->list);
1370 kfree(k);
1371 }
1372
1373 return 0;
1374}
1375
Ville Tervo6bd32322011-02-16 16:32:41 +02001376/* HCI command timer function */
1377static void hci_cmd_timer(unsigned long arg)
1378{
1379 struct hci_dev *hdev = (void *) arg;
1380
1381 BT_ERR("%s command tx timeout", hdev->name);
1382 atomic_set(&hdev->cmd_cnt, 1);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02001383 queue_work(hdev->workqueue, &hdev->cmd_work);
Ville Tervo6bd32322011-02-16 16:32:41 +02001384}
1385
Szymon Janc2763eda2011-03-22 13:12:22 +01001386struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
1387 bdaddr_t *bdaddr)
1388{
1389 struct oob_data *data;
1390
1391 list_for_each_entry(data, &hdev->remote_oob_data, list)
1392 if (bacmp(bdaddr, &data->bdaddr) == 0)
1393 return data;
1394
1395 return NULL;
1396}
1397
1398int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr)
1399{
1400 struct oob_data *data;
1401
1402 data = hci_find_remote_oob_data(hdev, bdaddr);
1403 if (!data)
1404 return -ENOENT;
1405
1406 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1407
1408 list_del(&data->list);
1409 kfree(data);
1410
1411 return 0;
1412}
1413
1414int hci_remote_oob_data_clear(struct hci_dev *hdev)
1415{
1416 struct oob_data *data, *n;
1417
1418 list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) {
1419 list_del(&data->list);
1420 kfree(data);
1421 }
1422
1423 return 0;
1424}
1425
1426int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
1427 u8 *randomizer)
1428{
1429 struct oob_data *data;
1430
1431 data = hci_find_remote_oob_data(hdev, bdaddr);
1432
1433 if (!data) {
1434 data = kmalloc(sizeof(*data), GFP_ATOMIC);
1435 if (!data)
1436 return -ENOMEM;
1437
1438 bacpy(&data->bdaddr, bdaddr);
1439 list_add(&data->list, &hdev->remote_oob_data);
1440 }
1441
1442 memcpy(data->hash, hash, sizeof(data->hash));
1443 memcpy(data->randomizer, randomizer, sizeof(data->randomizer));
1444
1445 BT_DBG("%s for %s", hdev->name, batostr(bdaddr));
1446
1447 return 0;
1448}
1449
Antti Julkub2a66aa2011-06-15 12:01:14 +03001450struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
1451 bdaddr_t *bdaddr)
1452{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001453 struct bdaddr_list *b;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001454
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001455 list_for_each_entry(b, &hdev->blacklist, list)
Antti Julkub2a66aa2011-06-15 12:01:14 +03001456 if (bacmp(bdaddr, &b->bdaddr) == 0)
1457 return b;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001458
1459 return NULL;
1460}
1461
1462int hci_blacklist_clear(struct hci_dev *hdev)
1463{
1464 struct list_head *p, *n;
1465
1466 list_for_each_safe(p, n, &hdev->blacklist) {
1467 struct bdaddr_list *b;
1468
1469 b = list_entry(p, struct bdaddr_list, list);
1470
1471 list_del(p);
1472 kfree(b);
1473 }
1474
1475 return 0;
1476}
1477
Johan Hedberg88c1fe42012-02-09 15:56:11 +02001478int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
Antti Julkub2a66aa2011-06-15 12:01:14 +03001479{
1480 struct bdaddr_list *entry;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001481
1482 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1483 return -EBADF;
1484
Antti Julku5e762442011-08-25 16:48:02 +03001485 if (hci_blacklist_lookup(hdev, bdaddr))
1486 return -EEXIST;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001487
1488 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
Antti Julku5e762442011-08-25 16:48:02 +03001489 if (!entry)
1490 return -ENOMEM;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001491
1492 bacpy(&entry->bdaddr, bdaddr);
1493
1494 list_add(&entry->list, &hdev->blacklist);
1495
Johan Hedberg88c1fe42012-02-09 15:56:11 +02001496 return mgmt_device_blocked(hdev, bdaddr, type);
Antti Julkub2a66aa2011-06-15 12:01:14 +03001497}
1498
Johan Hedberg88c1fe42012-02-09 15:56:11 +02001499int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
Antti Julkub2a66aa2011-06-15 12:01:14 +03001500{
1501 struct bdaddr_list *entry;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001502
Szymon Janc1ec918c2011-11-16 09:32:21 +01001503 if (bacmp(bdaddr, BDADDR_ANY) == 0)
Antti Julku5e762442011-08-25 16:48:02 +03001504 return hci_blacklist_clear(hdev);
Antti Julkub2a66aa2011-06-15 12:01:14 +03001505
1506 entry = hci_blacklist_lookup(hdev, bdaddr);
Szymon Janc1ec918c2011-11-16 09:32:21 +01001507 if (!entry)
Antti Julku5e762442011-08-25 16:48:02 +03001508 return -ENOENT;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001509
1510 list_del(&entry->list);
1511 kfree(entry);
1512
Johan Hedberg88c1fe42012-02-09 15:56:11 +02001513 return mgmt_device_unblocked(hdev, bdaddr, type);
Antti Julkub2a66aa2011-06-15 12:01:14 +03001514}
1515
Gustavo F. Padovandb323f22011-06-20 16:39:29 -03001516static void hci_clear_adv_cache(struct work_struct *work)
Andre Guedes35815082011-05-26 16:23:53 -03001517{
Gustavo F. Padovandb323f22011-06-20 16:39:29 -03001518 struct hci_dev *hdev = container_of(work, struct hci_dev,
1519 adv_work.work);
Andre Guedes35815082011-05-26 16:23:53 -03001520
1521 hci_dev_lock(hdev);
1522
1523 hci_adv_entries_clear(hdev);
1524
1525 hci_dev_unlock(hdev);
1526}
1527
Andre Guedes76c86862011-05-26 16:23:50 -03001528int hci_adv_entries_clear(struct hci_dev *hdev)
1529{
1530 struct adv_entry *entry, *tmp;
1531
1532 list_for_each_entry_safe(entry, tmp, &hdev->adv_entries, list) {
1533 list_del(&entry->list);
1534 kfree(entry);
1535 }
1536
1537 BT_DBG("%s adv cache cleared", hdev->name);
1538
1539 return 0;
1540}
1541
1542struct adv_entry *hci_find_adv_entry(struct hci_dev *hdev, bdaddr_t *bdaddr)
1543{
1544 struct adv_entry *entry;
1545
1546 list_for_each_entry(entry, &hdev->adv_entries, list)
1547 if (bacmp(bdaddr, &entry->bdaddr) == 0)
1548 return entry;
1549
1550 return NULL;
1551}
1552
1553static inline int is_connectable_adv(u8 evt_type)
1554{
1555 if (evt_type == ADV_IND || evt_type == ADV_DIRECT_IND)
1556 return 1;
1557
1558 return 0;
1559}
1560
1561int hci_add_adv_entry(struct hci_dev *hdev,
1562 struct hci_ev_le_advertising_info *ev)
1563{
1564 struct adv_entry *entry;
1565
1566 if (!is_connectable_adv(ev->evt_type))
1567 return -EINVAL;
1568
1569 /* Only new entries should be added to adv_entries. So, if
1570 * bdaddr was found, don't add it. */
1571 if (hci_find_adv_entry(hdev, &ev->bdaddr))
1572 return 0;
1573
Andre Guedes4777bfd2012-01-30 23:31:28 -03001574 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Andre Guedes76c86862011-05-26 16:23:50 -03001575 if (!entry)
1576 return -ENOMEM;
1577
1578 bacpy(&entry->bdaddr, &ev->bdaddr);
1579 entry->bdaddr_type = ev->bdaddr_type;
1580
1581 list_add(&entry->list, &hdev->adv_entries);
1582
1583 BT_DBG("%s adv entry added: address %s type %u", hdev->name,
1584 batostr(&entry->bdaddr), entry->bdaddr_type);
1585
1586 return 0;
1587}
1588
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001589static void le_scan_param_req(struct hci_dev *hdev, unsigned long opt)
1590{
1591 struct le_scan_params *param = (struct le_scan_params *) opt;
1592 struct hci_cp_le_set_scan_param cp;
1593
1594 memset(&cp, 0, sizeof(cp));
1595 cp.type = param->type;
1596 cp.interval = cpu_to_le16(param->interval);
1597 cp.window = cpu_to_le16(param->window);
1598
1599 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_PARAM, sizeof(cp), &cp);
1600}
1601
1602static void le_scan_enable_req(struct hci_dev *hdev, unsigned long opt)
1603{
1604 struct hci_cp_le_set_scan_enable cp;
1605
1606 memset(&cp, 0, sizeof(cp));
1607 cp.enable = 1;
1608
1609 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
1610}
1611
1612static int hci_do_le_scan(struct hci_dev *hdev, u8 type, u16 interval,
1613 u16 window, int timeout)
1614{
1615 long timeo = msecs_to_jiffies(3000);
1616 struct le_scan_params param;
1617 int err;
1618
1619 BT_DBG("%s", hdev->name);
1620
1621 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))
1622 return -EINPROGRESS;
1623
1624 param.type = type;
1625 param.interval = interval;
1626 param.window = window;
1627
1628 hci_req_lock(hdev);
1629
1630 err = __hci_request(hdev, le_scan_param_req, (unsigned long) &param,
1631 timeo);
1632 if (!err)
1633 err = __hci_request(hdev, le_scan_enable_req, 0, timeo);
1634
1635 hci_req_unlock(hdev);
1636
1637 if (err < 0)
1638 return err;
1639
1640 schedule_delayed_work(&hdev->le_scan_disable,
1641 msecs_to_jiffies(timeout));
1642
1643 return 0;
1644}
1645
1646static void le_scan_disable_work(struct work_struct *work)
1647{
1648 struct hci_dev *hdev = container_of(work, struct hci_dev,
1649 le_scan_disable.work);
1650 struct hci_cp_le_set_scan_enable cp;
1651
1652 BT_DBG("%s", hdev->name);
1653
1654 memset(&cp, 0, sizeof(cp));
1655
1656 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
1657}
1658
Andre Guedes28b75a82012-02-03 17:48:00 -03001659static void le_scan_work(struct work_struct *work)
1660{
1661 struct hci_dev *hdev = container_of(work, struct hci_dev, le_scan);
1662 struct le_scan_params *param = &hdev->le_scan_params;
1663
1664 BT_DBG("%s", hdev->name);
1665
1666 hci_do_le_scan(hdev, param->type, param->interval,
1667 param->window, param->timeout);
1668}
1669
1670int hci_le_scan(struct hci_dev *hdev, u8 type, u16 interval, u16 window,
1671 int timeout)
1672{
1673 struct le_scan_params *param = &hdev->le_scan_params;
1674
1675 BT_DBG("%s", hdev->name);
1676
1677 if (work_busy(&hdev->le_scan))
1678 return -EINPROGRESS;
1679
1680 param->type = type;
1681 param->interval = interval;
1682 param->window = window;
1683 param->timeout = timeout;
1684
1685 queue_work(system_long_wq, &hdev->le_scan);
1686
1687 return 0;
1688}
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690/* Register HCI device */
1691int hci_register_dev(struct hci_dev *hdev)
1692{
1693 struct list_head *head = &hci_dev_list, *p;
Mat Martineau08add512011-11-02 16:18:36 -07001694 int i, id, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
David Herrmanne9b9cfa2012-01-07 15:47:22 +01001696 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
David Herrmann010666a2012-01-07 15:47:07 +01001698 if (!hdev->open || !hdev->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return -EINVAL;
1700
Mat Martineau08add512011-11-02 16:18:36 -07001701 /* Do not allow HCI_AMP devices to register at index 0,
1702 * so the index can be used as the AMP controller ID.
1703 */
1704 id = (hdev->dev_type == HCI_BREDR) ? 0 : 1;
1705
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001706 write_lock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 /* Find first available device id */
1709 list_for_each(p, &hci_dev_list) {
1710 if (list_entry(p, struct hci_dev, list)->id != id)
1711 break;
1712 head = p; id++;
1713 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 sprintf(hdev->name, "hci%d", id);
1716 hdev->id = id;
Andrei Emeltchenkoc6feeb22011-11-16 17:30:20 +02001717 list_add_tail(&hdev->list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001719 mutex_init(&hdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 hdev->flags = 0;
Andre Guedesd23264a2011-11-25 20:53:38 -03001722 hdev->dev_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001724 hdev->esco_type = (ESCO_HV1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 hdev->link_mode = (HCI_LM_ACCEPT);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001726 hdev->io_capability = 0x03; /* No Input No Output */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Marcel Holtmann04837f62006-07-03 10:02:33 +02001728 hdev->idle_timeout = 0;
1729 hdev->sniff_max_interval = 800;
1730 hdev->sniff_min_interval = 80;
1731
Marcel Holtmannb78752c2010-08-08 23:06:53 -04001732 INIT_WORK(&hdev->rx_work, hci_rx_work);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02001733 INIT_WORK(&hdev->cmd_work, hci_cmd_work);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02001734 INIT_WORK(&hdev->tx_work, hci_tx_work);
Marcel Holtmannb78752c2010-08-08 23:06:53 -04001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 skb_queue_head_init(&hdev->rx_q);
1738 skb_queue_head_init(&hdev->cmd_q);
1739 skb_queue_head_init(&hdev->raw_q);
1740
Ville Tervo6bd32322011-02-16 16:32:41 +02001741 setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev);
1742
Suraj Sumangalacd4c5392010-07-14 13:02:16 +05301743 for (i = 0; i < NUM_REASSEMBLY; i++)
Marcel Holtmannef222012007-07-11 06:42:04 +02001744 hdev->reassembly[i] = NULL;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 init_waitqueue_head(&hdev->req_wait_q);
Thomas Gleixnera6a67ef2009-07-26 08:18:19 +00001747 mutex_init(&hdev->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Johan Hedberg30883512012-01-04 14:16:21 +02001749 discovery_init(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 hci_conn_hash_init(hdev);
1752
Johan Hedberg2e58ef32011-11-08 20:40:15 +02001753 INIT_LIST_HEAD(&hdev->mgmt_pending);
1754
David Millerea4bd8b2010-07-30 21:54:49 -07001755 INIT_LIST_HEAD(&hdev->blacklist);
Johan Hedbergf0358562010-05-18 13:20:32 +02001756
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001757 INIT_LIST_HEAD(&hdev->uuids);
1758
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001759 INIT_LIST_HEAD(&hdev->link_keys);
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001760 INIT_LIST_HEAD(&hdev->long_term_keys);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001761
Szymon Janc2763eda2011-03-22 13:12:22 +01001762 INIT_LIST_HEAD(&hdev->remote_oob_data);
1763
Andre Guedes76c86862011-05-26 16:23:50 -03001764 INIT_LIST_HEAD(&hdev->adv_entries);
1765
Gustavo F. Padovandb323f22011-06-20 16:39:29 -03001766 INIT_DELAYED_WORK(&hdev->adv_work, hci_clear_adv_cache);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001767 INIT_WORK(&hdev->power_on, hci_power_on);
Johan Hedberg32435532011-11-07 22:16:04 +02001768 INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001769
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001770 INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
1773
1774 atomic_set(&hdev->promisc, 0);
1775
Andre Guedes28b75a82012-02-03 17:48:00 -03001776 INIT_WORK(&hdev->le_scan, le_scan_work);
1777
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001778 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
1779
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001780 write_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Gustavo F. Padovan32845eb2011-12-17 17:47:30 -02001782 hdev->workqueue = alloc_workqueue(hdev->name, WQ_HIGHPRI | WQ_UNBOUND |
1783 WQ_MEM_RECLAIM, 1);
David Herrmann33ca9542011-10-08 14:58:49 +02001784 if (!hdev->workqueue) {
1785 error = -ENOMEM;
1786 goto err;
1787 }
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001788
David Herrmann33ca9542011-10-08 14:58:49 +02001789 error = hci_add_sysfs(hdev);
1790 if (error < 0)
1791 goto err_wqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001793 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
1794 RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, hdev);
1795 if (hdev->rfkill) {
1796 if (rfkill_register(hdev->rfkill) < 0) {
1797 rfkill_destroy(hdev->rfkill);
1798 hdev->rfkill = NULL;
1799 }
1800 }
1801
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001802 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
1803 set_bit(HCI_SETUP, &hdev->dev_flags);
Gustavo F. Padovan7f971042011-12-18 12:40:32 -02001804 schedule_work(&hdev->power_on);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 hci_notify(hdev, HCI_DEV_REG);
David Herrmanndc946bd2012-01-07 15:47:24 +01001807 hci_dev_hold(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 return id;
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001810
David Herrmann33ca9542011-10-08 14:58:49 +02001811err_wqueue:
1812 destroy_workqueue(hdev->workqueue);
1813err:
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001814 write_lock(&hci_dev_list_lock);
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001815 list_del(&hdev->list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001816 write_unlock(&hci_dev_list_lock);
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001817
David Herrmann33ca9542011-10-08 14:58:49 +02001818 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820EXPORT_SYMBOL(hci_register_dev);
1821
1822/* Unregister HCI device */
David Herrmann59735632011-10-26 10:43:19 +02001823void hci_unregister_dev(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Marcel Holtmannef222012007-07-11 06:42:04 +02001825 int i;
1826
Marcel Holtmannc13854c2010-02-08 15:27:07 +01001827 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001829 write_lock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 list_del(&hdev->list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001831 write_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 hci_dev_do_close(hdev);
1834
Suraj Sumangalacd4c5392010-07-14 13:02:16 +05301835 for (i = 0; i < NUM_REASSEMBLY; i++)
Marcel Holtmannef222012007-07-11 06:42:04 +02001836 kfree_skb(hdev->reassembly[i]);
1837
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001838 if (!test_bit(HCI_INIT, &hdev->flags) &&
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001839 !test_bit(HCI_SETUP, &hdev->dev_flags)) {
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001840 hci_dev_lock(hdev);
Johan Hedberg744cf192011-11-08 20:40:14 +02001841 mgmt_index_removed(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001842 hci_dev_unlock(hdev);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001843 }
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001844
Johan Hedberg2e58ef32011-11-08 20:40:15 +02001845 /* mgmt_index_removed should take care of emptying the
1846 * pending list */
1847 BUG_ON(!list_empty(&hdev->mgmt_pending));
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 hci_notify(hdev, HCI_DEV_UNREG);
1850
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001851 if (hdev->rfkill) {
1852 rfkill_unregister(hdev->rfkill);
1853 rfkill_destroy(hdev->rfkill);
1854 }
1855
David Herrmannce242972011-10-08 14:58:48 +02001856 hci_del_sysfs(hdev);
Dave Young147e2d52008-03-05 18:45:59 -08001857
Gustavo F. Padovandb323f22011-06-20 16:39:29 -03001858 cancel_delayed_work_sync(&hdev->adv_work);
Gustavo F. Padovanc6f3c5f2011-02-15 20:22:03 -03001859
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01001860 destroy_workqueue(hdev->workqueue);
1861
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001862 hci_dev_lock(hdev);
Johan Hedberge2e0cac2011-01-04 12:08:50 +02001863 hci_blacklist_clear(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001864 hci_uuids_clear(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001865 hci_link_keys_clear(hdev);
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001866 hci_smp_ltks_clear(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01001867 hci_remote_oob_data_clear(hdev);
Andre Guedes76c86862011-05-26 16:23:50 -03001868 hci_adv_entries_clear(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001869 hci_dev_unlock(hdev);
Johan Hedberge2e0cac2011-01-04 12:08:50 +02001870
David Herrmanndc946bd2012-01-07 15:47:24 +01001871 hci_dev_put(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873EXPORT_SYMBOL(hci_unregister_dev);
1874
1875/* Suspend HCI device */
1876int hci_suspend_dev(struct hci_dev *hdev)
1877{
1878 hci_notify(hdev, HCI_DEV_SUSPEND);
1879 return 0;
1880}
1881EXPORT_SYMBOL(hci_suspend_dev);
1882
1883/* Resume HCI device */
1884int hci_resume_dev(struct hci_dev *hdev)
1885{
1886 hci_notify(hdev, HCI_DEV_RESUME);
1887 return 0;
1888}
1889EXPORT_SYMBOL(hci_resume_dev);
1890
Marcel Holtmann76bca882009-11-18 00:40:39 +01001891/* Receive frame from HCI drivers */
1892int hci_recv_frame(struct sk_buff *skb)
1893{
1894 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
1895 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
1896 && !test_bit(HCI_INIT, &hdev->flags))) {
1897 kfree_skb(skb);
1898 return -ENXIO;
1899 }
1900
1901 /* Incomming skb */
1902 bt_cb(skb)->incoming = 1;
1903
1904 /* Time stamp */
1905 __net_timestamp(skb);
1906
Marcel Holtmann76bca882009-11-18 00:40:39 +01001907 skb_queue_tail(&hdev->rx_q, skb);
Marcel Holtmannb78752c2010-08-08 23:06:53 -04001908 queue_work(hdev->workqueue, &hdev->rx_work);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001909
Marcel Holtmann76bca882009-11-18 00:40:39 +01001910 return 0;
1911}
1912EXPORT_SYMBOL(hci_recv_frame);
1913
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301914static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001915 int count, __u8 index)
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301916{
1917 int len = 0;
1918 int hlen = 0;
1919 int remain = count;
1920 struct sk_buff *skb;
1921 struct bt_skb_cb *scb;
1922
1923 if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
1924 index >= NUM_REASSEMBLY)
1925 return -EILSEQ;
1926
1927 skb = hdev->reassembly[index];
1928
1929 if (!skb) {
1930 switch (type) {
1931 case HCI_ACLDATA_PKT:
1932 len = HCI_MAX_FRAME_SIZE;
1933 hlen = HCI_ACL_HDR_SIZE;
1934 break;
1935 case HCI_EVENT_PKT:
1936 len = HCI_MAX_EVENT_SIZE;
1937 hlen = HCI_EVENT_HDR_SIZE;
1938 break;
1939 case HCI_SCODATA_PKT:
1940 len = HCI_MAX_SCO_SIZE;
1941 hlen = HCI_SCO_HDR_SIZE;
1942 break;
1943 }
1944
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03001945 skb = bt_skb_alloc(len, GFP_ATOMIC);
Suraj Sumangala33e882a2010-07-14 13:02:17 +05301946 if (!skb)
1947 return -ENOMEM;
1948
1949 scb = (void *) skb->cb;
1950 scb->expect = hlen;
1951 scb->pkt_type = type;
1952
1953 skb->dev = (void *) hdev;
1954 hdev->reassembly[index] = skb;
1955 }
1956
1957 while (count) {
1958 scb = (void *) skb->cb;
1959 len = min(scb->expect, (__u16)count);
1960
1961 memcpy(skb_put(skb, len), data, len);
1962
1963 count -= len;
1964 data += len;
1965 scb->expect -= len;
1966 remain = count;
1967
1968 switch (type) {
1969 case HCI_EVENT_PKT:
1970 if (skb->len == HCI_EVENT_HDR_SIZE) {
1971 struct hci_event_hdr *h = hci_event_hdr(skb);
1972 scb->expect = h->plen;
1973
1974 if (skb_tailroom(skb) < scb->expect) {
1975 kfree_skb(skb);
1976 hdev->reassembly[index] = NULL;
1977 return -ENOMEM;
1978 }
1979 }
1980 break;
1981
1982 case HCI_ACLDATA_PKT:
1983 if (skb->len == HCI_ACL_HDR_SIZE) {
1984 struct hci_acl_hdr *h = hci_acl_hdr(skb);
1985 scb->expect = __le16_to_cpu(h->dlen);
1986
1987 if (skb_tailroom(skb) < scb->expect) {
1988 kfree_skb(skb);
1989 hdev->reassembly[index] = NULL;
1990 return -ENOMEM;
1991 }
1992 }
1993 break;
1994
1995 case HCI_SCODATA_PKT:
1996 if (skb->len == HCI_SCO_HDR_SIZE) {
1997 struct hci_sco_hdr *h = hci_sco_hdr(skb);
1998 scb->expect = h->dlen;
1999
2000 if (skb_tailroom(skb) < scb->expect) {
2001 kfree_skb(skb);
2002 hdev->reassembly[index] = NULL;
2003 return -ENOMEM;
2004 }
2005 }
2006 break;
2007 }
2008
2009 if (scb->expect == 0) {
2010 /* Complete frame */
2011
2012 bt_cb(skb)->pkt_type = type;
2013 hci_recv_frame(skb);
2014
2015 hdev->reassembly[index] = NULL;
2016 return remain;
2017 }
2018 }
2019
2020 return remain;
2021}
2022
Marcel Holtmannef222012007-07-11 06:42:04 +02002023int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
2024{
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05302025 int rem = 0;
2026
Marcel Holtmannef222012007-07-11 06:42:04 +02002027 if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
2028 return -EILSEQ;
2029
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03002030 while (count) {
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03002031 rem = hci_reassembly(hdev, type, data, count, type - 1);
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05302032 if (rem < 0)
2033 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02002034
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05302035 data += (count - rem);
2036 count = rem;
Joe Perchesf81c6222011-06-03 11:51:19 +00002037 }
Marcel Holtmannef222012007-07-11 06:42:04 +02002038
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05302039 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02002040}
2041EXPORT_SYMBOL(hci_recv_fragment);
2042
Suraj Sumangala99811512010-07-14 13:02:19 +05302043#define STREAM_REASSEMBLY 0
2044
2045int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
2046{
2047 int type;
2048 int rem = 0;
2049
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03002050 while (count) {
Suraj Sumangala99811512010-07-14 13:02:19 +05302051 struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
2052
2053 if (!skb) {
2054 struct { char type; } *pkt;
2055
2056 /* Start of the frame */
2057 pkt = data;
2058 type = pkt->type;
2059
2060 data++;
2061 count--;
2062 } else
2063 type = bt_cb(skb)->pkt_type;
2064
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03002065 rem = hci_reassembly(hdev, type, data, count,
2066 STREAM_REASSEMBLY);
Suraj Sumangala99811512010-07-14 13:02:19 +05302067 if (rem < 0)
2068 return rem;
2069
2070 data += (count - rem);
2071 count = rem;
Joe Perchesf81c6222011-06-03 11:51:19 +00002072 }
Suraj Sumangala99811512010-07-14 13:02:19 +05302073
2074 return rem;
2075}
2076EXPORT_SYMBOL(hci_recv_stream_fragment);
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078/* ---- Interface to upper protocols ---- */
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080int hci_register_cb(struct hci_cb *cb)
2081{
2082 BT_DBG("%p name %s", cb, cb->name);
2083
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002084 write_lock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 list_add(&cb->list, &hci_cb_list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002086 write_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 return 0;
2089}
2090EXPORT_SYMBOL(hci_register_cb);
2091
2092int hci_unregister_cb(struct hci_cb *cb)
2093{
2094 BT_DBG("%p name %s", cb, cb->name);
2095
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002096 write_lock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 list_del(&cb->list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002098 write_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 return 0;
2101}
2102EXPORT_SYMBOL(hci_unregister_cb);
2103
2104static int hci_send_frame(struct sk_buff *skb)
2105{
2106 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2107
2108 if (!hdev) {
2109 kfree_skb(skb);
2110 return -ENODEV;
2111 }
2112
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002113 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Marcel Holtmanncd82e612012-02-20 20:34:38 +01002115 /* Time stamp */
2116 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Marcel Holtmanncd82e612012-02-20 20:34:38 +01002118 /* Send copy to monitor */
2119 hci_send_to_monitor(hdev, skb);
2120
2121 if (atomic_read(&hdev->promisc)) {
2122 /* Send copy to the sockets */
Marcel Holtmann470fe1b2012-02-20 14:50:30 +01002123 hci_send_to_sock(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
2126 /* Get rid of skb owner, prior to sending to the driver. */
2127 skb_orphan(skb);
2128
2129 return hdev->send(skb);
2130}
2131
2132/* Send HCI command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002133int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 int len = HCI_COMMAND_HDR_SIZE + plen;
2136 struct hci_command_hdr *hdr;
2137 struct sk_buff *skb;
2138
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002139 BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 skb = bt_skb_alloc(len, GFP_ATOMIC);
2142 if (!skb) {
Marcel Holtmannef222012007-07-11 06:42:04 +02002143 BT_ERR("%s no memory for command", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return -ENOMEM;
2145 }
2146
2147 hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002148 hdr->opcode = cpu_to_le16(opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 hdr->plen = plen;
2150
2151 if (plen)
2152 memcpy(skb_put(skb, plen), param, plen);
2153
2154 BT_DBG("skb len %d", skb->len);
2155
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002156 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 skb->dev = (void *) hdev;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002158
Johan Hedberga5040ef2011-01-10 13:28:59 +02002159 if (test_bit(HCI_INIT, &hdev->flags))
2160 hdev->init_last_cmd = opcode;
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 skb_queue_tail(&hdev->cmd_q, skb);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002163 queue_work(hdev->workqueue, &hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 return 0;
2166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168/* Get data from the previously sent command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002169void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 struct hci_command_hdr *hdr;
2172
2173 if (!hdev->sent_cmd)
2174 return NULL;
2175
2176 hdr = (void *) hdev->sent_cmd->data;
2177
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002178 if (hdr->opcode != cpu_to_le16(opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return NULL;
2180
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002181 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
2184}
2185
2186/* Send ACL data */
2187static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
2188{
2189 struct hci_acl_hdr *hdr;
2190 int len = skb->len;
2191
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03002192 skb_push(skb, HCI_ACL_HDR_SIZE);
2193 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002194 hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002195 hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
2196 hdr->dlen = cpu_to_le16(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002199static void hci_queue_acl(struct hci_conn *conn, struct sk_buff_head *queue,
2200 struct sk_buff *skb, __u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 struct hci_dev *hdev = conn->hdev;
2203 struct sk_buff *list;
2204
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002205 list = skb_shinfo(skb)->frag_list;
2206 if (!list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /* Non fragmented */
2208 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
2209
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002210 skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 } else {
2212 /* Fragmented */
2213 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2214
2215 skb_shinfo(skb)->frag_list = NULL;
2216
2217 /* Queue all fragments atomically */
Gustavo F. Padovanaf3e6352011-12-22 16:35:05 -02002218 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002220 __skb_queue_tail(queue, skb);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02002221
2222 flags &= ~ACL_START;
2223 flags |= ACL_CONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 do {
2225 skb = list; list = list->next;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002228 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02002229 hci_add_acl_hdr(skb, conn->handle, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2232
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002233 __skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 } while (list);
2235
Gustavo F. Padovanaf3e6352011-12-22 16:35:05 -02002236 spin_unlock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002238}
2239
2240void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
2241{
2242 struct hci_conn *conn = chan->conn;
2243 struct hci_dev *hdev = conn->hdev;
2244
2245 BT_DBG("%s chan %p flags 0x%x", hdev->name, chan, flags);
2246
2247 skb->dev = (void *) hdev;
2248 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
2249 hci_add_acl_hdr(skb, conn->handle, flags);
2250
2251 hci_queue_acl(conn, &chan->data_q, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002253 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255EXPORT_SYMBOL(hci_send_acl);
2256
2257/* Send SCO data */
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -03002258void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 struct hci_dev *hdev = conn->hdev;
2261 struct hci_sco_hdr hdr;
2262
2263 BT_DBG("%s len %d", hdev->name, skb->len);
2264
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002265 hdr.handle = cpu_to_le16(conn->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 hdr.dlen = skb->len;
2267
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03002268 skb_push(skb, HCI_SCO_HDR_SIZE);
2269 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002270 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002273 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 skb_queue_tail(&conn->data_q, skb);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002276 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278EXPORT_SYMBOL(hci_send_sco);
2279
2280/* ---- HCI TX task (outgoing data) ---- */
2281
2282/* HCI Connection scheduler */
2283static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote)
2284{
2285 struct hci_conn_hash *h = &hdev->conn_hash;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002286 struct hci_conn *conn = NULL, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 int num = 0, min = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002289 /* We don't have to lock device here. Connections are always
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 * added and removed with TX task disabled. */
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002291
2292 rcu_read_lock();
2293
2294 list_for_each_entry_rcu(c, &h->list, list) {
Marcel Holtmann769be972008-07-14 20:13:49 +02002295 if (c->type != type || skb_queue_empty(&c->data_q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 continue;
Marcel Holtmann769be972008-07-14 20:13:49 +02002297
2298 if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
2299 continue;
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 num++;
2302
2303 if (c->sent < min) {
2304 min = c->sent;
2305 conn = c;
2306 }
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002307
2308 if (hci_conn_num(hdev, type) == num)
2309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
2311
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002312 rcu_read_unlock();
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (conn) {
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002315 int cnt, q;
2316
2317 switch (conn->type) {
2318 case ACL_LINK:
2319 cnt = hdev->acl_cnt;
2320 break;
2321 case SCO_LINK:
2322 case ESCO_LINK:
2323 cnt = hdev->sco_cnt;
2324 break;
2325 case LE_LINK:
2326 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2327 break;
2328 default:
2329 cnt = 0;
2330 BT_ERR("Unknown link type");
2331 }
2332
2333 q = cnt / num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 *quote = q ? q : 1;
2335 } else
2336 *quote = 0;
2337
2338 BT_DBG("conn %p quote %d", conn, *quote);
2339 return conn;
2340}
2341
Ville Tervobae1f5d92011-02-10 22:38:53 -03002342static inline void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 struct hci_conn_hash *h = &hdev->conn_hash;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002345 struct hci_conn *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Ville Tervobae1f5d92011-02-10 22:38:53 -03002347 BT_ERR("%s link tx timeout", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002349 rcu_read_lock();
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 /* Kill stalled connections */
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002352 list_for_each_entry_rcu(c, &h->list, list) {
Ville Tervobae1f5d92011-02-10 22:38:53 -03002353 if (c->type == type && c->sent) {
2354 BT_ERR("%s killing stalled connection %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 hdev->name, batostr(&c->dst));
2356 hci_acl_disconn(c, 0x13);
2357 }
2358 }
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002359
2360 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002363static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
2364 int *quote)
2365{
2366 struct hci_conn_hash *h = &hdev->conn_hash;
2367 struct hci_chan *chan = NULL;
2368 int num = 0, min = ~0, cur_prio = 0;
2369 struct hci_conn *conn;
2370 int cnt, q, conn_num = 0;
2371
2372 BT_DBG("%s", hdev->name);
2373
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002374 rcu_read_lock();
2375
2376 list_for_each_entry_rcu(conn, &h->list, list) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002377 struct hci_chan *tmp;
2378
2379 if (conn->type != type)
2380 continue;
2381
2382 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2383 continue;
2384
2385 conn_num++;
2386
Gustavo F. Padovan8192ede2011-12-14 15:08:48 -02002387 list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002388 struct sk_buff *skb;
2389
2390 if (skb_queue_empty(&tmp->data_q))
2391 continue;
2392
2393 skb = skb_peek(&tmp->data_q);
2394 if (skb->priority < cur_prio)
2395 continue;
2396
2397 if (skb->priority > cur_prio) {
2398 num = 0;
2399 min = ~0;
2400 cur_prio = skb->priority;
2401 }
2402
2403 num++;
2404
2405 if (conn->sent < min) {
2406 min = conn->sent;
2407 chan = tmp;
2408 }
2409 }
2410
2411 if (hci_conn_num(hdev, type) == conn_num)
2412 break;
2413 }
2414
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002415 rcu_read_unlock();
2416
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002417 if (!chan)
2418 return NULL;
2419
2420 switch (chan->conn->type) {
2421 case ACL_LINK:
2422 cnt = hdev->acl_cnt;
2423 break;
2424 case SCO_LINK:
2425 case ESCO_LINK:
2426 cnt = hdev->sco_cnt;
2427 break;
2428 case LE_LINK:
2429 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2430 break;
2431 default:
2432 cnt = 0;
2433 BT_ERR("Unknown link type");
2434 }
2435
2436 q = cnt / num;
2437 *quote = q ? q : 1;
2438 BT_DBG("chan %p quote %d", chan, *quote);
2439 return chan;
2440}
2441
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002442static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
2443{
2444 struct hci_conn_hash *h = &hdev->conn_hash;
2445 struct hci_conn *conn;
2446 int num = 0;
2447
2448 BT_DBG("%s", hdev->name);
2449
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002450 rcu_read_lock();
2451
2452 list_for_each_entry_rcu(conn, &h->list, list) {
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002453 struct hci_chan *chan;
2454
2455 if (conn->type != type)
2456 continue;
2457
2458 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2459 continue;
2460
2461 num++;
2462
Gustavo F. Padovan8192ede2011-12-14 15:08:48 -02002463 list_for_each_entry_rcu(chan, &conn->chan_list, list) {
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002464 struct sk_buff *skb;
2465
2466 if (chan->sent) {
2467 chan->sent = 0;
2468 continue;
2469 }
2470
2471 if (skb_queue_empty(&chan->data_q))
2472 continue;
2473
2474 skb = skb_peek(&chan->data_q);
2475 if (skb->priority >= HCI_PRIO_MAX - 1)
2476 continue;
2477
2478 skb->priority = HCI_PRIO_MAX - 1;
2479
2480 BT_DBG("chan %p skb %p promoted to %d", chan, skb,
2481 skb->priority);
2482 }
2483
2484 if (hci_conn_num(hdev, type) == num)
2485 break;
2486 }
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002487
2488 rcu_read_unlock();
2489
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002490}
2491
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002492static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb)
2493{
2494 /* Calculate count of blocks used by this packet */
2495 return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len);
2496}
2497
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002498static inline void __check_timeout(struct hci_dev *hdev, unsigned int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (!test_bit(HCI_RAW, &hdev->flags)) {
2501 /* ACL tx timeout must be longer than maximum
2502 * link supervision timeout (40.9 seconds) */
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002503 if (!cnt && time_after(jiffies, hdev->acl_last_tx +
Andrei Emeltchenkocc48dc02012-01-04 16:42:26 +02002504 msecs_to_jiffies(HCI_ACL_TX_TIMEOUT)))
Ville Tervobae1f5d92011-02-10 22:38:53 -03002505 hci_link_tx_to(hdev, ACL_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002507}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002509static inline void hci_sched_acl_pkt(struct hci_dev *hdev)
2510{
2511 unsigned int cnt = hdev->acl_cnt;
2512 struct hci_chan *chan;
2513 struct sk_buff *skb;
2514 int quote;
2515
2516 __check_timeout(hdev, cnt);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002517
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002518 while (hdev->acl_cnt &&
2519 (chan = hci_chan_sent(hdev, ACL_LINK, &quote))) {
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002520 u32 priority = (skb_peek(&chan->data_q))->priority;
2521 while (quote-- && (skb = skb_peek(&chan->data_q))) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002522 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2523 skb->len, skb->priority);
2524
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002525 /* Stop if priority has changed */
2526 if (skb->priority < priority)
2527 break;
2528
2529 skb = skb_dequeue(&chan->data_q);
2530
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002531 hci_conn_enter_active_mode(chan->conn,
2532 bt_cb(skb)->force_active);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 hci_send_frame(skb);
2535 hdev->acl_last_tx = jiffies;
2536
2537 hdev->acl_cnt--;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002538 chan->sent++;
2539 chan->conn->sent++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541 }
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002542
2543 if (cnt != hdev->acl_cnt)
2544 hci_prio_recalculate(hdev, ACL_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002547static inline void hci_sched_acl_blk(struct hci_dev *hdev)
2548{
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002549 unsigned int cnt = hdev->block_cnt;
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002550 struct hci_chan *chan;
2551 struct sk_buff *skb;
2552 int quote;
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002553
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002554 __check_timeout(hdev, cnt);
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002555
2556 while (hdev->block_cnt > 0 &&
2557 (chan = hci_chan_sent(hdev, ACL_LINK, &quote))) {
2558 u32 priority = (skb_peek(&chan->data_q))->priority;
2559 while (quote > 0 && (skb = skb_peek(&chan->data_q))) {
2560 int blocks;
2561
2562 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2563 skb->len, skb->priority);
2564
2565 /* Stop if priority has changed */
2566 if (skb->priority < priority)
2567 break;
2568
2569 skb = skb_dequeue(&chan->data_q);
2570
2571 blocks = __get_blocks(hdev, skb);
2572 if (blocks > hdev->block_cnt)
2573 return;
2574
2575 hci_conn_enter_active_mode(chan->conn,
2576 bt_cb(skb)->force_active);
2577
2578 hci_send_frame(skb);
2579 hdev->acl_last_tx = jiffies;
2580
2581 hdev->block_cnt -= blocks;
2582 quote -= blocks;
2583
2584 chan->sent += blocks;
2585 chan->conn->sent += blocks;
2586 }
2587 }
2588
2589 if (cnt != hdev->block_cnt)
2590 hci_prio_recalculate(hdev, ACL_LINK);
2591}
2592
2593static inline void hci_sched_acl(struct hci_dev *hdev)
2594{
2595 BT_DBG("%s", hdev->name);
2596
2597 if (!hci_conn_num(hdev, ACL_LINK))
2598 return;
2599
2600 switch (hdev->flow_ctl_mode) {
2601 case HCI_FLOW_CTL_MODE_PACKET_BASED:
2602 hci_sched_acl_pkt(hdev);
2603 break;
2604
2605 case HCI_FLOW_CTL_MODE_BLOCK_BASED:
2606 hci_sched_acl_blk(hdev);
2607 break;
2608 }
2609}
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611/* Schedule SCO */
2612static inline void hci_sched_sco(struct hci_dev *hdev)
2613{
2614 struct hci_conn *conn;
2615 struct sk_buff *skb;
2616 int quote;
2617
2618 BT_DBG("%s", hdev->name);
2619
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002620 if (!hci_conn_num(hdev, SCO_LINK))
2621 return;
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
2624 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2625 BT_DBG("skb %p len %d", skb, skb->len);
2626 hci_send_frame(skb);
2627
2628 conn->sent++;
2629 if (conn->sent == ~0)
2630 conn->sent = 0;
2631 }
2632 }
2633}
2634
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002635static inline void hci_sched_esco(struct hci_dev *hdev)
2636{
2637 struct hci_conn *conn;
2638 struct sk_buff *skb;
2639 int quote;
2640
2641 BT_DBG("%s", hdev->name);
2642
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002643 if (!hci_conn_num(hdev, ESCO_LINK))
2644 return;
2645
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002646 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, &quote))) {
2647 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2648 BT_DBG("skb %p len %d", skb, skb->len);
2649 hci_send_frame(skb);
2650
2651 conn->sent++;
2652 if (conn->sent == ~0)
2653 conn->sent = 0;
2654 }
2655 }
2656}
2657
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002658static inline void hci_sched_le(struct hci_dev *hdev)
2659{
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002660 struct hci_chan *chan;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002661 struct sk_buff *skb;
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002662 int quote, cnt, tmp;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002663
2664 BT_DBG("%s", hdev->name);
2665
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002666 if (!hci_conn_num(hdev, LE_LINK))
2667 return;
2668
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002669 if (!test_bit(HCI_RAW, &hdev->flags)) {
2670 /* LE tx timeout must be longer than maximum
2671 * link supervision timeout (40.9 seconds) */
Ville Tervobae1f5d92011-02-10 22:38:53 -03002672 if (!hdev->le_cnt && hdev->le_pkts &&
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002673 time_after(jiffies, hdev->le_last_tx + HZ * 45))
Ville Tervobae1f5d92011-02-10 22:38:53 -03002674 hci_link_tx_to(hdev, LE_LINK);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002675 }
2676
2677 cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002678 tmp = cnt;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002679 while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, &quote))) {
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002680 u32 priority = (skb_peek(&chan->data_q))->priority;
2681 while (quote-- && (skb = skb_peek(&chan->data_q))) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002682 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2683 skb->len, skb->priority);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002684
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002685 /* Stop if priority has changed */
2686 if (skb->priority < priority)
2687 break;
2688
2689 skb = skb_dequeue(&chan->data_q);
2690
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002691 hci_send_frame(skb);
2692 hdev->le_last_tx = jiffies;
2693
2694 cnt--;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002695 chan->sent++;
2696 chan->conn->sent++;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002697 }
2698 }
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002699
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002700 if (hdev->le_pkts)
2701 hdev->le_cnt = cnt;
2702 else
2703 hdev->acl_cnt = cnt;
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002704
2705 if (cnt != tmp)
2706 hci_prio_recalculate(hdev, LE_LINK);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002707}
2708
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002709static void hci_tx_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002711 struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 struct sk_buff *skb;
2713
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002714 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
2715 hdev->sco_cnt, hdev->le_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 /* Schedule queues and send stuff to HCI driver */
2718
2719 hci_sched_acl(hdev);
2720
2721 hci_sched_sco(hdev);
2722
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002723 hci_sched_esco(hdev);
2724
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002725 hci_sched_le(hdev);
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /* Send next queued raw (unknown type) packet */
2728 while ((skb = skb_dequeue(&hdev->raw_q)))
2729 hci_send_frame(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002732/* ----- HCI RX task (incoming data processing) ----- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734/* ACL data packet */
2735static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2736{
2737 struct hci_acl_hdr *hdr = (void *) skb->data;
2738 struct hci_conn *conn;
2739 __u16 handle, flags;
2740
2741 skb_pull(skb, HCI_ACL_HDR_SIZE);
2742
2743 handle = __le16_to_cpu(hdr->handle);
2744 flags = hci_flags(handle);
2745 handle = hci_handle(handle);
2746
2747 BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags);
2748
2749 hdev->stat.acl_rx++;
2750
2751 hci_dev_lock(hdev);
2752 conn = hci_conn_hash_lookup_handle(hdev, handle);
2753 hci_dev_unlock(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (conn) {
Mat Martineau65983fc2011-12-13 15:06:02 -08002756 hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 /* Send to upper protocol */
Ulisses Furquim686ebf22011-12-21 10:11:33 -02002759 l2cap_recv_acldata(conn, skb, flags);
2760 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002762 BT_ERR("%s ACL packet for unknown connection handle %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 hdev->name, handle);
2764 }
2765
2766 kfree_skb(skb);
2767}
2768
2769/* SCO data packet */
2770static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2771{
2772 struct hci_sco_hdr *hdr = (void *) skb->data;
2773 struct hci_conn *conn;
2774 __u16 handle;
2775
2776 skb_pull(skb, HCI_SCO_HDR_SIZE);
2777
2778 handle = __le16_to_cpu(hdr->handle);
2779
2780 BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle);
2781
2782 hdev->stat.sco_rx++;
2783
2784 hci_dev_lock(hdev);
2785 conn = hci_conn_hash_lookup_handle(hdev, handle);
2786 hci_dev_unlock(hdev);
2787
2788 if (conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /* Send to upper protocol */
Ulisses Furquim686ebf22011-12-21 10:11:33 -02002790 sco_recv_scodata(conn, skb);
2791 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002793 BT_ERR("%s SCO packet for unknown connection handle %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 hdev->name, handle);
2795 }
2796
2797 kfree_skb(skb);
2798}
2799
Marcel Holtmannb78752c2010-08-08 23:06:53 -04002800static void hci_rx_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
Marcel Holtmannb78752c2010-08-08 23:06:53 -04002802 struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 struct sk_buff *skb;
2804
2805 BT_DBG("%s", hdev->name);
2806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 while ((skb = skb_dequeue(&hdev->rx_q))) {
Marcel Holtmanncd82e612012-02-20 20:34:38 +01002808 /* Send copy to monitor */
2809 hci_send_to_monitor(hdev, skb);
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (atomic_read(&hdev->promisc)) {
2812 /* Send copy to the sockets */
Marcel Holtmann470fe1b2012-02-20 14:50:30 +01002813 hci_send_to_sock(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
2815
2816 if (test_bit(HCI_RAW, &hdev->flags)) {
2817 kfree_skb(skb);
2818 continue;
2819 }
2820
2821 if (test_bit(HCI_INIT, &hdev->flags)) {
2822 /* Don't process data packets in this states. */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002823 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 case HCI_ACLDATA_PKT:
2825 case HCI_SCODATA_PKT:
2826 kfree_skb(skb);
2827 continue;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830
2831 /* Process frame */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002832 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 case HCI_EVENT_PKT:
Marcel Holtmannb78752c2010-08-08 23:06:53 -04002834 BT_DBG("%s Event packet", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 hci_event_packet(hdev, skb);
2836 break;
2837
2838 case HCI_ACLDATA_PKT:
2839 BT_DBG("%s ACL data packet", hdev->name);
2840 hci_acldata_packet(hdev, skb);
2841 break;
2842
2843 case HCI_SCODATA_PKT:
2844 BT_DBG("%s SCO data packet", hdev->name);
2845 hci_scodata_packet(hdev, skb);
2846 break;
2847
2848 default:
2849 kfree_skb(skb);
2850 break;
2851 }
2852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002855static void hci_cmd_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002857 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct sk_buff *skb;
2859
2860 BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 /* Send queued commands */
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002863 if (atomic_read(&hdev->cmd_cnt)) {
2864 skb = skb_dequeue(&hdev->cmd_q);
2865 if (!skb)
2866 return;
2867
Wei Yongjun7585b972009-02-25 18:29:52 +08002868 kfree_skb(hdev->sent_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002870 hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC);
2871 if (hdev->sent_cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 atomic_dec(&hdev->cmd_cnt);
2873 hci_send_frame(skb);
Szymon Janc7bdb8a52011-07-26 22:46:54 +02002874 if (test_bit(HCI_RESET, &hdev->flags))
2875 del_timer(&hdev->cmd_timer);
2876 else
2877 mod_timer(&hdev->cmd_timer,
Ville Tervo6bd32322011-02-16 16:32:41 +02002878 jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 } else {
2880 skb_queue_head(&hdev->cmd_q, skb);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002881 queue_work(hdev->workqueue, &hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
2883 }
2884}
Andre Guedes2519a1f2011-11-07 11:45:24 -03002885
2886int hci_do_inquiry(struct hci_dev *hdev, u8 length)
2887{
2888 /* General inquiry access code (GIAC) */
2889 u8 lap[3] = { 0x33, 0x8b, 0x9e };
2890 struct hci_cp_inquiry cp;
2891
2892 BT_DBG("%s", hdev->name);
2893
2894 if (test_bit(HCI_INQUIRY, &hdev->flags))
2895 return -EINPROGRESS;
2896
Johan Hedberg46632622012-01-02 16:06:08 +02002897 inquiry_cache_flush(hdev);
2898
Andre Guedes2519a1f2011-11-07 11:45:24 -03002899 memset(&cp, 0, sizeof(cp));
2900 memcpy(&cp.lap, lap, sizeof(cp.lap));
2901 cp.length = length;
2902
2903 return hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
2904}
Andre Guedes023d50492011-11-04 14:16:52 -03002905
2906int hci_cancel_inquiry(struct hci_dev *hdev)
2907{
2908 BT_DBG("%s", hdev->name);
2909
2910 if (!test_bit(HCI_INQUIRY, &hdev->flags))
2911 return -EPERM;
2912
2913 return hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL);
2914}