YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
Gustavo F. Padovan | 590051d | 2011-12-18 13:39:33 -0200 | [diff] [blame] | 4 | Copyright (C) 2011 ProFUSION Embedded Systems |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 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 Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 16 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 20 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 21 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 22 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | SOFTWARE IS DISCLAIMED. |
| 24 | */ |
| 25 | |
| 26 | /* Bluetooth HCI core. */ |
| 27 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 28 | #include <linux/export.h> |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 29 | #include <linux/idr.h> |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 30 | #include <linux/rfkill.h> |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 31 | #include <linux/debugfs.h> |
Marcel Holtmann | 4721983 | 2013-10-17 17:24:15 -0700 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include <net/bluetooth/bluetooth.h> |
| 35 | #include <net/bluetooth/hci_core.h> |
| 36 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 37 | static void hci_rx_work(struct work_struct *work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 38 | static void hci_cmd_work(struct work_struct *work); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 39 | static void hci_tx_work(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* HCI device list */ |
| 42 | LIST_HEAD(hci_dev_list); |
| 43 | DEFINE_RWLOCK(hci_dev_list_lock); |
| 44 | |
| 45 | /* HCI callback list */ |
| 46 | LIST_HEAD(hci_cb_list); |
| 47 | DEFINE_RWLOCK(hci_cb_list_lock); |
| 48 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 49 | /* HCI ID Numbering */ |
| 50 | static DEFINE_IDA(hci_index_ida); |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* ---- HCI notifications ---- */ |
| 53 | |
Marcel Holtmann | 6516455 | 2005-10-28 19:20:48 +0200 | [diff] [blame] | 54 | static void hci_notify(struct hci_dev *hdev, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 56 | hci_sock_dev_event(hdev, event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 59 | /* ---- HCI debugfs entries ---- */ |
| 60 | |
Marcel Holtmann | 70afe0b | 2013-10-17 17:24:14 -0700 | [diff] [blame] | 61 | static int blacklist_show(struct seq_file *f, void *p) |
| 62 | { |
| 63 | struct hci_dev *hdev = f->private; |
| 64 | struct bdaddr_list *b; |
| 65 | |
| 66 | hci_dev_lock(hdev); |
| 67 | list_for_each_entry(b, &hdev->blacklist, list) |
| 68 | seq_printf(f, "%pMR\n", &b->bdaddr); |
| 69 | hci_dev_unlock(hdev); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int blacklist_open(struct inode *inode, struct file *file) |
| 75 | { |
| 76 | return single_open(file, blacklist_show, inode->i_private); |
| 77 | } |
| 78 | |
| 79 | static const struct file_operations blacklist_fops = { |
| 80 | .open = blacklist_open, |
| 81 | .read = seq_read, |
| 82 | .llseek = seq_lseek, |
| 83 | .release = single_release, |
| 84 | }; |
| 85 | |
Marcel Holtmann | 4721983 | 2013-10-17 17:24:15 -0700 | [diff] [blame] | 86 | static int uuids_show(struct seq_file *f, void *p) |
| 87 | { |
| 88 | struct hci_dev *hdev = f->private; |
| 89 | struct bt_uuid *uuid; |
| 90 | |
| 91 | hci_dev_lock(hdev); |
| 92 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 93 | u32 data0, data5; |
| 94 | u16 data1, data2, data3, data4; |
| 95 | |
| 96 | data5 = get_unaligned_le32(uuid); |
| 97 | data4 = get_unaligned_le16(uuid + 4); |
| 98 | data3 = get_unaligned_le16(uuid + 6); |
| 99 | data2 = get_unaligned_le16(uuid + 8); |
| 100 | data1 = get_unaligned_le16(uuid + 10); |
| 101 | data0 = get_unaligned_le32(uuid + 12); |
| 102 | |
| 103 | seq_printf(f, "%.8x-%.4x-%.4x-%.4x-%.4x%.8x\n", |
| 104 | data0, data1, data2, data3, data4, data5); |
| 105 | } |
| 106 | hci_dev_unlock(hdev); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int uuids_open(struct inode *inode, struct file *file) |
| 112 | { |
| 113 | return single_open(file, uuids_show, inode->i_private); |
| 114 | } |
| 115 | |
| 116 | static const struct file_operations uuids_fops = { |
| 117 | .open = uuids_open, |
| 118 | .read = seq_read, |
| 119 | .llseek = seq_lseek, |
| 120 | .release = single_release, |
| 121 | }; |
| 122 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 123 | static int inquiry_cache_show(struct seq_file *f, void *p) |
| 124 | { |
| 125 | struct hci_dev *hdev = f->private; |
| 126 | struct discovery_state *cache = &hdev->discovery; |
| 127 | struct inquiry_entry *e; |
| 128 | |
| 129 | hci_dev_lock(hdev); |
| 130 | |
| 131 | list_for_each_entry(e, &cache->all, all) { |
| 132 | struct inquiry_data *data = &e->data; |
| 133 | seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n", |
| 134 | &data->bdaddr, |
| 135 | data->pscan_rep_mode, data->pscan_period_mode, |
| 136 | data->pscan_mode, data->dev_class[2], |
| 137 | data->dev_class[1], data->dev_class[0], |
| 138 | __le16_to_cpu(data->clock_offset), |
| 139 | data->rssi, data->ssp_mode, e->timestamp); |
| 140 | } |
| 141 | |
| 142 | hci_dev_unlock(hdev); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int inquiry_cache_open(struct inode *inode, struct file *file) |
| 148 | { |
| 149 | return single_open(file, inquiry_cache_show, inode->i_private); |
| 150 | } |
| 151 | |
| 152 | static const struct file_operations inquiry_cache_fops = { |
| 153 | .open = inquiry_cache_open, |
| 154 | .read = seq_read, |
| 155 | .llseek = seq_lseek, |
| 156 | .release = single_release, |
| 157 | }; |
| 158 | |
Marcel Holtmann | 041000b | 2013-10-17 12:02:31 -0700 | [diff] [blame] | 159 | static int voice_setting_get(void *data, u64 *val) |
| 160 | { |
| 161 | struct hci_dev *hdev = data; |
| 162 | |
| 163 | hci_dev_lock(hdev); |
| 164 | *val = hdev->voice_setting; |
| 165 | hci_dev_unlock(hdev); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | DEFINE_SIMPLE_ATTRIBUTE(voice_setting_fops, voice_setting_get, |
| 171 | NULL, "0x%4.4llx\n"); |
| 172 | |
Marcel Holtmann | ebd1e33 | 2013-10-17 10:54:46 -0700 | [diff] [blame] | 173 | static int auto_accept_delay_set(void *data, u64 val) |
| 174 | { |
| 175 | struct hci_dev *hdev = data; |
| 176 | |
| 177 | hci_dev_lock(hdev); |
| 178 | hdev->auto_accept_delay = val; |
| 179 | hci_dev_unlock(hdev); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int auto_accept_delay_get(void *data, u64 *val) |
| 185 | { |
| 186 | struct hci_dev *hdev = data; |
| 187 | |
| 188 | hci_dev_lock(hdev); |
| 189 | *val = hdev->auto_accept_delay; |
| 190 | hci_dev_unlock(hdev); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get, |
| 196 | auto_accept_delay_set, "%llu\n"); |
| 197 | |
Marcel Holtmann | e7b8fc9 | 2013-10-17 11:45:09 -0700 | [diff] [blame] | 198 | static int static_address_show(struct seq_file *f, void *p) |
| 199 | { |
| 200 | struct hci_dev *hdev = f->private; |
| 201 | |
| 202 | hci_dev_lock(hdev); |
| 203 | seq_printf(f, "%pMR\n", &hdev->static_addr); |
| 204 | hci_dev_unlock(hdev); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int static_address_open(struct inode *inode, struct file *file) |
| 210 | { |
| 211 | return single_open(file, static_address_show, inode->i_private); |
| 212 | } |
| 213 | |
| 214 | static const struct file_operations static_address_fops = { |
| 215 | .open = static_address_open, |
| 216 | .read = seq_read, |
| 217 | .llseek = seq_lseek, |
| 218 | .release = single_release, |
| 219 | }; |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /* ---- HCI requests ---- */ |
| 222 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 223 | static void hci_req_sync_complete(struct hci_dev *hdev, u8 result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 225 | BT_DBG("%s result 0x%2.2x", hdev->name, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | if (hdev->req_status == HCI_REQ_PEND) { |
| 228 | hdev->req_result = result; |
| 229 | hdev->req_status = HCI_REQ_DONE; |
| 230 | wake_up_interruptible(&hdev->req_wait_q); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | static void hci_req_cancel(struct hci_dev *hdev, int err) |
| 235 | { |
| 236 | BT_DBG("%s err 0x%2.2x", hdev->name, err); |
| 237 | |
| 238 | if (hdev->req_status == HCI_REQ_PEND) { |
| 239 | hdev->req_result = err; |
| 240 | hdev->req_status = HCI_REQ_CANCELED; |
| 241 | wake_up_interruptible(&hdev->req_wait_q); |
| 242 | } |
| 243 | } |
| 244 | |
Fengguang Wu | 77a63e0 | 2013-04-20 16:24:31 +0300 | [diff] [blame] | 245 | static struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode, |
| 246 | u8 event) |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 247 | { |
| 248 | struct hci_ev_cmd_complete *ev; |
| 249 | struct hci_event_hdr *hdr; |
| 250 | struct sk_buff *skb; |
| 251 | |
| 252 | hci_dev_lock(hdev); |
| 253 | |
| 254 | skb = hdev->recv_evt; |
| 255 | hdev->recv_evt = NULL; |
| 256 | |
| 257 | hci_dev_unlock(hdev); |
| 258 | |
| 259 | if (!skb) |
| 260 | return ERR_PTR(-ENODATA); |
| 261 | |
| 262 | if (skb->len < sizeof(*hdr)) { |
| 263 | BT_ERR("Too short HCI event"); |
| 264 | goto failed; |
| 265 | } |
| 266 | |
| 267 | hdr = (void *) skb->data; |
| 268 | skb_pull(skb, HCI_EVENT_HDR_SIZE); |
| 269 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 270 | if (event) { |
| 271 | if (hdr->evt != event) |
| 272 | goto failed; |
| 273 | return skb; |
| 274 | } |
| 275 | |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 276 | if (hdr->evt != HCI_EV_CMD_COMPLETE) { |
| 277 | BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt); |
| 278 | goto failed; |
| 279 | } |
| 280 | |
| 281 | if (skb->len < sizeof(*ev)) { |
| 282 | BT_ERR("Too short cmd_complete event"); |
| 283 | goto failed; |
| 284 | } |
| 285 | |
| 286 | ev = (void *) skb->data; |
| 287 | skb_pull(skb, sizeof(*ev)); |
| 288 | |
| 289 | if (opcode == __le16_to_cpu(ev->opcode)) |
| 290 | return skb; |
| 291 | |
| 292 | BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode, |
| 293 | __le16_to_cpu(ev->opcode)); |
| 294 | |
| 295 | failed: |
| 296 | kfree_skb(skb); |
| 297 | return ERR_PTR(-ENODATA); |
| 298 | } |
| 299 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 300 | struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 301 | const void *param, u8 event, u32 timeout) |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 302 | { |
| 303 | DECLARE_WAITQUEUE(wait, current); |
| 304 | struct hci_request req; |
| 305 | int err = 0; |
| 306 | |
| 307 | BT_DBG("%s", hdev->name); |
| 308 | |
| 309 | hci_req_init(&req, hdev); |
| 310 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 311 | hci_req_add_ev(&req, opcode, plen, param, event); |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 312 | |
| 313 | hdev->req_status = HCI_REQ_PEND; |
| 314 | |
| 315 | err = hci_req_run(&req, hci_req_sync_complete); |
| 316 | if (err < 0) |
| 317 | return ERR_PTR(err); |
| 318 | |
| 319 | add_wait_queue(&hdev->req_wait_q, &wait); |
| 320 | set_current_state(TASK_INTERRUPTIBLE); |
| 321 | |
| 322 | schedule_timeout(timeout); |
| 323 | |
| 324 | remove_wait_queue(&hdev->req_wait_q, &wait); |
| 325 | |
| 326 | if (signal_pending(current)) |
| 327 | return ERR_PTR(-EINTR); |
| 328 | |
| 329 | switch (hdev->req_status) { |
| 330 | case HCI_REQ_DONE: |
| 331 | err = -bt_to_errno(hdev->req_result); |
| 332 | break; |
| 333 | |
| 334 | case HCI_REQ_CANCELED: |
| 335 | err = -hdev->req_result; |
| 336 | break; |
| 337 | |
| 338 | default: |
| 339 | err = -ETIMEDOUT; |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | hdev->req_status = hdev->req_result = 0; |
| 344 | |
| 345 | BT_DBG("%s end: err %d", hdev->name, err); |
| 346 | |
| 347 | if (err < 0) |
| 348 | return ERR_PTR(err); |
| 349 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 350 | return hci_get_cmd_complete(hdev, opcode, event); |
| 351 | } |
| 352 | EXPORT_SYMBOL(__hci_cmd_sync_ev); |
| 353 | |
| 354 | struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 355 | const void *param, u32 timeout) |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 356 | { |
| 357 | return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout); |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 358 | } |
| 359 | EXPORT_SYMBOL(__hci_cmd_sync); |
| 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | /* Execute request and wait for completion. */ |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 362 | static int __hci_req_sync(struct hci_dev *hdev, |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 363 | void (*func)(struct hci_request *req, |
| 364 | unsigned long opt), |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 365 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 367 | struct hci_request req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | DECLARE_WAITQUEUE(wait, current); |
| 369 | int err = 0; |
| 370 | |
| 371 | BT_DBG("%s start", hdev->name); |
| 372 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 373 | hci_req_init(&req, hdev); |
| 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | hdev->req_status = HCI_REQ_PEND; |
| 376 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 377 | func(&req, opt); |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 378 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 379 | err = hci_req_run(&req, hci_req_sync_complete); |
| 380 | if (err < 0) { |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 381 | hdev->req_status = 0; |
Andre Guedes | 920c830 | 2013-03-08 11:20:15 -0300 | [diff] [blame] | 382 | |
| 383 | /* ENODATA means the HCI request command queue is empty. |
| 384 | * This can happen when a request with conditionals doesn't |
| 385 | * trigger any commands to be sent. This is normal behavior |
| 386 | * and should not trigger an error return. |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 387 | */ |
Andre Guedes | 920c830 | 2013-03-08 11:20:15 -0300 | [diff] [blame] | 388 | if (err == -ENODATA) |
| 389 | return 0; |
| 390 | |
| 391 | return err; |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Andre Guedes | bc4445c | 2013-03-08 11:20:13 -0300 | [diff] [blame] | 394 | add_wait_queue(&hdev->req_wait_q, &wait); |
| 395 | set_current_state(TASK_INTERRUPTIBLE); |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | schedule_timeout(timeout); |
| 398 | |
| 399 | remove_wait_queue(&hdev->req_wait_q, &wait); |
| 400 | |
| 401 | if (signal_pending(current)) |
| 402 | return -EINTR; |
| 403 | |
| 404 | switch (hdev->req_status) { |
| 405 | case HCI_REQ_DONE: |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 406 | err = -bt_to_errno(hdev->req_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | break; |
| 408 | |
| 409 | case HCI_REQ_CANCELED: |
| 410 | err = -hdev->req_result; |
| 411 | break; |
| 412 | |
| 413 | default: |
| 414 | err = -ETIMEDOUT; |
| 415 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 418 | hdev->req_status = hdev->req_result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | BT_DBG("%s end: err %d", hdev->name, err); |
| 421 | |
| 422 | return err; |
| 423 | } |
| 424 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 425 | static int hci_req_sync(struct hci_dev *hdev, |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 426 | void (*req)(struct hci_request *req, |
| 427 | unsigned long opt), |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 428 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
| 430 | int ret; |
| 431 | |
Marcel Holtmann | 7c6a329 | 2008-09-12 03:11:54 +0200 | [diff] [blame] | 432 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 433 | return -ENETDOWN; |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | /* Serialize all requests */ |
| 436 | hci_req_lock(hdev); |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 437 | ret = __hci_req_sync(hdev, req, opt, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | hci_req_unlock(hdev); |
| 439 | |
| 440 | return ret; |
| 441 | } |
| 442 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 443 | static void hci_reset_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 445 | BT_DBG("%s %ld", req->hdev->name, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | /* Reset device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 448 | set_bit(HCI_RESET, &req->hdev->flags); |
| 449 | hci_req_add(req, HCI_OP_RESET, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 452 | static void bredr_init(struct hci_request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 454 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | /* Read Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 457 | hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Marcel Holtmann | 1143e5a | 2006-09-23 09:57:20 +0200 | [diff] [blame] | 459 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 460 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 461 | |
| 462 | /* Read BD Address */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 463 | hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 466 | static void amp_init(struct hci_request *req) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 467 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 468 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 469 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 470 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 471 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Andrei Emeltchenko | 6bcbc48 | 2012-03-28 16:31:24 +0300 | [diff] [blame] | 472 | |
Marcel Holtmann | f6996cf | 2013-10-07 02:31:39 -0700 | [diff] [blame] | 473 | /* Read Local Supported Commands */ |
| 474 | hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); |
| 475 | |
| 476 | /* Read Local Supported Features */ |
| 477 | hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
| 478 | |
Andrei Emeltchenko | 6bcbc48 | 2012-03-28 16:31:24 +0300 | [diff] [blame] | 479 | /* Read Local AMP Info */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 480 | hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); |
Andrei Emeltchenko | e71dfab | 2012-09-06 15:05:46 +0300 | [diff] [blame] | 481 | |
| 482 | /* Read Data Blk size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 483 | hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); |
Marcel Holtmann | 7528ca1 | 2013-10-07 03:55:52 -0700 | [diff] [blame] | 484 | |
Marcel Holtmann | f38ba94 | 2013-10-07 03:55:53 -0700 | [diff] [blame] | 485 | /* Read Flow Control Mode */ |
| 486 | hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL); |
| 487 | |
Marcel Holtmann | 7528ca1 | 2013-10-07 03:55:52 -0700 | [diff] [blame] | 488 | /* Read Location Data */ |
| 489 | hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 492 | static void hci_init1_req(struct hci_request *req, unsigned long opt) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 493 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 494 | struct hci_dev *hdev = req->hdev; |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 495 | |
| 496 | BT_DBG("%s %ld", hdev->name, opt); |
| 497 | |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 498 | /* Reset */ |
| 499 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 500 | hci_reset_req(req, 0); |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 501 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 502 | switch (hdev->dev_type) { |
| 503 | case HCI_BREDR: |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 504 | bredr_init(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 505 | break; |
| 506 | |
| 507 | case HCI_AMP: |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 508 | amp_init(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 509 | break; |
| 510 | |
| 511 | default: |
| 512 | BT_ERR("Unknown device type %d", hdev->dev_type); |
| 513 | break; |
| 514 | } |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 517 | static void bredr_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 518 | { |
Marcel Holtmann | 4ca048e | 2013-10-11 16:42:07 -0700 | [diff] [blame] | 519 | struct hci_dev *hdev = req->hdev; |
| 520 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 521 | __le16 param; |
| 522 | __u8 flt_type; |
| 523 | |
| 524 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 525 | hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 526 | |
| 527 | /* Read Class of Device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 528 | hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 529 | |
| 530 | /* Read Local Name */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 531 | hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 532 | |
| 533 | /* Read Voice Setting */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 534 | hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 535 | |
Marcel Holtmann | b4cb9fb | 2013-10-14 13:56:16 -0700 | [diff] [blame] | 536 | /* Read Number of Supported IAC */ |
| 537 | hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL); |
| 538 | |
Marcel Holtmann | 4b836f3 | 2013-10-14 14:06:36 -0700 | [diff] [blame] | 539 | /* Read Current IAC LAP */ |
| 540 | hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL); |
| 541 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 542 | /* Clear Event Filters */ |
| 543 | flt_type = HCI_FLT_CLEAR_ALL; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 544 | hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 545 | |
| 546 | /* Connection accept timeout ~20 secs */ |
| 547 | param = __constant_cpu_to_le16(0x7d00); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 548 | hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 549 | |
Marcel Holtmann | 4ca048e | 2013-10-11 16:42:07 -0700 | [diff] [blame] | 550 | /* AVM Berlin (31), aka "BlueFRITZ!", reports version 1.2, |
| 551 | * but it does not support page scan related HCI commands. |
| 552 | */ |
| 553 | if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) { |
Johan Hedberg | f332ec6 | 2013-03-15 17:07:11 -0500 | [diff] [blame] | 554 | hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL); |
| 555 | hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL); |
| 556 | } |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 559 | static void le_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 560 | { |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 561 | struct hci_dev *hdev = req->hdev; |
| 562 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 563 | /* Read LE Buffer Size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 564 | hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 565 | |
| 566 | /* Read LE Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 567 | hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 568 | |
| 569 | /* Read LE Advertising Channel TX Power */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 570 | hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 571 | |
| 572 | /* Read LE White List Size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 573 | hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 574 | |
| 575 | /* Read LE Supported States */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 576 | hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL); |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 577 | |
| 578 | /* LE-only controllers have LE implicitly enabled */ |
| 579 | if (!lmp_bredr_capable(hdev)) |
| 580 | set_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static u8 hci_get_inquiry_mode(struct hci_dev *hdev) |
| 584 | { |
| 585 | if (lmp_ext_inq_capable(hdev)) |
| 586 | return 0x02; |
| 587 | |
| 588 | if (lmp_inq_rssi_capable(hdev)) |
| 589 | return 0x01; |
| 590 | |
| 591 | if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 && |
| 592 | hdev->lmp_subver == 0x0757) |
| 593 | return 0x01; |
| 594 | |
| 595 | if (hdev->manufacturer == 15) { |
| 596 | if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963) |
| 597 | return 0x01; |
| 598 | if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963) |
| 599 | return 0x01; |
| 600 | if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965) |
| 601 | return 0x01; |
| 602 | } |
| 603 | |
| 604 | if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 && |
| 605 | hdev->lmp_subver == 0x1805) |
| 606 | return 0x01; |
| 607 | |
| 608 | return 0x00; |
| 609 | } |
| 610 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 611 | static void hci_setup_inquiry_mode(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 612 | { |
| 613 | u8 mode; |
| 614 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 615 | mode = hci_get_inquiry_mode(req->hdev); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 616 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 617 | hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 620 | static void hci_setup_event_mask(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 621 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 622 | struct hci_dev *hdev = req->hdev; |
| 623 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 624 | /* The second byte is 0xff instead of 0x9f (two reserved bits |
| 625 | * disabled) since a Broadcom 1.2 dongle doesn't respond to the |
| 626 | * command otherwise. |
| 627 | */ |
| 628 | u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; |
| 629 | |
| 630 | /* CSR 1.1 dongles does not accept any bitfield so don't try to set |
| 631 | * any event mask for pre 1.2 devices. |
| 632 | */ |
| 633 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 634 | return; |
| 635 | |
| 636 | if (lmp_bredr_capable(hdev)) { |
| 637 | events[4] |= 0x01; /* Flow Specification Complete */ |
| 638 | events[4] |= 0x02; /* Inquiry Result with RSSI */ |
| 639 | events[4] |= 0x04; /* Read Remote Extended Features Complete */ |
| 640 | events[5] |= 0x08; /* Synchronous Connection Complete */ |
| 641 | events[5] |= 0x10; /* Synchronous Connection Changed */ |
Marcel Holtmann | c7882cb | 2013-08-13 10:00:54 -0700 | [diff] [blame] | 642 | } else { |
| 643 | /* Use a different default for LE-only devices */ |
| 644 | memset(events, 0, sizeof(events)); |
| 645 | events[0] |= 0x10; /* Disconnection Complete */ |
| 646 | events[0] |= 0x80; /* Encryption Change */ |
| 647 | events[1] |= 0x08; /* Read Remote Version Information Complete */ |
| 648 | events[1] |= 0x20; /* Command Complete */ |
| 649 | events[1] |= 0x40; /* Command Status */ |
| 650 | events[1] |= 0x80; /* Hardware Error */ |
| 651 | events[2] |= 0x04; /* Number of Completed Packets */ |
| 652 | events[3] |= 0x02; /* Data Buffer Overflow */ |
| 653 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | if (lmp_inq_rssi_capable(hdev)) |
| 657 | events[4] |= 0x02; /* Inquiry Result with RSSI */ |
| 658 | |
| 659 | if (lmp_sniffsubr_capable(hdev)) |
| 660 | events[5] |= 0x20; /* Sniff Subrating */ |
| 661 | |
| 662 | if (lmp_pause_enc_capable(hdev)) |
| 663 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ |
| 664 | |
| 665 | if (lmp_ext_inq_capable(hdev)) |
| 666 | events[5] |= 0x40; /* Extended Inquiry Result */ |
| 667 | |
| 668 | if (lmp_no_flush_capable(hdev)) |
| 669 | events[7] |= 0x01; /* Enhanced Flush Complete */ |
| 670 | |
| 671 | if (lmp_lsto_capable(hdev)) |
| 672 | events[6] |= 0x80; /* Link Supervision Timeout Changed */ |
| 673 | |
| 674 | if (lmp_ssp_capable(hdev)) { |
| 675 | events[6] |= 0x01; /* IO Capability Request */ |
| 676 | events[6] |= 0x02; /* IO Capability Response */ |
| 677 | events[6] |= 0x04; /* User Confirmation Request */ |
| 678 | events[6] |= 0x08; /* User Passkey Request */ |
| 679 | events[6] |= 0x10; /* Remote OOB Data Request */ |
| 680 | events[6] |= 0x20; /* Simple Pairing Complete */ |
| 681 | events[7] |= 0x04; /* User Passkey Notification */ |
| 682 | events[7] |= 0x08; /* Keypress Notification */ |
| 683 | events[7] |= 0x10; /* Remote Host Supported |
| 684 | * Features Notification |
| 685 | */ |
| 686 | } |
| 687 | |
| 688 | if (lmp_le_capable(hdev)) |
| 689 | events[7] |= 0x20; /* LE Meta-Event */ |
| 690 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 691 | hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 692 | |
| 693 | if (lmp_le_capable(hdev)) { |
| 694 | memset(events, 0, sizeof(events)); |
| 695 | events[0] = 0x1f; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 696 | hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, |
| 697 | sizeof(events), events); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 701 | static void hci_init2_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 702 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 703 | struct hci_dev *hdev = req->hdev; |
| 704 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 705 | if (lmp_bredr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 706 | bredr_setup(req); |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 707 | else |
| 708 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 709 | |
| 710 | if (lmp_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 711 | le_setup(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 712 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 713 | hci_setup_event_mask(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 714 | |
Johan Hedberg | 3f8e2d7 | 2013-07-24 02:32:46 +0300 | [diff] [blame] | 715 | /* AVM Berlin (31), aka "BlueFRITZ!", doesn't support the read |
| 716 | * local supported commands HCI command. |
| 717 | */ |
| 718 | if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 719 | hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 720 | |
| 721 | if (lmp_ssp_capable(hdev)) { |
| 722 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 723 | u8 mode = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 724 | hci_req_add(req, HCI_OP_WRITE_SSP_MODE, |
| 725 | sizeof(mode), &mode); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 726 | } else { |
| 727 | struct hci_cp_write_eir cp; |
| 728 | |
| 729 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 730 | memset(&cp, 0, sizeof(cp)); |
| 731 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 732 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | if (lmp_inq_rssi_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 737 | hci_setup_inquiry_mode(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 738 | |
| 739 | if (lmp_inq_tx_pwr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 740 | hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 741 | |
| 742 | if (lmp_ext_feat_capable(hdev)) { |
| 743 | struct hci_cp_read_local_ext_features cp; |
| 744 | |
| 745 | cp.page = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 746 | hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, |
| 747 | sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) { |
| 751 | u8 enable = 1; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 752 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable), |
| 753 | &enable); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 757 | static void hci_setup_link_policy(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 758 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 759 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 760 | struct hci_cp_write_def_link_policy cp; |
| 761 | u16 link_policy = 0; |
| 762 | |
| 763 | if (lmp_rswitch_capable(hdev)) |
| 764 | link_policy |= HCI_LP_RSWITCH; |
| 765 | if (lmp_hold_capable(hdev)) |
| 766 | link_policy |= HCI_LP_HOLD; |
| 767 | if (lmp_sniff_capable(hdev)) |
| 768 | link_policy |= HCI_LP_SNIFF; |
| 769 | if (lmp_park_capable(hdev)) |
| 770 | link_policy |= HCI_LP_PARK; |
| 771 | |
| 772 | cp.policy = cpu_to_le16(link_policy); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 773 | hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 774 | } |
| 775 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 776 | static void hci_set_le_support(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 777 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 778 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 779 | struct hci_cp_write_le_host_supported cp; |
| 780 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 781 | /* LE-only devices do not support explicit enablement */ |
| 782 | if (!lmp_bredr_capable(hdev)) |
| 783 | return; |
| 784 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 785 | memset(&cp, 0, sizeof(cp)); |
| 786 | |
| 787 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 788 | cp.le = 0x01; |
| 789 | cp.simul = lmp_le_br_capable(hdev); |
| 790 | } |
| 791 | |
| 792 | if (cp.le != lmp_host_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 793 | hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp), |
| 794 | &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 795 | } |
| 796 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 797 | static void hci_set_event_mask_page_2(struct hci_request *req) |
| 798 | { |
| 799 | struct hci_dev *hdev = req->hdev; |
| 800 | u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 801 | |
| 802 | /* If Connectionless Slave Broadcast master role is supported |
| 803 | * enable all necessary events for it. |
| 804 | */ |
| 805 | if (hdev->features[2][0] & 0x01) { |
| 806 | events[1] |= 0x40; /* Triggered Clock Capture */ |
| 807 | events[1] |= 0x80; /* Synchronization Train Complete */ |
| 808 | events[2] |= 0x10; /* Slave Page Response Timeout */ |
| 809 | events[2] |= 0x20; /* CSB Channel Map Change */ |
| 810 | } |
| 811 | |
| 812 | /* If Connectionless Slave Broadcast slave role is supported |
| 813 | * enable all necessary events for it. |
| 814 | */ |
| 815 | if (hdev->features[2][0] & 0x02) { |
| 816 | events[2] |= 0x01; /* Synchronization Train Received */ |
| 817 | events[2] |= 0x02; /* CSB Receive */ |
| 818 | events[2] |= 0x04; /* CSB Timeout */ |
| 819 | events[2] |= 0x08; /* Truncated Page Complete */ |
| 820 | } |
| 821 | |
| 822 | hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events); |
| 823 | } |
| 824 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 825 | static void hci_init3_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 826 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 827 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | d2c5d77 | 2013-04-17 15:00:52 +0300 | [diff] [blame] | 828 | u8 p; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 829 | |
Gustavo Padovan | b8f4e06 | 2013-06-13 12:34:31 +0100 | [diff] [blame] | 830 | /* Some Broadcom based Bluetooth controllers do not support the |
| 831 | * Delete Stored Link Key command. They are clearly indicating its |
| 832 | * absence in the bit mask of supported commands. |
| 833 | * |
| 834 | * Check the supported commands and only if the the command is marked |
| 835 | * as supported send it. If not supported assume that the controller |
| 836 | * does not have actual support for stored link keys which makes this |
| 837 | * command redundant anyway. |
Marcel Holtmann | 637b4ca | 2013-07-01 14:14:46 -0700 | [diff] [blame] | 838 | */ |
Johan Hedberg | 59f45d5 | 2013-06-13 11:01:13 +0300 | [diff] [blame] | 839 | if (hdev->commands[6] & 0x80) { |
| 840 | struct hci_cp_delete_stored_link_key cp; |
| 841 | |
| 842 | bacpy(&cp.bdaddr, BDADDR_ANY); |
| 843 | cp.delete_all = 0x01; |
| 844 | hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY, |
| 845 | sizeof(cp), &cp); |
| 846 | } |
| 847 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 848 | if (hdev->commands[5] & 0x10) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 849 | hci_setup_link_policy(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 850 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 851 | if (lmp_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 852 | hci_set_le_support(req); |
Johan Hedberg | d2c5d77 | 2013-04-17 15:00:52 +0300 | [diff] [blame] | 853 | |
| 854 | /* Read features beyond page 1 if available */ |
| 855 | for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) { |
| 856 | struct hci_cp_read_local_ext_features cp; |
| 857 | |
| 858 | cp.page = p; |
| 859 | hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, |
| 860 | sizeof(cp), &cp); |
| 861 | } |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 862 | } |
| 863 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 864 | static void hci_init4_req(struct hci_request *req, unsigned long opt) |
| 865 | { |
| 866 | struct hci_dev *hdev = req->hdev; |
| 867 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 868 | /* Set event mask page 2 if the HCI command for it is supported */ |
| 869 | if (hdev->commands[22] & 0x04) |
| 870 | hci_set_event_mask_page_2(req); |
| 871 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 872 | /* Check for Synchronization Train support */ |
| 873 | if (hdev->features[2][0] & 0x04) |
| 874 | hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL); |
| 875 | } |
| 876 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 877 | static int __hci_init(struct hci_dev *hdev) |
| 878 | { |
| 879 | int err; |
| 880 | |
| 881 | err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT); |
| 882 | if (err < 0) |
| 883 | return err; |
| 884 | |
| 885 | /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode |
| 886 | * BR/EDR/LE type controllers. AMP controllers only need the |
| 887 | * first stage init. |
| 888 | */ |
| 889 | if (hdev->dev_type != HCI_BREDR) |
| 890 | return 0; |
| 891 | |
| 892 | err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT); |
| 893 | if (err < 0) |
| 894 | return err; |
| 895 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 896 | err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT); |
| 897 | if (err < 0) |
| 898 | return err; |
| 899 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 900 | err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT); |
| 901 | if (err < 0) |
| 902 | return err; |
| 903 | |
| 904 | /* Only create debugfs entries during the initial setup |
| 905 | * phase and not every time the controller gets powered on. |
| 906 | */ |
| 907 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) |
| 908 | return 0; |
| 909 | |
Marcel Holtmann | 70afe0b | 2013-10-17 17:24:14 -0700 | [diff] [blame] | 910 | debugfs_create_file("blacklist", 0444, hdev->debugfs, hdev, |
| 911 | &blacklist_fops); |
| 912 | |
Marcel Holtmann | 4721983 | 2013-10-17 17:24:15 -0700 | [diff] [blame] | 913 | debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops); |
| 914 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 915 | if (lmp_bredr_capable(hdev)) { |
| 916 | debugfs_create_file("inquiry_cache", 0444, hdev->debugfs, |
| 917 | hdev, &inquiry_cache_fops); |
Marcel Holtmann | 041000b | 2013-10-17 12:02:31 -0700 | [diff] [blame] | 918 | debugfs_create_file("voice_setting", 0444, hdev->debugfs, |
| 919 | hdev, &voice_setting_fops); |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Marcel Holtmann | ebd1e33 | 2013-10-17 10:54:46 -0700 | [diff] [blame] | 922 | if (lmp_ssp_capable(hdev)) |
| 923 | debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs, |
| 924 | hdev, &auto_accept_delay_fops); |
| 925 | |
Marcel Holtmann | e7b8fc9 | 2013-10-17 11:45:09 -0700 | [diff] [blame] | 926 | if (lmp_le_capable(hdev)) |
| 927 | debugfs_create_file("static_address", 0444, hdev->debugfs, |
| 928 | hdev, &static_address_fops); |
| 929 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 930 | return 0; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 933 | static void hci_scan_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | { |
| 935 | __u8 scan = opt; |
| 936 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 937 | BT_DBG("%s %x", req->hdev->name, scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
| 939 | /* Inquiry and Page scans */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 940 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 943 | static void hci_auth_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | { |
| 945 | __u8 auth = opt; |
| 946 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 947 | BT_DBG("%s %x", req->hdev->name, auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
| 949 | /* Authentication */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 950 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 953 | static void hci_encrypt_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | { |
| 955 | __u8 encrypt = opt; |
| 956 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 957 | BT_DBG("%s %x", req->hdev->name, encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 959 | /* Encryption */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 960 | hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 963 | static void hci_linkpol_req(struct hci_request *req, unsigned long opt) |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 964 | { |
| 965 | __le16 policy = cpu_to_le16(opt); |
| 966 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 967 | BT_DBG("%s %x", req->hdev->name, policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 968 | |
| 969 | /* Default link policy */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 970 | hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 971 | } |
| 972 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 973 | /* Get HCI device by index. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | * Device is held on return. */ |
| 975 | struct hci_dev *hci_dev_get(int index) |
| 976 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 977 | struct hci_dev *hdev = NULL, *d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
| 979 | BT_DBG("%d", index); |
| 980 | |
| 981 | if (index < 0) |
| 982 | return NULL; |
| 983 | |
| 984 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 985 | list_for_each_entry(d, &hci_dev_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | if (d->id == index) { |
| 987 | hdev = hci_dev_hold(d); |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | read_unlock(&hci_dev_list_lock); |
| 992 | return hdev; |
| 993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | /* ---- Inquiry support ---- */ |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 996 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 997 | bool hci_discovery_active(struct hci_dev *hdev) |
| 998 | { |
| 999 | struct discovery_state *discov = &hdev->discovery; |
| 1000 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 1001 | switch (discov->state) { |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 1002 | case DISCOVERY_FINDING: |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 1003 | case DISCOVERY_RESOLVING: |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1004 | return true; |
| 1005 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 1006 | default: |
| 1007 | return false; |
| 1008 | } |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1009 | } |
| 1010 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1011 | void hci_discovery_set_state(struct hci_dev *hdev, int state) |
| 1012 | { |
| 1013 | BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state); |
| 1014 | |
| 1015 | if (hdev->discovery.state == state) |
| 1016 | return; |
| 1017 | |
| 1018 | switch (state) { |
| 1019 | case DISCOVERY_STOPPED: |
Andre Guedes | 7b99b65 | 2012-02-13 15:41:02 -0300 | [diff] [blame] | 1020 | if (hdev->discovery.state != DISCOVERY_STARTING) |
| 1021 | mgmt_discovering(hdev, 0); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1022 | break; |
| 1023 | case DISCOVERY_STARTING: |
| 1024 | break; |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 1025 | case DISCOVERY_FINDING: |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1026 | mgmt_discovering(hdev, 1); |
| 1027 | break; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1028 | case DISCOVERY_RESOLVING: |
| 1029 | break; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1030 | case DISCOVERY_STOPPING: |
| 1031 | break; |
| 1032 | } |
| 1033 | |
| 1034 | hdev->discovery.state = state; |
| 1035 | } |
| 1036 | |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1037 | void hci_inquiry_cache_flush(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1039 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1040 | struct inquiry_entry *p, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1042 | list_for_each_entry_safe(p, n, &cache->all, all) { |
| 1043 | list_del(&p->all); |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1044 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | } |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1046 | |
| 1047 | INIT_LIST_HEAD(&cache->unknown); |
| 1048 | INIT_LIST_HEAD(&cache->resolve); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1051 | struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, |
| 1052 | bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1054 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | struct inquiry_entry *e; |
| 1056 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1057 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1059 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1061 | return e; |
| 1062 | } |
| 1063 | |
| 1064 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1067 | struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1068 | bdaddr_t *bdaddr) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1069 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1070 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1071 | struct inquiry_entry *e; |
| 1072 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1073 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1074 | |
| 1075 | list_for_each_entry(e, &cache->unknown, list) { |
| 1076 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 1077 | return e; |
| 1078 | } |
| 1079 | |
| 1080 | return NULL; |
| 1081 | } |
| 1082 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1083 | struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1084 | bdaddr_t *bdaddr, |
| 1085 | int state) |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1086 | { |
| 1087 | struct discovery_state *cache = &hdev->discovery; |
| 1088 | struct inquiry_entry *e; |
| 1089 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1090 | BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1091 | |
| 1092 | list_for_each_entry(e, &cache->resolve, list) { |
| 1093 | if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state) |
| 1094 | return e; |
| 1095 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 1096 | return e; |
| 1097 | } |
| 1098 | |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1102 | void hci_inquiry_cache_update_resolve(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1103 | struct inquiry_entry *ie) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1104 | { |
| 1105 | struct discovery_state *cache = &hdev->discovery; |
| 1106 | struct list_head *pos = &cache->resolve; |
| 1107 | struct inquiry_entry *p; |
| 1108 | |
| 1109 | list_del(&ie->list); |
| 1110 | |
| 1111 | list_for_each_entry(p, &cache->resolve, list) { |
| 1112 | if (p->name_state != NAME_PENDING && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1113 | abs(p->data.rssi) >= abs(ie->data.rssi)) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1114 | break; |
| 1115 | pos = &p->list; |
| 1116 | } |
| 1117 | |
| 1118 | list_add(&ie->list, pos); |
| 1119 | } |
| 1120 | |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1121 | bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1122 | bool name_known, bool *ssp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1124 | struct discovery_state *cache = &hdev->discovery; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1125 | struct inquiry_entry *ie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1127 | BT_DBG("cache %p, %pMR", cache, &data->bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
Szymon Janc | 2b2fec4 | 2012-11-20 11:38:54 +0100 | [diff] [blame] | 1129 | hci_remove_remote_oob_data(hdev, &data->bdaddr); |
| 1130 | |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 1131 | if (ssp) |
| 1132 | *ssp = data->ssp_mode; |
| 1133 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1134 | ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1135 | if (ie) { |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 1136 | if (ie->data.ssp_mode && ssp) |
| 1137 | *ssp = true; |
| 1138 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1139 | if (ie->name_state == NAME_NEEDED && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1140 | data->rssi != ie->data.rssi) { |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1141 | ie->data.rssi = data->rssi; |
| 1142 | hci_inquiry_cache_update_resolve(hdev, ie); |
| 1143 | } |
| 1144 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1145 | goto update; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1146 | } |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1147 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1148 | /* Entry not in the cache. Add new one. */ |
| 1149 | ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC); |
| 1150 | if (!ie) |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1151 | return false; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1152 | |
| 1153 | list_add(&ie->all, &cache->all); |
| 1154 | |
| 1155 | if (name_known) { |
| 1156 | ie->name_state = NAME_KNOWN; |
| 1157 | } else { |
| 1158 | ie->name_state = NAME_NOT_KNOWN; |
| 1159 | list_add(&ie->list, &cache->unknown); |
| 1160 | } |
| 1161 | |
| 1162 | update: |
| 1163 | if (name_known && ie->name_state != NAME_KNOWN && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1164 | ie->name_state != NAME_PENDING) { |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1165 | ie->name_state = NAME_KNOWN; |
| 1166 | list_del(&ie->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1169 | memcpy(&ie->data, data, sizeof(*data)); |
| 1170 | ie->timestamp = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | cache->timestamp = jiffies; |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1172 | |
| 1173 | if (ie->name_state == NAME_NOT_KNOWN) |
| 1174 | return false; |
| 1175 | |
| 1176 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) |
| 1180 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1181 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | struct inquiry_info *info = (struct inquiry_info *) buf; |
| 1183 | struct inquiry_entry *e; |
| 1184 | int copied = 0; |
| 1185 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1186 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | struct inquiry_data *data = &e->data; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1188 | |
| 1189 | if (copied >= num) |
| 1190 | break; |
| 1191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | bacpy(&info->bdaddr, &data->bdaddr); |
| 1193 | info->pscan_rep_mode = data->pscan_rep_mode; |
| 1194 | info->pscan_period_mode = data->pscan_period_mode; |
| 1195 | info->pscan_mode = data->pscan_mode; |
| 1196 | memcpy(info->dev_class, data->dev_class, 3); |
| 1197 | info->clock_offset = data->clock_offset; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | info++; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1200 | copied++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | BT_DBG("cache %p, copied %d", cache, copied); |
| 1204 | return copied; |
| 1205 | } |
| 1206 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1207 | static void hci_inq_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | { |
| 1209 | struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1210 | struct hci_dev *hdev = req->hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | struct hci_cp_inquiry cp; |
| 1212 | |
| 1213 | BT_DBG("%s", hdev->name); |
| 1214 | |
| 1215 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 1216 | return; |
| 1217 | |
| 1218 | /* Start Inquiry */ |
| 1219 | memcpy(&cp.lap, &ir->lap, 3); |
| 1220 | cp.length = ir->length; |
| 1221 | cp.num_rsp = ir->num_rsp; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1222 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Andre Guedes | 3e13fa1 | 2013-03-27 20:04:56 -0300 | [diff] [blame] | 1225 | static int wait_inquiry(void *word) |
| 1226 | { |
| 1227 | schedule(); |
| 1228 | return signal_pending(current); |
| 1229 | } |
| 1230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | int hci_inquiry(void __user *arg) |
| 1232 | { |
| 1233 | __u8 __user *ptr = arg; |
| 1234 | struct hci_inquiry_req ir; |
| 1235 | struct hci_dev *hdev; |
| 1236 | int err = 0, do_inquiry = 0, max_rsp; |
| 1237 | long timeo; |
| 1238 | __u8 *buf; |
| 1239 | |
| 1240 | if (copy_from_user(&ir, ptr, sizeof(ir))) |
| 1241 | return -EFAULT; |
| 1242 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 1243 | hdev = hci_dev_get(ir.dev_id); |
| 1244 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | return -ENODEV; |
| 1246 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1247 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1248 | err = -EBUSY; |
| 1249 | goto done; |
| 1250 | } |
| 1251 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 1252 | if (hdev->dev_type != HCI_BREDR) { |
| 1253 | err = -EOPNOTSUPP; |
| 1254 | goto done; |
| 1255 | } |
| 1256 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1257 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1258 | err = -EOPNOTSUPP; |
| 1259 | goto done; |
| 1260 | } |
| 1261 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1262 | hci_dev_lock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1263 | if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1264 | inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1265 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | do_inquiry = 1; |
| 1267 | } |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1268 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1270 | timeo = ir.length * msecs_to_jiffies(2000); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1271 | |
| 1272 | if (do_inquiry) { |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1273 | err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir, |
| 1274 | timeo); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1275 | if (err < 0) |
| 1276 | goto done; |
Andre Guedes | 3e13fa1 | 2013-03-27 20:04:56 -0300 | [diff] [blame] | 1277 | |
| 1278 | /* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is |
| 1279 | * cleared). If it is interrupted by a signal, return -EINTR. |
| 1280 | */ |
| 1281 | if (wait_on_bit(&hdev->flags, HCI_INQUIRY, wait_inquiry, |
| 1282 | TASK_INTERRUPTIBLE)) |
| 1283 | return -EINTR; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1286 | /* for unlimited number of responses we will use buffer with |
| 1287 | * 255 entries |
| 1288 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; |
| 1290 | |
| 1291 | /* cache_dump can't sleep. Therefore we allocate temp buffer and then |
| 1292 | * copy it to the user space. |
| 1293 | */ |
Szymon Janc | 01df8c3 | 2011-02-17 16:46:47 +0100 | [diff] [blame] | 1294 | buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1295 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | err = -ENOMEM; |
| 1297 | goto done; |
| 1298 | } |
| 1299 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1300 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1302 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
| 1304 | BT_DBG("num_rsp %d", ir.num_rsp); |
| 1305 | |
| 1306 | if (!copy_to_user(ptr, &ir, sizeof(ir))) { |
| 1307 | ptr += sizeof(ir); |
| 1308 | if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1309 | ir.num_rsp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | err = -EFAULT; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1311 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | err = -EFAULT; |
| 1313 | |
| 1314 | kfree(buf); |
| 1315 | |
| 1316 | done: |
| 1317 | hci_dev_put(hdev); |
| 1318 | return err; |
| 1319 | } |
| 1320 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1321 | static int hci_dev_do_open(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | int ret = 0; |
| 1324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | BT_DBG("%s %p", hdev->name, hdev); |
| 1326 | |
| 1327 | hci_req_lock(hdev); |
| 1328 | |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 1329 | if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) { |
| 1330 | ret = -ENODEV; |
| 1331 | goto done; |
| 1332 | } |
| 1333 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1334 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) { |
| 1335 | /* Check for rfkill but allow the HCI setup stage to |
| 1336 | * proceed (which in itself doesn't cause any RF activity). |
| 1337 | */ |
| 1338 | if (test_bit(HCI_RFKILLED, &hdev->dev_flags)) { |
| 1339 | ret = -ERFKILL; |
| 1340 | goto done; |
| 1341 | } |
| 1342 | |
| 1343 | /* Check for valid public address or a configured static |
| 1344 | * random adddress, but let the HCI setup proceed to |
| 1345 | * be able to determine if there is a public address |
| 1346 | * or not. |
| 1347 | * |
| 1348 | * This check is only valid for BR/EDR controllers |
| 1349 | * since AMP controllers do not have an address. |
| 1350 | */ |
| 1351 | if (hdev->dev_type == HCI_BREDR && |
| 1352 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && |
| 1353 | !bacmp(&hdev->static_addr, BDADDR_ANY)) { |
| 1354 | ret = -EADDRNOTAVAIL; |
| 1355 | goto done; |
| 1356 | } |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1357 | } |
| 1358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 1360 | ret = -EALREADY; |
| 1361 | goto done; |
| 1362 | } |
| 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | if (hdev->open(hdev)) { |
| 1365 | ret = -EIO; |
| 1366 | goto done; |
| 1367 | } |
| 1368 | |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1369 | atomic_set(&hdev->cmd_cnt, 1); |
| 1370 | set_bit(HCI_INIT, &hdev->flags); |
| 1371 | |
| 1372 | if (hdev->setup && test_bit(HCI_SETUP, &hdev->dev_flags)) |
| 1373 | ret = hdev->setup(hdev); |
| 1374 | |
| 1375 | if (!ret) { |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1376 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 1377 | set_bit(HCI_RAW, &hdev->flags); |
| 1378 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1379 | if (!test_bit(HCI_RAW, &hdev->flags) && |
| 1380 | !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1381 | ret = __hci_init(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1384 | clear_bit(HCI_INIT, &hdev->flags); |
| 1385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | if (!ret) { |
| 1387 | hci_dev_hold(hdev); |
| 1388 | set_bit(HCI_UP, &hdev->flags); |
| 1389 | hci_notify(hdev, HCI_DEV_UP); |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 1390 | if (!test_bit(HCI_SETUP, &hdev->dev_flags) && |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1391 | !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) && |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 1392 | hdev->dev_type == HCI_BREDR) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1393 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1394 | mgmt_powered(hdev, 1); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1395 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1396 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1397 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | /* Init failed, cleanup */ |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1399 | flush_work(&hdev->tx_work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1400 | flush_work(&hdev->cmd_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1401 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
| 1403 | skb_queue_purge(&hdev->cmd_q); |
| 1404 | skb_queue_purge(&hdev->rx_q); |
| 1405 | |
| 1406 | if (hdev->flush) |
| 1407 | hdev->flush(hdev); |
| 1408 | |
| 1409 | if (hdev->sent_cmd) { |
| 1410 | kfree_skb(hdev->sent_cmd); |
| 1411 | hdev->sent_cmd = NULL; |
| 1412 | } |
| 1413 | |
| 1414 | hdev->close(hdev); |
| 1415 | hdev->flags = 0; |
| 1416 | } |
| 1417 | |
| 1418 | done: |
| 1419 | hci_req_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | return ret; |
| 1421 | } |
| 1422 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1423 | /* ---- HCI ioctl helpers ---- */ |
| 1424 | |
| 1425 | int hci_dev_open(__u16 dev) |
| 1426 | { |
| 1427 | struct hci_dev *hdev; |
| 1428 | int err; |
| 1429 | |
| 1430 | hdev = hci_dev_get(dev); |
| 1431 | if (!hdev) |
| 1432 | return -ENODEV; |
| 1433 | |
Johan Hedberg | e1d08f4 | 2013-10-01 22:44:50 +0300 | [diff] [blame] | 1434 | /* We need to ensure that no other power on/off work is pending |
| 1435 | * before proceeding to call hci_dev_do_open. This is |
| 1436 | * particularly important if the setup procedure has not yet |
| 1437 | * completed. |
| 1438 | */ |
| 1439 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 1440 | cancel_delayed_work(&hdev->power_off); |
| 1441 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1442 | /* After this call it is guaranteed that the setup procedure |
| 1443 | * has finished. This means that error conditions like RFKILL |
| 1444 | * or no valid public or static random address apply. |
| 1445 | */ |
Johan Hedberg | e1d08f4 | 2013-10-01 22:44:50 +0300 | [diff] [blame] | 1446 | flush_workqueue(hdev->req_workqueue); |
| 1447 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1448 | err = hci_dev_do_open(hdev); |
| 1449 | |
| 1450 | hci_dev_put(hdev); |
| 1451 | |
| 1452 | return err; |
| 1453 | } |
| 1454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | static int hci_dev_do_close(struct hci_dev *hdev) |
| 1456 | { |
| 1457 | BT_DBG("%s %p", hdev->name, hdev); |
| 1458 | |
Vinicius Costa Gomes | 78c04c0 | 2012-09-14 16:34:46 -0300 | [diff] [blame] | 1459 | cancel_delayed_work(&hdev->power_off); |
| 1460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | hci_req_cancel(hdev, ENODEV); |
| 1462 | hci_req_lock(hdev); |
| 1463 | |
| 1464 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 1465 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | hci_req_unlock(hdev); |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1470 | /* Flush RX and TX works */ |
| 1471 | flush_work(&hdev->tx_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1472 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1474 | if (hdev->discov_timeout > 0) { |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 1475 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1476 | hdev->discov_timeout = 0; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1477 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1478 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1481 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1482 | cancel_delayed_work(&hdev->service_cache); |
| 1483 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1484 | cancel_delayed_work_sync(&hdev->le_scan_disable); |
| 1485 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1486 | hci_dev_lock(hdev); |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1487 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1489 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | |
| 1491 | hci_notify(hdev, HCI_DEV_DOWN); |
| 1492 | |
| 1493 | if (hdev->flush) |
| 1494 | hdev->flush(hdev); |
| 1495 | |
| 1496 | /* Reset device */ |
| 1497 | skb_queue_purge(&hdev->cmd_q); |
| 1498 | atomic_set(&hdev->cmd_cnt, 1); |
Johan Hedberg | 8af5946 | 2012-02-03 21:29:40 +0200 | [diff] [blame] | 1499 | if (!test_bit(HCI_RAW, &hdev->flags) && |
Marcel Holtmann | 3a6afbd | 2013-10-11 09:44:12 -0700 | [diff] [blame] | 1500 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags) && |
Szymon Janc | a6c511c | 2012-05-23 12:35:46 +0200 | [diff] [blame] | 1501 | test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | set_bit(HCI_INIT, &hdev->flags); |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1503 | __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | clear_bit(HCI_INIT, &hdev->flags); |
| 1505 | } |
| 1506 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1507 | /* flush cmd work */ |
| 1508 | flush_work(&hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | |
| 1510 | /* Drop queues */ |
| 1511 | skb_queue_purge(&hdev->rx_q); |
| 1512 | skb_queue_purge(&hdev->cmd_q); |
| 1513 | skb_queue_purge(&hdev->raw_q); |
| 1514 | |
| 1515 | /* Drop last sent command */ |
| 1516 | if (hdev->sent_cmd) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 1517 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | kfree_skb(hdev->sent_cmd); |
| 1519 | hdev->sent_cmd = NULL; |
| 1520 | } |
| 1521 | |
Johan Hedberg | b6ddb63 | 2013-04-02 13:34:31 +0300 | [diff] [blame] | 1522 | kfree_skb(hdev->recv_evt); |
| 1523 | hdev->recv_evt = NULL; |
| 1524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | /* After this point our queues are empty |
| 1526 | * and no tasks are scheduled. */ |
| 1527 | hdev->close(hdev); |
| 1528 | |
Johan Hedberg | 35b973c | 2013-03-15 17:06:59 -0500 | [diff] [blame] | 1529 | /* Clear flags */ |
| 1530 | hdev->flags = 0; |
| 1531 | hdev->dev_flags &= ~HCI_PERSISTENT_MASK; |
| 1532 | |
Marcel Holtmann | 93c311a | 2013-10-07 00:58:33 -0700 | [diff] [blame] | 1533 | if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1534 | if (hdev->dev_type == HCI_BREDR) { |
| 1535 | hci_dev_lock(hdev); |
| 1536 | mgmt_powered(hdev, 0); |
| 1537 | hci_dev_unlock(hdev); |
| 1538 | } |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1539 | } |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1540 | |
Andrei Emeltchenko | ced5c33 | 2012-11-28 17:59:42 +0200 | [diff] [blame] | 1541 | /* Controller radio is available but is currently powered down */ |
Marcel Holtmann | 536619e | 2013-10-05 11:47:45 -0700 | [diff] [blame] | 1542 | hdev->amp_status = AMP_STATUS_POWERED_DOWN; |
Andrei Emeltchenko | ced5c33 | 2012-11-28 17:59:42 +0200 | [diff] [blame] | 1543 | |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 1544 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
Johan Hedberg | 09b3c3f | 2012-02-22 22:01:41 +0200 | [diff] [blame] | 1545 | memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | hci_req_unlock(hdev); |
| 1548 | |
| 1549 | hci_dev_put(hdev); |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | int hci_dev_close(__u16 dev) |
| 1554 | { |
| 1555 | struct hci_dev *hdev; |
| 1556 | int err; |
| 1557 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1558 | hdev = hci_dev_get(dev); |
| 1559 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | return -ENODEV; |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1561 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1562 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1563 | err = -EBUSY; |
| 1564 | goto done; |
| 1565 | } |
| 1566 | |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1567 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 1568 | cancel_delayed_work(&hdev->power_off); |
| 1569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | err = hci_dev_do_close(hdev); |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1571 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1572 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | hci_dev_put(hdev); |
| 1574 | return err; |
| 1575 | } |
| 1576 | |
| 1577 | int hci_dev_reset(__u16 dev) |
| 1578 | { |
| 1579 | struct hci_dev *hdev; |
| 1580 | int ret = 0; |
| 1581 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1582 | hdev = hci_dev_get(dev); |
| 1583 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | return -ENODEV; |
| 1585 | |
| 1586 | hci_req_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
Marcel Holtmann | 808a049 | 2013-08-26 20:57:58 -0700 | [diff] [blame] | 1588 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1589 | ret = -ENETDOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | goto done; |
Marcel Holtmann | 808a049 | 2013-08-26 20:57:58 -0700 | [diff] [blame] | 1591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1593 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1594 | ret = -EBUSY; |
| 1595 | goto done; |
| 1596 | } |
| 1597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | /* Drop queues */ |
| 1599 | skb_queue_purge(&hdev->rx_q); |
| 1600 | skb_queue_purge(&hdev->cmd_q); |
| 1601 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1602 | hci_dev_lock(hdev); |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1603 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1605 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | |
| 1607 | if (hdev->flush) |
| 1608 | hdev->flush(hdev); |
| 1609 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1610 | atomic_set(&hdev->cmd_cnt, 1); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 1611 | hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | |
| 1613 | if (!test_bit(HCI_RAW, &hdev->flags)) |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1614 | ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
| 1616 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | hci_req_unlock(hdev); |
| 1618 | hci_dev_put(hdev); |
| 1619 | return ret; |
| 1620 | } |
| 1621 | |
| 1622 | int hci_dev_reset_stat(__u16 dev) |
| 1623 | { |
| 1624 | struct hci_dev *hdev; |
| 1625 | int ret = 0; |
| 1626 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1627 | hdev = hci_dev_get(dev); |
| 1628 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | return -ENODEV; |
| 1630 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1631 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1632 | ret = -EBUSY; |
| 1633 | goto done; |
| 1634 | } |
| 1635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
| 1637 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1638 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | hci_dev_put(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | return ret; |
| 1641 | } |
| 1642 | |
| 1643 | int hci_dev_cmd(unsigned int cmd, void __user *arg) |
| 1644 | { |
| 1645 | struct hci_dev *hdev; |
| 1646 | struct hci_dev_req dr; |
| 1647 | int err = 0; |
| 1648 | |
| 1649 | if (copy_from_user(&dr, arg, sizeof(dr))) |
| 1650 | return -EFAULT; |
| 1651 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1652 | hdev = hci_dev_get(dr.dev_id); |
| 1653 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | return -ENODEV; |
| 1655 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1656 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1657 | err = -EBUSY; |
| 1658 | goto done; |
| 1659 | } |
| 1660 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 1661 | if (hdev->dev_type != HCI_BREDR) { |
| 1662 | err = -EOPNOTSUPP; |
| 1663 | goto done; |
| 1664 | } |
| 1665 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1666 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1667 | err = -EOPNOTSUPP; |
| 1668 | goto done; |
| 1669 | } |
| 1670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | switch (cmd) { |
| 1672 | case HCISETAUTH: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1673 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
| 1674 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | break; |
| 1676 | |
| 1677 | case HCISETENCRYPT: |
| 1678 | if (!lmp_encrypt_capable(hdev)) { |
| 1679 | err = -EOPNOTSUPP; |
| 1680 | break; |
| 1681 | } |
| 1682 | |
| 1683 | if (!test_bit(HCI_AUTH, &hdev->flags)) { |
| 1684 | /* Auth must be enabled first */ |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1685 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
| 1686 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | if (err) |
| 1688 | break; |
| 1689 | } |
| 1690 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1691 | err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt, |
| 1692 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | break; |
| 1694 | |
| 1695 | case HCISETSCAN: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1696 | err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt, |
| 1697 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | break; |
| 1699 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1700 | case HCISETLINKPOL: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1701 | err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt, |
| 1702 | HCI_INIT_TIMEOUT); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1703 | break; |
| 1704 | |
| 1705 | case HCISETLINKMODE: |
| 1706 | hdev->link_mode = ((__u16) dr.dev_opt) & |
| 1707 | (HCI_LM_MASTER | HCI_LM_ACCEPT); |
| 1708 | break; |
| 1709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | case HCISETPTYPE: |
| 1711 | hdev->pkt_type = (__u16) dr.dev_opt; |
| 1712 | break; |
| 1713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | case HCISETACLMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1715 | hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 1716 | hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | break; |
| 1718 | |
| 1719 | case HCISETSCOMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1720 | hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 1721 | hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | break; |
| 1723 | |
| 1724 | default: |
| 1725 | err = -EINVAL; |
| 1726 | break; |
| 1727 | } |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1728 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1729 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | hci_dev_put(hdev); |
| 1731 | return err; |
| 1732 | } |
| 1733 | |
| 1734 | int hci_get_dev_list(void __user *arg) |
| 1735 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1736 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | struct hci_dev_list_req *dl; |
| 1738 | struct hci_dev_req *dr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | int n = 0, size, err; |
| 1740 | __u16 dev_num; |
| 1741 | |
| 1742 | if (get_user(dev_num, (__u16 __user *) arg)) |
| 1743 | return -EFAULT; |
| 1744 | |
| 1745 | if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) |
| 1746 | return -EINVAL; |
| 1747 | |
| 1748 | size = sizeof(*dl) + dev_num * sizeof(*dr); |
| 1749 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1750 | dl = kzalloc(size, GFP_KERNEL); |
| 1751 | if (!dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | return -ENOMEM; |
| 1753 | |
| 1754 | dr = dl->dev_req; |
| 1755 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1756 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1757 | list_for_each_entry(hdev, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1758 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 1759 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1760 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1761 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 1762 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | (dr + n)->dev_id = hdev->id; |
| 1765 | (dr + n)->dev_opt = hdev->flags; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | if (++n >= dev_num) |
| 1768 | break; |
| 1769 | } |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1770 | read_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
| 1772 | dl->dev_num = n; |
| 1773 | size = sizeof(*dl) + n * sizeof(*dr); |
| 1774 | |
| 1775 | err = copy_to_user(arg, dl, size); |
| 1776 | kfree(dl); |
| 1777 | |
| 1778 | return err ? -EFAULT : 0; |
| 1779 | } |
| 1780 | |
| 1781 | int hci_get_dev_info(void __user *arg) |
| 1782 | { |
| 1783 | struct hci_dev *hdev; |
| 1784 | struct hci_dev_info di; |
| 1785 | int err = 0; |
| 1786 | |
| 1787 | if (copy_from_user(&di, arg, sizeof(di))) |
| 1788 | return -EFAULT; |
| 1789 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1790 | hdev = hci_dev_get(di.dev_id); |
| 1791 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | return -ENODEV; |
| 1793 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1794 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 1795 | cancel_delayed_work_sync(&hdev->power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1796 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1797 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 1798 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | strcpy(di.name, hdev->name); |
| 1801 | di.bdaddr = hdev->bdaddr; |
Marcel Holtmann | 60f2a3e | 2013-10-01 22:59:20 -0700 | [diff] [blame] | 1802 | di.type = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | di.flags = hdev->flags; |
| 1804 | di.pkt_type = hdev->pkt_type; |
Johan Hedberg | 572c7f8 | 2012-10-19 20:57:46 +0300 | [diff] [blame] | 1805 | if (lmp_bredr_capable(hdev)) { |
| 1806 | di.acl_mtu = hdev->acl_mtu; |
| 1807 | di.acl_pkts = hdev->acl_pkts; |
| 1808 | di.sco_mtu = hdev->sco_mtu; |
| 1809 | di.sco_pkts = hdev->sco_pkts; |
| 1810 | } else { |
| 1811 | di.acl_mtu = hdev->le_mtu; |
| 1812 | di.acl_pkts = hdev->le_pkts; |
| 1813 | di.sco_mtu = 0; |
| 1814 | di.sco_pkts = 0; |
| 1815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | di.link_policy = hdev->link_policy; |
| 1817 | di.link_mode = hdev->link_mode; |
| 1818 | |
| 1819 | memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); |
| 1820 | memcpy(&di.features, &hdev->features, sizeof(di.features)); |
| 1821 | |
| 1822 | if (copy_to_user(arg, &di, sizeof(di))) |
| 1823 | err = -EFAULT; |
| 1824 | |
| 1825 | hci_dev_put(hdev); |
| 1826 | |
| 1827 | return err; |
| 1828 | } |
| 1829 | |
| 1830 | /* ---- Interface to HCI drivers ---- */ |
| 1831 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1832 | static int hci_rfkill_set_block(void *data, bool blocked) |
| 1833 | { |
| 1834 | struct hci_dev *hdev = data; |
| 1835 | |
| 1836 | BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); |
| 1837 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1838 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) |
| 1839 | return -EBUSY; |
| 1840 | |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 1841 | if (blocked) { |
| 1842 | set_bit(HCI_RFKILLED, &hdev->dev_flags); |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 1843 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) |
| 1844 | hci_dev_do_close(hdev); |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 1845 | } else { |
| 1846 | clear_bit(HCI_RFKILLED, &hdev->dev_flags); |
Gustavo Padovan | 1025c04 | 2013-09-27 11:56:14 -0300 | [diff] [blame] | 1847 | } |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1848 | |
| 1849 | return 0; |
| 1850 | } |
| 1851 | |
| 1852 | static const struct rfkill_ops hci_rfkill_ops = { |
| 1853 | .set_block = hci_rfkill_set_block, |
| 1854 | }; |
| 1855 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1856 | static void hci_power_on(struct work_struct *work) |
| 1857 | { |
| 1858 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 1859 | int err; |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1860 | |
| 1861 | BT_DBG("%s", hdev->name); |
| 1862 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1863 | err = hci_dev_do_open(hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 1864 | if (err < 0) { |
| 1865 | mgmt_set_powered_failed(hdev, err); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1866 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 1867 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1868 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1869 | /* During the HCI setup phase, a few error conditions are |
| 1870 | * ignored and they need to be checked now. If they are still |
| 1871 | * valid, it is important to turn the device back off. |
| 1872 | */ |
| 1873 | if (test_bit(HCI_RFKILLED, &hdev->dev_flags) || |
| 1874 | (hdev->dev_type == HCI_BREDR && |
| 1875 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && |
| 1876 | !bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 1877 | clear_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 1878 | hci_dev_do_close(hdev); |
| 1879 | } else if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1880 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1881 | HCI_AUTO_OFF_TIMEOUT); |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 1882 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1883 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1884 | if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1885 | mgmt_index_added(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | static void hci_power_off(struct work_struct *work) |
| 1889 | { |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 1890 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1891 | power_off.work); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1892 | |
| 1893 | BT_DBG("%s", hdev->name); |
| 1894 | |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1895 | hci_dev_do_close(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1896 | } |
| 1897 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1898 | static void hci_discov_off(struct work_struct *work) |
| 1899 | { |
| 1900 | struct hci_dev *hdev; |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1901 | |
| 1902 | hdev = container_of(work, struct hci_dev, discov_off.work); |
| 1903 | |
| 1904 | BT_DBG("%s", hdev->name); |
| 1905 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 1906 | mgmt_discoverable_timeout(hdev); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1907 | } |
| 1908 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1909 | int hci_uuids_clear(struct hci_dev *hdev) |
| 1910 | { |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 1911 | struct bt_uuid *uuid, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1912 | |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 1913 | list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) { |
| 1914 | list_del(&uuid->list); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1915 | kfree(uuid); |
| 1916 | } |
| 1917 | |
| 1918 | return 0; |
| 1919 | } |
| 1920 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1921 | int hci_link_keys_clear(struct hci_dev *hdev) |
| 1922 | { |
| 1923 | struct list_head *p, *n; |
| 1924 | |
| 1925 | list_for_each_safe(p, n, &hdev->link_keys) { |
| 1926 | struct link_key *key; |
| 1927 | |
| 1928 | key = list_entry(p, struct link_key, list); |
| 1929 | |
| 1930 | list_del(p); |
| 1931 | kfree(key); |
| 1932 | } |
| 1933 | |
| 1934 | return 0; |
| 1935 | } |
| 1936 | |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 1937 | int hci_smp_ltks_clear(struct hci_dev *hdev) |
| 1938 | { |
| 1939 | struct smp_ltk *k, *tmp; |
| 1940 | |
| 1941 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { |
| 1942 | list_del(&k->list); |
| 1943 | kfree(k); |
| 1944 | } |
| 1945 | |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1949 | struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1950 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1951 | struct link_key *k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1952 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1953 | list_for_each_entry(k, &hdev->link_keys, list) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1954 | if (bacmp(bdaddr, &k->bdaddr) == 0) |
| 1955 | return k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1956 | |
| 1957 | return NULL; |
| 1958 | } |
| 1959 | |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1960 | static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1961 | u8 key_type, u8 old_key_type) |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1962 | { |
| 1963 | /* Legacy key */ |
| 1964 | if (key_type < 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1965 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1966 | |
| 1967 | /* Debug keys are insecure so don't store them persistently */ |
| 1968 | if (key_type == HCI_LK_DEBUG_COMBINATION) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1969 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1970 | |
| 1971 | /* Changed combination key and there's no previous one */ |
| 1972 | if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1973 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1974 | |
| 1975 | /* Security mode 3 case */ |
| 1976 | if (!conn) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1977 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1978 | |
| 1979 | /* Neither local nor remote side had no-bonding as requirement */ |
| 1980 | if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1981 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1982 | |
| 1983 | /* Local side had dedicated bonding as requirement */ |
| 1984 | if (conn->auth_type == 0x02 || conn->auth_type == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1985 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1986 | |
| 1987 | /* Remote side had dedicated bonding as requirement */ |
| 1988 | if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1989 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1990 | |
| 1991 | /* If none of the above criteria match, then don't store the key |
| 1992 | * persistently */ |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1993 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1996 | struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1997 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1998 | struct smp_ltk *k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1999 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2000 | list_for_each_entry(k, &hdev->long_term_keys, list) { |
| 2001 | if (k->ediv != ediv || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2002 | memcmp(rand, k->rand, sizeof(k->rand))) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2003 | continue; |
| 2004 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2005 | return k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | return NULL; |
| 2009 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2010 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2011 | struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2012 | u8 addr_type) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2013 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2014 | struct smp_ltk *k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2015 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2016 | list_for_each_entry(k, &hdev->long_term_keys, list) |
| 2017 | if (addr_type == k->bdaddr_type && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2018 | bacmp(bdaddr, &k->bdaddr) == 0) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2019 | return k; |
| 2020 | |
| 2021 | return NULL; |
| 2022 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2023 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2024 | int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2025 | bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2026 | { |
| 2027 | struct link_key *key, *old_key; |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2028 | u8 old_key_type; |
| 2029 | bool persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2030 | |
| 2031 | old_key = hci_find_link_key(hdev, bdaddr); |
| 2032 | if (old_key) { |
| 2033 | old_key_type = old_key->type; |
| 2034 | key = old_key; |
| 2035 | } else { |
Johan Hedberg | 12adcf3 | 2011-04-28 11:29:00 -0700 | [diff] [blame] | 2036 | old_key_type = conn ? conn->key_type : 0xff; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2037 | key = kzalloc(sizeof(*key), GFP_ATOMIC); |
| 2038 | if (!key) |
| 2039 | return -ENOMEM; |
| 2040 | list_add(&key->list, &hdev->link_keys); |
| 2041 | } |
| 2042 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2043 | BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2044 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2045 | /* Some buggy controller combinations generate a changed |
| 2046 | * combination key for legacy pairing even when there's no |
| 2047 | * previous key */ |
| 2048 | if (type == HCI_LK_CHANGED_COMBINATION && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2049 | (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) { |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2050 | type = HCI_LK_COMBINATION; |
Johan Hedberg | 655fe6e | 2011-04-28 11:29:01 -0700 | [diff] [blame] | 2051 | if (conn) |
| 2052 | conn->key_type = type; |
| 2053 | } |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2054 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2055 | bacpy(&key->bdaddr, bdaddr); |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 2056 | memcpy(key->val, val, HCI_LINK_KEY_SIZE); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2057 | key->pin_len = pin_len; |
| 2058 | |
Waldemar Rymarkiewicz | b6020ba | 2011-04-28 12:07:53 +0200 | [diff] [blame] | 2059 | if (type == HCI_LK_CHANGED_COMBINATION) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2060 | key->type = old_key_type; |
Johan Hedberg | 4748fed | 2011-04-28 11:29:02 -0700 | [diff] [blame] | 2061 | else |
| 2062 | key->type = type; |
| 2063 | |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 2064 | if (!new_key) |
| 2065 | return 0; |
| 2066 | |
| 2067 | persistent = hci_persistent_key(hdev, conn, type, old_key_type); |
| 2068 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2069 | mgmt_new_link_key(hdev, key, persistent); |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 2070 | |
Vishal Agarwal | 6ec5bca | 2012-04-16 14:44:44 +0530 | [diff] [blame] | 2071 | if (conn) |
| 2072 | conn->flush_key = !persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2073 | |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2077 | int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type, |
Andrei Emeltchenko | 9a00665 | 2012-03-09 12:12:12 +0200 | [diff] [blame] | 2078 | int new_key, u8 authenticated, u8 tk[16], u8 enc_size, __le16 |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2079 | ediv, u8 rand[8]) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2080 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2081 | struct smp_ltk *key, *old_key; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2082 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2083 | if (!(type & HCI_SMP_STK) && !(type & HCI_SMP_LTK)) |
| 2084 | return 0; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2085 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2086 | old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type); |
| 2087 | if (old_key) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2088 | key = old_key; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2089 | else { |
| 2090 | key = kzalloc(sizeof(*key), GFP_ATOMIC); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2091 | if (!key) |
| 2092 | return -ENOMEM; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2093 | list_add(&key->list, &hdev->long_term_keys); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2094 | } |
| 2095 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2096 | bacpy(&key->bdaddr, bdaddr); |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2097 | key->bdaddr_type = addr_type; |
| 2098 | memcpy(key->val, tk, sizeof(key->val)); |
| 2099 | key->authenticated = authenticated; |
| 2100 | key->ediv = ediv; |
| 2101 | key->enc_size = enc_size; |
| 2102 | key->type = type; |
| 2103 | memcpy(key->rand, rand, sizeof(key->rand)); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2104 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2105 | if (!new_key) |
| 2106 | return 0; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2107 | |
Vinicius Costa Gomes | 261cc5a | 2012-02-02 21:08:05 -0300 | [diff] [blame] | 2108 | if (type & HCI_SMP_LTK) |
| 2109 | mgmt_new_ltk(hdev, key, 1); |
| 2110 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2111 | return 0; |
| 2112 | } |
| 2113 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2114 | int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2115 | { |
| 2116 | struct link_key *key; |
| 2117 | |
| 2118 | key = hci_find_link_key(hdev, bdaddr); |
| 2119 | if (!key) |
| 2120 | return -ENOENT; |
| 2121 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2122 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2123 | |
| 2124 | list_del(&key->list); |
| 2125 | kfree(key); |
| 2126 | |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2130 | int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2131 | { |
| 2132 | struct smp_ltk *k, *tmp; |
| 2133 | |
| 2134 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { |
| 2135 | if (bacmp(bdaddr, &k->bdaddr)) |
| 2136 | continue; |
| 2137 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2138 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2139 | |
| 2140 | list_del(&k->list); |
| 2141 | kfree(k); |
| 2142 | } |
| 2143 | |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2147 | /* HCI command timer function */ |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2148 | static void hci_cmd_timeout(unsigned long arg) |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2149 | { |
| 2150 | struct hci_dev *hdev = (void *) arg; |
| 2151 | |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2152 | if (hdev->sent_cmd) { |
| 2153 | struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data; |
| 2154 | u16 opcode = __le16_to_cpu(sent->opcode); |
| 2155 | |
| 2156 | BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode); |
| 2157 | } else { |
| 2158 | BT_ERR("%s command tx timeout", hdev->name); |
| 2159 | } |
| 2160 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2161 | atomic_set(&hdev->cmd_cnt, 1); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2162 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2163 | } |
| 2164 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2165 | struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2166 | bdaddr_t *bdaddr) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2167 | { |
| 2168 | struct oob_data *data; |
| 2169 | |
| 2170 | list_for_each_entry(data, &hdev->remote_oob_data, list) |
| 2171 | if (bacmp(bdaddr, &data->bdaddr) == 0) |
| 2172 | return data; |
| 2173 | |
| 2174 | return NULL; |
| 2175 | } |
| 2176 | |
| 2177 | int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2178 | { |
| 2179 | struct oob_data *data; |
| 2180 | |
| 2181 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 2182 | if (!data) |
| 2183 | return -ENOENT; |
| 2184 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2185 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2186 | |
| 2187 | list_del(&data->list); |
| 2188 | kfree(data); |
| 2189 | |
| 2190 | return 0; |
| 2191 | } |
| 2192 | |
| 2193 | int hci_remote_oob_data_clear(struct hci_dev *hdev) |
| 2194 | { |
| 2195 | struct oob_data *data, *n; |
| 2196 | |
| 2197 | list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { |
| 2198 | list_del(&data->list); |
| 2199 | kfree(data); |
| 2200 | } |
| 2201 | |
| 2202 | return 0; |
| 2203 | } |
| 2204 | |
| 2205 | int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2206 | u8 *randomizer) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2207 | { |
| 2208 | struct oob_data *data; |
| 2209 | |
| 2210 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 2211 | |
| 2212 | if (!data) { |
| 2213 | data = kmalloc(sizeof(*data), GFP_ATOMIC); |
| 2214 | if (!data) |
| 2215 | return -ENOMEM; |
| 2216 | |
| 2217 | bacpy(&data->bdaddr, bdaddr); |
| 2218 | list_add(&data->list, &hdev->remote_oob_data); |
| 2219 | } |
| 2220 | |
| 2221 | memcpy(data->hash, hash, sizeof(data->hash)); |
| 2222 | memcpy(data->randomizer, randomizer, sizeof(data->randomizer)); |
| 2223 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2224 | BT_DBG("%s for %pMR", hdev->name, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2225 | |
| 2226 | return 0; |
| 2227 | } |
| 2228 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2229 | struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, |
| 2230 | bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2231 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2232 | struct bdaddr_list *b; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2233 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2234 | list_for_each_entry(b, &hdev->blacklist, list) { |
| 2235 | if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2236 | return b; |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2237 | } |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2238 | |
| 2239 | return NULL; |
| 2240 | } |
| 2241 | |
| 2242 | int hci_blacklist_clear(struct hci_dev *hdev) |
| 2243 | { |
| 2244 | struct list_head *p, *n; |
| 2245 | |
| 2246 | list_for_each_safe(p, n, &hdev->blacklist) { |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2247 | struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2248 | |
| 2249 | list_del(p); |
| 2250 | kfree(b); |
| 2251 | } |
| 2252 | |
| 2253 | return 0; |
| 2254 | } |
| 2255 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2256 | int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2257 | { |
| 2258 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2259 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2260 | if (!bacmp(bdaddr, BDADDR_ANY)) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2261 | return -EBADF; |
| 2262 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2263 | if (hci_blacklist_lookup(hdev, bdaddr, type)) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2264 | return -EEXIST; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2265 | |
| 2266 | entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2267 | if (!entry) |
| 2268 | return -ENOMEM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2269 | |
| 2270 | bacpy(&entry->bdaddr, bdaddr); |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2271 | entry->bdaddr_type = type; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2272 | |
| 2273 | list_add(&entry->list, &hdev->blacklist); |
| 2274 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2275 | return mgmt_device_blocked(hdev, bdaddr, type); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2276 | } |
| 2277 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2278 | int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2279 | { |
| 2280 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2281 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2282 | if (!bacmp(bdaddr, BDADDR_ANY)) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2283 | return hci_blacklist_clear(hdev); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2284 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2285 | entry = hci_blacklist_lookup(hdev, bdaddr, type); |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 2286 | if (!entry) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2287 | return -ENOENT; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2288 | |
| 2289 | list_del(&entry->list); |
| 2290 | kfree(entry); |
| 2291 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2292 | return mgmt_device_unblocked(hdev, bdaddr, type); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2293 | } |
| 2294 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2295 | static void inquiry_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2296 | { |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2297 | if (status) { |
| 2298 | BT_ERR("Failed to start inquiry: status %d", status); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2299 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2300 | hci_dev_lock(hdev); |
| 2301 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2302 | hci_dev_unlock(hdev); |
| 2303 | return; |
| 2304 | } |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2305 | } |
| 2306 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2307 | static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2308 | { |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2309 | /* General inquiry access code (GIAC) */ |
| 2310 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 2311 | struct hci_request req; |
| 2312 | struct hci_cp_inquiry cp; |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2313 | int err; |
| 2314 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2315 | if (status) { |
| 2316 | BT_ERR("Failed to disable LE scanning: status %d", status); |
| 2317 | return; |
Andre Guedes | 7dbfac1 | 2012-03-15 16:52:07 -0300 | [diff] [blame] | 2318 | } |
| 2319 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2320 | switch (hdev->discovery.type) { |
| 2321 | case DISCOV_TYPE_LE: |
| 2322 | hci_dev_lock(hdev); |
| 2323 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2324 | hci_dev_unlock(hdev); |
| 2325 | break; |
| 2326 | |
| 2327 | case DISCOV_TYPE_INTERLEAVED: |
| 2328 | hci_req_init(&req, hdev); |
| 2329 | |
| 2330 | memset(&cp, 0, sizeof(cp)); |
| 2331 | memcpy(&cp.lap, lap, sizeof(cp.lap)); |
| 2332 | cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN; |
| 2333 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp); |
| 2334 | |
| 2335 | hci_dev_lock(hdev); |
| 2336 | |
| 2337 | hci_inquiry_cache_flush(hdev); |
| 2338 | |
| 2339 | err = hci_req_run(&req, inquiry_complete); |
| 2340 | if (err) { |
| 2341 | BT_ERR("Inquiry request failed: err %d", err); |
| 2342 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2343 | } |
| 2344 | |
| 2345 | hci_dev_unlock(hdev); |
| 2346 | break; |
| 2347 | } |
Andre Guedes | 7dbfac1 | 2012-03-15 16:52:07 -0300 | [diff] [blame] | 2348 | } |
| 2349 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2350 | static void le_scan_disable_work(struct work_struct *work) |
| 2351 | { |
| 2352 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2353 | le_scan_disable.work); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2354 | struct hci_cp_le_set_scan_enable cp; |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2355 | struct hci_request req; |
| 2356 | int err; |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2357 | |
| 2358 | BT_DBG("%s", hdev->name); |
| 2359 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2360 | hci_req_init(&req, hdev); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2361 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2362 | memset(&cp, 0, sizeof(cp)); |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2363 | cp.enable = LE_SCAN_DISABLE; |
| 2364 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2365 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2366 | err = hci_req_run(&req, le_scan_disable_work_complete); |
| 2367 | if (err) |
| 2368 | BT_ERR("Disable LE scanning request failed: err %d", err); |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 2369 | } |
| 2370 | |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2371 | /* Alloc HCI device */ |
| 2372 | struct hci_dev *hci_alloc_dev(void) |
| 2373 | { |
| 2374 | struct hci_dev *hdev; |
| 2375 | |
| 2376 | hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); |
| 2377 | if (!hdev) |
| 2378 | return NULL; |
| 2379 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2380 | hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); |
| 2381 | hdev->esco_type = (ESCO_HV1); |
| 2382 | hdev->link_mode = (HCI_LM_ACCEPT); |
Marcel Holtmann | b4cb9fb | 2013-10-14 13:56:16 -0700 | [diff] [blame] | 2383 | hdev->num_iac = 0x01; /* One IAC support is mandatory */ |
| 2384 | hdev->io_capability = 0x03; /* No Input No Output */ |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 2385 | hdev->inq_tx_power = HCI_TX_POWER_INVALID; |
| 2386 | hdev->adv_tx_power = HCI_TX_POWER_INVALID; |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2387 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2388 | hdev->sniff_max_interval = 800; |
| 2389 | hdev->sniff_min_interval = 80; |
| 2390 | |
Marcel Holtmann | bef6473 | 2013-10-11 08:23:19 -0700 | [diff] [blame] | 2391 | hdev->le_scan_interval = 0x0060; |
| 2392 | hdev->le_scan_window = 0x0030; |
| 2393 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2394 | mutex_init(&hdev->lock); |
| 2395 | mutex_init(&hdev->req_lock); |
| 2396 | |
| 2397 | INIT_LIST_HEAD(&hdev->mgmt_pending); |
| 2398 | INIT_LIST_HEAD(&hdev->blacklist); |
| 2399 | INIT_LIST_HEAD(&hdev->uuids); |
| 2400 | INIT_LIST_HEAD(&hdev->link_keys); |
| 2401 | INIT_LIST_HEAD(&hdev->long_term_keys); |
| 2402 | INIT_LIST_HEAD(&hdev->remote_oob_data); |
Andrei Emeltchenko | 6b536b5 | 2012-08-31 16:39:28 +0300 | [diff] [blame] | 2403 | INIT_LIST_HEAD(&hdev->conn_hash.list); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2404 | |
| 2405 | INIT_WORK(&hdev->rx_work, hci_rx_work); |
| 2406 | INIT_WORK(&hdev->cmd_work, hci_cmd_work); |
| 2407 | INIT_WORK(&hdev->tx_work, hci_tx_work); |
| 2408 | INIT_WORK(&hdev->power_on, hci_power_on); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2409 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2410 | INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); |
| 2411 | INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off); |
| 2412 | INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work); |
| 2413 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2414 | skb_queue_head_init(&hdev->rx_q); |
| 2415 | skb_queue_head_init(&hdev->cmd_q); |
| 2416 | skb_queue_head_init(&hdev->raw_q); |
| 2417 | |
| 2418 | init_waitqueue_head(&hdev->req_wait_q); |
| 2419 | |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2420 | setup_timer(&hdev->cmd_timer, hci_cmd_timeout, (unsigned long) hdev); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2421 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2422 | hci_init_sysfs(hdev); |
| 2423 | discovery_init(hdev); |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2424 | |
| 2425 | return hdev; |
| 2426 | } |
| 2427 | EXPORT_SYMBOL(hci_alloc_dev); |
| 2428 | |
| 2429 | /* Free HCI device */ |
| 2430 | void hci_free_dev(struct hci_dev *hdev) |
| 2431 | { |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2432 | /* will free via device release */ |
| 2433 | put_device(&hdev->dev); |
| 2434 | } |
| 2435 | EXPORT_SYMBOL(hci_free_dev); |
| 2436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | /* Register HCI device */ |
| 2438 | int hci_register_dev(struct hci_dev *hdev) |
| 2439 | { |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2440 | int id, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | |
David Herrmann | 010666a | 2012-01-07 15:47:07 +0100 | [diff] [blame] | 2442 | if (!hdev->open || !hdev->close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | return -EINVAL; |
| 2444 | |
Mat Martineau | 08add51 | 2011-11-02 16:18:36 -0700 | [diff] [blame] | 2445 | /* Do not allow HCI_AMP devices to register at index 0, |
| 2446 | * so the index can be used as the AMP controller ID. |
| 2447 | */ |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2448 | switch (hdev->dev_type) { |
| 2449 | case HCI_BREDR: |
| 2450 | id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL); |
| 2451 | break; |
| 2452 | case HCI_AMP: |
| 2453 | id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL); |
| 2454 | break; |
| 2455 | default: |
| 2456 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2458 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2459 | if (id < 0) |
| 2460 | return id; |
| 2461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | sprintf(hdev->name, "hci%d", id); |
| 2463 | hdev->id = id; |
Andrei Emeltchenko | 2d8b3a1 | 2012-04-16 16:32:04 +0300 | [diff] [blame] | 2464 | |
| 2465 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
| 2466 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 2467 | hdev->workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | |
| 2468 | WQ_MEM_RECLAIM, 1, hdev->name); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2469 | if (!hdev->workqueue) { |
| 2470 | error = -ENOMEM; |
| 2471 | goto err; |
| 2472 | } |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2473 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 2474 | hdev->req_workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | |
| 2475 | WQ_MEM_RECLAIM, 1, hdev->name); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 2476 | if (!hdev->req_workqueue) { |
| 2477 | destroy_workqueue(hdev->workqueue); |
| 2478 | error = -ENOMEM; |
| 2479 | goto err; |
| 2480 | } |
| 2481 | |
Marcel Holtmann | 0153e2e | 2013-10-17 17:24:17 -0700 | [diff] [blame^] | 2482 | if (!IS_ERR_OR_NULL(bt_debugfs)) |
| 2483 | hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs); |
| 2484 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2485 | error = hci_add_sysfs(hdev); |
| 2486 | if (error < 0) |
| 2487 | goto err_wqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2489 | hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2490 | RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, |
| 2491 | hdev); |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2492 | if (hdev->rfkill) { |
| 2493 | if (rfkill_register(hdev->rfkill) < 0) { |
| 2494 | rfkill_destroy(hdev->rfkill); |
| 2495 | hdev->rfkill = NULL; |
| 2496 | } |
| 2497 | } |
| 2498 | |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 2499 | if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) |
| 2500 | set_bit(HCI_RFKILLED, &hdev->dev_flags); |
| 2501 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2502 | set_bit(HCI_SETUP, &hdev->dev_flags); |
Marcel Holtmann | 004b025 | 2013-10-07 00:58:32 -0700 | [diff] [blame] | 2503 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
Andrei Emeltchenko | ce2be9a | 2012-06-29 15:07:00 +0300 | [diff] [blame] | 2504 | |
Marcel Holtmann | 01cd340 | 2013-10-06 01:16:22 -0700 | [diff] [blame] | 2505 | if (hdev->dev_type == HCI_BREDR) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2506 | /* Assume BR/EDR support until proven otherwise (such as |
| 2507 | * through reading supported features during init. |
| 2508 | */ |
| 2509 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 2510 | } |
Andrei Emeltchenko | ce2be9a | 2012-06-29 15:07:00 +0300 | [diff] [blame] | 2511 | |
Gustavo Padovan | fcee337 | 2013-07-11 11:34:28 +0100 | [diff] [blame] | 2512 | write_lock(&hci_dev_list_lock); |
| 2513 | list_add(&hdev->list, &hci_dev_list); |
| 2514 | write_unlock(&hci_dev_list_lock); |
| 2515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | hci_notify(hdev, HCI_DEV_REG); |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 2517 | hci_dev_hold(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 2519 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Marcel Holtmann | fbe96d6 | 2012-10-30 01:35:40 -0700 | [diff] [blame] | 2520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | return id; |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2522 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2523 | err_wqueue: |
| 2524 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 2525 | destroy_workqueue(hdev->req_workqueue); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2526 | err: |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2527 | ida_simple_remove(&hci_index_ida, hdev->id); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2528 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2529 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | } |
| 2531 | EXPORT_SYMBOL(hci_register_dev); |
| 2532 | |
| 2533 | /* Unregister HCI device */ |
David Herrmann | 5973563 | 2011-10-26 10:43:19 +0200 | [diff] [blame] | 2534 | void hci_unregister_dev(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | { |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2536 | int i, id; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2537 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 2538 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 2540 | set_bit(HCI_UNREGISTER, &hdev->dev_flags); |
| 2541 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2542 | id = hdev->id; |
| 2543 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2544 | write_lock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | list_del(&hdev->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2546 | write_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | |
| 2548 | hci_dev_do_close(hdev); |
| 2549 | |
Suraj Sumangala | cd4c539 | 2010-07-14 13:02:16 +0530 | [diff] [blame] | 2550 | for (i = 0; i < NUM_REASSEMBLY; i++) |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2551 | kfree_skb(hdev->reassembly[i]); |
| 2552 | |
Gustavo Padovan | b9b5ef1 | 2012-11-21 00:50:21 -0200 | [diff] [blame] | 2553 | cancel_work_sync(&hdev->power_on); |
| 2554 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2555 | if (!test_bit(HCI_INIT, &hdev->flags) && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2556 | !test_bit(HCI_SETUP, &hdev->dev_flags)) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2557 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2558 | mgmt_index_removed(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2559 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2560 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2561 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2562 | /* mgmt_index_removed should take care of emptying the |
| 2563 | * pending list */ |
| 2564 | BUG_ON(!list_empty(&hdev->mgmt_pending)); |
| 2565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | hci_notify(hdev, HCI_DEV_UNREG); |
| 2567 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2568 | if (hdev->rfkill) { |
| 2569 | rfkill_unregister(hdev->rfkill); |
| 2570 | rfkill_destroy(hdev->rfkill); |
| 2571 | } |
| 2572 | |
David Herrmann | ce24297 | 2011-10-08 14:58:48 +0200 | [diff] [blame] | 2573 | hci_del_sysfs(hdev); |
Dave Young | 147e2d5 | 2008-03-05 18:45:59 -0800 | [diff] [blame] | 2574 | |
Marcel Holtmann | 0153e2e | 2013-10-17 17:24:17 -0700 | [diff] [blame^] | 2575 | debugfs_remove_recursive(hdev->debugfs); |
| 2576 | |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2577 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 2578 | destroy_workqueue(hdev->req_workqueue); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2579 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2580 | hci_dev_lock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 2581 | hci_blacklist_clear(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2582 | hci_uuids_clear(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2583 | hci_link_keys_clear(hdev); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2584 | hci_smp_ltks_clear(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2585 | hci_remote_oob_data_clear(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2586 | hci_dev_unlock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 2587 | |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 2588 | hci_dev_put(hdev); |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2589 | |
| 2590 | ida_simple_remove(&hci_index_ida, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | } |
| 2592 | EXPORT_SYMBOL(hci_unregister_dev); |
| 2593 | |
| 2594 | /* Suspend HCI device */ |
| 2595 | int hci_suspend_dev(struct hci_dev *hdev) |
| 2596 | { |
| 2597 | hci_notify(hdev, HCI_DEV_SUSPEND); |
| 2598 | return 0; |
| 2599 | } |
| 2600 | EXPORT_SYMBOL(hci_suspend_dev); |
| 2601 | |
| 2602 | /* Resume HCI device */ |
| 2603 | int hci_resume_dev(struct hci_dev *hdev) |
| 2604 | { |
| 2605 | hci_notify(hdev, HCI_DEV_RESUME); |
| 2606 | return 0; |
| 2607 | } |
| 2608 | EXPORT_SYMBOL(hci_resume_dev); |
| 2609 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2610 | /* Receive frame from HCI drivers */ |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 2611 | int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb) |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2612 | { |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2613 | if (!hdev || (!test_bit(HCI_UP, &hdev->flags) |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2614 | && !test_bit(HCI_INIT, &hdev->flags))) { |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2615 | kfree_skb(skb); |
| 2616 | return -ENXIO; |
| 2617 | } |
| 2618 | |
Jorrit Schippers | d82603c | 2012-12-27 17:33:02 +0100 | [diff] [blame] | 2619 | /* Incoming skb */ |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2620 | bt_cb(skb)->incoming = 1; |
| 2621 | |
| 2622 | /* Time stamp */ |
| 2623 | __net_timestamp(skb); |
| 2624 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2625 | skb_queue_tail(&hdev->rx_q, skb); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 2626 | queue_work(hdev->workqueue, &hdev->rx_work); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2627 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2628 | return 0; |
| 2629 | } |
| 2630 | EXPORT_SYMBOL(hci_recv_frame); |
| 2631 | |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2632 | static int hci_reassembly(struct hci_dev *hdev, int type, void *data, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2633 | int count, __u8 index) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2634 | { |
| 2635 | int len = 0; |
| 2636 | int hlen = 0; |
| 2637 | int remain = count; |
| 2638 | struct sk_buff *skb; |
| 2639 | struct bt_skb_cb *scb; |
| 2640 | |
| 2641 | if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2642 | index >= NUM_REASSEMBLY) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2643 | return -EILSEQ; |
| 2644 | |
| 2645 | skb = hdev->reassembly[index]; |
| 2646 | |
| 2647 | if (!skb) { |
| 2648 | switch (type) { |
| 2649 | case HCI_ACLDATA_PKT: |
| 2650 | len = HCI_MAX_FRAME_SIZE; |
| 2651 | hlen = HCI_ACL_HDR_SIZE; |
| 2652 | break; |
| 2653 | case HCI_EVENT_PKT: |
| 2654 | len = HCI_MAX_EVENT_SIZE; |
| 2655 | hlen = HCI_EVENT_HDR_SIZE; |
| 2656 | break; |
| 2657 | case HCI_SCODATA_PKT: |
| 2658 | len = HCI_MAX_SCO_SIZE; |
| 2659 | hlen = HCI_SCO_HDR_SIZE; |
| 2660 | break; |
| 2661 | } |
| 2662 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 2663 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2664 | if (!skb) |
| 2665 | return -ENOMEM; |
| 2666 | |
| 2667 | scb = (void *) skb->cb; |
| 2668 | scb->expect = hlen; |
| 2669 | scb->pkt_type = type; |
| 2670 | |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2671 | hdev->reassembly[index] = skb; |
| 2672 | } |
| 2673 | |
| 2674 | while (count) { |
| 2675 | scb = (void *) skb->cb; |
Dan Carpenter | 89bb46d | 2012-02-28 09:57:59 +0300 | [diff] [blame] | 2676 | len = min_t(uint, scb->expect, count); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2677 | |
| 2678 | memcpy(skb_put(skb, len), data, len); |
| 2679 | |
| 2680 | count -= len; |
| 2681 | data += len; |
| 2682 | scb->expect -= len; |
| 2683 | remain = count; |
| 2684 | |
| 2685 | switch (type) { |
| 2686 | case HCI_EVENT_PKT: |
| 2687 | if (skb->len == HCI_EVENT_HDR_SIZE) { |
| 2688 | struct hci_event_hdr *h = hci_event_hdr(skb); |
| 2689 | scb->expect = h->plen; |
| 2690 | |
| 2691 | if (skb_tailroom(skb) < scb->expect) { |
| 2692 | kfree_skb(skb); |
| 2693 | hdev->reassembly[index] = NULL; |
| 2694 | return -ENOMEM; |
| 2695 | } |
| 2696 | } |
| 2697 | break; |
| 2698 | |
| 2699 | case HCI_ACLDATA_PKT: |
| 2700 | if (skb->len == HCI_ACL_HDR_SIZE) { |
| 2701 | struct hci_acl_hdr *h = hci_acl_hdr(skb); |
| 2702 | scb->expect = __le16_to_cpu(h->dlen); |
| 2703 | |
| 2704 | if (skb_tailroom(skb) < scb->expect) { |
| 2705 | kfree_skb(skb); |
| 2706 | hdev->reassembly[index] = NULL; |
| 2707 | return -ENOMEM; |
| 2708 | } |
| 2709 | } |
| 2710 | break; |
| 2711 | |
| 2712 | case HCI_SCODATA_PKT: |
| 2713 | if (skb->len == HCI_SCO_HDR_SIZE) { |
| 2714 | struct hci_sco_hdr *h = hci_sco_hdr(skb); |
| 2715 | scb->expect = h->dlen; |
| 2716 | |
| 2717 | if (skb_tailroom(skb) < scb->expect) { |
| 2718 | kfree_skb(skb); |
| 2719 | hdev->reassembly[index] = NULL; |
| 2720 | return -ENOMEM; |
| 2721 | } |
| 2722 | } |
| 2723 | break; |
| 2724 | } |
| 2725 | |
| 2726 | if (scb->expect == 0) { |
| 2727 | /* Complete frame */ |
| 2728 | |
| 2729 | bt_cb(skb)->pkt_type = type; |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 2730 | hci_recv_frame(hdev, skb); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2731 | |
| 2732 | hdev->reassembly[index] = NULL; |
| 2733 | return remain; |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | return remain; |
| 2738 | } |
| 2739 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2740 | int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) |
| 2741 | { |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2742 | int rem = 0; |
| 2743 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2744 | if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) |
| 2745 | return -EILSEQ; |
| 2746 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 2747 | while (count) { |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 2748 | rem = hci_reassembly(hdev, type, data, count, type - 1); |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2749 | if (rem < 0) |
| 2750 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2751 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2752 | data += (count - rem); |
| 2753 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 2754 | } |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2755 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2756 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2757 | } |
| 2758 | EXPORT_SYMBOL(hci_recv_fragment); |
| 2759 | |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2760 | #define STREAM_REASSEMBLY 0 |
| 2761 | |
| 2762 | int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count) |
| 2763 | { |
| 2764 | int type; |
| 2765 | int rem = 0; |
| 2766 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 2767 | while (count) { |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2768 | struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY]; |
| 2769 | |
| 2770 | if (!skb) { |
| 2771 | struct { char type; } *pkt; |
| 2772 | |
| 2773 | /* Start of the frame */ |
| 2774 | pkt = data; |
| 2775 | type = pkt->type; |
| 2776 | |
| 2777 | data++; |
| 2778 | count--; |
| 2779 | } else |
| 2780 | type = bt_cb(skb)->pkt_type; |
| 2781 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 2782 | rem = hci_reassembly(hdev, type, data, count, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2783 | STREAM_REASSEMBLY); |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2784 | if (rem < 0) |
| 2785 | return rem; |
| 2786 | |
| 2787 | data += (count - rem); |
| 2788 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 2789 | } |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2790 | |
| 2791 | return rem; |
| 2792 | } |
| 2793 | EXPORT_SYMBOL(hci_recv_stream_fragment); |
| 2794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | /* ---- Interface to upper protocols ---- */ |
| 2796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | int hci_register_cb(struct hci_cb *cb) |
| 2798 | { |
| 2799 | BT_DBG("%p name %s", cb, cb->name); |
| 2800 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2801 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | list_add(&cb->list, &hci_cb_list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2803 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | |
| 2805 | return 0; |
| 2806 | } |
| 2807 | EXPORT_SYMBOL(hci_register_cb); |
| 2808 | |
| 2809 | int hci_unregister_cb(struct hci_cb *cb) |
| 2810 | { |
| 2811 | BT_DBG("%p name %s", cb, cb->name); |
| 2812 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2813 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | list_del(&cb->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2815 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | |
| 2817 | return 0; |
| 2818 | } |
| 2819 | EXPORT_SYMBOL(hci_unregister_cb); |
| 2820 | |
Marcel Holtmann | 5108699 | 2013-10-10 14:54:19 -0700 | [diff] [blame] | 2821 | static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | { |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2823 | BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 2825 | /* Time stamp */ |
| 2826 | __net_timestamp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 2828 | /* Send copy to monitor */ |
| 2829 | hci_send_to_monitor(hdev, skb); |
| 2830 | |
| 2831 | if (atomic_read(&hdev->promisc)) { |
| 2832 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 2833 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 2837 | skb_orphan(skb); |
| 2838 | |
Marcel Holtmann | 7bd8f09 | 2013-10-11 06:19:18 -0700 | [diff] [blame] | 2839 | if (hdev->send(hdev, skb) < 0) |
Marcel Holtmann | 5108699 | 2013-10-10 14:54:19 -0700 | [diff] [blame] | 2840 | BT_ERR("%s sending frame failed", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 2843 | void hci_req_init(struct hci_request *req, struct hci_dev *hdev) |
| 2844 | { |
| 2845 | skb_queue_head_init(&req->cmd_q); |
| 2846 | req->hdev = hdev; |
Andre Guedes | 5d73e03 | 2013-03-08 11:20:16 -0300 | [diff] [blame] | 2847 | req->err = 0; |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | int hci_req_run(struct hci_request *req, hci_req_complete_t complete) |
| 2851 | { |
| 2852 | struct hci_dev *hdev = req->hdev; |
| 2853 | struct sk_buff *skb; |
| 2854 | unsigned long flags; |
| 2855 | |
| 2856 | BT_DBG("length %u", skb_queue_len(&req->cmd_q)); |
| 2857 | |
Andre Guedes | 5d73e03 | 2013-03-08 11:20:16 -0300 | [diff] [blame] | 2858 | /* If an error occured during request building, remove all HCI |
| 2859 | * commands queued on the HCI request queue. |
| 2860 | */ |
| 2861 | if (req->err) { |
| 2862 | skb_queue_purge(&req->cmd_q); |
| 2863 | return req->err; |
| 2864 | } |
| 2865 | |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 2866 | /* Do not allow empty requests */ |
| 2867 | if (skb_queue_empty(&req->cmd_q)) |
Andre Guedes | 382b0c3 | 2013-03-08 11:20:14 -0300 | [diff] [blame] | 2868 | return -ENODATA; |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 2869 | |
| 2870 | skb = skb_peek_tail(&req->cmd_q); |
| 2871 | bt_cb(skb)->req.complete = complete; |
| 2872 | |
| 2873 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 2874 | skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); |
| 2875 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 2876 | |
| 2877 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 2878 | |
| 2879 | return 0; |
| 2880 | } |
| 2881 | |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2882 | static struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 2883 | u32 plen, const void *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | { |
| 2885 | int len = HCI_COMMAND_HDR_SIZE + plen; |
| 2886 | struct hci_command_hdr *hdr; |
| 2887 | struct sk_buff *skb; |
| 2888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2890 | if (!skb) |
| 2891 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | |
| 2893 | hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2894 | hdr->opcode = cpu_to_le16(opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | hdr->plen = plen; |
| 2896 | |
| 2897 | if (plen) |
| 2898 | memcpy(skb_put(skb, plen), param, plen); |
| 2899 | |
| 2900 | BT_DBG("skb len %d", skb->len); |
| 2901 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2902 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2903 | |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2904 | return skb; |
| 2905 | } |
| 2906 | |
| 2907 | /* Send HCI command */ |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 2908 | int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, |
| 2909 | const void *param) |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2910 | { |
| 2911 | struct sk_buff *skb; |
| 2912 | |
| 2913 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 2914 | |
| 2915 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 2916 | if (!skb) { |
| 2917 | BT_ERR("%s no memory for command", hdev->name); |
| 2918 | return -ENOMEM; |
| 2919 | } |
| 2920 | |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 2921 | /* Stand-alone HCI commands must be flaged as |
| 2922 | * single-command requests. |
| 2923 | */ |
| 2924 | bt_cb(skb)->req.start = true; |
| 2925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2927 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | |
| 2929 | return 0; |
| 2930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2932 | /* Queue a command to an asynchronous HCI request */ |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 2933 | void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, |
| 2934 | const void *param, u8 event) |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2935 | { |
| 2936 | struct hci_dev *hdev = req->hdev; |
| 2937 | struct sk_buff *skb; |
| 2938 | |
| 2939 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 2940 | |
Andre Guedes | 34739c1 | 2013-03-08 11:20:18 -0300 | [diff] [blame] | 2941 | /* If an error occured during request building, there is no point in |
| 2942 | * queueing the HCI command. We can simply return. |
| 2943 | */ |
| 2944 | if (req->err) |
| 2945 | return; |
| 2946 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2947 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 2948 | if (!skb) { |
Andre Guedes | 5d73e03 | 2013-03-08 11:20:16 -0300 | [diff] [blame] | 2949 | BT_ERR("%s no memory for command (opcode 0x%4.4x)", |
| 2950 | hdev->name, opcode); |
| 2951 | req->err = -ENOMEM; |
Andre Guedes | e348fe6 | 2013-03-08 11:20:17 -0300 | [diff] [blame] | 2952 | return; |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | if (skb_queue_empty(&req->cmd_q)) |
| 2956 | bt_cb(skb)->req.start = true; |
| 2957 | |
Johan Hedberg | 02350a7 | 2013-04-03 21:50:29 +0300 | [diff] [blame] | 2958 | bt_cb(skb)->req.event = event; |
| 2959 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2960 | skb_queue_tail(&req->cmd_q, skb); |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2961 | } |
| 2962 | |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 2963 | void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, |
| 2964 | const void *param) |
Johan Hedberg | 02350a7 | 2013-04-03 21:50:29 +0300 | [diff] [blame] | 2965 | { |
| 2966 | hci_req_add_ev(req, opcode, plen, param, 0); |
| 2967 | } |
| 2968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | /* Get data from the previously sent command */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2970 | void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | { |
| 2972 | struct hci_command_hdr *hdr; |
| 2973 | |
| 2974 | if (!hdev->sent_cmd) |
| 2975 | return NULL; |
| 2976 | |
| 2977 | hdr = (void *) hdev->sent_cmd->data; |
| 2978 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2979 | if (hdr->opcode != cpu_to_le16(opcode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | return NULL; |
| 2981 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 2982 | BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | |
| 2984 | return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; |
| 2985 | } |
| 2986 | |
| 2987 | /* Send ACL data */ |
| 2988 | static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) |
| 2989 | { |
| 2990 | struct hci_acl_hdr *hdr; |
| 2991 | int len = skb->len; |
| 2992 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 2993 | skb_push(skb, HCI_ACL_HDR_SIZE); |
| 2994 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 2995 | hdr = (struct hci_acl_hdr *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2996 | hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); |
| 2997 | hdr->dlen = cpu_to_le16(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | } |
| 2999 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3000 | static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3001 | struct sk_buff *skb, __u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3003 | struct hci_conn *conn = chan->conn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | struct hci_dev *hdev = conn->hdev; |
| 3005 | struct sk_buff *list; |
| 3006 | |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 3007 | skb->len = skb_headlen(skb); |
| 3008 | skb->data_len = 0; |
| 3009 | |
| 3010 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | 204a6e5 | 2012-10-15 11:58:39 +0300 | [diff] [blame] | 3011 | |
| 3012 | switch (hdev->dev_type) { |
| 3013 | case HCI_BREDR: |
| 3014 | hci_add_acl_hdr(skb, conn->handle, flags); |
| 3015 | break; |
| 3016 | case HCI_AMP: |
| 3017 | hci_add_acl_hdr(skb, chan->handle, flags); |
| 3018 | break; |
| 3019 | default: |
| 3020 | BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type); |
| 3021 | return; |
| 3022 | } |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 3023 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 3024 | list = skb_shinfo(skb)->frag_list; |
| 3025 | if (!list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | /* Non fragmented */ |
| 3027 | BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); |
| 3028 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3029 | skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | } else { |
| 3031 | /* Fragmented */ |
| 3032 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 3033 | |
| 3034 | skb_shinfo(skb)->frag_list = NULL; |
| 3035 | |
| 3036 | /* Queue all fragments atomically */ |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 3037 | spin_lock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3039 | __skb_queue_tail(queue, skb); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 3040 | |
| 3041 | flags &= ~ACL_START; |
| 3042 | flags |= ACL_CONT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | do { |
| 3044 | skb = list; list = list->next; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3045 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3046 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 3047 | hci_add_acl_hdr(skb, conn->handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | |
| 3049 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 3050 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3051 | __skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | } while (list); |
| 3053 | |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 3054 | spin_unlock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) |
| 3059 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3060 | struct hci_dev *hdev = chan->conn->hdev; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3061 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3062 | BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3063 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3064 | hci_queue_acl(chan, &chan->data_q, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3066 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | |
| 3069 | /* Send SCO data */ |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 3070 | void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | { |
| 3072 | struct hci_dev *hdev = conn->hdev; |
| 3073 | struct hci_sco_hdr hdr; |
| 3074 | |
| 3075 | BT_DBG("%s len %d", hdev->name, skb->len); |
| 3076 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3077 | hdr.handle = cpu_to_le16(conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | hdr.dlen = skb->len; |
| 3079 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 3080 | skb_push(skb, HCI_SCO_HDR_SIZE); |
| 3081 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 3082 | memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3084 | bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 3085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | skb_queue_tail(&conn->data_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3087 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | |
| 3090 | /* ---- HCI TX task (outgoing data) ---- */ |
| 3091 | |
| 3092 | /* HCI Connection scheduler */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3093 | static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, |
| 3094 | int *quote) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | { |
| 3096 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3097 | struct hci_conn *conn = NULL, *c; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 3098 | unsigned int num = 0, min = ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3100 | /* We don't have to lock device here. Connections are always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | * added and removed with TX task disabled. */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3102 | |
| 3103 | rcu_read_lock(); |
| 3104 | |
| 3105 | list_for_each_entry_rcu(c, &h->list, list) { |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 3106 | if (c->type != type || skb_queue_empty(&c->data_q)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | continue; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 3108 | |
| 3109 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) |
| 3110 | continue; |
| 3111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | num++; |
| 3113 | |
| 3114 | if (c->sent < min) { |
| 3115 | min = c->sent; |
| 3116 | conn = c; |
| 3117 | } |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3118 | |
| 3119 | if (hci_conn_num(hdev, type) == num) |
| 3120 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | } |
| 3122 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3123 | rcu_read_unlock(); |
| 3124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | if (conn) { |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3126 | int cnt, q; |
| 3127 | |
| 3128 | switch (conn->type) { |
| 3129 | case ACL_LINK: |
| 3130 | cnt = hdev->acl_cnt; |
| 3131 | break; |
| 3132 | case SCO_LINK: |
| 3133 | case ESCO_LINK: |
| 3134 | cnt = hdev->sco_cnt; |
| 3135 | break; |
| 3136 | case LE_LINK: |
| 3137 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 3138 | break; |
| 3139 | default: |
| 3140 | cnt = 0; |
| 3141 | BT_ERR("Unknown link type"); |
| 3142 | } |
| 3143 | |
| 3144 | q = cnt / num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | *quote = q ? q : 1; |
| 3146 | } else |
| 3147 | *quote = 0; |
| 3148 | |
| 3149 | BT_DBG("conn %p quote %d", conn, *quote); |
| 3150 | return conn; |
| 3151 | } |
| 3152 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3153 | static void hci_link_tx_to(struct hci_dev *hdev, __u8 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | { |
| 3155 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3156 | struct hci_conn *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3158 | BT_ERR("%s link tx timeout", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3160 | rcu_read_lock(); |
| 3161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | /* Kill stalled connections */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3163 | list_for_each_entry_rcu(c, &h->list, list) { |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3164 | if (c->type == type && c->sent) { |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 3165 | BT_ERR("%s killing stalled connection %pMR", |
| 3166 | hdev->name, &c->dst); |
Andre Guedes | bed7174 | 2013-01-30 11:50:56 -0300 | [diff] [blame] | 3167 | hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | } |
| 3169 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3170 | |
| 3171 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | } |
| 3173 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3174 | static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, |
| 3175 | int *quote) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3176 | { |
| 3177 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 3178 | struct hci_chan *chan = NULL; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 3179 | unsigned int num = 0, min = ~0, cur_prio = 0; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3180 | struct hci_conn *conn; |
| 3181 | int cnt, q, conn_num = 0; |
| 3182 | |
| 3183 | BT_DBG("%s", hdev->name); |
| 3184 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3185 | rcu_read_lock(); |
| 3186 | |
| 3187 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3188 | struct hci_chan *tmp; |
| 3189 | |
| 3190 | if (conn->type != type) |
| 3191 | continue; |
| 3192 | |
| 3193 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 3194 | continue; |
| 3195 | |
| 3196 | conn_num++; |
| 3197 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 3198 | list_for_each_entry_rcu(tmp, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3199 | struct sk_buff *skb; |
| 3200 | |
| 3201 | if (skb_queue_empty(&tmp->data_q)) |
| 3202 | continue; |
| 3203 | |
| 3204 | skb = skb_peek(&tmp->data_q); |
| 3205 | if (skb->priority < cur_prio) |
| 3206 | continue; |
| 3207 | |
| 3208 | if (skb->priority > cur_prio) { |
| 3209 | num = 0; |
| 3210 | min = ~0; |
| 3211 | cur_prio = skb->priority; |
| 3212 | } |
| 3213 | |
| 3214 | num++; |
| 3215 | |
| 3216 | if (conn->sent < min) { |
| 3217 | min = conn->sent; |
| 3218 | chan = tmp; |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | if (hci_conn_num(hdev, type) == conn_num) |
| 3223 | break; |
| 3224 | } |
| 3225 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3226 | rcu_read_unlock(); |
| 3227 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3228 | if (!chan) |
| 3229 | return NULL; |
| 3230 | |
| 3231 | switch (chan->conn->type) { |
| 3232 | case ACL_LINK: |
| 3233 | cnt = hdev->acl_cnt; |
| 3234 | break; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3235 | case AMP_LINK: |
| 3236 | cnt = hdev->block_cnt; |
| 3237 | break; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3238 | case SCO_LINK: |
| 3239 | case ESCO_LINK: |
| 3240 | cnt = hdev->sco_cnt; |
| 3241 | break; |
| 3242 | case LE_LINK: |
| 3243 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 3244 | break; |
| 3245 | default: |
| 3246 | cnt = 0; |
| 3247 | BT_ERR("Unknown link type"); |
| 3248 | } |
| 3249 | |
| 3250 | q = cnt / num; |
| 3251 | *quote = q ? q : 1; |
| 3252 | BT_DBG("chan %p quote %d", chan, *quote); |
| 3253 | return chan; |
| 3254 | } |
| 3255 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3256 | static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) |
| 3257 | { |
| 3258 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 3259 | struct hci_conn *conn; |
| 3260 | int num = 0; |
| 3261 | |
| 3262 | BT_DBG("%s", hdev->name); |
| 3263 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3264 | rcu_read_lock(); |
| 3265 | |
| 3266 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3267 | struct hci_chan *chan; |
| 3268 | |
| 3269 | if (conn->type != type) |
| 3270 | continue; |
| 3271 | |
| 3272 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 3273 | continue; |
| 3274 | |
| 3275 | num++; |
| 3276 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 3277 | list_for_each_entry_rcu(chan, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3278 | struct sk_buff *skb; |
| 3279 | |
| 3280 | if (chan->sent) { |
| 3281 | chan->sent = 0; |
| 3282 | continue; |
| 3283 | } |
| 3284 | |
| 3285 | if (skb_queue_empty(&chan->data_q)) |
| 3286 | continue; |
| 3287 | |
| 3288 | skb = skb_peek(&chan->data_q); |
| 3289 | if (skb->priority >= HCI_PRIO_MAX - 1) |
| 3290 | continue; |
| 3291 | |
| 3292 | skb->priority = HCI_PRIO_MAX - 1; |
| 3293 | |
| 3294 | BT_DBG("chan %p skb %p promoted to %d", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3295 | skb->priority); |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | if (hci_conn_num(hdev, type) == num) |
| 3299 | break; |
| 3300 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3301 | |
| 3302 | rcu_read_unlock(); |
| 3303 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3304 | } |
| 3305 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3306 | static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb) |
| 3307 | { |
| 3308 | /* Calculate count of blocks used by this packet */ |
| 3309 | return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len); |
| 3310 | } |
| 3311 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3312 | static void __check_timeout(struct hci_dev *hdev, unsigned int cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 3315 | /* ACL tx timeout must be longer than maximum |
| 3316 | * link supervision timeout (40.9 seconds) */ |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3317 | if (!cnt && time_after(jiffies, hdev->acl_last_tx + |
Andrei Emeltchenko | 5f246e8 | 2012-06-11 11:13:07 +0300 | [diff] [blame] | 3318 | HCI_ACL_TX_TIMEOUT)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3319 | hci_link_tx_to(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | } |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3323 | static void hci_sched_acl_pkt(struct hci_dev *hdev) |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3324 | { |
| 3325 | unsigned int cnt = hdev->acl_cnt; |
| 3326 | struct hci_chan *chan; |
| 3327 | struct sk_buff *skb; |
| 3328 | int quote; |
| 3329 | |
| 3330 | __check_timeout(hdev, cnt); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3331 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3332 | while (hdev->acl_cnt && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3333 | (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3334 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3335 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3336 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3337 | skb->len, skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3338 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3339 | /* Stop if priority has changed */ |
| 3340 | if (skb->priority < priority) |
| 3341 | break; |
| 3342 | |
| 3343 | skb = skb_dequeue(&chan->data_q); |
| 3344 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3345 | hci_conn_enter_active_mode(chan->conn, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3346 | bt_cb(skb)->force_active); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3347 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3348 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | hdev->acl_last_tx = jiffies; |
| 3350 | |
| 3351 | hdev->acl_cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3352 | chan->sent++; |
| 3353 | chan->conn->sent++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | } |
| 3355 | } |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3356 | |
| 3357 | if (cnt != hdev->acl_cnt) |
| 3358 | hci_prio_recalculate(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | } |
| 3360 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3361 | static void hci_sched_acl_blk(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3362 | { |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3363 | unsigned int cnt = hdev->block_cnt; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3364 | struct hci_chan *chan; |
| 3365 | struct sk_buff *skb; |
| 3366 | int quote; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3367 | u8 type; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3368 | |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3369 | __check_timeout(hdev, cnt); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3370 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3371 | BT_DBG("%s", hdev->name); |
| 3372 | |
| 3373 | if (hdev->dev_type == HCI_AMP) |
| 3374 | type = AMP_LINK; |
| 3375 | else |
| 3376 | type = ACL_LINK; |
| 3377 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3378 | while (hdev->block_cnt > 0 && |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3379 | (chan = hci_chan_sent(hdev, type, "e))) { |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3380 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3381 | while (quote > 0 && (skb = skb_peek(&chan->data_q))) { |
| 3382 | int blocks; |
| 3383 | |
| 3384 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3385 | skb->len, skb->priority); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3386 | |
| 3387 | /* Stop if priority has changed */ |
| 3388 | if (skb->priority < priority) |
| 3389 | break; |
| 3390 | |
| 3391 | skb = skb_dequeue(&chan->data_q); |
| 3392 | |
| 3393 | blocks = __get_blocks(hdev, skb); |
| 3394 | if (blocks > hdev->block_cnt) |
| 3395 | return; |
| 3396 | |
| 3397 | hci_conn_enter_active_mode(chan->conn, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3398 | bt_cb(skb)->force_active); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3399 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3400 | hci_send_frame(hdev, skb); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3401 | hdev->acl_last_tx = jiffies; |
| 3402 | |
| 3403 | hdev->block_cnt -= blocks; |
| 3404 | quote -= blocks; |
| 3405 | |
| 3406 | chan->sent += blocks; |
| 3407 | chan->conn->sent += blocks; |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | if (cnt != hdev->block_cnt) |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3412 | hci_prio_recalculate(hdev, type); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3413 | } |
| 3414 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3415 | static void hci_sched_acl(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3416 | { |
| 3417 | BT_DBG("%s", hdev->name); |
| 3418 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3419 | /* No ACL link over BR/EDR controller */ |
| 3420 | if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR) |
| 3421 | return; |
| 3422 | |
| 3423 | /* No AMP link over AMP controller */ |
| 3424 | if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3425 | return; |
| 3426 | |
| 3427 | switch (hdev->flow_ctl_mode) { |
| 3428 | case HCI_FLOW_CTL_MODE_PACKET_BASED: |
| 3429 | hci_sched_acl_pkt(hdev); |
| 3430 | break; |
| 3431 | |
| 3432 | case HCI_FLOW_CTL_MODE_BLOCK_BASED: |
| 3433 | hci_sched_acl_blk(hdev); |
| 3434 | break; |
| 3435 | } |
| 3436 | } |
| 3437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | /* Schedule SCO */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3439 | static void hci_sched_sco(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | { |
| 3441 | struct hci_conn *conn; |
| 3442 | struct sk_buff *skb; |
| 3443 | int quote; |
| 3444 | |
| 3445 | BT_DBG("%s", hdev->name); |
| 3446 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3447 | if (!hci_conn_num(hdev, SCO_LINK)) |
| 3448 | return; |
| 3449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { |
| 3451 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 3452 | BT_DBG("skb %p len %d", skb, skb->len); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3453 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | |
| 3455 | conn->sent++; |
| 3456 | if (conn->sent == ~0) |
| 3457 | conn->sent = 0; |
| 3458 | } |
| 3459 | } |
| 3460 | } |
| 3461 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3462 | static void hci_sched_esco(struct hci_dev *hdev) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3463 | { |
| 3464 | struct hci_conn *conn; |
| 3465 | struct sk_buff *skb; |
| 3466 | int quote; |
| 3467 | |
| 3468 | BT_DBG("%s", hdev->name); |
| 3469 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3470 | if (!hci_conn_num(hdev, ESCO_LINK)) |
| 3471 | return; |
| 3472 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 3473 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, |
| 3474 | "e))) { |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3475 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 3476 | BT_DBG("skb %p len %d", skb, skb->len); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3477 | hci_send_frame(hdev, skb); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3478 | |
| 3479 | conn->sent++; |
| 3480 | if (conn->sent == ~0) |
| 3481 | conn->sent = 0; |
| 3482 | } |
| 3483 | } |
| 3484 | } |
| 3485 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3486 | static void hci_sched_le(struct hci_dev *hdev) |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3487 | { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3488 | struct hci_chan *chan; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3489 | struct sk_buff *skb; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3490 | int quote, cnt, tmp; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3491 | |
| 3492 | BT_DBG("%s", hdev->name); |
| 3493 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3494 | if (!hci_conn_num(hdev, LE_LINK)) |
| 3495 | return; |
| 3496 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3497 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 3498 | /* LE tx timeout must be longer than maximum |
| 3499 | * link supervision timeout (40.9 seconds) */ |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3500 | if (!hdev->le_cnt && hdev->le_pkts && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3501 | time_after(jiffies, hdev->le_last_tx + HZ * 45)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3502 | hci_link_tx_to(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3506 | tmp = cnt; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3507 | while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3508 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3509 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3510 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3511 | skb->len, skb->priority); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3512 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3513 | /* Stop if priority has changed */ |
| 3514 | if (skb->priority < priority) |
| 3515 | break; |
| 3516 | |
| 3517 | skb = skb_dequeue(&chan->data_q); |
| 3518 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3519 | hci_send_frame(hdev, skb); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3520 | hdev->le_last_tx = jiffies; |
| 3521 | |
| 3522 | cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3523 | chan->sent++; |
| 3524 | chan->conn->sent++; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3525 | } |
| 3526 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3527 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3528 | if (hdev->le_pkts) |
| 3529 | hdev->le_cnt = cnt; |
| 3530 | else |
| 3531 | hdev->acl_cnt = cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3532 | |
| 3533 | if (cnt != tmp) |
| 3534 | hci_prio_recalculate(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3535 | } |
| 3536 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3537 | static void hci_tx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | { |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3539 | struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | struct sk_buff *skb; |
| 3541 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3542 | BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3543 | hdev->sco_cnt, hdev->le_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | |
Marcel Holtmann | 52de599 | 2013-09-03 18:08:38 -0700 | [diff] [blame] | 3545 | if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 3546 | /* Schedule queues and send stuff to HCI driver */ |
| 3547 | hci_sched_acl(hdev); |
| 3548 | hci_sched_sco(hdev); |
| 3549 | hci_sched_esco(hdev); |
| 3550 | hci_sched_le(hdev); |
| 3551 | } |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | /* Send next queued raw (unknown type) packet */ |
| 3554 | while ((skb = skb_dequeue(&hdev->raw_q))) |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3555 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | } |
| 3557 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3558 | /* ----- HCI RX task (incoming data processing) ----- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | |
| 3560 | /* ACL data packet */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3561 | static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | { |
| 3563 | struct hci_acl_hdr *hdr = (void *) skb->data; |
| 3564 | struct hci_conn *conn; |
| 3565 | __u16 handle, flags; |
| 3566 | |
| 3567 | skb_pull(skb, HCI_ACL_HDR_SIZE); |
| 3568 | |
| 3569 | handle = __le16_to_cpu(hdr->handle); |
| 3570 | flags = hci_flags(handle); |
| 3571 | handle = hci_handle(handle); |
| 3572 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3573 | BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3574 | handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3575 | |
| 3576 | hdev->stat.acl_rx++; |
| 3577 | |
| 3578 | hci_dev_lock(hdev); |
| 3579 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 3580 | hci_dev_unlock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | if (conn) { |
Mat Martineau | 65983fc | 2011-12-13 15:06:02 -0800 | [diff] [blame] | 3583 | hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3585 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 3586 | l2cap_recv_acldata(conn, skb, flags); |
| 3587 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3589 | BT_ERR("%s ACL packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3590 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | kfree_skb(skb); |
| 3594 | } |
| 3595 | |
| 3596 | /* SCO data packet */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3597 | static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | { |
| 3599 | struct hci_sco_hdr *hdr = (void *) skb->data; |
| 3600 | struct hci_conn *conn; |
| 3601 | __u16 handle; |
| 3602 | |
| 3603 | skb_pull(skb, HCI_SCO_HDR_SIZE); |
| 3604 | |
| 3605 | handle = __le16_to_cpu(hdr->handle); |
| 3606 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3607 | BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3608 | |
| 3609 | hdev->stat.sco_rx++; |
| 3610 | |
| 3611 | hci_dev_lock(hdev); |
| 3612 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 3613 | hci_dev_unlock(hdev); |
| 3614 | |
| 3615 | if (conn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 3617 | sco_recv_scodata(conn, skb); |
| 3618 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3620 | BT_ERR("%s SCO packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3621 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | } |
| 3623 | |
| 3624 | kfree_skb(skb); |
| 3625 | } |
| 3626 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3627 | static bool hci_req_is_complete(struct hci_dev *hdev) |
| 3628 | { |
| 3629 | struct sk_buff *skb; |
| 3630 | |
| 3631 | skb = skb_peek(&hdev->cmd_q); |
| 3632 | if (!skb) |
| 3633 | return true; |
| 3634 | |
| 3635 | return bt_cb(skb)->req.start; |
| 3636 | } |
| 3637 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3638 | static void hci_resend_last(struct hci_dev *hdev) |
| 3639 | { |
| 3640 | struct hci_command_hdr *sent; |
| 3641 | struct sk_buff *skb; |
| 3642 | u16 opcode; |
| 3643 | |
| 3644 | if (!hdev->sent_cmd) |
| 3645 | return; |
| 3646 | |
| 3647 | sent = (void *) hdev->sent_cmd->data; |
| 3648 | opcode = __le16_to_cpu(sent->opcode); |
| 3649 | if (opcode == HCI_OP_RESET) |
| 3650 | return; |
| 3651 | |
| 3652 | skb = skb_clone(hdev->sent_cmd, GFP_KERNEL); |
| 3653 | if (!skb) |
| 3654 | return; |
| 3655 | |
| 3656 | skb_queue_head(&hdev->cmd_q, skb); |
| 3657 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 3658 | } |
| 3659 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3660 | void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status) |
| 3661 | { |
| 3662 | hci_req_complete_t req_complete = NULL; |
| 3663 | struct sk_buff *skb; |
| 3664 | unsigned long flags; |
| 3665 | |
| 3666 | BT_DBG("opcode 0x%04x status 0x%02x", opcode, status); |
| 3667 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3668 | /* If the completed command doesn't match the last one that was |
| 3669 | * sent we need to do special handling of it. |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3670 | */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3671 | if (!hci_sent_cmd_data(hdev, opcode)) { |
| 3672 | /* Some CSR based controllers generate a spontaneous |
| 3673 | * reset complete event during init and any pending |
| 3674 | * command will never be completed. In such a case we |
| 3675 | * need to resend whatever was the last sent |
| 3676 | * command. |
| 3677 | */ |
| 3678 | if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET) |
| 3679 | hci_resend_last(hdev); |
| 3680 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3681 | return; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3682 | } |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3683 | |
| 3684 | /* If the command succeeded and there's still more commands in |
| 3685 | * this request the request is not yet complete. |
| 3686 | */ |
| 3687 | if (!status && !hci_req_is_complete(hdev)) |
| 3688 | return; |
| 3689 | |
| 3690 | /* If this was the last command in a request the complete |
| 3691 | * callback would be found in hdev->sent_cmd instead of the |
| 3692 | * command queue (hdev->cmd_q). |
| 3693 | */ |
| 3694 | if (hdev->sent_cmd) { |
| 3695 | req_complete = bt_cb(hdev->sent_cmd)->req.complete; |
Johan Hedberg | 53e21fb | 2013-07-27 14:11:14 -0500 | [diff] [blame] | 3696 | |
| 3697 | if (req_complete) { |
| 3698 | /* We must set the complete callback to NULL to |
| 3699 | * avoid calling the callback more than once if |
| 3700 | * this function gets called again. |
| 3701 | */ |
| 3702 | bt_cb(hdev->sent_cmd)->req.complete = NULL; |
| 3703 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3704 | goto call_complete; |
Johan Hedberg | 53e21fb | 2013-07-27 14:11:14 -0500 | [diff] [blame] | 3705 | } |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | /* Remove all pending commands belonging to this request */ |
| 3709 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 3710 | while ((skb = __skb_dequeue(&hdev->cmd_q))) { |
| 3711 | if (bt_cb(skb)->req.start) { |
| 3712 | __skb_queue_head(&hdev->cmd_q, skb); |
| 3713 | break; |
| 3714 | } |
| 3715 | |
| 3716 | req_complete = bt_cb(skb)->req.complete; |
| 3717 | kfree_skb(skb); |
| 3718 | } |
| 3719 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 3720 | |
| 3721 | call_complete: |
| 3722 | if (req_complete) |
| 3723 | req_complete(hdev, status); |
| 3724 | } |
| 3725 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3726 | static void hci_rx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | { |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3728 | struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | struct sk_buff *skb; |
| 3730 | |
| 3731 | BT_DBG("%s", hdev->name); |
| 3732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | while ((skb = skb_dequeue(&hdev->rx_q))) { |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 3734 | /* Send copy to monitor */ |
| 3735 | hci_send_to_monitor(hdev, skb); |
| 3736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | if (atomic_read(&hdev->promisc)) { |
| 3738 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 3739 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | } |
| 3741 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 3742 | if (test_bit(HCI_RAW, &hdev->flags) || |
| 3743 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | kfree_skb(skb); |
| 3745 | continue; |
| 3746 | } |
| 3747 | |
| 3748 | if (test_bit(HCI_INIT, &hdev->flags)) { |
| 3749 | /* Don't process data packets in this states. */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3750 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | case HCI_ACLDATA_PKT: |
| 3752 | case HCI_SCODATA_PKT: |
| 3753 | kfree_skb(skb); |
| 3754 | continue; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3755 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | /* Process frame */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3759 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | case HCI_EVENT_PKT: |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3761 | BT_DBG("%s Event packet", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | hci_event_packet(hdev, skb); |
| 3763 | break; |
| 3764 | |
| 3765 | case HCI_ACLDATA_PKT: |
| 3766 | BT_DBG("%s ACL data packet", hdev->name); |
| 3767 | hci_acldata_packet(hdev, skb); |
| 3768 | break; |
| 3769 | |
| 3770 | case HCI_SCODATA_PKT: |
| 3771 | BT_DBG("%s SCO data packet", hdev->name); |
| 3772 | hci_scodata_packet(hdev, skb); |
| 3773 | break; |
| 3774 | |
| 3775 | default: |
| 3776 | kfree_skb(skb); |
| 3777 | break; |
| 3778 | } |
| 3779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | } |
| 3781 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3782 | static void hci_cmd_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | { |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3784 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3785 | struct sk_buff *skb; |
| 3786 | |
Andrei Emeltchenko | 2104786 | 2012-07-10 15:27:47 +0300 | [diff] [blame] | 3787 | BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name, |
| 3788 | atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | /* Send queued commands */ |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 3791 | if (atomic_read(&hdev->cmd_cnt)) { |
| 3792 | skb = skb_dequeue(&hdev->cmd_q); |
| 3793 | if (!skb) |
| 3794 | return; |
| 3795 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 3796 | kfree_skb(hdev->sent_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3797 | |
Marcel Holtmann | a675d7f | 2013-09-03 18:11:07 -0700 | [diff] [blame] | 3798 | hdev->sent_cmd = skb_clone(skb, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 3799 | if (hdev->sent_cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3800 | atomic_dec(&hdev->cmd_cnt); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3801 | hci_send_frame(hdev, skb); |
Szymon Janc | 7bdb8a5 | 2011-07-26 22:46:54 +0200 | [diff] [blame] | 3802 | if (test_bit(HCI_RESET, &hdev->flags)) |
| 3803 | del_timer(&hdev->cmd_timer); |
| 3804 | else |
| 3805 | mod_timer(&hdev->cmd_timer, |
Andrei Emeltchenko | 5f246e8 | 2012-06-11 11:13:07 +0300 | [diff] [blame] | 3806 | jiffies + HCI_CMD_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3807 | } else { |
| 3808 | skb_queue_head(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3809 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | } |
| 3811 | } |
| 3812 | } |