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