core.c 29.4 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12
   HIDP implementation for Linux Bluetooth stack (BlueZ).
   Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>

   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
13 14 15
   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
L
Linus Torvalds 已提交
16 17
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

18 19
   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
L
Linus Torvalds 已提交
20 21 22 23 24
   SOFTWARE IS DISCLAIMED.
*/

#include <linux/module.h>
#include <linux/file.h>
S
Szymon Janc 已提交
25
#include <linux/kthread.h>
26
#include <linux/hidraw.h>
L
Linus Torvalds 已提交
27 28

#include <net/bluetooth/bluetooth.h>
29
#include <net/bluetooth/hci_core.h>
L
Linus Torvalds 已提交
30 31 32 33
#include <net/bluetooth/l2cap.h>

#include "hidp.h"

34
#define VERSION "1.2"
L
Linus Torvalds 已提交
35 36 37 38 39

static DECLARE_RWSEM(hidp_session_sem);
static LIST_HEAD(hidp_session_list);

static unsigned char hidp_keycode[256] = {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	  0,   0,   0,   0,  30,  48,  46,  32,  18,  33,  34,  35,  23,  36,
	 37,  38,  50,  49,  24,  25,  16,  19,  31,  20,  22,  47,  17,  45,
	 21,  44,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  28,   1,
	 14,  15,  57,  12,  13,  26,  27,  43,  43,  39,  40,  41,  51,  52,
	 53,  58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  87,  88,
	 99,  70, 119, 110, 102, 104, 111, 107, 109, 106, 105, 108, 103,  69,
	 98,  55,  74,  78,  96,  79,  80,  81,  75,  76,  77,  71,  72,  73,
	 82,  83,  86, 127, 116, 117, 183, 184, 185, 186, 187, 188, 189, 190,
	191, 192, 193, 194, 134, 138, 130, 132, 128, 129, 131, 137, 133, 135,
	136, 113, 115, 114,   0,   0,   0, 121,   0,  89,  93, 124,  92,  94,
	 95,   0,   0,   0, 122, 123,  90,  91,  85,   0,   0,   0,   0,   0,
	  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
	  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
	  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
	  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
	  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
	 29,  42,  56, 125,  97,  54, 100, 126, 164, 166, 165, 163, 161, 115,
	114, 113, 150, 158, 159, 128, 136, 177, 178, 176, 142, 152, 173, 140
L
Linus Torvalds 已提交
58 59 60 61
};

static unsigned char hidp_mkeyspat[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };

62 63 64 65 66 67 68 69 70
static inline void hidp_schedule(struct hidp_session *session)
{
	struct sock *ctrl_sk = session->ctrl_sock->sk;
	struct sock *intr_sk = session->intr_sock->sk;

	wake_up_interruptible(sk_sleep(ctrl_sk));
	wake_up_interruptible(sk_sleep(intr_sk));
}

L
Linus Torvalds 已提交
71 72 73 74 75 76
static struct hidp_session *__hidp_get_session(bdaddr_t *bdaddr)
{
	struct hidp_session *session;

	BT_DBG("");

77
	list_for_each_entry(session, &hidp_session_list, list) {
L
Linus Torvalds 已提交
78 79 80
		if (!bacmp(bdaddr, &session->bdaddr))
			return session;
	}
81

L
Linus Torvalds 已提交
82 83 84 85 86
	return NULL;
}

static void __hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
{
87
	memset(ci, 0, sizeof(*ci));
L
Linus Torvalds 已提交
88 89 90
	bacpy(&ci->bdaddr, &session->bdaddr);

	ci->flags = session->flags;
91
	ci->state = BT_CONNECTED;
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105

	ci->vendor  = 0x0000;
	ci->product = 0x0000;
	ci->version = 0x0000;

	if (session->input) {
		ci->vendor  = session->input->id.vendor;
		ci->product = session->input->id.product;
		ci->version = session->input->id.version;
		if (session->input->name)
			strncpy(ci->name, session->input->name, 128);
		else
			strncpy(ci->name, "HID Boot Device", 128);
	}
106 107 108 109 110 111 112

	if (session->hid) {
		ci->vendor  = session->hid->vendor;
		ci->product = session->hid->product;
		ci->version = session->hid->version;
		strncpy(ci->name, session->hid->name, 128);
	}
L
Linus Torvalds 已提交
113 114
}

A
Andrew Morton 已提交
115 116
static int hidp_queue_event(struct hidp_session *session, struct input_dev *dev,
				unsigned int type, unsigned int code, int value)
L
Linus Torvalds 已提交
117 118
{
	unsigned char newleds;
119
	struct sk_buff *skb;
L
Linus Torvalds 已提交
120

121
	BT_DBG("session %p type %d code %d value %d", session, type, code, value);
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

	if (type != EV_LED)
		return -1;

	newleds = (!!test_bit(LED_KANA,    dev->led) << 3) |
		  (!!test_bit(LED_COMPOSE, dev->led) << 3) |
		  (!!test_bit(LED_SCROLLL, dev->led) << 2) |
		  (!!test_bit(LED_CAPSL,   dev->led) << 1) |
		  (!!test_bit(LED_NUML,    dev->led));

	if (session->leds == newleds)
		return 0;

	session->leds = newleds;

137 138
	skb = alloc_skb(3, GFP_ATOMIC);
	if (!skb) {
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		BT_ERR("Can't allocate memory for new frame");
		return -ENOMEM;
	}

	*skb_put(skb, 1) = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
	*skb_put(skb, 1) = 0x01;
	*skb_put(skb, 1) = newleds;

	skb_queue_tail(&session->intr_transmit, skb);

	hidp_schedule(session);

	return 0;
}

154 155
static int hidp_hidinput_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
{
156
	struct hid_device *hid = input_get_drvdata(dev);
157 158 159 160 161 162 163
	struct hidp_session *session = hid->driver_data;

	return hidp_queue_event(session, dev, type, code, value);
}

static int hidp_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
{
164
	struct hidp_session *session = input_get_drvdata(dev);
165 166 167 168

	return hidp_queue_event(session, dev, type, code, value);
}

L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
{
	struct input_dev *dev = session->input;
	unsigned char *keys = session->keys;
	unsigned char *udata = skb->data + 1;
	signed char *sdata = skb->data + 1;
	int i, size = skb->len - 1;

	switch (skb->data[0]) {
	case 0x01:	/* Keyboard report */
		for (i = 0; i < 8; i++)
			input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1);

		/* If all the key codes have been set to 0x01, it means
		 * too many keys were pressed at the same time. */
		if (!memcmp(udata + 2, hidp_mkeyspat, 6))
			break;

		for (i = 2; i < 8; i++) {
			if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) {
				if (hidp_keycode[keys[i]])
					input_report_key(dev, hidp_keycode[keys[i]], 0);
				else
					BT_ERR("Unknown key (scancode %#x) released.", keys[i]);
			}

			if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) {
				if (hidp_keycode[udata[i]])
					input_report_key(dev, hidp_keycode[udata[i]], 1);
				else
					BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
			}
		}

		memcpy(keys, udata, 8);
		break;

	case 0x02:	/* Mouse report */
		input_report_key(dev, BTN_LEFT,   sdata[0] & 0x01);
		input_report_key(dev, BTN_RIGHT,  sdata[0] & 0x02);
		input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04);
		input_report_key(dev, BTN_SIDE,   sdata[0] & 0x08);
		input_report_key(dev, BTN_EXTRA,  sdata[0] & 0x10);

		input_report_rel(dev, REL_X, sdata[1]);
		input_report_rel(dev, REL_Y, sdata[2]);

		if (size > 3)
			input_report_rel(dev, REL_WHEEL, sdata[3]);
		break;
	}

	input_sync(dev);
}

224
static int __hidp_send_ctrl_message(struct hidp_session *session,
225 226
				    unsigned char hdr, unsigned char *data,
				    int size)
227 228 229 230 231
{
	struct sk_buff *skb;

	BT_DBG("session %p data %p size %d", session, data, size);

232 233 234
	if (atomic_read(&session->terminate))
		return -EIO;

235 236
	skb = alloc_skb(size + 1, GFP_ATOMIC);
	if (!skb) {
237 238 239 240 241 242 243 244 245 246 247 248 249
		BT_ERR("Can't allocate memory for new frame");
		return -ENOMEM;
	}

	*skb_put(skb, 1) = hdr;
	if (data && size > 0)
		memcpy(skb_put(skb, size), data, size);

	skb_queue_tail(&session->ctrl_transmit, skb);

	return 0;
}

250
static int hidp_send_ctrl_message(struct hidp_session *session,
251 252 253 254 255 256 257 258 259 260 261
			unsigned char hdr, unsigned char *data, int size)
{
	int err;

	err = __hidp_send_ctrl_message(session, hdr, data, size);

	hidp_schedule(session);

	return err;
}

A
Andrew Morton 已提交
262 263
static int hidp_queue_report(struct hidp_session *session,
				unsigned char *data, int size)
264 265 266
{
	struct sk_buff *skb;

267
	BT_DBG("session %p hid %p data %p size %d", session, session->hid, data, size);
268

269 270
	skb = alloc_skb(size + 1, GFP_ATOMIC);
	if (!skb) {
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		BT_ERR("Can't allocate memory for new frame");
		return -ENOMEM;
	}

	*skb_put(skb, 1) = 0xa2;
	if (size > 0)
		memcpy(skb_put(skb, size), data, size);

	skb_queue_tail(&session->intr_transmit, skb);

	hidp_schedule(session);

	return 0;
}

static int hidp_send_report(struct hidp_session *session, struct hid_report *report)
{
	unsigned char buf[32];
	int rsize;

	rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0);
	if (rsize > sizeof(buf))
		return -EIO;

	hid_output_report(report, buf);

	return hidp_queue_report(session, buf, rsize);
}

300 301 302 303 304 305 306 307 308
static int hidp_get_raw_report(struct hid_device *hid,
		unsigned char report_number,
		unsigned char *data, size_t count,
		unsigned char report_type)
{
	struct hidp_session *session = hid->driver_data;
	struct sk_buff *skb;
	size_t len;
	int numbered_reports = hid->report_enum[report_type].numbered;
309
	int ret;
310

311 312 313
	if (atomic_read(&session->terminate))
		return -EIO;

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	switch (report_type) {
	case HID_FEATURE_REPORT:
		report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE;
		break;
	case HID_INPUT_REPORT:
		report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT;
		break;
	case HID_OUTPUT_REPORT:
		report_type = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUPUT;
		break;
	default:
		return -EINVAL;
	}

	if (mutex_lock_interruptible(&session->report_mutex))
		return -ERESTARTSYS;

	/* Set up our wait, and send the report request to the device. */
	session->waiting_report_type = report_type & HIDP_DATA_RTYPE_MASK;
	session->waiting_report_number = numbered_reports ? report_number : -1;
	set_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
	data[0] = report_number;
336 337 338
	ret = hidp_send_ctrl_message(hid->driver_data, report_type, data, 1);
	if (ret)
		goto err;
339 340 341

	/* Wait for the return of the report. The returned report
	   gets put in session->report_return.  */
342 343
	while (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
	       !atomic_read(&session->terminate)) {
344 345 346
		int res;

		res = wait_event_interruptible_timeout(session->report_queue,
347 348
			!test_bit(HIDP_WAITING_FOR_RETURN, &session->flags)
				|| atomic_read(&session->terminate),
349 350 351
			5*HZ);
		if (res == 0) {
			/* timeout */
352 353
			ret = -EIO;
			goto err;
354 355 356
		}
		if (res < 0) {
			/* signal */
357 358
			ret = -ERESTARTSYS;
			goto err;
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
		}
	}

	skb = session->report_return;
	if (skb) {
		len = skb->len < count ? skb->len : count;
		memcpy(data, skb->data, len);

		kfree_skb(skb);
		session->report_return = NULL;
	} else {
		/* Device returned a HANDSHAKE, indicating  protocol error. */
		len = -EIO;
	}

	clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
	mutex_unlock(&session->report_mutex);

	return len;

379
err:
380 381
	clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
	mutex_unlock(&session->report_mutex);
382
	return ret;
383 384
}

385 386
static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count,
		unsigned char report_type)
387
{
A
Alan Ott 已提交
388 389 390
	struct hidp_session *session = hid->driver_data;
	int ret;

391 392 393 394 395
	switch (report_type) {
	case HID_FEATURE_REPORT:
		report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE;
		break;
	case HID_OUTPUT_REPORT:
396
		report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_OUPUT;
397 398 399 400 401
		break;
	default:
		return -EINVAL;
	}

A
Alan Ott 已提交
402 403 404 405 406
	if (mutex_lock_interruptible(&session->report_mutex))
		return -ERESTARTSYS;

	/* Set up our wait, and send the report request to the device. */
	set_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
407 408 409
	ret = hidp_send_ctrl_message(hid->driver_data, report_type, data,
									count);
	if (ret)
A
Alan Ott 已提交
410 411 412
		goto err;

	/* Wait for the ACK from the device. */
413 414
	while (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags) &&
	       !atomic_read(&session->terminate)) {
A
Alan Ott 已提交
415 416 417
		int res;

		res = wait_event_interruptible_timeout(session->report_queue,
418 419
			!test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags)
				|| atomic_read(&session->terminate),
A
Alan Ott 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
			10*HZ);
		if (res == 0) {
			/* timeout */
			ret = -EIO;
			goto err;
		}
		if (res < 0) {
			/* signal */
			ret = -ERESTARTSYS;
			goto err;
		}
	}

	if (!session->output_report_success) {
		ret = -EIO;
		goto err;
	}

	ret = count;

err:
	clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
	mutex_unlock(&session->report_mutex);
	return ret;
444 445
}

L
Linus Torvalds 已提交
446 447 448 449
static void hidp_idle_timeout(unsigned long arg)
{
	struct hidp_session *session = (struct hidp_session *) arg;

450 451
	atomic_inc(&session->terminate);
	wake_up_process(session->task);
L
Linus Torvalds 已提交
452 453
}

A
Andrew Morton 已提交
454
static void hidp_set_timer(struct hidp_session *session)
L
Linus Torvalds 已提交
455 456 457 458 459
{
	if (session->idle_to > 0)
		mod_timer(&session->timer, jiffies + HZ * session->idle_to);
}

460
static void hidp_del_timer(struct hidp_session *session)
L
Linus Torvalds 已提交
461 462 463 464 465
{
	if (session->idle_to > 0)
		del_timer(&session->timer);
}

A
Andrew Morton 已提交
466 467
static void hidp_process_handshake(struct hidp_session *session,
					unsigned char param)
L
Linus Torvalds 已提交
468 469
{
	BT_DBG("session %p param 0x%02x", session, param);
A
Alan Ott 已提交
470
	session->output_report_success = 0; /* default condition */
L
Linus Torvalds 已提交
471 472 473 474

	switch (param) {
	case HIDP_HSHK_SUCCESSFUL:
		/* FIXME: Call into SET_ GET_ handlers here */
A
Alan Ott 已提交
475
		session->output_report_success = 1;
L
Linus Torvalds 已提交
476 477 478 479 480 481
		break;

	case HIDP_HSHK_NOT_READY:
	case HIDP_HSHK_ERR_INVALID_REPORT_ID:
	case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
	case HIDP_HSHK_ERR_INVALID_PARAMETER:
482
		if (test_and_clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags))
483
			wake_up_interruptible(&session->report_queue);
484

L
Linus Torvalds 已提交
485 486 487 488 489 490 491 492
		/* FIXME: Call into SET_ GET_ handlers here */
		break;

	case HIDP_HSHK_ERR_UNKNOWN:
		break;

	case HIDP_HSHK_ERR_FATAL:
		/* Device requests a reboot, as this is the only way this error
493
		 * can be recovered. */
L
Linus Torvalds 已提交
494 495 496 497 498 499 500 501 502
		__hidp_send_ctrl_message(session,
			HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0);
		break;

	default:
		__hidp_send_ctrl_message(session,
			HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
		break;
	}
A
Alan Ott 已提交
503 504

	/* Wake up the waiting thread. */
505
	if (test_and_clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags))
A
Alan Ott 已提交
506
		wake_up_interruptible(&session->report_queue);
L
Linus Torvalds 已提交
507 508
}

A
Andrew Morton 已提交
509 510
static void hidp_process_hid_control(struct hidp_session *session,
					unsigned char param)
L
Linus Torvalds 已提交
511 512 513
{
	BT_DBG("session %p param 0x%02x", session, param);

514
	if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG) {
L
Linus Torvalds 已提交
515 516 517 518
		/* Flush the transmit queues */
		skb_queue_purge(&session->ctrl_transmit);
		skb_queue_purge(&session->intr_transmit);

519 520
		atomic_inc(&session->terminate);
		wake_up_process(current);
L
Linus Torvalds 已提交
521 522 523
	}
}

524 525
/* Returns true if the passed-in skb should be freed by the caller. */
static int hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
A
Andrew Morton 已提交
526
				unsigned char param)
L
Linus Torvalds 已提交
527
{
528
	int done_with_skb = 1;
L
Linus Torvalds 已提交
529 530 531 532 533 534 535 536
	BT_DBG("session %p skb %p len %d param 0x%02x", session, skb, skb->len, param);

	switch (param) {
	case HIDP_DATA_RTYPE_INPUT:
		hidp_set_timer(session);

		if (session->input)
			hidp_input_report(session, skb);
537 538 539

		if (session->hid)
			hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 0);
L
Linus Torvalds 已提交
540 541 542 543 544 545 546 547 548 549 550
		break;

	case HIDP_DATA_RTYPE_OTHER:
	case HIDP_DATA_RTYPE_OUPUT:
	case HIDP_DATA_RTYPE_FEATURE:
		break;

	default:
		__hidp_send_ctrl_message(session,
			HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
	}
551 552 553 554 555 556 557 558 559 560 561 562 563 564

	if (test_bit(HIDP_WAITING_FOR_RETURN, &session->flags) &&
				param == session->waiting_report_type) {
		if (session->waiting_report_number < 0 ||
		    session->waiting_report_number == skb->data[0]) {
			/* hidp_get_raw_report() is waiting on this report. */
			session->report_return = skb;
			done_with_skb = 0;
			clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
			wake_up_interruptible(&session->report_queue);
		}
	}

	return done_with_skb;
L
Linus Torvalds 已提交
565 566
}

A
Andrew Morton 已提交
567 568
static void hidp_recv_ctrl_frame(struct hidp_session *session,
					struct sk_buff *skb)
L
Linus Torvalds 已提交
569 570
{
	unsigned char hdr, type, param;
571
	int free_skb = 1;
L
Linus Torvalds 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590

	BT_DBG("session %p skb %p len %d", session, skb, skb->len);

	hdr = skb->data[0];
	skb_pull(skb, 1);

	type = hdr & HIDP_HEADER_TRANS_MASK;
	param = hdr & HIDP_HEADER_PARAM_MASK;

	switch (type) {
	case HIDP_TRANS_HANDSHAKE:
		hidp_process_handshake(session, param);
		break;

	case HIDP_TRANS_HID_CONTROL:
		hidp_process_hid_control(session, param);
		break;

	case HIDP_TRANS_DATA:
591
		free_skb = hidp_process_data(session, skb, param);
L
Linus Torvalds 已提交
592 593 594 595 596 597 598 599
		break;

	default:
		__hidp_send_ctrl_message(session,
			HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
		break;
	}

600 601
	if (free_skb)
		kfree_skb(skb);
L
Linus Torvalds 已提交
602 603
}

A
Andrew Morton 已提交
604 605
static void hidp_recv_intr_frame(struct hidp_session *session,
				struct sk_buff *skb)
L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613 614 615
{
	unsigned char hdr;

	BT_DBG("session %p skb %p len %d", session, skb, skb->len);

	hdr = skb->data[0];
	skb_pull(skb, 1);

	if (hdr == (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
		hidp_set_timer(session);
616

L
Linus Torvalds 已提交
617 618
		if (session->input)
			hidp_input_report(session, skb);
619 620 621 622 623

		if (session->hid) {
			hid_input_report(session->hid, HID_INPUT_REPORT, skb->data, skb->len, 1);
			BT_DBG("report len %d", skb->len);
		}
L
Linus Torvalds 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	} else {
		BT_DBG("Unsupported protocol header 0x%02x", hdr);
	}

	kfree_skb(skb);
}

static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
{
	struct kvec iv = { data, len };
	struct msghdr msg;

	BT_DBG("sock %p data %p len %d", sock, data, len);

	if (!len)
		return 0;

	memset(&msg, 0, sizeof(msg));

	return kernel_sendmsg(sock, &msg, &iv, 1, len);
}

646
static void hidp_process_intr_transmit(struct hidp_session *session)
L
Linus Torvalds 已提交
647 648 649 650 651
{
	struct sk_buff *skb;

	BT_DBG("session %p", session);

652 653 654
	while ((skb = skb_dequeue(&session->intr_transmit))) {
		if (hidp_send_frame(session->intr_sock, skb->data, skb->len) < 0) {
			skb_queue_head(&session->intr_transmit, skb);
L
Linus Torvalds 已提交
655 656 657 658 659 660
			break;
		}

		hidp_set_timer(session);
		kfree_skb(skb);
	}
661
}
L
Linus Torvalds 已提交
662

663 664 665 666 667 668 669 670 671
static void hidp_process_ctrl_transmit(struct hidp_session *session)
{
	struct sk_buff *skb;

	BT_DBG("session %p", session);

	while ((skb = skb_dequeue(&session->ctrl_transmit))) {
		if (hidp_send_frame(session->ctrl_sock, skb->data, skb->len) < 0) {
			skb_queue_head(&session->ctrl_transmit, skb);
L
Linus Torvalds 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
			break;
		}

		hidp_set_timer(session);
		kfree_skb(skb);
	}
}

static int hidp_session(void *arg)
{
	struct hidp_session *session = arg;
	struct sock *ctrl_sk = session->ctrl_sock->sk;
	struct sock *intr_sk = session->intr_sock->sk;
	struct sk_buff *skb;
	wait_queue_t ctrl_wait, intr_wait;

	BT_DBG("session %p", session);

690
	__module_get(THIS_MODULE);
L
Linus Torvalds 已提交
691 692 693 694
	set_user_nice(current, -15);

	init_waitqueue_entry(&ctrl_wait, current);
	init_waitqueue_entry(&intr_wait, current);
E
Eric Dumazet 已提交
695 696
	add_wait_queue(sk_sleep(ctrl_sk), &ctrl_wait);
	add_wait_queue(sk_sleep(intr_sk), &intr_wait);
697 698
	session->waiting_for_startup = 0;
	wake_up_interruptible(&session->startup_queue);
699 700
	set_current_state(TASK_INTERRUPTIBLE);
	while (!atomic_read(&session->terminate)) {
701 702
		if (ctrl_sk->sk_state != BT_CONNECTED ||
				intr_sk->sk_state != BT_CONNECTED)
L
Linus Torvalds 已提交
703 704
			break;

705
		while ((skb = skb_dequeue(&intr_sk->sk_receive_queue))) {
L
Linus Torvalds 已提交
706
			skb_orphan(skb);
707
			if (!skb_linearize(skb))
708
				hidp_recv_intr_frame(session, skb);
709 710
			else
				kfree_skb(skb);
L
Linus Torvalds 已提交
711 712
		}

713 714
		hidp_process_intr_transmit(session);

715
		while ((skb = skb_dequeue(&ctrl_sk->sk_receive_queue))) {
L
Linus Torvalds 已提交
716
			skb_orphan(skb);
717
			if (!skb_linearize(skb))
718
				hidp_recv_ctrl_frame(session, skb);
719 720
			else
				kfree_skb(skb);
L
Linus Torvalds 已提交
721 722
		}

723
		hidp_process_ctrl_transmit(session);
L
Linus Torvalds 已提交
724 725

		schedule();
726
		set_current_state(TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
727 728
	}
	set_current_state(TASK_RUNNING);
729
	atomic_inc(&session->terminate);
E
Eric Dumazet 已提交
730 731
	remove_wait_queue(sk_sleep(intr_sk), &intr_wait);
	remove_wait_queue(sk_sleep(ctrl_sk), &ctrl_wait);
L
Linus Torvalds 已提交
732

733 734 735 736
	clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
	clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
	wake_up_interruptible(&session->report_queue);

L
Linus Torvalds 已提交
737 738 739 740 741 742
	down_write(&hidp_session_sem);

	hidp_del_timer(session);

	if (session->input) {
		input_unregister_device(session->input);
743
		session->input = NULL;
L
Linus Torvalds 已提交
744 745
	}

746
	if (session->hid) {
J
Jiri Slaby 已提交
747
		hid_destroy_device(session->hid);
748
		session->hid = NULL;
749 750
	}

751 752 753 754 755 756
	/* Wakeup user-space polling for socket errors */
	session->intr_sock->sk->sk_err = EUNATCH;
	session->ctrl_sock->sk->sk_err = EUNATCH;

	hidp_schedule(session);

757 758
	fput(session->intr_sock->file);

E
Eric Dumazet 已提交
759
	wait_event_timeout(*(sk_sleep(ctrl_sk)),
760 761 762 763
		(ctrl_sk->sk_state == BT_CLOSED), msecs_to_jiffies(500));

	fput(session->ctrl_sock->file);

764
	list_del(&session->list);
765

L
Linus Torvalds 已提交
766 767
	up_write(&hidp_session_sem);

768
	kfree(session->rd_data);
L
Linus Torvalds 已提交
769
	kfree(session);
770
	module_put_and_exit(0);
L
Linus Torvalds 已提交
771 772 773
	return 0;
}

774
static struct hci_conn *hidp_get_connection(struct hidp_session *session)
775 776 777
{
	bdaddr_t *src = &bt_sk(session->ctrl_sock->sk)->src;
	bdaddr_t *dst = &bt_sk(session->ctrl_sock->sk)->dst;
778
	struct hci_conn *conn;
779 780 781 782 783 784
	struct hci_dev *hdev;

	hdev = hci_get_route(dst, src);
	if (!hdev)
		return NULL;

785
	hci_dev_lock(hdev);
786
	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
787
	hci_dev_unlock(hdev);
788 789 790

	hci_dev_put(hdev);

791
	return conn;
792 793
}

A
Andrew Morton 已提交
794 795
static int hidp_setup_input(struct hidp_session *session,
				struct hidp_connadd_req *req)
L
Linus Torvalds 已提交
796
{
J
Jiri Slaby 已提交
797
	struct input_dev *input;
798
	int i;
L
Linus Torvalds 已提交
799

J
Jiri Slaby 已提交
800 801 802 803 804 805
	input = input_allocate_device();
	if (!input)
		return -ENOMEM;

	session->input = input;

806
	input_set_drvdata(input, session);
L
Linus Torvalds 已提交
807

808 809
	input->name = "Bluetooth HID Boot Protocol Device";

L
Linus Torvalds 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
	input->id.bustype = BUS_BLUETOOTH;
	input->id.vendor  = req->vendor;
	input->id.product = req->product;
	input->id.version = req->version;

	if (req->subclass & 0x40) {
		set_bit(EV_KEY, input->evbit);
		set_bit(EV_LED, input->evbit);
		set_bit(EV_REP, input->evbit);

		set_bit(LED_NUML,    input->ledbit);
		set_bit(LED_CAPSL,   input->ledbit);
		set_bit(LED_SCROLLL, input->ledbit);
		set_bit(LED_COMPOSE, input->ledbit);
		set_bit(LED_KANA,    input->ledbit);

		for (i = 0; i < sizeof(hidp_keycode); i++)
			set_bit(hidp_keycode[i], input->keybit);
		clear_bit(0, input->keybit);
	}

	if (req->subclass & 0x80) {
832 833 834 835 836 837 838
		input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
		input->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
			BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
		input->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
		input->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
			BIT_MASK(BTN_EXTRA);
		input->relbit[0] |= BIT_MASK(REL_WHEEL);
L
Linus Torvalds 已提交
839 840
	}

841
	input->dev.parent = &session->conn->dev;
842

L
Linus Torvalds 已提交
843 844
	input->event = hidp_input_event;

845
	return 0;
L
Linus Torvalds 已提交
846 847
}

848 849 850 851 852 853 854 855 856
static int hidp_open(struct hid_device *hid)
{
	return 0;
}

static void hidp_close(struct hid_device *hid)
{
}

J
Jiri Slaby 已提交
857 858 859 860
static int hidp_parse(struct hid_device *hid)
{
	struct hidp_session *session = hid->driver_data;

861 862
	return hid_parse_report(session->hid, session->rd_data,
			session->rd_size);
J
Jiri Slaby 已提交
863 864 865 866 867 868 869
}

static int hidp_start(struct hid_device *hid)
{
	struct hidp_session *session = hid->driver_data;
	struct hid_report *report;

870 871 872
	if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS)
		return 0;

J
Jiri Slaby 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
	list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].
			report_list, list)
		hidp_send_report(session, report);

	list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].
			report_list, list)
		hidp_send_report(session, report);

	return 0;
}

static void hidp_stop(struct hid_device *hid)
{
	struct hidp_session *session = hid->driver_data;

	skb_queue_purge(&session->ctrl_transmit);
	skb_queue_purge(&session->intr_transmit);

	hid->claimed = 0;
}

static struct hid_ll_driver hidp_hid_driver = {
	.parse = hidp_parse,
	.start = hidp_start,
	.stop = hidp_stop,
	.open  = hidp_open,
	.close = hidp_close,
	.hidinput_input_event = hidp_hidinput_event,
};

903 904
/* This function sets up the hid device. It does not add it
   to the HID system. That is done in hidp_add_connection(). */
J
Jiri Slaby 已提交
905
static int hidp_setup_hid(struct hidp_session *session,
A
Andrew Morton 已提交
906
				struct hidp_connadd_req *req)
907
{
J
Jiri Slaby 已提交
908
	struct hid_device *hid;
909
	int err;
910

911 912 913 914 915 916 917 918 919 920
	session->rd_data = kzalloc(req->rd_size, GFP_KERNEL);
	if (!session->rd_data)
		return -ENOMEM;

	if (copy_from_user(session->rd_data, req->rd_data, req->rd_size)) {
		err = -EFAULT;
		goto fault;
	}
	session->rd_size = req->rd_size;

J
Jiri Slaby 已提交
921
	hid = hid_allocate_device();
922 923 924 925
	if (IS_ERR(hid)) {
		err = PTR_ERR(hid);
		goto fault;
	}
926

J
Jiri Slaby 已提交
927
	session->hid = hid;
928

929 930 931 932 933 934
	hid->driver_data = session;

	hid->bus     = BUS_BLUETOOTH;
	hid->vendor  = req->vendor;
	hid->product = req->product;
	hid->version = req->version;
J
Jiri Slaby 已提交
935
	hid->country = req->country;
936

937
	strncpy(hid->name, req->name, sizeof(req->name) - 1);
938 939 940 941 942 943

	snprintf(hid->phys, sizeof(hid->phys), "%pMR",
		 &bt_sk(session->ctrl_sock->sk)->src);

	snprintf(hid->uniq, sizeof(hid->uniq), "%pMR",
		 &bt_sk(session->ctrl_sock->sk)->dst);
944

945
	hid->dev.parent = &session->conn->dev;
J
Jiri Slaby 已提交
946
	hid->ll_driver = &hidp_hid_driver;
J
Jiri Slaby 已提交
947

948
	hid->hid_get_raw_report = hidp_get_raw_report;
949 950
	hid->hid_output_raw_report = hidp_output_raw_report;

951 952 953 954 955 956 957
	/* True if device is blacklisted in drivers/hid/hid-core.c */
	if (hid_ignore(hid)) {
		hid_destroy_device(session->hid);
		session->hid = NULL;
		return -ENODEV;
	}

J
Jiri Slaby 已提交
958
	return 0;
959

960 961 962 963
fault:
	kfree(session->rd_data);
	session->rd_data = NULL;

964
	return err;
965 966
}

L
Linus Torvalds 已提交
967 968 969
int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock)
{
	struct hidp_session *session, *s;
S
Szymon Janc 已提交
970
	int vendor, product;
L
Linus Torvalds 已提交
971 972 973 974
	int err;

	BT_DBG("");

975 976
	if (!l2cap_is_socket(ctrl_sock) || !l2cap_is_socket(intr_sock))
		return -EINVAL;
L
Linus Torvalds 已提交
977 978 979 980
	if (bacmp(&bt_sk(ctrl_sock->sk)->src, &bt_sk(intr_sock->sk)->src) ||
			bacmp(&bt_sk(ctrl_sock->sk)->dst, &bt_sk(intr_sock->sk)->dst))
		return -ENOTUNIQ;

981 982
	BT_DBG("rd_data %p rd_size %d", req->rd_data, req->rd_size);

L
Linus Torvalds 已提交
983 984 985
	down_write(&hidp_session_sem);

	s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst);
986
	if (s) {
987 988 989 990 991 992 993 994
		up_write(&hidp_session_sem);
		return -EEXIST;
	}

	session = kzalloc(sizeof(struct hidp_session), GFP_KERNEL);
	if (!session) {
		up_write(&hidp_session_sem);
		return -ENOMEM;
L
Linus Torvalds 已提交
995 996 997 998
	}

	bacpy(&session->bdaddr, &bt_sk(ctrl_sock->sk)->dst);

999 1000 1001 1002
	session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl_sock->sk)->chan->omtu,
					l2cap_pi(ctrl_sock->sk)->chan->imtu);
	session->intr_mtu = min_t(uint, l2cap_pi(intr_sock->sk)->chan->omtu,
					l2cap_pi(intr_sock->sk)->chan->imtu);
L
Linus Torvalds 已提交
1003 1004 1005 1006 1007 1008

	BT_DBG("ctrl mtu %d intr mtu %d", session->ctrl_mtu, session->intr_mtu);

	session->ctrl_sock = ctrl_sock;
	session->intr_sock = intr_sock;

1009 1010 1011 1012 1013 1014
	session->conn = hidp_get_connection(session);
	if (!session->conn) {
		err = -ENOTCONN;
		goto failed;
	}

1015
	setup_timer(&session->timer, hidp_idle_timeout, (unsigned long)session);
L
Linus Torvalds 已提交
1016 1017 1018 1019

	skb_queue_head_init(&session->ctrl_transmit);
	skb_queue_head_init(&session->intr_transmit);

1020 1021
	mutex_init(&session->report_mutex);
	init_waitqueue_head(&session->report_queue);
1022 1023
	init_waitqueue_head(&session->startup_queue);
	session->waiting_for_startup = 1;
L
Linus Torvalds 已提交
1024 1025 1026
	session->flags   = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
	session->idle_to = req->idle_to;

1027
	list_add(&session->list, &hidp_session_list);
1028

J
Jiri Slaby 已提交
1029
	if (req->rd_size > 0) {
J
Jiri Slaby 已提交
1030
		err = hidp_setup_hid(session, req);
1031
		if (err && err != -ENODEV)
1032
			goto purge;
J
Jiri Slaby 已提交
1033 1034 1035 1036 1037
	}

	if (!session->hid) {
		err = hidp_setup_input(session, req);
		if (err < 0)
1038
			goto purge;
J
Jiri Slaby 已提交
1039
	}
1040

L
Linus Torvalds 已提交
1041 1042
	hidp_set_timer(session);

S
Szymon Janc 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	if (session->hid) {
		vendor  = session->hid->vendor;
		product = session->hid->product;
	} else if (session->input) {
		vendor  = session->input->id.vendor;
		product = session->input->id.product;
	} else {
		vendor = 0x0000;
		product = 0x0000;
	}

	session->task = kthread_run(hidp_session, session, "khidpd_%04x%04x",
							vendor, product);
	if (IS_ERR(session->task)) {
		err = PTR_ERR(session->task);
L
Linus Torvalds 已提交
1058
		goto unlink;
S
Szymon Janc 已提交
1059 1060
	}

1061 1062 1063 1064 1065
	while (session->waiting_for_startup) {
		wait_event_interruptible(session->startup_queue,
			!session->waiting_for_startup);
	}

1066 1067 1068 1069 1070
	if (session->hid)
		err = hid_add_device(session->hid);
	else
		err = input_register_device(session->input);

1071 1072 1073 1074 1075 1076
	if (err < 0) {
		atomic_inc(&session->terminate);
		wake_up_process(session->task);
		up_write(&hidp_session_sem);
		return err;
	}
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092

	if (session->input) {
		hidp_send_ctrl_message(session,
			HIDP_TRANS_SET_PROTOCOL | HIDP_PROTO_BOOT, NULL, 0);
		session->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);

		session->leds = 0xff;
		hidp_input_event(session->input, EV_LED, 0, 0);
	}

	up_write(&hidp_session_sem);
	return 0;

unlink:
	hidp_del_timer(session);

1093
	if (session->input) {
L
Linus Torvalds 已提交
1094
		input_unregister_device(session->input);
1095 1096 1097 1098
		session->input = NULL;
	}

	if (session->hid) {
J
Jiri Slaby 已提交
1099
		hid_destroy_device(session->hid);
1100 1101 1102
		session->hid = NULL;
	}

1103 1104 1105
	kfree(session->rd_data);
	session->rd_data = NULL;

1106
purge:
1107
	list_del(&session->list);
1108

J
Jiri Slaby 已提交
1109 1110
	skb_queue_purge(&session->ctrl_transmit);
	skb_queue_purge(&session->intr_transmit);
1111

J
Jiri Slaby 已提交
1112 1113
failed:
	up_write(&hidp_session_sem);
1114

L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	kfree(session);
	return err;
}

int hidp_del_connection(struct hidp_conndel_req *req)
{
	struct hidp_session *session;
	int err = 0;

	BT_DBG("");

	down_read(&hidp_session_sem);

	session = __hidp_get_session(&req->bdaddr);
	if (session) {
		if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) {
			hidp_send_ctrl_message(session,
				HIDP_TRANS_HID_CONTROL | HIDP_CTRL_VIRTUAL_CABLE_UNPLUG, NULL, 0);
		} else {
			/* Flush the transmit queues */
			skb_queue_purge(&session->ctrl_transmit);
			skb_queue_purge(&session->intr_transmit);

1138 1139
			atomic_inc(&session->terminate);
			wake_up_process(session->task);
L
Linus Torvalds 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
		}
	} else
		err = -ENOENT;

	up_read(&hidp_session_sem);
	return err;
}

int hidp_get_connlist(struct hidp_connlist_req *req)
{
1150
	struct hidp_session *session;
L
Linus Torvalds 已提交
1151 1152 1153 1154 1155 1156
	int err = 0, n = 0;

	BT_DBG("");

	down_read(&hidp_session_sem);

1157
	list_for_each_entry(session, &hidp_session_list, list) {
L
Linus Torvalds 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
		struct hidp_conninfo ci;

		__hidp_copy_session(session, &ci);

		if (copy_to_user(req->ci, &ci, sizeof(ci))) {
			err = -EFAULT;
			break;
		}

		if (++n >= req->cnum)
			break;

		req->ci++;
	}
	req->cnum = n;

	up_read(&hidp_session_sem);
	return err;
}

int hidp_get_conninfo(struct hidp_conninfo *ci)
{
	struct hidp_session *session;
	int err = 0;

	down_read(&hidp_session_sem);

	session = __hidp_get_session(&ci->bdaddr);
	if (session)
		__hidp_copy_session(session, ci);
	else
		err = -ENOENT;

	up_read(&hidp_session_sem);
	return err;
}

static int __init hidp_init(void)
{
	BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);

1199
	return hidp_init_sockets();
L
Linus Torvalds 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
}

static void __exit hidp_exit(void)
{
	hidp_cleanup_sockets();
}

module_init(hidp_init);
module_exit(hidp_exit);

MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");
MODULE_ALIAS("bt-proto-6");