rc-main.c 48.4 KB
Newer Older
1
/* rc-main.c - Remote Controller core module
2
 *
3
 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
4 5 6 7 8 9 10 11 12
 *
 * This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
13 14
 */

15 16
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

17
#include <media/rc-core.h>
18
#include <linux/bsearch.h>
19 20
#include <linux/spinlock.h>
#include <linux/delay.h>
21
#include <linux/input.h>
22
#include <linux/leds.h>
23
#include <linux/slab.h>
24
#include <linux/idr.h>
25
#include <linux/device.h>
26
#include <linux/module.h>
27
#include "rc-core-priv.h"
28

29 30 31
/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
#define IR_TAB_MIN_SIZE	256
#define IR_TAB_MAX_SIZE	8192
32
#define RC_DEV_MAX	256
33

34 35 36 37 38
static const struct {
	const char *name;
	unsigned int repeat_period;
	unsigned int scancode_bits;
} protocols[] = {
39 40 41
	[RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 250 },
	[RC_PROTO_OTHER] = { .name = "other", .repeat_period = 250 },
	[RC_PROTO_RC5] = { .name = "rc-5",
42
		.scancode_bits = 0x1f7f, .repeat_period = 164 },
43
	[RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
44
		.scancode_bits = 0x1f7f3f, .repeat_period = 164 },
45
	[RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
46
		.scancode_bits = 0x2fff, .repeat_period = 164 },
47
	[RC_PROTO_JVC] = { .name = "jvc",
48
		.scancode_bits = 0xffff, .repeat_period = 250 },
49
	[RC_PROTO_SONY12] = { .name = "sony-12",
50
		.scancode_bits = 0x1f007f, .repeat_period = 100 },
51
	[RC_PROTO_SONY15] = { .name = "sony-15",
52
		.scancode_bits = 0xff007f, .repeat_period = 100 },
53
	[RC_PROTO_SONY20] = { .name = "sony-20",
54
		.scancode_bits = 0x1fff7f, .repeat_period = 100 },
55
	[RC_PROTO_NEC] = { .name = "nec",
56
		.scancode_bits = 0xffff, .repeat_period = 160 },
57
	[RC_PROTO_NECX] = { .name = "nec-x",
58
		.scancode_bits = 0xffffff, .repeat_period = 160 },
59
	[RC_PROTO_NEC32] = { .name = "nec-32",
60
		.scancode_bits = 0xffffffff, .repeat_period = 160 },
61
	[RC_PROTO_SANYO] = { .name = "sanyo",
62
		.scancode_bits = 0x1fffff, .repeat_period = 250 },
63
	[RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
64
		.scancode_bits = 0xffff, .repeat_period = 150 },
65
	[RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
66
		.scancode_bits = 0x1fffff, .repeat_period = 150 },
67
	[RC_PROTO_RC6_0] = { .name = "rc-6-0",
68
		.scancode_bits = 0xffff, .repeat_period = 164 },
69
	[RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
70
		.scancode_bits = 0xfffff, .repeat_period = 164 },
71
	[RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
72
		.scancode_bits = 0xffffff, .repeat_period = 164 },
73
	[RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
74
		.scancode_bits = 0xffffffff, .repeat_period = 164 },
75
	[RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
76
		.scancode_bits = 0xffff7fff, .repeat_period = 164 },
77
	[RC_PROTO_SHARP] = { .name = "sharp",
78
		.scancode_bits = 0x1fff, .repeat_period = 250 },
79 80
	[RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 },
	[RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 },
81
};
82

83
/* Used to keep track of known keymaps */
84 85
static LIST_HEAD(rc_map_list);
static DEFINE_SPINLOCK(rc_map_lock);
86
static struct led_trigger *led_feedback;
87

88 89 90
/* Used to keep track of rc devices */
static DEFINE_IDA(rc_ida);

91
static struct rc_map_list *seek_rc_map(const char *name)
92
{
93
	struct rc_map_list *map = NULL;
94 95 96 97 98 99 100 101 102 103 104 105 106

	spin_lock(&rc_map_lock);
	list_for_each_entry(map, &rc_map_list, list) {
		if (!strcmp(name, map->map.name)) {
			spin_unlock(&rc_map_lock);
			return map;
		}
	}
	spin_unlock(&rc_map_lock);

	return NULL;
}

107
struct rc_map *rc_map_get(const char *name)
108 109
{

110
	struct rc_map_list *map;
111 112

	map = seek_rc_map(name);
113
#ifdef CONFIG_MODULES
114
	if (!map) {
115
		int rc = request_module("%s", name);
116
		if (rc < 0) {
117
			pr_err("Couldn't load IR keymap %s\n", name);
118 119 120 121 122 123 124 125
			return NULL;
		}
		msleep(20);	/* Give some time for IR to register */

		map = seek_rc_map(name);
	}
#endif
	if (!map) {
126
		pr_err("IR keymap %s not found\n", name);
127 128 129 130 131 132 133
		return NULL;
	}

	printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);

	return &map->map;
}
134
EXPORT_SYMBOL_GPL(rc_map_get);
135

136
int rc_map_register(struct rc_map_list *map)
137 138 139 140 141 142
{
	spin_lock(&rc_map_lock);
	list_add_tail(&map->list, &rc_map_list);
	spin_unlock(&rc_map_lock);
	return 0;
}
143
EXPORT_SYMBOL_GPL(rc_map_register);
144

145
void rc_map_unregister(struct rc_map_list *map)
146 147 148 149 150
{
	spin_lock(&rc_map_lock);
	list_del(&map->list);
	spin_unlock(&rc_map_lock);
}
151
EXPORT_SYMBOL_GPL(rc_map_unregister);
152 153


154
static struct rc_map_table empty[] = {
155 156 157
	{ 0x2a, KEY_COFFEE },
};

158
static struct rc_map_list empty_map = {
159
	.map = {
160 161 162 163
		.scan     = empty,
		.size     = ARRAY_SIZE(empty),
		.rc_proto = RC_PROTO_UNKNOWN,	/* Legacy IR type */
		.name     = RC_MAP_EMPTY,
164 165 166
	}
};

167 168
/**
 * ir_create_table() - initializes a scancode table
169
 * @rc_map:	the rc_map to initialize
170
 * @name:	name to assign to the table
171
 * @rc_proto:	ir type to assign to the new table
172 173 174
 * @size:	initial size of the table
 * @return:	zero on success or a negative error code
 *
175
 * This routine will initialize the rc_map and will allocate
176
 * memory to hold at least the specified number of elements.
177
 */
178
static int ir_create_table(struct rc_map *rc_map,
179
			   const char *name, u64 rc_proto, size_t size)
180
{
181 182 183
	rc_map->name = kstrdup(name, GFP_KERNEL);
	if (!rc_map->name)
		return -ENOMEM;
184
	rc_map->rc_proto = rc_proto;
185 186
	rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
	rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
187
	rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
188 189 190
	if (!rc_map->scan) {
		kfree(rc_map->name);
		rc_map->name = NULL;
191
		return -ENOMEM;
192
	}
193 194

	IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
195
		   rc_map->size, rc_map->alloc);
196 197 198 199 200
	return 0;
}

/**
 * ir_free_table() - frees memory allocated by a scancode table
201
 * @rc_map:	the table whose mappings need to be freed
202 203 204 205
 *
 * This routine will free memory alloctaed for key mappings used by given
 * scancode table.
 */
206
static void ir_free_table(struct rc_map *rc_map)
207
{
208
	rc_map->size = 0;
209
	kfree(rc_map->name);
210
	rc_map->name = NULL;
211 212
	kfree(rc_map->scan);
	rc_map->scan = NULL;
213 214
}

215
/**
216
 * ir_resize_table() - resizes a scancode table if necessary
217
 * @rc_map:	the rc_map to resize
218
 * @gfp_flags:	gfp flags to use when allocating memory
219
 * @return:	zero on success or a negative error code
220
 *
221
 * This routine will shrink the rc_map if it has lots of
222
 * unused entries and grow it if it is full.
223
 */
224
static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
225
{
226
	unsigned int oldalloc = rc_map->alloc;
227
	unsigned int newalloc = oldalloc;
228 229
	struct rc_map_table *oldscan = rc_map->scan;
	struct rc_map_table *newscan;
230

231
	if (rc_map->size == rc_map->len) {
232
		/* All entries in use -> grow keytable */
233
		if (rc_map->alloc >= IR_TAB_MAX_SIZE)
234
			return -ENOMEM;
235

236 237 238
		newalloc *= 2;
		IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
	}
239

240
	if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
241 242 243 244
		/* Less than 1/3 of entries in use -> shrink keytable */
		newalloc /= 2;
		IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
	}
245

246 247
	if (newalloc == oldalloc)
		return 0;
248

249
	newscan = kmalloc(newalloc, gfp_flags);
250 251 252 253
	if (!newscan) {
		IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
		return -ENOMEM;
	}
254

255
	memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
256 257
	rc_map->scan = newscan;
	rc_map->alloc = newalloc;
258
	rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
259 260
	kfree(oldscan);
	return 0;
261 262
}

263
/**
264
 * ir_update_mapping() - set a keycode in the scancode->keycode table
265
 * @dev:	the struct rc_dev device descriptor
266
 * @rc_map:	scancode table to be adjusted
267 268 269 270
 * @index:	index of the mapping that needs to be updated
 * @keycode:	the desired keycode
 * @return:	previous keycode assigned to the mapping
 *
271
 * This routine is used to update scancode->keycode mapping at given
272 273
 * position.
 */
274
static unsigned int ir_update_mapping(struct rc_dev *dev,
275
				      struct rc_map *rc_map,
276 277 278
				      unsigned int index,
				      unsigned int new_keycode)
{
279
	int old_keycode = rc_map->scan[index].keycode;
280 281 282 283 284
	int i;

	/* Did the user wish to remove the mapping? */
	if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
		IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
285 286 287
			   index, rc_map->scan[index].scancode);
		rc_map->len--;
		memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
288
			(rc_map->len - index) * sizeof(struct rc_map_table));
289 290 291 292
	} else {
		IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
			   index,
			   old_keycode == KEY_RESERVED ? "New" : "Replacing",
293 294
			   rc_map->scan[index].scancode, new_keycode);
		rc_map->scan[index].keycode = new_keycode;
295
		__set_bit(new_keycode, dev->input_dev->keybit);
296 297 298 299
	}

	if (old_keycode != KEY_RESERVED) {
		/* A previous mapping was updated... */
300
		__clear_bit(old_keycode, dev->input_dev->keybit);
301
		/* ... but another scancode might use the same keycode */
302 303
		for (i = 0; i < rc_map->len; i++) {
			if (rc_map->scan[i].keycode == old_keycode) {
304
				__set_bit(old_keycode, dev->input_dev->keybit);
305 306 307 308 309
				break;
			}
		}

		/* Possibly shrink the keytable, failure is not a problem */
310
		ir_resize_table(rc_map, GFP_ATOMIC);
311 312 313 314 315 316
	}

	return old_keycode;
}

/**
317
 * ir_establish_scancode() - set a keycode in the scancode->keycode table
318
 * @dev:	the struct rc_dev device descriptor
319
 * @rc_map:	scancode table to be searched
320 321
 * @scancode:	the desired scancode
 * @resize:	controls whether we allowed to resize the table to
L
Lucas De Marchi 已提交
322
 *		accommodate not yet present scancodes
323 324
 * @return:	index of the mapping containing scancode in question
 *		or -1U in case of failure.
325
 *
326
 * This routine is used to locate given scancode in rc_map.
327 328
 * If scancode is not yet present the routine will allocate a new slot
 * for it.
329
 */
330
static unsigned int ir_establish_scancode(struct rc_dev *dev,
331
					  struct rc_map *rc_map,
332 333
					  unsigned int scancode,
					  bool resize)
334
{
335
	unsigned int i;
336 337 338 339 340 341

	/*
	 * Unfortunately, some hardware-based IR decoders don't provide
	 * all bits for the complete IR code. In general, they provide only
	 * the command part of the IR code. Yet, as it is possible to replace
	 * the provided IR with another one, it is needed to allow loading
342 343
	 * IR tables from other remotes. So, we support specifying a mask to
	 * indicate the valid bits of the scancodes.
344
	 */
345 346
	if (dev->scancode_mask)
		scancode &= dev->scancode_mask;
347 348

	/* First check if we already have a mapping for this ir command */
349 350
	for (i = 0; i < rc_map->len; i++) {
		if (rc_map->scan[i].scancode == scancode)
351 352
			return i;

353
		/* Keytable is sorted from lowest to highest scancode */
354
		if (rc_map->scan[i].scancode >= scancode)
355 356
			break;
	}
357

358
	/* No previous mapping found, we might need to grow the table */
359 360
	if (rc_map->size == rc_map->len) {
		if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
361 362
			return -1U;
	}
363

364
	/* i is the proper index to insert our new keycode */
365 366
	if (i < rc_map->len)
		memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
367
			(rc_map->len - i) * sizeof(struct rc_map_table));
368 369 370
	rc_map->scan[i].scancode = scancode;
	rc_map->scan[i].keycode = KEY_RESERVED;
	rc_map->len++;
371

372
	return i;
373 374
}

375
/**
376
 * ir_setkeycode() - set a keycode in the scancode->keycode table
377
 * @idev:	the struct input_dev device descriptor
378
 * @scancode:	the desired scancode
379 380
 * @keycode:	result
 * @return:	-EINVAL if the keycode could not be inserted, otherwise zero.
381
 *
382
 * This routine is used to handle evdev EVIOCSKEY ioctl.
383
 */
384
static int ir_setkeycode(struct input_dev *idev,
385 386
			 const struct input_keymap_entry *ke,
			 unsigned int *old_keycode)
387
{
388
	struct rc_dev *rdev = input_get_drvdata(idev);
389
	struct rc_map *rc_map = &rdev->rc_map;
390 391
	unsigned int index;
	unsigned int scancode;
392
	int retval = 0;
393
	unsigned long flags;
394

395
	spin_lock_irqsave(&rc_map->lock, flags);
396 397 398

	if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
		index = ke->index;
399
		if (index >= rc_map->len) {
400 401 402 403 404 405 406 407
			retval = -EINVAL;
			goto out;
		}
	} else {
		retval = input_scancode_to_scalar(ke, &scancode);
		if (retval)
			goto out;

408 409
		index = ir_establish_scancode(rdev, rc_map, scancode, true);
		if (index >= rc_map->len) {
410 411 412 413 414
			retval = -ENOMEM;
			goto out;
		}
	}

415
	*old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
416 417

out:
418
	spin_unlock_irqrestore(&rc_map->lock, flags);
419
	return retval;
420 421 422
}

/**
423
 * ir_setkeytable() - sets several entries in the scancode->keycode table
424
 * @dev:	the struct rc_dev device descriptor
425 426
 * @to:		the struct rc_map to copy entries to
 * @from:	the struct rc_map to copy entries from
427
 * @return:	-ENOMEM if all keycodes could not be inserted, otherwise zero.
428
 *
429
 * This routine is used to handle table initialization.
430
 */
431
static int ir_setkeytable(struct rc_dev *dev,
432
			  const struct rc_map *from)
433
{
434
	struct rc_map *rc_map = &dev->rc_map;
435 436 437
	unsigned int i, index;
	int rc;

438
	rc = ir_create_table(rc_map, from->name,
439
			     from->rc_proto, from->size);
440 441 442
	if (rc)
		return rc;

443
	for (i = 0; i < from->size; i++) {
444
		index = ir_establish_scancode(dev, rc_map,
445
					      from->scan[i].scancode, false);
446
		if (index >= rc_map->len) {
447
			rc = -ENOMEM;
448
			break;
449 450
		}

451
		ir_update_mapping(dev, rc_map, index,
452
				  from->scan[i].keycode);
453
	}
454 455

	if (rc)
456
		ir_free_table(rc_map);
457

458
	return rc;
459 460
}

461 462 463 464 465 466 467 468 469 470 471 472
static int rc_map_cmp(const void *key, const void *elt)
{
	const unsigned int *scancode = key;
	const struct rc_map_table *e = elt;

	if (*scancode < e->scancode)
		return -1;
	else if (*scancode > e->scancode)
		return 1;
	return 0;
}

473 474
/**
 * ir_lookup_by_scancode() - locate mapping by scancode
475
 * @rc_map:	the struct rc_map to search
476 477 478 479 480 481
 * @scancode:	scancode to look for in the table
 * @return:	index in the table, -1U if not found
 *
 * This routine performs binary search in RC keykeymap table for
 * given scancode.
 */
482
static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
483 484
					  unsigned int scancode)
{
485 486 487 488 489 490 491 492
	struct rc_map_table *res;

	res = bsearch(&scancode, rc_map->scan, rc_map->len,
		      sizeof(struct rc_map_table), rc_map_cmp);
	if (!res)
		return -1U;
	else
		return res - rc_map->scan;
493 494
}

495
/**
496
 * ir_getkeycode() - get a keycode from the scancode->keycode table
497
 * @idev:	the struct input_dev device descriptor
498
 * @scancode:	the desired scancode
499 500
 * @keycode:	used to return the keycode, if found, or KEY_RESERVED
 * @return:	always returns zero.
501
 *
502
 * This routine is used to handle evdev EVIOCGKEY ioctl.
503
 */
504
static int ir_getkeycode(struct input_dev *idev,
505
			 struct input_keymap_entry *ke)
506
{
507
	struct rc_dev *rdev = input_get_drvdata(idev);
508
	struct rc_map *rc_map = &rdev->rc_map;
509
	struct rc_map_table *entry;
510 511 512 513
	unsigned long flags;
	unsigned int index;
	unsigned int scancode;
	int retval;
514

515
	spin_lock_irqsave(&rc_map->lock, flags);
516 517 518 519 520 521 522 523

	if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
		index = ke->index;
	} else {
		retval = input_scancode_to_scalar(ke, &scancode);
		if (retval)
			goto out;

524
		index = ir_lookup_by_scancode(rc_map, scancode);
525 526
	}

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
	if (index < rc_map->len) {
		entry = &rc_map->scan[index];

		ke->index = index;
		ke->keycode = entry->keycode;
		ke->len = sizeof(entry->scancode);
		memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));

	} else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
		/*
		 * We do not really know the valid range of scancodes
		 * so let's respond with KEY_RESERVED to anything we
		 * do not have mapping for [yet].
		 */
		ke->index = index;
		ke->keycode = KEY_RESERVED;
	} else {
544 545
		retval = -EINVAL;
		goto out;
546 547
	}

548 549
	retval = 0;

550
out:
551
	spin_unlock_irqrestore(&rc_map->lock, flags);
552
	return retval;
553 554 555
}

/**
556
 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
557 558 559
 * @dev:	the struct rc_dev descriptor of the device
 * @scancode:	the scancode to look for
 * @return:	the corresponding keycode, or KEY_RESERVED
560
 *
561 562 563
 * This routine is used by drivers which need to convert a scancode to a
 * keycode. Normally it should not be used since drivers should have no
 * interest in keycodes.
564
 */
565
u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
566
{
567
	struct rc_map *rc_map = &dev->rc_map;
568 569 570 571
	unsigned int keycode;
	unsigned int index;
	unsigned long flags;

572
	spin_lock_irqsave(&rc_map->lock, flags);
573

574 575 576
	index = ir_lookup_by_scancode(rc_map, scancode);
	keycode = index < rc_map->len ?
			rc_map->scan[index].keycode : KEY_RESERVED;
577

578
	spin_unlock_irqrestore(&rc_map->lock, flags);
579

580 581
	if (keycode != KEY_RESERVED)
		IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
582
			   dev->device_name, scancode, keycode);
583

584
	return keycode;
585
}
586
EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
587

588
/**
589
 * ir_do_keyup() - internal function to signal the release of a keypress
590
 * @dev:	the struct rc_dev descriptor of the device
591
 * @sync:	whether or not to call input_sync
592
 *
593 594
 * This function is used internally to release a keypress, it must be
 * called with keylock held.
595
 */
596
static void ir_do_keyup(struct rc_dev *dev, bool sync)
597
{
598
	if (!dev->keypressed)
599 600
		return;

601 602
	IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
	input_report_key(dev->input_dev, dev->last_keycode, 0);
603
	led_trigger_event(led_feedback, LED_OFF);
604 605
	if (sync)
		input_sync(dev->input_dev);
606
	dev->keypressed = false;
607
}
608 609

/**
610
 * rc_keyup() - signals the release of a keypress
611
 * @dev:	the struct rc_dev descriptor of the device
612 613 614 615
 *
 * This routine is used to signal that a key has been released on the
 * remote control.
 */
616
void rc_keyup(struct rc_dev *dev)
617 618 619
{
	unsigned long flags;

620
	spin_lock_irqsave(&dev->keylock, flags);
621
	ir_do_keyup(dev, true);
622
	spin_unlock_irqrestore(&dev->keylock, flags);
623
}
624
EXPORT_SYMBOL_GPL(rc_keyup);
625 626 627

/**
 * ir_timer_keyup() - generates a keyup event after a timeout
628
 * @cookie:	a pointer to the struct rc_dev for the device
629 630 631
 *
 * This routine will generate a keyup event some time after a keydown event
 * is generated when no further activity has been detected.
632
 */
633
static void ir_timer_keyup(struct timer_list *t)
634
{
635
	struct rc_dev *dev = from_timer(dev, t, timer_keyup);
636 637 638 639 640 641 642 643 644 645 646 647
	unsigned long flags;

	/*
	 * ir->keyup_jiffies is used to prevent a race condition if a
	 * hardware interrupt occurs at this point and the keyup timer
	 * event is moved further into the future as a result.
	 *
	 * The timer will then be reactivated and this function called
	 * again in the future. We need to exit gracefully in that case
	 * to allow the input subsystem to do its auto-repeat magic or
	 * a keyup event might follow immediately after the keydown.
	 */
648 649
	spin_lock_irqsave(&dev->keylock, flags);
	if (time_is_before_eq_jiffies(dev->keyup_jiffies))
650
		ir_do_keyup(dev, true);
651
	spin_unlock_irqrestore(&dev->keylock, flags);
652 653 654
}

/**
655
 * rc_repeat() - signals that a key is still pressed
656
 * @dev:	the struct rc_dev descriptor of the device
657 658 659 660 661
 *
 * This routine is used by IR decoders when a repeat message which does
 * not include the necessary bits to reproduce the scancode has been
 * received.
 */
662
void rc_repeat(struct rc_dev *dev)
663 664
{
	unsigned long flags;
665
	unsigned int timeout = protocols[dev->last_protocol].repeat_period;
666

667
	spin_lock_irqsave(&dev->keylock, flags);
668

669
	if (!dev->keypressed)
670
		goto out;
671

672 673 674
	input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
	input_sync(dev->input_dev);

675
	dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
676
	mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
677 678

out:
679
	spin_unlock_irqrestore(&dev->keylock, flags);
680
}
681
EXPORT_SYMBOL_GPL(rc_repeat);
682 683

/**
684
 * ir_do_keydown() - internal function to process a keypress
685
 * @dev:	the struct rc_dev descriptor of the device
686
 * @protocol:	the protocol of the keypress
687 688 689
 * @scancode:   the scancode of the keypress
 * @keycode:    the keycode of the keypress
 * @toggle:     the toggle value of the keypress
690
 *
691 692
 * This function is used internally to register a keypress, it must be
 * called with keylock held.
693
 */
694
static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
695
			  u32 scancode, u32 keycode, u8 toggle)
696
{
697
	bool new_event = (!dev->keypressed		 ||
698
			  dev->last_protocol != protocol ||
699
			  dev->last_scancode != scancode ||
700
			  dev->last_toggle   != toggle);
701

702 703
	if (new_event && dev->keypressed)
		ir_do_keyup(dev, false);
704

705
	input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
706

707 708 709
	if (new_event && keycode != KEY_RESERVED) {
		/* Register a keypress */
		dev->keypressed = true;
710
		dev->last_protocol = protocol;
711 712 713 714
		dev->last_scancode = scancode;
		dev->last_toggle = toggle;
		dev->last_keycode = keycode;

715
		IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
716
			   dev->device_name, keycode, protocol, scancode);
717
		input_report_key(dev->input_dev, keycode, 1);
718 719

		led_trigger_event(led_feedback, LED_FULL);
720
	}
721

722
	input_sync(dev->input_dev);
723
}
724

725
/**
726
 * rc_keydown() - generates input event for a key press
727
 * @dev:	the struct rc_dev descriptor of the device
728 729
 * @protocol:	the protocol for the keypress
 * @scancode:	the scancode for the keypress
730 731 732
 * @toggle:     the toggle value (protocol dependent, if the protocol doesn't
 *              support toggle values, this should be set to zero)
 *
733 734
 * This routine is used to signal that a key has been pressed on the
 * remote control.
735
 */
736 737
void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
		u8 toggle)
738 739
{
	unsigned long flags;
740
	u32 keycode = rc_g_keycode_from_table(dev, scancode);
741

742
	spin_lock_irqsave(&dev->keylock, flags);
743
	ir_do_keydown(dev, protocol, scancode, keycode, toggle);
744

745
	if (dev->keypressed) {
746 747
		dev->keyup_jiffies = jiffies +
			msecs_to_jiffies(protocols[protocol].repeat_period);
748
		mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
749
	}
750
	spin_unlock_irqrestore(&dev->keylock, flags);
751
}
752
EXPORT_SYMBOL_GPL(rc_keydown);
753

754
/**
755
 * rc_keydown_notimeout() - generates input event for a key press without
756
 *                          an automatic keyup event at a later time
757
 * @dev:	the struct rc_dev descriptor of the device
758 759
 * @protocol:	the protocol for the keypress
 * @scancode:	the scancode for the keypress
760 761 762
 * @toggle:     the toggle value (protocol dependent, if the protocol doesn't
 *              support toggle values, this should be set to zero)
 *
763
 * This routine is used to signal that a key has been pressed on the
764
 * remote control. The driver must manually call rc_keyup() at a later stage.
765
 */
766
void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
767
			  u32 scancode, u8 toggle)
768 769
{
	unsigned long flags;
770
	u32 keycode = rc_g_keycode_from_table(dev, scancode);
771

772
	spin_lock_irqsave(&dev->keylock, flags);
773
	ir_do_keydown(dev, protocol, scancode, keycode, toggle);
774
	spin_unlock_irqrestore(&dev->keylock, flags);
775
}
776
EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
777

S
Sean Young 已提交
778 779 780
/**
 * rc_validate_filter() - checks that the scancode and mask are valid and
 *			  provides sensible defaults
781
 * @dev:	the struct rc_dev descriptor of the device
S
Sean Young 已提交
782 783 784
 * @filter:	the scancode and mask
 * @return:	0 or -EINVAL if the filter is not valid
 */
785
static int rc_validate_filter(struct rc_dev *dev,
S
Sean Young 已提交
786 787
			      struct rc_scancode_filter *filter)
{
788
	u32 mask, s = filter->data;
789
	enum rc_proto protocol = dev->wakeup_protocol;
S
Sean Young 已提交
790

791
	if (protocol >= ARRAY_SIZE(protocols))
792 793
		return -EINVAL;

794 795
	mask = protocols[protocol].scancode_bits;

S
Sean Young 已提交
796
	switch (protocol) {
797
	case RC_PROTO_NECX:
S
Sean Young 已提交
798 799 800
		if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
			return -EINVAL;
		break;
801
	case RC_PROTO_NEC32:
S
Sean Young 已提交
802 803 804
		if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
			return -EINVAL;
		break;
805
	case RC_PROTO_RC6_MCE:
S
Sean Young 已提交
806 807 808
		if ((s & 0xffff0000) != 0x800f0000)
			return -EINVAL;
		break;
809
	case RC_PROTO_RC6_6A_32:
S
Sean Young 已提交
810 811 812 813 814 815 816
		if ((s & 0xffff0000) == 0x800f0000)
			return -EINVAL;
		break;
	default:
		break;
	}

817 818
	filter->data &= mask;
	filter->mask &= mask;
S
Sean Young 已提交
819

820 821 822
	/*
	 * If we have to raw encode the IR for wakeup, we cannot have a mask
	 */
823
	if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
824 825
		return -EINVAL;

S
Sean Young 已提交
826 827 828
	return 0;
}

829 830 831 832 833 834 835 836
int rc_open(struct rc_dev *rdev)
{
	int rval = 0;

	if (!rdev)
		return -EINVAL;

	mutex_lock(&rdev->lock);
837

838
	if (!rdev->users++ && rdev->open != NULL)
839 840 841 842 843 844 845 846 847 848 849
		rval = rdev->open(rdev);

	if (rval)
		rdev->users--;

	mutex_unlock(&rdev->lock);

	return rval;
}
EXPORT_SYMBOL_GPL(rc_open);

850
static int ir_open(struct input_dev *idev)
851
{
852
	struct rc_dev *rdev = input_get_drvdata(idev);
853

854 855 856 857 858 859 860 861
	return rc_open(rdev);
}

void rc_close(struct rc_dev *rdev)
{
	if (rdev) {
		mutex_lock(&rdev->lock);

862
		if (!--rdev->users && rdev->close != NULL)
863 864 865 866
			rdev->close(rdev);

		mutex_unlock(&rdev->lock);
	}
867
}
868
EXPORT_SYMBOL_GPL(rc_close);
869

870
static void ir_close(struct input_dev *idev)
871
{
872
	struct rc_dev *rdev = input_get_drvdata(idev);
873
	rc_close(rdev);
874 875
}

876
/* class for /sys/class/rc */
877
static char *rc_devnode(struct device *dev, umode_t *mode)
878 879 880 881
{
	return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
}

882
static struct class rc_class = {
883
	.name		= "rc",
884
	.devnode	= rc_devnode,
885 886
};

887 888 889 890 891
/*
 * These are the protocol textual descriptions that are
 * used by the sysfs protocols file. Note that the order
 * of the entries is relevant.
 */
892
static const struct {
893
	u64	type;
894
	const char	*name;
895
	const char	*module_name;
896
} proto_names[] = {
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	{ RC_PROTO_BIT_NONE,	"none",		NULL			},
	{ RC_PROTO_BIT_OTHER,	"other",	NULL			},
	{ RC_PROTO_BIT_UNKNOWN,	"unknown",	NULL			},
	{ RC_PROTO_BIT_RC5 |
	  RC_PROTO_BIT_RC5X_20,	"rc-5",		"ir-rc5-decoder"	},
	{ RC_PROTO_BIT_NEC |
	  RC_PROTO_BIT_NECX |
	  RC_PROTO_BIT_NEC32,	"nec",		"ir-nec-decoder"	},
	{ RC_PROTO_BIT_RC6_0 |
	  RC_PROTO_BIT_RC6_6A_20 |
	  RC_PROTO_BIT_RC6_6A_24 |
	  RC_PROTO_BIT_RC6_6A_32 |
	  RC_PROTO_BIT_RC6_MCE,	"rc-6",		"ir-rc6-decoder"	},
	{ RC_PROTO_BIT_JVC,	"jvc",		"ir-jvc-decoder"	},
	{ RC_PROTO_BIT_SONY12 |
	  RC_PROTO_BIT_SONY15 |
	  RC_PROTO_BIT_SONY20,	"sony",		"ir-sony-decoder"	},
	{ RC_PROTO_BIT_RC5_SZ,	"rc-5-sz",	"ir-rc5-decoder"	},
	{ RC_PROTO_BIT_SANYO,	"sanyo",	"ir-sanyo-decoder"	},
	{ RC_PROTO_BIT_SHARP,	"sharp",	"ir-sharp-decoder"	},
	{ RC_PROTO_BIT_MCIR2_KBD |
	  RC_PROTO_BIT_MCIR2_MSE, "mce_kbd",	"ir-mce_kbd-decoder"	},
	{ RC_PROTO_BIT_XMP,	"xmp",		"ir-xmp-decoder"	},
	{ RC_PROTO_BIT_CEC,	"cec",		NULL			},
921 922 923
};

/**
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
 * struct rc_filter_attribute - Device attribute relating to a filter type.
 * @attr:	Device attribute.
 * @type:	Filter type.
 * @mask:	false for filter value, true for filter mask.
 */
struct rc_filter_attribute {
	struct device_attribute		attr;
	enum rc_filter_type		type;
	bool				mask;
};
#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)

#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask)	\
	struct rc_filter_attribute dev_attr_##_name = {			\
		.attr = __ATTR(_name, _mode, _show, _store),		\
		.type = (_type),					\
		.mask = (_mask),					\
	}

943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
static bool lirc_is_present(void)
{
#if defined(CONFIG_LIRC_MODULE)
	struct module *lirc;

	mutex_lock(&module_mutex);
	lirc = find_module("lirc_dev");
	mutex_unlock(&module_mutex);

	return lirc ? true : false;
#elif defined(CONFIG_LIRC)
	return true;
#else
	return false;
#endif
}

960
/**
961
 * show_protocols() - shows the current IR protocol(s)
962
 * @device:	the device descriptor
963
 * @mattr:	the device attribute struct
964 965 966
 * @buf:	a pointer to the output buffer
 *
 * This routine is a callback routine for input read the IR protocol type(s).
967
 * it is trigged by reading /sys/class/rc/rc?/protocols.
968 969
 * It returns the protocol names of supported protocols.
 * Enabled protocols are printed in brackets.
970
 *
971 972
 * dev->lock is taken to guard against races between
 * store_protocols and show_protocols.
973
 */
974
static ssize_t show_protocols(struct device *device,
975 976
			      struct device_attribute *mattr, char *buf)
{
977
	struct rc_dev *dev = to_rc_dev(device);
978 979 980 981
	u64 allowed, enabled;
	char *tmp = buf;
	int i;

982 983
	mutex_lock(&dev->lock);

984 985 986 987
	enabled = dev->enabled_protocols;
	allowed = dev->allowed_protocols;
	if (dev->raw && !allowed)
		allowed = ir_raw_get_allowed_protocols();
988

989 990 991 992
	mutex_unlock(&dev->lock);

	IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
		   __func__, (long long)allowed, (long long)enabled);
993 994 995 996 997 998

	for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
		if (allowed & enabled & proto_names[i].type)
			tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
		else if (allowed & proto_names[i].type)
			tmp += sprintf(tmp, "%s ", proto_names[i].name);
999 1000 1001

		if (allowed & proto_names[i].type)
			allowed &= ~proto_names[i].type;
1002 1003
	}

1004
	if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
1005 1006
		tmp += sprintf(tmp, "[lirc] ");

1007 1008 1009
	if (tmp != buf)
		tmp--;
	*tmp = '\n';
1010

1011 1012 1013 1014
	return tmp + 1 - buf;
}

/**
1015 1016 1017
 * parse_protocol_change() - parses a protocol change request
 * @protocols:	pointer to the bitmask of current protocols
 * @buf:	pointer to the buffer with a list of changes
1018
 *
1019 1020
 * Writing "+proto" will add a protocol to the protocol mask.
 * Writing "-proto" will remove a protocol from protocol mask.
1021 1022
 * Writing "proto" will enable only "proto".
 * Writing "none" will disable all protocols.
1023
 * Returns the number of changes performed or a negative error code.
1024
 */
1025
static int parse_protocol_change(u64 *protocols, const char *buf)
1026 1027
{
	const char *tmp;
1028 1029
	unsigned count = 0;
	bool enable, disable;
1030
	u64 mask;
1031
	int i;
1032

1033
	while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
		if (!*tmp)
			break;

		if (*tmp == '+') {
			enable = true;
			disable = false;
			tmp++;
		} else if (*tmp == '-') {
			enable = false;
			disable = true;
			tmp++;
		} else {
			enable = false;
			disable = false;
		}

1050 1051 1052 1053
		for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
			if (!strcasecmp(tmp, proto_names[i].name)) {
				mask = proto_names[i].type;
				break;
1054 1055 1056
			}
		}

1057
		if (i == ARRAY_SIZE(proto_names)) {
1058 1059 1060 1061 1062 1063
			if (!strcasecmp(tmp, "lirc"))
				mask = 0;
			else {
				IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
				return -EINVAL;
			}
1064 1065 1066 1067
		}

		count++;

1068
		if (enable)
1069
			*protocols |= mask;
1070
		else if (disable)
1071
			*protocols &= ~mask;
1072
		else
1073
			*protocols = mask;
1074 1075 1076 1077
	}

	if (!count) {
		IR_dprintk(1, "Protocol not specified\n");
1078 1079 1080 1081 1082 1083
		return -EINVAL;
	}

	return count;
}

1084 1085 1086 1087 1088 1089
static void ir_raw_load_modules(u64 *protocols)
{
	u64 available;
	int i, ret;

	for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1090 1091 1092
		if (proto_names[i].type == RC_PROTO_BIT_NONE ||
		    proto_names[i].type & (RC_PROTO_BIT_OTHER |
					   RC_PROTO_BIT_UNKNOWN))
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
			continue;

		available = ir_raw_get_allowed_protocols();
		if (!(*protocols & proto_names[i].type & ~available))
			continue;

		if (!proto_names[i].module_name) {
			pr_err("Can't enable IR protocol %s\n",
			       proto_names[i].name);
			*protocols &= ~proto_names[i].type;
			continue;
		}

		ret = request_module("%s", proto_names[i].module_name);
		if (ret < 0) {
			pr_err("Couldn't load IR protocol module %s\n",
			       proto_names[i].module_name);
			*protocols &= ~proto_names[i].type;
			continue;
		}
		msleep(20);
		available = ir_raw_get_allowed_protocols();
		if (!(*protocols & proto_names[i].type & ~available))
			continue;

1118
		pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
1119 1120 1121 1122 1123 1124
		       proto_names[i].module_name,
		       proto_names[i].name);
		*protocols &= ~proto_names[i].type;
	}
}

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
/**
 * store_protocols() - changes the current/wakeup IR protocol(s)
 * @device:	the device descriptor
 * @mattr:	the device attribute struct
 * @buf:	a pointer to the input buffer
 * @len:	length of the input buffer
 *
 * This routine is for changing the IR protocol type.
 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
 * See parse_protocol_change() for the valid commands.
 * Returns @len on success or a negative error code.
 *
1137 1138
 * dev->lock is taken to guard against races between
 * store_protocols and show_protocols.
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
 */
static ssize_t store_protocols(struct device *device,
			       struct device_attribute *mattr,
			       const char *buf, size_t len)
{
	struct rc_dev *dev = to_rc_dev(device);
	u64 *current_protocols;
	struct rc_scancode_filter *filter;
	u64 old_protocols, new_protocols;
	ssize_t rc;

1150 1151 1152
	IR_dprintk(1, "Normal protocol change requested\n");
	current_protocols = &dev->enabled_protocols;
	filter = &dev->scancode_filter;
1153

1154
	if (!dev->change_protocol) {
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
		IR_dprintk(1, "Protocol switching not supported\n");
		return -EINVAL;
	}

	mutex_lock(&dev->lock);

	old_protocols = *current_protocols;
	new_protocols = old_protocols;
	rc = parse_protocol_change(&new_protocols, buf);
	if (rc < 0)
		goto out;

1167
	rc = dev->change_protocol(dev, &new_protocols);
1168 1169 1170
	if (rc < 0) {
		IR_dprintk(1, "Error setting protocols to 0x%llx\n",
			   (long long)new_protocols);
1171
		goto out;
1172 1173
	}

1174 1175 1176
	if (dev->driver_type == RC_DRIVER_IR_RAW)
		ir_raw_load_modules(&new_protocols);

1177 1178 1179 1180
	if (new_protocols != old_protocols) {
		*current_protocols = new_protocols;
		IR_dprintk(1, "Protocols changed to 0x%llx\n",
			   (long long)new_protocols);
1181 1182
	}

1183
	/*
1184 1185 1186
	 * If a protocol change was attempted the filter may need updating, even
	 * if the actual protocol mask hasn't changed (since the driver may have
	 * cleared the filter).
1187 1188 1189
	 * Try setting the same filter with the new protocol (if any).
	 * Fall back to clearing the filter.
	 */
1190
	if (dev->s_filter && filter->mask) {
1191
		if (new_protocols)
1192
			rc = dev->s_filter(dev, filter);
1193 1194
		else
			rc = -1;
1195

1196 1197 1198
		if (rc < 0) {
			filter->data = 0;
			filter->mask = 0;
1199
			dev->s_filter(dev, filter);
1200
		}
1201 1202
	}

1203
	rc = len;
1204 1205 1206

out:
	mutex_unlock(&dev->lock);
1207
	return rc;
1208 1209
}

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
/**
 * show_filter() - shows the current scancode filter value or mask
 * @device:	the device descriptor
 * @attr:	the device attribute struct
 * @buf:	a pointer to the output buffer
 *
 * This routine is a callback routine to read a scancode filter value or mask.
 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
 * It prints the current scancode filter value or mask of the appropriate filter
 * type in hexadecimal into @buf and returns the size of the buffer.
 *
 * Bits of the filter value corresponding to set bits in the filter mask are
 * compared against input scancodes and non-matching scancodes are discarded.
 *
1224
 * dev->lock is taken to guard against races between
1225 1226 1227 1228 1229 1230 1231 1232
 * store_filter and show_filter.
 */
static ssize_t show_filter(struct device *device,
			   struct device_attribute *attr,
			   char *buf)
{
	struct rc_dev *dev = to_rc_dev(device);
	struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
1233
	struct rc_scancode_filter *filter;
1234 1235
	u32 val;

1236 1237
	mutex_lock(&dev->lock);

1238
	if (fattr->type == RC_FILTER_NORMAL)
1239
		filter = &dev->scancode_filter;
1240
	else
1241
		filter = &dev->scancode_wakeup_filter;
1242 1243 1244

	if (fattr->mask)
		val = filter->mask;
1245
	else
1246
		val = filter->data;
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	mutex_unlock(&dev->lock);

	return sprintf(buf, "%#x\n", val);
}

/**
 * store_filter() - changes the scancode filter value
 * @device:	the device descriptor
 * @attr:	the device attribute struct
 * @buf:	a pointer to the input buffer
 * @len:	length of the input buffer
 *
 * This routine is for changing a scancode filter value or mask.
 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
 * Returns -EINVAL if an invalid filter value for the current protocol was
 * specified or if scancode filtering is not supported by the driver, otherwise
 * returns @len.
 *
 * Bits of the filter value corresponding to set bits in the filter mask are
 * compared against input scancodes and non-matching scancodes are discarded.
 *
1268
 * dev->lock is taken to guard against races between
1269 1270 1271 1272
 * store_filter and show_filter.
 */
static ssize_t store_filter(struct device *device,
			    struct device_attribute *attr,
1273
			    const char *buf, size_t len)
1274 1275 1276
{
	struct rc_dev *dev = to_rc_dev(device);
	struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
1277
	struct rc_scancode_filter new_filter, *filter;
1278 1279
	int ret;
	unsigned long val;
1280
	int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
1281 1282 1283 1284 1285

	ret = kstrtoul(buf, 0, &val);
	if (ret < 0)
		return ret;

1286 1287
	if (fattr->type == RC_FILTER_NORMAL) {
		set_filter = dev->s_filter;
1288
		filter = &dev->scancode_filter;
1289 1290
	} else {
		set_filter = dev->s_wakeup_filter;
1291
		filter = &dev->scancode_wakeup_filter;
1292 1293
	}

1294 1295
	if (!set_filter)
		return -EINVAL;
1296 1297 1298

	mutex_lock(&dev->lock);

1299
	new_filter = *filter;
1300
	if (fattr->mask)
1301
		new_filter.mask = val;
1302
	else
1303
		new_filter.data = val;
1304

1305
	if (fattr->type == RC_FILTER_WAKEUP) {
S
Sean Young 已提交
1306 1307 1308 1309
		/*
		 * Refuse to set a filter unless a protocol is enabled
		 * and the filter is valid for that protocol
		 */
1310
		if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
1311
			ret = rc_validate_filter(dev, &new_filter);
S
Sean Young 已提交
1312
		else
1313
			ret = -EINVAL;
S
Sean Young 已提交
1314 1315

		if (ret != 0)
1316 1317 1318 1319 1320
			goto unlock;
	}

	if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
	    val) {
1321 1322 1323 1324
		/* refuse to set a filter unless a protocol is enabled */
		ret = -EINVAL;
		goto unlock;
	}
1325

1326
	ret = set_filter(dev, &new_filter);
1327 1328
	if (ret < 0)
		goto unlock;
1329

1330
	*filter = new_filter;
1331 1332 1333

unlock:
	mutex_unlock(&dev->lock);
1334
	return (ret < 0) ? ret : len;
1335 1336
}

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
/**
 * show_wakeup_protocols() - shows the wakeup IR protocol
 * @device:	the device descriptor
 * @mattr:	the device attribute struct
 * @buf:	a pointer to the output buffer
 *
 * This routine is a callback routine for input read the IR protocol type(s).
 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
 * It returns the protocol names of supported protocols.
 * The enabled protocols are printed in brackets.
 *
1348 1349
 * dev->lock is taken to guard against races between
 * store_wakeup_protocols and show_wakeup_protocols.
1350 1351 1352 1353 1354 1355 1356
 */
static ssize_t show_wakeup_protocols(struct device *device,
				     struct device_attribute *mattr,
				     char *buf)
{
	struct rc_dev *dev = to_rc_dev(device);
	u64 allowed;
1357
	enum rc_proto enabled;
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
	char *tmp = buf;
	int i;

	mutex_lock(&dev->lock);

	allowed = dev->allowed_wakeup_protocols;
	enabled = dev->wakeup_protocol;

	mutex_unlock(&dev->lock);

	IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
		   __func__, (long long)allowed, enabled);

1371
	for (i = 0; i < ARRAY_SIZE(protocols); i++) {
1372 1373
		if (allowed & (1ULL << i)) {
			if (i == enabled)
1374
				tmp += sprintf(tmp, "[%s] ", protocols[i].name);
1375
			else
1376
				tmp += sprintf(tmp, "%s ", protocols[i].name);
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
		}
	}

	if (tmp != buf)
		tmp--;
	*tmp = '\n';

	return tmp + 1 - buf;
}

/**
 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
 * @device:	the device descriptor
 * @mattr:	the device attribute struct
 * @buf:	a pointer to the input buffer
 * @len:	length of the input buffer
 *
 * This routine is for changing the IR protocol type.
 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
 * Returns @len on success or a negative error code.
 *
1398 1399
 * dev->lock is taken to guard against races between
 * store_wakeup_protocols and show_wakeup_protocols.
1400 1401 1402 1403 1404 1405
 */
static ssize_t store_wakeup_protocols(struct device *device,
				      struct device_attribute *mattr,
				      const char *buf, size_t len)
{
	struct rc_dev *dev = to_rc_dev(device);
1406
	enum rc_proto protocol;
1407 1408 1409 1410 1411 1412 1413 1414 1415
	ssize_t rc;
	u64 allowed;
	int i;

	mutex_lock(&dev->lock);

	allowed = dev->allowed_wakeup_protocols;

	if (sysfs_streq(buf, "none")) {
1416
		protocol = RC_PROTO_UNKNOWN;
1417
	} else {
1418
		for (i = 0; i < ARRAY_SIZE(protocols); i++) {
1419
			if ((allowed & (1ULL << i)) &&
1420
			    sysfs_streq(buf, protocols[i].name)) {
1421 1422 1423 1424 1425
				protocol = i;
				break;
			}
		}

1426
		if (i == ARRAY_SIZE(protocols)) {
1427 1428 1429
			rc = -EINVAL;
			goto out;
		}
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439

		if (dev->encode_wakeup) {
			u64 mask = 1ULL << protocol;

			ir_raw_load_modules(&mask);
			if (!mask) {
				rc = -EINVAL;
				goto out;
			}
		}
1440 1441 1442 1443 1444 1445
	}

	if (dev->wakeup_protocol != protocol) {
		dev->wakeup_protocol = protocol;
		IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);

1446
		if (protocol == RC_PROTO_RC6_MCE)
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
			dev->scancode_wakeup_filter.data = 0x800f0000;
		else
			dev->scancode_wakeup_filter.data = 0;
		dev->scancode_wakeup_filter.mask = 0;

		rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
		if (rc == 0)
			rc = len;
	} else {
		rc = len;
	}

out:
	mutex_unlock(&dev->lock);
	return rc;
}

1464 1465
static void rc_dev_release(struct device *device)
{
1466 1467 1468
	struct rc_dev *dev = to_rc_dev(device);

	kfree(dev);
1469 1470
}

1471 1472 1473 1474 1475 1476 1477 1478 1479
#define ADD_HOTPLUG_VAR(fmt, val...)					\
	do {								\
		int err = add_uevent_var(env, fmt, val);		\
		if (err)						\
			return err;					\
	} while (0)

static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
{
1480
	struct rc_dev *dev = to_rc_dev(device);
1481

1482 1483
	if (dev->rc_map.name)
		ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
1484 1485
	if (dev->driver_name)
		ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
1486 1487
	if (dev->device_name)
		ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
1488 1489 1490 1491 1492 1493 1494

	return 0;
}

/*
 * Static device attribute struct with the sysfs attributes for IR's
 */
1495 1496 1497 1498
static struct device_attribute dev_attr_ro_protocols =
__ATTR(protocols, 0444, show_protocols, NULL);
static struct device_attribute dev_attr_rw_protocols =
__ATTR(protocols, 0644, show_protocols, store_protocols);
1499 1500
static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
		   store_wakeup_protocols);
1501 1502 1503 1504 1505 1506 1507 1508
static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
		      show_filter, store_filter, RC_FILTER_NORMAL, false);
static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
		      show_filter, store_filter, RC_FILTER_NORMAL, true);
static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
		      show_filter, store_filter, RC_FILTER_WAKEUP, false);
static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
		      show_filter, store_filter, RC_FILTER_WAKEUP, true);
1509

1510 1511
static struct attribute *rc_dev_rw_protocol_attrs[] = {
	&dev_attr_rw_protocols.attr,
1512 1513 1514
	NULL,
};

1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
	.attrs	= rc_dev_rw_protocol_attrs,
};

static struct attribute *rc_dev_ro_protocol_attrs[] = {
	&dev_attr_ro_protocols.attr,
	NULL,
};

static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
	.attrs	= rc_dev_ro_protocol_attrs,
1526 1527 1528
};

static struct attribute *rc_dev_filter_attrs[] = {
1529 1530
	&dev_attr_filter.attr.attr,
	&dev_attr_filter_mask.attr.attr,
1531 1532 1533
	NULL,
};

1534
static const struct attribute_group rc_dev_filter_attr_grp = {
1535
	.attrs	= rc_dev_filter_attrs,
1536 1537
};

1538 1539 1540
static struct attribute *rc_dev_wakeup_filter_attrs[] = {
	&dev_attr_wakeup_filter.attr.attr,
	&dev_attr_wakeup_filter_mask.attr.attr,
1541
	&dev_attr_wakeup_protocols.attr,
1542 1543 1544
	NULL,
};

1545
static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
1546
	.attrs	= rc_dev_wakeup_filter_attrs,
1547 1548
};

1549
static const struct device_type rc_dev_type = {
1550
	.release	= rc_dev_release,
1551 1552 1553
	.uevent		= rc_dev_uevent,
};

1554
struct rc_dev *rc_allocate_device(enum rc_driver_type type)
1555
{
1556
	struct rc_dev *dev;
1557

1558 1559 1560 1561
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return NULL;

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
	if (type != RC_DRIVER_IR_RAW_TX) {
		dev->input_dev = input_allocate_device();
		if (!dev->input_dev) {
			kfree(dev);
			return NULL;
		}

		dev->input_dev->getkeycode = ir_getkeycode;
		dev->input_dev->setkeycode = ir_setkeycode;
		input_set_drvdata(dev->input_dev, dev);
1572

1573
		timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
1574

1575 1576 1577
		spin_lock_init(&dev->rc_map.lock);
		spin_lock_init(&dev->keylock);
	}
1578
	mutex_init(&dev->lock);
1579

1580
	dev->dev.type = &rc_dev_type;
1581
	dev->dev.class = &rc_class;
1582 1583
	device_initialize(&dev->dev);

1584 1585
	dev->driver_type = type;

1586 1587 1588 1589 1590 1591
	__module_get(THIS_MODULE);
	return dev;
}
EXPORT_SYMBOL_GPL(rc_allocate_device);

void rc_free_device(struct rc_dev *dev)
1592
{
1593 1594 1595
	if (!dev)
		return;

1596
	input_free_device(dev->input_dev);
1597 1598 1599

	put_device(&dev->dev);

1600 1601 1602
	/* kfree(dev) will be called by the callback function
	   rc_dev_release() */

1603
	module_put(THIS_MODULE);
1604 1605 1606
}
EXPORT_SYMBOL_GPL(rc_free_device);

1607 1608 1609 1610 1611
static void devm_rc_alloc_release(struct device *dev, void *res)
{
	rc_free_device(*(struct rc_dev **)res);
}

1612 1613
struct rc_dev *devm_rc_allocate_device(struct device *dev,
				       enum rc_driver_type type)
1614 1615 1616 1617 1618 1619 1620
{
	struct rc_dev **dr, *rc;

	dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
	if (!dr)
		return NULL;

1621
	rc = rc_allocate_device(type);
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
	if (!rc) {
		devres_free(dr);
		return NULL;
	}

	rc->dev.parent = dev;
	rc->managed_alloc = true;
	*dr = rc;
	devres_add(dev, dr);

	return rc;
}
EXPORT_SYMBOL_GPL(devm_rc_allocate_device);

1636
static int rc_prepare_rx_device(struct rc_dev *dev)
1637
{
1638
	int rc;
1639
	struct rc_map *rc_map;
1640
	u64 rc_proto;
1641

1642
	if (!dev->map_name)
1643
		return -EINVAL;
1644

1645
	rc_map = rc_map_get(dev->map_name);
1646
	if (!rc_map)
1647
		rc_map = rc_map_get(RC_MAP_EMPTY);
1648
	if (!rc_map || !rc_map->scan || rc_map->size == 0)
1649 1650
		return -EINVAL;

1651 1652 1653 1654
	rc = ir_setkeytable(dev, rc_map);
	if (rc)
		return rc;

1655
	rc_proto = BIT_ULL(rc_map->rc_proto);
1656

1657 1658 1659
	if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
		dev->enabled_protocols = dev->allowed_protocols;

1660
	if (dev->change_protocol) {
1661
		rc = dev->change_protocol(dev, &rc_proto);
1662 1663
		if (rc < 0)
			goto out_table;
1664
		dev->enabled_protocols = rc_proto;
1665 1666
	}

1667
	if (dev->driver_type == RC_DRIVER_IR_RAW)
1668
		ir_raw_load_modules(&rc_proto);
1669

1670 1671 1672 1673 1674 1675 1676 1677 1678
	set_bit(EV_KEY, dev->input_dev->evbit);
	set_bit(EV_REP, dev->input_dev->evbit);
	set_bit(EV_MSC, dev->input_dev->evbit);
	set_bit(MSC_SCAN, dev->input_dev->mscbit);
	if (dev->open)
		dev->input_dev->open = ir_open;
	if (dev->close)
		dev->input_dev->close = ir_close;

1679 1680 1681
	dev->input_dev->dev.parent = &dev->dev;
	memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
	dev->input_dev->phys = dev->input_phys;
1682
	dev->input_dev->name = dev->device_name;
1683

1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
	return 0;

out_table:
	ir_free_table(&dev->rc_map);

	return rc;
}

static int rc_setup_rx_device(struct rc_dev *dev)
{
	int rc;

1696 1697 1698
	/* rc_open will be called here */
	rc = input_register_device(dev->input_dev);
	if (rc)
1699
		return rc;
1700

1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
	/*
	 * Default delay of 250ms is too short for some protocols, especially
	 * since the timeout is currently set to 250ms. Increase it to 500ms,
	 * to avoid wrong repetition of the keycodes. Note that this must be
	 * set after the call to input_register_device().
	 */
	dev->input_dev->rep[REP_DELAY] = 500;

	/*
	 * As a repeat event on protocols like RC-5 and NEC take as long as
	 * 110/114ms, using 33ms as a repeat period is not the right thing
	 * to do.
	 */
	dev->input_dev->rep[REP_PERIOD] = 125;

	return 0;
}

static void rc_free_rx_device(struct rc_dev *dev)
{
1721
	if (!dev)
1722 1723
		return;

1724 1725 1726 1727
	if (dev->input_dev) {
		input_unregister_device(dev->input_dev);
		dev->input_dev = NULL;
	}
1728

1729
	ir_free_table(&dev->rc_map);
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
}

int rc_register_device(struct rc_dev *dev)
{
	const char *path;
	int attr = 0;
	int minor;
	int rc;

	if (!dev)
		return -EINVAL;

1742 1743 1744 1745 1746 1747 1748
	minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
	if (minor < 0)
		return minor;

	dev->minor = minor;
	dev_set_name(&dev->dev, "rc%u", dev->minor);
	dev_set_drvdata(&dev->dev, dev);
1749

1750
	dev->dev.groups = dev->sysfs_groups;
1751 1752 1753 1754
	if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
		dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
	else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
		dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
1755
	if (dev->s_filter)
1756
		dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
1757 1758 1759 1760
	if (dev->s_wakeup_filter)
		dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
	dev->sysfs_groups[attr++] = NULL;

1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
	if (dev->driver_type == RC_DRIVER_IR_RAW ||
	    dev->driver_type == RC_DRIVER_IR_RAW_TX) {
		rc = ir_raw_event_prepare(dev);
		if (rc < 0)
			goto out_minor;
	}

	if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
		rc = rc_prepare_rx_device(dev);
		if (rc)
			goto out_raw;
	}

1774 1775
	rc = device_add(&dev->dev);
	if (rc)
1776
		goto out_rx_free;
1777

1778
	path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1779
	dev_info(&dev->dev, "%s as %s\n",
1780
		 dev->device_name ?: "Unspecified device", path ?: "N/A");
1781 1782
	kfree(path);

1783 1784 1785 1786 1787 1788
	if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
		rc = rc_setup_rx_device(dev);
		if (rc)
			goto out_dev;
	}

1789 1790
	if (dev->driver_type == RC_DRIVER_IR_RAW ||
	    dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1791 1792
		rc = ir_raw_event_register(dev);
		if (rc < 0)
1793
			goto out_rx;
1794 1795
	}

1796
	IR_dprintk(1, "Registered rc%u (driver: %s)\n",
1797
		   dev->minor,
1798
		   dev->driver_name ? dev->driver_name : "unknown");
1799

1800
	return 0;
1801

1802 1803
out_rx:
	rc_free_rx_device(dev);
1804 1805
out_dev:
	device_del(&dev->dev);
1806 1807 1808 1809 1810
out_rx_free:
	ir_free_table(&dev->rc_map);
out_raw:
	ir_raw_event_free(dev);
out_minor:
1811
	ida_simple_remove(&rc_ida, minor);
1812
	return rc;
1813
}
1814
EXPORT_SYMBOL_GPL(rc_register_device);
1815

1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
static void devm_rc_release(struct device *dev, void *res)
{
	rc_unregister_device(*(struct rc_dev **)res);
}

int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
{
	struct rc_dev **dr;
	int ret;

	dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
	if (!dr)
		return -ENOMEM;

	ret = rc_register_device(dev);
	if (ret) {
		devres_free(dr);
		return ret;
	}

	*dr = dev;
	devres_add(parent, dr);

	return 0;
}
EXPORT_SYMBOL_GPL(devm_rc_register_device);

1843
void rc_unregister_device(struct rc_dev *dev)
1844
{
1845 1846
	if (!dev)
		return;
1847

1848
	del_timer_sync(&dev->timer_keyup);
1849

1850 1851 1852
	if (dev->driver_type == RC_DRIVER_IR_RAW)
		ir_raw_event_unregister(dev);

1853
	rc_free_rx_device(dev);
1854

1855
	device_del(&dev->dev);
1856

1857 1858
	ida_simple_remove(&rc_ida, dev->minor);

1859 1860
	if (!dev->managed_alloc)
		rc_free_device(dev);
1861
}
1862

1863
EXPORT_SYMBOL_GPL(rc_unregister_device);
1864 1865 1866 1867 1868

/*
 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
 */

1869
static int __init rc_core_init(void)
1870
{
1871
	int rc = class_register(&rc_class);
1872
	if (rc) {
1873
		pr_err("rc_core: unable to register rc class\n");
1874 1875 1876
		return rc;
	}

1877
	led_trigger_register_simple("rc-feedback", &led_feedback);
1878
	rc_map_register(&empty_map);
1879 1880 1881 1882

	return 0;
}

1883
static void __exit rc_core_exit(void)
1884
{
1885
	class_unregister(&rc_class);
1886
	led_trigger_unregister_simple(led_feedback);
1887
	rc_map_unregister(&empty_map);
1888 1889
}

1890
subsys_initcall(rc_core_init);
1891
module_exit(rc_core_exit);
1892

1893 1894 1895
int rc_core_debug;    /* ir_debug level (0,1,2) */
EXPORT_SYMBOL_GPL(rc_core_debug);
module_param_named(debug, rc_core_debug, int, 0644);
1896

1897
MODULE_AUTHOR("Mauro Carvalho Chehab");
1898
MODULE_LICENSE("GPL");