Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 1 | /* ir-register.c - handle IR scancode->keycode tables |
| 2 | * |
| 3 | * Copyright (C) 2009 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 15 | #include <linux/slab.h> |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 16 | #include <linux/input.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <media/ir-core.h> |
| 19 | |
| 20 | #define IRRCV_NUM_DEVICES 256 |
| 21 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 22 | /* bit array to represent IR sysfs device number */ |
| 23 | static unsigned long ir_core_dev_number; |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 24 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 25 | /* class for /sys/class/irrcv */ |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 26 | static struct class *ir_input_class; |
| 27 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 28 | /** |
| 29 | * show_protocol() - shows the current IR protocol |
| 30 | * @d: the device descriptor |
| 31 | * @mattr: the device attribute struct (unused) |
| 32 | * @buf: a pointer to the output buffer |
| 33 | * |
| 34 | * This routine is a callback routine for input read the IR protocol type. |
| 35 | * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol. |
| 36 | * It returns the protocol name, as understood by the driver. |
| 37 | */ |
Mauro Carvalho Chehab | 53f8702 | 2009-12-14 02:16:36 -0300 | [diff] [blame] | 38 | static ssize_t show_protocol(struct device *d, |
| 39 | struct device_attribute *mattr, char *buf) |
| 40 | { |
| 41 | char *s; |
| 42 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
Mauro Carvalho Chehab | 971e829 | 2009-12-14 13:53:37 -0300 | [diff] [blame] | 43 | u64 ir_type = ir_dev->rc_tab.ir_type; |
Mauro Carvalho Chehab | 53f8702 | 2009-12-14 02:16:36 -0300 | [diff] [blame] | 44 | |
Mauro Carvalho Chehab | 971e829 | 2009-12-14 13:53:37 -0300 | [diff] [blame] | 45 | IR_dprintk(1, "Current protocol is %lld\n", (long long)ir_type); |
Mauro Carvalho Chehab | 53f8702 | 2009-12-14 02:16:36 -0300 | [diff] [blame] | 46 | |
| 47 | /* FIXME: doesn't support multiple protocols at the same time */ |
| 48 | if (ir_type == IR_TYPE_UNKNOWN) |
| 49 | s = "Unknown"; |
| 50 | else if (ir_type == IR_TYPE_RC5) |
| 51 | s = "RC-5"; |
| 52 | else if (ir_type == IR_TYPE_PD) |
| 53 | s = "Pulse/distance"; |
| 54 | else if (ir_type == IR_TYPE_NEC) |
| 55 | s = "NEC"; |
| 56 | else |
| 57 | s = "Other"; |
| 58 | |
| 59 | return sprintf(buf, "%s\n", s); |
| 60 | } |
| 61 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 62 | /** |
| 63 | * store_protocol() - shows the current IR protocol |
| 64 | * @d: the device descriptor |
| 65 | * @mattr: the device attribute struct (unused) |
| 66 | * @buf: a pointer to the input buffer |
| 67 | * @len: length of the input buffer |
| 68 | * |
| 69 | * This routine is a callback routine for changing the IR protocol type. |
| 70 | * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol. |
| 71 | * It changes the IR the protocol name, if the IR type is recognized |
| 72 | * by the driver. |
| 73 | * If an unknown protocol name is used, returns -EINVAL. |
| 74 | */ |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 75 | static ssize_t store_protocol(struct device *d, |
| 76 | struct device_attribute *mattr, |
| 77 | const char *data, |
| 78 | size_t len) |
| 79 | { |
| 80 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
Mauro Carvalho Chehab | 971e829 | 2009-12-14 13:53:37 -0300 | [diff] [blame] | 81 | u64 ir_type = IR_TYPE_UNKNOWN; |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 82 | int rc = -EINVAL; |
Mauro Carvalho Chehab | eecee32 | 2009-12-14 02:56:15 -0300 | [diff] [blame] | 83 | unsigned long flags; |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 84 | char *buf; |
| 85 | |
| 86 | buf = strsep((char **) &data, "\n"); |
| 87 | |
| 88 | if (!strcasecmp(buf, "rc-5")) |
| 89 | ir_type = IR_TYPE_RC5; |
| 90 | else if (!strcasecmp(buf, "pd")) |
| 91 | ir_type = IR_TYPE_PD; |
| 92 | else if (!strcasecmp(buf, "nec")) |
| 93 | ir_type = IR_TYPE_NEC; |
| 94 | |
| 95 | if (ir_type == IR_TYPE_UNKNOWN) { |
Mauro Carvalho Chehab | 971e829 | 2009-12-14 13:53:37 -0300 | [diff] [blame] | 96 | IR_dprintk(1, "Error setting protocol to %lld\n", |
| 97 | (long long)ir_type); |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 98 | return -EINVAL; |
| 99 | } |
| 100 | |
Mauro Carvalho Chehab | 0f7ff39 | 2009-12-16 23:46:48 -0300 | [diff] [blame] | 101 | if (ir_dev->props && ir_dev->props->change_protocol) |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 102 | rc = ir_dev->props->change_protocol(ir_dev->props->priv, |
| 103 | ir_type); |
| 104 | |
| 105 | if (rc < 0) { |
Mauro Carvalho Chehab | 971e829 | 2009-12-14 13:53:37 -0300 | [diff] [blame] | 106 | IR_dprintk(1, "Error setting protocol to %lld\n", |
| 107 | (long long)ir_type); |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 108 | return -EINVAL; |
| 109 | } |
| 110 | |
Mauro Carvalho Chehab | eecee32 | 2009-12-14 02:56:15 -0300 | [diff] [blame] | 111 | spin_lock_irqsave(&ir_dev->rc_tab.lock, flags); |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 112 | ir_dev->rc_tab.ir_type = ir_type; |
Mauro Carvalho Chehab | eecee32 | 2009-12-14 02:56:15 -0300 | [diff] [blame] | 113 | spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags); |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 114 | |
Mauro Carvalho Chehab | 971e829 | 2009-12-14 13:53:37 -0300 | [diff] [blame] | 115 | IR_dprintk(1, "Current protocol is %lld\n", |
| 116 | (long long)ir_type); |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 117 | |
| 118 | return len; |
| 119 | } |
| 120 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 121 | /* |
| 122 | * Static device attribute struct with the sysfs attributes for IR's |
| 123 | */ |
Mauro Carvalho Chehab | 53f8702 | 2009-12-14 02:16:36 -0300 | [diff] [blame] | 124 | static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR, |
Mauro Carvalho Chehab | 09b01b9 | 2009-12-14 02:46:42 -0300 | [diff] [blame] | 125 | show_protocol, store_protocol); |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 126 | |
| 127 | static struct attribute *ir_dev_attrs[] = { |
Mauro Carvalho Chehab | 53f8702 | 2009-12-14 02:16:36 -0300 | [diff] [blame] | 128 | &dev_attr_current_protocol.attr, |
Francesco Lavra | 9714d58 | 2009-12-29 15:48:04 -0300 | [diff] [blame] | 129 | NULL, |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 130 | }; |
| 131 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 132 | /** |
| 133 | * ir_register_class() - creates the sysfs for /sys/class/irrcv/irrcv? |
| 134 | * @input_dev: the struct input_dev descriptor of the device |
| 135 | * |
| 136 | * This routine is used to register the syfs code for IR class |
| 137 | */ |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 138 | int ir_register_class(struct input_dev *input_dev) |
| 139 | { |
| 140 | int rc; |
| 141 | struct kobject *kobj; |
| 142 | |
| 143 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 144 | int devno = find_first_zero_bit(&ir_core_dev_number, |
| 145 | IRRCV_NUM_DEVICES); |
| 146 | |
| 147 | if (unlikely(devno < 0)) |
| 148 | return devno; |
| 149 | |
| 150 | ir_dev->attr.attrs = ir_dev_attrs; |
| 151 | ir_dev->class_dev = device_create(ir_input_class, NULL, |
| 152 | input_dev->dev.devt, ir_dev, |
| 153 | "irrcv%d", devno); |
| 154 | kobj = &ir_dev->class_dev->kobj; |
| 155 | |
| 156 | printk(KERN_WARNING "Creating IR device %s\n", kobject_name(kobj)); |
| 157 | rc = sysfs_create_group(kobj, &ir_dev->attr); |
| 158 | if (unlikely(rc < 0)) { |
| 159 | device_destroy(ir_input_class, input_dev->dev.devt); |
| 160 | return -ENOMEM; |
| 161 | } |
| 162 | |
| 163 | ir_dev->devno = devno; |
| 164 | set_bit(devno, &ir_core_dev_number); |
| 165 | |
| 166 | return 0; |
| 167 | }; |
| 168 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 169 | /** |
| 170 | * ir_unregister_class() - removes the sysfs for sysfs for |
| 171 | * /sys/class/irrcv/irrcv? |
| 172 | * @input_dev: the struct input_dev descriptor of the device |
| 173 | * |
| 174 | * This routine is used to unregister the syfs code for IR class |
| 175 | */ |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 176 | void ir_unregister_class(struct input_dev *input_dev) |
| 177 | { |
| 178 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 179 | struct kobject *kobj; |
| 180 | |
| 181 | clear_bit(ir_dev->devno, &ir_core_dev_number); |
| 182 | |
| 183 | kobj = &ir_dev->class_dev->kobj; |
| 184 | |
| 185 | sysfs_remove_group(kobj, &ir_dev->attr); |
| 186 | device_destroy(ir_input_class, input_dev->dev.devt); |
| 187 | |
| 188 | kfree(ir_dev->attr.name); |
| 189 | } |
| 190 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 191 | /* |
| 192 | * Init/exit code for the module. Basically, creates/removes /sys/class/irrcv |
| 193 | */ |
| 194 | |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 195 | static int __init ir_core_init(void) |
| 196 | { |
| 197 | ir_input_class = class_create(THIS_MODULE, "irrcv"); |
| 198 | if (IS_ERR(ir_input_class)) { |
| 199 | printk(KERN_ERR "ir_core: unable to register irrcv class\n"); |
| 200 | return PTR_ERR(ir_input_class); |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static void __exit ir_core_exit(void) |
| 207 | { |
| 208 | class_destroy(ir_input_class); |
| 209 | } |
| 210 | |
| 211 | module_init(ir_core_init); |
| 212 | module_exit(ir_core_exit); |