blob: 10e4be22dbfdf6f4dcd991b9b672875d8d0ee255 [file] [log] [blame]
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -03001/* ir-register.c - handle IR scancode->keycode tables
2 *
3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03004 *
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.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030013 */
14
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030015
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030016#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030018#include <media/ir-common.h>
19
David Härdemanb3074c02010-04-02 15:58:28 -030020/* 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
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030023
24/**
David Härdemanb3074c02010-04-02 15:58:28 -030025 * 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
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -030028 *
David Härdemanb3074c02010-04-02 15:58:28 -030029 * This routine will shrink the ir_scancode_table if it has lots of
30 * unused entries and grow it if it is full.
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -030031 */
David Härdemanb3074c02010-04-02 15:58:28 -030032static 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 */
83static 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 */
149static int ir_setkeycode(struct input_dev *dev,
150 unsigned int scancode, unsigned int keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300151{
152 int rc;
153 unsigned long flags;
David Härdemanb3074c02010-04-02 15:58:28 -0300154 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
155 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300156
157 spin_lock_irqsave(&rc_tab->lock, flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300158 rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode);
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300159 spin_unlock_irqrestore(&rc_tab->lock, flags);
160 return rc;
161}
162
163/**
David Härdemanb3074c02010-04-02 15:58:28 -0300164 * 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.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300169 *
David Härdemanb3074c02010-04-02 15:58:28 -0300170 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300171 */
David Härdemanb3074c02010-04-02 15:58:28 -0300172static int ir_setkeytable(struct input_dev *dev,
173 struct ir_scancode_table *to,
174 const struct ir_scancode_table *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300175{
David Härdemanb3074c02010-04-02 15:58:28 -0300176 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;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300181
David Härdemanb3074c02010-04-02 15:58:28 -0300182 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;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300188 }
David Härdemanb3074c02010-04-02 15:58:28 -0300189 spin_unlock_irqrestore(&rc_tab->lock, flags);
190 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300191}
192
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300193/**
David Härdemanb3074c02010-04-02 15:58:28 -0300194 * ir_getkeycode() - get a keycode from the scancode->keycode table
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300195 * @dev: the struct input_dev device descriptor
196 * @scancode: the desired scancode
David Härdemanb3074c02010-04-02 15:58:28 -0300197 * @keycode: used to return the keycode, if found, or KEY_RESERVED
198 * @return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300199 *
200 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300201 */
202static int ir_getkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800203 unsigned int scancode, unsigned int *keycode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300204{
David Härdemanb3074c02010-04-02 15:58:28 -0300205 int start, end, mid;
206 unsigned long flags;
207 int key = KEY_RESERVED;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300208 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
209 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300210
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300211 spin_lock_irqsave(&rc_tab->lock, flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300212 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 }
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300224 }
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300225 spin_unlock_irqrestore(&rc_tab->lock, flags);
226
David Härdemanb3074c02010-04-02 15:58:28 -0300227 *keycode = key;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300228 return 0;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300229}
230
231/**
232 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300233 * @input_dev: the struct input_dev descriptor of the device
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300234 * @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.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300238 * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300239 * corresponding keycode from the table.
240 */
241u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
242{
David Härdemanb3074c02010-04-02 15:58:28 -0300243 int keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300244
David Härdemanb3074c02010-04-02 15:58:28 -0300245 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;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300249}
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -0300250EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300251
252/**
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300253 * 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 */
259void 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
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300266 IR_dprintk(1, "keyup key 0x%04x\n", ir->keycode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300267 input_report_key(dev, ir->keycode, 0);
268 input_sync(dev);
269 ir->keypressed = 0;
270}
271EXPORT_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 */
282void 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}
305EXPORT_SYMBOL_GPL(ir_keydown);
306
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300307static 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
314static 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}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300320
321/**
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300322 * __ir_input_register() - sets the IR keycode table and add the handlers
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300323 * 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 *
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300327 * 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
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300331 */
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300332int __ir_input_register(struct input_dev *input_dev,
Mauro Carvalho Chehabe93854d2009-12-14 00:16:55 -0300333 const struct ir_scancode_table *rc_tab,
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -0300334 const struct ir_dev_props *props,
335 const char *driver_name)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300336{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300337 struct ir_input_dev *ir_dev;
David Härdemanb3074c02010-04-02 15:58:28 -0300338 int rc;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300339
340 if (rc_tab->scan == NULL || !rc_tab->size)
341 return -EINVAL;
342
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300343 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
344 if (!ir_dev)
345 return -ENOMEM;
346
David Härdemanb3074c02010-04-02 15:58:28 -0300347 ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
348 if (!ir_dev->driver_name) {
349 rc = -ENOMEM;
350 goto out_dev;
Alexander Beregalov82311522010-01-09 13:51:14 -0300351 }
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300352
David Härdemanb3074c02010-04-02 15:58:28 -0300353 input_dev->getkeycode = ir_getkeycode;
354 input_dev->setkeycode = ir_setkeycode;
355 input_set_drvdata(input_dev, ir_dev);
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300356
David Härdemanb3074c02010-04-02 15:58:28 -0300357 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
Mauro Carvalho Chehabe93854d2009-12-14 00:16:55 -0300379 ir_dev->props = props;
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300380 if (props && props->open)
381 input_dev->open = ir_open;
382 if (props && props->close)
383 input_dev->close = ir_close;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300384
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300385 rc = ir_register_class(input_dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300386 if (rc < 0)
David Härdemanb3074c02010-04-02 15:58:28 -0300387 goto out_table;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300388
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300389 return 0;
390
David Härdemanb3074c02010-04-02 15:58:28 -0300391out_table:
392 kfree(ir_dev->rc_tab.scan);
393out_name:
394 kfree(ir_dev->driver_name);
395out_dev:
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300396 kfree(ir_dev);
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300397 return rc;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300398}
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300399EXPORT_SYMBOL_GPL(__ir_input_register);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300400
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300401/**
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 */
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300407void ir_input_unregister(struct input_dev *dev)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300408{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300409 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300410 struct ir_scancode_table *rc_tab;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300411
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300412 if (!ir_dev)
Mauro Carvalho Chehab05395a32009-12-06 08:32:49 -0300413 return;
414
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300415 IR_dprintk(1, "Freed keycode table\n");
416
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300417 rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300418 rc_tab->size = 0;
419 kfree(rc_tab->scan);
420 rc_tab->scan = NULL;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300421
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300422 ir_unregister_class(dev);
423
David Härdemanb3074c02010-04-02 15:58:28 -0300424 kfree(ir_dev->driver_name);
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300425 kfree(ir_dev);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300426}
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300427EXPORT_SYMBOL_GPL(ir_input_unregister);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300428
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -0300429int ir_core_debug; /* ir_debug level (0,1,2) */
430EXPORT_SYMBOL_GPL(ir_core_debug);
431module_param_named(debug, ir_core_debug, int, 0644);
432
433MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
434MODULE_LICENSE("GPL");