blob: 94b08aa9a081d0603cbdabe61940b66907b5dc53 [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
Gustavo Padovan8c520a52012-05-23 04:04:22 -030028#include <linux/export.h>
Sasha Levin3df92b32012-05-27 22:36:56 +020029#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Marcel Holtmann611b30f2009-06-08 14:41:38 +020031#include <linux/rfkill.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <net/bluetooth/bluetooth.h>
34#include <net/bluetooth/hci_core.h>
35
Marcel Holtmannb78752c2010-08-08 23:06:53 -040036static void hci_rx_work(struct work_struct *work);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -020037static void hci_cmd_work(struct work_struct *work);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -020038static void hci_tx_work(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* HCI device list */
41LIST_HEAD(hci_dev_list);
42DEFINE_RWLOCK(hci_dev_list_lock);
43
44/* HCI callback list */
45LIST_HEAD(hci_cb_list);
46DEFINE_RWLOCK(hci_cb_list_lock);
47
Sasha Levin3df92b32012-05-27 22:36:56 +020048/* HCI ID Numbering */
49static DEFINE_IDA(hci_index_ida);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* ---- HCI notifications ---- */
52
Marcel Holtmann65164552005-10-28 19:20:48 +020053static void hci_notify(struct hci_dev *hdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Marcel Holtmann040030e2012-02-20 14:50:37 +010055 hci_sock_dev_event(hdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58/* ---- HCI requests ---- */
59
Johan Hedberg23bb5762010-12-21 23:01:27 +020060void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Andrei Emeltchenkof0e09512012-06-11 11:13:09 +030062 BT_DBG("%s command 0x%4.4x result 0x%2.2x", hdev->name, cmd, result);
Johan Hedberg23bb5762010-12-21 23:01:27 +020063
Johan Hedberga5040ef2011-01-10 13:28:59 +020064 /* If this is the init phase check if the completed command matches
65 * the last init command, and if not just return.
66 */
Johan Hedberg75fb0e32012-03-01 21:35:55 +020067 if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd) {
68 struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data;
Andrei Emeltchenko1036b892012-03-12 15:59:33 +020069 u16 opcode = __le16_to_cpu(sent->opcode);
Johan Hedberg75fb0e32012-03-01 21:35:55 +020070 struct sk_buff *skb;
71
72 /* Some CSR based controllers generate a spontaneous
73 * reset complete event during init and any pending
74 * command will never be completed. In such a case we
75 * need to resend whatever was the last sent
76 * command.
77 */
78
Andrei Emeltchenko1036b892012-03-12 15:59:33 +020079 if (cmd != HCI_OP_RESET || opcode == HCI_OP_RESET)
Johan Hedberg75fb0e32012-03-01 21:35:55 +020080 return;
81
82 skb = skb_clone(hdev->sent_cmd, GFP_ATOMIC);
83 if (skb) {
84 skb_queue_head(&hdev->cmd_q, skb);
85 queue_work(hdev->workqueue, &hdev->cmd_work);
86 }
87
Johan Hedberg23bb5762010-12-21 23:01:27 +020088 return;
Johan Hedberg75fb0e32012-03-01 21:35:55 +020089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 if (hdev->req_status == HCI_REQ_PEND) {
92 hdev->req_result = result;
93 hdev->req_status = HCI_REQ_DONE;
94 wake_up_interruptible(&hdev->req_wait_q);
95 }
96}
97
98static void hci_req_cancel(struct hci_dev *hdev, int err)
99{
100 BT_DBG("%s err 0x%2.2x", hdev->name, err);
101
102 if (hdev->req_status == HCI_REQ_PEND) {
103 hdev->req_result = err;
104 hdev->req_status = HCI_REQ_CANCELED;
105 wake_up_interruptible(&hdev->req_wait_q);
106 }
107}
108
109/* Execute request and wait for completion. */
Johan Hedberg01178cd2013-03-05 20:37:41 +0200110static int __hci_req_sync(struct hci_dev *hdev,
111 void (*req)(struct hci_dev *hdev, unsigned long opt),
112 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 DECLARE_WAITQUEUE(wait, current);
115 int err = 0;
116
117 BT_DBG("%s start", hdev->name);
118
119 hdev->req_status = HCI_REQ_PEND;
120
121 add_wait_queue(&hdev->req_wait_q, &wait);
122 set_current_state(TASK_INTERRUPTIBLE);
123
124 req(hdev, opt);
Johan Hedberg53cce222013-03-05 20:37:42 +0200125
126 /* If the request didn't send any commands return immediately */
127 if (skb_queue_empty(&hdev->cmd_q) && atomic_read(&hdev->cmd_cnt)) {
128 hdev->req_status = 0;
129 remove_wait_queue(&hdev->req_wait_q, &wait);
130 return err;
131 }
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 schedule_timeout(timeout);
134
135 remove_wait_queue(&hdev->req_wait_q, &wait);
136
137 if (signal_pending(current))
138 return -EINTR;
139
140 switch (hdev->req_status) {
141 case HCI_REQ_DONE:
Joe Perchese1750722011-06-29 18:18:29 -0700142 err = -bt_to_errno(hdev->req_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144
145 case HCI_REQ_CANCELED:
146 err = -hdev->req_result;
147 break;
148
149 default:
150 err = -ETIMEDOUT;
151 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Johan Hedberga5040ef2011-01-10 13:28:59 +0200154 hdev->req_status = hdev->req_result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 BT_DBG("%s end: err %d", hdev->name, err);
157
158 return err;
159}
160
Johan Hedberg01178cd2013-03-05 20:37:41 +0200161static int hci_req_sync(struct hci_dev *hdev,
162 void (*req)(struct hci_dev *hdev, unsigned long opt),
163 unsigned long opt, __u32 timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int ret;
166
Marcel Holtmann7c6a3292008-09-12 03:11:54 +0200167 if (!test_bit(HCI_UP, &hdev->flags))
168 return -ENETDOWN;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* Serialize all requests */
171 hci_req_lock(hdev);
Johan Hedberg01178cd2013-03-05 20:37:41 +0200172 ret = __hci_req_sync(hdev, req, opt, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 hci_req_unlock(hdev);
174
175 return ret;
176}
177
178static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
179{
180 BT_DBG("%s %ld", hdev->name, opt);
181
182 /* Reset device */
Gustavo F. Padovanf630cf02011-03-16 15:36:29 -0300183 set_bit(HCI_RESET, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200184 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200187static void bredr_init(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Andrei Emeltchenko2455a3e2011-12-19 16:31:28 +0200189 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Read Local Supported Features */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200192 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200194 /* Read Local Version */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200195 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
Johan Hedberg2177bab2013-03-05 20:37:43 +0200196
197 /* Read BD Address */
198 hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200201static void amp_init(struct hci_dev *hdev)
202{
Andrei Emeltchenko2455a3e2011-12-19 16:31:28 +0200203 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
204
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200205 /* Read Local Version */
206 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
Andrei Emeltchenko6bcbc482012-03-28 16:31:24 +0300207
208 /* Read Local AMP Info */
209 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
Andrei Emeltchenkoe71dfab2012-09-06 15:05:46 +0300210
211 /* Read Data Blk size */
212 hci_send_cmd(hdev, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL);
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200213}
214
Johan Hedberg2177bab2013-03-05 20:37:43 +0200215static void hci_init1_req(struct hci_dev *hdev, unsigned long opt)
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200216{
217 struct sk_buff *skb;
218
219 BT_DBG("%s %ld", hdev->name, opt);
220
221 /* Driver initialization */
222
223 /* Special commands */
224 while ((skb = skb_dequeue(&hdev->driver_init))) {
225 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
226 skb->dev = (void *) hdev;
227
228 skb_queue_tail(&hdev->cmd_q, skb);
229 queue_work(hdev->workqueue, &hdev->cmd_work);
230 }
231 skb_queue_purge(&hdev->driver_init);
232
Andrei Emeltchenko11778712012-06-11 11:13:10 +0300233 /* Reset */
234 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks))
235 hci_reset_req(hdev, 0);
236
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200237 switch (hdev->dev_type) {
238 case HCI_BREDR:
239 bredr_init(hdev);
240 break;
241
242 case HCI_AMP:
243 amp_init(hdev);
244 break;
245
246 default:
247 BT_ERR("Unknown device type %d", hdev->dev_type);
248 break;
249 }
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200250}
251
Johan Hedberg2177bab2013-03-05 20:37:43 +0200252static void bredr_setup(struct hci_dev *hdev)
253{
254 struct hci_cp_delete_stored_link_key cp;
255 __le16 param;
256 __u8 flt_type;
257
258 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
259 hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
260
261 /* Read Class of Device */
262 hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
263
264 /* Read Local Name */
265 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL);
266
267 /* Read Voice Setting */
268 hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL);
269
270 /* Clear Event Filters */
271 flt_type = HCI_FLT_CLEAR_ALL;
272 hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
273
274 /* Connection accept timeout ~20 secs */
275 param = __constant_cpu_to_le16(0x7d00);
276 hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
277
278 bacpy(&cp.bdaddr, BDADDR_ANY);
279 cp.delete_all = 0x01;
280 hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp);
281}
282
283static void le_setup(struct hci_dev *hdev)
284{
285 /* Read LE Buffer Size */
286 hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
287
288 /* Read LE Local Supported Features */
289 hci_send_cmd(hdev, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL);
290
291 /* Read LE Advertising Channel TX Power */
292 hci_send_cmd(hdev, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL);
293
294 /* Read LE White List Size */
295 hci_send_cmd(hdev, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL);
296
297 /* Read LE Supported States */
298 hci_send_cmd(hdev, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL);
299}
300
301static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
302{
303 if (lmp_ext_inq_capable(hdev))
304 return 0x02;
305
306 if (lmp_inq_rssi_capable(hdev))
307 return 0x01;
308
309 if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
310 hdev->lmp_subver == 0x0757)
311 return 0x01;
312
313 if (hdev->manufacturer == 15) {
314 if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
315 return 0x01;
316 if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
317 return 0x01;
318 if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
319 return 0x01;
320 }
321
322 if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
323 hdev->lmp_subver == 0x1805)
324 return 0x01;
325
326 return 0x00;
327}
328
329static void hci_setup_inquiry_mode(struct hci_dev *hdev)
330{
331 u8 mode;
332
333 mode = hci_get_inquiry_mode(hdev);
334
335 hci_send_cmd(hdev, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
336}
337
338static void hci_setup_event_mask(struct hci_dev *hdev)
339{
340 /* The second byte is 0xff instead of 0x9f (two reserved bits
341 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
342 * command otherwise.
343 */
344 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
345
346 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
347 * any event mask for pre 1.2 devices.
348 */
349 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
350 return;
351
352 if (lmp_bredr_capable(hdev)) {
353 events[4] |= 0x01; /* Flow Specification Complete */
354 events[4] |= 0x02; /* Inquiry Result with RSSI */
355 events[4] |= 0x04; /* Read Remote Extended Features Complete */
356 events[5] |= 0x08; /* Synchronous Connection Complete */
357 events[5] |= 0x10; /* Synchronous Connection Changed */
358 }
359
360 if (lmp_inq_rssi_capable(hdev))
361 events[4] |= 0x02; /* Inquiry Result with RSSI */
362
363 if (lmp_sniffsubr_capable(hdev))
364 events[5] |= 0x20; /* Sniff Subrating */
365
366 if (lmp_pause_enc_capable(hdev))
367 events[5] |= 0x80; /* Encryption Key Refresh Complete */
368
369 if (lmp_ext_inq_capable(hdev))
370 events[5] |= 0x40; /* Extended Inquiry Result */
371
372 if (lmp_no_flush_capable(hdev))
373 events[7] |= 0x01; /* Enhanced Flush Complete */
374
375 if (lmp_lsto_capable(hdev))
376 events[6] |= 0x80; /* Link Supervision Timeout Changed */
377
378 if (lmp_ssp_capable(hdev)) {
379 events[6] |= 0x01; /* IO Capability Request */
380 events[6] |= 0x02; /* IO Capability Response */
381 events[6] |= 0x04; /* User Confirmation Request */
382 events[6] |= 0x08; /* User Passkey Request */
383 events[6] |= 0x10; /* Remote OOB Data Request */
384 events[6] |= 0x20; /* Simple Pairing Complete */
385 events[7] |= 0x04; /* User Passkey Notification */
386 events[7] |= 0x08; /* Keypress Notification */
387 events[7] |= 0x10; /* Remote Host Supported
388 * Features Notification
389 */
390 }
391
392 if (lmp_le_capable(hdev))
393 events[7] |= 0x20; /* LE Meta-Event */
394
395 hci_send_cmd(hdev, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
396
397 if (lmp_le_capable(hdev)) {
398 memset(events, 0, sizeof(events));
399 events[0] = 0x1f;
400 hci_send_cmd(hdev, HCI_OP_LE_SET_EVENT_MASK,
401 sizeof(events), events);
402 }
403}
404
405static void hci_init2_req(struct hci_dev *hdev, unsigned long opt)
406{
407 if (lmp_bredr_capable(hdev))
408 bredr_setup(hdev);
409
410 if (lmp_le_capable(hdev))
411 le_setup(hdev);
412
413 hci_setup_event_mask(hdev);
414
415 if (hdev->hci_ver > BLUETOOTH_VER_1_1)
416 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
417
418 if (lmp_ssp_capable(hdev)) {
419 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
420 u8 mode = 0x01;
421 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE,
422 sizeof(mode), &mode);
423 } else {
424 struct hci_cp_write_eir cp;
425
426 memset(hdev->eir, 0, sizeof(hdev->eir));
427 memset(&cp, 0, sizeof(cp));
428
429 hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
430 }
431 }
432
433 if (lmp_inq_rssi_capable(hdev))
434 hci_setup_inquiry_mode(hdev);
435
436 if (lmp_inq_tx_pwr_capable(hdev))
437 hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
438
439 if (lmp_ext_feat_capable(hdev)) {
440 struct hci_cp_read_local_ext_features cp;
441
442 cp.page = 0x01;
443 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES, sizeof(cp),
444 &cp);
445 }
446
447 if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) {
448 u8 enable = 1;
449 hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable),
450 &enable);
451 }
452}
453
454static void hci_setup_link_policy(struct hci_dev *hdev)
455{
456 struct hci_cp_write_def_link_policy cp;
457 u16 link_policy = 0;
458
459 if (lmp_rswitch_capable(hdev))
460 link_policy |= HCI_LP_RSWITCH;
461 if (lmp_hold_capable(hdev))
462 link_policy |= HCI_LP_HOLD;
463 if (lmp_sniff_capable(hdev))
464 link_policy |= HCI_LP_SNIFF;
465 if (lmp_park_capable(hdev))
466 link_policy |= HCI_LP_PARK;
467
468 cp.policy = cpu_to_le16(link_policy);
469 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp);
470}
471
472static void hci_set_le_support(struct hci_dev *hdev)
473{
474 struct hci_cp_write_le_host_supported cp;
475
476 memset(&cp, 0, sizeof(cp));
477
478 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
479 cp.le = 0x01;
480 cp.simul = lmp_le_br_capable(hdev);
481 }
482
483 if (cp.le != lmp_host_le_capable(hdev))
484 hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
485 &cp);
486}
487
488static void hci_init3_req(struct hci_dev *hdev, unsigned long opt)
489{
490 if (hdev->commands[5] & 0x10)
491 hci_setup_link_policy(hdev);
492
493 if (lmp_le_capable(hdev))
494 hci_set_le_support(hdev);
495}
496
497static int __hci_init(struct hci_dev *hdev)
498{
499 int err;
500
501 err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT);
502 if (err < 0)
503 return err;
504
505 /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode
506 * BR/EDR/LE type controllers. AMP controllers only need the
507 * first stage init.
508 */
509 if (hdev->dev_type != HCI_BREDR)
510 return 0;
511
512 err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT);
513 if (err < 0)
514 return err;
515
516 return __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT);
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static void hci_scan_req(struct hci_dev *hdev, unsigned long opt)
520{
521 __u8 scan = opt;
522
523 BT_DBG("%s %x", hdev->name, scan);
524
525 /* Inquiry and Page scans */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200526 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529static void hci_auth_req(struct hci_dev *hdev, unsigned long opt)
530{
531 __u8 auth = opt;
532
533 BT_DBG("%s %x", hdev->name, auth);
534
535 /* Authentication */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200536 hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
540{
541 __u8 encrypt = opt;
542
543 BT_DBG("%s %x", hdev->name, encrypt);
544
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200545 /* Encryption */
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200546 hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200549static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt)
550{
551 __le16 policy = cpu_to_le16(opt);
552
Marcel Holtmanna418b892008-11-30 12:17:28 +0100553 BT_DBG("%s %x", hdev->name, policy);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200554
555 /* Default link policy */
556 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
557}
558
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900559/* Get HCI device by index.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * Device is held on return. */
561struct hci_dev *hci_dev_get(int index)
562{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200563 struct hci_dev *hdev = NULL, *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 BT_DBG("%d", index);
566
567 if (index < 0)
568 return NULL;
569
570 read_lock(&hci_dev_list_lock);
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200571 list_for_each_entry(d, &hci_dev_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (d->id == index) {
573 hdev = hci_dev_hold(d);
574 break;
575 }
576 }
577 read_unlock(&hci_dev_list_lock);
578 return hdev;
579}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581/* ---- Inquiry support ---- */
Johan Hedbergff9ef572012-01-04 14:23:45 +0200582
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200583bool hci_discovery_active(struct hci_dev *hdev)
584{
585 struct discovery_state *discov = &hdev->discovery;
586
Andre Guedes6fbe1952012-02-03 17:47:58 -0300587 switch (discov->state) {
Andre Guedes343f9352012-02-17 20:39:37 -0300588 case DISCOVERY_FINDING:
Andre Guedes6fbe1952012-02-03 17:47:58 -0300589 case DISCOVERY_RESOLVING:
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200590 return true;
591
Andre Guedes6fbe1952012-02-03 17:47:58 -0300592 default:
593 return false;
594 }
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200595}
596
Johan Hedbergff9ef572012-01-04 14:23:45 +0200597void hci_discovery_set_state(struct hci_dev *hdev, int state)
598{
599 BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
600
601 if (hdev->discovery.state == state)
602 return;
603
604 switch (state) {
605 case DISCOVERY_STOPPED:
Andre Guedes7b99b652012-02-13 15:41:02 -0300606 if (hdev->discovery.state != DISCOVERY_STARTING)
607 mgmt_discovering(hdev, 0);
Johan Hedbergff9ef572012-01-04 14:23:45 +0200608 break;
609 case DISCOVERY_STARTING:
610 break;
Andre Guedes343f9352012-02-17 20:39:37 -0300611 case DISCOVERY_FINDING:
Johan Hedbergff9ef572012-01-04 14:23:45 +0200612 mgmt_discovering(hdev, 1);
613 break;
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200614 case DISCOVERY_RESOLVING:
615 break;
Johan Hedbergff9ef572012-01-04 14:23:45 +0200616 case DISCOVERY_STOPPING:
617 break;
618 }
619
620 hdev->discovery.state = state;
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static void inquiry_cache_flush(struct hci_dev *hdev)
624{
Johan Hedberg30883512012-01-04 14:16:21 +0200625 struct discovery_state *cache = &hdev->discovery;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200626 struct inquiry_entry *p, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Johan Hedberg561aafb2012-01-04 13:31:59 +0200628 list_for_each_entry_safe(p, n, &cache->all, all) {
629 list_del(&p->all);
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200630 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Johan Hedberg561aafb2012-01-04 13:31:59 +0200632
633 INIT_LIST_HEAD(&cache->unknown);
634 INIT_LIST_HEAD(&cache->resolve);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Gustavo Padovana8c5fb12012-05-17 00:36:26 -0300637struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
638 bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Johan Hedberg30883512012-01-04 14:16:21 +0200640 struct discovery_state *cache = &hdev->discovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct inquiry_entry *e;
642
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300643 BT_DBG("cache %p, %pMR", cache, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Johan Hedberg561aafb2012-01-04 13:31:59 +0200645 list_for_each_entry(e, &cache->all, all) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!bacmp(&e->data.bdaddr, bdaddr))
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200647 return e;
648 }
649
650 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Johan Hedberg561aafb2012-01-04 13:31:59 +0200653struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300654 bdaddr_t *bdaddr)
Johan Hedberg561aafb2012-01-04 13:31:59 +0200655{
Johan Hedberg30883512012-01-04 14:16:21 +0200656 struct discovery_state *cache = &hdev->discovery;
Johan Hedberg561aafb2012-01-04 13:31:59 +0200657 struct inquiry_entry *e;
658
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300659 BT_DBG("cache %p, %pMR", cache, bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +0200660
661 list_for_each_entry(e, &cache->unknown, list) {
662 if (!bacmp(&e->data.bdaddr, bdaddr))
663 return e;
664 }
665
666 return NULL;
667}
668
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200669struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300670 bdaddr_t *bdaddr,
671 int state)
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200672{
673 struct discovery_state *cache = &hdev->discovery;
674 struct inquiry_entry *e;
675
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300676 BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state);
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200677
678 list_for_each_entry(e, &cache->resolve, list) {
679 if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state)
680 return e;
681 if (!bacmp(&e->data.bdaddr, bdaddr))
682 return e;
683 }
684
685 return NULL;
686}
687
Johan Hedberga3d4e202012-01-09 00:53:02 +0200688void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300689 struct inquiry_entry *ie)
Johan Hedberga3d4e202012-01-09 00:53:02 +0200690{
691 struct discovery_state *cache = &hdev->discovery;
692 struct list_head *pos = &cache->resolve;
693 struct inquiry_entry *p;
694
695 list_del(&ie->list);
696
697 list_for_each_entry(p, &cache->resolve, list) {
698 if (p->name_state != NAME_PENDING &&
Gustavo Padovana8c5fb12012-05-17 00:36:26 -0300699 abs(p->data.rssi) >= abs(ie->data.rssi))
Johan Hedberga3d4e202012-01-09 00:53:02 +0200700 break;
701 pos = &p->list;
702 }
703
704 list_add(&ie->list, pos);
705}
706
Johan Hedberg31754052012-01-04 13:39:52 +0200707bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300708 bool name_known, bool *ssp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Johan Hedberg30883512012-01-04 14:16:21 +0200710 struct discovery_state *cache = &hdev->discovery;
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200711 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300713 BT_DBG("cache %p, %pMR", cache, &data->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Szymon Janc2b2fec42012-11-20 11:38:54 +0100715 hci_remove_remote_oob_data(hdev, &data->bdaddr);
716
Johan Hedberg388fc8f2012-02-23 00:38:59 +0200717 if (ssp)
718 *ssp = data->ssp_mode;
719
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200720 ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
Johan Hedberga3d4e202012-01-09 00:53:02 +0200721 if (ie) {
Johan Hedberg388fc8f2012-02-23 00:38:59 +0200722 if (ie->data.ssp_mode && ssp)
723 *ssp = true;
724
Johan Hedberga3d4e202012-01-09 00:53:02 +0200725 if (ie->name_state == NAME_NEEDED &&
Gustavo Padovana8c5fb12012-05-17 00:36:26 -0300726 data->rssi != ie->data.rssi) {
Johan Hedberga3d4e202012-01-09 00:53:02 +0200727 ie->data.rssi = data->rssi;
728 hci_inquiry_cache_update_resolve(hdev, ie);
729 }
730
Johan Hedberg561aafb2012-01-04 13:31:59 +0200731 goto update;
Johan Hedberga3d4e202012-01-09 00:53:02 +0200732 }
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200733
Johan Hedberg561aafb2012-01-04 13:31:59 +0200734 /* Entry not in the cache. Add new one. */
735 ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
736 if (!ie)
Johan Hedberg31754052012-01-04 13:39:52 +0200737 return false;
Johan Hedberg561aafb2012-01-04 13:31:59 +0200738
739 list_add(&ie->all, &cache->all);
740
741 if (name_known) {
742 ie->name_state = NAME_KNOWN;
743 } else {
744 ie->name_state = NAME_NOT_KNOWN;
745 list_add(&ie->list, &cache->unknown);
746 }
747
748update:
749 if (name_known && ie->name_state != NAME_KNOWN &&
Gustavo Padovana8c5fb12012-05-17 00:36:26 -0300750 ie->name_state != NAME_PENDING) {
Johan Hedberg561aafb2012-01-04 13:31:59 +0200751 ie->name_state = NAME_KNOWN;
752 list_del(&ie->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200755 memcpy(&ie->data, data, sizeof(*data));
756 ie->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 cache->timestamp = jiffies;
Johan Hedberg31754052012-01-04 13:39:52 +0200758
759 if (ie->name_state == NAME_NOT_KNOWN)
760 return false;
761
762 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
766{
Johan Hedberg30883512012-01-04 14:16:21 +0200767 struct discovery_state *cache = &hdev->discovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct inquiry_info *info = (struct inquiry_info *) buf;
769 struct inquiry_entry *e;
770 int copied = 0;
771
Johan Hedberg561aafb2012-01-04 13:31:59 +0200772 list_for_each_entry(e, &cache->all, all) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct inquiry_data *data = &e->data;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200774
775 if (copied >= num)
776 break;
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 bacpy(&info->bdaddr, &data->bdaddr);
779 info->pscan_rep_mode = data->pscan_rep_mode;
780 info->pscan_period_mode = data->pscan_period_mode;
781 info->pscan_mode = data->pscan_mode;
782 memcpy(info->dev_class, data->dev_class, 3);
783 info->clock_offset = data->clock_offset;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 info++;
Johan Hedbergb57c1a52012-01-03 16:03:00 +0200786 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
789 BT_DBG("cache %p, copied %d", cache, copied);
790 return copied;
791}
792
793static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
794{
795 struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
796 struct hci_cp_inquiry cp;
797
798 BT_DBG("%s", hdev->name);
799
800 if (test_bit(HCI_INQUIRY, &hdev->flags))
801 return;
802
803 /* Start Inquiry */
804 memcpy(&cp.lap, &ir->lap, 3);
805 cp.length = ir->length;
806 cp.num_rsp = ir->num_rsp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200807 hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810int hci_inquiry(void __user *arg)
811{
812 __u8 __user *ptr = arg;
813 struct hci_inquiry_req ir;
814 struct hci_dev *hdev;
815 int err = 0, do_inquiry = 0, max_rsp;
816 long timeo;
817 __u8 *buf;
818
819 if (copy_from_user(&ir, ptr, sizeof(ir)))
820 return -EFAULT;
821
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200822 hdev = hci_dev_get(ir.dev_id);
823 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -ENODEV;
825
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300826 hci_dev_lock(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900827 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
Gustavo Padovana8c5fb12012-05-17 00:36:26 -0300828 inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 inquiry_cache_flush(hdev);
830 do_inquiry = 1;
831 }
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300832 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Marcel Holtmann04837f62006-07-03 10:02:33 +0200834 timeo = ir.length * msecs_to_jiffies(2000);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200835
836 if (do_inquiry) {
Johan Hedberg01178cd2013-03-05 20:37:41 +0200837 err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir,
838 timeo);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200839 if (err < 0)
840 goto done;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300843 /* for unlimited number of responses we will use buffer with
844 * 255 entries
845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
847
848 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
849 * copy it to the user space.
850 */
Szymon Janc01df8c32011-02-17 16:46:47 +0100851 buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200852 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 err = -ENOMEM;
854 goto done;
855 }
856
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300857 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300859 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 BT_DBG("num_rsp %d", ir.num_rsp);
862
863 if (!copy_to_user(ptr, &ir, sizeof(ir))) {
864 ptr += sizeof(ir);
865 if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
Gustavo Padovana8c5fb12012-05-17 00:36:26 -0300866 ir.num_rsp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 err = -EFAULT;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900868 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 err = -EFAULT;
870
871 kfree(buf);
872
873done:
874 hci_dev_put(hdev);
875 return err;
876}
877
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100878static u8 create_ad(struct hci_dev *hdev, u8 *ptr)
879{
880 u8 ad_len = 0, flags = 0;
881 size_t name_len;
882
883 if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags))
884 flags |= LE_AD_GENERAL;
885
886 if (!lmp_bredr_capable(hdev))
887 flags |= LE_AD_NO_BREDR;
888
889 if (lmp_le_br_capable(hdev))
890 flags |= LE_AD_SIM_LE_BREDR_CTRL;
891
892 if (lmp_host_le_br_capable(hdev))
893 flags |= LE_AD_SIM_LE_BREDR_HOST;
894
895 if (flags) {
896 BT_DBG("adv flags 0x%02x", flags);
897
898 ptr[0] = 2;
899 ptr[1] = EIR_FLAGS;
900 ptr[2] = flags;
901
902 ad_len += 3;
903 ptr += 3;
904 }
905
906 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) {
907 ptr[0] = 2;
908 ptr[1] = EIR_TX_POWER;
909 ptr[2] = (u8) hdev->adv_tx_power;
910
911 ad_len += 3;
912 ptr += 3;
913 }
914
915 name_len = strlen(hdev->dev_name);
916 if (name_len > 0) {
917 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
918
919 if (name_len > max_len) {
920 name_len = max_len;
921 ptr[1] = EIR_NAME_SHORT;
922 } else
923 ptr[1] = EIR_NAME_COMPLETE;
924
925 ptr[0] = name_len + 1;
926
927 memcpy(ptr + 2, hdev->dev_name, name_len);
928
929 ad_len += (name_len + 2);
930 ptr += (name_len + 2);
931 }
932
933 return ad_len;
934}
935
936int hci_update_ad(struct hci_dev *hdev)
937{
938 struct hci_cp_le_set_adv_data cp;
939 u8 len;
940 int err;
941
942 hci_dev_lock(hdev);
943
944 if (!lmp_le_capable(hdev)) {
945 err = -EINVAL;
946 goto unlock;
947 }
948
949 memset(&cp, 0, sizeof(cp));
950
951 len = create_ad(hdev, cp.data);
952
953 if (hdev->adv_data_len == len &&
954 memcmp(cp.data, hdev->adv_data, len) == 0) {
955 err = 0;
956 goto unlock;
957 }
958
959 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
960 hdev->adv_data_len = len;
961
962 cp.length = len;
963 err = hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
964
965unlock:
966 hci_dev_unlock(hdev);
967
968 return err;
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/* ---- HCI ioctl helpers ---- */
972
973int hci_dev_open(__u16 dev)
974{
975 struct hci_dev *hdev;
976 int ret = 0;
977
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200978 hdev = hci_dev_get(dev);
979 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -ENODEV;
981
982 BT_DBG("%s %p", hdev->name, hdev);
983
984 hci_req_lock(hdev);
985
Johan Hovold94324962012-03-15 14:48:41 +0100986 if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) {
987 ret = -ENODEV;
988 goto done;
989 }
990
Marcel Holtmann611b30f2009-06-08 14:41:38 +0200991 if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) {
992 ret = -ERFKILL;
993 goto done;
994 }
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (test_bit(HCI_UP, &hdev->flags)) {
997 ret = -EALREADY;
998 goto done;
999 }
1000
1001 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
1002 set_bit(HCI_RAW, &hdev->flags);
1003
Andrei Emeltchenko07e3b942011-11-11 17:02:15 +02001004 /* Treat all non BR/EDR controllers as raw devices if
1005 enable_hs is not set */
1006 if (hdev->dev_type != HCI_BREDR && !enable_hs)
Marcel Holtmann943da252010-02-13 02:28:41 +01001007 set_bit(HCI_RAW, &hdev->flags);
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (hdev->open(hdev)) {
1010 ret = -EIO;
1011 goto done;
1012 }
1013
1014 if (!test_bit(HCI_RAW, &hdev->flags)) {
1015 atomic_set(&hdev->cmd_cnt, 1);
1016 set_bit(HCI_INIT, &hdev->flags);
Johan Hedberga5040ef2011-01-10 13:28:59 +02001017 hdev->init_last_cmd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Johan Hedberg2177bab2013-03-05 20:37:43 +02001019 ret = __hci_init(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 clear_bit(HCI_INIT, &hdev->flags);
1022 }
1023
1024 if (!ret) {
1025 hci_dev_hold(hdev);
1026 set_bit(HCI_UP, &hdev->flags);
1027 hci_notify(hdev, HCI_DEV_UP);
Johan Hedberg3f0f5242012-11-08 01:23:00 +01001028 hci_update_ad(hdev);
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03001029 if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
1030 mgmt_valid_hdev(hdev)) {
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001031 hci_dev_lock(hdev);
Johan Hedberg744cf192011-11-08 20:40:14 +02001032 mgmt_powered(hdev, 1);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001033 hci_dev_unlock(hdev);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001034 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001035 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /* Init failed, cleanup */
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02001037 flush_work(&hdev->tx_work);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02001038 flush_work(&hdev->cmd_work);
Marcel Holtmannb78752c2010-08-08 23:06:53 -04001039 flush_work(&hdev->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 skb_queue_purge(&hdev->cmd_q);
1042 skb_queue_purge(&hdev->rx_q);
1043
1044 if (hdev->flush)
1045 hdev->flush(hdev);
1046
1047 if (hdev->sent_cmd) {
1048 kfree_skb(hdev->sent_cmd);
1049 hdev->sent_cmd = NULL;
1050 }
1051
1052 hdev->close(hdev);
1053 hdev->flags = 0;
1054 }
1055
1056done:
1057 hci_req_unlock(hdev);
1058 hci_dev_put(hdev);
1059 return ret;
1060}
1061
1062static int hci_dev_do_close(struct hci_dev *hdev)
1063{
1064 BT_DBG("%s %p", hdev->name, hdev);
1065
Andre Guedes28b75a82012-02-03 17:48:00 -03001066 cancel_work_sync(&hdev->le_scan);
1067
Vinicius Costa Gomes78c04c02012-09-14 16:34:46 -03001068 cancel_delayed_work(&hdev->power_off);
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 hci_req_cancel(hdev, ENODEV);
1071 hci_req_lock(hdev);
1072
1073 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
Vinicius Costa Gomesb79f44c2011-04-11 18:46:55 -03001074 del_timer_sync(&hdev->cmd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 hci_req_unlock(hdev);
1076 return 0;
1077 }
1078
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02001079 /* Flush RX and TX works */
1080 flush_work(&hdev->tx_work);
Marcel Holtmannb78752c2010-08-08 23:06:53 -04001081 flush_work(&hdev->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001083 if (hdev->discov_timeout > 0) {
Johan Hedberge0f93092011-11-09 01:44:22 +02001084 cancel_delayed_work(&hdev->discov_off);
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001085 hdev->discov_timeout = 0;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001086 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001087 }
1088
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001089 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg7d785252011-12-15 00:47:39 +02001090 cancel_delayed_work(&hdev->service_cache);
1091
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001092 cancel_delayed_work_sync(&hdev->le_scan_disable);
1093
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001094 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 inquiry_cache_flush(hdev);
1096 hci_conn_hash_flush(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001097 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 hci_notify(hdev, HCI_DEV_DOWN);
1100
1101 if (hdev->flush)
1102 hdev->flush(hdev);
1103
1104 /* Reset device */
1105 skb_queue_purge(&hdev->cmd_q);
1106 atomic_set(&hdev->cmd_cnt, 1);
Johan Hedberg8af59462012-02-03 21:29:40 +02001107 if (!test_bit(HCI_RAW, &hdev->flags) &&
Szymon Janca6c511c2012-05-23 12:35:46 +02001108 test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 set_bit(HCI_INIT, &hdev->flags);
Johan Hedberg01178cd2013-03-05 20:37:41 +02001110 __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 clear_bit(HCI_INIT, &hdev->flags);
1112 }
1113
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02001114 /* flush cmd work */
1115 flush_work(&hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 /* Drop queues */
1118 skb_queue_purge(&hdev->rx_q);
1119 skb_queue_purge(&hdev->cmd_q);
1120 skb_queue_purge(&hdev->raw_q);
1121
1122 /* Drop last sent command */
1123 if (hdev->sent_cmd) {
Vinicius Costa Gomesb79f44c2011-04-11 18:46:55 -03001124 del_timer_sync(&hdev->cmd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 kfree_skb(hdev->sent_cmd);
1126 hdev->sent_cmd = NULL;
1127 }
1128
1129 /* After this point our queues are empty
1130 * and no tasks are scheduled. */
1131 hdev->close(hdev);
1132
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03001133 if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags) &&
1134 mgmt_valid_hdev(hdev)) {
Marcel Holtmann8ee56542012-02-21 12:33:48 +01001135 hci_dev_lock(hdev);
1136 mgmt_powered(hdev, 0);
1137 hci_dev_unlock(hdev);
1138 }
Johan Hedberg5add6af2010-12-16 10:00:37 +02001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* Clear flags */
1141 hdev->flags = 0;
1142
Andrei Emeltchenkoced5c332012-11-28 17:59:42 +02001143 /* Controller radio is available but is currently powered down */
1144 hdev->amp_status = 0;
1145
Johan Hedberge59fda82012-02-22 18:11:53 +02001146 memset(hdev->eir, 0, sizeof(hdev->eir));
Johan Hedberg09b3c3f2012-02-22 22:01:41 +02001147 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
Johan Hedberge59fda82012-02-22 18:11:53 +02001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 hci_req_unlock(hdev);
1150
1151 hci_dev_put(hdev);
1152 return 0;
1153}
1154
1155int hci_dev_close(__u16 dev)
1156{
1157 struct hci_dev *hdev;
1158 int err;
1159
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001160 hdev = hci_dev_get(dev);
1161 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -ENODEV;
Marcel Holtmann8ee56542012-02-21 12:33:48 +01001163
1164 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
1165 cancel_delayed_work(&hdev->power_off);
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 err = hci_dev_do_close(hdev);
Marcel Holtmann8ee56542012-02-21 12:33:48 +01001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 hci_dev_put(hdev);
1170 return err;
1171}
1172
1173int hci_dev_reset(__u16 dev)
1174{
1175 struct hci_dev *hdev;
1176 int ret = 0;
1177
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001178 hdev = hci_dev_get(dev);
1179 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return -ENODEV;
1181
1182 hci_req_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if (!test_bit(HCI_UP, &hdev->flags))
1185 goto done;
1186
1187 /* Drop queues */
1188 skb_queue_purge(&hdev->rx_q);
1189 skb_queue_purge(&hdev->cmd_q);
1190
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001191 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 inquiry_cache_flush(hdev);
1193 hci_conn_hash_flush(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001194 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (hdev->flush)
1197 hdev->flush(hdev);
1198
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001199 atomic_set(&hdev->cmd_cnt, 1);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001200 hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 if (!test_bit(HCI_RAW, &hdev->flags))
Johan Hedberg01178cd2013-03-05 20:37:41 +02001203 ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 hci_req_unlock(hdev);
1207 hci_dev_put(hdev);
1208 return ret;
1209}
1210
1211int hci_dev_reset_stat(__u16 dev)
1212{
1213 struct hci_dev *hdev;
1214 int ret = 0;
1215
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001216 hdev = hci_dev_get(dev);
1217 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return -ENODEV;
1219
1220 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
1221
1222 hci_dev_put(hdev);
1223
1224 return ret;
1225}
1226
1227int hci_dev_cmd(unsigned int cmd, void __user *arg)
1228{
1229 struct hci_dev *hdev;
1230 struct hci_dev_req dr;
1231 int err = 0;
1232
1233 if (copy_from_user(&dr, arg, sizeof(dr)))
1234 return -EFAULT;
1235
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001236 hdev = hci_dev_get(dr.dev_id);
1237 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -ENODEV;
1239
1240 switch (cmd) {
1241 case HCISETAUTH:
Johan Hedberg01178cd2013-03-05 20:37:41 +02001242 err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
1243 HCI_INIT_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 break;
1245
1246 case HCISETENCRYPT:
1247 if (!lmp_encrypt_capable(hdev)) {
1248 err = -EOPNOTSUPP;
1249 break;
1250 }
1251
1252 if (!test_bit(HCI_AUTH, &hdev->flags)) {
1253 /* Auth must be enabled first */
Johan Hedberg01178cd2013-03-05 20:37:41 +02001254 err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
1255 HCI_INIT_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (err)
1257 break;
1258 }
1259
Johan Hedberg01178cd2013-03-05 20:37:41 +02001260 err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt,
1261 HCI_INIT_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 break;
1263
1264 case HCISETSCAN:
Johan Hedberg01178cd2013-03-05 20:37:41 +02001265 err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt,
1266 HCI_INIT_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 break;
1268
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02001269 case HCISETLINKPOL:
Johan Hedberg01178cd2013-03-05 20:37:41 +02001270 err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt,
1271 HCI_INIT_TIMEOUT);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02001272 break;
1273
1274 case HCISETLINKMODE:
1275 hdev->link_mode = ((__u16) dr.dev_opt) &
1276 (HCI_LM_MASTER | HCI_LM_ACCEPT);
1277 break;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 case HCISETPTYPE:
1280 hdev->pkt_type = (__u16) dr.dev_opt;
1281 break;
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 case HCISETACLMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02001284 hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1);
1285 hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 break;
1287
1288 case HCISETSCOMTU:
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02001289 hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1);
1290 hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 break;
1292
1293 default:
1294 err = -EINVAL;
1295 break;
1296 }
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 hci_dev_put(hdev);
1299 return err;
1300}
1301
1302int hci_get_dev_list(void __user *arg)
1303{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001304 struct hci_dev *hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 struct hci_dev_list_req *dl;
1306 struct hci_dev_req *dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int n = 0, size, err;
1308 __u16 dev_num;
1309
1310 if (get_user(dev_num, (__u16 __user *) arg))
1311 return -EFAULT;
1312
1313 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
1314 return -EINVAL;
1315
1316 size = sizeof(*dl) + dev_num * sizeof(*dr);
1317
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001318 dl = kzalloc(size, GFP_KERNEL);
1319 if (!dl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return -ENOMEM;
1321
1322 dr = dl->dev_req;
1323
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001324 read_lock(&hci_dev_list_lock);
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001325 list_for_each_entry(hdev, &hci_dev_list, list) {
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001326 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Johan Hedberge0f93092011-11-09 01:44:22 +02001327 cancel_delayed_work(&hdev->power_off);
Johan Hedbergc542a062011-01-26 13:11:03 +02001328
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001329 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
1330 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergc542a062011-01-26 13:11:03 +02001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 (dr + n)->dev_id = hdev->id;
1333 (dr + n)->dev_opt = hdev->flags;
Johan Hedbergc542a062011-01-26 13:11:03 +02001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (++n >= dev_num)
1336 break;
1337 }
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001338 read_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 dl->dev_num = n;
1341 size = sizeof(*dl) + n * sizeof(*dr);
1342
1343 err = copy_to_user(arg, dl, size);
1344 kfree(dl);
1345
1346 return err ? -EFAULT : 0;
1347}
1348
1349int hci_get_dev_info(void __user *arg)
1350{
1351 struct hci_dev *hdev;
1352 struct hci_dev_info di;
1353 int err = 0;
1354
1355 if (copy_from_user(&di, arg, sizeof(di)))
1356 return -EFAULT;
1357
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001358 hdev = hci_dev_get(di.dev_id);
1359 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -ENODEV;
1361
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001362 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Johan Hedberg32435532011-11-07 22:16:04 +02001363 cancel_delayed_work_sync(&hdev->power_off);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001364
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001365 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
1366 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergc542a062011-01-26 13:11:03 +02001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 strcpy(di.name, hdev->name);
1369 di.bdaddr = hdev->bdaddr;
Marcel Holtmann943da252010-02-13 02:28:41 +01001370 di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 di.flags = hdev->flags;
1372 di.pkt_type = hdev->pkt_type;
Johan Hedberg572c7f82012-10-19 20:57:46 +03001373 if (lmp_bredr_capable(hdev)) {
1374 di.acl_mtu = hdev->acl_mtu;
1375 di.acl_pkts = hdev->acl_pkts;
1376 di.sco_mtu = hdev->sco_mtu;
1377 di.sco_pkts = hdev->sco_pkts;
1378 } else {
1379 di.acl_mtu = hdev->le_mtu;
1380 di.acl_pkts = hdev->le_pkts;
1381 di.sco_mtu = 0;
1382 di.sco_pkts = 0;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 di.link_policy = hdev->link_policy;
1385 di.link_mode = hdev->link_mode;
1386
1387 memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
1388 memcpy(&di.features, &hdev->features, sizeof(di.features));
1389
1390 if (copy_to_user(arg, &di, sizeof(di)))
1391 err = -EFAULT;
1392
1393 hci_dev_put(hdev);
1394
1395 return err;
1396}
1397
1398/* ---- Interface to HCI drivers ---- */
1399
Marcel Holtmann611b30f2009-06-08 14:41:38 +02001400static int hci_rfkill_set_block(void *data, bool blocked)
1401{
1402 struct hci_dev *hdev = data;
1403
1404 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
1405
1406 if (!blocked)
1407 return 0;
1408
1409 hci_dev_do_close(hdev);
1410
1411 return 0;
1412}
1413
1414static const struct rfkill_ops hci_rfkill_ops = {
1415 .set_block = hci_rfkill_set_block,
1416};
1417
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001418static void hci_power_on(struct work_struct *work)
1419{
1420 struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
1421
1422 BT_DBG("%s", hdev->name);
1423
1424 if (hci_dev_open(hdev->id) < 0)
1425 return;
1426
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001427 if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
Johan Hedberg19202572013-01-14 22:33:51 +02001428 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1429 HCI_AUTO_OFF_TIMEOUT);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001430
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001431 if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +02001432 mgmt_index_added(hdev);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001433}
1434
1435static void hci_power_off(struct work_struct *work)
1436{
Johan Hedberg32435532011-11-07 22:16:04 +02001437 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03001438 power_off.work);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001439
1440 BT_DBG("%s", hdev->name);
1441
Marcel Holtmann8ee56542012-02-21 12:33:48 +01001442 hci_dev_do_close(hdev);
Johan Hedbergab81cbf2010-12-15 13:53:18 +02001443}
1444
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001445static void hci_discov_off(struct work_struct *work)
1446{
1447 struct hci_dev *hdev;
1448 u8 scan = SCAN_PAGE;
1449
1450 hdev = container_of(work, struct hci_dev, discov_off.work);
1451
1452 BT_DBG("%s", hdev->name);
1453
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001454 hci_dev_lock(hdev);
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001455
1456 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
1457
1458 hdev->discov_timeout = 0;
1459
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001460 hci_dev_unlock(hdev);
Johan Hedberg16ab91a2011-11-07 22:16:02 +02001461}
1462
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001463int hci_uuids_clear(struct hci_dev *hdev)
1464{
Johan Hedberg48210022013-01-27 00:31:28 +02001465 struct bt_uuid *uuid, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001466
Johan Hedberg48210022013-01-27 00:31:28 +02001467 list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) {
1468 list_del(&uuid->list);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001469 kfree(uuid);
1470 }
1471
1472 return 0;
1473}
1474
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001475int hci_link_keys_clear(struct hci_dev *hdev)
1476{
1477 struct list_head *p, *n;
1478
1479 list_for_each_safe(p, n, &hdev->link_keys) {
1480 struct link_key *key;
1481
1482 key = list_entry(p, struct link_key, list);
1483
1484 list_del(p);
1485 kfree(key);
1486 }
1487
1488 return 0;
1489}
1490
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001491int hci_smp_ltks_clear(struct hci_dev *hdev)
1492{
1493 struct smp_ltk *k, *tmp;
1494
1495 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1496 list_del(&k->list);
1497 kfree(k);
1498 }
1499
1500 return 0;
1501}
1502
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001503struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1504{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001505 struct link_key *k;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001506
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001507 list_for_each_entry(k, &hdev->link_keys, list)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001508 if (bacmp(bdaddr, &k->bdaddr) == 0)
1509 return k;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001510
1511 return NULL;
1512}
1513
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301514static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03001515 u8 key_type, u8 old_key_type)
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001516{
1517 /* Legacy key */
1518 if (key_type < 0x03)
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301519 return true;
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001520
1521 /* Debug keys are insecure so don't store them persistently */
1522 if (key_type == HCI_LK_DEBUG_COMBINATION)
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301523 return false;
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001524
1525 /* Changed combination key and there's no previous one */
1526 if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff)
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301527 return false;
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001528
1529 /* Security mode 3 case */
1530 if (!conn)
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301531 return true;
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001532
1533 /* Neither local nor remote side had no-bonding as requirement */
1534 if (conn->auth_type > 0x01 && conn->remote_auth > 0x01)
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301535 return true;
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001536
1537 /* Local side had dedicated bonding as requirement */
1538 if (conn->auth_type == 0x02 || conn->auth_type == 0x03)
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301539 return true;
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001540
1541 /* Remote side had dedicated bonding as requirement */
1542 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03)
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301543 return true;
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001544
1545 /* If none of the above criteria match, then don't store the key
1546 * persistently */
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301547 return false;
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001548}
1549
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001550struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8])
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001551{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001552 struct smp_ltk *k;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001553
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001554 list_for_each_entry(k, &hdev->long_term_keys, list) {
1555 if (k->ediv != ediv ||
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03001556 memcmp(rand, k->rand, sizeof(k->rand)))
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001557 continue;
1558
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001559 return k;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001560 }
1561
1562 return NULL;
1563}
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001564
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001565struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001566 u8 addr_type)
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001567{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001568 struct smp_ltk *k;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001569
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001570 list_for_each_entry(k, &hdev->long_term_keys, list)
1571 if (addr_type == k->bdaddr_type &&
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03001572 bacmp(bdaddr, &k->bdaddr) == 0)
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001573 return k;
1574
1575 return NULL;
1576}
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001577
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001578int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001579 bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001580{
1581 struct link_key *key, *old_key;
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05301582 u8 old_key_type;
1583 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001584
1585 old_key = hci_find_link_key(hdev, bdaddr);
1586 if (old_key) {
1587 old_key_type = old_key->type;
1588 key = old_key;
1589 } else {
Johan Hedberg12adcf32011-04-28 11:29:00 -07001590 old_key_type = conn ? conn->key_type : 0xff;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001591 key = kzalloc(sizeof(*key), GFP_ATOMIC);
1592 if (!key)
1593 return -ENOMEM;
1594 list_add(&key->list, &hdev->link_keys);
1595 }
1596
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001597 BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001598
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001599 /* Some buggy controller combinations generate a changed
1600 * combination key for legacy pairing even when there's no
1601 * previous key */
1602 if (type == HCI_LK_CHANGED_COMBINATION &&
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03001603 (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) {
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001604 type = HCI_LK_COMBINATION;
Johan Hedberg655fe6e2011-04-28 11:29:01 -07001605 if (conn)
1606 conn->key_type = type;
1607 }
Johan Hedbergd25e28a2011-04-28 11:28:59 -07001608
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001609 bacpy(&key->bdaddr, bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03001610 memcpy(key->val, val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001611 key->pin_len = pin_len;
1612
Waldemar Rymarkiewiczb6020ba2011-04-28 12:07:53 +02001613 if (type == HCI_LK_CHANGED_COMBINATION)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001614 key->type = old_key_type;
Johan Hedberg4748fed2011-04-28 11:29:02 -07001615 else
1616 key->type = type;
1617
Johan Hedberg4df378a2011-04-28 11:29:03 -07001618 if (!new_key)
1619 return 0;
1620
1621 persistent = hci_persistent_key(hdev, conn, type, old_key_type);
1622
Johan Hedberg744cf192011-11-08 20:40:14 +02001623 mgmt_new_link_key(hdev, key, persistent);
Johan Hedberg4df378a2011-04-28 11:29:03 -07001624
Vishal Agarwal6ec5bca2012-04-16 14:44:44 +05301625 if (conn)
1626 conn->flush_key = !persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001627
1628 return 0;
1629}
1630
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001631int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type,
Andrei Emeltchenko9a006652012-03-09 12:12:12 +02001632 int new_key, u8 authenticated, u8 tk[16], u8 enc_size, __le16
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001633 ediv, u8 rand[8])
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001634{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001635 struct smp_ltk *key, *old_key;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001636
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001637 if (!(type & HCI_SMP_STK) && !(type & HCI_SMP_LTK))
1638 return 0;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001639
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001640 old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type);
1641 if (old_key)
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001642 key = old_key;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001643 else {
1644 key = kzalloc(sizeof(*key), GFP_ATOMIC);
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001645 if (!key)
1646 return -ENOMEM;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001647 list_add(&key->list, &hdev->long_term_keys);
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001648 }
1649
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001650 bacpy(&key->bdaddr, bdaddr);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001651 key->bdaddr_type = addr_type;
1652 memcpy(key->val, tk, sizeof(key->val));
1653 key->authenticated = authenticated;
1654 key->ediv = ediv;
1655 key->enc_size = enc_size;
1656 key->type = type;
1657 memcpy(key->rand, rand, sizeof(key->rand));
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001658
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03001659 if (!new_key)
1660 return 0;
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001661
Vinicius Costa Gomes261cc5a2012-02-02 21:08:05 -03001662 if (type & HCI_SMP_LTK)
1663 mgmt_new_ltk(hdev, key, 1);
1664
Vinicius Costa Gomes75d262c2011-07-07 18:59:36 -03001665 return 0;
1666}
1667
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001668int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1669{
1670 struct link_key *key;
1671
1672 key = hci_find_link_key(hdev, bdaddr);
1673 if (!key)
1674 return -ENOENT;
1675
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001676 BT_DBG("%s removing %pMR", hdev->name, bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001677
1678 list_del(&key->list);
1679 kfree(key);
1680
1681 return 0;
1682}
1683
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001684int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr)
1685{
1686 struct smp_ltk *k, *tmp;
1687
1688 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1689 if (bacmp(bdaddr, &k->bdaddr))
1690 continue;
1691
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001692 BT_DBG("%s removing %pMR", hdev->name, bdaddr);
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03001693
1694 list_del(&k->list);
1695 kfree(k);
1696 }
1697
1698 return 0;
1699}
1700
Ville Tervo6bd32322011-02-16 16:32:41 +02001701/* HCI command timer function */
Andrei Emeltchenkobda4f232012-06-11 11:13:08 +03001702static void hci_cmd_timeout(unsigned long arg)
Ville Tervo6bd32322011-02-16 16:32:41 +02001703{
1704 struct hci_dev *hdev = (void *) arg;
1705
Andrei Emeltchenkobda4f232012-06-11 11:13:08 +03001706 if (hdev->sent_cmd) {
1707 struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data;
1708 u16 opcode = __le16_to_cpu(sent->opcode);
1709
1710 BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode);
1711 } else {
1712 BT_ERR("%s command tx timeout", hdev->name);
1713 }
1714
Ville Tervo6bd32322011-02-16 16:32:41 +02001715 atomic_set(&hdev->cmd_cnt, 1);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02001716 queue_work(hdev->workqueue, &hdev->cmd_work);
Ville Tervo6bd32322011-02-16 16:32:41 +02001717}
1718
Szymon Janc2763eda2011-03-22 13:12:22 +01001719struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001720 bdaddr_t *bdaddr)
Szymon Janc2763eda2011-03-22 13:12:22 +01001721{
1722 struct oob_data *data;
1723
1724 list_for_each_entry(data, &hdev->remote_oob_data, list)
1725 if (bacmp(bdaddr, &data->bdaddr) == 0)
1726 return data;
1727
1728 return NULL;
1729}
1730
1731int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr)
1732{
1733 struct oob_data *data;
1734
1735 data = hci_find_remote_oob_data(hdev, bdaddr);
1736 if (!data)
1737 return -ENOENT;
1738
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001739 BT_DBG("%s removing %pMR", hdev->name, bdaddr);
Szymon Janc2763eda2011-03-22 13:12:22 +01001740
1741 list_del(&data->list);
1742 kfree(data);
1743
1744 return 0;
1745}
1746
1747int hci_remote_oob_data_clear(struct hci_dev *hdev)
1748{
1749 struct oob_data *data, *n;
1750
1751 list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) {
1752 list_del(&data->list);
1753 kfree(data);
1754 }
1755
1756 return 0;
1757}
1758
1759int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001760 u8 *randomizer)
Szymon Janc2763eda2011-03-22 13:12:22 +01001761{
1762 struct oob_data *data;
1763
1764 data = hci_find_remote_oob_data(hdev, bdaddr);
1765
1766 if (!data) {
1767 data = kmalloc(sizeof(*data), GFP_ATOMIC);
1768 if (!data)
1769 return -ENOMEM;
1770
1771 bacpy(&data->bdaddr, bdaddr);
1772 list_add(&data->list, &hdev->remote_oob_data);
1773 }
1774
1775 memcpy(data->hash, hash, sizeof(data->hash));
1776 memcpy(data->randomizer, randomizer, sizeof(data->randomizer));
1777
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001778 BT_DBG("%s for %pMR", hdev->name, bdaddr);
Szymon Janc2763eda2011-03-22 13:12:22 +01001779
1780 return 0;
1781}
1782
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001783struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
Antti Julkub2a66aa2011-06-15 12:01:14 +03001784{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001785 struct bdaddr_list *b;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001786
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001787 list_for_each_entry(b, &hdev->blacklist, list)
Antti Julkub2a66aa2011-06-15 12:01:14 +03001788 if (bacmp(bdaddr, &b->bdaddr) == 0)
1789 return b;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001790
1791 return NULL;
1792}
1793
1794int hci_blacklist_clear(struct hci_dev *hdev)
1795{
1796 struct list_head *p, *n;
1797
1798 list_for_each_safe(p, n, &hdev->blacklist) {
1799 struct bdaddr_list *b;
1800
1801 b = list_entry(p, struct bdaddr_list, list);
1802
1803 list_del(p);
1804 kfree(b);
1805 }
1806
1807 return 0;
1808}
1809
Johan Hedberg88c1fe42012-02-09 15:56:11 +02001810int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
Antti Julkub2a66aa2011-06-15 12:01:14 +03001811{
1812 struct bdaddr_list *entry;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001813
1814 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1815 return -EBADF;
1816
Antti Julku5e762442011-08-25 16:48:02 +03001817 if (hci_blacklist_lookup(hdev, bdaddr))
1818 return -EEXIST;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001819
1820 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
Antti Julku5e762442011-08-25 16:48:02 +03001821 if (!entry)
1822 return -ENOMEM;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001823
1824 bacpy(&entry->bdaddr, bdaddr);
1825
1826 list_add(&entry->list, &hdev->blacklist);
1827
Johan Hedberg88c1fe42012-02-09 15:56:11 +02001828 return mgmt_device_blocked(hdev, bdaddr, type);
Antti Julkub2a66aa2011-06-15 12:01:14 +03001829}
1830
Johan Hedberg88c1fe42012-02-09 15:56:11 +02001831int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
Antti Julkub2a66aa2011-06-15 12:01:14 +03001832{
1833 struct bdaddr_list *entry;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001834
Szymon Janc1ec918c2011-11-16 09:32:21 +01001835 if (bacmp(bdaddr, BDADDR_ANY) == 0)
Antti Julku5e762442011-08-25 16:48:02 +03001836 return hci_blacklist_clear(hdev);
Antti Julkub2a66aa2011-06-15 12:01:14 +03001837
1838 entry = hci_blacklist_lookup(hdev, bdaddr);
Szymon Janc1ec918c2011-11-16 09:32:21 +01001839 if (!entry)
Antti Julku5e762442011-08-25 16:48:02 +03001840 return -ENOENT;
Antti Julkub2a66aa2011-06-15 12:01:14 +03001841
1842 list_del(&entry->list);
1843 kfree(entry);
1844
Johan Hedberg88c1fe42012-02-09 15:56:11 +02001845 return mgmt_device_unblocked(hdev, bdaddr, type);
Antti Julkub2a66aa2011-06-15 12:01:14 +03001846}
1847
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001848static void le_scan_param_req(struct hci_dev *hdev, unsigned long opt)
1849{
1850 struct le_scan_params *param = (struct le_scan_params *) opt;
1851 struct hci_cp_le_set_scan_param cp;
1852
1853 memset(&cp, 0, sizeof(cp));
1854 cp.type = param->type;
1855 cp.interval = cpu_to_le16(param->interval);
1856 cp.window = cpu_to_le16(param->window);
1857
1858 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_PARAM, sizeof(cp), &cp);
1859}
1860
1861static void le_scan_enable_req(struct hci_dev *hdev, unsigned long opt)
1862{
1863 struct hci_cp_le_set_scan_enable cp;
1864
1865 memset(&cp, 0, sizeof(cp));
1866 cp.enable = 1;
Andre Guedes0431a432012-05-31 20:01:41 -03001867 cp.filter_dup = 1;
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001868
1869 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
1870}
1871
1872static int hci_do_le_scan(struct hci_dev *hdev, u8 type, u16 interval,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001873 u16 window, int timeout)
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001874{
1875 long timeo = msecs_to_jiffies(3000);
1876 struct le_scan_params param;
1877 int err;
1878
1879 BT_DBG("%s", hdev->name);
1880
1881 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))
1882 return -EINPROGRESS;
1883
1884 param.type = type;
1885 param.interval = interval;
1886 param.window = window;
1887
1888 hci_req_lock(hdev);
1889
Johan Hedberg01178cd2013-03-05 20:37:41 +02001890 err = __hci_req_sync(hdev, le_scan_param_req, (unsigned long) &param,
1891 timeo);
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001892 if (!err)
Johan Hedberg01178cd2013-03-05 20:37:41 +02001893 err = __hci_req_sync(hdev, le_scan_enable_req, 0, timeo);
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001894
1895 hci_req_unlock(hdev);
1896
1897 if (err < 0)
1898 return err;
1899
Johan Hedberg46818ed2013-01-14 22:33:52 +02001900 queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable,
1901 msecs_to_jiffies(timeout));
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001902
1903 return 0;
1904}
1905
Andre Guedes7dbfac12012-03-15 16:52:07 -03001906int hci_cancel_le_scan(struct hci_dev *hdev)
1907{
1908 BT_DBG("%s", hdev->name);
1909
1910 if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
1911 return -EALREADY;
1912
1913 if (cancel_delayed_work(&hdev->le_scan_disable)) {
1914 struct hci_cp_le_set_scan_enable cp;
1915
1916 /* Send HCI command to disable LE Scan */
1917 memset(&cp, 0, sizeof(cp));
1918 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
1919 }
1920
1921 return 0;
1922}
1923
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001924static void le_scan_disable_work(struct work_struct *work)
1925{
1926 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001927 le_scan_disable.work);
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001928 struct hci_cp_le_set_scan_enable cp;
1929
1930 BT_DBG("%s", hdev->name);
1931
1932 memset(&cp, 0, sizeof(cp));
1933
1934 hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
1935}
1936
Andre Guedes28b75a82012-02-03 17:48:00 -03001937static void le_scan_work(struct work_struct *work)
1938{
1939 struct hci_dev *hdev = container_of(work, struct hci_dev, le_scan);
1940 struct le_scan_params *param = &hdev->le_scan_params;
1941
1942 BT_DBG("%s", hdev->name);
1943
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001944 hci_do_le_scan(hdev, param->type, param->interval, param->window,
1945 param->timeout);
Andre Guedes28b75a82012-02-03 17:48:00 -03001946}
1947
1948int hci_le_scan(struct hci_dev *hdev, u8 type, u16 interval, u16 window,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001949 int timeout)
Andre Guedes28b75a82012-02-03 17:48:00 -03001950{
1951 struct le_scan_params *param = &hdev->le_scan_params;
1952
1953 BT_DBG("%s", hdev->name);
1954
Johan Hedbergf1550472012-10-24 21:12:03 +03001955 if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags))
1956 return -ENOTSUPP;
1957
Andre Guedes28b75a82012-02-03 17:48:00 -03001958 if (work_busy(&hdev->le_scan))
1959 return -EINPROGRESS;
1960
1961 param->type = type;
1962 param->interval = interval;
1963 param->window = window;
1964 param->timeout = timeout;
1965
1966 queue_work(system_long_wq, &hdev->le_scan);
1967
1968 return 0;
1969}
1970
David Herrmann9be0dab2012-04-22 14:39:57 +02001971/* Alloc HCI device */
1972struct hci_dev *hci_alloc_dev(void)
1973{
1974 struct hci_dev *hdev;
1975
1976 hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
1977 if (!hdev)
1978 return NULL;
1979
David Herrmannb1b813d2012-04-22 14:39:58 +02001980 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
1981 hdev->esco_type = (ESCO_HV1);
1982 hdev->link_mode = (HCI_LM_ACCEPT);
1983 hdev->io_capability = 0x03; /* No Input No Output */
Johan Hedbergbbaf4442012-11-08 01:22:59 +01001984 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
1985 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
David Herrmannb1b813d2012-04-22 14:39:58 +02001986
David Herrmannb1b813d2012-04-22 14:39:58 +02001987 hdev->sniff_max_interval = 800;
1988 hdev->sniff_min_interval = 80;
1989
1990 mutex_init(&hdev->lock);
1991 mutex_init(&hdev->req_lock);
1992
1993 INIT_LIST_HEAD(&hdev->mgmt_pending);
1994 INIT_LIST_HEAD(&hdev->blacklist);
1995 INIT_LIST_HEAD(&hdev->uuids);
1996 INIT_LIST_HEAD(&hdev->link_keys);
1997 INIT_LIST_HEAD(&hdev->long_term_keys);
1998 INIT_LIST_HEAD(&hdev->remote_oob_data);
Andrei Emeltchenko6b536b52012-08-31 16:39:28 +03001999 INIT_LIST_HEAD(&hdev->conn_hash.list);
David Herrmannb1b813d2012-04-22 14:39:58 +02002000
2001 INIT_WORK(&hdev->rx_work, hci_rx_work);
2002 INIT_WORK(&hdev->cmd_work, hci_cmd_work);
2003 INIT_WORK(&hdev->tx_work, hci_tx_work);
2004 INIT_WORK(&hdev->power_on, hci_power_on);
2005 INIT_WORK(&hdev->le_scan, le_scan_work);
2006
David Herrmannb1b813d2012-04-22 14:39:58 +02002007 INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
2008 INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
2009 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
2010
David Herrmann9be0dab2012-04-22 14:39:57 +02002011 skb_queue_head_init(&hdev->driver_init);
David Herrmannb1b813d2012-04-22 14:39:58 +02002012 skb_queue_head_init(&hdev->rx_q);
2013 skb_queue_head_init(&hdev->cmd_q);
2014 skb_queue_head_init(&hdev->raw_q);
2015
2016 init_waitqueue_head(&hdev->req_wait_q);
2017
Andrei Emeltchenkobda4f232012-06-11 11:13:08 +03002018 setup_timer(&hdev->cmd_timer, hci_cmd_timeout, (unsigned long) hdev);
David Herrmannb1b813d2012-04-22 14:39:58 +02002019
David Herrmannb1b813d2012-04-22 14:39:58 +02002020 hci_init_sysfs(hdev);
2021 discovery_init(hdev);
David Herrmann9be0dab2012-04-22 14:39:57 +02002022
2023 return hdev;
2024}
2025EXPORT_SYMBOL(hci_alloc_dev);
2026
2027/* Free HCI device */
2028void hci_free_dev(struct hci_dev *hdev)
2029{
2030 skb_queue_purge(&hdev->driver_init);
2031
2032 /* will free via device release */
2033 put_device(&hdev->dev);
2034}
2035EXPORT_SYMBOL(hci_free_dev);
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/* Register HCI device */
2038int hci_register_dev(struct hci_dev *hdev)
2039{
David Herrmannb1b813d2012-04-22 14:39:58 +02002040 int id, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
David Herrmann010666a2012-01-07 15:47:07 +01002042 if (!hdev->open || !hdev->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return -EINVAL;
2044
Mat Martineau08add512011-11-02 16:18:36 -07002045 /* Do not allow HCI_AMP devices to register at index 0,
2046 * so the index can be used as the AMP controller ID.
2047 */
Sasha Levin3df92b32012-05-27 22:36:56 +02002048 switch (hdev->dev_type) {
2049 case HCI_BREDR:
2050 id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL);
2051 break;
2052 case HCI_AMP:
2053 id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL);
2054 break;
2055 default:
2056 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002058
Sasha Levin3df92b32012-05-27 22:36:56 +02002059 if (id < 0)
2060 return id;
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 sprintf(hdev->name, "hci%d", id);
2063 hdev->id = id;
Andrei Emeltchenko2d8b3a12012-04-16 16:32:04 +03002064
2065 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
2066
Sasha Levin3df92b32012-05-27 22:36:56 +02002067 write_lock(&hci_dev_list_lock);
2068 list_add(&hdev->list, &hci_dev_list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002069 write_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Gustavo F. Padovan32845eb2011-12-17 17:47:30 -02002071 hdev->workqueue = alloc_workqueue(hdev->name, WQ_HIGHPRI | WQ_UNBOUND |
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002072 WQ_MEM_RECLAIM, 1);
David Herrmann33ca9542011-10-08 14:58:49 +02002073 if (!hdev->workqueue) {
2074 error = -ENOMEM;
2075 goto err;
2076 }
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01002077
Johan Hedberg6ead1bb2013-01-14 22:33:50 +02002078 hdev->req_workqueue = alloc_workqueue(hdev->name,
2079 WQ_HIGHPRI | WQ_UNBOUND |
2080 WQ_MEM_RECLAIM, 1);
2081 if (!hdev->req_workqueue) {
2082 destroy_workqueue(hdev->workqueue);
2083 error = -ENOMEM;
2084 goto err;
2085 }
2086
David Herrmann33ca9542011-10-08 14:58:49 +02002087 error = hci_add_sysfs(hdev);
2088 if (error < 0)
2089 goto err_wqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Marcel Holtmann611b30f2009-06-08 14:41:38 +02002091 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002092 RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops,
2093 hdev);
Marcel Holtmann611b30f2009-06-08 14:41:38 +02002094 if (hdev->rfkill) {
2095 if (rfkill_register(hdev->rfkill) < 0) {
2096 rfkill_destroy(hdev->rfkill);
2097 hdev->rfkill = NULL;
2098 }
2099 }
2100
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002101 set_bit(HCI_SETUP, &hdev->dev_flags);
Andrei Emeltchenkoce2be9a2012-06-29 15:07:00 +03002102
2103 if (hdev->dev_type != HCI_AMP)
2104 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 hci_notify(hdev, HCI_DEV_REG);
David Herrmanndc946bd2012-01-07 15:47:24 +01002107 hci_dev_hold(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Johan Hedberg19202572013-01-14 22:33:51 +02002109 queue_work(hdev->req_workqueue, &hdev->power_on);
Marcel Holtmannfbe96d62012-10-30 01:35:40 -07002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return id;
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01002112
David Herrmann33ca9542011-10-08 14:58:49 +02002113err_wqueue:
2114 destroy_workqueue(hdev->workqueue);
Johan Hedberg6ead1bb2013-01-14 22:33:50 +02002115 destroy_workqueue(hdev->req_workqueue);
David Herrmann33ca9542011-10-08 14:58:49 +02002116err:
Sasha Levin3df92b32012-05-27 22:36:56 +02002117 ida_simple_remove(&hci_index_ida, hdev->id);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002118 write_lock(&hci_dev_list_lock);
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01002119 list_del(&hdev->list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002120 write_unlock(&hci_dev_list_lock);
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01002121
David Herrmann33ca9542011-10-08 14:58:49 +02002122 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124EXPORT_SYMBOL(hci_register_dev);
2125
2126/* Unregister HCI device */
David Herrmann59735632011-10-26 10:43:19 +02002127void hci_unregister_dev(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Sasha Levin3df92b32012-05-27 22:36:56 +02002129 int i, id;
Marcel Holtmannef222012007-07-11 06:42:04 +02002130
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002131 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Johan Hovold94324962012-03-15 14:48:41 +01002133 set_bit(HCI_UNREGISTER, &hdev->dev_flags);
2134
Sasha Levin3df92b32012-05-27 22:36:56 +02002135 id = hdev->id;
2136
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002137 write_lock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 list_del(&hdev->list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002139 write_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 hci_dev_do_close(hdev);
2142
Suraj Sumangalacd4c5392010-07-14 13:02:16 +05302143 for (i = 0; i < NUM_REASSEMBLY; i++)
Marcel Holtmannef222012007-07-11 06:42:04 +02002144 kfree_skb(hdev->reassembly[i]);
2145
Gustavo Padovanb9b5ef12012-11-21 00:50:21 -02002146 cancel_work_sync(&hdev->power_on);
2147
Johan Hedbergab81cbf2010-12-15 13:53:18 +02002148 if (!test_bit(HCI_INIT, &hdev->flags) &&
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002149 !test_bit(HCI_SETUP, &hdev->dev_flags)) {
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002150 hci_dev_lock(hdev);
Johan Hedberg744cf192011-11-08 20:40:14 +02002151 mgmt_index_removed(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002152 hci_dev_unlock(hdev);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002153 }
Johan Hedbergab81cbf2010-12-15 13:53:18 +02002154
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002155 /* mgmt_index_removed should take care of emptying the
2156 * pending list */
2157 BUG_ON(!list_empty(&hdev->mgmt_pending));
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 hci_notify(hdev, HCI_DEV_UNREG);
2160
Marcel Holtmann611b30f2009-06-08 14:41:38 +02002161 if (hdev->rfkill) {
2162 rfkill_unregister(hdev->rfkill);
2163 rfkill_destroy(hdev->rfkill);
2164 }
2165
David Herrmannce242972011-10-08 14:58:48 +02002166 hci_del_sysfs(hdev);
Dave Young147e2d52008-03-05 18:45:59 -08002167
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01002168 destroy_workqueue(hdev->workqueue);
Johan Hedberg6ead1bb2013-01-14 22:33:50 +02002169 destroy_workqueue(hdev->req_workqueue);
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +01002170
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002171 hci_dev_lock(hdev);
Johan Hedberge2e0cac2011-01-04 12:08:50 +02002172 hci_blacklist_clear(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002173 hci_uuids_clear(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002174 hci_link_keys_clear(hdev);
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -03002175 hci_smp_ltks_clear(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01002176 hci_remote_oob_data_clear(hdev);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002177 hci_dev_unlock(hdev);
Johan Hedberge2e0cac2011-01-04 12:08:50 +02002178
David Herrmanndc946bd2012-01-07 15:47:24 +01002179 hci_dev_put(hdev);
Sasha Levin3df92b32012-05-27 22:36:56 +02002180
2181 ida_simple_remove(&hci_index_ida, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183EXPORT_SYMBOL(hci_unregister_dev);
2184
2185/* Suspend HCI device */
2186int hci_suspend_dev(struct hci_dev *hdev)
2187{
2188 hci_notify(hdev, HCI_DEV_SUSPEND);
2189 return 0;
2190}
2191EXPORT_SYMBOL(hci_suspend_dev);
2192
2193/* Resume HCI device */
2194int hci_resume_dev(struct hci_dev *hdev)
2195{
2196 hci_notify(hdev, HCI_DEV_RESUME);
2197 return 0;
2198}
2199EXPORT_SYMBOL(hci_resume_dev);
2200
Marcel Holtmann76bca882009-11-18 00:40:39 +01002201/* Receive frame from HCI drivers */
2202int hci_recv_frame(struct sk_buff *skb)
2203{
2204 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2205 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002206 && !test_bit(HCI_INIT, &hdev->flags))) {
Marcel Holtmann76bca882009-11-18 00:40:39 +01002207 kfree_skb(skb);
2208 return -ENXIO;
2209 }
2210
Jorrit Schippersd82603c2012-12-27 17:33:02 +01002211 /* Incoming skb */
Marcel Holtmann76bca882009-11-18 00:40:39 +01002212 bt_cb(skb)->incoming = 1;
2213
2214 /* Time stamp */
2215 __net_timestamp(skb);
2216
Marcel Holtmann76bca882009-11-18 00:40:39 +01002217 skb_queue_tail(&hdev->rx_q, skb);
Marcel Holtmannb78752c2010-08-08 23:06:53 -04002218 queue_work(hdev->workqueue, &hdev->rx_work);
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002219
Marcel Holtmann76bca882009-11-18 00:40:39 +01002220 return 0;
2221}
2222EXPORT_SYMBOL(hci_recv_frame);
2223
Suraj Sumangala33e882a2010-07-14 13:02:17 +05302224static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002225 int count, __u8 index)
Suraj Sumangala33e882a2010-07-14 13:02:17 +05302226{
2227 int len = 0;
2228 int hlen = 0;
2229 int remain = count;
2230 struct sk_buff *skb;
2231 struct bt_skb_cb *scb;
2232
2233 if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002234 index >= NUM_REASSEMBLY)
Suraj Sumangala33e882a2010-07-14 13:02:17 +05302235 return -EILSEQ;
2236
2237 skb = hdev->reassembly[index];
2238
2239 if (!skb) {
2240 switch (type) {
2241 case HCI_ACLDATA_PKT:
2242 len = HCI_MAX_FRAME_SIZE;
2243 hlen = HCI_ACL_HDR_SIZE;
2244 break;
2245 case HCI_EVENT_PKT:
2246 len = HCI_MAX_EVENT_SIZE;
2247 hlen = HCI_EVENT_HDR_SIZE;
2248 break;
2249 case HCI_SCODATA_PKT:
2250 len = HCI_MAX_SCO_SIZE;
2251 hlen = HCI_SCO_HDR_SIZE;
2252 break;
2253 }
2254
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03002255 skb = bt_skb_alloc(len, GFP_ATOMIC);
Suraj Sumangala33e882a2010-07-14 13:02:17 +05302256 if (!skb)
2257 return -ENOMEM;
2258
2259 scb = (void *) skb->cb;
2260 scb->expect = hlen;
2261 scb->pkt_type = type;
2262
2263 skb->dev = (void *) hdev;
2264 hdev->reassembly[index] = skb;
2265 }
2266
2267 while (count) {
2268 scb = (void *) skb->cb;
Dan Carpenter89bb46d2012-02-28 09:57:59 +03002269 len = min_t(uint, scb->expect, count);
Suraj Sumangala33e882a2010-07-14 13:02:17 +05302270
2271 memcpy(skb_put(skb, len), data, len);
2272
2273 count -= len;
2274 data += len;
2275 scb->expect -= len;
2276 remain = count;
2277
2278 switch (type) {
2279 case HCI_EVENT_PKT:
2280 if (skb->len == HCI_EVENT_HDR_SIZE) {
2281 struct hci_event_hdr *h = hci_event_hdr(skb);
2282 scb->expect = h->plen;
2283
2284 if (skb_tailroom(skb) < scb->expect) {
2285 kfree_skb(skb);
2286 hdev->reassembly[index] = NULL;
2287 return -ENOMEM;
2288 }
2289 }
2290 break;
2291
2292 case HCI_ACLDATA_PKT:
2293 if (skb->len == HCI_ACL_HDR_SIZE) {
2294 struct hci_acl_hdr *h = hci_acl_hdr(skb);
2295 scb->expect = __le16_to_cpu(h->dlen);
2296
2297 if (skb_tailroom(skb) < scb->expect) {
2298 kfree_skb(skb);
2299 hdev->reassembly[index] = NULL;
2300 return -ENOMEM;
2301 }
2302 }
2303 break;
2304
2305 case HCI_SCODATA_PKT:
2306 if (skb->len == HCI_SCO_HDR_SIZE) {
2307 struct hci_sco_hdr *h = hci_sco_hdr(skb);
2308 scb->expect = h->dlen;
2309
2310 if (skb_tailroom(skb) < scb->expect) {
2311 kfree_skb(skb);
2312 hdev->reassembly[index] = NULL;
2313 return -ENOMEM;
2314 }
2315 }
2316 break;
2317 }
2318
2319 if (scb->expect == 0) {
2320 /* Complete frame */
2321
2322 bt_cb(skb)->pkt_type = type;
2323 hci_recv_frame(skb);
2324
2325 hdev->reassembly[index] = NULL;
2326 return remain;
2327 }
2328 }
2329
2330 return remain;
2331}
2332
Marcel Holtmannef222012007-07-11 06:42:04 +02002333int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
2334{
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05302335 int rem = 0;
2336
Marcel Holtmannef222012007-07-11 06:42:04 +02002337 if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
2338 return -EILSEQ;
2339
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03002340 while (count) {
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03002341 rem = hci_reassembly(hdev, type, data, count, type - 1);
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05302342 if (rem < 0)
2343 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02002344
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05302345 data += (count - rem);
2346 count = rem;
Joe Perchesf81c6222011-06-03 11:51:19 +00002347 }
Marcel Holtmannef222012007-07-11 06:42:04 +02002348
Suraj Sumangalaf39a3c02010-07-14 13:02:18 +05302349 return rem;
Marcel Holtmannef222012007-07-11 06:42:04 +02002350}
2351EXPORT_SYMBOL(hci_recv_fragment);
2352
Suraj Sumangala99811512010-07-14 13:02:19 +05302353#define STREAM_REASSEMBLY 0
2354
2355int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
2356{
2357 int type;
2358 int rem = 0;
2359
Gustavo F. Padovanda5f6c32010-07-24 01:34:54 -03002360 while (count) {
Suraj Sumangala99811512010-07-14 13:02:19 +05302361 struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
2362
2363 if (!skb) {
2364 struct { char type; } *pkt;
2365
2366 /* Start of the frame */
2367 pkt = data;
2368 type = pkt->type;
2369
2370 data++;
2371 count--;
2372 } else
2373 type = bt_cb(skb)->pkt_type;
2374
Gustavo F. Padovan1e429f32011-04-04 18:25:14 -03002375 rem = hci_reassembly(hdev, type, data, count,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002376 STREAM_REASSEMBLY);
Suraj Sumangala99811512010-07-14 13:02:19 +05302377 if (rem < 0)
2378 return rem;
2379
2380 data += (count - rem);
2381 count = rem;
Joe Perchesf81c6222011-06-03 11:51:19 +00002382 }
Suraj Sumangala99811512010-07-14 13:02:19 +05302383
2384 return rem;
2385}
2386EXPORT_SYMBOL(hci_recv_stream_fragment);
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388/* ---- Interface to upper protocols ---- */
2389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390int hci_register_cb(struct hci_cb *cb)
2391{
2392 BT_DBG("%p name %s", cb, cb->name);
2393
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002394 write_lock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 list_add(&cb->list, &hci_cb_list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002396 write_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 return 0;
2399}
2400EXPORT_SYMBOL(hci_register_cb);
2401
2402int hci_unregister_cb(struct hci_cb *cb)
2403{
2404 BT_DBG("%p name %s", cb, cb->name);
2405
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002406 write_lock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 list_del(&cb->list);
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02002408 write_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 return 0;
2411}
2412EXPORT_SYMBOL(hci_unregister_cb);
2413
2414static int hci_send_frame(struct sk_buff *skb)
2415{
2416 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2417
2418 if (!hdev) {
2419 kfree_skb(skb);
2420 return -ENODEV;
2421 }
2422
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002423 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Marcel Holtmanncd82e612012-02-20 20:34:38 +01002425 /* Time stamp */
2426 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Marcel Holtmanncd82e612012-02-20 20:34:38 +01002428 /* Send copy to monitor */
2429 hci_send_to_monitor(hdev, skb);
2430
2431 if (atomic_read(&hdev->promisc)) {
2432 /* Send copy to the sockets */
Marcel Holtmann470fe1b2012-02-20 14:50:30 +01002433 hci_send_to_sock(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
2435
2436 /* Get rid of skb owner, prior to sending to the driver. */
2437 skb_orphan(skb);
2438
2439 return hdev->send(skb);
2440}
2441
Johan Hedberg3119ae92013-03-05 20:37:44 +02002442void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
2443{
2444 skb_queue_head_init(&req->cmd_q);
2445 req->hdev = hdev;
2446}
2447
2448int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
2449{
2450 struct hci_dev *hdev = req->hdev;
2451 struct sk_buff *skb;
2452 unsigned long flags;
2453
2454 BT_DBG("length %u", skb_queue_len(&req->cmd_q));
2455
2456 /* Do not allow empty requests */
2457 if (skb_queue_empty(&req->cmd_q))
2458 return -EINVAL;
2459
2460 skb = skb_peek_tail(&req->cmd_q);
2461 bt_cb(skb)->req.complete = complete;
2462
2463 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
2464 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
2465 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
2466
2467 queue_work(hdev->workqueue, &hdev->cmd_work);
2468
2469 return 0;
2470}
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472/* Send HCI command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002473int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
2475 int len = HCI_COMMAND_HDR_SIZE + plen;
2476 struct hci_command_hdr *hdr;
2477 struct sk_buff *skb;
2478
Andrei Emeltchenkof0e09512012-06-11 11:13:09 +03002479 BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 skb = bt_skb_alloc(len, GFP_ATOMIC);
2482 if (!skb) {
Marcel Holtmannef222012007-07-11 06:42:04 +02002483 BT_ERR("%s no memory for command", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return -ENOMEM;
2485 }
2486
2487 hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002488 hdr->opcode = cpu_to_le16(opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 hdr->plen = plen;
2490
2491 if (plen)
2492 memcpy(skb_put(skb, plen), param, plen);
2493
2494 BT_DBG("skb len %d", skb->len);
2495
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002496 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 skb->dev = (void *) hdev;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002498
Johan Hedberga5040ef2011-01-10 13:28:59 +02002499 if (test_bit(HCI_INIT, &hdev->flags))
2500 hdev->init_last_cmd = opcode;
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 skb_queue_tail(&hdev->cmd_q, skb);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002503 queue_work(hdev->workqueue, &hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 return 0;
2506}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508/* Get data from the previously sent command */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002509void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 struct hci_command_hdr *hdr;
2512
2513 if (!hdev->sent_cmd)
2514 return NULL;
2515
2516 hdr = (void *) hdev->sent_cmd->data;
2517
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002518 if (hdr->opcode != cpu_to_le16(opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return NULL;
2520
Andrei Emeltchenkof0e09512012-06-11 11:13:09 +03002521 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
2524}
2525
2526/* Send ACL data */
2527static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
2528{
2529 struct hci_acl_hdr *hdr;
2530 int len = skb->len;
2531
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03002532 skb_push(skb, HCI_ACL_HDR_SIZE);
2533 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002534 hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002535 hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
2536 hdr->dlen = cpu_to_le16(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Andrei Emeltchenkoee22be72012-09-21 12:30:04 +03002539static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002540 struct sk_buff *skb, __u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Andrei Emeltchenkoee22be72012-09-21 12:30:04 +03002542 struct hci_conn *conn = chan->conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct hci_dev *hdev = conn->hdev;
2544 struct sk_buff *list;
2545
Gustavo Padovan087bfd92012-05-11 13:16:11 -03002546 skb->len = skb_headlen(skb);
2547 skb->data_len = 0;
2548
2549 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
Andrei Emeltchenko204a6e52012-10-15 11:58:39 +03002550
2551 switch (hdev->dev_type) {
2552 case HCI_BREDR:
2553 hci_add_acl_hdr(skb, conn->handle, flags);
2554 break;
2555 case HCI_AMP:
2556 hci_add_acl_hdr(skb, chan->handle, flags);
2557 break;
2558 default:
2559 BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
2560 return;
2561 }
Gustavo Padovan087bfd92012-05-11 13:16:11 -03002562
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002563 list = skb_shinfo(skb)->frag_list;
2564 if (!list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 /* Non fragmented */
2566 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
2567
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002568 skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 } else {
2570 /* Fragmented */
2571 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2572
2573 skb_shinfo(skb)->frag_list = NULL;
2574
2575 /* Queue all fragments atomically */
Gustavo F. Padovanaf3e6352011-12-22 16:35:05 -02002576 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002578 __skb_queue_tail(queue, skb);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02002579
2580 flags &= ~ACL_START;
2581 flags |= ACL_CONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 do {
2583 skb = list; list = list->next;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002586 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +02002587 hci_add_acl_hdr(skb, conn->handle, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2590
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002591 __skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 } while (list);
2593
Gustavo F. Padovanaf3e6352011-12-22 16:35:05 -02002594 spin_unlock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002596}
2597
2598void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
2599{
Andrei Emeltchenkoee22be72012-09-21 12:30:04 +03002600 struct hci_dev *hdev = chan->conn->hdev;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002601
Andrei Emeltchenkof0e09512012-06-11 11:13:09 +03002602 BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002603
2604 skb->dev = (void *) hdev;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002605
Andrei Emeltchenkoee22be72012-09-21 12:30:04 +03002606 hci_queue_acl(chan, &chan->data_q, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002608 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611/* Send SCO data */
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -03002612void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
2614 struct hci_dev *hdev = conn->hdev;
2615 struct hci_sco_hdr hdr;
2616
2617 BT_DBG("%s len %d", hdev->name, skb->len);
2618
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002619 hdr.handle = cpu_to_le16(conn->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 hdr.dlen = skb->len;
2621
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03002622 skb_push(skb, HCI_SCO_HDR_SIZE);
2623 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002624 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 skb->dev = (void *) hdev;
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002627 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 skb_queue_tail(&conn->data_q, skb);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002630 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633/* ---- HCI TX task (outgoing data) ---- */
2634
2635/* HCI Connection scheduler */
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002636static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type,
2637 int *quote)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 struct hci_conn_hash *h = &hdev->conn_hash;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002640 struct hci_conn *conn = NULL, *c;
Mikel Astizabc5de82012-04-11 08:48:47 +02002641 unsigned int num = 0, min = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002643 /* We don't have to lock device here. Connections are always
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 * added and removed with TX task disabled. */
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002645
2646 rcu_read_lock();
2647
2648 list_for_each_entry_rcu(c, &h->list, list) {
Marcel Holtmann769be972008-07-14 20:13:49 +02002649 if (c->type != type || skb_queue_empty(&c->data_q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 continue;
Marcel Holtmann769be972008-07-14 20:13:49 +02002651
2652 if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
2653 continue;
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 num++;
2656
2657 if (c->sent < min) {
2658 min = c->sent;
2659 conn = c;
2660 }
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002661
2662 if (hci_conn_num(hdev, type) == num)
2663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 }
2665
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002666 rcu_read_unlock();
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (conn) {
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002669 int cnt, q;
2670
2671 switch (conn->type) {
2672 case ACL_LINK:
2673 cnt = hdev->acl_cnt;
2674 break;
2675 case SCO_LINK:
2676 case ESCO_LINK:
2677 cnt = hdev->sco_cnt;
2678 break;
2679 case LE_LINK:
2680 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2681 break;
2682 default:
2683 cnt = 0;
2684 BT_ERR("Unknown link type");
2685 }
2686
2687 q = cnt / num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 *quote = q ? q : 1;
2689 } else
2690 *quote = 0;
2691
2692 BT_DBG("conn %p quote %d", conn, *quote);
2693 return conn;
2694}
2695
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002696static void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
2698 struct hci_conn_hash *h = &hdev->conn_hash;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002699 struct hci_conn *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Ville Tervobae1f5d92011-02-10 22:38:53 -03002701 BT_ERR("%s link tx timeout", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002703 rcu_read_lock();
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /* Kill stalled connections */
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002706 list_for_each_entry_rcu(c, &h->list, list) {
Ville Tervobae1f5d92011-02-10 22:38:53 -03002707 if (c->type == type && c->sent) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002708 BT_ERR("%s killing stalled connection %pMR",
2709 hdev->name, &c->dst);
Andre Guedesbed71742013-01-30 11:50:56 -03002710 hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
2712 }
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002713
2714 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715}
2716
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002717static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
2718 int *quote)
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002719{
2720 struct hci_conn_hash *h = &hdev->conn_hash;
2721 struct hci_chan *chan = NULL;
Mikel Astizabc5de82012-04-11 08:48:47 +02002722 unsigned int num = 0, min = ~0, cur_prio = 0;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002723 struct hci_conn *conn;
2724 int cnt, q, conn_num = 0;
2725
2726 BT_DBG("%s", hdev->name);
2727
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002728 rcu_read_lock();
2729
2730 list_for_each_entry_rcu(conn, &h->list, list) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002731 struct hci_chan *tmp;
2732
2733 if (conn->type != type)
2734 continue;
2735
2736 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2737 continue;
2738
2739 conn_num++;
2740
Gustavo F. Padovan8192ede2011-12-14 15:08:48 -02002741 list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002742 struct sk_buff *skb;
2743
2744 if (skb_queue_empty(&tmp->data_q))
2745 continue;
2746
2747 skb = skb_peek(&tmp->data_q);
2748 if (skb->priority < cur_prio)
2749 continue;
2750
2751 if (skb->priority > cur_prio) {
2752 num = 0;
2753 min = ~0;
2754 cur_prio = skb->priority;
2755 }
2756
2757 num++;
2758
2759 if (conn->sent < min) {
2760 min = conn->sent;
2761 chan = tmp;
2762 }
2763 }
2764
2765 if (hci_conn_num(hdev, type) == conn_num)
2766 break;
2767 }
2768
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002769 rcu_read_unlock();
2770
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002771 if (!chan)
2772 return NULL;
2773
2774 switch (chan->conn->type) {
2775 case ACL_LINK:
2776 cnt = hdev->acl_cnt;
2777 break;
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03002778 case AMP_LINK:
2779 cnt = hdev->block_cnt;
2780 break;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002781 case SCO_LINK:
2782 case ESCO_LINK:
2783 cnt = hdev->sco_cnt;
2784 break;
2785 case LE_LINK:
2786 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2787 break;
2788 default:
2789 cnt = 0;
2790 BT_ERR("Unknown link type");
2791 }
2792
2793 q = cnt / num;
2794 *quote = q ? q : 1;
2795 BT_DBG("chan %p quote %d", chan, *quote);
2796 return chan;
2797}
2798
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002799static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
2800{
2801 struct hci_conn_hash *h = &hdev->conn_hash;
2802 struct hci_conn *conn;
2803 int num = 0;
2804
2805 BT_DBG("%s", hdev->name);
2806
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002807 rcu_read_lock();
2808
2809 list_for_each_entry_rcu(conn, &h->list, list) {
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002810 struct hci_chan *chan;
2811
2812 if (conn->type != type)
2813 continue;
2814
2815 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2816 continue;
2817
2818 num++;
2819
Gustavo F. Padovan8192ede2011-12-14 15:08:48 -02002820 list_for_each_entry_rcu(chan, &conn->chan_list, list) {
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002821 struct sk_buff *skb;
2822
2823 if (chan->sent) {
2824 chan->sent = 0;
2825 continue;
2826 }
2827
2828 if (skb_queue_empty(&chan->data_q))
2829 continue;
2830
2831 skb = skb_peek(&chan->data_q);
2832 if (skb->priority >= HCI_PRIO_MAX - 1)
2833 continue;
2834
2835 skb->priority = HCI_PRIO_MAX - 1;
2836
2837 BT_DBG("chan %p skb %p promoted to %d", chan, skb,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002838 skb->priority);
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002839 }
2840
2841 if (hci_conn_num(hdev, type) == num)
2842 break;
2843 }
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -02002844
2845 rcu_read_unlock();
2846
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002847}
2848
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002849static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb)
2850{
2851 /* Calculate count of blocks used by this packet */
2852 return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len);
2853}
2854
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002855static void __check_timeout(struct hci_dev *hdev, unsigned int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (!test_bit(HCI_RAW, &hdev->flags)) {
2858 /* ACL tx timeout must be longer than maximum
2859 * link supervision timeout (40.9 seconds) */
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002860 if (!cnt && time_after(jiffies, hdev->acl_last_tx +
Andrei Emeltchenko5f246e82012-06-11 11:13:07 +03002861 HCI_ACL_TX_TIMEOUT))
Ville Tervobae1f5d92011-02-10 22:38:53 -03002862 hci_link_tx_to(hdev, ACL_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002864}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002866static void hci_sched_acl_pkt(struct hci_dev *hdev)
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002867{
2868 unsigned int cnt = hdev->acl_cnt;
2869 struct hci_chan *chan;
2870 struct sk_buff *skb;
2871 int quote;
2872
2873 __check_timeout(hdev, cnt);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002874
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002875 while (hdev->acl_cnt &&
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002876 (chan = hci_chan_sent(hdev, ACL_LINK, &quote))) {
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002877 u32 priority = (skb_peek(&chan->data_q))->priority;
2878 while (quote-- && (skb = skb_peek(&chan->data_q))) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002879 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002880 skb->len, skb->priority);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002881
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02002882 /* Stop if priority has changed */
2883 if (skb->priority < priority)
2884 break;
2885
2886 skb = skb_dequeue(&chan->data_q);
2887
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002888 hci_conn_enter_active_mode(chan->conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002889 bt_cb(skb)->force_active);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 hci_send_frame(skb);
2892 hdev->acl_last_tx = jiffies;
2893
2894 hdev->acl_cnt--;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002895 chan->sent++;
2896 chan->conn->sent++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898 }
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02002899
2900 if (cnt != hdev->acl_cnt)
2901 hci_prio_recalculate(hdev, ACL_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002904static void hci_sched_acl_blk(struct hci_dev *hdev)
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002905{
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002906 unsigned int cnt = hdev->block_cnt;
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002907 struct hci_chan *chan;
2908 struct sk_buff *skb;
2909 int quote;
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03002910 u8 type;
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002911
Andrei Emeltchenko63d2bc12012-02-03 16:27:55 +02002912 __check_timeout(hdev, cnt);
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002913
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03002914 BT_DBG("%s", hdev->name);
2915
2916 if (hdev->dev_type == HCI_AMP)
2917 type = AMP_LINK;
2918 else
2919 type = ACL_LINK;
2920
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002921 while (hdev->block_cnt > 0 &&
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03002922 (chan = hci_chan_sent(hdev, type, &quote))) {
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002923 u32 priority = (skb_peek(&chan->data_q))->priority;
2924 while (quote > 0 && (skb = skb_peek(&chan->data_q))) {
2925 int blocks;
2926
2927 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002928 skb->len, skb->priority);
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002929
2930 /* Stop if priority has changed */
2931 if (skb->priority < priority)
2932 break;
2933
2934 skb = skb_dequeue(&chan->data_q);
2935
2936 blocks = __get_blocks(hdev, skb);
2937 if (blocks > hdev->block_cnt)
2938 return;
2939
2940 hci_conn_enter_active_mode(chan->conn,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03002941 bt_cb(skb)->force_active);
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002942
2943 hci_send_frame(skb);
2944 hdev->acl_last_tx = jiffies;
2945
2946 hdev->block_cnt -= blocks;
2947 quote -= blocks;
2948
2949 chan->sent += blocks;
2950 chan->conn->sent += blocks;
2951 }
2952 }
2953
2954 if (cnt != hdev->block_cnt)
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03002955 hci_prio_recalculate(hdev, type);
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002956}
2957
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002958static void hci_sched_acl(struct hci_dev *hdev)
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002959{
2960 BT_DBG("%s", hdev->name);
2961
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03002962 /* No ACL link over BR/EDR controller */
2963 if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR)
2964 return;
2965
2966 /* No AMP link over AMP controller */
2967 if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP)
Andrei Emeltchenkob71d3852012-02-03 16:27:54 +02002968 return;
2969
2970 switch (hdev->flow_ctl_mode) {
2971 case HCI_FLOW_CTL_MODE_PACKET_BASED:
2972 hci_sched_acl_pkt(hdev);
2973 break;
2974
2975 case HCI_FLOW_CTL_MODE_BLOCK_BASED:
2976 hci_sched_acl_blk(hdev);
2977 break;
2978 }
2979}
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981/* Schedule SCO */
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002982static void hci_sched_sco(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
2984 struct hci_conn *conn;
2985 struct sk_buff *skb;
2986 int quote;
2987
2988 BT_DBG("%s", hdev->name);
2989
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03002990 if (!hci_conn_num(hdev, SCO_LINK))
2991 return;
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
2994 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2995 BT_DBG("skb %p len %d", skb, skb->len);
2996 hci_send_frame(skb);
2997
2998 conn->sent++;
2999 if (conn->sent == ~0)
3000 conn->sent = 0;
3001 }
3002 }
3003}
3004
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003005static void hci_sched_esco(struct hci_dev *hdev)
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003006{
3007 struct hci_conn *conn;
3008 struct sk_buff *skb;
3009 int quote;
3010
3011 BT_DBG("%s", hdev->name);
3012
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03003013 if (!hci_conn_num(hdev, ESCO_LINK))
3014 return;
3015
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003016 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK,
3017 &quote))) {
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003018 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
3019 BT_DBG("skb %p len %d", skb, skb->len);
3020 hci_send_frame(skb);
3021
3022 conn->sent++;
3023 if (conn->sent == ~0)
3024 conn->sent = 0;
3025 }
3026 }
3027}
3028
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003029static void hci_sched_le(struct hci_dev *hdev)
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003030{
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02003031 struct hci_chan *chan;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003032 struct sk_buff *skb;
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02003033 int quote, cnt, tmp;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003034
3035 BT_DBG("%s", hdev->name);
3036
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +03003037 if (!hci_conn_num(hdev, LE_LINK))
3038 return;
3039
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003040 if (!test_bit(HCI_RAW, &hdev->flags)) {
3041 /* LE tx timeout must be longer than maximum
3042 * link supervision timeout (40.9 seconds) */
Ville Tervobae1f5d92011-02-10 22:38:53 -03003043 if (!hdev->le_cnt && hdev->le_pkts &&
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03003044 time_after(jiffies, hdev->le_last_tx + HZ * 45))
Ville Tervobae1f5d92011-02-10 22:38:53 -03003045 hci_link_tx_to(hdev, LE_LINK);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003046 }
3047
3048 cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02003049 tmp = cnt;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02003050 while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, &quote))) {
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02003051 u32 priority = (skb_peek(&chan->data_q))->priority;
3052 while (quote-- && (skb = skb_peek(&chan->data_q))) {
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02003053 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03003054 skb->len, skb->priority);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003055
Luiz Augusto von Dentzec1cce22011-11-02 15:52:02 +02003056 /* Stop if priority has changed */
3057 if (skb->priority < priority)
3058 break;
3059
3060 skb = skb_dequeue(&chan->data_q);
3061
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003062 hci_send_frame(skb);
3063 hdev->le_last_tx = jiffies;
3064
3065 cnt--;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02003066 chan->sent++;
3067 chan->conn->sent++;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003068 }
3069 }
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02003070
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003071 if (hdev->le_pkts)
3072 hdev->le_cnt = cnt;
3073 else
3074 hdev->acl_cnt = cnt;
Luiz Augusto von Dentz02b20f02011-11-02 15:52:03 +02003075
3076 if (cnt != tmp)
3077 hci_prio_recalculate(hdev, LE_LINK);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003078}
3079
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003080static void hci_tx_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003082 struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct sk_buff *skb;
3084
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003085 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03003086 hdev->sco_cnt, hdev->le_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 /* Schedule queues and send stuff to HCI driver */
3089
3090 hci_sched_acl(hdev);
3091
3092 hci_sched_sco(hdev);
3093
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003094 hci_sched_esco(hdev);
3095
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003096 hci_sched_le(hdev);
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 /* Send next queued raw (unknown type) packet */
3099 while ((skb = skb_dequeue(&hdev->raw_q)))
3100 hci_send_frame(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003103/* ----- HCI RX task (incoming data processing) ----- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105/* ACL data packet */
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003106static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 struct hci_acl_hdr *hdr = (void *) skb->data;
3109 struct hci_conn *conn;
3110 __u16 handle, flags;
3111
3112 skb_pull(skb, HCI_ACL_HDR_SIZE);
3113
3114 handle = __le16_to_cpu(hdr->handle);
3115 flags = hci_flags(handle);
3116 handle = hci_handle(handle);
3117
Andrei Emeltchenkof0e09512012-06-11 11:13:09 +03003118 BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len,
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03003119 handle, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 hdev->stat.acl_rx++;
3122
3123 hci_dev_lock(hdev);
3124 conn = hci_conn_hash_lookup_handle(hdev, handle);
3125 hci_dev_unlock(hdev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (conn) {
Mat Martineau65983fc2011-12-13 15:06:02 -08003128 hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 /* Send to upper protocol */
Ulisses Furquim686ebf22011-12-21 10:11:33 -02003131 l2cap_recv_acldata(conn, skb, flags);
3132 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09003134 BT_ERR("%s ACL packet for unknown connection handle %d",
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03003135 hdev->name, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 }
3137
3138 kfree_skb(skb);
3139}
3140
3141/* SCO data packet */
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003142static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 struct hci_sco_hdr *hdr = (void *) skb->data;
3145 struct hci_conn *conn;
3146 __u16 handle;
3147
3148 skb_pull(skb, HCI_SCO_HDR_SIZE);
3149
3150 handle = __le16_to_cpu(hdr->handle);
3151
Andrei Emeltchenkof0e09512012-06-11 11:13:09 +03003152 BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 hdev->stat.sco_rx++;
3155
3156 hci_dev_lock(hdev);
3157 conn = hci_conn_hash_lookup_handle(hdev, handle);
3158 hci_dev_unlock(hdev);
3159
3160 if (conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 /* Send to upper protocol */
Ulisses Furquim686ebf22011-12-21 10:11:33 -02003162 sco_recv_scodata(conn, skb);
3163 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 } else {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09003165 BT_ERR("%s SCO packet for unknown connection handle %d",
Gustavo Padovana8c5fb12012-05-17 00:36:26 -03003166 hdev->name, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
3168
3169 kfree_skb(skb);
3170}
3171
Marcel Holtmannb78752c2010-08-08 23:06:53 -04003172static void hci_rx_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Marcel Holtmannb78752c2010-08-08 23:06:53 -04003174 struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 struct sk_buff *skb;
3176
3177 BT_DBG("%s", hdev->name);
3178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 while ((skb = skb_dequeue(&hdev->rx_q))) {
Marcel Holtmanncd82e612012-02-20 20:34:38 +01003180 /* Send copy to monitor */
3181 hci_send_to_monitor(hdev, skb);
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (atomic_read(&hdev->promisc)) {
3184 /* Send copy to the sockets */
Marcel Holtmann470fe1b2012-02-20 14:50:30 +01003185 hci_send_to_sock(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
3187
3188 if (test_bit(HCI_RAW, &hdev->flags)) {
3189 kfree_skb(skb);
3190 continue;
3191 }
3192
3193 if (test_bit(HCI_INIT, &hdev->flags)) {
3194 /* Don't process data packets in this states. */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07003195 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 case HCI_ACLDATA_PKT:
3197 case HCI_SCODATA_PKT:
3198 kfree_skb(skb);
3199 continue;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 }
3202
3203 /* Process frame */
Marcel Holtmann0d48d932005-08-09 20:30:28 -07003204 switch (bt_cb(skb)->pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 case HCI_EVENT_PKT:
Marcel Holtmannb78752c2010-08-08 23:06:53 -04003206 BT_DBG("%s Event packet", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 hci_event_packet(hdev, skb);
3208 break;
3209
3210 case HCI_ACLDATA_PKT:
3211 BT_DBG("%s ACL data packet", hdev->name);
3212 hci_acldata_packet(hdev, skb);
3213 break;
3214
3215 case HCI_SCODATA_PKT:
3216 BT_DBG("%s SCO data packet", hdev->name);
3217 hci_scodata_packet(hdev, skb);
3218 break;
3219
3220 default:
3221 kfree_skb(skb);
3222 break;
3223 }
3224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02003227static void hci_cmd_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02003229 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 struct sk_buff *skb;
3231
Andrei Emeltchenko21047862012-07-10 15:27:47 +03003232 BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name,
3233 atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 /* Send queued commands */
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02003236 if (atomic_read(&hdev->cmd_cnt)) {
3237 skb = skb_dequeue(&hdev->cmd_q);
3238 if (!skb)
3239 return;
3240
Wei Yongjun7585b972009-02-25 18:29:52 +08003241 kfree_skb(hdev->sent_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02003243 hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC);
3244 if (hdev->sent_cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 atomic_dec(&hdev->cmd_cnt);
3246 hci_send_frame(skb);
Szymon Janc7bdb8a52011-07-26 22:46:54 +02003247 if (test_bit(HCI_RESET, &hdev->flags))
3248 del_timer(&hdev->cmd_timer);
3249 else
3250 mod_timer(&hdev->cmd_timer,
Andrei Emeltchenko5f246e82012-06-11 11:13:07 +03003251 jiffies + HCI_CMD_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 } else {
3253 skb_queue_head(&hdev->cmd_q, skb);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02003254 queue_work(hdev->workqueue, &hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 }
3256 }
3257}
Andre Guedes2519a1f2011-11-07 11:45:24 -03003258
3259int hci_do_inquiry(struct hci_dev *hdev, u8 length)
3260{
3261 /* General inquiry access code (GIAC) */
3262 u8 lap[3] = { 0x33, 0x8b, 0x9e };
3263 struct hci_cp_inquiry cp;
3264
3265 BT_DBG("%s", hdev->name);
3266
3267 if (test_bit(HCI_INQUIRY, &hdev->flags))
3268 return -EINPROGRESS;
3269
Johan Hedberg46632622012-01-02 16:06:08 +02003270 inquiry_cache_flush(hdev);
3271
Andre Guedes2519a1f2011-11-07 11:45:24 -03003272 memset(&cp, 0, sizeof(cp));
3273 memcpy(&cp.lap, lap, sizeof(cp.lap));
3274 cp.length = length;
3275
3276 return hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
3277}
Andre Guedes023d50492011-11-04 14:16:52 -03003278
3279int hci_cancel_inquiry(struct hci_dev *hdev)
3280{
3281 BT_DBG("%s", hdev->name);
3282
3283 if (!test_bit(HCI_INQUIRY, &hdev->flags))
Andre Guedes7537e5c2012-03-20 00:13:38 -03003284 return -EALREADY;
Andre Guedes023d50492011-11-04 14:16:52 -03003285
3286 return hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL);
3287}
Andre Guedes31f79562012-04-24 21:02:53 -03003288
3289u8 bdaddr_to_le(u8 bdaddr_type)
3290{
3291 switch (bdaddr_type) {
3292 case BDADDR_LE_PUBLIC:
3293 return ADDR_LE_DEV_PUBLIC;
3294
3295 default:
3296 /* Fallback to LE Random address type */
3297 return ADDR_LE_DEV_RANDOM;
3298 }
3299}