]> Pileus Git - ~andy/linux/blob - drivers/media/IR/ir-keytable.c
V4L/DVB: ir-core: re-add some debug functions for keytable changes
[~andy/linux] / 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 /* 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 /**
25  * ir_resize_table() - resizes a scancode table if necessary
26  * @rc_tab:     the ir_scancode_table to resize
27  * @return:     zero on success or a negative error code
28  *
29  * This routine will shrink the ir_scancode_table if it has lots of
30  * unused entries and grow it if it is full.
31  */
32 static int ir_resize_table(struct ir_scancode_table *rc_tab)
33 {
34         unsigned int oldalloc = rc_tab->alloc;
35         unsigned int newalloc = oldalloc;
36         struct ir_scancode *oldscan = rc_tab->scan;
37         struct ir_scancode *newscan;
38
39         if (rc_tab->size == rc_tab->len) {
40                 /* All entries in use -> grow keytable */
41                 if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
42                         return -ENOMEM;
43
44                 newalloc *= 2;
45                 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
46         }
47
48         if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
49                 /* Less than 1/3 of entries in use -> shrink keytable */
50                 newalloc /= 2;
51                 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
52         }
53
54         if (newalloc == oldalloc)
55                 return 0;
56
57         newscan = kmalloc(newalloc, GFP_ATOMIC);
58         if (!newscan) {
59                 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
60                 return -ENOMEM;
61         }
62
63         memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
64         rc_tab->scan = newscan;
65         rc_tab->alloc = newalloc;
66         rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
67         kfree(oldscan);
68         return 0;
69 }
70
71 /**
72  * ir_do_setkeycode() - internal function to set a keycode in the
73  *                      scancode->keycode table
74  * @dev:        the struct input_dev device descriptor
75  * @rc_tab:     the struct ir_scancode_table to set the keycode in
76  * @scancode:   the scancode for the ir command
77  * @keycode:    the keycode for the ir command
78  * @return:     -EINVAL if the keycode could not be inserted, otherwise zero.
79  *
80  * This routine is used internally to manipulate the scancode->keycode table.
81  * The caller has to hold @rc_tab->lock.
82  */
83 static int ir_do_setkeycode(struct input_dev *dev,
84                             struct ir_scancode_table *rc_tab,
85                             unsigned scancode, unsigned keycode)
86 {
87         unsigned int i;
88         int old_keycode = KEY_RESERVED;
89
90         /* First check if we already have a mapping for this ir command */
91         for (i = 0; i < rc_tab->len; i++) {
92                 /* Keytable is sorted from lowest to highest scancode */
93                 if (rc_tab->scan[i].scancode > scancode)
94                         break;
95                 else if (rc_tab->scan[i].scancode < scancode)
96                         continue;
97
98                 old_keycode = rc_tab->scan[i].keycode;
99                 rc_tab->scan[i].keycode = keycode;
100
101                 /* Did the user wish to remove the mapping? */
102                 if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) {
103                         IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
104                                    i, scancode);
105                         rc_tab->len--;
106                         memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1],
107                                 (rc_tab->len - i) * sizeof(struct ir_scancode));
108                 }
109
110                 /* Possibly shrink the keytable, failure is not a problem */
111                 ir_resize_table(rc_tab);
112                 break;
113         }
114
115         if (old_keycode == KEY_RESERVED) {
116                 /* No previous mapping found, we might need to grow the table */
117                 if (ir_resize_table(rc_tab))
118                         return -ENOMEM;
119
120                 IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
121                            i, scancode, keycode);
122
123                 /* i is the proper index to insert our new keycode */
124                 memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
125                         (rc_tab->len - i) * sizeof(struct ir_scancode));
126                 rc_tab->scan[i].scancode = scancode;
127                 rc_tab->scan[i].keycode = keycode;
128                 rc_tab->len++;
129                 set_bit(keycode, dev->keybit);
130         } else {
131                 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
132                            i, scancode, keycode);
133                 /* A previous mapping was updated... */
134                 clear_bit(old_keycode, dev->keybit);
135                 /* ...but another scancode might use the same keycode */
136                 for (i = 0; i < rc_tab->len; i++) {
137                         if (rc_tab->scan[i].keycode == old_keycode) {
138                                 set_bit(old_keycode, dev->keybit);
139                                 break;
140                         }
141                 }
142         }
143
144         return 0;
145 }
146
147 /**
148  * ir_setkeycode() - set a keycode in the scancode->keycode table
149  * @dev:        the struct input_dev device descriptor
150  * @scancode:   the desired scancode
151  * @keycode:    result
152  * @return:     -EINVAL if the keycode could not be inserted, otherwise zero.
153  *
154  * This routine is used to handle evdev EVIOCSKEY ioctl.
155  */
156 static int ir_setkeycode(struct input_dev *dev,
157                          unsigned int scancode, unsigned int keycode)
158 {
159         int rc;
160         unsigned long flags;
161         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
162         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
163
164         spin_lock_irqsave(&rc_tab->lock, flags);
165         rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode);
166         spin_unlock_irqrestore(&rc_tab->lock, flags);
167         return rc;
168 }
169
170 /**
171  * ir_setkeytable() - sets several entries in the scancode->keycode table
172  * @dev:        the struct input_dev device descriptor
173  * @to:         the struct ir_scancode_table to copy entries to
174  * @from:       the struct ir_scancode_table to copy entries from
175  * @return:     -EINVAL if all keycodes could not be inserted, otherwise zero.
176  *
177  * This routine is used to handle table initialization.
178  */
179 static int ir_setkeytable(struct input_dev *dev,
180                           struct ir_scancode_table *to,
181                           const struct ir_scancode_table *from)
182 {
183         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
184         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
185         unsigned long flags;
186         unsigned int i;
187         int rc = 0;
188
189         spin_lock_irqsave(&rc_tab->lock, flags);
190         for (i = 0; i < from->size; i++) {
191                 rc = ir_do_setkeycode(dev, to, from->scan[i].scancode,
192                                       from->scan[i].keycode);
193                 if (rc)
194                         break;
195         }
196         spin_unlock_irqrestore(&rc_tab->lock, flags);
197         return rc;
198 }
199
200 /**
201  * ir_getkeycode() - get a keycode from the scancode->keycode table
202  * @dev:        the struct input_dev device descriptor
203  * @scancode:   the desired scancode
204  * @keycode:    used to return the keycode, if found, or KEY_RESERVED
205  * @return:     always returns zero.
206  *
207  * This routine is used to handle evdev EVIOCGKEY ioctl.
208  */
209 static int ir_getkeycode(struct input_dev *dev,
210                          unsigned int scancode, unsigned int *keycode)
211 {
212         int start, end, mid;
213         unsigned long flags;
214         int key = KEY_RESERVED;
215         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
216         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
217
218         spin_lock_irqsave(&rc_tab->lock, flags);
219         start = 0;
220         end = rc_tab->len - 1;
221         while (start <= end) {
222                 mid = (start + end) / 2;
223                 if (rc_tab->scan[mid].scancode < scancode)
224                         start = mid + 1;
225                 else if (rc_tab->scan[mid].scancode > scancode)
226                         end = mid - 1;
227                 else {
228                         key = rc_tab->scan[mid].keycode;
229                         break;
230                 }
231         }
232         spin_unlock_irqrestore(&rc_tab->lock, flags);
233
234         if (key == KEY_RESERVED)
235                 IR_dprintk(1, "unknown key for scancode 0x%04x\n",
236                            scancode);
237
238         *keycode = key;
239         return 0;
240 }
241
242 /**
243  * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
244  * @input_dev:  the struct input_dev descriptor of the device
245  * @scancode:   the scancode that we're seeking
246  *
247  * This routine is used by the input routines when a key is pressed at the
248  * IR. The scancode is received and needs to be converted into a keycode.
249  * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
250  * corresponding keycode from the table.
251  */
252 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
253 {
254         int keycode;
255
256         ir_getkeycode(dev, scancode, &keycode);
257         if (keycode != KEY_RESERVED)
258                 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
259                            dev->name, scancode, keycode);
260         return keycode;
261 }
262 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
263
264 /**
265  * ir_keyup() - generates input event to cleanup a key press
266  * @input_dev:  the struct input_dev descriptor of the device
267  *
268  * This routine is used by the input routines when a key is pressed at the
269  * IR. It reports a keyup input event via input_report_key().
270  */
271 void ir_keyup(struct input_dev *dev)
272 {
273         struct ir_input_dev *ir = input_get_drvdata(dev);
274
275         if (!ir->keypressed)
276                 return;
277
278         IR_dprintk(1, "keyup key 0x%04x\n", ir->keycode);
279         input_report_key(dev, ir->keycode, 0);
280         input_sync(dev);
281         ir->keypressed = 0;
282 }
283 EXPORT_SYMBOL_GPL(ir_keyup);
284
285 /**
286  * ir_keydown() - generates input event for a key press
287  * @input_dev:  the struct input_dev descriptor of the device
288  * @scancode:   the scancode that we're seeking
289  *
290  * This routine is used by the input routines when a key is pressed at the
291  * IR. It gets the keycode for a scancode and reports an input event via
292  * input_report_key().
293  */
294 void ir_keydown(struct input_dev *dev, int scancode)
295 {
296         struct ir_input_dev *ir = input_get_drvdata(dev);
297
298         u32 keycode = ir_g_keycode_from_table(dev, scancode);
299
300         /* If already sent a keydown, do a keyup */
301         if (ir->keypressed)
302                 ir_keyup(dev);
303
304         if (KEY_RESERVED == keycode)
305                 return;
306
307         ir->keycode = keycode;
308         ir->keypressed = 1;
309
310         IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
311                 dev->name, keycode, scancode);
312
313         input_report_key(dev, ir->keycode, 1);
314         input_sync(dev);
315
316 }
317 EXPORT_SYMBOL_GPL(ir_keydown);
318
319 static int ir_open(struct input_dev *input_dev)
320 {
321         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
322
323         return ir_dev->props->open(ir_dev->props->priv);
324 }
325
326 static void ir_close(struct input_dev *input_dev)
327 {
328         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
329
330         ir_dev->props->close(ir_dev->props->priv);
331 }
332
333 /**
334  * __ir_input_register() - sets the IR keycode table and add the handlers
335  *                          for keymap table get/set
336  * @input_dev:  the struct input_dev descriptor of the device
337  * @rc_tab:     the struct ir_scancode_table table of scancode/keymap
338  *
339  * This routine is used to initialize the input infrastructure
340  * to work with an IR.
341  * It will register the input/evdev interface for the device and
342  * register the syfs code for IR class
343  */
344 int __ir_input_register(struct input_dev *input_dev,
345                       const struct ir_scancode_table *rc_tab,
346                       const struct ir_dev_props *props,
347                       const char *driver_name)
348 {
349         struct ir_input_dev *ir_dev;
350         int rc;
351
352         if (rc_tab->scan == NULL || !rc_tab->size)
353                 return -EINVAL;
354
355         ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
356         if (!ir_dev)
357                 return -ENOMEM;
358
359         ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
360         if (!ir_dev->driver_name) {
361                 rc = -ENOMEM;
362                 goto out_dev;
363         }
364
365         input_dev->getkeycode = ir_getkeycode;
366         input_dev->setkeycode = ir_setkeycode;
367         input_set_drvdata(input_dev, ir_dev);
368
369         spin_lock_init(&ir_dev->rc_tab.lock);
370         ir_dev->rc_tab.name = rc_tab->name;
371         ir_dev->rc_tab.ir_type = rc_tab->ir_type;
372         ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size *
373                                                   sizeof(struct ir_scancode));
374         ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL);
375         ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode);
376
377         if (!ir_dev->rc_tab.scan) {
378                 rc = -ENOMEM;
379                 goto out_name;
380         }
381
382         IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
383                    ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
384
385         set_bit(EV_KEY, input_dev->evbit);
386         if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
387                 rc = -ENOMEM;
388                 goto out_table;
389         }
390
391         ir_dev->props = props;
392         if (props && props->open)
393                 input_dev->open = ir_open;
394         if (props && props->close)
395                 input_dev->close = ir_close;
396
397         rc = ir_register_class(input_dev);
398         if (rc < 0)
399                 goto out_table;
400
401         IR_dprintk(1, "Registered input device on %s for %s remote.\n",
402                    driver_name, rc_tab->name);
403
404         return 0;
405
406 out_table:
407         kfree(ir_dev->rc_tab.scan);
408 out_name:
409         kfree(ir_dev->driver_name);
410 out_dev:
411         kfree(ir_dev);
412         return rc;
413 }
414 EXPORT_SYMBOL_GPL(__ir_input_register);
415
416 /**
417  * ir_input_unregister() - unregisters IR and frees resources
418  * @input_dev:  the struct input_dev descriptor of the device
419
420  * This routine is used to free memory and de-register interfaces.
421  */
422 void ir_input_unregister(struct input_dev *dev)
423 {
424         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
425         struct ir_scancode_table *rc_tab;
426
427         if (!ir_dev)
428                 return;
429
430         IR_dprintk(1, "Freed keycode table\n");
431
432         rc_tab = &ir_dev->rc_tab;
433         rc_tab->size = 0;
434         kfree(rc_tab->scan);
435         rc_tab->scan = NULL;
436
437         ir_unregister_class(dev);
438
439         kfree(ir_dev->driver_name);
440         kfree(ir_dev);
441 }
442 EXPORT_SYMBOL_GPL(ir_input_unregister);
443
444 int ir_core_debug;    /* ir_debug level (0,1,2) */
445 EXPORT_SYMBOL_GPL(ir_core_debug);
446 module_param_named(debug, ir_core_debug, int, 0644);
447
448 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
449 MODULE_LICENSE("GPL");