1 /* ir-register.c - handle IR scancode->keycode tables
3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
16 #include <linux/input.h>
17 #include <linux/slab.h>
18 #include <media/ir-common.h>
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
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
29 * This routine will shrink the ir_scancode_table if it has lots of
30 * unused entries and grow it if it is full.
32 static int ir_resize_table(struct ir_scancode_table *rc_tab)
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;
39 if (rc_tab->size == rc_tab->len) {
40 /* All entries in use -> grow keytable */
41 if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
45 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
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 */
51 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
54 if (newalloc == oldalloc)
57 newscan = kmalloc(newalloc, GFP_ATOMIC);
59 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
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);
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.
80 * This routine is used internally to manipulate the scancode->keycode table.
81 * The caller has to hold @rc_tab->lock.
83 static int ir_do_setkeycode(struct input_dev *dev,
84 struct ir_scancode_table *rc_tab,
85 unsigned scancode, unsigned keycode)
88 int old_keycode = KEY_RESERVED;
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)
95 else if (rc_tab->scan[i].scancode < scancode)
98 old_keycode = rc_tab->scan[i].keycode;
99 rc_tab->scan[i].keycode = keycode;
101 /* Did the user wish to remove the mapping? */
102 if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) {
104 memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1],
105 (rc_tab->len - i) * sizeof(struct ir_scancode));
108 /* Possibly shrink the keytable, failure is not a problem */
109 ir_resize_table(rc_tab);
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))
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;
124 set_bit(keycode, dev->keybit);
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);
141 * ir_setkeycode() - set a keycode in the scancode->keycode table
142 * @dev: the struct input_dev device descriptor
143 * @scancode: the desired scancode
145 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
147 * This routine is used to handle evdev EVIOCSKEY ioctl.
149 static int ir_setkeycode(struct input_dev *dev,
150 unsigned int scancode, unsigned int keycode)
154 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
155 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
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);
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.
170 * This routine is used to handle table initialization.
172 static int ir_setkeytable(struct input_dev *dev,
173 struct ir_scancode_table *to,
174 const struct ir_scancode_table *from)
176 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
177 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
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);
189 spin_unlock_irqrestore(&rc_tab->lock, flags);
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.
200 * This routine is used to handle evdev EVIOCGKEY ioctl.
202 static int ir_getkeycode(struct input_dev *dev,
203 unsigned int scancode, unsigned int *keycode)
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;
211 spin_lock_irqsave(&rc_tab->lock, flags);
213 end = rc_tab->len - 1;
214 while (start <= end) {
215 mid = (start + end) / 2;
216 if (rc_tab->scan[mid].scancode < scancode)
218 else if (rc_tab->scan[mid].scancode > scancode)
221 key = rc_tab->scan[mid].keycode;
225 spin_unlock_irqrestore(&rc_tab->lock, flags);
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
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.
241 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
245 ir_getkeycode(dev, scancode, &keycode);
246 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
247 dev->name, scancode, keycode);
250 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
253 * ir_keyup() - generates input event to cleanup a key press
254 * @input_dev: the struct input_dev descriptor of the device
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().
259 void ir_keyup(struct input_dev *dev)
261 struct ir_input_dev *ir = input_get_drvdata(dev);
266 IR_dprintk(1, "keyup key 0x%04x\n", ir->keycode);
267 input_report_key(dev, ir->keycode, 0);
271 EXPORT_SYMBOL_GPL(ir_keyup);
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
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().
282 void ir_keydown(struct input_dev *dev, int scancode)
284 struct ir_input_dev *ir = input_get_drvdata(dev);
286 u32 keycode = ir_g_keycode_from_table(dev, scancode);
288 /* If already sent a keydown, do a keyup */
292 if (KEY_RESERVED == keycode)
295 ir->keycode = keycode;
298 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
299 dev->name, keycode, scancode);
301 input_report_key(dev, ir->keycode, 1);
305 EXPORT_SYMBOL_GPL(ir_keydown);
307 static int ir_open(struct input_dev *input_dev)
309 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
311 return ir_dev->props->open(ir_dev->props->priv);
314 static void ir_close(struct input_dev *input_dev)
316 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
318 ir_dev->props->close(ir_dev->props->priv);
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
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
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)
337 struct ir_input_dev *ir_dev;
340 if (rc_tab->scan == NULL || !rc_tab->size)
343 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
347 ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
348 if (!ir_dev->driver_name) {
353 input_dev->getkeycode = ir_getkeycode;
354 input_dev->setkeycode = ir_setkeycode;
355 input_set_drvdata(input_dev, ir_dev);
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);
365 if (!ir_dev->rc_tab.scan) {
370 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
371 ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
373 set_bit(EV_KEY, input_dev->evbit);
374 if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
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;
385 rc = ir_register_class(input_dev);
392 kfree(ir_dev->rc_tab.scan);
394 kfree(ir_dev->driver_name);
399 EXPORT_SYMBOL_GPL(__ir_input_register);
402 * ir_input_unregister() - unregisters IR and frees resources
403 * @input_dev: the struct input_dev descriptor of the device
405 * This routine is used to free memory and de-register interfaces.
407 void ir_input_unregister(struct input_dev *dev)
409 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
410 struct ir_scancode_table *rc_tab;
415 IR_dprintk(1, "Freed keycode table\n");
417 rc_tab = &ir_dev->rc_tab;
422 ir_unregister_class(dev);
424 kfree(ir_dev->driver_name);
427 EXPORT_SYMBOL_GPL(ir_input_unregister);
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);
433 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
434 MODULE_LICENSE("GPL");