• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/media/IR/
1/* ir-keytable.c - handle IR scancode->keycode tables
2 *
3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 *  it under the terms of the GNU General Public License as published by
7 *  the Free Software Foundation version 2 of the License.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *  GNU General Public License for more details.
13 */
14
15
16#include <linux/input.h>
17#include <linux/slab.h>
18#include "ir-core-priv.h"
19
20/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
21#define IR_TAB_MIN_SIZE	256
22#define IR_TAB_MAX_SIZE	8192
23
24#define IR_KEYPRESS_TIMEOUT 250
25
26/**
27 * ir_resize_table() - resizes a scancode table if necessary
28 * @rc_tab:	the ir_scancode_table to resize
29 * @return:	zero on success or a negative error code
30 *
31 * This routine will shrink the ir_scancode_table if it has lots of
32 * unused entries and grow it if it is full.
33 */
34static int ir_resize_table(struct ir_scancode_table *rc_tab)
35{
36	unsigned int oldalloc = rc_tab->alloc;
37	unsigned int newalloc = oldalloc;
38	struct ir_scancode *oldscan = rc_tab->scan;
39	struct ir_scancode *newscan;
40
41	if (rc_tab->size == rc_tab->len) {
42		/* All entries in use -> grow keytable */
43		if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
44			return -ENOMEM;
45
46		newalloc *= 2;
47		IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
48	}
49
50	if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
51		/* Less than 1/3 of entries in use -> shrink keytable */
52		newalloc /= 2;
53		IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
54	}
55
56	if (newalloc == oldalloc)
57		return 0;
58
59	newscan = kmalloc(newalloc, GFP_ATOMIC);
60	if (!newscan) {
61		IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
62		return -ENOMEM;
63	}
64
65	memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
66	rc_tab->scan = newscan;
67	rc_tab->alloc = newalloc;
68	rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
69	kfree(oldscan);
70	return 0;
71}
72
73/**
74 * ir_do_setkeycode() - internal function to set a keycode in the
75 *			scancode->keycode table
76 * @dev:	the struct input_dev device descriptor
77 * @rc_tab:	the struct ir_scancode_table to set the keycode in
78 * @scancode:	the scancode for the ir command
79 * @keycode:	the keycode for the ir command
80 * @resize:	whether the keytable may be shrunk
81 * @return:	-EINVAL if the keycode could not be inserted, otherwise zero.
82 *
83 * This routine is used internally to manipulate the scancode->keycode table.
84 * The caller has to hold @rc_tab->lock.
85 */
86static int ir_do_setkeycode(struct input_dev *dev,
87			    struct ir_scancode_table *rc_tab,
88			    unsigned scancode, unsigned keycode,
89			    bool resize)
90{
91	unsigned int i;
92	int old_keycode = KEY_RESERVED;
93	struct ir_input_dev *ir_dev = input_get_drvdata(dev);
94
95	/*
96	 * Unfortunately, some hardware-based IR decoders don't provide
97	 * all bits for the complete IR code. In general, they provide only
98	 * the command part of the IR code. Yet, as it is possible to replace
99	 * the provided IR with another one, it is needed to allow loading
100	 * IR tables from other remotes. So,
101	 */
102	if (ir_dev->props && ir_dev->props->scanmask) {
103		scancode &= ir_dev->props->scanmask;
104	}
105
106	/* First check if we already have a mapping for this ir command */
107	for (i = 0; i < rc_tab->len; i++) {
108		/* Keytable is sorted from lowest to highest scancode */
109		if (rc_tab->scan[i].scancode > scancode)
110			break;
111		else if (rc_tab->scan[i].scancode < scancode)
112			continue;
113
114		old_keycode = rc_tab->scan[i].keycode;
115		rc_tab->scan[i].keycode = keycode;
116
117		/* Did the user wish to remove the mapping? */
118		if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) {
119			IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
120				   i, scancode);
121			rc_tab->len--;
122			memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1],
123				(rc_tab->len - i) * sizeof(struct ir_scancode));
124		}
125
126		/* Possibly shrink the keytable, failure is not a problem */
127		ir_resize_table(rc_tab);
128		break;
129	}
130
131	if (old_keycode == KEY_RESERVED && keycode != KEY_RESERVED) {
132		/* No previous mapping found, we might need to grow the table */
133		if (resize && ir_resize_table(rc_tab))
134			return -ENOMEM;
135
136		IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
137			   i, scancode, keycode);
138
139		/* i is the proper index to insert our new keycode */
140		memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
141			(rc_tab->len - i) * sizeof(struct ir_scancode));
142		rc_tab->scan[i].scancode = scancode;
143		rc_tab->scan[i].keycode = keycode;
144		rc_tab->len++;
145		set_bit(keycode, dev->keybit);
146	} else {
147		IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
148			   i, scancode, keycode);
149		/* A previous mapping was updated... */
150		clear_bit(old_keycode, dev->keybit);
151		/* ...but another scancode might use the same keycode */
152		for (i = 0; i < rc_tab->len; i++) {
153			if (rc_tab->scan[i].keycode == old_keycode) {
154				set_bit(old_keycode, dev->keybit);
155				break;
156			}
157		}
158	}
159
160	return 0;
161}
162
163/**
164 * ir_setkeycode() - set a keycode in the scancode->keycode table
165 * @dev:	the struct input_dev device descriptor
166 * @scancode:	the desired scancode
167 * @keycode:	result
168 * @return:	-EINVAL if the keycode could not be inserted, otherwise zero.
169 *
170 * This routine is used to handle evdev EVIOCSKEY ioctl.
171 */
172static int ir_setkeycode(struct input_dev *dev,
173			 unsigned int scancode, unsigned int keycode)
174{
175	int rc;
176	unsigned long flags;
177	struct ir_input_dev *ir_dev = input_get_drvdata(dev);
178	struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
179
180	spin_lock_irqsave(&rc_tab->lock, flags);
181	rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode, true);
182	spin_unlock_irqrestore(&rc_tab->lock, flags);
183	return rc;
184}
185
186/**
187 * ir_setkeytable() - sets several entries in the scancode->keycode table
188 * @dev:	the struct input_dev device descriptor
189 * @to:		the struct ir_scancode_table to copy entries to
190 * @from:	the struct ir_scancode_table to copy entries from
191 * @return:	-EINVAL if all keycodes could not be inserted, otherwise zero.
192 *
193 * This routine is used to handle table initialization.
194 */
195static int ir_setkeytable(struct input_dev *dev,
196			  struct ir_scancode_table *to,
197			  const struct ir_scancode_table *from)
198{
199	struct ir_input_dev *ir_dev = input_get_drvdata(dev);
200	struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
201	unsigned long flags;
202	unsigned int i;
203	int rc = 0;
204
205	spin_lock_irqsave(&rc_tab->lock, flags);
206	for (i = 0; i < from->size; i++) {
207		rc = ir_do_setkeycode(dev, to, from->scan[i].scancode,
208				      from->scan[i].keycode, false);
209		if (rc)
210			break;
211	}
212	spin_unlock_irqrestore(&rc_tab->lock, flags);
213	return rc;
214}
215
216/**
217 * ir_getkeycode() - get a keycode from the scancode->keycode table
218 * @dev:	the struct input_dev device descriptor
219 * @scancode:	the desired scancode
220 * @keycode:	used to return the keycode, if found, or KEY_RESERVED
221 * @return:	always returns zero.
222 *
223 * This routine is used to handle evdev EVIOCGKEY ioctl.
224 */
225static int ir_getkeycode(struct input_dev *dev,
226			 unsigned int scancode, unsigned int *keycode)
227{
228	int start, end, mid;
229	unsigned long flags;
230	int key = KEY_RESERVED;
231	struct ir_input_dev *ir_dev = input_get_drvdata(dev);
232	struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
233
234	spin_lock_irqsave(&rc_tab->lock, flags);
235	start = 0;
236	end = rc_tab->len - 1;
237	while (start <= end) {
238		mid = (start + end) / 2;
239		if (rc_tab->scan[mid].scancode < scancode)
240			start = mid + 1;
241		else if (rc_tab->scan[mid].scancode > scancode)
242			end = mid - 1;
243		else {
244			key = rc_tab->scan[mid].keycode;
245			break;
246		}
247	}
248	spin_unlock_irqrestore(&rc_tab->lock, flags);
249
250	if (key == KEY_RESERVED)
251		IR_dprintk(1, "unknown key for scancode 0x%04x\n",
252			   scancode);
253
254	*keycode = key;
255	return 0;
256}
257
258/**
259 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
260 * @input_dev:	the struct input_dev descriptor of the device
261 * @scancode:	the scancode that we're seeking
262 *
263 * This routine is used by the input routines when a key is pressed at the
264 * IR. The scancode is received and needs to be converted into a keycode.
265 * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
266 * corresponding keycode from the table.
267 */
268u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
269{
270	int keycode;
271
272	ir_getkeycode(dev, scancode, &keycode);
273	if (keycode != KEY_RESERVED)
274		IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
275			   dev->name, scancode, keycode);
276	return keycode;
277}
278EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
279
280/**
281 * ir_keyup() - generates input event to cleanup a key press
282 * @ir:         the struct ir_input_dev descriptor of the device
283 *
284 * This routine is used to signal that a key has been released on the
285 * remote control. It reports a keyup input event via input_report_key().
286 */
287static void ir_keyup(struct ir_input_dev *ir)
288{
289	if (!ir->keypressed)
290		return;
291
292	IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
293	input_report_key(ir->input_dev, ir->last_keycode, 0);
294	input_sync(ir->input_dev);
295	ir->keypressed = false;
296}
297
298/**
299 * ir_timer_keyup() - generates a keyup event after a timeout
300 * @cookie:     a pointer to struct ir_input_dev passed to setup_timer()
301 *
302 * This routine will generate a keyup event some time after a keydown event
303 * is generated when no further activity has been detected.
304 */
305static void ir_timer_keyup(unsigned long cookie)
306{
307	struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
308	unsigned long flags;
309
310	/*
311	 * ir->keyup_jiffies is used to prevent a race condition if a
312	 * hardware interrupt occurs at this point and the keyup timer
313	 * event is moved further into the future as a result.
314	 *
315	 * The timer will then be reactivated and this function called
316	 * again in the future. We need to exit gracefully in that case
317	 * to allow the input subsystem to do its auto-repeat magic or
318	 * a keyup event might follow immediately after the keydown.
319	 */
320	spin_lock_irqsave(&ir->keylock, flags);
321	if (time_is_before_eq_jiffies(ir->keyup_jiffies))
322		ir_keyup(ir);
323	spin_unlock_irqrestore(&ir->keylock, flags);
324}
325
326/**
327 * ir_repeat() - notifies the IR core that a key is still pressed
328 * @dev:        the struct input_dev descriptor of the device
329 *
330 * This routine is used by IR decoders when a repeat message which does
331 * not include the necessary bits to reproduce the scancode has been
332 * received.
333 */
334void ir_repeat(struct input_dev *dev)
335{
336	unsigned long flags;
337	struct ir_input_dev *ir = input_get_drvdata(dev);
338
339	spin_lock_irqsave(&ir->keylock, flags);
340
341	input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
342
343	if (!ir->keypressed)
344		goto out;
345
346	ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
347	mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
348
349out:
350	spin_unlock_irqrestore(&ir->keylock, flags);
351}
352EXPORT_SYMBOL_GPL(ir_repeat);
353
354/**
355 * ir_keydown() - generates input event for a key press
356 * @dev:        the struct input_dev descriptor of the device
357 * @scancode:   the scancode that we're seeking
358 * @toggle:     the toggle value (protocol dependent, if the protocol doesn't
359 *              support toggle values, this should be set to zero)
360 *
361 * This routine is used by the input routines when a key is pressed at the
362 * IR. It gets the keycode for a scancode and reports an input event via
363 * input_report_key().
364 */
365void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
366{
367	unsigned long flags;
368	struct ir_input_dev *ir = input_get_drvdata(dev);
369
370	u32 keycode = ir_g_keycode_from_table(dev, scancode);
371
372	spin_lock_irqsave(&ir->keylock, flags);
373
374	input_event(dev, EV_MSC, MSC_SCAN, scancode);
375
376	/* Repeat event? */
377	if (ir->keypressed &&
378	    ir->last_scancode == scancode &&
379	    ir->last_toggle == toggle)
380		goto set_timer;
381
382	/* Release old keypress */
383	ir_keyup(ir);
384
385	ir->last_scancode = scancode;
386	ir->last_toggle = toggle;
387	ir->last_keycode = keycode;
388
389
390	if (keycode == KEY_RESERVED)
391		goto out;
392
393
394	/* Register a keypress */
395	ir->keypressed = true;
396	IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
397		   dev->name, keycode, scancode);
398	input_report_key(dev, ir->last_keycode, 1);
399	input_sync(dev);
400
401set_timer:
402	ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
403	mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
404out:
405	spin_unlock_irqrestore(&ir->keylock, flags);
406}
407EXPORT_SYMBOL_GPL(ir_keydown);
408
409static int ir_open(struct input_dev *input_dev)
410{
411	struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
412
413	return ir_dev->props->open(ir_dev->props->priv);
414}
415
416static void ir_close(struct input_dev *input_dev)
417{
418	struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
419
420	ir_dev->props->close(ir_dev->props->priv);
421}
422
423/**
424 * __ir_input_register() - sets the IR keycode table and add the handlers
425 *			    for keymap table get/set
426 * @input_dev:	the struct input_dev descriptor of the device
427 * @rc_tab:	the struct ir_scancode_table table of scancode/keymap
428 *
429 * This routine is used to initialize the input infrastructure
430 * to work with an IR.
431 * It will register the input/evdev interface for the device and
432 * register the syfs code for IR class
433 */
434int __ir_input_register(struct input_dev *input_dev,
435		      const struct ir_scancode_table *rc_tab,
436		      struct ir_dev_props *props,
437		      const char *driver_name)
438{
439	struct ir_input_dev *ir_dev;
440	int rc;
441
442	if (rc_tab->scan == NULL || !rc_tab->size)
443		return -EINVAL;
444
445	ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
446	if (!ir_dev)
447		return -ENOMEM;
448
449	ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
450	if (!ir_dev->driver_name) {
451		rc = -ENOMEM;
452		goto out_dev;
453	}
454
455	input_dev->getkeycode = ir_getkeycode;
456	input_dev->setkeycode = ir_setkeycode;
457	input_set_drvdata(input_dev, ir_dev);
458	ir_dev->input_dev = input_dev;
459
460	spin_lock_init(&ir_dev->rc_tab.lock);
461	spin_lock_init(&ir_dev->keylock);
462	setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
463
464	ir_dev->rc_tab.name = rc_tab->name;
465	ir_dev->rc_tab.ir_type = rc_tab->ir_type;
466	ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size *
467						  sizeof(struct ir_scancode));
468	ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL);
469	ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode);
470	if (props) {
471		ir_dev->props = props;
472		if (props->open)
473			input_dev->open = ir_open;
474		if (props->close)
475			input_dev->close = ir_close;
476	}
477
478	if (!ir_dev->rc_tab.scan) {
479		rc = -ENOMEM;
480		goto out_name;
481	}
482
483	IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
484		   ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
485
486	set_bit(EV_KEY, input_dev->evbit);
487	set_bit(EV_REP, input_dev->evbit);
488	set_bit(EV_MSC, input_dev->evbit);
489	set_bit(MSC_SCAN, input_dev->mscbit);
490
491	if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
492		rc = -ENOMEM;
493		goto out_table;
494	}
495
496	rc = ir_register_class(input_dev);
497	if (rc < 0)
498		goto out_table;
499
500	if (ir_dev->props)
501		if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
502			rc = ir_raw_event_register(input_dev);
503			if (rc < 0)
504				goto out_event;
505		}
506
507	IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
508		   driver_name, rc_tab->name,
509		   (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
510			" in raw mode" : "");
511
512	/*
513	 * Default delay of 250ms is too short for some protocols, expecially
514	 * since the timeout is currently set to 250ms. Increase it to 500ms,
515	 * to avoid wrong repetition of the keycodes.
516	 */
517	input_dev->rep[REP_DELAY] = 500;
518
519	return 0;
520
521out_event:
522	ir_unregister_class(input_dev);
523out_table:
524	kfree(ir_dev->rc_tab.scan);
525out_name:
526	kfree(ir_dev->driver_name);
527out_dev:
528	kfree(ir_dev);
529	return rc;
530}
531EXPORT_SYMBOL_GPL(__ir_input_register);
532
533/**
534 * ir_input_unregister() - unregisters IR and frees resources
535 * @input_dev:	the struct input_dev descriptor of the device
536
537 * This routine is used to free memory and de-register interfaces.
538 */
539void ir_input_unregister(struct input_dev *input_dev)
540{
541	struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
542	struct ir_scancode_table *rc_tab;
543
544	if (!ir_dev)
545		return;
546
547	IR_dprintk(1, "Freed keycode table\n");
548
549	del_timer_sync(&ir_dev->timer_keyup);
550	if (ir_dev->props)
551		if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
552			ir_raw_event_unregister(input_dev);
553
554	rc_tab = &ir_dev->rc_tab;
555	rc_tab->size = 0;
556	kfree(rc_tab->scan);
557	rc_tab->scan = NULL;
558
559	ir_unregister_class(input_dev);
560
561	kfree(ir_dev->driver_name);
562	kfree(ir_dev);
563}
564EXPORT_SYMBOL_GPL(ir_input_unregister);
565
566int ir_core_debug;    /* ir_debug level (0,1,2) */
567EXPORT_SYMBOL_GPL(ir_core_debug);
568module_param_named(debug, ir_core_debug, int, 0644);
569
570MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
571MODULE_LICENSE("GPL");
572