V4L/DVB: ir-core: add two functions to report keyup/keydown events
[safe/jmp/linux-2.6] / drivers / media / IR / ir-keytable.c
1 /* ir-register.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 <media/ir-common.h>
19
20 #define IR_TAB_MIN_SIZE 32
21 #define IR_TAB_MAX_SIZE 1024
22
23 /**
24  * ir_seek_table() - returns the element order on the table
25  * @rc_tab:     the ir_scancode_table with the keymap to be used
26  * @scancode:   the scancode that we're seeking
27  *
28  * This routine is used by the input routines when a key is pressed at the
29  * IR. The scancode is received and needs to be converted into a keycode.
30  * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
31  * corresponding keycode from the table.
32  */
33 static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode)
34 {
35         int rc;
36         unsigned long flags;
37         struct ir_scancode *keymap = rc_tab->scan;
38
39         spin_lock_irqsave(&rc_tab->lock, flags);
40
41         /* FIXME: replace it by a binary search */
42
43         for (rc = 0; rc < rc_tab->size; rc++)
44                 if (keymap[rc].scancode == scancode)
45                         goto exit;
46
47         /* Not found */
48         rc = -EINVAL;
49
50 exit:
51         spin_unlock_irqrestore(&rc_tab->lock, flags);
52         return rc;
53 }
54
55 /**
56  * ir_roundup_tablesize() - gets an optimum value for the table size
57  * @n_elems:            minimum number of entries to store keycodes
58  *
59  * This routine is used to choose the keycode table size.
60  *
61  * In order to have some empty space for new keycodes,
62  * and knowing in advance that kmalloc allocates only power of two
63  * segments, it optimizes the allocated space to have some spare space
64  * for those new keycodes by using the maximum number of entries that
65  * will be effectively be allocated by kmalloc.
66  * In order to reduce the quantity of table resizes, it has a minimum
67  * table size of IR_TAB_MIN_SIZE.
68  */
69 static int ir_roundup_tablesize(int n_elems)
70 {
71         size_t size;
72
73         if (n_elems < IR_TAB_MIN_SIZE)
74                 n_elems = IR_TAB_MIN_SIZE;
75
76         /*
77          * As kmalloc only allocates sizes of power of two, get as
78          * much entries as possible for the allocated memory segment
79          */
80         size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
81         n_elems = size / sizeof(struct ir_scancode);
82
83         return n_elems;
84 }
85
86 /**
87  * ir_copy_table() - copies a keytable, discarding the unused entries
88  * @destin:     destin table
89  * @origin:     origin table
90  *
91  * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
92  * Also copies table size and table protocol.
93  * NOTE: It shouldn't copy the lock field
94  */
95
96 static int ir_copy_table(struct ir_scancode_table *destin,
97                  const struct ir_scancode_table *origin)
98 {
99         int i, j = 0;
100
101         for (i = 0; i < origin->size; i++) {
102                 if (origin->scan[i].keycode == KEY_UNKNOWN ||
103                    origin->scan[i].keycode == KEY_RESERVED)
104                         continue;
105
106                 memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
107                 j++;
108         }
109         destin->size = j;
110         destin->ir_type = origin->ir_type;
111
112         IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
113
114         return 0;
115 }
116
117 /**
118  * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
119  * @dev:        the struct input_dev device descriptor
120  * @scancode:   the desired scancode
121  * @keycode:    the keycode to be retorned.
122  *
123  * This routine is used to handle evdev EVIOCGKEY ioctl.
124  * If the key is not found, returns -EINVAL, otherwise, returns 0.
125  */
126 static int ir_getkeycode(struct input_dev *dev,
127                          unsigned int scancode, unsigned int *keycode)
128 {
129         int elem;
130         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
131         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
132
133         elem = ir_seek_table(rc_tab, scancode);
134         if (elem >= 0) {
135                 *keycode = rc_tab->scan[elem].keycode;
136                 return 0;
137         }
138
139         /*
140          * Scancode not found and table can't be expanded
141          */
142         if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
143                 return -EINVAL;
144
145         /*
146          * If is there extra space, returns KEY_RESERVED,
147          * otherwise, input core won't let ir_setkeycode to work
148          */
149         *keycode = KEY_RESERVED;
150         return 0;
151 }
152
153 /**
154  * ir_is_resize_needed() - Check if the table needs rezise
155  * @table:              keycode table that may need to resize
156  * @n_elems:            minimum number of entries to store keycodes
157  *
158  * Considering that kmalloc uses power of two storage areas, this
159  * routine detects if the real alloced size will change. If not, it
160  * just returns without doing nothing. Otherwise, it will extend or
161  * reduce the table size to meet the new needs.
162  *
163  * It returns 0 if no resize is needed, 1 otherwise.
164  */
165 static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
166 {
167         int cur_size = ir_roundup_tablesize(table->size);
168         int new_size = ir_roundup_tablesize(n_elems);
169
170         if (cur_size == new_size)
171                 return 0;
172
173         /* Resize is needed */
174         return 1;
175 }
176
177 /**
178  * ir_delete_key() - remove a keycode from the table
179  * @rc_tab:             keycode table
180  * @elem:               element to be removed
181  *
182  */
183 static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
184 {
185         unsigned long flags = 0;
186         int newsize = rc_tab->size - 1;
187         int resize = ir_is_resize_needed(rc_tab, newsize);
188         struct ir_scancode *oldkeymap = rc_tab->scan;
189         struct ir_scancode *newkeymap = NULL;
190
191         if (resize)
192                 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
193                                     sizeof(*newkeymap), GFP_ATOMIC);
194
195         /* There's no memory for resize. Keep the old table */
196         if (!resize || !newkeymap) {
197                 newkeymap = oldkeymap;
198
199                 /* We'll modify the live table. Lock it */
200                 spin_lock_irqsave(&rc_tab->lock, flags);
201         }
202
203         /*
204          * Copy the elements before the one that will be deleted
205          * if (!resize), both oldkeymap and newkeymap points
206          * to the same place, so, there's no need to copy
207          */
208         if (resize && elem > 0)
209                 memcpy(newkeymap, oldkeymap,
210                        elem * sizeof(*newkeymap));
211
212         /*
213          * Copy the other elements overwriting the element to be removed
214          * This operation applies to both resize and non-resize case
215          */
216         if (elem < newsize)
217                 memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
218                        (newsize - elem) * sizeof(*newkeymap));
219
220         if (resize) {
221                 /*
222                  * As the copy happened to a temporary table, only here
223                  * it needs to lock while replacing the table pointers
224                  * to use the new table
225                  */
226                 spin_lock_irqsave(&rc_tab->lock, flags);
227                 rc_tab->size = newsize;
228                 rc_tab->scan = newkeymap;
229                 spin_unlock_irqrestore(&rc_tab->lock, flags);
230
231                 /* Frees the old keytable */
232                 kfree(oldkeymap);
233         } else {
234                 rc_tab->size = newsize;
235                 spin_unlock_irqrestore(&rc_tab->lock, flags);
236         }
237 }
238
239 /**
240  * ir_insert_key() - insert a keycode at the table
241  * @rc_tab:             keycode table
242  * @scancode:   the desired scancode
243  * @keycode:    the keycode to be retorned.
244  *
245  */
246 static int ir_insert_key(struct ir_scancode_table *rc_tab,
247                           int scancode, int keycode)
248 {
249         unsigned long flags;
250         int elem = rc_tab->size;
251         int newsize = rc_tab->size + 1;
252         int resize = ir_is_resize_needed(rc_tab, newsize);
253         struct ir_scancode *oldkeymap = rc_tab->scan;
254         struct ir_scancode *newkeymap;
255
256         if (resize) {
257                 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
258                                     sizeof(*newkeymap), GFP_ATOMIC);
259                 if (!newkeymap)
260                         return -ENOMEM;
261
262                 memcpy(newkeymap, oldkeymap,
263                        rc_tab->size * sizeof(*newkeymap));
264         } else
265                 newkeymap  = oldkeymap;
266
267         /* Stores the new code at the table */
268         IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
269                    rc_tab->size, scancode, keycode);
270
271         spin_lock_irqsave(&rc_tab->lock, flags);
272         rc_tab->size = newsize;
273         if (resize) {
274                 rc_tab->scan = newkeymap;
275                 kfree(oldkeymap);
276         }
277         newkeymap[elem].scancode = scancode;
278         newkeymap[elem].keycode  = keycode;
279         spin_unlock_irqrestore(&rc_tab->lock, flags);
280
281         return 0;
282 }
283
284 /**
285  * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
286  * @dev:        the struct input_dev device descriptor
287  * @scancode:   the desired scancode
288  * @keycode:    the keycode to be retorned.
289  *
290  * This routine is used to handle evdev EVIOCSKEY ioctl.
291  * There's one caveat here: how can we increase the size of the table?
292  * If the key is not found, returns -EINVAL, otherwise, returns 0.
293  */
294 static int ir_setkeycode(struct input_dev *dev,
295                          unsigned int scancode, unsigned int keycode)
296 {
297         int rc = 0;
298         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
299         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
300         struct ir_scancode *keymap = rc_tab->scan;
301         unsigned long flags;
302
303         /*
304          * Handle keycode table deletions
305          *
306          * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
307          * deal as a trial to remove an existing scancode attribution
308          * if table become too big, reduce it to save space
309          */
310         if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
311                 rc = ir_seek_table(rc_tab, scancode);
312                 if (rc < 0)
313                         return 0;
314
315                 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
316                 clear_bit(keymap[rc].keycode, dev->keybit);
317                 ir_delete_key(rc_tab, rc);
318
319                 return 0;
320         }
321
322         /*
323          * Handle keycode replacements
324          *
325          * If the scancode exists, just replace by the new value
326          */
327         rc = ir_seek_table(rc_tab, scancode);
328         if (rc >= 0) {
329                 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
330                         rc, scancode, keycode);
331
332                 clear_bit(keymap[rc].keycode, dev->keybit);
333
334                 spin_lock_irqsave(&rc_tab->lock, flags);
335                 keymap[rc].keycode = keycode;
336                 spin_unlock_irqrestore(&rc_tab->lock, flags);
337
338                 set_bit(keycode, dev->keybit);
339
340                 return 0;
341         }
342
343         /*
344          * Handle new scancode inserts
345          *
346          * reallocate table if needed and insert a new keycode
347          */
348
349         /* Avoid growing the table indefinitely */
350         if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
351                 return -EINVAL;
352
353         rc = ir_insert_key(rc_tab, scancode, keycode);
354         if (rc < 0)
355                 return rc;
356         set_bit(keycode, dev->keybit);
357
358         return 0;
359 }
360
361 /**
362  * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
363  * @input_dev:  the struct input_dev descriptor of the device
364  * @scancode:   the scancode that we're seeking
365  *
366  * This routine is used by the input routines when a key is pressed at the
367  * IR. The scancode is received and needs to be converted into a keycode.
368  * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
369  * corresponding keycode from the table.
370  */
371 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
372 {
373         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
374         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
375         struct ir_scancode *keymap = rc_tab->scan;
376         int elem;
377
378         elem = ir_seek_table(rc_tab, scancode);
379         if (elem >= 0) {
380                 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
381                            dev->name, scancode, keymap[elem].keycode);
382
383                 return rc_tab->scan[elem].keycode;
384         }
385
386         printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
387                dev->name, scancode);
388
389         /* Reports userspace that an unknown keycode were got */
390         return KEY_RESERVED;
391 }
392 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
393
394 /**
395  * ir_keyup() - generates input event to cleanup a key press
396  * @input_dev:  the struct input_dev descriptor of the device
397  *
398  * This routine is used by the input routines when a key is pressed at the
399  * IR. It reports a keyup input event via input_report_key().
400  */
401 void ir_keyup(struct input_dev *dev)
402 {
403         struct ir_input_dev *ir = input_get_drvdata(dev);
404
405         if (!ir->keypressed)
406                 return;
407
408         input_report_key(dev, ir->keycode, 0);
409         input_sync(dev);
410         ir->keypressed = 0;
411 }
412 EXPORT_SYMBOL_GPL(ir_keyup);
413
414 /**
415  * ir_keydown() - generates input event for a key press
416  * @input_dev:  the struct input_dev descriptor of the device
417  * @scancode:   the scancode that we're seeking
418  *
419  * This routine is used by the input routines when a key is pressed at the
420  * IR. It gets the keycode for a scancode and reports an input event via
421  * input_report_key().
422  */
423 void ir_keydown(struct input_dev *dev, int scancode)
424 {
425         struct ir_input_dev *ir = input_get_drvdata(dev);
426
427         u32 keycode = ir_g_keycode_from_table(dev, scancode);
428
429         /* If already sent a keydown, do a keyup */
430         if (ir->keypressed)
431                 ir_keyup(dev);
432
433         if (KEY_RESERVED == keycode)
434                 return;
435
436         ir->keycode = keycode;
437         ir->keypressed = 1;
438
439         IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
440                 dev->name, keycode, scancode);
441
442         input_report_key(dev, ir->keycode, 1);
443         input_sync(dev);
444
445 }
446 EXPORT_SYMBOL_GPL(ir_keydown);
447
448
449 /**
450  * ir_input_register() - sets the IR keycode table and add the handlers
451  *                          for keymap table get/set
452  * @input_dev:  the struct input_dev descriptor of the device
453  * @rc_tab:     the struct ir_scancode_table table of scancode/keymap
454  *
455  * This routine is used to initialize the input infrastructure
456  * to work with an IR.
457  * It will register the input/evdev interface for the device and
458  * register the syfs code for IR class
459  */
460 int ir_input_register(struct input_dev *input_dev,
461                       const struct ir_scancode_table *rc_tab,
462                       const struct ir_dev_props *props,
463                       const char *driver_name)
464 {
465         struct ir_input_dev *ir_dev;
466         struct ir_scancode  *keymap    = rc_tab->scan;
467         int i, rc;
468
469         if (rc_tab->scan == NULL || !rc_tab->size)
470                 return -EINVAL;
471
472         ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
473         if (!ir_dev)
474                 return -ENOMEM;
475
476         spin_lock_init(&ir_dev->rc_tab.lock);
477
478         ir_dev->driver_name = kmalloc(strlen(driver_name) + 1, GFP_KERNEL);
479         if (!ir_dev->driver_name)
480                 return -ENOMEM;
481         strcpy(ir_dev->driver_name, driver_name);
482         ir_dev->rc_tab.name = rc_tab->name;
483         ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size);
484         ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size *
485                                     sizeof(struct ir_scancode), GFP_KERNEL);
486         if (!ir_dev->rc_tab.scan) {
487                 kfree(ir_dev);
488                 return -ENOMEM;
489         }
490
491         IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
492                 ir_dev->rc_tab.size,
493                 ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan));
494
495         ir_copy_table(&ir_dev->rc_tab, rc_tab);
496         ir_dev->props = props;
497
498         /* set the bits for the keys */
499         IR_dprintk(1, "key map size: %d\n", rc_tab->size);
500         for (i = 0; i < rc_tab->size; i++) {
501                 IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
502                         i, keymap[i].keycode);
503                 set_bit(keymap[i].keycode, input_dev->keybit);
504         }
505         clear_bit(0, input_dev->keybit);
506
507         set_bit(EV_KEY, input_dev->evbit);
508
509         input_dev->getkeycode = ir_getkeycode;
510         input_dev->setkeycode = ir_setkeycode;
511         input_set_drvdata(input_dev, ir_dev);
512
513         rc = ir_register_class(input_dev);
514         if (rc < 0)
515                 goto err;
516
517         return 0;
518
519 err:
520         kfree(rc_tab->scan);
521         kfree(ir_dev);
522         return rc;
523 }
524 EXPORT_SYMBOL_GPL(ir_input_register);
525
526 /**
527  * ir_input_unregister() - unregisters IR and frees resources
528  * @input_dev:  the struct input_dev descriptor of the device
529
530  * This routine is used to free memory and de-register interfaces.
531  */
532 void ir_input_unregister(struct input_dev *dev)
533 {
534         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
535         struct ir_scancode_table *rc_tab;
536
537         if (!ir_dev)
538                 return;
539
540         IR_dprintk(1, "Freed keycode table\n");
541
542         rc_tab = &ir_dev->rc_tab;
543         rc_tab->size = 0;
544         kfree(rc_tab->scan);
545         rc_tab->scan = NULL;
546
547         ir_unregister_class(dev);
548
549         kfree(ir_dev);
550 }
551 EXPORT_SYMBOL_GPL(ir_input_unregister);
552
553 int ir_core_debug;    /* ir_debug level (0,1,2) */
554 EXPORT_SYMBOL_GPL(ir_core_debug);
555 module_param_named(debug, ir_core_debug, int, 0644);
556
557 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
558 MODULE_LICENSE("GPL");