Commit | Line | Data |
---|---|---|
8e87d142 | 1 | /* |
1da177e4 LT |
2 | BlueZ - Bluetooth protocol stack for Linux |
3 | Copyright (C) 2000-2001 Qualcomm Incorporated | |
4 | ||
5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License version 2 as | |
9 | published by the Free Software Foundation; | |
10 | ||
11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. | |
14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY | |
8e87d142 YH |
15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
1da177e4 LT |
18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
19 | ||
8e87d142 YH |
20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS | |
1da177e4 LT |
22 | SOFTWARE IS DISCLAIMED. |
23 | */ | |
24 | ||
25 | /* Bluetooth HCI core. */ | |
26 | ||
82453021 | 27 | #include <linux/jiffies.h> |
1da177e4 LT |
28 | #include <linux/module.h> |
29 | #include <linux/kmod.h> | |
30 | ||
31 | #include <linux/types.h> | |
32 | #include <linux/errno.h> | |
33 | #include <linux/kernel.h> | |
1da177e4 LT |
34 | #include <linux/sched.h> |
35 | #include <linux/slab.h> | |
36 | #include <linux/poll.h> | |
37 | #include <linux/fcntl.h> | |
38 | #include <linux/init.h> | |
39 | #include <linux/skbuff.h> | |
f48fd9c8 | 40 | #include <linux/workqueue.h> |
1da177e4 LT |
41 | #include <linux/interrupt.h> |
42 | #include <linux/notifier.h> | |
611b30f7 | 43 | #include <linux/rfkill.h> |
6bd32326 | 44 | #include <linux/timer.h> |
1da177e4 LT |
45 | #include <net/sock.h> |
46 | ||
47 | #include <asm/system.h> | |
70f23020 | 48 | #include <linux/uaccess.h> |
1da177e4 LT |
49 | #include <asm/unaligned.h> |
50 | ||
51 | #include <net/bluetooth/bluetooth.h> | |
52 | #include <net/bluetooth/hci_core.h> | |
53 | ||
ab81cbf9 JH |
54 | #define AUTO_OFF_TIMEOUT 2000 |
55 | ||
1da177e4 LT |
56 | static void hci_cmd_task(unsigned long arg); |
57 | static void hci_rx_task(unsigned long arg); | |
58 | static void hci_tx_task(unsigned long arg); | |
59 | static void hci_notify(struct hci_dev *hdev, int event); | |
60 | ||
61 | static DEFINE_RWLOCK(hci_task_lock); | |
62 | ||
63 | /* HCI device list */ | |
64 | LIST_HEAD(hci_dev_list); | |
65 | DEFINE_RWLOCK(hci_dev_list_lock); | |
66 | ||
67 | /* HCI callback list */ | |
68 | LIST_HEAD(hci_cb_list); | |
69 | DEFINE_RWLOCK(hci_cb_list_lock); | |
70 | ||
71 | /* HCI protocols */ | |
72 | #define HCI_MAX_PROTO 2 | |
73 | struct hci_proto *hci_proto[HCI_MAX_PROTO]; | |
74 | ||
75 | /* HCI notifiers list */ | |
e041c683 | 76 | static ATOMIC_NOTIFIER_HEAD(hci_notifier); |
1da177e4 LT |
77 | |
78 | /* ---- HCI notifications ---- */ | |
79 | ||
80 | int hci_register_notifier(struct notifier_block *nb) | |
81 | { | |
e041c683 | 82 | return atomic_notifier_chain_register(&hci_notifier, nb); |
1da177e4 LT |
83 | } |
84 | ||
85 | int hci_unregister_notifier(struct notifier_block *nb) | |
86 | { | |
e041c683 | 87 | return atomic_notifier_chain_unregister(&hci_notifier, nb); |
1da177e4 LT |
88 | } |
89 | ||
6516455d | 90 | static void hci_notify(struct hci_dev *hdev, int event) |
1da177e4 | 91 | { |
e041c683 | 92 | atomic_notifier_call_chain(&hci_notifier, event, hdev); |
1da177e4 LT |
93 | } |
94 | ||
95 | /* ---- HCI requests ---- */ | |
96 | ||
23bb5763 | 97 | void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result) |
1da177e4 | 98 | { |
23bb5763 JH |
99 | BT_DBG("%s command 0x%04x result 0x%2.2x", hdev->name, cmd, result); |
100 | ||
a5040efa JH |
101 | /* If this is the init phase check if the completed command matches |
102 | * the last init command, and if not just return. | |
103 | */ | |
104 | if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd) | |
23bb5763 | 105 | return; |
1da177e4 LT |
106 | |
107 | if (hdev->req_status == HCI_REQ_PEND) { | |
108 | hdev->req_result = result; | |
109 | hdev->req_status = HCI_REQ_DONE; | |
110 | wake_up_interruptible(&hdev->req_wait_q); | |
111 | } | |
112 | } | |
113 | ||
114 | static void hci_req_cancel(struct hci_dev *hdev, int err) | |
115 | { | |
116 | BT_DBG("%s err 0x%2.2x", hdev->name, err); | |
117 | ||
118 | if (hdev->req_status == HCI_REQ_PEND) { | |
119 | hdev->req_result = err; | |
120 | hdev->req_status = HCI_REQ_CANCELED; | |
121 | wake_up_interruptible(&hdev->req_wait_q); | |
122 | } | |
123 | } | |
124 | ||
125 | /* Execute request and wait for completion. */ | |
8e87d142 | 126 | static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), |
1da177e4 LT |
127 | unsigned long opt, __u32 timeout) |
128 | { | |
129 | DECLARE_WAITQUEUE(wait, current); | |
130 | int err = 0; | |
131 | ||
132 | BT_DBG("%s start", hdev->name); | |
133 | ||
134 | hdev->req_status = HCI_REQ_PEND; | |
135 | ||
136 | add_wait_queue(&hdev->req_wait_q, &wait); | |
137 | set_current_state(TASK_INTERRUPTIBLE); | |
138 | ||
139 | req(hdev, opt); | |
140 | schedule_timeout(timeout); | |
141 | ||
142 | remove_wait_queue(&hdev->req_wait_q, &wait); | |
143 | ||
144 | if (signal_pending(current)) | |
145 | return -EINTR; | |
146 | ||
147 | switch (hdev->req_status) { | |
148 | case HCI_REQ_DONE: | |
149 | err = -bt_err(hdev->req_result); | |
150 | break; | |
151 | ||
152 | case HCI_REQ_CANCELED: | |
153 | err = -hdev->req_result; | |
154 | break; | |
155 | ||
156 | default: | |
157 | err = -ETIMEDOUT; | |
158 | break; | |
3ff50b79 | 159 | } |
1da177e4 | 160 | |
a5040efa | 161 | hdev->req_status = hdev->req_result = 0; |
1da177e4 LT |
162 | |
163 | BT_DBG("%s end: err %d", hdev->name, err); | |
164 | ||
165 | return err; | |
166 | } | |
167 | ||
168 | static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), | |
169 | unsigned long opt, __u32 timeout) | |
170 | { | |
171 | int ret; | |
172 | ||
7c6a329e MH |
173 | if (!test_bit(HCI_UP, &hdev->flags)) |
174 | return -ENETDOWN; | |
175 | ||
1da177e4 LT |
176 | /* Serialize all requests */ |
177 | hci_req_lock(hdev); | |
178 | ret = __hci_request(hdev, req, opt, timeout); | |
179 | hci_req_unlock(hdev); | |
180 | ||
181 | return ret; | |
182 | } | |
183 | ||
184 | static void hci_reset_req(struct hci_dev *hdev, unsigned long opt) | |
185 | { | |
186 | BT_DBG("%s %ld", hdev->name, opt); | |
187 | ||
188 | /* Reset device */ | |
a9de9248 | 189 | hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL); |
1da177e4 LT |
190 | } |
191 | ||
192 | static void hci_init_req(struct hci_dev *hdev, unsigned long opt) | |
193 | { | |
b0916ea0 | 194 | struct hci_cp_delete_stored_link_key cp; |
1da177e4 | 195 | struct sk_buff *skb; |
1ebb9252 | 196 | __le16 param; |
89f2783d | 197 | __u8 flt_type; |
1da177e4 LT |
198 | |
199 | BT_DBG("%s %ld", hdev->name, opt); | |
200 | ||
201 | /* Driver initialization */ | |
202 | ||
203 | /* Special commands */ | |
204 | while ((skb = skb_dequeue(&hdev->driver_init))) { | |
0d48d939 | 205 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
1da177e4 | 206 | skb->dev = (void *) hdev; |
c78ae283 | 207 | |
1da177e4 | 208 | skb_queue_tail(&hdev->cmd_q, skb); |
c78ae283 | 209 | tasklet_schedule(&hdev->cmd_task); |
1da177e4 LT |
210 | } |
211 | skb_queue_purge(&hdev->driver_init); | |
212 | ||
213 | /* Mandatory initialization */ | |
214 | ||
215 | /* Reset */ | |
7a9d4020 | 216 | if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) |
a9de9248 | 217 | hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL); |
1da177e4 LT |
218 | |
219 | /* Read Local Supported Features */ | |
a9de9248 | 220 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
1da177e4 | 221 | |
1143e5a6 | 222 | /* Read Local Version */ |
a9de9248 | 223 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
1143e5a6 | 224 | |
1da177e4 | 225 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ |
a9de9248 | 226 | hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL); |
1da177e4 LT |
227 | |
228 | #if 0 | |
229 | /* Host buffer size */ | |
230 | { | |
231 | struct hci_cp_host_buffer_size cp; | |
aca3192c | 232 | cp.acl_mtu = cpu_to_le16(HCI_MAX_ACL_SIZE); |
1da177e4 | 233 | cp.sco_mtu = HCI_MAX_SCO_SIZE; |
aca3192c YH |
234 | cp.acl_max_pkt = cpu_to_le16(0xffff); |
235 | cp.sco_max_pkt = cpu_to_le16(0xffff); | |
a9de9248 | 236 | hci_send_cmd(hdev, HCI_OP_HOST_BUFFER_SIZE, sizeof(cp), &cp); |
1da177e4 LT |
237 | } |
238 | #endif | |
239 | ||
240 | /* Read BD Address */ | |
a9de9248 MH |
241 | hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL); |
242 | ||
243 | /* Read Class of Device */ | |
244 | hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); | |
245 | ||
246 | /* Read Local Name */ | |
247 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL); | |
1da177e4 LT |
248 | |
249 | /* Read Voice Setting */ | |
a9de9248 | 250 | hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL); |
1da177e4 LT |
251 | |
252 | /* Optional initialization */ | |
253 | ||
254 | /* Clear Event Filters */ | |
89f2783d | 255 | flt_type = HCI_FLT_CLEAR_ALL; |
a9de9248 | 256 | hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type); |
1da177e4 | 257 | |
1da177e4 | 258 | /* Connection accept timeout ~20 secs */ |
aca3192c | 259 | param = cpu_to_le16(0x7d00); |
a9de9248 | 260 | hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); |
b0916ea0 JH |
261 | |
262 | bacpy(&cp.bdaddr, BDADDR_ANY); | |
263 | cp.delete_all = 1; | |
264 | hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp); | |
1da177e4 LT |
265 | } |
266 | ||
6ed58ec5 VT |
267 | static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt) |
268 | { | |
269 | BT_DBG("%s", hdev->name); | |
270 | ||
271 | /* Read LE buffer size */ | |
272 | hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); | |
273 | } | |
274 | ||
1da177e4 LT |
275 | static void hci_scan_req(struct hci_dev *hdev, unsigned long opt) |
276 | { | |
277 | __u8 scan = opt; | |
278 | ||
279 | BT_DBG("%s %x", hdev->name, scan); | |
280 | ||
281 | /* Inquiry and Page scans */ | |
a9de9248 | 282 | hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
1da177e4 LT |
283 | } |
284 | ||
285 | static void hci_auth_req(struct hci_dev *hdev, unsigned long opt) | |
286 | { | |
287 | __u8 auth = opt; | |
288 | ||
289 | BT_DBG("%s %x", hdev->name, auth); | |
290 | ||
291 | /* Authentication */ | |
a9de9248 | 292 | hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); |
1da177e4 LT |
293 | } |
294 | ||
295 | static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt) | |
296 | { | |
297 | __u8 encrypt = opt; | |
298 | ||
299 | BT_DBG("%s %x", hdev->name, encrypt); | |
300 | ||
e4e8e37c | 301 | /* Encryption */ |
a9de9248 | 302 | hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); |
1da177e4 LT |
303 | } |
304 | ||
e4e8e37c MH |
305 | static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt) |
306 | { | |
307 | __le16 policy = cpu_to_le16(opt); | |
308 | ||
a418b893 | 309 | BT_DBG("%s %x", hdev->name, policy); |
e4e8e37c MH |
310 | |
311 | /* Default link policy */ | |
312 | hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); | |
313 | } | |
314 | ||
8e87d142 | 315 | /* Get HCI device by index. |
1da177e4 LT |
316 | * Device is held on return. */ |
317 | struct hci_dev *hci_dev_get(int index) | |
318 | { | |
319 | struct hci_dev *hdev = NULL; | |
320 | struct list_head *p; | |
321 | ||
322 | BT_DBG("%d", index); | |
323 | ||
324 | if (index < 0) | |
325 | return NULL; | |
326 | ||
327 | read_lock(&hci_dev_list_lock); | |
328 | list_for_each(p, &hci_dev_list) { | |
329 | struct hci_dev *d = list_entry(p, struct hci_dev, list); | |
330 | if (d->id == index) { | |
331 | hdev = hci_dev_hold(d); | |
332 | break; | |
333 | } | |
334 | } | |
335 | read_unlock(&hci_dev_list_lock); | |
336 | return hdev; | |
337 | } | |
1da177e4 LT |
338 | |
339 | /* ---- Inquiry support ---- */ | |
340 | static void inquiry_cache_flush(struct hci_dev *hdev) | |
341 | { | |
342 | struct inquiry_cache *cache = &hdev->inq_cache; | |
343 | struct inquiry_entry *next = cache->list, *e; | |
344 | ||
345 | BT_DBG("cache %p", cache); | |
346 | ||
347 | cache->list = NULL; | |
348 | while ((e = next)) { | |
349 | next = e->next; | |
350 | kfree(e); | |
351 | } | |
352 | } | |
353 | ||
354 | struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr) | |
355 | { | |
356 | struct inquiry_cache *cache = &hdev->inq_cache; | |
357 | struct inquiry_entry *e; | |
358 | ||
359 | BT_DBG("cache %p, %s", cache, batostr(bdaddr)); | |
360 | ||
361 | for (e = cache->list; e; e = e->next) | |
362 | if (!bacmp(&e->data.bdaddr, bdaddr)) | |
363 | break; | |
364 | return e; | |
365 | } | |
366 | ||
367 | void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data) | |
368 | { | |
369 | struct inquiry_cache *cache = &hdev->inq_cache; | |
70f23020 | 370 | struct inquiry_entry *ie; |
1da177e4 LT |
371 | |
372 | BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr)); | |
373 | ||
70f23020 AE |
374 | ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); |
375 | if (!ie) { | |
1da177e4 | 376 | /* Entry not in the cache. Add new one. */ |
70f23020 AE |
377 | ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC); |
378 | if (!ie) | |
1da177e4 | 379 | return; |
70f23020 AE |
380 | |
381 | ie->next = cache->list; | |
382 | cache->list = ie; | |
1da177e4 LT |
383 | } |
384 | ||
70f23020 AE |
385 | memcpy(&ie->data, data, sizeof(*data)); |
386 | ie->timestamp = jiffies; | |
1da177e4 LT |
387 | cache->timestamp = jiffies; |
388 | } | |
389 | ||
390 | static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) | |
391 | { | |
392 | struct inquiry_cache *cache = &hdev->inq_cache; | |
393 | struct inquiry_info *info = (struct inquiry_info *) buf; | |
394 | struct inquiry_entry *e; | |
395 | int copied = 0; | |
396 | ||
397 | for (e = cache->list; e && copied < num; e = e->next, copied++) { | |
398 | struct inquiry_data *data = &e->data; | |
399 | bacpy(&info->bdaddr, &data->bdaddr); | |
400 | info->pscan_rep_mode = data->pscan_rep_mode; | |
401 | info->pscan_period_mode = data->pscan_period_mode; | |
402 | info->pscan_mode = data->pscan_mode; | |
403 | memcpy(info->dev_class, data->dev_class, 3); | |
404 | info->clock_offset = data->clock_offset; | |
405 | info++; | |
406 | } | |
407 | ||
408 | BT_DBG("cache %p, copied %d", cache, copied); | |
409 | return copied; | |
410 | } | |
411 | ||
412 | static void hci_inq_req(struct hci_dev *hdev, unsigned long opt) | |
413 | { | |
414 | struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; | |
415 | struct hci_cp_inquiry cp; | |
416 | ||
417 | BT_DBG("%s", hdev->name); | |
418 | ||
419 | if (test_bit(HCI_INQUIRY, &hdev->flags)) | |
420 | return; | |
421 | ||
422 | /* Start Inquiry */ | |
423 | memcpy(&cp.lap, &ir->lap, 3); | |
424 | cp.length = ir->length; | |
425 | cp.num_rsp = ir->num_rsp; | |
a9de9248 | 426 | hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); |
1da177e4 LT |
427 | } |
428 | ||
429 | int hci_inquiry(void __user *arg) | |
430 | { | |
431 | __u8 __user *ptr = arg; | |
432 | struct hci_inquiry_req ir; | |
433 | struct hci_dev *hdev; | |
434 | int err = 0, do_inquiry = 0, max_rsp; | |
435 | long timeo; | |
436 | __u8 *buf; | |
437 | ||
438 | if (copy_from_user(&ir, ptr, sizeof(ir))) | |
439 | return -EFAULT; | |
440 | ||
5a08ecce AE |
441 | hdev = hci_dev_get(ir.dev_id); |
442 | if (!hdev) | |
1da177e4 LT |
443 | return -ENODEV; |
444 | ||
445 | hci_dev_lock_bh(hdev); | |
8e87d142 | 446 | if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || |
70f23020 AE |
447 | inquiry_cache_empty(hdev) || |
448 | ir.flags & IREQ_CACHE_FLUSH) { | |
1da177e4 LT |
449 | inquiry_cache_flush(hdev); |
450 | do_inquiry = 1; | |
451 | } | |
452 | hci_dev_unlock_bh(hdev); | |
453 | ||
04837f64 | 454 | timeo = ir.length * msecs_to_jiffies(2000); |
70f23020 AE |
455 | |
456 | if (do_inquiry) { | |
457 | err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo); | |
458 | if (err < 0) | |
459 | goto done; | |
460 | } | |
1da177e4 LT |
461 | |
462 | /* for unlimited number of responses we will use buffer with 255 entries */ | |
463 | max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; | |
464 | ||
465 | /* cache_dump can't sleep. Therefore we allocate temp buffer and then | |
466 | * copy it to the user space. | |
467 | */ | |
70f23020 AE |
468 | buf = kmalloc(sizeof(struct inquiry_info) *max_rsp, GFP_KERNEL); |
469 | if (!buf) { | |
1da177e4 LT |
470 | err = -ENOMEM; |
471 | goto done; | |
472 | } | |
473 | ||
474 | hci_dev_lock_bh(hdev); | |
475 | ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); | |
476 | hci_dev_unlock_bh(hdev); | |
477 | ||
478 | BT_DBG("num_rsp %d", ir.num_rsp); | |
479 | ||
480 | if (!copy_to_user(ptr, &ir, sizeof(ir))) { | |
481 | ptr += sizeof(ir); | |
482 | if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * | |
483 | ir.num_rsp)) | |
484 | err = -EFAULT; | |
8e87d142 | 485 | } else |
1da177e4 LT |
486 | err = -EFAULT; |
487 | ||
488 | kfree(buf); | |
489 | ||
490 | done: | |
491 | hci_dev_put(hdev); | |
492 | return err; | |
493 | } | |
494 | ||
495 | /* ---- HCI ioctl helpers ---- */ | |
496 | ||
497 | int hci_dev_open(__u16 dev) | |
498 | { | |
499 | struct hci_dev *hdev; | |
500 | int ret = 0; | |
501 | ||
5a08ecce AE |
502 | hdev = hci_dev_get(dev); |
503 | if (!hdev) | |
1da177e4 LT |
504 | return -ENODEV; |
505 | ||
506 | BT_DBG("%s %p", hdev->name, hdev); | |
507 | ||
508 | hci_req_lock(hdev); | |
509 | ||
611b30f7 MH |
510 | if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) { |
511 | ret = -ERFKILL; | |
512 | goto done; | |
513 | } | |
514 | ||
1da177e4 LT |
515 | if (test_bit(HCI_UP, &hdev->flags)) { |
516 | ret = -EALREADY; | |
517 | goto done; | |
518 | } | |
519 | ||
520 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) | |
521 | set_bit(HCI_RAW, &hdev->flags); | |
522 | ||
943da25d MH |
523 | /* Treat all non BR/EDR controllers as raw devices for now */ |
524 | if (hdev->dev_type != HCI_BREDR) | |
525 | set_bit(HCI_RAW, &hdev->flags); | |
526 | ||
1da177e4 LT |
527 | if (hdev->open(hdev)) { |
528 | ret = -EIO; | |
529 | goto done; | |
530 | } | |
531 | ||
532 | if (!test_bit(HCI_RAW, &hdev->flags)) { | |
533 | atomic_set(&hdev->cmd_cnt, 1); | |
534 | set_bit(HCI_INIT, &hdev->flags); | |
a5040efa | 535 | hdev->init_last_cmd = 0; |
1da177e4 LT |
536 | |
537 | //__hci_request(hdev, hci_reset_req, 0, HZ); | |
04837f64 MH |
538 | ret = __hci_request(hdev, hci_init_req, 0, |
539 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 | 540 | |
6ed58ec5 VT |
541 | if (lmp_le_capable(hdev)) |
542 | ret = __hci_request(hdev, hci_le_init_req, 0, | |
543 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
544 | ||
1da177e4 LT |
545 | clear_bit(HCI_INIT, &hdev->flags); |
546 | } | |
547 | ||
548 | if (!ret) { | |
549 | hci_dev_hold(hdev); | |
550 | set_bit(HCI_UP, &hdev->flags); | |
551 | hci_notify(hdev, HCI_DEV_UP); | |
5add6af8 JH |
552 | if (!test_bit(HCI_SETUP, &hdev->flags)) |
553 | mgmt_powered(hdev->id, 1); | |
8e87d142 | 554 | } else { |
1da177e4 LT |
555 | /* Init failed, cleanup */ |
556 | tasklet_kill(&hdev->rx_task); | |
557 | tasklet_kill(&hdev->tx_task); | |
558 | tasklet_kill(&hdev->cmd_task); | |
559 | ||
560 | skb_queue_purge(&hdev->cmd_q); | |
561 | skb_queue_purge(&hdev->rx_q); | |
562 | ||
563 | if (hdev->flush) | |
564 | hdev->flush(hdev); | |
565 | ||
566 | if (hdev->sent_cmd) { | |
567 | kfree_skb(hdev->sent_cmd); | |
568 | hdev->sent_cmd = NULL; | |
569 | } | |
570 | ||
571 | hdev->close(hdev); | |
572 | hdev->flags = 0; | |
573 | } | |
574 | ||
575 | done: | |
576 | hci_req_unlock(hdev); | |
577 | hci_dev_put(hdev); | |
578 | return ret; | |
579 | } | |
580 | ||
581 | static int hci_dev_do_close(struct hci_dev *hdev) | |
582 | { | |
583 | BT_DBG("%s %p", hdev->name, hdev); | |
584 | ||
585 | hci_req_cancel(hdev, ENODEV); | |
586 | hci_req_lock(hdev); | |
587 | ||
588 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { | |
589 | hci_req_unlock(hdev); | |
590 | return 0; | |
591 | } | |
592 | ||
593 | /* Kill RX and TX tasks */ | |
594 | tasklet_kill(&hdev->rx_task); | |
595 | tasklet_kill(&hdev->tx_task); | |
596 | ||
597 | hci_dev_lock_bh(hdev); | |
598 | inquiry_cache_flush(hdev); | |
599 | hci_conn_hash_flush(hdev); | |
600 | hci_dev_unlock_bh(hdev); | |
601 | ||
602 | hci_notify(hdev, HCI_DEV_DOWN); | |
603 | ||
604 | if (hdev->flush) | |
605 | hdev->flush(hdev); | |
606 | ||
607 | /* Reset device */ | |
608 | skb_queue_purge(&hdev->cmd_q); | |
609 | atomic_set(&hdev->cmd_cnt, 1); | |
610 | if (!test_bit(HCI_RAW, &hdev->flags)) { | |
611 | set_bit(HCI_INIT, &hdev->flags); | |
04837f64 MH |
612 | __hci_request(hdev, hci_reset_req, 0, |
613 | msecs_to_jiffies(250)); | |
1da177e4 LT |
614 | clear_bit(HCI_INIT, &hdev->flags); |
615 | } | |
616 | ||
617 | /* Kill cmd task */ | |
618 | tasklet_kill(&hdev->cmd_task); | |
619 | ||
620 | /* Drop queues */ | |
621 | skb_queue_purge(&hdev->rx_q); | |
622 | skb_queue_purge(&hdev->cmd_q); | |
623 | skb_queue_purge(&hdev->raw_q); | |
624 | ||
625 | /* Drop last sent command */ | |
626 | if (hdev->sent_cmd) { | |
6bd32326 | 627 | del_timer_sync(&hdev->cmd_timer); |
1da177e4 LT |
628 | kfree_skb(hdev->sent_cmd); |
629 | hdev->sent_cmd = NULL; | |
630 | } | |
631 | ||
632 | /* After this point our queues are empty | |
633 | * and no tasks are scheduled. */ | |
634 | hdev->close(hdev); | |
635 | ||
5add6af8 JH |
636 | mgmt_powered(hdev->id, 0); |
637 | ||
1da177e4 LT |
638 | /* Clear flags */ |
639 | hdev->flags = 0; | |
640 | ||
641 | hci_req_unlock(hdev); | |
642 | ||
643 | hci_dev_put(hdev); | |
644 | return 0; | |
645 | } | |
646 | ||
647 | int hci_dev_close(__u16 dev) | |
648 | { | |
649 | struct hci_dev *hdev; | |
650 | int err; | |
651 | ||
70f23020 AE |
652 | hdev = hci_dev_get(dev); |
653 | if (!hdev) | |
1da177e4 LT |
654 | return -ENODEV; |
655 | err = hci_dev_do_close(hdev); | |
656 | hci_dev_put(hdev); | |
657 | return err; | |
658 | } | |
659 | ||
660 | int hci_dev_reset(__u16 dev) | |
661 | { | |
662 | struct hci_dev *hdev; | |
663 | int ret = 0; | |
664 | ||
70f23020 AE |
665 | hdev = hci_dev_get(dev); |
666 | if (!hdev) | |
1da177e4 LT |
667 | return -ENODEV; |
668 | ||
669 | hci_req_lock(hdev); | |
670 | tasklet_disable(&hdev->tx_task); | |
671 | ||
672 | if (!test_bit(HCI_UP, &hdev->flags)) | |
673 | goto done; | |
674 | ||
675 | /* Drop queues */ | |
676 | skb_queue_purge(&hdev->rx_q); | |
677 | skb_queue_purge(&hdev->cmd_q); | |
678 | ||
679 | hci_dev_lock_bh(hdev); | |
680 | inquiry_cache_flush(hdev); | |
681 | hci_conn_hash_flush(hdev); | |
682 | hci_dev_unlock_bh(hdev); | |
683 | ||
684 | if (hdev->flush) | |
685 | hdev->flush(hdev); | |
686 | ||
8e87d142 | 687 | atomic_set(&hdev->cmd_cnt, 1); |
6ed58ec5 | 688 | hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; |
1da177e4 LT |
689 | |
690 | if (!test_bit(HCI_RAW, &hdev->flags)) | |
04837f64 MH |
691 | ret = __hci_request(hdev, hci_reset_req, 0, |
692 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
693 | |
694 | done: | |
695 | tasklet_enable(&hdev->tx_task); | |
696 | hci_req_unlock(hdev); | |
697 | hci_dev_put(hdev); | |
698 | return ret; | |
699 | } | |
700 | ||
701 | int hci_dev_reset_stat(__u16 dev) | |
702 | { | |
703 | struct hci_dev *hdev; | |
704 | int ret = 0; | |
705 | ||
70f23020 AE |
706 | hdev = hci_dev_get(dev); |
707 | if (!hdev) | |
1da177e4 LT |
708 | return -ENODEV; |
709 | ||
710 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); | |
711 | ||
712 | hci_dev_put(hdev); | |
713 | ||
714 | return ret; | |
715 | } | |
716 | ||
717 | int hci_dev_cmd(unsigned int cmd, void __user *arg) | |
718 | { | |
719 | struct hci_dev *hdev; | |
720 | struct hci_dev_req dr; | |
721 | int err = 0; | |
722 | ||
723 | if (copy_from_user(&dr, arg, sizeof(dr))) | |
724 | return -EFAULT; | |
725 | ||
70f23020 AE |
726 | hdev = hci_dev_get(dr.dev_id); |
727 | if (!hdev) | |
1da177e4 LT |
728 | return -ENODEV; |
729 | ||
730 | switch (cmd) { | |
731 | case HCISETAUTH: | |
04837f64 MH |
732 | err = hci_request(hdev, hci_auth_req, dr.dev_opt, |
733 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
734 | break; |
735 | ||
736 | case HCISETENCRYPT: | |
737 | if (!lmp_encrypt_capable(hdev)) { | |
738 | err = -EOPNOTSUPP; | |
739 | break; | |
740 | } | |
741 | ||
742 | if (!test_bit(HCI_AUTH, &hdev->flags)) { | |
743 | /* Auth must be enabled first */ | |
04837f64 MH |
744 | err = hci_request(hdev, hci_auth_req, dr.dev_opt, |
745 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
746 | if (err) |
747 | break; | |
748 | } | |
749 | ||
04837f64 MH |
750 | err = hci_request(hdev, hci_encrypt_req, dr.dev_opt, |
751 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
752 | break; |
753 | ||
754 | case HCISETSCAN: | |
04837f64 MH |
755 | err = hci_request(hdev, hci_scan_req, dr.dev_opt, |
756 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
757 | break; |
758 | ||
1da177e4 | 759 | case HCISETLINKPOL: |
e4e8e37c MH |
760 | err = hci_request(hdev, hci_linkpol_req, dr.dev_opt, |
761 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); | |
1da177e4 LT |
762 | break; |
763 | ||
764 | case HCISETLINKMODE: | |
e4e8e37c MH |
765 | hdev->link_mode = ((__u16) dr.dev_opt) & |
766 | (HCI_LM_MASTER | HCI_LM_ACCEPT); | |
767 | break; | |
768 | ||
769 | case HCISETPTYPE: | |
770 | hdev->pkt_type = (__u16) dr.dev_opt; | |
1da177e4 LT |
771 | break; |
772 | ||
773 | case HCISETACLMTU: | |
e4e8e37c MH |
774 | hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); |
775 | hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); | |
1da177e4 LT |
776 | break; |
777 | ||
778 | case HCISETSCOMTU: | |
e4e8e37c MH |
779 | hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); |
780 | hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); | |
1da177e4 LT |
781 | break; |
782 | ||
783 | default: | |
784 | err = -EINVAL; | |
785 | break; | |
786 | } | |
e4e8e37c | 787 | |
1da177e4 LT |
788 | hci_dev_put(hdev); |
789 | return err; | |
790 | } | |
791 | ||
792 | int hci_get_dev_list(void __user *arg) | |
793 | { | |
794 | struct hci_dev_list_req *dl; | |
795 | struct hci_dev_req *dr; | |
796 | struct list_head *p; | |
797 | int n = 0, size, err; | |
798 | __u16 dev_num; | |
799 | ||
800 | if (get_user(dev_num, (__u16 __user *) arg)) | |
801 | return -EFAULT; | |
802 | ||
803 | if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) | |
804 | return -EINVAL; | |
805 | ||
806 | size = sizeof(*dl) + dev_num * sizeof(*dr); | |
807 | ||
70f23020 AE |
808 | dl = kzalloc(size, GFP_KERNEL); |
809 | if (!dl) | |
1da177e4 LT |
810 | return -ENOMEM; |
811 | ||
812 | dr = dl->dev_req; | |
813 | ||
814 | read_lock_bh(&hci_dev_list_lock); | |
815 | list_for_each(p, &hci_dev_list) { | |
816 | struct hci_dev *hdev; | |
c542a06c | 817 | |
1da177e4 | 818 | hdev = list_entry(p, struct hci_dev, list); |
c542a06c | 819 | |
ab81cbf9 | 820 | hci_del_off_timer(hdev); |
c542a06c JH |
821 | |
822 | if (!test_bit(HCI_MGMT, &hdev->flags)) | |
823 | set_bit(HCI_PAIRABLE, &hdev->flags); | |
824 | ||
1da177e4 LT |
825 | (dr + n)->dev_id = hdev->id; |
826 | (dr + n)->dev_opt = hdev->flags; | |
c542a06c | 827 | |
1da177e4 LT |
828 | if (++n >= dev_num) |
829 | break; | |
830 | } | |
831 | read_unlock_bh(&hci_dev_list_lock); | |
832 | ||
833 | dl->dev_num = n; | |
834 | size = sizeof(*dl) + n * sizeof(*dr); | |
835 | ||
836 | err = copy_to_user(arg, dl, size); | |
837 | kfree(dl); | |
838 | ||
839 | return err ? -EFAULT : 0; | |
840 | } | |
841 | ||
842 | int hci_get_dev_info(void __user *arg) | |
843 | { | |
844 | struct hci_dev *hdev; | |
845 | struct hci_dev_info di; | |
846 | int err = 0; | |
847 | ||
848 | if (copy_from_user(&di, arg, sizeof(di))) | |
849 | return -EFAULT; | |
850 | ||
70f23020 AE |
851 | hdev = hci_dev_get(di.dev_id); |
852 | if (!hdev) | |
1da177e4 LT |
853 | return -ENODEV; |
854 | ||
ab81cbf9 JH |
855 | hci_del_off_timer(hdev); |
856 | ||
c542a06c JH |
857 | if (!test_bit(HCI_MGMT, &hdev->flags)) |
858 | set_bit(HCI_PAIRABLE, &hdev->flags); | |
859 | ||
1da177e4 LT |
860 | strcpy(di.name, hdev->name); |
861 | di.bdaddr = hdev->bdaddr; | |
943da25d | 862 | di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4); |
1da177e4 LT |
863 | di.flags = hdev->flags; |
864 | di.pkt_type = hdev->pkt_type; | |
865 | di.acl_mtu = hdev->acl_mtu; | |
866 | di.acl_pkts = hdev->acl_pkts; | |
867 | di.sco_mtu = hdev->sco_mtu; | |
868 | di.sco_pkts = hdev->sco_pkts; | |
869 | di.link_policy = hdev->link_policy; | |
870 | di.link_mode = hdev->link_mode; | |
871 | ||
872 | memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); | |
873 | memcpy(&di.features, &hdev->features, sizeof(di.features)); | |
874 | ||
875 | if (copy_to_user(arg, &di, sizeof(di))) | |
876 | err = -EFAULT; | |
877 | ||
878 | hci_dev_put(hdev); | |
879 | ||
880 | return err; | |
881 | } | |
882 | ||
883 | /* ---- Interface to HCI drivers ---- */ | |
884 | ||
611b30f7 MH |
885 | static int hci_rfkill_set_block(void *data, bool blocked) |
886 | { | |
887 | struct hci_dev *hdev = data; | |
888 | ||
889 | BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); | |
890 | ||
891 | if (!blocked) | |
892 | return 0; | |
893 | ||
894 | hci_dev_do_close(hdev); | |
895 | ||
896 | return 0; | |
897 | } | |
898 | ||
899 | static const struct rfkill_ops hci_rfkill_ops = { | |
900 | .set_block = hci_rfkill_set_block, | |
901 | }; | |
902 | ||
1da177e4 LT |
903 | /* Alloc HCI device */ |
904 | struct hci_dev *hci_alloc_dev(void) | |
905 | { | |
906 | struct hci_dev *hdev; | |
907 | ||
25ea6db0 | 908 | hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); |
1da177e4 LT |
909 | if (!hdev) |
910 | return NULL; | |
911 | ||
1da177e4 LT |
912 | skb_queue_head_init(&hdev->driver_init); |
913 | ||
914 | return hdev; | |
915 | } | |
916 | EXPORT_SYMBOL(hci_alloc_dev); | |
917 | ||
918 | /* Free HCI device */ | |
919 | void hci_free_dev(struct hci_dev *hdev) | |
920 | { | |
921 | skb_queue_purge(&hdev->driver_init); | |
922 | ||
a91f2e39 MH |
923 | /* will free via device release */ |
924 | put_device(&hdev->dev); | |
1da177e4 LT |
925 | } |
926 | EXPORT_SYMBOL(hci_free_dev); | |
927 | ||
ab81cbf9 JH |
928 | static void hci_power_on(struct work_struct *work) |
929 | { | |
930 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); | |
931 | ||
932 | BT_DBG("%s", hdev->name); | |
933 | ||
934 | if (hci_dev_open(hdev->id) < 0) | |
935 | return; | |
936 | ||
937 | if (test_bit(HCI_AUTO_OFF, &hdev->flags)) | |
938 | mod_timer(&hdev->off_timer, | |
939 | jiffies + msecs_to_jiffies(AUTO_OFF_TIMEOUT)); | |
940 | ||
941 | if (test_and_clear_bit(HCI_SETUP, &hdev->flags)) | |
942 | mgmt_index_added(hdev->id); | |
943 | } | |
944 | ||
945 | static void hci_power_off(struct work_struct *work) | |
946 | { | |
947 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_off); | |
948 | ||
949 | BT_DBG("%s", hdev->name); | |
950 | ||
951 | hci_dev_close(hdev->id); | |
952 | } | |
953 | ||
954 | static void hci_auto_off(unsigned long data) | |
955 | { | |
956 | struct hci_dev *hdev = (struct hci_dev *) data; | |
957 | ||
958 | BT_DBG("%s", hdev->name); | |
959 | ||
960 | clear_bit(HCI_AUTO_OFF, &hdev->flags); | |
961 | ||
962 | queue_work(hdev->workqueue, &hdev->power_off); | |
963 | } | |
964 | ||
965 | void hci_del_off_timer(struct hci_dev *hdev) | |
966 | { | |
967 | BT_DBG("%s", hdev->name); | |
968 | ||
969 | clear_bit(HCI_AUTO_OFF, &hdev->flags); | |
970 | del_timer(&hdev->off_timer); | |
971 | } | |
972 | ||
2aeb9a1a JH |
973 | int hci_uuids_clear(struct hci_dev *hdev) |
974 | { | |
975 | struct list_head *p, *n; | |
976 | ||
977 | list_for_each_safe(p, n, &hdev->uuids) { | |
978 | struct bt_uuid *uuid; | |
979 | ||
980 | uuid = list_entry(p, struct bt_uuid, list); | |
981 | ||
982 | list_del(p); | |
983 | kfree(uuid); | |
984 | } | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
55ed8ca1 JH |
989 | int hci_link_keys_clear(struct hci_dev *hdev) |
990 | { | |
991 | struct list_head *p, *n; | |
992 | ||
993 | list_for_each_safe(p, n, &hdev->link_keys) { | |
994 | struct link_key *key; | |
995 | ||
996 | key = list_entry(p, struct link_key, list); | |
997 | ||
998 | list_del(p); | |
999 | kfree(key); | |
1000 | } | |
1001 | ||
1002 | return 0; | |
1003 | } | |
1004 | ||
1005 | struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) | |
1006 | { | |
1007 | struct list_head *p; | |
1008 | ||
1009 | list_for_each(p, &hdev->link_keys) { | |
1010 | struct link_key *k; | |
1011 | ||
1012 | k = list_entry(p, struct link_key, list); | |
1013 | ||
1014 | if (bacmp(bdaddr, &k->bdaddr) == 0) | |
1015 | return k; | |
1016 | } | |
1017 | ||
1018 | return NULL; | |
1019 | } | |
1020 | ||
1021 | int hci_add_link_key(struct hci_dev *hdev, int new_key, bdaddr_t *bdaddr, | |
1022 | u8 *val, u8 type, u8 pin_len) | |
1023 | { | |
1024 | struct link_key *key, *old_key; | |
1025 | u8 old_key_type; | |
1026 | ||
1027 | old_key = hci_find_link_key(hdev, bdaddr); | |
1028 | if (old_key) { | |
1029 | old_key_type = old_key->type; | |
1030 | key = old_key; | |
1031 | } else { | |
1032 | old_key_type = 0xff; | |
1033 | key = kzalloc(sizeof(*key), GFP_ATOMIC); | |
1034 | if (!key) | |
1035 | return -ENOMEM; | |
1036 | list_add(&key->list, &hdev->link_keys); | |
1037 | } | |
1038 | ||
1039 | BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type); | |
1040 | ||
1041 | bacpy(&key->bdaddr, bdaddr); | |
1042 | memcpy(key->val, val, 16); | |
1043 | key->type = type; | |
1044 | key->pin_len = pin_len; | |
1045 | ||
1046 | if (new_key) | |
1047 | mgmt_new_key(hdev->id, key, old_key_type); | |
1048 | ||
1049 | if (type == 0x06) | |
1050 | key->type = old_key_type; | |
1051 | ||
1052 | return 0; | |
1053 | } | |
1054 | ||
1055 | int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) | |
1056 | { | |
1057 | struct link_key *key; | |
1058 | ||
1059 | key = hci_find_link_key(hdev, bdaddr); | |
1060 | if (!key) | |
1061 | return -ENOENT; | |
1062 | ||
1063 | BT_DBG("%s removing %s", hdev->name, batostr(bdaddr)); | |
1064 | ||
1065 | list_del(&key->list); | |
1066 | kfree(key); | |
1067 | ||
1068 | return 0; | |
1069 | } | |
1070 | ||
6bd32326 VT |
1071 | /* HCI command timer function */ |
1072 | static void hci_cmd_timer(unsigned long arg) | |
1073 | { | |
1074 | struct hci_dev *hdev = (void *) arg; | |
1075 | ||
1076 | BT_ERR("%s command tx timeout", hdev->name); | |
1077 | atomic_set(&hdev->cmd_cnt, 1); | |
1078 | tasklet_schedule(&hdev->cmd_task); | |
1079 | } | |
1080 | ||
1da177e4 LT |
1081 | /* Register HCI device */ |
1082 | int hci_register_dev(struct hci_dev *hdev) | |
1083 | { | |
1084 | struct list_head *head = &hci_dev_list, *p; | |
ef222013 | 1085 | int i, id = 0; |
1da177e4 | 1086 | |
c13854ce MH |
1087 | BT_DBG("%p name %s bus %d owner %p", hdev, hdev->name, |
1088 | hdev->bus, hdev->owner); | |
1da177e4 LT |
1089 | |
1090 | if (!hdev->open || !hdev->close || !hdev->destruct) | |
1091 | return -EINVAL; | |
1092 | ||
1093 | write_lock_bh(&hci_dev_list_lock); | |
1094 | ||
1095 | /* Find first available device id */ | |
1096 | list_for_each(p, &hci_dev_list) { | |
1097 | if (list_entry(p, struct hci_dev, list)->id != id) | |
1098 | break; | |
1099 | head = p; id++; | |
1100 | } | |
8e87d142 | 1101 | |
1da177e4 LT |
1102 | sprintf(hdev->name, "hci%d", id); |
1103 | hdev->id = id; | |
1104 | list_add(&hdev->list, head); | |
1105 | ||
1106 | atomic_set(&hdev->refcnt, 1); | |
1107 | spin_lock_init(&hdev->lock); | |
1108 | ||
1109 | hdev->flags = 0; | |
1110 | hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); | |
5b7f9909 | 1111 | hdev->esco_type = (ESCO_HV1); |
1da177e4 | 1112 | hdev->link_mode = (HCI_LM_ACCEPT); |
17fa4b9d | 1113 | hdev->io_capability = 0x03; /* No Input No Output */ |
1da177e4 | 1114 | |
04837f64 MH |
1115 | hdev->idle_timeout = 0; |
1116 | hdev->sniff_max_interval = 800; | |
1117 | hdev->sniff_min_interval = 80; | |
1118 | ||
70f23020 | 1119 | tasklet_init(&hdev->cmd_task, hci_cmd_task, (unsigned long) hdev); |
1da177e4 LT |
1120 | tasklet_init(&hdev->rx_task, hci_rx_task, (unsigned long) hdev); |
1121 | tasklet_init(&hdev->tx_task, hci_tx_task, (unsigned long) hdev); | |
1122 | ||
1123 | skb_queue_head_init(&hdev->rx_q); | |
1124 | skb_queue_head_init(&hdev->cmd_q); | |
1125 | skb_queue_head_init(&hdev->raw_q); | |
1126 | ||
6bd32326 VT |
1127 | setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev); |
1128 | ||
cd4c5391 | 1129 | for (i = 0; i < NUM_REASSEMBLY; i++) |
ef222013 MH |
1130 | hdev->reassembly[i] = NULL; |
1131 | ||
1da177e4 | 1132 | init_waitqueue_head(&hdev->req_wait_q); |
a6a67efd | 1133 | mutex_init(&hdev->req_lock); |
1da177e4 LT |
1134 | |
1135 | inquiry_cache_init(hdev); | |
1136 | ||
1137 | hci_conn_hash_init(hdev); | |
1138 | ||
ea4bd8ba | 1139 | INIT_LIST_HEAD(&hdev->blacklist); |
f0358568 | 1140 | |
2aeb9a1a JH |
1141 | INIT_LIST_HEAD(&hdev->uuids); |
1142 | ||
55ed8ca1 JH |
1143 | INIT_LIST_HEAD(&hdev->link_keys); |
1144 | ||
ab81cbf9 JH |
1145 | INIT_WORK(&hdev->power_on, hci_power_on); |
1146 | INIT_WORK(&hdev->power_off, hci_power_off); | |
1147 | setup_timer(&hdev->off_timer, hci_auto_off, (unsigned long) hdev); | |
1148 | ||
1da177e4 LT |
1149 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
1150 | ||
1151 | atomic_set(&hdev->promisc, 0); | |
1152 | ||
1153 | write_unlock_bh(&hci_dev_list_lock); | |
1154 | ||
f48fd9c8 MH |
1155 | hdev->workqueue = create_singlethread_workqueue(hdev->name); |
1156 | if (!hdev->workqueue) | |
1157 | goto nomem; | |
1158 | ||
1da177e4 LT |
1159 | hci_register_sysfs(hdev); |
1160 | ||
611b30f7 MH |
1161 | hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, |
1162 | RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, hdev); | |
1163 | if (hdev->rfkill) { | |
1164 | if (rfkill_register(hdev->rfkill) < 0) { | |
1165 | rfkill_destroy(hdev->rfkill); | |
1166 | hdev->rfkill = NULL; | |
1167 | } | |
1168 | } | |
1169 | ||
ab81cbf9 JH |
1170 | set_bit(HCI_AUTO_OFF, &hdev->flags); |
1171 | set_bit(HCI_SETUP, &hdev->flags); | |
1172 | queue_work(hdev->workqueue, &hdev->power_on); | |
1173 | ||
1da177e4 LT |
1174 | hci_notify(hdev, HCI_DEV_REG); |
1175 | ||
1176 | return id; | |
f48fd9c8 MH |
1177 | |
1178 | nomem: | |
1179 | write_lock_bh(&hci_dev_list_lock); | |
1180 | list_del(&hdev->list); | |
1181 | write_unlock_bh(&hci_dev_list_lock); | |
1182 | ||
1183 | return -ENOMEM; | |
1da177e4 LT |
1184 | } |
1185 | EXPORT_SYMBOL(hci_register_dev); | |
1186 | ||
1187 | /* Unregister HCI device */ | |
1188 | int hci_unregister_dev(struct hci_dev *hdev) | |
1189 | { | |
ef222013 MH |
1190 | int i; |
1191 | ||
c13854ce | 1192 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
1da177e4 | 1193 | |
1da177e4 LT |
1194 | write_lock_bh(&hci_dev_list_lock); |
1195 | list_del(&hdev->list); | |
1196 | write_unlock_bh(&hci_dev_list_lock); | |
1197 | ||
1198 | hci_dev_do_close(hdev); | |
1199 | ||
cd4c5391 | 1200 | for (i = 0; i < NUM_REASSEMBLY; i++) |
ef222013 MH |
1201 | kfree_skb(hdev->reassembly[i]); |
1202 | ||
ab81cbf9 JH |
1203 | if (!test_bit(HCI_INIT, &hdev->flags) && |
1204 | !test_bit(HCI_SETUP, &hdev->flags)) | |
1205 | mgmt_index_removed(hdev->id); | |
1206 | ||
1da177e4 LT |
1207 | hci_notify(hdev, HCI_DEV_UNREG); |
1208 | ||
611b30f7 MH |
1209 | if (hdev->rfkill) { |
1210 | rfkill_unregister(hdev->rfkill); | |
1211 | rfkill_destroy(hdev->rfkill); | |
1212 | } | |
1213 | ||
147e2d59 DY |
1214 | hci_unregister_sysfs(hdev); |
1215 | ||
c6f3c5f7 GP |
1216 | hci_del_off_timer(hdev); |
1217 | ||
f48fd9c8 MH |
1218 | destroy_workqueue(hdev->workqueue); |
1219 | ||
e2e0cacb JH |
1220 | hci_dev_lock_bh(hdev); |
1221 | hci_blacklist_clear(hdev); | |
2aeb9a1a | 1222 | hci_uuids_clear(hdev); |
55ed8ca1 | 1223 | hci_link_keys_clear(hdev); |
e2e0cacb JH |
1224 | hci_dev_unlock_bh(hdev); |
1225 | ||
1da177e4 | 1226 | __hci_dev_put(hdev); |
ef222013 | 1227 | |
1da177e4 LT |
1228 | return 0; |
1229 | } | |
1230 | EXPORT_SYMBOL(hci_unregister_dev); | |
1231 | ||
1232 | /* Suspend HCI device */ | |
1233 | int hci_suspend_dev(struct hci_dev *hdev) | |
1234 | { | |
1235 | hci_notify(hdev, HCI_DEV_SUSPEND); | |
1236 | return 0; | |
1237 | } | |
1238 | EXPORT_SYMBOL(hci_suspend_dev); | |
1239 | ||
1240 | /* Resume HCI device */ | |
1241 | int hci_resume_dev(struct hci_dev *hdev) | |
1242 | { | |
1243 | hci_notify(hdev, HCI_DEV_RESUME); | |
1244 | return 0; | |
1245 | } | |
1246 | EXPORT_SYMBOL(hci_resume_dev); | |
1247 | ||
76bca880 MH |
1248 | /* Receive frame from HCI drivers */ |
1249 | int hci_recv_frame(struct sk_buff *skb) | |
1250 | { | |
1251 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
1252 | if (!hdev || (!test_bit(HCI_UP, &hdev->flags) | |
1253 | && !test_bit(HCI_INIT, &hdev->flags))) { | |
1254 | kfree_skb(skb); | |
1255 | return -ENXIO; | |
1256 | } | |
1257 | ||
1258 | /* Incomming skb */ | |
1259 | bt_cb(skb)->incoming = 1; | |
1260 | ||
1261 | /* Time stamp */ | |
1262 | __net_timestamp(skb); | |
1263 | ||
1264 | /* Queue frame for rx task */ | |
1265 | skb_queue_tail(&hdev->rx_q, skb); | |
c78ae283 MH |
1266 | tasklet_schedule(&hdev->rx_task); |
1267 | ||
76bca880 MH |
1268 | return 0; |
1269 | } | |
1270 | EXPORT_SYMBOL(hci_recv_frame); | |
1271 | ||
33e882a5 SS |
1272 | static int hci_reassembly(struct hci_dev *hdev, int type, void *data, |
1273 | int count, __u8 index, gfp_t gfp_mask) | |
1274 | { | |
1275 | int len = 0; | |
1276 | int hlen = 0; | |
1277 | int remain = count; | |
1278 | struct sk_buff *skb; | |
1279 | struct bt_skb_cb *scb; | |
1280 | ||
1281 | if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || | |
1282 | index >= NUM_REASSEMBLY) | |
1283 | return -EILSEQ; | |
1284 | ||
1285 | skb = hdev->reassembly[index]; | |
1286 | ||
1287 | if (!skb) { | |
1288 | switch (type) { | |
1289 | case HCI_ACLDATA_PKT: | |
1290 | len = HCI_MAX_FRAME_SIZE; | |
1291 | hlen = HCI_ACL_HDR_SIZE; | |
1292 | break; | |
1293 | case HCI_EVENT_PKT: | |
1294 | len = HCI_MAX_EVENT_SIZE; | |
1295 | hlen = HCI_EVENT_HDR_SIZE; | |
1296 | break; | |
1297 | case HCI_SCODATA_PKT: | |
1298 | len = HCI_MAX_SCO_SIZE; | |
1299 | hlen = HCI_SCO_HDR_SIZE; | |
1300 | break; | |
1301 | } | |
1302 | ||
1303 | skb = bt_skb_alloc(len, gfp_mask); | |
1304 | if (!skb) | |
1305 | return -ENOMEM; | |
1306 | ||
1307 | scb = (void *) skb->cb; | |
1308 | scb->expect = hlen; | |
1309 | scb->pkt_type = type; | |
1310 | ||
1311 | skb->dev = (void *) hdev; | |
1312 | hdev->reassembly[index] = skb; | |
1313 | } | |
1314 | ||
1315 | while (count) { | |
1316 | scb = (void *) skb->cb; | |
1317 | len = min(scb->expect, (__u16)count); | |
1318 | ||
1319 | memcpy(skb_put(skb, len), data, len); | |
1320 | ||
1321 | count -= len; | |
1322 | data += len; | |
1323 | scb->expect -= len; | |
1324 | remain = count; | |
1325 | ||
1326 | switch (type) { | |
1327 | case HCI_EVENT_PKT: | |
1328 | if (skb->len == HCI_EVENT_HDR_SIZE) { | |
1329 | struct hci_event_hdr *h = hci_event_hdr(skb); | |
1330 | scb->expect = h->plen; | |
1331 | ||
1332 | if (skb_tailroom(skb) < scb->expect) { | |
1333 | kfree_skb(skb); | |
1334 | hdev->reassembly[index] = NULL; | |
1335 | return -ENOMEM; | |
1336 | } | |
1337 | } | |
1338 | break; | |
1339 | ||
1340 | case HCI_ACLDATA_PKT: | |
1341 | if (skb->len == HCI_ACL_HDR_SIZE) { | |
1342 | struct hci_acl_hdr *h = hci_acl_hdr(skb); | |
1343 | scb->expect = __le16_to_cpu(h->dlen); | |
1344 | ||
1345 | if (skb_tailroom(skb) < scb->expect) { | |
1346 | kfree_skb(skb); | |
1347 | hdev->reassembly[index] = NULL; | |
1348 | return -ENOMEM; | |
1349 | } | |
1350 | } | |
1351 | break; | |
1352 | ||
1353 | case HCI_SCODATA_PKT: | |
1354 | if (skb->len == HCI_SCO_HDR_SIZE) { | |
1355 | struct hci_sco_hdr *h = hci_sco_hdr(skb); | |
1356 | scb->expect = h->dlen; | |
1357 | ||
1358 | if (skb_tailroom(skb) < scb->expect) { | |
1359 | kfree_skb(skb); | |
1360 | hdev->reassembly[index] = NULL; | |
1361 | return -ENOMEM; | |
1362 | } | |
1363 | } | |
1364 | break; | |
1365 | } | |
1366 | ||
1367 | if (scb->expect == 0) { | |
1368 | /* Complete frame */ | |
1369 | ||
1370 | bt_cb(skb)->pkt_type = type; | |
1371 | hci_recv_frame(skb); | |
1372 | ||
1373 | hdev->reassembly[index] = NULL; | |
1374 | return remain; | |
1375 | } | |
1376 | } | |
1377 | ||
1378 | return remain; | |
1379 | } | |
1380 | ||
ef222013 MH |
1381 | int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) |
1382 | { | |
f39a3c06 SS |
1383 | int rem = 0; |
1384 | ||
ef222013 MH |
1385 | if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) |
1386 | return -EILSEQ; | |
1387 | ||
da5f6c37 | 1388 | while (count) { |
f39a3c06 SS |
1389 | rem = hci_reassembly(hdev, type, data, count, |
1390 | type - 1, GFP_ATOMIC); | |
1391 | if (rem < 0) | |
1392 | return rem; | |
ef222013 | 1393 | |
f39a3c06 SS |
1394 | data += (count - rem); |
1395 | count = rem; | |
da5f6c37 | 1396 | }; |
ef222013 | 1397 | |
f39a3c06 | 1398 | return rem; |
ef222013 MH |
1399 | } |
1400 | EXPORT_SYMBOL(hci_recv_fragment); | |
1401 | ||
99811510 SS |
1402 | #define STREAM_REASSEMBLY 0 |
1403 | ||
1404 | int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count) | |
1405 | { | |
1406 | int type; | |
1407 | int rem = 0; | |
1408 | ||
da5f6c37 | 1409 | while (count) { |
99811510 SS |
1410 | struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY]; |
1411 | ||
1412 | if (!skb) { | |
1413 | struct { char type; } *pkt; | |
1414 | ||
1415 | /* Start of the frame */ | |
1416 | pkt = data; | |
1417 | type = pkt->type; | |
1418 | ||
1419 | data++; | |
1420 | count--; | |
1421 | } else | |
1422 | type = bt_cb(skb)->pkt_type; | |
1423 | ||
1424 | rem = hci_reassembly(hdev, type, data, | |
1425 | count, STREAM_REASSEMBLY, GFP_ATOMIC); | |
1426 | if (rem < 0) | |
1427 | return rem; | |
1428 | ||
1429 | data += (count - rem); | |
1430 | count = rem; | |
da5f6c37 | 1431 | }; |
99811510 SS |
1432 | |
1433 | return rem; | |
1434 | } | |
1435 | EXPORT_SYMBOL(hci_recv_stream_fragment); | |
1436 | ||
1da177e4 LT |
1437 | /* ---- Interface to upper protocols ---- */ |
1438 | ||
1439 | /* Register/Unregister protocols. | |
1440 | * hci_task_lock is used to ensure that no tasks are running. */ | |
1441 | int hci_register_proto(struct hci_proto *hp) | |
1442 | { | |
1443 | int err = 0; | |
1444 | ||
1445 | BT_DBG("%p name %s id %d", hp, hp->name, hp->id); | |
1446 | ||
1447 | if (hp->id >= HCI_MAX_PROTO) | |
1448 | return -EINVAL; | |
1449 | ||
1450 | write_lock_bh(&hci_task_lock); | |
1451 | ||
1452 | if (!hci_proto[hp->id]) | |
1453 | hci_proto[hp->id] = hp; | |
1454 | else | |
1455 | err = -EEXIST; | |
1456 | ||
1457 | write_unlock_bh(&hci_task_lock); | |
1458 | ||
1459 | return err; | |
1460 | } | |
1461 | EXPORT_SYMBOL(hci_register_proto); | |
1462 | ||
1463 | int hci_unregister_proto(struct hci_proto *hp) | |
1464 | { | |
1465 | int err = 0; | |
1466 | ||
1467 | BT_DBG("%p name %s id %d", hp, hp->name, hp->id); | |
1468 | ||
1469 | if (hp->id >= HCI_MAX_PROTO) | |
1470 | return -EINVAL; | |
1471 | ||
1472 | write_lock_bh(&hci_task_lock); | |
1473 | ||
1474 | if (hci_proto[hp->id]) | |
1475 | hci_proto[hp->id] = NULL; | |
1476 | else | |
1477 | err = -ENOENT; | |
1478 | ||
1479 | write_unlock_bh(&hci_task_lock); | |
1480 | ||
1481 | return err; | |
1482 | } | |
1483 | EXPORT_SYMBOL(hci_unregister_proto); | |
1484 | ||
1485 | int hci_register_cb(struct hci_cb *cb) | |
1486 | { | |
1487 | BT_DBG("%p name %s", cb, cb->name); | |
1488 | ||
1489 | write_lock_bh(&hci_cb_list_lock); | |
1490 | list_add(&cb->list, &hci_cb_list); | |
1491 | write_unlock_bh(&hci_cb_list_lock); | |
1492 | ||
1493 | return 0; | |
1494 | } | |
1495 | EXPORT_SYMBOL(hci_register_cb); | |
1496 | ||
1497 | int hci_unregister_cb(struct hci_cb *cb) | |
1498 | { | |
1499 | BT_DBG("%p name %s", cb, cb->name); | |
1500 | ||
1501 | write_lock_bh(&hci_cb_list_lock); | |
1502 | list_del(&cb->list); | |
1503 | write_unlock_bh(&hci_cb_list_lock); | |
1504 | ||
1505 | return 0; | |
1506 | } | |
1507 | EXPORT_SYMBOL(hci_unregister_cb); | |
1508 | ||
1509 | static int hci_send_frame(struct sk_buff *skb) | |
1510 | { | |
1511 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
1512 | ||
1513 | if (!hdev) { | |
1514 | kfree_skb(skb); | |
1515 | return -ENODEV; | |
1516 | } | |
1517 | ||
0d48d939 | 1518 | BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); |
1da177e4 LT |
1519 | |
1520 | if (atomic_read(&hdev->promisc)) { | |
1521 | /* Time stamp */ | |
a61bbcf2 | 1522 | __net_timestamp(skb); |
1da177e4 | 1523 | |
eec8d2bc | 1524 | hci_send_to_sock(hdev, skb, NULL); |
1da177e4 LT |
1525 | } |
1526 | ||
1527 | /* Get rid of skb owner, prior to sending to the driver. */ | |
1528 | skb_orphan(skb); | |
1529 | ||
1530 | return hdev->send(skb); | |
1531 | } | |
1532 | ||
1533 | /* Send HCI command */ | |
a9de9248 | 1534 | int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param) |
1da177e4 LT |
1535 | { |
1536 | int len = HCI_COMMAND_HDR_SIZE + plen; | |
1537 | struct hci_command_hdr *hdr; | |
1538 | struct sk_buff *skb; | |
1539 | ||
a9de9248 | 1540 | BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen); |
1da177e4 LT |
1541 | |
1542 | skb = bt_skb_alloc(len, GFP_ATOMIC); | |
1543 | if (!skb) { | |
ef222013 | 1544 | BT_ERR("%s no memory for command", hdev->name); |
1da177e4 LT |
1545 | return -ENOMEM; |
1546 | } | |
1547 | ||
1548 | hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); | |
a9de9248 | 1549 | hdr->opcode = cpu_to_le16(opcode); |
1da177e4 LT |
1550 | hdr->plen = plen; |
1551 | ||
1552 | if (plen) | |
1553 | memcpy(skb_put(skb, plen), param, plen); | |
1554 | ||
1555 | BT_DBG("skb len %d", skb->len); | |
1556 | ||
0d48d939 | 1557 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
1da177e4 | 1558 | skb->dev = (void *) hdev; |
c78ae283 | 1559 | |
a5040efa JH |
1560 | if (test_bit(HCI_INIT, &hdev->flags)) |
1561 | hdev->init_last_cmd = opcode; | |
1562 | ||
1da177e4 | 1563 | skb_queue_tail(&hdev->cmd_q, skb); |
c78ae283 | 1564 | tasklet_schedule(&hdev->cmd_task); |
1da177e4 LT |
1565 | |
1566 | return 0; | |
1567 | } | |
1da177e4 LT |
1568 | |
1569 | /* Get data from the previously sent command */ | |
a9de9248 | 1570 | void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) |
1da177e4 LT |
1571 | { |
1572 | struct hci_command_hdr *hdr; | |
1573 | ||
1574 | if (!hdev->sent_cmd) | |
1575 | return NULL; | |
1576 | ||
1577 | hdr = (void *) hdev->sent_cmd->data; | |
1578 | ||
a9de9248 | 1579 | if (hdr->opcode != cpu_to_le16(opcode)) |
1da177e4 LT |
1580 | return NULL; |
1581 | ||
a9de9248 | 1582 | BT_DBG("%s opcode 0x%x", hdev->name, opcode); |
1da177e4 LT |
1583 | |
1584 | return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; | |
1585 | } | |
1586 | ||
1587 | /* Send ACL data */ | |
1588 | static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) | |
1589 | { | |
1590 | struct hci_acl_hdr *hdr; | |
1591 | int len = skb->len; | |
1592 | ||
badff6d0 ACM |
1593 | skb_push(skb, HCI_ACL_HDR_SIZE); |
1594 | skb_reset_transport_header(skb); | |
9c70220b | 1595 | hdr = (struct hci_acl_hdr *)skb_transport_header(skb); |
aca3192c YH |
1596 | hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); |
1597 | hdr->dlen = cpu_to_le16(len); | |
1da177e4 LT |
1598 | } |
1599 | ||
9a9c6a34 | 1600 | void hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags) |
1da177e4 LT |
1601 | { |
1602 | struct hci_dev *hdev = conn->hdev; | |
1603 | struct sk_buff *list; | |
1604 | ||
1605 | BT_DBG("%s conn %p flags 0x%x", hdev->name, conn, flags); | |
1606 | ||
1607 | skb->dev = (void *) hdev; | |
0d48d939 | 1608 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
e702112f | 1609 | hci_add_acl_hdr(skb, conn->handle, flags); |
1da177e4 | 1610 | |
70f23020 AE |
1611 | list = skb_shinfo(skb)->frag_list; |
1612 | if (!list) { | |
1da177e4 LT |
1613 | /* Non fragmented */ |
1614 | BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); | |
1615 | ||
1616 | skb_queue_tail(&conn->data_q, skb); | |
1617 | } else { | |
1618 | /* Fragmented */ | |
1619 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); | |
1620 | ||
1621 | skb_shinfo(skb)->frag_list = NULL; | |
1622 | ||
1623 | /* Queue all fragments atomically */ | |
1624 | spin_lock_bh(&conn->data_q.lock); | |
1625 | ||
1626 | __skb_queue_tail(&conn->data_q, skb); | |
e702112f AE |
1627 | |
1628 | flags &= ~ACL_START; | |
1629 | flags |= ACL_CONT; | |
1da177e4 LT |
1630 | do { |
1631 | skb = list; list = list->next; | |
8e87d142 | 1632 | |
1da177e4 | 1633 | skb->dev = (void *) hdev; |
0d48d939 | 1634 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
e702112f | 1635 | hci_add_acl_hdr(skb, conn->handle, flags); |
1da177e4 LT |
1636 | |
1637 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); | |
1638 | ||
1639 | __skb_queue_tail(&conn->data_q, skb); | |
1640 | } while (list); | |
1641 | ||
1642 | spin_unlock_bh(&conn->data_q.lock); | |
1643 | } | |
1644 | ||
c78ae283 | 1645 | tasklet_schedule(&hdev->tx_task); |
1da177e4 LT |
1646 | } |
1647 | EXPORT_SYMBOL(hci_send_acl); | |
1648 | ||
1649 | /* Send SCO data */ | |
0d861d8b | 1650 | void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) |
1da177e4 LT |
1651 | { |
1652 | struct hci_dev *hdev = conn->hdev; | |
1653 | struct hci_sco_hdr hdr; | |
1654 | ||
1655 | BT_DBG("%s len %d", hdev->name, skb->len); | |
1656 | ||
aca3192c | 1657 | hdr.handle = cpu_to_le16(conn->handle); |
1da177e4 LT |
1658 | hdr.dlen = skb->len; |
1659 | ||
badff6d0 ACM |
1660 | skb_push(skb, HCI_SCO_HDR_SIZE); |
1661 | skb_reset_transport_header(skb); | |
9c70220b | 1662 | memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); |
1da177e4 LT |
1663 | |
1664 | skb->dev = (void *) hdev; | |
0d48d939 | 1665 | bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; |
c78ae283 | 1666 | |
1da177e4 | 1667 | skb_queue_tail(&conn->data_q, skb); |
c78ae283 | 1668 | tasklet_schedule(&hdev->tx_task); |
1da177e4 LT |
1669 | } |
1670 | EXPORT_SYMBOL(hci_send_sco); | |
1671 | ||
1672 | /* ---- HCI TX task (outgoing data) ---- */ | |
1673 | ||
1674 | /* HCI Connection scheduler */ | |
1675 | static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote) | |
1676 | { | |
1677 | struct hci_conn_hash *h = &hdev->conn_hash; | |
5b7f9909 | 1678 | struct hci_conn *conn = NULL; |
1da177e4 LT |
1679 | int num = 0, min = ~0; |
1680 | struct list_head *p; | |
1681 | ||
8e87d142 | 1682 | /* We don't have to lock device here. Connections are always |
1da177e4 LT |
1683 | * added and removed with TX task disabled. */ |
1684 | list_for_each(p, &h->list) { | |
1685 | struct hci_conn *c; | |
1686 | c = list_entry(p, struct hci_conn, list); | |
1687 | ||
769be974 | 1688 | if (c->type != type || skb_queue_empty(&c->data_q)) |
1da177e4 | 1689 | continue; |
769be974 MH |
1690 | |
1691 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) | |
1692 | continue; | |
1693 | ||
1da177e4 LT |
1694 | num++; |
1695 | ||
1696 | if (c->sent < min) { | |
1697 | min = c->sent; | |
1698 | conn = c; | |
1699 | } | |
1700 | } | |
1701 | ||
1702 | if (conn) { | |
6ed58ec5 VT |
1703 | int cnt, q; |
1704 | ||
1705 | switch (conn->type) { | |
1706 | case ACL_LINK: | |
1707 | cnt = hdev->acl_cnt; | |
1708 | break; | |
1709 | case SCO_LINK: | |
1710 | case ESCO_LINK: | |
1711 | cnt = hdev->sco_cnt; | |
1712 | break; | |
1713 | case LE_LINK: | |
1714 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; | |
1715 | break; | |
1716 | default: | |
1717 | cnt = 0; | |
1718 | BT_ERR("Unknown link type"); | |
1719 | } | |
1720 | ||
1721 | q = cnt / num; | |
1da177e4 LT |
1722 | *quote = q ? q : 1; |
1723 | } else | |
1724 | *quote = 0; | |
1725 | ||
1726 | BT_DBG("conn %p quote %d", conn, *quote); | |
1727 | return conn; | |
1728 | } | |
1729 | ||
bae1f5d9 | 1730 | static inline void hci_link_tx_to(struct hci_dev *hdev, __u8 type) |
1da177e4 LT |
1731 | { |
1732 | struct hci_conn_hash *h = &hdev->conn_hash; | |
1733 | struct list_head *p; | |
1734 | struct hci_conn *c; | |
1735 | ||
bae1f5d9 | 1736 | BT_ERR("%s link tx timeout", hdev->name); |
1da177e4 LT |
1737 | |
1738 | /* Kill stalled connections */ | |
1739 | list_for_each(p, &h->list) { | |
1740 | c = list_entry(p, struct hci_conn, list); | |
bae1f5d9 VT |
1741 | if (c->type == type && c->sent) { |
1742 | BT_ERR("%s killing stalled connection %s", | |
1da177e4 LT |
1743 | hdev->name, batostr(&c->dst)); |
1744 | hci_acl_disconn(c, 0x13); | |
1745 | } | |
1746 | } | |
1747 | } | |
1748 | ||
1749 | static inline void hci_sched_acl(struct hci_dev *hdev) | |
1750 | { | |
1751 | struct hci_conn *conn; | |
1752 | struct sk_buff *skb; | |
1753 | int quote; | |
1754 | ||
1755 | BT_DBG("%s", hdev->name); | |
1756 | ||
1757 | if (!test_bit(HCI_RAW, &hdev->flags)) { | |
1758 | /* ACL tx timeout must be longer than maximum | |
1759 | * link supervision timeout (40.9 seconds) */ | |
82453021 | 1760 | if (!hdev->acl_cnt && time_after(jiffies, hdev->acl_last_tx + HZ * 45)) |
bae1f5d9 | 1761 | hci_link_tx_to(hdev, ACL_LINK); |
1da177e4 LT |
1762 | } |
1763 | ||
1764 | while (hdev->acl_cnt && (conn = hci_low_sent(hdev, ACL_LINK, "e))) { | |
1765 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { | |
1766 | BT_DBG("skb %p len %d", skb, skb->len); | |
04837f64 MH |
1767 | |
1768 | hci_conn_enter_active_mode(conn); | |
1769 | ||
1da177e4 LT |
1770 | hci_send_frame(skb); |
1771 | hdev->acl_last_tx = jiffies; | |
1772 | ||
1773 | hdev->acl_cnt--; | |
1774 | conn->sent++; | |
1775 | } | |
1776 | } | |
1777 | } | |
1778 | ||
1779 | /* Schedule SCO */ | |
1780 | static inline void hci_sched_sco(struct hci_dev *hdev) | |
1781 | { | |
1782 | struct hci_conn *conn; | |
1783 | struct sk_buff *skb; | |
1784 | int quote; | |
1785 | ||
1786 | BT_DBG("%s", hdev->name); | |
1787 | ||
1788 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { | |
1789 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { | |
1790 | BT_DBG("skb %p len %d", skb, skb->len); | |
1791 | hci_send_frame(skb); | |
1792 | ||
1793 | conn->sent++; | |
1794 | if (conn->sent == ~0) | |
1795 | conn->sent = 0; | |
1796 | } | |
1797 | } | |
1798 | } | |
1799 | ||
b6a0dc82 MH |
1800 | static inline void hci_sched_esco(struct hci_dev *hdev) |
1801 | { | |
1802 | struct hci_conn *conn; | |
1803 | struct sk_buff *skb; | |
1804 | int quote; | |
1805 | ||
1806 | BT_DBG("%s", hdev->name); | |
1807 | ||
1808 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, "e))) { | |
1809 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { | |
1810 | BT_DBG("skb %p len %d", skb, skb->len); | |
1811 | hci_send_frame(skb); | |
1812 | ||
1813 | conn->sent++; | |
1814 | if (conn->sent == ~0) | |
1815 | conn->sent = 0; | |
1816 | } | |
1817 | } | |
1818 | } | |
1819 | ||
6ed58ec5 VT |
1820 | static inline void hci_sched_le(struct hci_dev *hdev) |
1821 | { | |
1822 | struct hci_conn *conn; | |
1823 | struct sk_buff *skb; | |
1824 | int quote, cnt; | |
1825 | ||
1826 | BT_DBG("%s", hdev->name); | |
1827 | ||
1828 | if (!test_bit(HCI_RAW, &hdev->flags)) { | |
1829 | /* LE tx timeout must be longer than maximum | |
1830 | * link supervision timeout (40.9 seconds) */ | |
bae1f5d9 | 1831 | if (!hdev->le_cnt && hdev->le_pkts && |
6ed58ec5 | 1832 | time_after(jiffies, hdev->le_last_tx + HZ * 45)) |
bae1f5d9 | 1833 | hci_link_tx_to(hdev, LE_LINK); |
6ed58ec5 VT |
1834 | } |
1835 | ||
1836 | cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; | |
1837 | while (cnt && (conn = hci_low_sent(hdev, LE_LINK, "e))) { | |
1838 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { | |
1839 | BT_DBG("skb %p len %d", skb, skb->len); | |
1840 | ||
1841 | hci_send_frame(skb); | |
1842 | hdev->le_last_tx = jiffies; | |
1843 | ||
1844 | cnt--; | |
1845 | conn->sent++; | |
1846 | } | |
1847 | } | |
1848 | if (hdev->le_pkts) | |
1849 | hdev->le_cnt = cnt; | |
1850 | else | |
1851 | hdev->acl_cnt = cnt; | |
1852 | } | |
1853 | ||
1da177e4 LT |
1854 | static void hci_tx_task(unsigned long arg) |
1855 | { | |
1856 | struct hci_dev *hdev = (struct hci_dev *) arg; | |
1857 | struct sk_buff *skb; | |
1858 | ||
1859 | read_lock(&hci_task_lock); | |
1860 | ||
6ed58ec5 VT |
1861 | BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, |
1862 | hdev->sco_cnt, hdev->le_cnt); | |
1da177e4 LT |
1863 | |
1864 | /* Schedule queues and send stuff to HCI driver */ | |
1865 | ||
1866 | hci_sched_acl(hdev); | |
1867 | ||
1868 | hci_sched_sco(hdev); | |
1869 | ||
b6a0dc82 MH |
1870 | hci_sched_esco(hdev); |
1871 | ||
6ed58ec5 VT |
1872 | hci_sched_le(hdev); |
1873 | ||
1da177e4 LT |
1874 | /* Send next queued raw (unknown type) packet */ |
1875 | while ((skb = skb_dequeue(&hdev->raw_q))) | |
1876 | hci_send_frame(skb); | |
1877 | ||
1878 | read_unlock(&hci_task_lock); | |
1879 | } | |
1880 | ||
1881 | /* ----- HCI RX task (incoming data proccessing) ----- */ | |
1882 | ||
1883 | /* ACL data packet */ | |
1884 | static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) | |
1885 | { | |
1886 | struct hci_acl_hdr *hdr = (void *) skb->data; | |
1887 | struct hci_conn *conn; | |
1888 | __u16 handle, flags; | |
1889 | ||
1890 | skb_pull(skb, HCI_ACL_HDR_SIZE); | |
1891 | ||
1892 | handle = __le16_to_cpu(hdr->handle); | |
1893 | flags = hci_flags(handle); | |
1894 | handle = hci_handle(handle); | |
1895 | ||
1896 | BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags); | |
1897 | ||
1898 | hdev->stat.acl_rx++; | |
1899 | ||
1900 | hci_dev_lock(hdev); | |
1901 | conn = hci_conn_hash_lookup_handle(hdev, handle); | |
1902 | hci_dev_unlock(hdev); | |
8e87d142 | 1903 | |
1da177e4 LT |
1904 | if (conn) { |
1905 | register struct hci_proto *hp; | |
1906 | ||
04837f64 MH |
1907 | hci_conn_enter_active_mode(conn); |
1908 | ||
1da177e4 | 1909 | /* Send to upper protocol */ |
70f23020 AE |
1910 | hp = hci_proto[HCI_PROTO_L2CAP]; |
1911 | if (hp && hp->recv_acldata) { | |
1da177e4 LT |
1912 | hp->recv_acldata(conn, skb, flags); |
1913 | return; | |
1914 | } | |
1915 | } else { | |
8e87d142 | 1916 | BT_ERR("%s ACL packet for unknown connection handle %d", |
1da177e4 LT |
1917 | hdev->name, handle); |
1918 | } | |
1919 | ||
1920 | kfree_skb(skb); | |
1921 | } | |
1922 | ||
1923 | /* SCO data packet */ | |
1924 | static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) | |
1925 | { | |
1926 | struct hci_sco_hdr *hdr = (void *) skb->data; | |
1927 | struct hci_conn *conn; | |
1928 | __u16 handle; | |
1929 | ||
1930 | skb_pull(skb, HCI_SCO_HDR_SIZE); | |
1931 | ||
1932 | handle = __le16_to_cpu(hdr->handle); | |
1933 | ||
1934 | BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle); | |
1935 | ||
1936 | hdev->stat.sco_rx++; | |
1937 | ||
1938 | hci_dev_lock(hdev); | |
1939 | conn = hci_conn_hash_lookup_handle(hdev, handle); | |
1940 | hci_dev_unlock(hdev); | |
1941 | ||
1942 | if (conn) { | |
1943 | register struct hci_proto *hp; | |
1944 | ||
1945 | /* Send to upper protocol */ | |
70f23020 AE |
1946 | hp = hci_proto[HCI_PROTO_SCO]; |
1947 | if (hp && hp->recv_scodata) { | |
1da177e4 LT |
1948 | hp->recv_scodata(conn, skb); |
1949 | return; | |
1950 | } | |
1951 | } else { | |
8e87d142 | 1952 | BT_ERR("%s SCO packet for unknown connection handle %d", |
1da177e4 LT |
1953 | hdev->name, handle); |
1954 | } | |
1955 | ||
1956 | kfree_skb(skb); | |
1957 | } | |
1958 | ||
6516455d | 1959 | static void hci_rx_task(unsigned long arg) |
1da177e4 LT |
1960 | { |
1961 | struct hci_dev *hdev = (struct hci_dev *) arg; | |
1962 | struct sk_buff *skb; | |
1963 | ||
1964 | BT_DBG("%s", hdev->name); | |
1965 | ||
1966 | read_lock(&hci_task_lock); | |
1967 | ||
1968 | while ((skb = skb_dequeue(&hdev->rx_q))) { | |
1969 | if (atomic_read(&hdev->promisc)) { | |
1970 | /* Send copy to the sockets */ | |
eec8d2bc | 1971 | hci_send_to_sock(hdev, skb, NULL); |
1da177e4 LT |
1972 | } |
1973 | ||
1974 | if (test_bit(HCI_RAW, &hdev->flags)) { | |
1975 | kfree_skb(skb); | |
1976 | continue; | |
1977 | } | |
1978 | ||
1979 | if (test_bit(HCI_INIT, &hdev->flags)) { | |
1980 | /* Don't process data packets in this states. */ | |
0d48d939 | 1981 | switch (bt_cb(skb)->pkt_type) { |
1da177e4 LT |
1982 | case HCI_ACLDATA_PKT: |
1983 | case HCI_SCODATA_PKT: | |
1984 | kfree_skb(skb); | |
1985 | continue; | |
3ff50b79 | 1986 | } |
1da177e4 LT |
1987 | } |
1988 | ||
1989 | /* Process frame */ | |
0d48d939 | 1990 | switch (bt_cb(skb)->pkt_type) { |
1da177e4 LT |
1991 | case HCI_EVENT_PKT: |
1992 | hci_event_packet(hdev, skb); | |
1993 | break; | |
1994 | ||
1995 | case HCI_ACLDATA_PKT: | |
1996 | BT_DBG("%s ACL data packet", hdev->name); | |
1997 | hci_acldata_packet(hdev, skb); | |
1998 | break; | |
1999 | ||
2000 | case HCI_SCODATA_PKT: | |
2001 | BT_DBG("%s SCO data packet", hdev->name); | |
2002 | hci_scodata_packet(hdev, skb); | |
2003 | break; | |
2004 | ||
2005 | default: | |
2006 | kfree_skb(skb); | |
2007 | break; | |
2008 | } | |
2009 | } | |
2010 | ||
2011 | read_unlock(&hci_task_lock); | |
2012 | } | |
2013 | ||
2014 | static void hci_cmd_task(unsigned long arg) | |
2015 | { | |
2016 | struct hci_dev *hdev = (struct hci_dev *) arg; | |
2017 | struct sk_buff *skb; | |
2018 | ||
2019 | BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt)); | |
2020 | ||
1da177e4 | 2021 | /* Send queued commands */ |
5a08ecce AE |
2022 | if (atomic_read(&hdev->cmd_cnt)) { |
2023 | skb = skb_dequeue(&hdev->cmd_q); | |
2024 | if (!skb) | |
2025 | return; | |
2026 | ||
7585b97a | 2027 | kfree_skb(hdev->sent_cmd); |
1da177e4 | 2028 | |
70f23020 AE |
2029 | hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC); |
2030 | if (hdev->sent_cmd) { | |
1da177e4 LT |
2031 | atomic_dec(&hdev->cmd_cnt); |
2032 | hci_send_frame(skb); | |
6bd32326 VT |
2033 | mod_timer(&hdev->cmd_timer, |
2034 | jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT)); | |
1da177e4 LT |
2035 | } else { |
2036 | skb_queue_head(&hdev->cmd_q, skb); | |
c78ae283 | 2037 | tasklet_schedule(&hdev->cmd_task); |
1da177e4 LT |
2038 | } |
2039 | } | |
2040 | } |