aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/IR/ir-keytable.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/IR/ir-keytable.c')
-rw-r--r--drivers/media/IR/ir-keytable.c687
1 files changed, 367 insertions, 320 deletions
diff --git a/drivers/media/IR/ir-keytable.c b/drivers/media/IR/ir-keytable.c
index bfca26d51827..9374a006f43d 100644
--- a/drivers/media/IR/ir-keytable.c
+++ b/drivers/media/IR/ir-keytable.c
@@ -1,4 +1,4 @@
1/* ir-register.c - handle IR scancode->keycode tables 1/* ir-keytable.c - handle IR scancode->keycode tables
2 * 2 *
3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com> 3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
4 * 4 *
@@ -15,384 +15,408 @@
15 15
16#include <linux/input.h> 16#include <linux/input.h>
17#include <linux/slab.h> 17#include <linux/slab.h>
18#include <media/ir-common.h> 18#include "ir-core-priv.h"
19 19
20#define IR_TAB_MIN_SIZE 32 20/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
21#define IR_TAB_MAX_SIZE 1024 21#define IR_TAB_MIN_SIZE 256
22#define IR_TAB_MAX_SIZE 8192
23
24/* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
25#define IR_KEYPRESS_TIMEOUT 250
22 26
23/** 27/**
24 * ir_seek_table() - returns the element order on the table 28 * ir_resize_table() - resizes a scancode table if necessary
25 * @rc_tab: the ir_scancode_table with the keymap to be used 29 * @rc_tab: the ir_scancode_table to resize
26 * @scancode: the scancode that we're seeking 30 * @return: zero on success or a negative error code
27 * 31 *
28 * This routine is used by the input routines when a key is pressed at the 32 * This routine will shrink the ir_scancode_table if it has lots of
29 * IR. The scancode is received and needs to be converted into a keycode. 33 * unused entries and grow it if it is full.
30 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
31 * corresponding keycode from the table.
32 */ 34 */
33static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode) 35static int ir_resize_table(struct ir_scancode_table *rc_tab)
34{ 36{
35 int rc; 37 unsigned int oldalloc = rc_tab->alloc;
36 unsigned long flags; 38 unsigned int newalloc = oldalloc;
37 struct ir_scancode *keymap = rc_tab->scan; 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;
38 46
39 spin_lock_irqsave(&rc_tab->lock, flags); 47 newalloc *= 2;
48 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
49 }
40 50
41 /* FIXME: replace it by a binary search */ 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 }
42 56
43 for (rc = 0; rc < rc_tab->size; rc++) 57 if (newalloc == oldalloc)
44 if (keymap[rc].scancode == scancode) 58 return 0;
45 goto exit;
46 59
47 /* Not found */ 60 newscan = kmalloc(newalloc, GFP_ATOMIC);
48 rc = -EINVAL; 61 if (!newscan) {
62 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
63 return -ENOMEM;
64 }
49 65
50exit: 66 memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
51 spin_unlock_irqrestore(&rc_tab->lock, flags); 67 rc_tab->scan = newscan;
52 return rc; 68 rc_tab->alloc = newalloc;
69 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
70 kfree(oldscan);
71 return 0;
53} 72}
54 73
55/** 74/**
56 * ir_roundup_tablesize() - gets an optimum value for the table size 75 * ir_do_setkeycode() - internal function to set a keycode in the
57 * @n_elems: minimum number of entries to store keycodes 76 * scancode->keycode table
58 * 77 * @dev: the struct input_dev device descriptor
59 * This routine is used to choose the keycode table size. 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 * @resize: whether the keytable may be shrunk
82 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
60 * 83 *
61 * In order to have some empty space for new keycodes, 84 * This routine is used internally to manipulate the scancode->keycode table.
62 * and knowing in advance that kmalloc allocates only power of two 85 * The caller has to hold @rc_tab->lock.
63 * segments, it optimizes the allocated space to have some spare space
64 * for those new keycodes by using the maximum number of entries that
65 * will be effectively be allocated by kmalloc.
66 * In order to reduce the quantity of table resizes, it has a minimum
67 * table size of IR_TAB_MIN_SIZE.
68 */ 86 */
69static int ir_roundup_tablesize(int n_elems) 87static int ir_do_setkeycode(struct input_dev *dev,
88 struct ir_scancode_table *rc_tab,
89 unsigned scancode, unsigned keycode,
90 bool resize)
70{ 91{
71 size_t size; 92 unsigned int i;
72 93 int old_keycode = KEY_RESERVED;
73 if (n_elems < IR_TAB_MIN_SIZE) 94 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
74 n_elems = IR_TAB_MIN_SIZE;
75 95
76 /* 96 /*
77 * As kmalloc only allocates sizes of power of two, get as 97 * Unfortunately, some hardware-based IR decoders don't provide
78 * much entries as possible for the allocated memory segment 98 * all bits for the complete IR code. In general, they provide only
99 * the command part of the IR code. Yet, as it is possible to replace
100 * the provided IR with another one, it is needed to allow loading
101 * IR tables from other remotes. So,
79 */ 102 */
80 size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode)); 103 if (ir_dev->props && ir_dev->props->scanmask) {
81 n_elems = size / sizeof(struct ir_scancode); 104 scancode &= ir_dev->props->scanmask;
105 }
82 106
83 return n_elems; 107 /* First check if we already have a mapping for this ir command */
108 for (i = 0; i < rc_tab->len; i++) {
109 /* Keytable is sorted from lowest to highest scancode */
110 if (rc_tab->scan[i].scancode > scancode)
111 break;
112 else if (rc_tab->scan[i].scancode < scancode)
113 continue;
114
115 old_keycode = rc_tab->scan[i].keycode;
116 rc_tab->scan[i].keycode = keycode;
117
118 /* Did the user wish to remove the mapping? */
119 if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) {
120 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
121 i, scancode);
122 rc_tab->len--;
123 memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1],
124 (rc_tab->len - i) * sizeof(struct ir_scancode));
125 }
126
127 /* Possibly shrink the keytable, failure is not a problem */
128 ir_resize_table(rc_tab);
129 break;
130 }
131
132 if (old_keycode == KEY_RESERVED && keycode != KEY_RESERVED) {
133 /* No previous mapping found, we might need to grow the table */
134 if (resize && ir_resize_table(rc_tab))
135 return -ENOMEM;
136
137 IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
138 i, scancode, keycode);
139
140 /* i is the proper index to insert our new keycode */
141 memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
142 (rc_tab->len - i) * sizeof(struct ir_scancode));
143 rc_tab->scan[i].scancode = scancode;
144 rc_tab->scan[i].keycode = keycode;
145 rc_tab->len++;
146 set_bit(keycode, dev->keybit);
147 } else {
148 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
149 i, scancode, keycode);
150 /* A previous mapping was updated... */
151 clear_bit(old_keycode, dev->keybit);
152 /* ...but another scancode might use the same keycode */
153 for (i = 0; i < rc_tab->len; i++) {
154 if (rc_tab->scan[i].keycode == old_keycode) {
155 set_bit(old_keycode, dev->keybit);
156 break;
157 }
158 }
159 }
160
161 return 0;
84} 162}
85 163
86/** 164/**
87 * ir_copy_table() - copies a keytable, discarding the unused entries 165 * ir_setkeycode() - set a keycode in the scancode->keycode table
88 * @destin: destin table 166 * @dev: the struct input_dev device descriptor
89 * @origin: origin table 167 * @scancode: the desired scancode
168 * @keycode: result
169 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
90 * 170 *
91 * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED 171 * This routine is used to handle evdev EVIOCSKEY ioctl.
92 * Also copies table size and table protocol.
93 * NOTE: It shouldn't copy the lock field
94 */ 172 */
95 173static int ir_setkeycode(struct input_dev *dev,
96static int ir_copy_table(struct ir_scancode_table *destin, 174 unsigned int scancode, unsigned int keycode)
97 const struct ir_scancode_table *origin)
98{ 175{
99 int i, j = 0; 176 int rc;
100 177 unsigned long flags;
101 for (i = 0; i < origin->size; i++) { 178 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
102 if (origin->scan[i].keycode == KEY_UNKNOWN || 179 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
103 origin->scan[i].keycode == KEY_RESERVED)
104 continue;
105 180
106 memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode)); 181 spin_lock_irqsave(&rc_tab->lock, flags);
107 j++; 182 rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode, true);
108 } 183 spin_unlock_irqrestore(&rc_tab->lock, flags);
109 destin->size = j; 184 return rc;
110 destin->ir_type = origin->ir_type; 185}
111 186
112 IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size); 187/**
188 * ir_setkeytable() - sets several entries in the scancode->keycode table
189 * @dev: the struct input_dev device descriptor
190 * @to: the struct ir_scancode_table to copy entries to
191 * @from: the struct ir_scancode_table to copy entries from
192 * @return: -EINVAL if all keycodes could not be inserted, otherwise zero.
193 *
194 * This routine is used to handle table initialization.
195 */
196static int ir_setkeytable(struct input_dev *dev,
197 struct ir_scancode_table *to,
198 const struct ir_scancode_table *from)
199{
200 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
201 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
202 unsigned long flags;
203 unsigned int i;
204 int rc = 0;
113 205
114 return 0; 206 spin_lock_irqsave(&rc_tab->lock, flags);
207 for (i = 0; i < from->size; i++) {
208 rc = ir_do_setkeycode(dev, to, from->scan[i].scancode,
209 from->scan[i].keycode, false);
210 if (rc)
211 break;
212 }
213 spin_unlock_irqrestore(&rc_tab->lock, flags);
214 return rc;
115} 215}
116 216
117/** 217/**
118 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table 218 * ir_getkeycode() - get a keycode from the scancode->keycode table
119 * @dev: the struct input_dev device descriptor 219 * @dev: the struct input_dev device descriptor
120 * @scancode: the desired scancode 220 * @scancode: the desired scancode
121 * @keycode: the keycode to be retorned. 221 * @keycode: used to return the keycode, if found, or KEY_RESERVED
222 * @return: always returns zero.
122 * 223 *
123 * This routine is used to handle evdev EVIOCGKEY ioctl. 224 * This routine is used to handle evdev EVIOCGKEY ioctl.
124 * If the key is not found, returns -EINVAL, otherwise, returns 0.
125 */ 225 */
126static int ir_getkeycode(struct input_dev *dev, 226static int ir_getkeycode(struct input_dev *dev,
127 unsigned int scancode, unsigned int *keycode) 227 unsigned int scancode, unsigned int *keycode)
128{ 228{
129 int elem; 229 int start, end, mid;
230 unsigned long flags;
231 int key = KEY_RESERVED;
130 struct ir_input_dev *ir_dev = input_get_drvdata(dev); 232 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
131 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; 233 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
132 234
133 elem = ir_seek_table(rc_tab, scancode); 235 spin_lock_irqsave(&rc_tab->lock, flags);
134 if (elem >= 0) { 236 start = 0;
135 *keycode = rc_tab->scan[elem].keycode; 237 end = rc_tab->len - 1;
136 return 0; 238 while (start <= end) {
239 mid = (start + end) / 2;
240 if (rc_tab->scan[mid].scancode < scancode)
241 start = mid + 1;
242 else if (rc_tab->scan[mid].scancode > scancode)
243 end = mid - 1;
244 else {
245 key = rc_tab->scan[mid].keycode;
246 break;
247 }
137 } 248 }
249 spin_unlock_irqrestore(&rc_tab->lock, flags);
138 250
139 /* 251 if (key == KEY_RESERVED)
140 * Scancode not found and table can't be expanded 252 IR_dprintk(1, "unknown key for scancode 0x%04x\n",
141 */ 253 scancode);
142 if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
143 return -EINVAL;
144 254
145 /* 255 *keycode = key;
146 * If is there extra space, returns KEY_RESERVED,
147 * otherwise, input core won't let ir_setkeycode to work
148 */
149 *keycode = KEY_RESERVED;
150 return 0; 256 return 0;
151} 257}
152 258
153/** 259/**
154 * ir_is_resize_needed() - Check if the table needs rezise 260 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
155 * @table: keycode table that may need to resize 261 * @input_dev: the struct input_dev descriptor of the device
156 * @n_elems: minimum number of entries to store keycodes 262 * @scancode: the scancode that we're seeking
157 *
158 * Considering that kmalloc uses power of two storage areas, this
159 * routine detects if the real alloced size will change. If not, it
160 * just returns without doing nothing. Otherwise, it will extend or
161 * reduce the table size to meet the new needs.
162 * 263 *
163 * It returns 0 if no resize is needed, 1 otherwise. 264 * This routine is used by the input routines when a key is pressed at the
265 * IR. The scancode is received and needs to be converted into a keycode.
266 * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
267 * corresponding keycode from the table.
164 */ 268 */
165static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems) 269u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
166{ 270{
167 int cur_size = ir_roundup_tablesize(table->size); 271 int keycode;
168 int new_size = ir_roundup_tablesize(n_elems);
169
170 if (cur_size == new_size)
171 return 0;
172 272
173 /* Resize is needed */ 273 ir_getkeycode(dev, scancode, &keycode);
174 return 1; 274 if (keycode != KEY_RESERVED)
275 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
276 dev->name, scancode, keycode);
277 return keycode;
175} 278}
279EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
176 280
177/** 281/**
178 * ir_delete_key() - remove a keycode from the table 282 * ir_keyup() - generates input event to cleanup a key press
179 * @rc_tab: keycode table 283 * @ir: the struct ir_input_dev descriptor of the device
180 * @elem: element to be removed
181 * 284 *
285 * This routine is used to signal that a key has been released on the
286 * remote control. It reports a keyup input event via input_report_key().
182 */ 287 */
183static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem) 288static void ir_keyup(struct ir_input_dev *ir)
184{ 289{
185 unsigned long flags = 0; 290 if (!ir->keypressed)
186 int newsize = rc_tab->size - 1; 291 return;
187 int resize = ir_is_resize_needed(rc_tab, newsize);
188 struct ir_scancode *oldkeymap = rc_tab->scan;
189 struct ir_scancode *newkeymap = NULL;
190
191 if (resize)
192 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
193 sizeof(*newkeymap), GFP_ATOMIC);
194
195 /* There's no memory for resize. Keep the old table */
196 if (!resize || !newkeymap) {
197 newkeymap = oldkeymap;
198
199 /* We'll modify the live table. Lock it */
200 spin_lock_irqsave(&rc_tab->lock, flags);
201 }
202 292
203 /* 293 IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
204 * Copy the elements before the one that will be deleted 294 input_report_key(ir->input_dev, ir->last_keycode, 0);
205 * if (!resize), both oldkeymap and newkeymap points 295 input_sync(ir->input_dev);
206 * to the same place, so, there's no need to copy 296 ir->keypressed = false;
207 */ 297}
208 if (resize && elem > 0) 298
209 memcpy(newkeymap, oldkeymap, 299/**
210 elem * sizeof(*newkeymap)); 300 * ir_timer_keyup() - generates a keyup event after a timeout
301 * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
302 *
303 * This routine will generate a keyup event some time after a keydown event
304 * is generated when no further activity has been detected.
305 */
306static void ir_timer_keyup(unsigned long cookie)
307{
308 struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
309 unsigned long flags;
211 310
212 /* 311 /*
213 * Copy the other elements overwriting the element to be removed 312 * ir->keyup_jiffies is used to prevent a race condition if a
214 * This operation applies to both resize and non-resize case 313 * hardware interrupt occurs at this point and the keyup timer
314 * event is moved further into the future as a result.
315 *
316 * The timer will then be reactivated and this function called
317 * again in the future. We need to exit gracefully in that case
318 * to allow the input subsystem to do its auto-repeat magic or
319 * a keyup event might follow immediately after the keydown.
215 */ 320 */
216 if (elem < newsize) 321 spin_lock_irqsave(&ir->keylock, flags);
217 memcpy(&newkeymap[elem], &oldkeymap[elem + 1], 322 if (time_is_after_eq_jiffies(ir->keyup_jiffies))
218 (newsize - elem) * sizeof(*newkeymap)); 323 ir_keyup(ir);
219 324 spin_unlock_irqrestore(&ir->keylock, flags);
220 if (resize) {
221 /*
222 * As the copy happened to a temporary table, only here
223 * it needs to lock while replacing the table pointers
224 * to use the new table
225 */
226 spin_lock_irqsave(&rc_tab->lock, flags);
227 rc_tab->size = newsize;
228 rc_tab->scan = newkeymap;
229 spin_unlock_irqrestore(&rc_tab->lock, flags);
230
231 /* Frees the old keytable */
232 kfree(oldkeymap);
233 } else {
234 rc_tab->size = newsize;
235 spin_unlock_irqrestore(&rc_tab->lock, flags);
236 }
237} 325}
238 326
239/** 327/**
240 * ir_insert_key() - insert a keycode at the table 328 * ir_repeat() - notifies the IR core that a key is still pressed
241 * @rc_tab: keycode table 329 * @dev: the struct input_dev descriptor of the device
242 * @scancode: the desired scancode
243 * @keycode: the keycode to be retorned.
244 * 330 *
331 * This routine is used by IR decoders when a repeat message which does
332 * not include the necessary bits to reproduce the scancode has been
333 * received.
245 */ 334 */
246static int ir_insert_key(struct ir_scancode_table *rc_tab, 335void ir_repeat(struct input_dev *dev)
247 int scancode, int keycode)
248{ 336{
249 unsigned long flags; 337 unsigned long flags;
250 int elem = rc_tab->size; 338 struct ir_input_dev *ir = input_get_drvdata(dev);
251 int newsize = rc_tab->size + 1;
252 int resize = ir_is_resize_needed(rc_tab, newsize);
253 struct ir_scancode *oldkeymap = rc_tab->scan;
254 struct ir_scancode *newkeymap;
255
256 if (resize) {
257 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
258 sizeof(*newkeymap), GFP_ATOMIC);
259 if (!newkeymap)
260 return -ENOMEM;
261 339
262 memcpy(newkeymap, oldkeymap, 340 spin_lock_irqsave(&ir->keylock, flags);
263 rc_tab->size * sizeof(*newkeymap));
264 } else
265 newkeymap = oldkeymap;
266 341
267 /* Stores the new code at the table */ 342 if (!ir->keypressed)
268 IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n", 343 goto out;
269 rc_tab->size, scancode, keycode);
270 344
271 spin_lock_irqsave(&rc_tab->lock, flags); 345 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
272 rc_tab->size = newsize; 346 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
273 if (resize) {
274 rc_tab->scan = newkeymap;
275 kfree(oldkeymap);
276 }
277 newkeymap[elem].scancode = scancode;
278 newkeymap[elem].keycode = keycode;
279 spin_unlock_irqrestore(&rc_tab->lock, flags);
280 347
281 return 0; 348out:
349 spin_unlock_irqrestore(&ir->keylock, flags);
282} 350}
351EXPORT_SYMBOL_GPL(ir_repeat);
283 352
284/** 353/**
285 * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table 354 * ir_keydown() - generates input event for a key press
286 * @dev: the struct input_dev device descriptor 355 * @dev: the struct input_dev descriptor of the device
287 * @scancode: the desired scancode 356 * @scancode: the scancode that we're seeking
288 * @keycode: the keycode to be retorned. 357 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
358 * support toggle values, this should be set to zero)
289 * 359 *
290 * This routine is used to handle evdev EVIOCSKEY ioctl. 360 * This routine is used by the input routines when a key is pressed at the
291 * There's one caveat here: how can we increase the size of the table? 361 * IR. It gets the keycode for a scancode and reports an input event via
292 * If the key is not found, returns -EINVAL, otherwise, returns 0. 362 * input_report_key().
293 */ 363 */
294static int ir_setkeycode(struct input_dev *dev, 364void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
295 unsigned int scancode, unsigned int keycode)
296{ 365{
297 int rc = 0;
298 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
299 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
300 struct ir_scancode *keymap = rc_tab->scan;
301 unsigned long flags; 366 unsigned long flags;
367 struct ir_input_dev *ir = input_get_drvdata(dev);
302 368
303 /* 369 u32 keycode = ir_g_keycode_from_table(dev, scancode);
304 * Handle keycode table deletions
305 *
306 * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
307 * deal as a trial to remove an existing scancode attribution
308 * if table become too big, reduce it to save space
309 */
310 if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
311 rc = ir_seek_table(rc_tab, scancode);
312 if (rc < 0)
313 return 0;
314
315 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
316 clear_bit(keymap[rc].keycode, dev->keybit);
317 ir_delete_key(rc_tab, rc);
318
319 return 0;
320 }
321 370
322 /* 371 spin_lock_irqsave(&ir->keylock, flags);
323 * Handle keycode replacements
324 *
325 * If the scancode exists, just replace by the new value
326 */
327 rc = ir_seek_table(rc_tab, scancode);
328 if (rc >= 0) {
329 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
330 rc, scancode, keycode);
331 372
332 clear_bit(keymap[rc].keycode, dev->keybit); 373 /* Repeat event? */
374 if (ir->keypressed &&
375 ir->last_scancode == scancode &&
376 ir->last_toggle == toggle)
377 goto set_timer;
333 378
334 spin_lock_irqsave(&rc_tab->lock, flags); 379 /* Release old keypress */
335 keymap[rc].keycode = keycode; 380 ir_keyup(ir);
336 spin_unlock_irqrestore(&rc_tab->lock, flags);
337 381
338 set_bit(keycode, dev->keybit); 382 ir->last_scancode = scancode;
383 ir->last_toggle = toggle;
384 ir->last_keycode = keycode;
339 385
340 return 0; 386 if (keycode == KEY_RESERVED)
341 } 387 goto out;
342 388
343 /* 389 /* Register a keypress */
344 * Handle new scancode inserts 390 ir->keypressed = true;
345 * 391 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
346 * reallocate table if needed and insert a new keycode 392 dev->name, keycode, scancode);
347 */ 393 input_report_key(dev, ir->last_keycode, 1);
394 input_sync(dev);
348 395
349 /* Avoid growing the table indefinitely */ 396set_timer:
350 if (rc_tab->size + 1 > IR_TAB_MAX_SIZE) 397 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
351 return -EINVAL; 398 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
352 399out:
353 rc = ir_insert_key(rc_tab, scancode, keycode); 400 spin_unlock_irqrestore(&ir->keylock, flags);
354 if (rc < 0)
355 return rc;
356 set_bit(keycode, dev->keybit);
357
358 return 0;
359} 401}
402EXPORT_SYMBOL_GPL(ir_keydown);
360 403
361/** 404static int ir_open(struct input_dev *input_dev)
362 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
363 * @input_dev: the struct input_dev descriptor of the device
364 * @scancode: the scancode that we're seeking
365 *
366 * This routine is used by the input routines when a key is pressed at the
367 * IR. The scancode is received and needs to be converted into a keycode.
368 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
369 * corresponding keycode from the table.
370 */
371u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
372{ 405{
373 struct ir_input_dev *ir_dev = input_get_drvdata(dev); 406 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
374 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
375 struct ir_scancode *keymap = rc_tab->scan;
376 int elem;
377 407
378 elem = ir_seek_table(rc_tab, scancode); 408 return ir_dev->props->open(ir_dev->props->priv);
379 if (elem >= 0) { 409}
380 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
381 dev->name, scancode, keymap[elem].keycode);
382
383 return rc_tab->scan[elem].keycode;
384 }
385 410
386 printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n", 411static void ir_close(struct input_dev *input_dev)
387 dev->name, scancode); 412{
413 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
388 414
389 /* Reports userspace that an unknown keycode were got */ 415 ir_dev->props->close(ir_dev->props->priv);
390 return KEY_RESERVED;
391} 416}
392EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
393 417
394/** 418/**
395 * ir_input_register() - sets the IR keycode table and add the handlers 419 * __ir_input_register() - sets the IR keycode table and add the handlers
396 * for keymap table get/set 420 * for keymap table get/set
397 * @input_dev: the struct input_dev descriptor of the device 421 * @input_dev: the struct input_dev descriptor of the device
398 * @rc_tab: the struct ir_scancode_table table of scancode/keymap 422 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
@@ -402,13 +426,13 @@ EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
402 * It will register the input/evdev interface for the device and 426 * It will register the input/evdev interface for the device and
403 * register the syfs code for IR class 427 * register the syfs code for IR class
404 */ 428 */
405int ir_input_register(struct input_dev *input_dev, 429int __ir_input_register(struct input_dev *input_dev,
406 const struct ir_scancode_table *rc_tab, 430 const struct ir_scancode_table *rc_tab,
407 const struct ir_dev_props *props) 431 const struct ir_dev_props *props,
432 const char *driver_name)
408{ 433{
409 struct ir_input_dev *ir_dev; 434 struct ir_input_dev *ir_dev;
410 struct ir_scancode *keymap = rc_tab->scan; 435 int rc;
411 int i, rc;
412 436
413 if (rc_tab->scan == NULL || !rc_tab->size) 437 if (rc_tab->scan == NULL || !rc_tab->size)
414 return -EINVAL; 438 return -EINVAL;
@@ -417,57 +441,77 @@ int ir_input_register(struct input_dev *input_dev,
417 if (!ir_dev) 441 if (!ir_dev)
418 return -ENOMEM; 442 return -ENOMEM;
419 443
420 spin_lock_init(&ir_dev->rc_tab.lock); 444 ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
421 445 if (!ir_dev->driver_name) {
422 ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size); 446 rc = -ENOMEM;
423 ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size * 447 goto out_dev;
424 sizeof(struct ir_scancode), GFP_KERNEL);
425 if (!ir_dev->rc_tab.scan) {
426 kfree(ir_dev);
427 return -ENOMEM;
428 } 448 }
429 449
430 IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n", 450 input_dev->getkeycode = ir_getkeycode;
431 ir_dev->rc_tab.size, 451 input_dev->setkeycode = ir_setkeycode;
432 ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan)); 452 input_set_drvdata(input_dev, ir_dev);
453 ir_dev->input_dev = input_dev;
433 454
434 ir_copy_table(&ir_dev->rc_tab, rc_tab); 455 spin_lock_init(&ir_dev->rc_tab.lock);
435 ir_dev->props = props; 456 spin_lock_init(&ir_dev->keylock);
457 setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
458
459 ir_dev->rc_tab.name = rc_tab->name;
460 ir_dev->rc_tab.ir_type = rc_tab->ir_type;
461 ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size *
462 sizeof(struct ir_scancode));
463 ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL);
464 ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode);
465 if (props) {
466 ir_dev->props = props;
467 if (props->open)
468 input_dev->open = ir_open;
469 if (props->close)
470 input_dev->close = ir_close;
471 }
436 472
437 /* set the bits for the keys */ 473 if (!ir_dev->rc_tab.scan) {
438 IR_dprintk(1, "key map size: %d\n", rc_tab->size); 474 rc = -ENOMEM;
439 for (i = 0; i < rc_tab->size; i++) { 475 goto out_name;
440 IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
441 i, keymap[i].keycode);
442 set_bit(keymap[i].keycode, input_dev->keybit);
443 } 476 }
444 clear_bit(0, input_dev->keybit); 477
478 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
479 ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
445 480
446 set_bit(EV_KEY, input_dev->evbit); 481 set_bit(EV_KEY, input_dev->evbit);
482 set_bit(EV_REP, input_dev->evbit);
447 483
448 input_dev->getkeycode = ir_getkeycode; 484 if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
449 input_dev->setkeycode = ir_setkeycode; 485 rc = -ENOMEM;
450 input_set_drvdata(input_dev, ir_dev); 486 goto out_table;
487 }
451 488
452 rc = input_register_device(input_dev); 489 rc = ir_register_class(input_dev);
453 if (rc < 0) 490 if (rc < 0)
454 goto err; 491 goto out_table;
455 492
456 rc = ir_register_class(input_dev); 493 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
457 if (rc < 0) { 494 rc = ir_raw_event_register(input_dev);
458 input_unregister_device(input_dev); 495 if (rc < 0)
459 goto err; 496 goto out_event;
460 } 497 }
461 498
499 IR_dprintk(1, "Registered input device on %s for %s remote.\n",
500 driver_name, rc_tab->name);
501
462 return 0; 502 return 0;
463 503
464err: 504out_event:
465 kfree(rc_tab->scan); 505 ir_unregister_class(input_dev);
506out_table:
507 kfree(ir_dev->rc_tab.scan);
508out_name:
509 kfree(ir_dev->driver_name);
510out_dev:
466 kfree(ir_dev); 511 kfree(ir_dev);
467 input_set_drvdata(input_dev, NULL);
468 return rc; 512 return rc;
469} 513}
470EXPORT_SYMBOL_GPL(ir_input_register); 514EXPORT_SYMBOL_GPL(__ir_input_register);
471 515
472/** 516/**
473 * ir_input_unregister() - unregisters IR and frees resources 517 * ir_input_unregister() - unregisters IR and frees resources
@@ -475,9 +519,9 @@ EXPORT_SYMBOL_GPL(ir_input_register);
475 519
476 * This routine is used to free memory and de-register interfaces. 520 * This routine is used to free memory and de-register interfaces.
477 */ 521 */
478void ir_input_unregister(struct input_dev *dev) 522void ir_input_unregister(struct input_dev *input_dev)
479{ 523{
480 struct ir_input_dev *ir_dev = input_get_drvdata(dev); 524 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
481 struct ir_scancode_table *rc_tab; 525 struct ir_scancode_table *rc_tab;
482 526
483 if (!ir_dev) 527 if (!ir_dev)
@@ -485,15 +529,18 @@ void ir_input_unregister(struct input_dev *dev)
485 529
486 IR_dprintk(1, "Freed keycode table\n"); 530 IR_dprintk(1, "Freed keycode table\n");
487 531
532 del_timer_sync(&ir_dev->timer_keyup);
533 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
534 ir_raw_event_unregister(input_dev);
488 rc_tab = &ir_dev->rc_tab; 535 rc_tab = &ir_dev->rc_tab;
489 rc_tab->size = 0; 536 rc_tab->size = 0;
490 kfree(rc_tab->scan); 537 kfree(rc_tab->scan);
491 rc_tab->scan = NULL; 538 rc_tab->scan = NULL;
492 539
493 ir_unregister_class(dev); 540 ir_unregister_class(input_dev);
494 541
542 kfree(ir_dev->driver_name);
495 kfree(ir_dev); 543 kfree(ir_dev);
496 input_unregister_device(dev);
497} 544}
498EXPORT_SYMBOL_GPL(ir_input_unregister); 545EXPORT_SYMBOL_GPL(ir_input_unregister);
499 546