18 #include <linux/input.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include <linux/module.h>
25 #define IR_TAB_MIN_SIZE 256
26 #define IR_TAB_MAX_SIZE 8192
29 #define IR_KEYPRESS_TIMEOUT 250
39 spin_lock(&rc_map_lock);
42 spin_unlock(&rc_map_lock);
46 spin_unlock(&rc_map_lock);
56 map = seek_rc_map(name);
59 int rc = request_module(name);
66 map = seek_rc_map(name);
82 spin_lock(&rc_map_lock);
84 spin_unlock(&rc_map_lock);
91 spin_lock(&rc_map_lock);
93 spin_unlock(&rc_map_lock);
123 const char *name,
u64 rc_type,
size_t size)
133 IR_dprintk(1,
"Allocated space for %u keycode entries (%u bytes)\n",
145 static void ir_free_table(
struct rc_map *rc_map)
161 static int ir_resize_table(
struct rc_map *rc_map,
gfp_t gfp_flags)
163 unsigned int oldalloc = rc_map->
alloc;
164 unsigned int newalloc = oldalloc;
168 if (rc_map->
size == rc_map->
len) {
174 IR_dprintk(1,
"Growing table to %u bytes\n", newalloc);
180 IR_dprintk(1,
"Shrinking table to %u bytes\n", newalloc);
183 if (newalloc == oldalloc)
186 newscan =
kmalloc(newalloc, gfp_flags);
188 IR_dprintk(1,
"Failed to kmalloc %u bytes\n", newalloc);
193 rc_map->
scan = newscan;
194 rc_map->
alloc = newalloc;
211 static unsigned int ir_update_mapping(
struct rc_dev *
dev,
212 struct rc_map *rc_map,
214 unsigned int new_keycode)
216 int old_keycode = rc_map->
scan[
index].keycode;
222 index, rc_map->
scan[index].scancode);
227 IR_dprintk(1,
"#%d: %s scan 0x%04x with key 0x%04x\n",
230 rc_map->
scan[index].scancode, new_keycode);
231 rc_map->
scan[
index].keycode = new_keycode;
239 for (i = 0; i < rc_map->
len; i++) {
240 if (rc_map->
scan[i].keycode == old_keycode) {
267 static unsigned int ir_establish_scancode(
struct rc_dev *dev,
268 struct rc_map *rc_map,
286 for (i = 0; i < rc_map->
len; i++) {
287 if (rc_map->
scan[i].scancode == scancode)
291 if (rc_map->
scan[i].scancode >= scancode)
296 if (rc_map->
size == rc_map->
len) {
297 if (!resize || ir_resize_table(rc_map,
GFP_ATOMIC))
321 static int ir_setkeycode(
struct input_dev *
idev,
323 unsigned int *old_keycode)
325 struct rc_dev *
rdev = input_get_drvdata(idev);
326 struct rc_map *rc_map = &rdev->
rc_map;
328 unsigned int scancode;
336 if (index >= rc_map->
len) {
341 retval = input_scancode_to_scalar(ke, &scancode);
345 index = ir_establish_scancode(rdev, rc_map, scancode,
true);
346 if (index >= rc_map->
len) {
352 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->
keycode);
355 spin_unlock_irqrestore(&rc_map->
lock, flags);
368 static int ir_setkeytable(
struct rc_dev *dev,
369 const struct rc_map *
from)
371 struct rc_map *rc_map = &dev->
rc_map;
375 rc = ir_create_table(rc_map, from->
name,
380 IR_dprintk(1,
"Allocated space for %u keycode entries (%u bytes)\n",
383 for (i = 0; i < from->
size; i++) {
384 index = ir_establish_scancode(dev, rc_map,
385 from->
scan[i].scancode,
false);
386 if (index >= rc_map->
len) {
391 ir_update_mapping(dev, rc_map, index,
392 from->
scan[i].keycode);
396 ir_free_table(rc_map);
410 static unsigned int ir_lookup_by_scancode(
const struct rc_map *rc_map,
411 unsigned int scancode)
414 int end = rc_map->
len - 1;
417 while (start <= end) {
418 mid = (start +
end) / 2;
419 if (rc_map->
scan[mid].scancode < scancode)
421 else if (rc_map->
scan[mid].scancode > scancode)
439 static int ir_getkeycode(
struct input_dev *idev,
442 struct rc_dev *rdev = input_get_drvdata(idev);
443 struct rc_map *rc_map = &rdev->
rc_map;
455 retval = input_scancode_to_scalar(ke, &scancode);
459 index = ir_lookup_by_scancode(rc_map, scancode);
462 if (index < rc_map->len) {
486 spin_unlock_irqrestore(&rc_map->
lock, flags);
502 struct rc_map *rc_map = &dev->
rc_map;
509 index = ir_lookup_by_scancode(rc_map, scancode);
510 keycode = index < rc_map->
len ?
513 spin_unlock_irqrestore(&rc_map->
lock, flags);
516 IR_dprintk(1,
"%s: scancode 0x%04x keycode 0x%02x\n",
531 static void ir_do_keyup(
struct rc_dev *dev,
bool sync)
555 ir_do_keyup(dev,
true);
556 spin_unlock_irqrestore(&dev->
keylock, flags);
567 static void ir_timer_keyup(
unsigned long cookie)
584 ir_do_keyup(dev,
true);
585 spin_unlock_irqrestore(&dev->
keylock, flags);
612 spin_unlock_irqrestore(&dev->
keylock, flags);
626 static void ir_do_keydown(
struct rc_dev *dev,
int scancode,
634 ir_do_keyup(dev,
false);
646 "key 0x%04x, scancode 0x%04x\n",
648 input_report_key(dev->
input_dev, keycode, 1);
670 ir_do_keydown(dev, scancode, keycode, toggle);
676 spin_unlock_irqrestore(&dev->
keylock, flags);
697 ir_do_keydown(dev, scancode, keycode, toggle);
698 spin_unlock_irqrestore(&dev->
keylock, flags);
702 static int ir_open(
struct input_dev *idev)
704 struct rc_dev *rdev = input_get_drvdata(idev);
706 return rdev->
open(rdev);
709 static void ir_close(
struct input_dev *idev)
711 struct rc_dev *rdev = input_get_drvdata(idev);
723 static struct class ir_input_class = {
725 .devnode = ir_devnode,
745 #define PROTO_NONE "none"
776 enabled = dev->
rc_map.rc_type;
778 }
else if (dev->
raw) {
779 enabled = dev->
raw->enabled_protocols;
784 IR_dprintk(1,
"allowed - 0x%llx, enabled - 0x%llx\n",
788 for (i = 0; i <
ARRAY_SIZE(proto_names); i++) {
789 if (allowed & enabled & proto_names[i].
type)
790 tmp +=
sprintf(tmp,
"[%s] ", proto_names[i].name);
791 else if (allowed & proto_names[i].type)
792 tmp +=
sprintf(tmp,
"%s ", proto_names[i].name);
801 return tmp + 1 -
buf;
844 type = dev->
rc_map.rc_type;
846 type = dev->
raw->enabled_protocols;
848 IR_dprintk(1,
"Protocol switching not supported\n");
853 while ((tmp =
strsep((
char **) &data,
" \n")) !=
NULL) {
861 }
else if (*tmp ==
'-') {
875 for (i = 0; i <
ARRAY_SIZE(proto_names); i++) {
877 tmp +=
strlen(proto_names[i].name);
878 mask = proto_names[
i].type;
883 IR_dprintk(1,
"Unknown protocol: '%s'\n", tmp);
907 IR_dprintk(1,
"Error setting protocols to 0x%llx\n",
917 spin_unlock_irqrestore(&dev->
rc_map.lock, flags);
919 dev->
raw->enabled_protocols =
type;
922 IR_dprintk(1,
"Current protocol(s): 0x%llx\n",
932 static void rc_dev_release(
struct device *device)
936 #define ADD_HOTPLUG_VAR(fmt, val...) \
938 int err = add_uevent_var(env, fmt, val); \
962 show_protocols, store_protocols);
964 static struct attribute *rc_dev_attrs[] = {
965 &dev_attr_protocols.attr,
970 .attrs = rc_dev_attrs,
979 .groups = rc_dev_attr_groups,
980 .release = rc_dev_release,
981 .uevent = rc_dev_uevent,
992 dev->
input_dev = input_allocate_device();
998 dev->
input_dev->getkeycode = ir_getkeycode;
999 dev->
input_dev->setkeycode = ir_setkeycode;
1007 dev->dev.type = &rc_dev_type;
1008 dev->dev.class = &ir_input_class;
1033 static bool raw_init =
false;
1035 struct rc_map *rc_map;
1045 if (!rc_map || !rc_map->
scan || rc_map->
size == 0)
1073 rc = ir_setkeytable(dev, rc_map);
1081 rc = input_register_device(dev->
input_dev);
1102 dev_name(&dev->
dev),
1104 path ? path :
"N/A");
1127 IR_dprintk(1,
"Registered rc%ld (driver: %s, remote: %s, mode %s)\n",
1130 rc_map->
name ? rc_map->
name :
"unknown",
1139 input_unregister_device(dev->
input_dev);
1142 ir_free_table(&dev->
rc_map);
1162 ir_free_table(&dev->
rc_map);
1165 input_unregister_device(dev->
input_dev);
1179 static int __init rc_core_init(
void)
1192 static void __exit rc_core_exit(
void)