]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/IR/ir-keytable.c
10e4be22dbfdf6f4dcd991b9b672875d8d0ee255
[net-next-2.6.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 <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                         rc_tab->len--;
104                         memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1],
105                                 (rc_tab->len - i) * sizeof(struct ir_scancode));
106                 }
107
108                 /* Possibly shrink the keytable, failure is not a problem */
109                 ir_resize_table(rc_tab);
110                 break;
111         }
112
113         if (old_keycode == KEY_RESERVED) {
114                 /* No previous mapping found, we might need to grow the table */
115                 if (ir_resize_table(rc_tab))
116                         return -ENOMEM;
117
118                 /* i is the proper index to insert our new keycode */
119                 memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
120                         (rc_tab->len - i) * sizeof(struct ir_scancode));
121                 rc_tab->scan[i].scancode = scancode;
122                 rc_tab->scan[i].keycode = keycode;
123                 rc_tab->len++;
124                 set_bit(keycode, dev->keybit);
125         } else {
126                 /* A previous mapping was updated... */
127                 clear_bit(old_keycode, dev->keybit);
128                 /* ...but another scancode might use the same keycode */
129                 for (i = 0; i < rc_tab->len; i++) {
130                         if (rc_tab->scan[i].keycode == old_keycode) {
131                                 set_bit(old_keycode, dev->keybit);
132                                 break;
133                         }
134                 }
135         }
136
137         return 0;
138 }
139
140 /**
141  * ir_setkeycode() - set a keycode in the scancode->keycode table
142  * @dev:        the struct input_dev device descriptor
143  * @scancode:   the desired scancode
144  * @keycode:    result
145  * @return:     -EINVAL if the keycode could not be inserted, otherwise zero.
146  *
147  * This routine is used to handle evdev EVIOCSKEY ioctl.
148  */
149 static int ir_setkeycode(struct input_dev *dev,
150                          unsigned int scancode, unsigned int keycode)
151 {
152         int rc;
153         unsigned long flags;
154         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
155         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
156
157         spin_lock_irqsave(&rc_tab->lock, flags);
158         rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode);
159         spin_unlock_irqrestore(&rc_tab->lock, flags);
160         return rc;
161 }
162
163 /**
164  * ir_setkeytable() - sets several entries in the scancode->keycode table
165  * @dev:        the struct input_dev device descriptor
166  * @to:         the struct ir_scancode_table to copy entries to
167  * @from:       the struct ir_scancode_table to copy entries from
168  * @return:     -EINVAL if all keycodes could not be inserted, otherwise zero.
169  *
170  * This routine is used to handle table initialization.
171  */
172 static int ir_setkeytable(struct input_dev *dev,
173                           struct ir_scancode_table *to,
174                           const struct ir_scancode_table *from)
175 {
176         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
177         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
178         unsigned long flags;
179         unsigned int i;
180         int rc = 0;
181
182         spin_lock_irqsave(&rc_tab->lock, flags);
183         for (i = 0; i < from->size; i++) {
184                 rc = ir_do_setkeycode(dev, to, from->scan[i].scancode,
185                                       from->scan[i].keycode);
186                 if (rc)
187                         break;
188         }
189         spin_unlock_irqrestore(&rc_tab->lock, flags);
190         return rc;
191 }
192
193 /**
194  * ir_getkeycode() - get a keycode from the scancode->keycode table
195  * @dev:        the struct input_dev device descriptor
196  * @scancode:   the desired scancode
197  * @keycode:    used to return the keycode, if found, or KEY_RESERVED
198  * @return:     always returns zero.
199  *
200  * This routine is used to handle evdev EVIOCGKEY ioctl.
201  */
202 static int ir_getkeycode(struct input_dev *dev,
203                          unsigned int scancode, unsigned int *keycode)
204 {
205         int start, end, mid;
206         unsigned long flags;
207         int key = KEY_RESERVED;
208         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
209         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
210
211         spin_lock_irqsave(&rc_tab->lock, flags);
212         start = 0;
213         end = rc_tab->len - 1;
214         while (start <= end) {
215                 mid = (start + end) / 2;
216                 if (rc_tab->scan[mid].scancode < scancode)
217                         start = mid + 1;
218                 else if (rc_tab->scan[mid].scancode > scancode)
219                         end = mid - 1;
220                 else {
221                         key = rc_tab->scan[mid].keycode;
222                         break;
223                 }
224         }
225         spin_unlock_irqrestore(&rc_tab->lock, flags);
226
227         *keycode = key;
228         return 0;
229 }
230
231 /**
232  * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
233  * @input_dev:  the struct input_dev descriptor of the device
234  * @scancode:   the scancode that we're seeking
235  *
236  * This routine is used by the input routines when a key is pressed at the
237  * IR. The scancode is received and needs to be converted into a keycode.
238  * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
239  * corresponding keycode from the table.
240  */
241 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
242 {
243         int keycode;
244
245         ir_getkeycode(dev, scancode, &keycode);
246         IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
247                    dev->name, scancode, keycode);
248         return keycode;
249 }
250 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
251
252 /**
253  * ir_keyup() - generates input event to cleanup a key press
254  * @input_dev:  the struct input_dev descriptor of the device
255  *
256  * This routine is used by the input routines when a key is pressed at the
257  * IR. It reports a keyup input event via input_report_key().
258  */
259 void ir_keyup(struct input_dev *dev)
260 {
261         struct ir_input_dev *ir = input_get_drvdata(dev);
262
263         if (!ir->keypressed)
264                 return;
265
266         IR_dprintk(1, "keyup key 0x%04x\n", ir->keycode);
267         input_report_key(dev, ir->keycode, 0);
268         input_sync(dev);
269         ir->keypressed = 0;
270 }
271 EXPORT_SYMBOL_GPL(ir_keyup);
272
273 /**
274  * ir_keydown() - generates input event for a key press
275  * @input_dev:  the struct input_dev descriptor of the device
276  * @scancode:   the scancode that we're seeking
277  *
278  * This routine is used by the input routines when a key is pressed at the
279  * IR. It gets the keycode for a scancode and reports an input event via
280  * input_report_key().
281  */
282 void ir_keydown(struct input_dev *dev, int scancode)
283 {
284         struct ir_input_dev *ir = input_get_drvdata(dev);
285
286         u32 keycode = ir_g_keycode_from_table(dev, scancode);
287
288         /* If already sent a keydown, do a keyup */
289         if (ir->keypressed)
290                 ir_keyup(dev);
291
292         if (KEY_RESERVED == keycode)
293                 return;
294
295         ir->keycode = keycode;
296         ir->keypressed = 1;
297
298         IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
299                 dev->name, keycode, scancode);
300
301         input_report_key(dev, ir->keycode, 1);
302         input_sync(dev);
303
304 }
305 EXPORT_SYMBOL_GPL(ir_keydown);
306
307 static int ir_open(struct input_dev *input_dev)
308 {
309         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
310
311         return ir_dev->props->open(ir_dev->props->priv);
312 }
313
314 static void ir_close(struct input_dev *input_dev)
315 {
316         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
317
318         ir_dev->props->close(ir_dev->props->priv);
319 }
320
321 /**
322  * __ir_input_register() - sets the IR keycode table and add the handlers
323  *                          for keymap table get/set
324  * @input_dev:  the struct input_dev descriptor of the device
325  * @rc_tab:     the struct ir_scancode_table table of scancode/keymap
326  *
327  * This routine is used to initialize the input infrastructure
328  * to work with an IR.
329  * It will register the input/evdev interface for the device and
330  * register the syfs code for IR class
331  */
332 int __ir_input_register(struct input_dev *input_dev,
333                       const struct ir_scancode_table *rc_tab,
334                       const struct ir_dev_props *props,
335                       const char *driver_name)
336 {
337         struct ir_input_dev *ir_dev;
338         int rc;
339
340         if (rc_tab->scan == NULL || !rc_tab->size)
341                 return -EINVAL;
342
343         ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
344         if (!ir_dev)
345                 return -ENOMEM;
346
347         ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
348         if (!ir_dev->driver_name) {
349                 rc = -ENOMEM;
350                 goto out_dev;
351         }
352
353         input_dev->getkeycode = ir_getkeycode;
354         input_dev->setkeycode = ir_setkeycode;
355         input_set_drvdata(input_dev, ir_dev);
356
357         spin_lock_init(&ir_dev->rc_tab.lock);
358         ir_dev->rc_tab.name = rc_tab->name;
359         ir_dev->rc_tab.ir_type = rc_tab->ir_type;
360         ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size *
361                                                   sizeof(struct ir_scancode));
362         ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL);
363         ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode);
364
365         if (!ir_dev->rc_tab.scan) {
366                 rc = -ENOMEM;
367                 goto out_name;
368         }
369
370         IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
371                    ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
372
373         set_bit(EV_KEY, input_dev->evbit);
374         if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
375                 rc = -ENOMEM;
376                 goto out_table;
377         }
378
379         ir_dev->props = props;
380         if (props && props->open)
381                 input_dev->open = ir_open;
382         if (props && props->close)
383                 input_dev->close = ir_close;
384
385         rc = ir_register_class(input_dev);
386         if (rc < 0)
387                 goto out_table;
388
389         return 0;
390
391 out_table:
392         kfree(ir_dev->rc_tab.scan);
393 out_name:
394         kfree(ir_dev->driver_name);
395 out_dev:
396         kfree(ir_dev);
397         return rc;
398 }
399 EXPORT_SYMBOL_GPL(__ir_input_register);
400
401 /**
402  * ir_input_unregister() - unregisters IR and frees resources
403  * @input_dev:  the struct input_dev descriptor of the device
404
405  * This routine is used to free memory and de-register interfaces.
406  */
407 void ir_input_unregister(struct input_dev *dev)
408 {
409         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
410         struct ir_scancode_table *rc_tab;
411
412         if (!ir_dev)
413                 return;
414
415         IR_dprintk(1, "Freed keycode table\n");
416
417         rc_tab = &ir_dev->rc_tab;
418         rc_tab->size = 0;
419         kfree(rc_tab->scan);
420         rc_tab->scan = NULL;
421
422         ir_unregister_class(dev);
423
424         kfree(ir_dev->driver_name);
425         kfree(ir_dev);
426 }
427 EXPORT_SYMBOL_GPL(ir_input_unregister);
428
429 int ir_core_debug;    /* ir_debug level (0,1,2) */
430 EXPORT_SYMBOL_GPL(ir_core_debug);
431 module_param_named(debug, ir_core_debug, int, 0644);
432
433 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
434 MODULE_LICENSE("GPL");