hci_core.c 29.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* 
   BlueZ - Bluetooth protocol stack for Linux
   Copyright (C) 2000-2001 Qualcomm Incorporated

   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 as
   published by the Free Software Foundation;

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
   SOFTWARE IS DISCLAIMED.
*/

/*
26
 * Bluetooth HCI Core.
Linus Torvalds's avatar
Linus Torvalds committed
27
 *
28
 * $Id: hci_core.c,v 1.6 2002/04/17 17:37:16 maxk Exp $
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32
 */

#include <linux/config.h>
#include <linux/module.h>
33
#include <linux/kmod.h>
Linus Torvalds's avatar
Linus Torvalds committed
34 35 36 37 38 39

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
Linus Torvalds's avatar
Linus Torvalds committed
40
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43 44 45 46 47 48 49 50
#include <linux/poll.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/interrupt.h>
#include <linux/notifier.h>
#include <net/sock.h>

#include <asm/system.h>
#include <asm/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
51
#include <asm/unaligned.h>
Linus Torvalds's avatar
Linus Torvalds committed
52 53 54 55

#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>

56
#ifndef CONFIG_BT_HCI_CORE_DEBUG
57 58
#undef  BT_DBG
#define BT_DBG( A... )
Linus Torvalds's avatar
Linus Torvalds committed
59 60 61 62 63 64 65
#endif

static void hci_cmd_task(unsigned long arg);
static void hci_rx_task(unsigned long arg);
static void hci_tx_task(unsigned long arg);
static void hci_notify(struct hci_dev *hdev, int event);

66
rwlock_t hci_task_lock = RW_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
67 68

/* HCI device list */
69 70
LIST_HEAD(hci_dev_list);
rwlock_t hci_dev_list_lock = RW_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
71

72 73 74
/* HCI protocols */
#define HCI_MAX_PROTO	2
struct hci_proto *hci_proto[HCI_MAX_PROTO];
Linus Torvalds's avatar
Linus Torvalds committed
75 76

/* HCI notifiers list */
77
static struct notifier_block *hci_notifier;
Linus Torvalds's avatar
Linus Torvalds committed
78

79
/* ---- HCI notifications ---- */
Linus Torvalds's avatar
Linus Torvalds committed
80

81
int hci_register_notifier(struct notifier_block *nb)
Linus Torvalds's avatar
Linus Torvalds committed
82
{
83
	return notifier_chain_register(&hci_notifier, nb);
Linus Torvalds's avatar
Linus Torvalds committed
84 85
}

86
int hci_unregister_notifier(struct notifier_block *nb)
Linus Torvalds's avatar
Linus Torvalds committed
87
{
88
	return notifier_chain_unregister(&hci_notifier, nb);
Linus Torvalds's avatar
Linus Torvalds committed
89 90
}

91
void hci_notify(struct hci_dev *hdev, int event)
Linus Torvalds's avatar
Linus Torvalds committed
92
{
93
	notifier_call_chain(&hci_notifier, event, hdev);
Linus Torvalds's avatar
Linus Torvalds committed
94 95
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/* ---- HCI hotplug support ---- */

#ifdef CONFIG_HOTPLUG

static int hci_run_hotplug(char *dev, char *action)
{
	char *argv[3], *envp[5], dstr[20], astr[32];

	sprintf(dstr, "DEVICE=%s", dev);
	sprintf(astr, "ACTION=%s", action);

        argv[0] = hotplug_path;
        argv[1] = "bluetooth";
        argv[2] = NULL;

	envp[0] = "HOME=/";
	envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
	envp[2] = dstr;
	envp[3] = astr;
	envp[4] = NULL;
	
117
	return call_usermodehelper(argv[0], argv, envp, 0);
118 119 120 121
}
#else
#define hci_run_hotplug(A...)
#endif
Linus Torvalds's avatar
Linus Torvalds committed
122

123
/* ---- HCI requests ---- */
Linus Torvalds's avatar
Linus Torvalds committed
124

125
void hci_req_complete(struct hci_dev *hdev, int result)
Linus Torvalds's avatar
Linus Torvalds committed
126
{
127
	BT_DBG("%s result 0x%2.2x", hdev->name, result);
Linus Torvalds's avatar
Linus Torvalds committed
128 129 130 131 132 133 134 135

	if (hdev->req_status == HCI_REQ_PEND) {
		hdev->req_result = result;
		hdev->req_status = HCI_REQ_DONE;
		wake_up_interruptible(&hdev->req_wait_q);
	}
}

136
void hci_req_cancel(struct hci_dev *hdev, int err)
Linus Torvalds's avatar
Linus Torvalds committed
137
{
138
	BT_DBG("%s err 0x%2.2x", hdev->name, err);
Linus Torvalds's avatar
Linus Torvalds committed
139 140 141 142 143 144 145 146 147

	if (hdev->req_status == HCI_REQ_PEND) {
		hdev->req_result = err;
		hdev->req_status = HCI_REQ_CANCELED;
		wake_up_interruptible(&hdev->req_wait_q);
	}
}

/* Execute request and wait for completion. */
148 149
static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), 
				unsigned long opt, __u32 timeout)
Linus Torvalds's avatar
Linus Torvalds committed
150 151 152 153
{
	DECLARE_WAITQUEUE(wait, current);
	int err = 0;

154
	BT_DBG("%s start", hdev->name);
Linus Torvalds's avatar
Linus Torvalds committed
155 156 157 158

	hdev->req_status = HCI_REQ_PEND;

	add_wait_queue(&hdev->req_wait_q, &wait);
159
	set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163

	req(hdev, opt);
	schedule_timeout(timeout);

164
	set_current_state(TASK_RUNNING);
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168 169 170 171
	remove_wait_queue(&hdev->req_wait_q, &wait);

	if (signal_pending(current))
		return -EINTR;

	switch (hdev->req_status) {
	case HCI_REQ_DONE:
172
		err = -bt_err(hdev->req_result);
Linus Torvalds's avatar
Linus Torvalds committed
173 174 175 176 177 178 179 180 181 182 183 184 185
		break;

	case HCI_REQ_CANCELED:
		err = -hdev->req_result;
		break;

	default:
		err = -ETIMEDOUT;
		break;
	};

	hdev->req_status = hdev->req_result = 0;

186
	BT_DBG("%s end: err %d", hdev->name, err);
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189 190

	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
191
static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
192
				unsigned long opt, __u32 timeout)
Linus Torvalds's avatar
Linus Torvalds committed
193 194 195 196 197 198 199 200 201 202 203 204 205
{
	int ret;

	/* Serialize all requests */
	hci_req_lock(hdev);
	ret = __hci_request(hdev, req, opt, timeout);
	hci_req_unlock(hdev);

	return ret;
}

static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
{
206
	BT_DBG("%s %ld", hdev->name, opt);
Linus Torvalds's avatar
Linus Torvalds committed
207 208 209 210 211 212 213 214 215

	/* Reset device */
	hci_send_cmd(hdev, OGF_HOST_CTL, OCF_RESET, 0, NULL);
}

static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
{
	__u16 param;

216
	BT_DBG("%s %ld", hdev->name, opt);
Linus Torvalds's avatar
Linus Torvalds committed
217 218 219

	/* Mandatory initialization */

Linus Torvalds's avatar
Linus Torvalds committed
220 221 222
	/* Read Local Supported Features */
	hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_LOCAL_FEATURES, 0, NULL);

Linus Torvalds's avatar
Linus Torvalds committed
223 224 225
	/* Read Buffer Size (ACL mtu, max pkt, etc.) */
	hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_BUFFER_SIZE, 0, NULL);

226 227 228
#if 0
	/* Host buffer size */
	{
229
		struct hci_cp_host_buffer_size cp;
230 231 232 233 234
		cp.acl_mtu = __cpu_to_le16(HCI_MAX_ACL_SIZE);
		cp.sco_mtu = HCI_MAX_SCO_SIZE;
		cp.acl_max_pkt = __cpu_to_le16(0xffff);
		cp.sco_max_pkt = __cpu_to_le16(0xffff);
		hci_send_cmd(hdev, OGF_HOST_CTL, OCF_HOST_BUFFER_SIZE, sizeof(cp), &cp);
235 236 237
	}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
238 239 240 241 242 243
	/* Read BD Address */
	hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_BD_ADDR, 0, NULL);

	/* Optional initialization */

	/* Clear Event Filters */
244
	{
245 246
		struct hci_cp_set_event_flt cp;
		cp.flt_type  = HCI_FLT_CLEAR_ALL;
247 248
		hci_send_cmd(hdev, OGF_HOST_CTL, OCF_SET_EVENT_FLT, sizeof(cp), &cp);
	}
Linus Torvalds's avatar
Linus Torvalds committed
249

Linus Torvalds's avatar
Linus Torvalds committed
250
	/* Page timeout ~20 secs */
Linus Torvalds's avatar
Linus Torvalds committed
251 252 253 254 255 256 257 258 259 260 261 262
	param = __cpu_to_le16(0x8000);
	hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_PG_TIMEOUT, 2, &param);

	/* Connection accept timeout ~20 secs */
	param = __cpu_to_le16(0x7d00);
	hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_CA_TIMEOUT, 2, &param);
}

static void hci_scan_req(struct hci_dev *hdev, unsigned long opt)
{
	__u8 scan = opt;

263
	BT_DBG("%s %x", hdev->name, scan);
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267 268 269 270 271 272

	/* Inquiry and Page scans */
	hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE, 1, &scan);
}

static void hci_auth_req(struct hci_dev *hdev, unsigned long opt)
{
	__u8 auth = opt;

273
	BT_DBG("%s %x", hdev->name, auth);
Linus Torvalds's avatar
Linus Torvalds committed
274 275 276 277 278

	/* Authentication */
	hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE, 1, &auth);
}

279
static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
Linus Torvalds's avatar
Linus Torvalds committed
280
{
281
	__u8 encrypt = opt;
Linus Torvalds's avatar
Linus Torvalds committed
282

283
	BT_DBG("%s %x", hdev->name, encrypt);
Linus Torvalds's avatar
Linus Torvalds committed
284

285 286
	/* Authentication */
	hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE, 1, &encrypt);
Linus Torvalds's avatar
Linus Torvalds committed
287 288
}

289
/* Get HCI device by index. 
290
 * Device is held on return. */
291
struct hci_dev *hci_dev_get(int index)
Linus Torvalds's avatar
Linus Torvalds committed
292
{
293
	struct hci_dev *hdev = NULL;
294
	struct list_head *p;
Linus Torvalds's avatar
Linus Torvalds committed
295

296
	BT_DBG("%d", index);
Linus Torvalds's avatar
Linus Torvalds committed
297

298 299
	if (index < 0)
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
300

301 302
	read_lock(&hci_dev_list_lock);
	list_for_each(p, &hci_dev_list) {
303 304 305 306
		struct hci_dev *d = list_entry(p, struct hci_dev, list);
		if (d->id == index) {
			hdev = hci_dev_hold(d);
			break;
307
		}
Linus Torvalds's avatar
Linus Torvalds committed
308
	}
309
	read_unlock(&hci_dev_list_lock);
310 311
	return hdev;
}
Linus Torvalds's avatar
Linus Torvalds committed
312

313 314 315 316 317
/* ---- Inquiry support ---- */
void inquiry_cache_flush(struct hci_dev *hdev)
{
	struct inquiry_cache *cache = &hdev->inq_cache;
	struct inquiry_entry *next  = cache->list, *e;
Linus Torvalds's avatar
Linus Torvalds committed
318

319
	BT_DBG("cache %p", cache);
Linus Torvalds's avatar
Linus Torvalds committed
320

321 322 323 324
	cache->list = NULL;
	while ((e = next)) {
		next = e->next;
		kfree(e);
Linus Torvalds's avatar
Linus Torvalds committed
325
	}
326
}
Linus Torvalds's avatar
Linus Torvalds committed
327

328 329 330 331
struct inquiry_entry *inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
{
	struct inquiry_cache *cache = &hdev->inq_cache;
	struct inquiry_entry *e;
Linus Torvalds's avatar
Linus Torvalds committed
332

333
	BT_DBG("cache %p, %s", cache, batostr(bdaddr));
Linus Torvalds's avatar
Linus Torvalds committed
334

335 336 337 338 339
	for (e = cache->list; e; e = e->next)
		if (!bacmp(&e->info.bdaddr, bdaddr))
			break;
	return e;
}
Linus Torvalds's avatar
Linus Torvalds committed
340

341
void inquiry_cache_update(struct hci_dev *hdev, struct inquiry_info *info)
342 343 344
{
	struct inquiry_cache *cache = &hdev->inq_cache;
	struct inquiry_entry *e;
Linus Torvalds's avatar
Linus Torvalds committed
345

346
	BT_DBG("cache %p, %s", cache, batostr(&info->bdaddr));
Linus Torvalds's avatar
Linus Torvalds committed
347

348 349 350 351 352 353 354 355
	if (!(e = inquiry_cache_lookup(hdev, &info->bdaddr))) {
		/* Entry not in the cache. Add new one. */
		if (!(e = kmalloc(sizeof(struct inquiry_entry), GFP_ATOMIC)))
			return;
		memset(e, 0, sizeof(struct inquiry_entry));
		e->next     = cache->list;
		cache->list = e;
	}
Linus Torvalds's avatar
Linus Torvalds committed
356

357
	memcpy(&e->info, info, sizeof(*info));
358 359
	e->timestamp = jiffies;
	cache->timestamp = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
360 361
}

362
int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
Linus Torvalds's avatar
Linus Torvalds committed
363
{
364
	struct inquiry_cache *cache = &hdev->inq_cache;
365
	struct inquiry_info *info = (struct inquiry_info *) buf;
366 367
	struct inquiry_entry *e;
	int copied = 0;
Linus Torvalds's avatar
Linus Torvalds committed
368

369
	for (e = cache->list; e && copied < num; e = e->next, copied++)
370
		memcpy(info++, &e->info, sizeof(*info));
Linus Torvalds's avatar
Linus Torvalds committed
371

372 373 374
	BT_DBG("cache %p, copied %d", cache, copied);
	return copied;
}
Linus Torvalds's avatar
Linus Torvalds committed
375

376 377 378
static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
{
	struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
379
	struct hci_cp_inquiry cp;
Linus Torvalds's avatar
Linus Torvalds committed
380

381
	BT_DBG("%s", hdev->name);
Linus Torvalds's avatar
Linus Torvalds committed
382

383 384
	if (test_bit(HCI_INQUIRY, &hdev->flags))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
385

386
	/* Start Inquiry */
387 388 389 390
	memcpy(&cp.lap, &ir->lap, 3);
	cp.length  = ir->length;
	cp.num_rsp = ir->num_rsp;
	hci_send_cmd(hdev, OGF_LINK_CTL, OCF_INQUIRY, sizeof(cp), &cp);
391
}
Linus Torvalds's avatar
Linus Torvalds committed
392

393 394 395 396
int hci_inquiry(unsigned long arg)
{
	struct hci_inquiry_req ir;
	struct hci_dev *hdev;
397
	int err = 0, do_inquiry = 0, max_rsp;
398 399
	long timeo;
	__u8 *buf, *ptr;
Linus Torvalds's avatar
Linus Torvalds committed
400

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	ptr = (void *) arg;
	if (copy_from_user(&ir, ptr, sizeof(ir)))
		return -EFAULT;

	if (!(hdev = hci_dev_get(ir.dev_id)))
		return -ENODEV;

	hci_dev_lock_bh(hdev);
	if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || 
					ir.flags & IREQ_CACHE_FLUSH) {
		inquiry_cache_flush(hdev);
		do_inquiry = 1;
	}
	hci_dev_unlock_bh(hdev);

	timeo = ir.length * 2 * HZ;
	if (do_inquiry && (err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo)) < 0)
		goto done;

420 421 422
	/* for unlimited number of responses we will use buffer with 255 entries */
	max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;

423 424 425
	/* cache_dump can't sleep. Therefore we allocate temp buffer and then
	 * copy it to the user space.
	 */
426
	if (!(buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL))) {
427 428 429 430 431
		err = -ENOMEM;
		goto done;
	}

	hci_dev_lock_bh(hdev);
432
	ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
433 434 435 436 437
	hci_dev_unlock_bh(hdev);

	BT_DBG("num_rsp %d", ir.num_rsp);

	if (!verify_area(VERIFY_WRITE, ptr, sizeof(ir) + 
438
			(sizeof(struct inquiry_info) * ir.num_rsp))) {
439 440
		copy_to_user(ptr, &ir, sizeof(ir));
		ptr += sizeof(ir);
441
	        copy_to_user(ptr, buf, sizeof(struct inquiry_info) * ir.num_rsp);
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	} else 
		err = -EFAULT;

	kfree(buf);

done:
	hci_dev_put(hdev);
	return err;
}

/* ---- HCI ioctl helpers ---- */

int hci_dev_open(__u16 dev)
{
	struct hci_dev *hdev;
	int ret = 0;

	if (!(hdev = hci_dev_get(dev)))
		return -ENODEV;

	BT_DBG("%s %p", hdev->name, hdev);

	hci_req_lock(hdev);

	if (test_bit(HCI_UP, &hdev->flags)) {
		ret = -EALREADY;
		goto done;
	}

	if (hdev->open(hdev)) {
		ret = -EIO;
		goto done;
	}

	if (!test_bit(HCI_RAW, &hdev->flags)) {
		atomic_set(&hdev->cmd_cnt, 1);
		set_bit(HCI_INIT, &hdev->flags);

		//__hci_request(hdev, hci_reset_req, 0, HZ);
		ret = __hci_request(hdev, hci_init_req, 0, HCI_INIT_TIMEOUT);
       
		clear_bit(HCI_INIT, &hdev->flags);
	}

	if (!ret) {
487
		hci_dev_hold(hdev);
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
		set_bit(HCI_UP, &hdev->flags);
		hci_notify(hdev, HCI_DEV_UP);
	} else {	
		/* Init failed, cleanup */
		tasklet_kill(&hdev->rx_task);
		tasklet_kill(&hdev->tx_task);
		tasklet_kill(&hdev->cmd_task);

		skb_queue_purge(&hdev->cmd_q);
		skb_queue_purge(&hdev->rx_q);

		if (hdev->flush)
			hdev->flush(hdev);

		if (hdev->sent_cmd) {
			kfree_skb(hdev->sent_cmd);
			hdev->sent_cmd = NULL;
		}

		hdev->close(hdev);
		hdev->flags = 0;
	}

done:
	hci_req_unlock(hdev);
	hci_dev_put(hdev);
	return ret;
}

static int hci_dev_do_close(struct hci_dev *hdev)
{
	BT_DBG("%s %p", hdev->name, hdev);

	hci_req_cancel(hdev, ENODEV);
	hci_req_lock(hdev);

	if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
		hci_req_unlock(hdev);
		return 0;
	}

	/* Kill RX and TX tasks */
	tasklet_kill(&hdev->rx_task);
	tasklet_kill(&hdev->tx_task);
Linus Torvalds's avatar
Linus Torvalds committed
532

533 534 535 536 537
	hci_dev_lock_bh(hdev);
	inquiry_cache_flush(hdev);
	hci_conn_hash_flush(hdev);
	hci_dev_unlock_bh(hdev);
	
Linus Torvalds's avatar
Linus Torvalds committed
538 539 540 541 542 543
	hci_notify(hdev, HCI_DEV_DOWN);

	if (hdev->flush)
		hdev->flush(hdev);

	/* Reset device */
Linus Torvalds's avatar
Linus Torvalds committed
544
	skb_queue_purge(&hdev->cmd_q);
Linus Torvalds's avatar
Linus Torvalds committed
545
	atomic_set(&hdev->cmd_cnt, 1);
546 547 548
	set_bit(HCI_INIT, &hdev->flags);
	__hci_request(hdev, hci_reset_req, 0, HZ/4);
	clear_bit(HCI_INIT, &hdev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
549 550 551 552 553

	/* Kill cmd task */
	tasklet_kill(&hdev->cmd_task);

	/* Drop queues */
Linus Torvalds's avatar
Linus Torvalds committed
554 555 556
	skb_queue_purge(&hdev->rx_q);
	skb_queue_purge(&hdev->cmd_q);
	skb_queue_purge(&hdev->raw_q);
Linus Torvalds's avatar
Linus Torvalds committed
557 558

	/* Drop last sent command */
Linus Torvalds's avatar
Linus Torvalds committed
559 560 561
	if (hdev->sent_cmd) {
		kfree_skb(hdev->sent_cmd);
		hdev->sent_cmd = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
562 563 564
	}

	/* After this point our queues are empty
565
	 * and no tasks are scheduled. */
Linus Torvalds's avatar
Linus Torvalds committed
566 567
	hdev->close(hdev);

568 569
	/* Clear flags */
	hdev->flags = 0;
Linus Torvalds's avatar
Linus Torvalds committed
570

571
	hci_req_unlock(hdev);
572 573

	hci_dev_put(hdev);
Linus Torvalds's avatar
Linus Torvalds committed
574 575 576
	return 0;
}

577 578 579 580 581 582 583 584 585 586 587 588
int hci_dev_close(__u16 dev)
{
	struct hci_dev *hdev;
	int err;
	
	if (!(hdev = hci_dev_get(dev)))
		return -ENODEV;
	err = hci_dev_do_close(hdev);
	hci_dev_put(hdev);
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
589 590 591 592 593 594 595 596 597 598 599
int hci_dev_reset(__u16 dev)
{
	struct hci_dev *hdev;
	int ret = 0;

	if (!(hdev = hci_dev_get(dev)))
		return -ENODEV;

	hci_req_lock(hdev);
	tasklet_disable(&hdev->tx_task);

600
	if (!test_bit(HCI_UP, &hdev->flags))
Linus Torvalds's avatar
Linus Torvalds committed
601 602 603 604 605 606
		goto done;

	/* Drop queues */
	skb_queue_purge(&hdev->rx_q);
	skb_queue_purge(&hdev->cmd_q);

607 608
	hci_dev_lock_bh(hdev);
	inquiry_cache_flush(hdev);
Linus Torvalds's avatar
Linus Torvalds committed
609
	hci_conn_hash_flush(hdev);
610
	hci_dev_unlock_bh(hdev);
Linus Torvalds's avatar
Linus Torvalds committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

	if (hdev->flush)
		hdev->flush(hdev);

	atomic_set(&hdev->cmd_cnt, 1); 
	hdev->acl_cnt = 0; hdev->sco_cnt = 0;

	ret = __hci_request(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT);

done:
	tasklet_enable(&hdev->tx_task);
	hci_req_unlock(hdev);
	hci_dev_put(hdev);
	return ret;
}

int hci_dev_reset_stat(__u16 dev)
{
	struct hci_dev *hdev;
	int ret = 0;

	if (!(hdev = hci_dev_get(dev)))
		return -ENODEV;

	memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));

	hci_dev_put(hdev);

	return ret;
}

642
int hci_dev_cmd(unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
643 644 645
{
	struct hci_dev *hdev;
	struct hci_dev_req dr;
646
	int err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
647 648 649 650 651 652 653

	if (copy_from_user(&dr, (void *) arg, sizeof(dr)))
		return -EFAULT;

	if (!(hdev = hci_dev_get(dr.dev_id)))
		return -ENODEV;

654 655 656 657
	switch (cmd) {
	case HCISETAUTH:
		err = hci_request(hdev, hci_auth_req, dr.dev_opt, HCI_INIT_TIMEOUT);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
658

659 660 661 662 663
	case HCISETENCRYPT:
		if (!lmp_encrypt_capable(hdev)) {
			err = -EOPNOTSUPP;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
664

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
		if (!test_bit(HCI_AUTH, &hdev->flags)) {
			/* Auth must be enabled first */
			err = hci_request(hdev, hci_auth_req,
					dr.dev_opt, HCI_INIT_TIMEOUT);
			if (err)
				break;
		}
			
		err = hci_request(hdev, hci_encrypt_req,
					dr.dev_opt, HCI_INIT_TIMEOUT);
		break;
	
	case HCISETSCAN:
		err = hci_request(hdev, hci_scan_req, dr.dev_opt, HCI_INIT_TIMEOUT);
		break;
	
	case HCISETPTYPE:
		hdev->pkt_type = (__u16) dr.dev_opt;
		break;
		
	case HCISETLINKPOL:
		hdev->link_policy = (__u16) dr.dev_opt;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
688

689 690 691
	case HCISETLINKMODE:
		hdev->link_mode = ((__u16) dr.dev_opt) & (HCI_LM_MASTER | HCI_LM_ACCEPT);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
692

693 694 695 696
	case HCISETACLMTU:
		hdev->acl_mtu  = *((__u16 *)&dr.dev_opt + 1);
		hdev->acl_pkts = *((__u16 *)&dr.dev_opt + 0);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
697

698 699 700 701
	case HCISETSCOMTU:
		hdev->sco_mtu  = *((__u16 *)&dr.dev_opt + 1);
		hdev->sco_pkts = *((__u16 *)&dr.dev_opt + 0);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
702

703 704 705 706
	default:
		err = -EINVAL;
		break;
	}	
Linus Torvalds's avatar
Linus Torvalds committed
707
	hci_dev_put(hdev);
708
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
709 710
}

711
int hci_get_dev_list(unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
712 713 714
{
	struct hci_dev_list_req *dl;
	struct hci_dev_req *dr;
715 716
	struct list_head *p;
	int n = 0, size;
Linus Torvalds's avatar
Linus Torvalds committed
717 718 719 720 721
	__u16 dev_num;

	if (get_user(dev_num, (__u16 *) arg))
		return -EFAULT;

722
	if (!dev_num)
Linus Torvalds's avatar
Linus Torvalds committed
723 724
		return -EINVAL;
	
725
	size = dev_num * sizeof(*dr) + sizeof(*dl);
Linus Torvalds's avatar
Linus Torvalds committed
726 727 728 729 730 731 732 733

	if (verify_area(VERIFY_WRITE, (void *) arg, size))
		return -EFAULT;

	if (!(dl = kmalloc(size, GFP_KERNEL)))
		return -ENOMEM;
	dr = dl->dev_req;

734 735
	read_lock_bh(&hci_dev_list_lock);
	list_for_each(p, &hci_dev_list) {
736 737 738 739 740 741
		struct hci_dev *hdev;
		hdev = list_entry(p, struct hci_dev, list);
		(dr + n)->dev_id  = hdev->id;
		(dr + n)->dev_opt = hdev->flags;
		if (++n >= dev_num)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
742
	}
743
	read_unlock_bh(&hci_dev_list_lock);
Linus Torvalds's avatar
Linus Torvalds committed
744 745

	dl->dev_num = n;
746
	size = n * sizeof(*dr) + sizeof(*dl);
Linus Torvalds's avatar
Linus Torvalds committed
747 748

	copy_to_user((void *) arg, dl, size);
749
	kfree(dl);
Linus Torvalds's avatar
Linus Torvalds committed
750 751 752 753

	return 0;
}

754
int hci_get_dev_info(unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
{
	struct hci_dev *hdev;
	struct hci_dev_info di;
	int err = 0;

	if (copy_from_user(&di, (void *) arg, sizeof(di)))
		return -EFAULT;

	if (!(hdev = hci_dev_get(di.dev_id)))
		return -ENODEV;

	strcpy(di.name, hdev->name);
	di.bdaddr   = hdev->bdaddr;
	di.type     = hdev->type;
	di.flags    = hdev->flags;
	di.pkt_type = hdev->pkt_type;
	di.acl_mtu  = hdev->acl_mtu;
772
	di.acl_pkts = hdev->acl_pkts;
Linus Torvalds's avatar
Linus Torvalds committed
773
	di.sco_mtu  = hdev->sco_mtu;
774 775 776
	di.sco_pkts = hdev->sco_pkts;
	di.link_policy = hdev->link_policy;
	di.link_mode   = hdev->link_mode;
Linus Torvalds's avatar
Linus Torvalds committed
777 778 779 780 781 782 783 784 785 786 787 788

	memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
	memcpy(&di.features, &hdev->features, sizeof(di.features));

	if (copy_to_user((void *) arg, &di, sizeof(di)))
		err = -EFAULT;

	hci_dev_put(hdev);

	return err;
}

789
/* ---- Interface to HCI drivers ---- */
Linus Torvalds's avatar
Linus Torvalds committed
790

791 792
/* Register HCI device */
int hci_register_dev(struct hci_dev *hdev)
Linus Torvalds's avatar
Linus Torvalds committed
793
{
794
	struct list_head *head = &hci_dev_list, *p;
795
	int id = 0;
Linus Torvalds's avatar
Linus Torvalds committed
796

797
	BT_DBG("%p name %s type %d owner %p", hdev, hdev->name, hdev->type, hdev->owner);
Linus Torvalds's avatar
Linus Torvalds committed
798

799 800
	if (!hdev->open || !hdev->close || !hdev->destruct)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
801

802
	write_lock_bh(&hci_dev_list_lock);
Linus Torvalds's avatar
Linus Torvalds committed
803

804
	/* Find first available device id */
805
	list_for_each(p, &hci_dev_list) {
806 807 808 809
	       	if (list_entry(p, struct hci_dev, list)->id != id)
			break;
		head = p; id++;
	}
Linus Torvalds's avatar
Linus Torvalds committed
810
	
811 812 813
	sprintf(hdev->name, "hci%d", id);
	hdev->id = id;
	list_add(&hdev->list, head);
Linus Torvalds's avatar
Linus Torvalds committed
814

815 816 817 818 819 820
	atomic_set(&hdev->refcnt, 1);
	spin_lock_init(&hdev->lock);
			
	hdev->flags = 0;
	hdev->pkt_type  = (HCI_DM1 | HCI_DH1 | HCI_HV1);
	hdev->link_mode = (HCI_LM_ACCEPT);
Linus Torvalds's avatar
Linus Torvalds committed
821

822 823 824
	tasklet_init(&hdev->cmd_task, hci_cmd_task,(unsigned long) hdev);
	tasklet_init(&hdev->rx_task, hci_rx_task, (unsigned long) hdev);
	tasklet_init(&hdev->tx_task, hci_tx_task, (unsigned long) hdev);
Linus Torvalds's avatar
Linus Torvalds committed
825

826 827 828
	skb_queue_head_init(&hdev->rx_q);
	skb_queue_head_init(&hdev->cmd_q);
	skb_queue_head_init(&hdev->raw_q);
Linus Torvalds's avatar
Linus Torvalds committed
829

830 831
	init_waitqueue_head(&hdev->req_wait_q);
	init_MUTEX(&hdev->req_lock);
Linus Torvalds's avatar
Linus Torvalds committed
832

833
	inquiry_cache_init(hdev);
Linus Torvalds's avatar
Linus Torvalds committed
834

835
	hci_conn_hash_init(hdev);
836 837 838 839 840

	memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));

	atomic_set(&hdev->promisc, 0);
		
841 842 843
	write_unlock_bh(&hci_dev_list_lock);

	hci_dev_proc_init(hdev);
844 845 846

	hci_notify(hdev, HCI_DEV_REG);
	hci_run_hotplug(hdev->name, "register");
847 848

	return id;
Linus Torvalds's avatar
Linus Torvalds committed
849 850
}

851 852
/* Unregister HCI device */
int hci_unregister_dev(struct hci_dev *hdev)
Linus Torvalds's avatar
Linus Torvalds committed
853
{
854
	BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
Linus Torvalds's avatar
Linus Torvalds committed
855

856 857 858
	hci_dev_proc_cleanup(hdev);

	write_lock_bh(&hci_dev_list_lock);
859
	list_del(&hdev->list);
860
	write_unlock_bh(&hci_dev_list_lock);
Linus Torvalds's avatar
Linus Torvalds committed
861

862
	hci_dev_do_close(hdev);
Linus Torvalds's avatar
Linus Torvalds committed
863

864
	hci_notify(hdev, HCI_DEV_UNREG);
865 866
	hci_run_hotplug(hdev->name, "unregister");
	
867
	__hci_dev_put(hdev);
868 869
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
870

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
/* Suspend HCI device */
int hci_suspend_dev(struct hci_dev *hdev)
{
	hci_notify(hdev, HCI_DEV_SUSPEND);
	hci_run_hotplug(hdev->name, "suspend");
	return 0;
}

/* Resume HCI device */
int hci_resume_dev(struct hci_dev *hdev)
{
	hci_notify(hdev, HCI_DEV_RESUME);
	hci_run_hotplug(hdev->name, "resume");
	return 0;
}       

887
/* ---- Interface to upper protocols ---- */
Linus Torvalds's avatar
Linus Torvalds committed
888 889

/* Register/Unregister protocols.
890 891
 * hci_task_lock is used to ensure that no tasks are running. */
int hci_register_proto(struct hci_proto *hp)
Linus Torvalds's avatar
Linus Torvalds committed
892 893 894
{
	int err = 0;

895
	BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
Linus Torvalds's avatar
Linus Torvalds committed
896

897
	if (hp->id >= HCI_MAX_PROTO)
Linus Torvalds's avatar
Linus Torvalds committed
898 899 900 901
		return -EINVAL;

	write_lock_bh(&hci_task_lock);

902 903
	if (!hci_proto[hp->id])
		hci_proto[hp->id] = hp;
Linus Torvalds's avatar
Linus Torvalds committed
904
	else
905
		err = -EEXIST;
Linus Torvalds's avatar
Linus Torvalds committed
906 907 908 909 910 911

	write_unlock_bh(&hci_task_lock);

	return err;
}

912
int hci_unregister_proto(struct hci_proto *hp)
Linus Torvalds's avatar
Linus Torvalds committed
913 914 915
{
	int err = 0;

916
	BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
Linus Torvalds's avatar
Linus Torvalds committed
917

918
	if (hp->id >= HCI_MAX_PROTO)
Linus Torvalds's avatar
Linus Torvalds committed
919 920 921 922
		return -EINVAL;

	write_lock_bh(&hci_task_lock);

923 924
	if (hci_proto[hp->id])
		hci_proto[hp->id] = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
925 926 927 928 929 930 931 932 933 934 935 936 937
	else
		err = -ENOENT;

	write_unlock_bh(&hci_task_lock);

	return err;
}

static int hci_send_frame(struct sk_buff *skb)
{
	struct hci_dev *hdev = (struct hci_dev *) skb->dev;

	if (!hdev) {
Linus Torvalds's avatar
Linus Torvalds committed
938
		kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
939 940 941
		return -ENODEV;
	}

942 943 944 945 946
	BT_DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);

	if (atomic_read(&hdev->promisc)) {
		/* Time stamp */
	        do_gettimeofday(&skb->stamp);
Linus Torvalds's avatar
Linus Torvalds committed
947 948

		hci_send_to_sock(hdev, skb);
949
	}
Linus Torvalds's avatar
Linus Torvalds committed
950 951 952 953 954 955 956 957 958 959 960

	/* Get rid of skb owner, prior to sending to the driver. */
	skb_orphan(skb);

	return hdev->send(skb);
}

/* Send HCI command */
int hci_send_cmd(struct hci_dev *hdev, __u16 ogf, __u16 ocf, __u32 plen, void *param)
{
	int len = HCI_COMMAND_HDR_SIZE + plen;
961
	struct hci_command_hdr *hdr;
Linus Torvalds's avatar
Linus Torvalds committed
962 963
	struct sk_buff *skb;

964
	BT_DBG("%s ogf 0x%x ocf 0x%x plen %d", hdev->name, ogf, ocf, plen);
Linus Torvalds's avatar
Linus Torvalds committed
965

966 967
	skb = bt_skb_alloc(len, GFP_ATOMIC);
	if (!skb) {
968
		BT_ERR("%s Can't allocate memory for HCI command", hdev->name);
Linus Torvalds's avatar
Linus Torvalds committed
969 970
		return -ENOMEM;
	}
971

972
	hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
973
	hdr->opcode = __cpu_to_le16(hci_opcode_pack(ogf, ocf));
974
	hdr->plen   = plen;
Linus Torvalds's avatar
Linus Torvalds committed
975 976 977 978

	if (plen)
		memcpy(skb_put(skb, plen), param, plen);

979
	BT_DBG("skb len %d", skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
980 981 982 983 984 985 986 987 988

	skb->pkt_type = HCI_COMMAND_PKT;
	skb->dev = (void *) hdev;
	skb_queue_tail(&hdev->cmd_q, skb);
	hci_sched_cmd(hdev);

	return 0;
}

989 990 991
/* Get data from the previously sent command */
void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 ogf, __u16 ocf)
{
992
	struct hci_command_hdr *hdr;
993 994 995 996

	if (!hdev->sent_cmd)
		return NULL;

997
	hdr = (void *) hdev->sent_cmd->data;
998

999
	if (hdr->opcode != __cpu_to_le16(hci_opcode_pack(ogf, ocf)))
1000 1001 1002 1003 1004 1005 1006
		return NULL;

	BT_DBG("%s ogf 0x%x ocf 0x%x", hdev->name, ogf, ocf);

	return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
}

Linus Torvalds's avatar
Linus Torvalds committed
1007 1008 1009
/* Send ACL data */
static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
{
1010
	struct hci_acl_hdr *hdr;
1011
	int len = skb->len;
Linus Torvalds's avatar
Linus Torvalds committed
1012

1013
	hdr = (struct hci_acl_hdr *) skb_push(skb, HCI_ACL_HDR_SIZE);
1014
	hdr->handle = __cpu_to_le16(hci_handle_pack(handle, flags));
1015
	hdr->dlen   = __cpu_to_le16(len);
Linus Torvalds's avatar
Linus Torvalds committed
1016

1017
	skb->h.raw = (void *) hdr;
Linus Torvalds's avatar
Linus Torvalds committed
1018 1019 1020 1021 1022
}

int hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags)
{
	struct hci_dev *hdev = conn->hdev;
Linus Torvalds's avatar
Linus Torvalds committed
1023
	struct sk_buff *list;
Linus Torvalds's avatar
Linus Torvalds committed
1024

1025
	BT_DBG("%s conn %p flags 0x%x", hdev->name, conn, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1026

Linus Torvalds's avatar
Linus Torvalds committed
1027 1028 1029
	skb->dev = (void *) hdev;
	skb->pkt_type = HCI_ACLDATA_PKT;
	hci_add_acl_hdr(skb, conn->handle, flags | ACL_START);
Linus Torvalds's avatar
Linus Torvalds committed
1030

Linus Torvalds's avatar
Linus Torvalds committed
1031 1032
	if (!(list = skb_shinfo(skb)->frag_list)) {
		/* Non fragmented */
1033
		BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
1034 1035 1036 1037
		
		skb_queue_tail(&conn->data_q, skb);
	} else {
		/* Fragmented */
1038
		BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
1039

Linus Torvalds's avatar
Linus Torvalds committed
1040
		skb_shinfo(skb)->frag_list = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1041

Linus Torvalds's avatar
Linus Torvalds committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
		/* Queue all fragments atomically */
		spin_lock_bh(&conn->data_q.lock);

		__skb_queue_tail(&conn->data_q, skb);
		do {
			skb = list; list = list->next;
			
			skb->dev = (void *) hdev;
			skb->pkt_type = HCI_ACLDATA_PKT;
			hci_add_acl_hdr(skb, conn->handle, flags | ACL_CONT);
		
1053
			BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
1054

Linus Torvalds's avatar
Linus Torvalds committed
1055 1056 1057 1058 1059 1060 1061 1062
			__skb_queue_tail(&conn->data_q, skb);
		} while (list);

		spin_unlock_bh(&conn->data_q.lock);
	}
		
	hci_sched_tx(hdev);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1063 1064 1065 1066 1067 1068
}

/* Send SCO data */
int hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
{
	struct hci_dev *hdev = conn->hdev;
1069
	struct hci_sco_hdr hdr;
Linus Torvalds's avatar
Linus Torvalds committed
1070

1071
	BT_DBG("%s len %d", hdev->name, skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
1072 1073

	if (skb->len > hdev->sco_mtu) {
Linus Torvalds's avatar
Linus Torvalds committed
1074
		kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1075 1076 1077
		return -EINVAL;
	}

1078 1079
	hdr.handle = __cpu_to_le16(conn->handle);
	hdr.dlen   = skb->len;
Linus Torvalds's avatar
Linus Torvalds committed
1080 1081

	skb->h.raw = skb_push(skb, HCI_SCO_HDR_SIZE);
1082
	memcpy(skb->h.raw, &hdr, HCI_SCO_HDR_SIZE);
Linus Torvalds's avatar
Linus Torvalds committed
1083 1084 1085

	skb->dev = (void *) hdev;
	skb->pkt_type = HCI_SCODATA_PKT;
Linus Torvalds's avatar
Linus Torvalds committed
1086
	skb_queue_tail(&conn->data_q, skb);
Linus Torvalds's avatar
Linus Torvalds committed
1087 1088 1089 1090
	hci_sched_tx(hdev);
	return 0;
}

1091
/* ---- HCI TX task (outgoing data) ---- */
Linus Torvalds's avatar
Linus Torvalds committed
1092

1093 1094
/* HCI Connection scheduler */
static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote)
Linus Torvalds's avatar
Linus Torvalds committed
1095
{
1096
	struct hci_conn_hash *h = &hdev->conn_hash;
1097
	struct hci_conn  *conn = NULL;
1098
	int num = 0, min = ~0;
1099
        struct list_head *p;
Linus Torvalds's avatar
Linus Torvalds committed
1100

1101 1102 1103 1104 1105
	/* We don't have to lock device here. Connections are always 
	 * added and removed with TX task disabled. */
	list_for_each(p, &h->list) {
		struct hci_conn *c;
		c = list_entry(p, struct hci_conn, list);
Linus Torvalds's avatar
Linus Torvalds committed
1106

1107 1108 1109 1110
		if (c->type != type || c->state != BT_CONNECTED
				|| skb_queue_empty(&c->data_q))
			continue;
		num++;
Linus Torvalds's avatar
Linus Torvalds committed
1111

1112
		if (c->sent < min) {
1113 1114
			min  = c->sent;
			conn = c;
Linus Torvalds's avatar
Linus Torvalds committed
1115
		}
1116
	}
Linus Torvalds's avatar
Linus Torvalds committed
1117

1118
	if (conn) {
1119 1120
		int cnt = (type == ACL_LINK ? hdev->acl_cnt : hdev->sco_cnt);
		int q = cnt / num;
1121 1122 1123
		*quote = q ? q : 1;
	} else
		*quote = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1124

1125 1126
	BT_DBG("conn %p quote %d", conn, *quote);
	return conn;
Linus Torvalds's avatar
Linus Torvalds committed
1127 1128
}

1129 1130
static inline void hci_acl_tx_to(struct hci_dev *hdev)
{
1131
	struct hci_conn_hash *h = &hdev->conn_hash;
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
        struct list_head *p;
	struct hci_conn  *c;

	BT_ERR("%s ACL tx timeout", hdev->name);

	/* Kill stalled connections */
	list_for_each(p, &h->list) {
		c = list_entry(p, struct hci_conn, list);
		if (c->type == ACL_LINK && c->sent) {
			BT_ERR("%s killing stalled ACL connection %s",
				hdev->name, batostr(&c->dst));
			hci_acl_disconn(c, 0x13);
		}
	}
}

1148
static inline void hci_sched_acl(struct hci_dev *hdev)
Linus Torvalds's avatar
Linus Torvalds committed
1149
{
1150 1151 1152
	struct hci_conn *conn;
	struct sk_buff *skb;
	int quote;
Linus Torvalds's avatar
Linus Torvalds committed
1153

1154
	BT_DBG("%s", hdev->name);
Linus Torvalds's avatar
Linus Torvalds committed
1155

1156 1157 1158 1159 1160
	/* ACL tx timeout must be longer than maximum
	 * link supervision timeout (40.9 seconds) */
	if (!hdev->acl_cnt && (jiffies - hdev->acl_last_tx) > (HZ * 45))
		hci_acl_tx_to(hdev);

1161
	while (hdev->acl_cnt && (conn = hci_low_sent(hdev, ACL_LINK, &quote))) {
1162
		while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
1163 1164 1165
			BT_DBG("skb %p len %d", skb, skb->len);
			hci_send_frame(skb);
			hdev->acl_last_tx = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
1166

1167
			hdev->acl_cnt--;
1168
			conn->sent++;
Linus Torvalds's avatar
Linus Torvalds committed
1169
		}
Linus Torvalds's avatar
Linus Torvalds committed
1170 1171 1172
	}
}

1173 1174
/* Schedule SCO */
static inline void hci_sched_sco(struct hci_dev *hdev)
Linus Torvalds's avatar
Linus Torvalds committed
1175
{
1176 1177 1178
	struct hci_conn *conn;
	struct sk_buff *skb;
	int quote;
Linus Torvalds's avatar
Linus Torvalds committed
1179

1180
	BT_DBG("%s", hdev->name);
Linus Torvalds's avatar
Linus Torvalds committed
1181

1182 1183
	while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
		while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
1184 1185
			BT_DBG("skb %p len %d", skb, skb->len);
			hci_send_frame(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1186

1187 1188 1189
			conn->sent++;
			if (conn->sent == ~0)
				conn->sent = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1190
		}
Linus Torvalds's avatar
Linus Torvalds committed
1191 1192 1193
	}
}

1194
static void hci_tx_task(unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
1195
{
1196 1197
	struct hci_dev *hdev = (struct hci_dev *) arg;
	struct sk_buff *skb;
Linus Torvalds's avatar
Linus Torvalds committed
1198

1199
	read_lock(&hci_task_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1200

1201
	BT_DBG("%s acl %d sco %d", hdev->name, hdev->acl_cnt, hdev->sco_cnt);
Linus Torvalds's avatar
Linus Torvalds committed
1202

1203
	/* Schedule queues and send stuff to HCI driver */
Linus Torvalds's avatar
Linus Torvalds committed
1204

1205
	hci_sched_acl(hdev);
Linus Torvalds's avatar
Linus Torvalds committed
1206

1207
	hci_sched_sco(hdev);
Linus Torvalds's avatar
Linus Torvalds committed
1208

1209 1210 1211
	/* Send next queued raw (unknown type) packet */
	while ((skb = skb_dequeue(&hdev->raw_q)))
		hci_send_frame(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1212

1213
	read_unlock(&hci_task_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1214 1215
}

1216
/* ----- HCI RX task (incoming data proccessing) ----- */
Linus Torvalds's avatar
Linus Torvalds committed
1217 1218

/* ACL data packet */
Linus Torvalds's avatar
Linus Torvalds committed
1219
static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
1220
{
1221
	struct hci_acl_hdr *hdr = (void *) skb->data;
Linus Torvalds's avatar
Linus Torvalds committed
1222 1223 1224 1225 1226
	struct hci_conn *conn;
	__u16 handle, flags;

	skb_pull(skb, HCI_ACL_HDR_SIZE);

1227
	handle = __le16_to_cpu(hdr->handle);
1228 1229
	flags  = hci_flags(handle);
	handle = hci_handle(handle);
Linus Torvalds's avatar
Linus Torvalds committed
1230

1231 1232 1233
	BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags);

	hdev->stat.acl_rx++;
Linus Torvalds's avatar
Linus Torvalds committed
1234

1235
	hci_dev_lock(hdev);
1236
	conn = hci_conn_hash_lookup_handle(hdev, handle);
1237 1238 1239
	hci_dev_unlock(hdev);
	
	if (conn) {
Linus Torvalds's avatar
Linus Torvalds committed
1240
		register struct hci_proto *hp;
Linus Torvalds's avatar
Linus Torvalds committed
1241 1242

		/* Send to upper protocol */
1243
		if ((hp = hci_proto[HCI_PROTO_L2CAP]) && hp->recv_acldata) {
Linus Torvalds's avatar
Linus Torvalds committed
1244
			hp->recv_acldata(conn, skb, flags);
1245
			return;
Linus Torvalds's avatar
Linus Torvalds committed
1246 1247
		}
	} else {
1248 1249
		BT_ERR("%s ACL packet for unknown connection handle %d", 
			hdev->name, handle);
Linus Torvalds's avatar
Linus Torvalds committed
1250 1251
	}

Linus Torvalds's avatar
Linus Torvalds committed
1252
	kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1253 1254 1255
}

/* SCO data packet */
Linus Torvalds's avatar
Linus Torvalds committed
1256
static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
1257
{
1258
	struct hci_sco_hdr *hdr = (void *) skb->data;
1259 1260 1261 1262 1263
	struct hci_conn *conn;
	__u16 handle;

	skb_pull(skb, HCI_SCO_HDR_SIZE);

1264
	handle = __le16_to_cpu(hdr->handle);
1265 1266

	BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle);
Linus Torvalds's avatar
Linus Torvalds committed
1267 1268

	hdev->stat.sco_rx++;
1269 1270

	hci_dev_lock(hdev);
1271
	conn = hci_conn_hash_lookup_handle(hdev, handle);
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
	hci_dev_unlock(hdev);
	
	if (conn) {
		register struct hci_proto *hp;

		/* Send to upper protocol */
		if ((hp = hci_proto[HCI_PROTO_SCO]) && hp->recv_scodata) {
			hp->recv_scodata(conn, skb);
			return;
		}
	} else {
		BT_ERR("%s SCO packet for unknown connection handle %d", 
			hdev->name, handle);
	}

	kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1288 1289 1290 1291 1292 1293 1294
}

void hci_rx_task(unsigned long arg)
{
	struct hci_dev *hdev = (struct hci_dev *) arg;
	struct sk_buff *skb;

1295
	BT_DBG("%s", hdev->name);
Linus Torvalds's avatar
Linus Torvalds committed
1296 1297 1298 1299

	read_lock(&hci_task_lock);

	while ((skb = skb_dequeue(&hdev->rx_q))) {
1300
		if (atomic_read(&hdev->promisc)) {
Linus Torvalds's avatar
Linus Torvalds committed
1301 1302 1303 1304
			/* Send copy to the sockets */
			hci_send_to_sock(hdev, skb);
		}

1305 1306 1307 1308 1309 1310
		if (test_bit(HCI_RAW, &hdev->flags)) {
			kfree_skb(skb);
			continue;
		}

		if (test_bit(HCI_INIT, &hdev->flags)) {
Linus Torvalds's avatar
Linus Torvalds committed
1311 1312 1313 1314
			/* Don't process data packets in this states. */
			switch (skb->pkt_type) {
			case HCI_ACLDATA_PKT:
			case HCI_SCODATA_PKT:
Linus Torvalds's avatar
Linus Torvalds committed
1315
				kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1316 1317 1318 1319
				continue;
			};
		}

1320 1321 1322 1323 1324
		/* Process frame */
		switch (skb->pkt_type) {
		case HCI_EVENT_PKT:
			hci_event_packet(hdev, skb);
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1325

1326 1327 1328 1329
		case HCI_ACLDATA_PKT:
			BT_DBG("%s ACL data packet", hdev->name);
			hci_acldata_packet(hdev, skb);
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1330

1331 1332 1333 1334
		case HCI_SCODATA_PKT:
			BT_DBG("%s SCO data packet", hdev->name);
			hci_scodata_packet(hdev, skb);
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1335

1336
		default:
Linus Torvalds's avatar
Linus Torvalds committed
1337
			kfree_skb(skb);
1338
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
		}
	}

	read_unlock(&hci_task_lock);
}

static void hci_cmd_task(unsigned long arg)
{
	struct hci_dev *hdev = (struct hci_dev *) arg;
	struct sk_buff *skb;

1350
	BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
Linus Torvalds's avatar
Linus Torvalds committed
1351

1352 1353 1354 1355 1356
	if (!atomic_read(&hdev->cmd_cnt) && (jiffies - hdev->cmd_last_tx) > HZ) {
		BT_ERR("%s command tx timeout", hdev->name);
		atomic_set(&hdev->cmd_cnt, 1);
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
1357 1358
	/* Send queued commands */
	if (atomic_read(&hdev->cmd_cnt) && (skb = skb_dequeue(&hdev->cmd_q))) {
Linus Torvalds's avatar
Linus Torvalds committed
1359 1360
		if (hdev->sent_cmd)
			kfree_skb(hdev->sent_cmd);
Linus Torvalds's avatar
Linus Torvalds committed
1361

Linus Torvalds's avatar
Linus Torvalds committed
1362
		if ((hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC))) {
Linus Torvalds's avatar
Linus Torvalds committed
1363 1364
			atomic_dec(&hdev->cmd_cnt);
			hci_send_frame(skb);
1365
			hdev->cmd_last_tx = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
1366 1367 1368 1369 1370 1371
		} else {
			skb_queue_head(&hdev->cmd_q, skb);
			hci_sched_cmd(hdev);
		}
	}
}