blob: a89456932f7cb157f64b4bae38203d34b637bc66 [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
David Härdemana374fef2010-04-02 15:58:29 -030024/* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
25#define IR_KEYPRESS_TIMEOUT 250
26
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030027/**
David Härdemanb3074c02010-04-02 15:58:28 -030028 * ir_resize_table() - resizes a scancode table if necessary
29 * @rc_tab: the ir_scancode_table to resize
30 * @return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -030031 *
David Härdemanb3074c02010-04-02 15:58:28 -030032 * This routine will shrink the ir_scancode_table if it has lots of
33 * unused entries and grow it if it is full.
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -030034 */
David Härdemanb3074c02010-04-02 15:58:28 -030035static int ir_resize_table(struct ir_scancode_table *rc_tab)
36{
37 unsigned int oldalloc = rc_tab->alloc;
38 unsigned int newalloc = oldalloc;
39 struct ir_scancode *oldscan = rc_tab->scan;
40 struct ir_scancode *newscan;
41
42 if (rc_tab->size == rc_tab->len) {
43 /* All entries in use -> grow keytable */
44 if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
45 return -ENOMEM;
46
47 newalloc *= 2;
48 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
49 }
50
51 if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
52 /* Less than 1/3 of entries in use -> shrink keytable */
53 newalloc /= 2;
54 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
55 }
56
57 if (newalloc == oldalloc)
58 return 0;
59
60 newscan = kmalloc(newalloc, GFP_ATOMIC);
61 if (!newscan) {
62 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
63 return -ENOMEM;
64 }
65
66 memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
67 rc_tab->scan = newscan;
68 rc_tab->alloc = newalloc;
69 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
70 kfree(oldscan);
71 return 0;
72}
73
74/**
75 * ir_do_setkeycode() - internal function to set a keycode in the
76 * scancode->keycode table
77 * @dev: the struct input_dev device descriptor
78 * @rc_tab: the struct ir_scancode_table to set the keycode in
79 * @scancode: the scancode for the ir command
80 * @keycode: the keycode for the ir command
81 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
82 *
83 * This routine is used internally to manipulate the scancode->keycode table.
84 * The caller has to hold @rc_tab->lock.
85 */
86static int ir_do_setkeycode(struct input_dev *dev,
87 struct ir_scancode_table *rc_tab,
88 unsigned scancode, unsigned keycode)
89{
90 unsigned int i;
91 int old_keycode = KEY_RESERVED;
92
93 /* First check if we already have a mapping for this ir command */
94 for (i = 0; i < rc_tab->len; i++) {
95 /* Keytable is sorted from lowest to highest scancode */
96 if (rc_tab->scan[i].scancode > scancode)
97 break;
98 else if (rc_tab->scan[i].scancode < scancode)
99 continue;
100
101 old_keycode = rc_tab->scan[i].keycode;
102 rc_tab->scan[i].keycode = keycode;
103
104 /* Did the user wish to remove the mapping? */
105 if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) {
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300106 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
107 i, scancode);
David Härdemanb3074c02010-04-02 15:58:28 -0300108 rc_tab->len--;
109 memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1],
110 (rc_tab->len - i) * sizeof(struct ir_scancode));
111 }
112
113 /* Possibly shrink the keytable, failure is not a problem */
114 ir_resize_table(rc_tab);
115 break;
116 }
117
118 if (old_keycode == KEY_RESERVED) {
119 /* No previous mapping found, we might need to grow the table */
120 if (ir_resize_table(rc_tab))
121 return -ENOMEM;
122
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300123 IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
124 i, scancode, keycode);
125
David Härdemanb3074c02010-04-02 15:58:28 -0300126 /* i is the proper index to insert our new keycode */
127 memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
128 (rc_tab->len - i) * sizeof(struct ir_scancode));
129 rc_tab->scan[i].scancode = scancode;
130 rc_tab->scan[i].keycode = keycode;
131 rc_tab->len++;
132 set_bit(keycode, dev->keybit);
133 } else {
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300134 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
135 i, scancode, keycode);
David Härdemanb3074c02010-04-02 15:58:28 -0300136 /* A previous mapping was updated... */
137 clear_bit(old_keycode, dev->keybit);
138 /* ...but another scancode might use the same keycode */
139 for (i = 0; i < rc_tab->len; i++) {
140 if (rc_tab->scan[i].keycode == old_keycode) {
141 set_bit(old_keycode, dev->keybit);
142 break;
143 }
144 }
145 }
146
147 return 0;
148}
149
150/**
151 * ir_setkeycode() - set a keycode in the scancode->keycode table
152 * @dev: the struct input_dev device descriptor
153 * @scancode: the desired scancode
154 * @keycode: result
155 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
156 *
157 * This routine is used to handle evdev EVIOCSKEY ioctl.
158 */
159static int ir_setkeycode(struct input_dev *dev,
160 unsigned int scancode, unsigned int keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300161{
162 int rc;
163 unsigned long flags;
David Härdemanb3074c02010-04-02 15:58:28 -0300164 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
165 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300166
167 spin_lock_irqsave(&rc_tab->lock, flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300168 rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode);
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300169 spin_unlock_irqrestore(&rc_tab->lock, flags);
170 return rc;
171}
172
173/**
David Härdemanb3074c02010-04-02 15:58:28 -0300174 * ir_setkeytable() - sets several entries in the scancode->keycode table
175 * @dev: the struct input_dev device descriptor
176 * @to: the struct ir_scancode_table to copy entries to
177 * @from: the struct ir_scancode_table to copy entries from
178 * @return: -EINVAL if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300179 *
David Härdemanb3074c02010-04-02 15:58:28 -0300180 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300181 */
David Härdemanb3074c02010-04-02 15:58:28 -0300182static int ir_setkeytable(struct input_dev *dev,
183 struct ir_scancode_table *to,
184 const struct ir_scancode_table *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300185{
David Härdemanb3074c02010-04-02 15:58:28 -0300186 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
187 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
188 unsigned long flags;
189 unsigned int i;
190 int rc = 0;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300191
David Härdemanb3074c02010-04-02 15:58:28 -0300192 spin_lock_irqsave(&rc_tab->lock, flags);
193 for (i = 0; i < from->size; i++) {
194 rc = ir_do_setkeycode(dev, to, from->scan[i].scancode,
195 from->scan[i].keycode);
196 if (rc)
197 break;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300198 }
David Härdemanb3074c02010-04-02 15:58:28 -0300199 spin_unlock_irqrestore(&rc_tab->lock, flags);
200 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300201}
202
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300203/**
David Härdemanb3074c02010-04-02 15:58:28 -0300204 * ir_getkeycode() - get a keycode from the scancode->keycode table
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300205 * @dev: the struct input_dev device descriptor
206 * @scancode: the desired scancode
David Härdemanb3074c02010-04-02 15:58:28 -0300207 * @keycode: used to return the keycode, if found, or KEY_RESERVED
208 * @return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300209 *
210 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300211 */
212static int ir_getkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800213 unsigned int scancode, unsigned int *keycode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300214{
David Härdemanb3074c02010-04-02 15:58:28 -0300215 int start, end, mid;
216 unsigned long flags;
217 int key = KEY_RESERVED;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300218 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
219 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300220
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300221 spin_lock_irqsave(&rc_tab->lock, flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300222 start = 0;
223 end = rc_tab->len - 1;
224 while (start <= end) {
225 mid = (start + end) / 2;
226 if (rc_tab->scan[mid].scancode < scancode)
227 start = mid + 1;
228 else if (rc_tab->scan[mid].scancode > scancode)
229 end = mid - 1;
230 else {
231 key = rc_tab->scan[mid].keycode;
232 break;
233 }
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300234 }
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300235 spin_unlock_irqrestore(&rc_tab->lock, flags);
236
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300237 if (key == KEY_RESERVED)
238 IR_dprintk(1, "unknown key for scancode 0x%04x\n",
239 scancode);
240
David Härdemanb3074c02010-04-02 15:58:28 -0300241 *keycode = key;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300242 return 0;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300243}
244
245/**
246 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300247 * @input_dev: the struct input_dev descriptor of the device
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300248 * @scancode: the scancode that we're seeking
249 *
250 * This routine is used by the input routines when a key is pressed at the
251 * IR. The scancode is received and needs to be converted into a keycode.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300252 * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300253 * corresponding keycode from the table.
254 */
255u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
256{
David Härdemanb3074c02010-04-02 15:58:28 -0300257 int keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300258
David Härdemanb3074c02010-04-02 15:58:28 -0300259 ir_getkeycode(dev, scancode, &keycode);
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300260 if (keycode != KEY_RESERVED)
261 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
262 dev->name, scancode, keycode);
David Härdemanb3074c02010-04-02 15:58:28 -0300263 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300264}
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -0300265EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300266
267/**
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300268 * ir_keyup() - generates input event to cleanup a key press
David Härdemana374fef2010-04-02 15:58:29 -0300269 * @ir: the struct ir_input_dev descriptor of the device
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300270 *
David Härdemana374fef2010-04-02 15:58:29 -0300271 * This routine is used to signal that a key has been released on the
272 * remote control. It reports a keyup input event via input_report_key().
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300273 */
David Härdemana374fef2010-04-02 15:58:29 -0300274static void ir_keyup(struct ir_input_dev *ir)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300275{
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300276 if (!ir->keypressed)
277 return;
278
David Härdemana374fef2010-04-02 15:58:29 -0300279 IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
280 input_report_key(ir->input_dev, ir->last_keycode, 0);
281 input_sync(ir->input_dev);
282 ir->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300283}
David Härdemana374fef2010-04-02 15:58:29 -0300284
285/**
286 * ir_timer_keyup() - generates a keyup event after a timeout
287 * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
288 *
289 * This routine will generate a keyup event some time after a keydown event
290 * is generated when no further activity has been detected.
291 */
292static void ir_timer_keyup(unsigned long cookie)
293{
294 struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
295 unsigned long flags;
296
297 /*
298 * ir->keyup_jiffies is used to prevent a race condition if a
299 * hardware interrupt occurs at this point and the keyup timer
300 * event is moved further into the future as a result.
301 *
302 * The timer will then be reactivated and this function called
303 * again in the future. We need to exit gracefully in that case
304 * to allow the input subsystem to do its auto-repeat magic or
305 * a keyup event might follow immediately after the keydown.
306 */
307 spin_lock_irqsave(&ir->keylock, flags);
308 if (time_is_after_eq_jiffies(ir->keyup_jiffies))
309 ir_keyup(ir);
310 spin_unlock_irqrestore(&ir->keylock, flags);
311}
312
313/**
314 * ir_repeat() - notifies the IR core that a key is still pressed
315 * @dev: the struct input_dev descriptor of the device
316 *
317 * This routine is used by IR decoders when a repeat message which does
318 * not include the necessary bits to reproduce the scancode has been
319 * received.
320 */
321void ir_repeat(struct input_dev *dev)
322{
323 unsigned long flags;
324 struct ir_input_dev *ir = input_get_drvdata(dev);
325
326 spin_lock_irqsave(&ir->keylock, flags);
327
328 if (!ir->keypressed)
329 goto out;
330
331 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
332 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
333
334out:
335 spin_unlock_irqrestore(&ir->keylock, flags);
336}
337EXPORT_SYMBOL_GPL(ir_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300338
339/**
340 * ir_keydown() - generates input event for a key press
David Härdemana374fef2010-04-02 15:58:29 -0300341 * @dev: the struct input_dev descriptor of the device
342 * @scancode: the scancode that we're seeking
343 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
344 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300345 *
346 * This routine is used by the input routines when a key is pressed at the
347 * IR. It gets the keycode for a scancode and reports an input event via
348 * input_report_key().
349 */
David Härdemana374fef2010-04-02 15:58:29 -0300350void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300351{
David Härdemana374fef2010-04-02 15:58:29 -0300352 unsigned long flags;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300353 struct ir_input_dev *ir = input_get_drvdata(dev);
354
355 u32 keycode = ir_g_keycode_from_table(dev, scancode);
356
David Härdemana374fef2010-04-02 15:58:29 -0300357 spin_lock_irqsave(&ir->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300358
David Härdemana374fef2010-04-02 15:58:29 -0300359 /* Repeat event? */
360 if (ir->keypressed &&
361 ir->last_scancode == scancode &&
362 ir->last_toggle == toggle)
363 goto set_timer;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300364
David Härdemana374fef2010-04-02 15:58:29 -0300365 /* Release old keypress */
366 ir_keyup(ir);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300367
David Härdemana374fef2010-04-02 15:58:29 -0300368 ir->last_scancode = scancode;
369 ir->last_toggle = toggle;
370 ir->last_keycode = keycode;
371
372 if (keycode == KEY_RESERVED)
373 goto out;
374
375 /* Register a keypress */
376 ir->keypressed = true;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300377 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
David Härdemana374fef2010-04-02 15:58:29 -0300378 dev->name, keycode, scancode);
379 input_report_key(dev, ir->last_keycode, 1);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300380 input_sync(dev);
381
David Härdemana374fef2010-04-02 15:58:29 -0300382set_timer:
383 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
384 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
385out:
386 spin_unlock_irqrestore(&ir->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300387}
388EXPORT_SYMBOL_GPL(ir_keydown);
389
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300390static int ir_open(struct input_dev *input_dev)
391{
392 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
393
394 return ir_dev->props->open(ir_dev->props->priv);
395}
396
397static void ir_close(struct input_dev *input_dev)
398{
399 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
400
401 ir_dev->props->close(ir_dev->props->priv);
402}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300403
404/**
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300405 * __ir_input_register() - sets the IR keycode table and add the handlers
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300406 * for keymap table get/set
407 * @input_dev: the struct input_dev descriptor of the device
408 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
409 *
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300410 * This routine is used to initialize the input infrastructure
411 * to work with an IR.
412 * It will register the input/evdev interface for the device and
413 * register the syfs code for IR class
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300414 */
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300415int __ir_input_register(struct input_dev *input_dev,
Mauro Carvalho Chehabe93854d2009-12-14 00:16:55 -0300416 const struct ir_scancode_table *rc_tab,
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -0300417 const struct ir_dev_props *props,
418 const char *driver_name)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300419{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300420 struct ir_input_dev *ir_dev;
David Härdemanb3074c02010-04-02 15:58:28 -0300421 int rc;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300422
423 if (rc_tab->scan == NULL || !rc_tab->size)
424 return -EINVAL;
425
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300426 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
427 if (!ir_dev)
428 return -ENOMEM;
429
David Härdemanb3074c02010-04-02 15:58:28 -0300430 ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
431 if (!ir_dev->driver_name) {
432 rc = -ENOMEM;
433 goto out_dev;
Alexander Beregalov82311522010-01-09 13:51:14 -0300434 }
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300435
David Härdemanb3074c02010-04-02 15:58:28 -0300436 input_dev->getkeycode = ir_getkeycode;
437 input_dev->setkeycode = ir_setkeycode;
438 input_set_drvdata(input_dev, ir_dev);
David Härdemana374fef2010-04-02 15:58:29 -0300439 ir_dev->input_dev = input_dev;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300440
David Härdemanb3074c02010-04-02 15:58:28 -0300441 spin_lock_init(&ir_dev->rc_tab.lock);
David Härdemana374fef2010-04-02 15:58:29 -0300442 spin_lock_init(&ir_dev->keylock);
443 setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
444
David Härdemanb3074c02010-04-02 15:58:28 -0300445 ir_dev->rc_tab.name = rc_tab->name;
446 ir_dev->rc_tab.ir_type = rc_tab->ir_type;
447 ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size *
448 sizeof(struct ir_scancode));
449 ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL);
450 ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode);
451
452 if (!ir_dev->rc_tab.scan) {
453 rc = -ENOMEM;
454 goto out_name;
455 }
456
457 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
458 ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
459
460 set_bit(EV_KEY, input_dev->evbit);
David Härdemana374fef2010-04-02 15:58:29 -0300461 set_bit(EV_REP, input_dev->evbit);
462
David Härdemanb3074c02010-04-02 15:58:28 -0300463 if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
464 rc = -ENOMEM;
465 goto out_table;
466 }
467
Mauro Carvalho Chehabe93854d2009-12-14 00:16:55 -0300468 ir_dev->props = props;
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300469 if (props && props->open)
470 input_dev->open = ir_open;
471 if (props && props->close)
472 input_dev->close = ir_close;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300473
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300474 rc = ir_register_class(input_dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300475 if (rc < 0)
David Härdemanb3074c02010-04-02 15:58:28 -0300476 goto out_table;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300477
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300478 IR_dprintk(1, "Registered input device on %s for %s remote.\n",
479 driver_name, rc_tab->name);
480
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300481 return 0;
482
David Härdemanb3074c02010-04-02 15:58:28 -0300483out_table:
484 kfree(ir_dev->rc_tab.scan);
485out_name:
486 kfree(ir_dev->driver_name);
487out_dev:
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300488 kfree(ir_dev);
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300489 return rc;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300490}
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300491EXPORT_SYMBOL_GPL(__ir_input_register);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300492
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300493/**
494 * ir_input_unregister() - unregisters IR and frees resources
495 * @input_dev: the struct input_dev descriptor of the device
496
497 * This routine is used to free memory and de-register interfaces.
498 */
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300499void ir_input_unregister(struct input_dev *dev)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300500{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300501 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300502 struct ir_scancode_table *rc_tab;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300503
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300504 if (!ir_dev)
Mauro Carvalho Chehab05395a32009-12-06 08:32:49 -0300505 return;
506
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300507 IR_dprintk(1, "Freed keycode table\n");
David Härdemana374fef2010-04-02 15:58:29 -0300508 del_timer_sync(&ir_dev->timer_keyup);
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300509 rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300510 rc_tab->size = 0;
511 kfree(rc_tab->scan);
512 rc_tab->scan = NULL;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300513
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300514 ir_unregister_class(dev);
515
David Härdemanb3074c02010-04-02 15:58:28 -0300516 kfree(ir_dev->driver_name);
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300517 kfree(ir_dev);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300518}
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300519EXPORT_SYMBOL_GPL(ir_input_unregister);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300520
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -0300521int ir_core_debug; /* ir_debug level (0,1,2) */
522EXPORT_SYMBOL_GPL(ir_core_debug);
523module_param_named(debug, ir_core_debug, int, 0644);
524
525MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
526MODULE_LICENSE("GPL");