]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/media/rc/rc-main.c
67a6bd5d94978e83f6bce2033ba8b67b4e850b53
[linux-beck.git] / drivers / media / rc / rc-main.c
1 /* rc-core.c - handle IR scancode->keycode tables
2  *
3  * Copyright (C) 2009-2010 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 #include <media/ir-core.h>
16 #include <linux/spinlock.h>
17 #include <linux/delay.h>
18 #include <linux/input.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include "rc-core-priv.h"
22
23 #define IRRCV_NUM_DEVICES       256
24
25 /* bit array to represent IR sysfs device number */
26 static unsigned long ir_core_dev_number;
27
28 /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
29 #define IR_TAB_MIN_SIZE 256
30 #define IR_TAB_MAX_SIZE 8192
31
32 /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
33 #define IR_KEYPRESS_TIMEOUT 250
34
35 /* Used to keep track of known keymaps */
36 static LIST_HEAD(rc_map_list);
37 static DEFINE_SPINLOCK(rc_map_lock);
38
39 /* Forward declarations */
40 static int ir_register_class(struct input_dev *input_dev);
41 static void ir_unregister_class(struct input_dev *input_dev);
42 static int ir_register_input(struct input_dev *input_dev);
43
44
45 static struct rc_keymap *seek_rc_map(const char *name)
46 {
47         struct rc_keymap *map = NULL;
48
49         spin_lock(&rc_map_lock);
50         list_for_each_entry(map, &rc_map_list, list) {
51                 if (!strcmp(name, map->map.name)) {
52                         spin_unlock(&rc_map_lock);
53                         return map;
54                 }
55         }
56         spin_unlock(&rc_map_lock);
57
58         return NULL;
59 }
60
61 struct ir_scancode_table *get_rc_map(const char *name)
62 {
63
64         struct rc_keymap *map;
65
66         map = seek_rc_map(name);
67 #ifdef MODULE
68         if (!map) {
69                 int rc = request_module(name);
70                 if (rc < 0) {
71                         printk(KERN_ERR "Couldn't load IR keymap %s\n", name);
72                         return NULL;
73                 }
74                 msleep(20);     /* Give some time for IR to register */
75
76                 map = seek_rc_map(name);
77         }
78 #endif
79         if (!map) {
80                 printk(KERN_ERR "IR keymap %s not found\n", name);
81                 return NULL;
82         }
83
84         printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
85
86         return &map->map;
87 }
88 EXPORT_SYMBOL_GPL(get_rc_map);
89
90 int ir_register_map(struct rc_keymap *map)
91 {
92         spin_lock(&rc_map_lock);
93         list_add_tail(&map->list, &rc_map_list);
94         spin_unlock(&rc_map_lock);
95         return 0;
96 }
97 EXPORT_SYMBOL_GPL(ir_register_map);
98
99 void ir_unregister_map(struct rc_keymap *map)
100 {
101         spin_lock(&rc_map_lock);
102         list_del(&map->list);
103         spin_unlock(&rc_map_lock);
104 }
105 EXPORT_SYMBOL_GPL(ir_unregister_map);
106
107
108 static struct ir_scancode empty[] = {
109         { 0x2a, KEY_COFFEE },
110 };
111
112 static struct rc_keymap empty_map = {
113         .map = {
114                 .scan    = empty,
115                 .size    = ARRAY_SIZE(empty),
116                 .ir_type = IR_TYPE_UNKNOWN,     /* Legacy IR type */
117                 .name    = RC_MAP_EMPTY,
118         }
119 };
120
121 /**
122  * ir_create_table() - initializes a scancode table
123  * @rc_tab:     the ir_scancode_table to initialize
124  * @name:       name to assign to the table
125  * @ir_type:    ir type to assign to the new table
126  * @size:       initial size of the table
127  * @return:     zero on success or a negative error code
128  *
129  * This routine will initialize the ir_scancode_table and will allocate
130  * memory to hold at least the specified number elements.
131  */
132 static int ir_create_table(struct ir_scancode_table *rc_tab,
133                            const char *name, u64 ir_type, size_t size)
134 {
135         rc_tab->name = name;
136         rc_tab->ir_type = ir_type;
137         rc_tab->alloc = roundup_pow_of_two(size * sizeof(struct ir_scancode));
138         rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
139         rc_tab->scan = kmalloc(rc_tab->alloc, GFP_KERNEL);
140         if (!rc_tab->scan)
141                 return -ENOMEM;
142
143         IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
144                    rc_tab->size, rc_tab->alloc);
145         return 0;
146 }
147
148 /**
149  * ir_free_table() - frees memory allocated by a scancode table
150  * @rc_tab:     the table whose mappings need to be freed
151  *
152  * This routine will free memory alloctaed for key mappings used by given
153  * scancode table.
154  */
155 static void ir_free_table(struct ir_scancode_table *rc_tab)
156 {
157         rc_tab->size = 0;
158         kfree(rc_tab->scan);
159         rc_tab->scan = NULL;
160 }
161
162 /**
163  * ir_resize_table() - resizes a scancode table if necessary
164  * @rc_tab:     the ir_scancode_table to resize
165  * @gfp_flags:  gfp flags to use when allocating memory
166  * @return:     zero on success or a negative error code
167  *
168  * This routine will shrink the ir_scancode_table if it has lots of
169  * unused entries and grow it if it is full.
170  */
171 static int ir_resize_table(struct ir_scancode_table *rc_tab, gfp_t gfp_flags)
172 {
173         unsigned int oldalloc = rc_tab->alloc;
174         unsigned int newalloc = oldalloc;
175         struct ir_scancode *oldscan = rc_tab->scan;
176         struct ir_scancode *newscan;
177
178         if (rc_tab->size == rc_tab->len) {
179                 /* All entries in use -> grow keytable */
180                 if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
181                         return -ENOMEM;
182
183                 newalloc *= 2;
184                 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
185         }
186
187         if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
188                 /* Less than 1/3 of entries in use -> shrink keytable */
189                 newalloc /= 2;
190                 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
191         }
192
193         if (newalloc == oldalloc)
194                 return 0;
195
196         newscan = kmalloc(newalloc, gfp_flags);
197         if (!newscan) {
198                 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
199                 return -ENOMEM;
200         }
201
202         memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
203         rc_tab->scan = newscan;
204         rc_tab->alloc = newalloc;
205         rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
206         kfree(oldscan);
207         return 0;
208 }
209
210 /**
211  * ir_update_mapping() - set a keycode in the scancode->keycode table
212  * @dev:        the struct input_dev device descriptor
213  * @rc_tab:     scancode table to be adjusted
214  * @index:      index of the mapping that needs to be updated
215  * @keycode:    the desired keycode
216  * @return:     previous keycode assigned to the mapping
217  *
218  * This routine is used to update scancode->keycopde mapping at given
219  * position.
220  */
221 static unsigned int ir_update_mapping(struct input_dev *dev,
222                                       struct ir_scancode_table *rc_tab,
223                                       unsigned int index,
224                                       unsigned int new_keycode)
225 {
226         int old_keycode = rc_tab->scan[index].keycode;
227         int i;
228
229         /* Did the user wish to remove the mapping? */
230         if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
231                 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
232                            index, rc_tab->scan[index].scancode);
233                 rc_tab->len--;
234                 memmove(&rc_tab->scan[index], &rc_tab->scan[index+ 1],
235                         (rc_tab->len - index) * sizeof(struct ir_scancode));
236         } else {
237                 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
238                            index,
239                            old_keycode == KEY_RESERVED ? "New" : "Replacing",
240                            rc_tab->scan[index].scancode, new_keycode);
241                 rc_tab->scan[index].keycode = new_keycode;
242                 __set_bit(new_keycode, dev->keybit);
243         }
244
245         if (old_keycode != KEY_RESERVED) {
246                 /* A previous mapping was updated... */
247                 __clear_bit(old_keycode, dev->keybit);
248                 /* ... but another scancode might use the same keycode */
249                 for (i = 0; i < rc_tab->len; i++) {
250                         if (rc_tab->scan[i].keycode == old_keycode) {
251                                 __set_bit(old_keycode, dev->keybit);
252                                 break;
253                         }
254                 }
255
256                 /* Possibly shrink the keytable, failure is not a problem */
257                 ir_resize_table(rc_tab, GFP_ATOMIC);
258         }
259
260         return old_keycode;
261 }
262
263 /**
264  * ir_establish_scancode() - set a keycode in the scancode->keycode table
265  * @ir_dev:     the struct ir_input_dev device descriptor
266  * @rc_tab:     scancode table to be searched
267  * @scancode:   the desired scancode
268  * @resize:     controls whether we allowed to resize the table to
269  *              accomodate not yet present scancodes
270  * @return:     index of the mapping containing scancode in question
271  *              or -1U in case of failure.
272  *
273  * This routine is used to locate given scancode in ir_scancode_table.
274  * If scancode is not yet present the routine will allocate a new slot
275  * for it.
276  */
277 static unsigned int ir_establish_scancode(struct ir_input_dev *ir_dev,
278                                           struct ir_scancode_table *rc_tab,
279                                           unsigned int scancode,
280                                           bool resize)
281 {
282         unsigned int i;
283
284         /*
285          * Unfortunately, some hardware-based IR decoders don't provide
286          * all bits for the complete IR code. In general, they provide only
287          * the command part of the IR code. Yet, as it is possible to replace
288          * the provided IR with another one, it is needed to allow loading
289          * IR tables from other remotes. So,
290          */
291         if (ir_dev->props && ir_dev->props->scanmask)
292                 scancode &= ir_dev->props->scanmask;
293
294         /* First check if we already have a mapping for this ir command */
295         for (i = 0; i < rc_tab->len; i++) {
296                 if (rc_tab->scan[i].scancode == scancode)
297                         return i;
298
299                 /* Keytable is sorted from lowest to highest scancode */
300                 if (rc_tab->scan[i].scancode >= scancode)
301                         break;
302         }
303
304         /* No previous mapping found, we might need to grow the table */
305         if (rc_tab->size == rc_tab->len) {
306                 if (!resize || ir_resize_table(rc_tab, GFP_ATOMIC))
307                         return -1U;
308         }
309
310         /* i is the proper index to insert our new keycode */
311         if (i < rc_tab->len)
312                 memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
313                         (rc_tab->len - i) * sizeof(struct ir_scancode));
314         rc_tab->scan[i].scancode = scancode;
315         rc_tab->scan[i].keycode = KEY_RESERVED;
316         rc_tab->len++;
317
318         return i;
319 }
320
321 /**
322  * ir_setkeycode() - set a keycode in the scancode->keycode table
323  * @dev:        the struct input_dev device descriptor
324  * @scancode:   the desired scancode
325  * @keycode:    result
326  * @return:     -EINVAL if the keycode could not be inserted, otherwise zero.
327  *
328  * This routine is used to handle evdev EVIOCSKEY ioctl.
329  */
330 static int ir_setkeycode(struct input_dev *dev,
331                          const struct input_keymap_entry *ke,
332                          unsigned int *old_keycode)
333 {
334         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
335         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
336         unsigned int index;
337         unsigned int scancode;
338         int retval;
339         unsigned long flags;
340
341         spin_lock_irqsave(&rc_tab->lock, flags);
342
343         if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
344                 index = ke->index;
345                 if (index >= rc_tab->len) {
346                         retval = -EINVAL;
347                         goto out;
348                 }
349         } else {
350                 retval = input_scancode_to_scalar(ke, &scancode);
351                 if (retval)
352                         goto out;
353
354                 index = ir_establish_scancode(ir_dev, rc_tab, scancode, true);
355                 if (index >= rc_tab->len) {
356                         retval = -ENOMEM;
357                         goto out;
358                 }
359         }
360
361         *old_keycode = ir_update_mapping(dev, rc_tab, index, ke->keycode);
362
363 out:
364         spin_unlock_irqrestore(&rc_tab->lock, flags);
365         return retval;
366 }
367
368 /**
369  * ir_setkeytable() - sets several entries in the scancode->keycode table
370  * @dev:        the struct input_dev device descriptor
371  * @to:         the struct ir_scancode_table to copy entries to
372  * @from:       the struct ir_scancode_table to copy entries from
373  * @return:     -ENOMEM if all keycodes could not be inserted, otherwise zero.
374  *
375  * This routine is used to handle table initialization.
376  */
377 static int ir_setkeytable(struct ir_input_dev *ir_dev,
378                           const struct ir_scancode_table *from)
379 {
380         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
381         unsigned int i, index;
382         int rc;
383
384         rc = ir_create_table(&ir_dev->rc_tab,
385                              from->name, from->ir_type, from->size);
386         if (rc)
387                 return rc;
388
389         IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
390                    rc_tab->size, rc_tab->alloc);
391
392         for (i = 0; i < from->size; i++) {
393                 index = ir_establish_scancode(ir_dev, rc_tab,
394                                               from->scan[i].scancode, false);
395                 if (index >= rc_tab->len) {
396                         rc = -ENOMEM;
397                         break;
398                 }
399
400                 ir_update_mapping(ir_dev->input_dev, rc_tab, index,
401                                   from->scan[i].keycode);
402         }
403
404         if (rc)
405                 ir_free_table(rc_tab);
406
407         return rc;
408 }
409
410 /**
411  * ir_lookup_by_scancode() - locate mapping by scancode
412  * @rc_tab:     the &struct ir_scancode_table to search
413  * @scancode:   scancode to look for in the table
414  * @return:     index in the table, -1U if not found
415  *
416  * This routine performs binary search in RC keykeymap table for
417  * given scancode.
418  */
419 static unsigned int ir_lookup_by_scancode(const struct ir_scancode_table *rc_tab,
420                                           unsigned int scancode)
421 {
422         int start = 0;
423         int end = rc_tab->len - 1;
424         int mid;
425
426         while (start <= end) {
427                 mid = (start + end) / 2;
428                 if (rc_tab->scan[mid].scancode < scancode)
429                         start = mid + 1;
430                 else if (rc_tab->scan[mid].scancode > scancode)
431                         end = mid - 1;
432                 else
433                         return mid;
434         }
435
436         return -1U;
437 }
438
439 /**
440  * ir_getkeycode() - get a keycode from the scancode->keycode table
441  * @dev:        the struct input_dev device descriptor
442  * @scancode:   the desired scancode
443  * @keycode:    used to return the keycode, if found, or KEY_RESERVED
444  * @return:     always returns zero.
445  *
446  * This routine is used to handle evdev EVIOCGKEY ioctl.
447  */
448 static int ir_getkeycode(struct input_dev *dev,
449                          struct input_keymap_entry *ke)
450 {
451         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
452         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
453         struct ir_scancode *entry;
454         unsigned long flags;
455         unsigned int index;
456         unsigned int scancode;
457         int retval;
458
459         spin_lock_irqsave(&rc_tab->lock, flags);
460
461         if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
462                 index = ke->index;
463         } else {
464                 retval = input_scancode_to_scalar(ke, &scancode);
465                 if (retval)
466                         goto out;
467
468                 index = ir_lookup_by_scancode(rc_tab, scancode);
469         }
470
471         if (index >= rc_tab->len) {
472                 if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
473                         IR_dprintk(1, "unknown key for scancode 0x%04x\n",
474                                    scancode);
475                 retval = -EINVAL;
476                 goto out;
477         }
478
479         entry = &rc_tab->scan[index];
480
481         ke->index = index;
482         ke->keycode = entry->keycode;
483         ke->len = sizeof(entry->scancode);
484         memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
485
486         retval = 0;
487
488 out:
489         spin_unlock_irqrestore(&rc_tab->lock, flags);
490         return retval;
491 }
492
493 /**
494  * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
495  * @input_dev:  the struct input_dev descriptor of the device
496  * @scancode:   the scancode that we're seeking
497  *
498  * This routine is used by the input routines when a key is pressed at the
499  * IR. The scancode is received and needs to be converted into a keycode.
500  * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
501  * corresponding keycode from the table.
502  */
503 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
504 {
505         struct ir_input_dev *ir_dev = input_get_drvdata(dev);
506         struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
507         unsigned int keycode;
508         unsigned int index;
509         unsigned long flags;
510
511         spin_lock_irqsave(&rc_tab->lock, flags);
512
513         index = ir_lookup_by_scancode(rc_tab, scancode);
514         keycode = index < rc_tab->len ?
515                         rc_tab->scan[index].keycode : KEY_RESERVED;
516
517         spin_unlock_irqrestore(&rc_tab->lock, flags);
518
519         if (keycode != KEY_RESERVED)
520                 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
521                            dev->name, scancode, keycode);
522
523         return keycode;
524 }
525 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
526
527 /**
528  * ir_do_keyup() - internal function to signal the release of a keypress
529  * @ir:         the struct ir_input_dev descriptor of the device
530  *
531  * This function is used internally to release a keypress, it must be
532  * called with keylock held.
533  */
534 static void ir_do_keyup(struct ir_input_dev *ir)
535 {
536         if (!ir->keypressed)
537                 return;
538
539         IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
540         input_report_key(ir->input_dev, ir->last_keycode, 0);
541         input_sync(ir->input_dev);
542         ir->keypressed = false;
543 }
544
545 /**
546  * ir_keyup() - generates input event to signal the release of a keypress
547  * @dev:        the struct input_dev descriptor of the device
548  *
549  * This routine is used to signal that a key has been released on the
550  * remote control.
551  */
552 void ir_keyup(struct input_dev *dev)
553 {
554         unsigned long flags;
555         struct ir_input_dev *ir = input_get_drvdata(dev);
556
557         spin_lock_irqsave(&ir->keylock, flags);
558         ir_do_keyup(ir);
559         spin_unlock_irqrestore(&ir->keylock, flags);
560 }
561 EXPORT_SYMBOL_GPL(ir_keyup);
562
563 /**
564  * ir_timer_keyup() - generates a keyup event after a timeout
565  * @cookie:     a pointer to struct ir_input_dev passed to setup_timer()
566  *
567  * This routine will generate a keyup event some time after a keydown event
568  * is generated when no further activity has been detected.
569  */
570 static void ir_timer_keyup(unsigned long cookie)
571 {
572         struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
573         unsigned long flags;
574
575         /*
576          * ir->keyup_jiffies is used to prevent a race condition if a
577          * hardware interrupt occurs at this point and the keyup timer
578          * event is moved further into the future as a result.
579          *
580          * The timer will then be reactivated and this function called
581          * again in the future. We need to exit gracefully in that case
582          * to allow the input subsystem to do its auto-repeat magic or
583          * a keyup event might follow immediately after the keydown.
584          */
585         spin_lock_irqsave(&ir->keylock, flags);
586         if (time_is_before_eq_jiffies(ir->keyup_jiffies))
587                 ir_do_keyup(ir);
588         spin_unlock_irqrestore(&ir->keylock, flags);
589 }
590
591 /**
592  * ir_repeat() - notifies the IR core that a key is still pressed
593  * @dev:        the struct input_dev descriptor of the device
594  *
595  * This routine is used by IR decoders when a repeat message which does
596  * not include the necessary bits to reproduce the scancode has been
597  * received.
598  */
599 void ir_repeat(struct input_dev *dev)
600 {
601         unsigned long flags;
602         struct ir_input_dev *ir = input_get_drvdata(dev);
603
604         spin_lock_irqsave(&ir->keylock, flags);
605
606         input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
607
608         if (!ir->keypressed)
609                 goto out;
610
611         ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
612         mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
613
614 out:
615         spin_unlock_irqrestore(&ir->keylock, flags);
616 }
617 EXPORT_SYMBOL_GPL(ir_repeat);
618
619 /**
620  * ir_do_keydown() - internal function to process a keypress
621  * @dev:        the struct input_dev descriptor of the device
622  * @scancode:   the scancode of the keypress
623  * @keycode:    the keycode of the keypress
624  * @toggle:     the toggle value of the keypress
625  *
626  * This function is used internally to register a keypress, it must be
627  * called with keylock held.
628  */
629 static void ir_do_keydown(struct input_dev *dev, int scancode,
630                           u32 keycode, u8 toggle)
631 {
632         struct ir_input_dev *ir = input_get_drvdata(dev);
633
634         input_event(dev, EV_MSC, MSC_SCAN, scancode);
635
636         /* Repeat event? */
637         if (ir->keypressed &&
638             ir->last_scancode == scancode &&
639             ir->last_toggle == toggle)
640                 return;
641
642         /* Release old keypress */
643         ir_do_keyup(ir);
644
645         ir->last_scancode = scancode;
646         ir->last_toggle = toggle;
647         ir->last_keycode = keycode;
648
649         if (keycode == KEY_RESERVED)
650                 return;
651
652         /* Register a keypress */
653         ir->keypressed = true;
654         IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
655                    dev->name, keycode, scancode);
656         input_report_key(dev, ir->last_keycode, 1);
657         input_sync(dev);
658 }
659
660 /**
661  * ir_keydown() - generates input event for a key press
662  * @dev:        the struct input_dev descriptor of the device
663  * @scancode:   the scancode that we're seeking
664  * @toggle:     the toggle value (protocol dependent, if the protocol doesn't
665  *              support toggle values, this should be set to zero)
666  *
667  * This routine is used by the input routines when a key is pressed at the
668  * IR. It gets the keycode for a scancode and reports an input event via
669  * input_report_key().
670  */
671 void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
672 {
673         unsigned long flags;
674         struct ir_input_dev *ir = input_get_drvdata(dev);
675         u32 keycode = ir_g_keycode_from_table(dev, scancode);
676
677         spin_lock_irqsave(&ir->keylock, flags);
678         ir_do_keydown(dev, scancode, keycode, toggle);
679
680         if (ir->keypressed) {
681                 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
682                 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
683         }
684         spin_unlock_irqrestore(&ir->keylock, flags);
685 }
686 EXPORT_SYMBOL_GPL(ir_keydown);
687
688 /**
689  * ir_keydown_notimeout() - generates input event for a key press without
690  *                          an automatic keyup event at a later time
691  * @dev:        the struct input_dev descriptor of the device
692  * @scancode:   the scancode that we're seeking
693  * @toggle:     the toggle value (protocol dependent, if the protocol doesn't
694  *              support toggle values, this should be set to zero)
695  *
696  * This routine is used by the input routines when a key is pressed at the
697  * IR. It gets the keycode for a scancode and reports an input event via
698  * input_report_key(). The driver must manually call ir_keyup() at a later
699  * stage.
700  */
701 void ir_keydown_notimeout(struct input_dev *dev, int scancode, u8 toggle)
702 {
703         unsigned long flags;
704         struct ir_input_dev *ir = input_get_drvdata(dev);
705         u32 keycode = ir_g_keycode_from_table(dev, scancode);
706
707         spin_lock_irqsave(&ir->keylock, flags);
708         ir_do_keydown(dev, scancode, keycode, toggle);
709         spin_unlock_irqrestore(&ir->keylock, flags);
710 }
711 EXPORT_SYMBOL_GPL(ir_keydown_notimeout);
712
713 static int ir_open(struct input_dev *input_dev)
714 {
715         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
716
717         return ir_dev->props->open(ir_dev->props->priv);
718 }
719
720 static void ir_close(struct input_dev *input_dev)
721 {
722         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
723
724         ir_dev->props->close(ir_dev->props->priv);
725 }
726
727 /**
728  * __ir_input_register() - sets the IR keycode table and add the handlers
729  *                          for keymap table get/set
730  * @input_dev:  the struct input_dev descriptor of the device
731  * @rc_tab:     the struct ir_scancode_table table of scancode/keymap
732  *
733  * This routine is used to initialize the input infrastructure
734  * to work with an IR.
735  * It will register the input/evdev interface for the device and
736  * register the syfs code for IR class
737  */
738 int __ir_input_register(struct input_dev *input_dev,
739                       const struct ir_scancode_table *rc_tab,
740                       struct ir_dev_props *props,
741                       const char *driver_name)
742 {
743         struct ir_input_dev *ir_dev;
744         int rc;
745
746         if (rc_tab->scan == NULL || !rc_tab->size)
747                 return -EINVAL;
748
749         ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
750         if (!ir_dev)
751                 return -ENOMEM;
752
753         ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
754         if (!ir_dev->driver_name) {
755                 rc = -ENOMEM;
756                 goto out_dev;
757         }
758
759         input_dev->getkeycode_new = ir_getkeycode;
760         input_dev->setkeycode_new = ir_setkeycode;
761         input_set_drvdata(input_dev, ir_dev);
762         ir_dev->input_dev = input_dev;
763
764         spin_lock_init(&ir_dev->rc_tab.lock);
765         spin_lock_init(&ir_dev->keylock);
766         setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
767
768         if (props) {
769                 ir_dev->props = props;
770                 if (props->open)
771                         input_dev->open = ir_open;
772                 if (props->close)
773                         input_dev->close = ir_close;
774         }
775
776         set_bit(EV_KEY, input_dev->evbit);
777         set_bit(EV_REP, input_dev->evbit);
778         set_bit(EV_MSC, input_dev->evbit);
779         set_bit(MSC_SCAN, input_dev->mscbit);
780
781         rc = ir_setkeytable(ir_dev, rc_tab);
782         if (rc)
783                 goto out_name;
784
785         rc = ir_register_class(input_dev);
786         if (rc < 0)
787                 goto out_table;
788
789         if (ir_dev->props)
790                 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
791                         rc = ir_raw_event_register(input_dev);
792                         if (rc < 0)
793                                 goto out_event;
794                 }
795
796         rc = ir_register_input(input_dev);
797         if (rc < 0)
798                 goto out_event;
799
800         IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
801                    driver_name, rc_tab->name,
802                    (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
803                         " in raw mode" : "");
804
805         /*
806          * Default delay of 250ms is too short for some protocols, expecially
807          * since the timeout is currently set to 250ms. Increase it to 500ms,
808          * to avoid wrong repetition of the keycodes.
809          */
810         input_dev->rep[REP_DELAY] = 500;
811
812         return 0;
813
814 out_event:
815         ir_unregister_class(input_dev);
816 out_table:
817         ir_free_table(&ir_dev->rc_tab);
818 out_name:
819         kfree(ir_dev->driver_name);
820 out_dev:
821         kfree(ir_dev);
822         return rc;
823 }
824 EXPORT_SYMBOL_GPL(__ir_input_register);
825
826 /**
827  * ir_input_unregister() - unregisters IR and frees resources
828  * @input_dev:  the struct input_dev descriptor of the device
829
830  * This routine is used to free memory and de-register interfaces.
831  */
832 void ir_input_unregister(struct input_dev *input_dev)
833 {
834         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
835
836         if (!ir_dev)
837                 return;
838
839         IR_dprintk(1, "Freed keycode table\n");
840
841         del_timer_sync(&ir_dev->timer_keyup);
842         if (ir_dev->props)
843                 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
844                         ir_raw_event_unregister(input_dev);
845
846         ir_free_table(&ir_dev->rc_tab);
847
848         ir_unregister_class(input_dev);
849
850         kfree(ir_dev->driver_name);
851         kfree(ir_dev);
852 }
853 EXPORT_SYMBOL_GPL(ir_input_unregister);
854
855 /* class for /sys/class/rc */
856 static char *ir_devnode(struct device *dev, mode_t *mode)
857 {
858         return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
859 }
860
861 static struct class ir_input_class = {
862         .name           = "rc",
863         .devnode        = ir_devnode,
864 };
865
866 static struct {
867         u64     type;
868         char    *name;
869 } proto_names[] = {
870         { IR_TYPE_UNKNOWN,      "unknown"       },
871         { IR_TYPE_RC5,          "rc-5"          },
872         { IR_TYPE_NEC,          "nec"           },
873         { IR_TYPE_RC6,          "rc-6"          },
874         { IR_TYPE_JVC,          "jvc"           },
875         { IR_TYPE_SONY,         "sony"          },
876         { IR_TYPE_RC5_SZ,       "rc-5-sz"       },
877         { IR_TYPE_LIRC,         "lirc"          },
878 };
879
880 #define PROTO_NONE      "none"
881
882 /**
883  * show_protocols() - shows the current IR protocol(s)
884  * @d:          the device descriptor
885  * @mattr:      the device attribute struct (unused)
886  * @buf:        a pointer to the output buffer
887  *
888  * This routine is a callback routine for input read the IR protocol type(s).
889  * it is trigged by reading /sys/class/rc/rc?/protocols.
890  * It returns the protocol names of supported protocols.
891  * Enabled protocols are printed in brackets.
892  */
893 static ssize_t show_protocols(struct device *d,
894                               struct device_attribute *mattr, char *buf)
895 {
896         struct ir_input_dev *ir_dev = dev_get_drvdata(d);
897         u64 allowed, enabled;
898         char *tmp = buf;
899         int i;
900
901         /* Device is being removed */
902         if (!ir_dev)
903                 return -EINVAL;
904
905         if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
906                 enabled = ir_dev->rc_tab.ir_type;
907                 allowed = ir_dev->props->allowed_protos;
908         } else if (ir_dev->raw) {
909                 enabled = ir_dev->raw->enabled_protocols;
910                 allowed = ir_raw_get_allowed_protocols();
911         } else
912                 return sprintf(tmp, "[builtin]\n");
913
914         IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
915                    (long long)allowed,
916                    (long long)enabled);
917
918         for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
919                 if (allowed & enabled & proto_names[i].type)
920                         tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
921                 else if (allowed & proto_names[i].type)
922                         tmp += sprintf(tmp, "%s ", proto_names[i].name);
923         }
924
925         if (tmp != buf)
926                 tmp--;
927         *tmp = '\n';
928         return tmp + 1 - buf;
929 }
930
931 /**
932  * store_protocols() - changes the current IR protocol(s)
933  * @d:          the device descriptor
934  * @mattr:      the device attribute struct (unused)
935  * @buf:        a pointer to the input buffer
936  * @len:        length of the input buffer
937  *
938  * This routine is a callback routine for changing the IR protocol type.
939  * It is trigged by writing to /sys/class/rc/rc?/protocols.
940  * Writing "+proto" will add a protocol to the list of enabled protocols.
941  * Writing "-proto" will remove a protocol from the list of enabled protocols.
942  * Writing "proto" will enable only "proto".
943  * Writing "none" will disable all protocols.
944  * Returns -EINVAL if an invalid protocol combination or unknown protocol name
945  * is used, otherwise @len.
946  */
947 static ssize_t store_protocols(struct device *d,
948                                struct device_attribute *mattr,
949                                const char *data,
950                                size_t len)
951 {
952         struct ir_input_dev *ir_dev = dev_get_drvdata(d);
953         bool enable, disable;
954         const char *tmp;
955         u64 type;
956         u64 mask;
957         int rc, i, count = 0;
958         unsigned long flags;
959
960         /* Device is being removed */
961         if (!ir_dev)
962                 return -EINVAL;
963
964         if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
965                 type = ir_dev->rc_tab.ir_type;
966         else if (ir_dev->raw)
967                 type = ir_dev->raw->enabled_protocols;
968         else {
969                 IR_dprintk(1, "Protocol switching not supported\n");
970                 return -EINVAL;
971         }
972
973         while ((tmp = strsep((char **) &data, " \n")) != NULL) {
974                 if (!*tmp)
975                         break;
976
977                 if (*tmp == '+') {
978                         enable = true;
979                         disable = false;
980                         tmp++;
981                 } else if (*tmp == '-') {
982                         enable = false;
983                         disable = true;
984                         tmp++;
985                 } else {
986                         enable = false;
987                         disable = false;
988                 }
989
990                 if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
991                         tmp += sizeof(PROTO_NONE);
992                         mask = 0;
993                         count++;
994                 } else {
995                         for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
996                                 if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
997                                         tmp += strlen(proto_names[i].name);
998                                         mask = proto_names[i].type;
999                                         break;
1000                                 }
1001                         }
1002                         if (i == ARRAY_SIZE(proto_names)) {
1003                                 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1004                                 return -EINVAL;
1005                         }
1006                         count++;
1007                 }
1008
1009                 if (enable)
1010                         type |= mask;
1011                 else if (disable)
1012                         type &= ~mask;
1013                 else
1014                         type = mask;
1015         }
1016
1017         if (!count) {
1018                 IR_dprintk(1, "Protocol not specified\n");
1019                 return -EINVAL;
1020         }
1021
1022         if (ir_dev->props && ir_dev->props->change_protocol) {
1023                 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
1024                                                     type);
1025                 if (rc < 0) {
1026                         IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1027                                    (long long)type);
1028                         return -EINVAL;
1029                 }
1030         }
1031
1032         if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
1033                 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
1034                 ir_dev->rc_tab.ir_type = type;
1035                 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
1036         } else {
1037                 ir_dev->raw->enabled_protocols = type;
1038         }
1039
1040         IR_dprintk(1, "Current protocol(s): 0x%llx\n",
1041                    (long long)type);
1042
1043         return len;
1044 }
1045
1046 #define ADD_HOTPLUG_VAR(fmt, val...)                                    \
1047         do {                                                            \
1048                 int err = add_uevent_var(env, fmt, val);                \
1049                 if (err)                                                \
1050                         return err;                                     \
1051         } while (0)
1052
1053 static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1054 {
1055         struct ir_input_dev *ir_dev = dev_get_drvdata(device);
1056
1057         if (ir_dev->rc_tab.name)
1058                 ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
1059         if (ir_dev->driver_name)
1060                 ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
1061
1062         return 0;
1063 }
1064
1065 /*
1066  * Static device attribute struct with the sysfs attributes for IR's
1067  */
1068 static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
1069                    show_protocols, store_protocols);
1070
1071 static struct attribute *rc_dev_attrs[] = {
1072         &dev_attr_protocols.attr,
1073         NULL,
1074 };
1075
1076 static struct attribute_group rc_dev_attr_grp = {
1077         .attrs  = rc_dev_attrs,
1078 };
1079
1080 static const struct attribute_group *rc_dev_attr_groups[] = {
1081         &rc_dev_attr_grp,
1082         NULL
1083 };
1084
1085 static struct device_type rc_dev_type = {
1086         .groups         = rc_dev_attr_groups,
1087         .uevent         = rc_dev_uevent,
1088 };
1089
1090 /**
1091  * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
1092  * @input_dev:  the struct input_dev descriptor of the device
1093  *
1094  * This routine is used to register the syfs code for IR class
1095  */
1096 static int ir_register_class(struct input_dev *input_dev)
1097 {
1098         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1099         int devno = find_first_zero_bit(&ir_core_dev_number,
1100                                         IRRCV_NUM_DEVICES);
1101
1102         if (unlikely(devno < 0))
1103                 return devno;
1104
1105         ir_dev->dev.type = &rc_dev_type;
1106         ir_dev->devno = devno;
1107
1108         ir_dev->dev.class = &ir_input_class;
1109         ir_dev->dev.parent = input_dev->dev.parent;
1110         input_dev->dev.parent = &ir_dev->dev;
1111         dev_set_name(&ir_dev->dev, "rc%d", devno);
1112         dev_set_drvdata(&ir_dev->dev, ir_dev);
1113         return  device_register(&ir_dev->dev);
1114 };
1115
1116 /**
1117  * ir_register_input - registers ir input device with input subsystem
1118  * @input_dev:  the struct input_dev descriptor of the device
1119  */
1120
1121 static int ir_register_input(struct input_dev *input_dev)
1122 {
1123         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1124         int rc;
1125         const char *path;
1126
1127
1128         rc = input_register_device(input_dev);
1129         if (rc < 0) {
1130                 device_del(&ir_dev->dev);
1131                 return rc;
1132         }
1133
1134         __module_get(THIS_MODULE);
1135
1136         path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
1137         printk(KERN_INFO "%s: %s as %s\n",
1138                 dev_name(&ir_dev->dev),
1139                 input_dev->name ? input_dev->name : "Unspecified device",
1140                 path ? path : "N/A");
1141         kfree(path);
1142
1143         set_bit(ir_dev->devno, &ir_core_dev_number);
1144         return 0;
1145 }
1146
1147 /**
1148  * ir_unregister_class() - removes the sysfs for sysfs for
1149  *                         /sys/class/rc/rc?
1150  * @input_dev:  the struct input_dev descriptor of the device
1151  *
1152  * This routine is used to unregister the syfs code for IR class
1153  */
1154 static void ir_unregister_class(struct input_dev *input_dev)
1155 {
1156         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1157
1158         input_set_drvdata(input_dev, NULL);
1159         clear_bit(ir_dev->devno, &ir_core_dev_number);
1160         input_unregister_device(input_dev);
1161         device_del(&ir_dev->dev);
1162
1163         module_put(THIS_MODULE);
1164 }
1165
1166 /*
1167  * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1168  */
1169
1170 static int __init ir_core_init(void)
1171 {
1172         int rc = class_register(&ir_input_class);
1173         if (rc) {
1174                 printk(KERN_ERR "ir_core: unable to register rc class\n");
1175                 return rc;
1176         }
1177
1178         /* Initialize/load the decoders/keymap code that will be used */
1179         ir_raw_init();
1180         ir_register_map(&empty_map);
1181
1182         return 0;
1183 }
1184
1185 static void __exit ir_core_exit(void)
1186 {
1187         class_unregister(&ir_input_class);
1188         ir_unregister_map(&empty_map);
1189 }
1190
1191 module_init(ir_core_init);
1192 module_exit(ir_core_exit);
1193
1194 int ir_core_debug;    /* ir_debug level (0,1,2) */
1195 EXPORT_SYMBOL_GPL(ir_core_debug);
1196 module_param_named(debug, ir_core_debug, int, 0644);
1197
1198 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1199 MODULE_LICENSE("GPL");