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