| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 | 
|  | 2 | // rc-main.c - Remote Controller core module | 
|  | 3 | // | 
|  | 4 | // Copyright (C) 2009-2010 by Mauro Carvalho Chehab | 
|  | 5 |  | 
|  | 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 
|  | 7 |  | 
|  | 8 | #include <media/rc-core.h> | 
|  | 9 | #include <linux/bsearch.h> | 
|  | 10 | #include <linux/spinlock.h> | 
|  | 11 | #include <linux/delay.h> | 
|  | 12 | #include <linux/input.h> | 
|  | 13 | #include <linux/leds.h> | 
|  | 14 | #include <linux/slab.h> | 
|  | 15 | #include <linux/idr.h> | 
|  | 16 | #include <linux/device.h> | 
|  | 17 | #include <linux/module.h> | 
|  | 18 | #include "rc-core-priv.h" | 
|  | 19 |  | 
|  | 20 | /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */ | 
|  | 21 | #define IR_TAB_MIN_SIZE	256 | 
|  | 22 | #define IR_TAB_MAX_SIZE	8192 | 
|  | 23 |  | 
|  | 24 | static const struct { | 
|  | 25 | const char *name; | 
|  | 26 | unsigned int repeat_period; | 
|  | 27 | unsigned int scancode_bits; | 
|  | 28 | } protocols[] = { | 
|  | 29 | [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 125 }, | 
|  | 30 | [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 125 }, | 
|  | 31 | [RC_PROTO_RC5] = { .name = "rc-5", | 
|  | 32 | .scancode_bits = 0x1f7f, .repeat_period = 114 }, | 
|  | 33 | [RC_PROTO_RC5X_20] = { .name = "rc-5x-20", | 
|  | 34 | .scancode_bits = 0x1f7f3f, .repeat_period = 114 }, | 
|  | 35 | [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz", | 
|  | 36 | .scancode_bits = 0x2fff, .repeat_period = 114 }, | 
|  | 37 | [RC_PROTO_JVC] = { .name = "jvc", | 
|  | 38 | .scancode_bits = 0xffff, .repeat_period = 125 }, | 
|  | 39 | [RC_PROTO_SONY12] = { .name = "sony-12", | 
|  | 40 | .scancode_bits = 0x1f007f, .repeat_period = 100 }, | 
|  | 41 | [RC_PROTO_SONY15] = { .name = "sony-15", | 
|  | 42 | .scancode_bits = 0xff007f, .repeat_period = 100 }, | 
|  | 43 | [RC_PROTO_SONY20] = { .name = "sony-20", | 
|  | 44 | .scancode_bits = 0x1fff7f, .repeat_period = 100 }, | 
|  | 45 | [RC_PROTO_NEC] = { .name = "nec", | 
|  | 46 | .scancode_bits = 0xffff, .repeat_period = 110 }, | 
|  | 47 | [RC_PROTO_NECX] = { .name = "nec-x", | 
|  | 48 | .scancode_bits = 0xffffff, .repeat_period = 110 }, | 
|  | 49 | [RC_PROTO_NEC32] = { .name = "nec-32", | 
|  | 50 | .scancode_bits = 0xffffffff, .repeat_period = 110 }, | 
|  | 51 | [RC_PROTO_SANYO] = { .name = "sanyo", | 
|  | 52 | .scancode_bits = 0x1fffff, .repeat_period = 125 }, | 
|  | 53 | [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd", | 
|  | 54 | .scancode_bits = 0xffffff, .repeat_period = 100 }, | 
|  | 55 | [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse", | 
|  | 56 | .scancode_bits = 0x1fffff, .repeat_period = 100 }, | 
|  | 57 | [RC_PROTO_RC6_0] = { .name = "rc-6-0", | 
|  | 58 | .scancode_bits = 0xffff, .repeat_period = 114 }, | 
|  | 59 | [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20", | 
|  | 60 | .scancode_bits = 0xfffff, .repeat_period = 114 }, | 
|  | 61 | [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24", | 
|  | 62 | .scancode_bits = 0xffffff, .repeat_period = 114 }, | 
|  | 63 | [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32", | 
|  | 64 | .scancode_bits = 0xffffffff, .repeat_period = 114 }, | 
|  | 65 | [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce", | 
|  | 66 | .scancode_bits = 0xffff7fff, .repeat_period = 114 }, | 
|  | 67 | [RC_PROTO_SHARP] = { .name = "sharp", | 
|  | 68 | .scancode_bits = 0x1fff, .repeat_period = 125 }, | 
|  | 69 | [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 125 }, | 
|  | 70 | [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 0 }, | 
|  | 71 | [RC_PROTO_IMON] = { .name = "imon", | 
|  | 72 | .scancode_bits = 0x7fffffff, .repeat_period = 114 }, | 
|  | 73 | }; | 
|  | 74 |  | 
|  | 75 | /* Used to keep track of known keymaps */ | 
|  | 76 | static LIST_HEAD(rc_map_list); | 
|  | 77 | static DEFINE_SPINLOCK(rc_map_lock); | 
|  | 78 | static struct led_trigger *led_feedback; | 
|  | 79 |  | 
|  | 80 | /* Used to keep track of rc devices */ | 
|  | 81 | static DEFINE_IDA(rc_ida); | 
|  | 82 |  | 
|  | 83 | static struct rc_map_list *seek_rc_map(const char *name) | 
|  | 84 | { | 
|  | 85 | struct rc_map_list *map = NULL; | 
|  | 86 |  | 
|  | 87 | spin_lock(&rc_map_lock); | 
|  | 88 | list_for_each_entry(map, &rc_map_list, list) { | 
|  | 89 | if (!strcmp(name, map->map.name)) { | 
|  | 90 | spin_unlock(&rc_map_lock); | 
|  | 91 | return map; | 
|  | 92 | } | 
|  | 93 | } | 
|  | 94 | spin_unlock(&rc_map_lock); | 
|  | 95 |  | 
|  | 96 | return NULL; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | struct rc_map *rc_map_get(const char *name) | 
|  | 100 | { | 
|  | 101 |  | 
|  | 102 | struct rc_map_list *map; | 
|  | 103 |  | 
|  | 104 | map = seek_rc_map(name); | 
|  | 105 | #ifdef CONFIG_MODULES | 
|  | 106 | if (!map) { | 
|  | 107 | int rc = request_module("%s", name); | 
|  | 108 | if (rc < 0) { | 
|  | 109 | pr_err("Couldn't load IR keymap %s\n", name); | 
|  | 110 | return NULL; | 
|  | 111 | } | 
|  | 112 | msleep(20);	/* Give some time for IR to register */ | 
|  | 113 |  | 
|  | 114 | map = seek_rc_map(name); | 
|  | 115 | } | 
|  | 116 | #endif | 
|  | 117 | if (!map) { | 
|  | 118 | pr_err("IR keymap %s not found\n", name); | 
|  | 119 | return NULL; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | printk(KERN_INFO "Registered IR keymap %s\n", map->map.name); | 
|  | 123 |  | 
|  | 124 | return &map->map; | 
|  | 125 | } | 
|  | 126 | EXPORT_SYMBOL_GPL(rc_map_get); | 
|  | 127 |  | 
|  | 128 | int rc_map_register(struct rc_map_list *map) | 
|  | 129 | { | 
|  | 130 | spin_lock(&rc_map_lock); | 
|  | 131 | list_add_tail(&map->list, &rc_map_list); | 
|  | 132 | spin_unlock(&rc_map_lock); | 
|  | 133 | return 0; | 
|  | 134 | } | 
|  | 135 | EXPORT_SYMBOL_GPL(rc_map_register); | 
|  | 136 |  | 
|  | 137 | void rc_map_unregister(struct rc_map_list *map) | 
|  | 138 | { | 
|  | 139 | spin_lock(&rc_map_lock); | 
|  | 140 | list_del(&map->list); | 
|  | 141 | spin_unlock(&rc_map_lock); | 
|  | 142 | } | 
|  | 143 | EXPORT_SYMBOL_GPL(rc_map_unregister); | 
|  | 144 |  | 
|  | 145 |  | 
|  | 146 | static struct rc_map_table empty[] = { | 
|  | 147 | { 0x2a, KEY_COFFEE }, | 
|  | 148 | }; | 
|  | 149 |  | 
|  | 150 | static struct rc_map_list empty_map = { | 
|  | 151 | .map = { | 
|  | 152 | .scan     = empty, | 
|  | 153 | .size     = ARRAY_SIZE(empty), | 
|  | 154 | .rc_proto = RC_PROTO_UNKNOWN,	/* Legacy IR type */ | 
|  | 155 | .name     = RC_MAP_EMPTY, | 
|  | 156 | } | 
|  | 157 | }; | 
|  | 158 |  | 
|  | 159 | /** | 
|  | 160 | * ir_create_table() - initializes a scancode table | 
|  | 161 | * @dev:	the rc_dev device | 
|  | 162 | * @rc_map:	the rc_map to initialize | 
|  | 163 | * @name:	name to assign to the table | 
|  | 164 | * @rc_proto:	ir type to assign to the new table | 
|  | 165 | * @size:	initial size of the table | 
|  | 166 | * | 
|  | 167 | * This routine will initialize the rc_map and will allocate | 
|  | 168 | * memory to hold at least the specified number of elements. | 
|  | 169 | * | 
|  | 170 | * return:	zero on success or a negative error code | 
|  | 171 | */ | 
|  | 172 | static int ir_create_table(struct rc_dev *dev, struct rc_map *rc_map, | 
|  | 173 | const char *name, u64 rc_proto, size_t size) | 
|  | 174 | { | 
|  | 175 | rc_map->name = kstrdup(name, GFP_KERNEL); | 
|  | 176 | if (!rc_map->name) | 
|  | 177 | return -ENOMEM; | 
|  | 178 | rc_map->rc_proto = rc_proto; | 
|  | 179 | rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table)); | 
|  | 180 | rc_map->size = rc_map->alloc / sizeof(struct rc_map_table); | 
|  | 181 | rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL); | 
|  | 182 | if (!rc_map->scan) { | 
|  | 183 | kfree(rc_map->name); | 
|  | 184 | rc_map->name = NULL; | 
|  | 185 | return -ENOMEM; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | dev_dbg(&dev->dev, "Allocated space for %u keycode entries (%u bytes)\n", | 
|  | 189 | rc_map->size, rc_map->alloc); | 
|  | 190 | return 0; | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | /** | 
|  | 194 | * ir_free_table() - frees memory allocated by a scancode table | 
|  | 195 | * @rc_map:	the table whose mappings need to be freed | 
|  | 196 | * | 
|  | 197 | * This routine will free memory alloctaed for key mappings used by given | 
|  | 198 | * scancode table. | 
|  | 199 | */ | 
|  | 200 | static void ir_free_table(struct rc_map *rc_map) | 
|  | 201 | { | 
|  | 202 | rc_map->size = 0; | 
|  | 203 | kfree(rc_map->name); | 
|  | 204 | rc_map->name = NULL; | 
|  | 205 | kfree(rc_map->scan); | 
|  | 206 | rc_map->scan = NULL; | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | /** | 
|  | 210 | * ir_resize_table() - resizes a scancode table if necessary | 
|  | 211 | * @dev:	the rc_dev device | 
|  | 212 | * @rc_map:	the rc_map to resize | 
|  | 213 | * @gfp_flags:	gfp flags to use when allocating memory | 
|  | 214 | * | 
|  | 215 | * This routine will shrink the rc_map if it has lots of | 
|  | 216 | * unused entries and grow it if it is full. | 
|  | 217 | * | 
|  | 218 | * return:	zero on success or a negative error code | 
|  | 219 | */ | 
|  | 220 | static int ir_resize_table(struct rc_dev *dev, struct rc_map *rc_map, | 
|  | 221 | gfp_t gfp_flags) | 
|  | 222 | { | 
|  | 223 | unsigned int oldalloc = rc_map->alloc; | 
|  | 224 | unsigned int newalloc = oldalloc; | 
|  | 225 | struct rc_map_table *oldscan = rc_map->scan; | 
|  | 226 | struct rc_map_table *newscan; | 
|  | 227 |  | 
|  | 228 | if (rc_map->size == rc_map->len) { | 
|  | 229 | /* All entries in use -> grow keytable */ | 
|  | 230 | if (rc_map->alloc >= IR_TAB_MAX_SIZE) | 
|  | 231 | return -ENOMEM; | 
|  | 232 |  | 
|  | 233 | newalloc *= 2; | 
|  | 234 | dev_dbg(&dev->dev, "Growing table to %u bytes\n", newalloc); | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) { | 
|  | 238 | /* Less than 1/3 of entries in use -> shrink keytable */ | 
|  | 239 | newalloc /= 2; | 
|  | 240 | dev_dbg(&dev->dev, "Shrinking table to %u bytes\n", newalloc); | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | if (newalloc == oldalloc) | 
|  | 244 | return 0; | 
|  | 245 |  | 
|  | 246 | newscan = kmalloc(newalloc, gfp_flags); | 
|  | 247 | if (!newscan) | 
|  | 248 | return -ENOMEM; | 
|  | 249 |  | 
|  | 250 | memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table)); | 
|  | 251 | rc_map->scan = newscan; | 
|  | 252 | rc_map->alloc = newalloc; | 
|  | 253 | rc_map->size = rc_map->alloc / sizeof(struct rc_map_table); | 
|  | 254 | kfree(oldscan); | 
|  | 255 | return 0; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | /** | 
|  | 259 | * ir_update_mapping() - set a keycode in the scancode->keycode table | 
|  | 260 | * @dev:	the struct rc_dev device descriptor | 
|  | 261 | * @rc_map:	scancode table to be adjusted | 
|  | 262 | * @index:	index of the mapping that needs to be updated | 
|  | 263 | * @new_keycode: the desired keycode | 
|  | 264 | * | 
|  | 265 | * This routine is used to update scancode->keycode mapping at given | 
|  | 266 | * position. | 
|  | 267 | * | 
|  | 268 | * return:	previous keycode assigned to the mapping | 
|  | 269 | * | 
|  | 270 | */ | 
|  | 271 | static unsigned int ir_update_mapping(struct rc_dev *dev, | 
|  | 272 | struct rc_map *rc_map, | 
|  | 273 | unsigned int index, | 
|  | 274 | unsigned int new_keycode) | 
|  | 275 | { | 
|  | 276 | int old_keycode = rc_map->scan[index].keycode; | 
|  | 277 | int i; | 
|  | 278 |  | 
|  | 279 | /* Did the user wish to remove the mapping? */ | 
|  | 280 | if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) { | 
|  | 281 | dev_dbg(&dev->dev, "#%d: Deleting scan 0x%04x\n", | 
|  | 282 | index, rc_map->scan[index].scancode); | 
|  | 283 | rc_map->len--; | 
|  | 284 | memmove(&rc_map->scan[index], &rc_map->scan[index+ 1], | 
|  | 285 | (rc_map->len - index) * sizeof(struct rc_map_table)); | 
|  | 286 | } else { | 
|  | 287 | dev_dbg(&dev->dev, "#%d: %s scan 0x%04x with key 0x%04x\n", | 
|  | 288 | index, | 
|  | 289 | old_keycode == KEY_RESERVED ? "New" : "Replacing", | 
|  | 290 | rc_map->scan[index].scancode, new_keycode); | 
|  | 291 | rc_map->scan[index].keycode = new_keycode; | 
|  | 292 | __set_bit(new_keycode, dev->input_dev->keybit); | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | if (old_keycode != KEY_RESERVED) { | 
|  | 296 | /* A previous mapping was updated... */ | 
|  | 297 | __clear_bit(old_keycode, dev->input_dev->keybit); | 
|  | 298 | /* ... but another scancode might use the same keycode */ | 
|  | 299 | for (i = 0; i < rc_map->len; i++) { | 
|  | 300 | if (rc_map->scan[i].keycode == old_keycode) { | 
|  | 301 | __set_bit(old_keycode, dev->input_dev->keybit); | 
|  | 302 | break; | 
|  | 303 | } | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | /* Possibly shrink the keytable, failure is not a problem */ | 
|  | 307 | ir_resize_table(dev, rc_map, GFP_ATOMIC); | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | return old_keycode; | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | /** | 
|  | 314 | * ir_establish_scancode() - set a keycode in the scancode->keycode table | 
|  | 315 | * @dev:	the struct rc_dev device descriptor | 
|  | 316 | * @rc_map:	scancode table to be searched | 
|  | 317 | * @scancode:	the desired scancode | 
|  | 318 | * @resize:	controls whether we allowed to resize the table to | 
|  | 319 | *		accommodate not yet present scancodes | 
|  | 320 | * | 
|  | 321 | * This routine is used to locate given scancode in rc_map. | 
|  | 322 | * If scancode is not yet present the routine will allocate a new slot | 
|  | 323 | * for it. | 
|  | 324 | * | 
|  | 325 | * return:	index of the mapping containing scancode in question | 
|  | 326 | *		or -1U in case of failure. | 
|  | 327 | */ | 
|  | 328 | static unsigned int ir_establish_scancode(struct rc_dev *dev, | 
|  | 329 | struct rc_map *rc_map, | 
|  | 330 | unsigned int scancode, | 
|  | 331 | bool resize) | 
|  | 332 | { | 
|  | 333 | unsigned int i; | 
|  | 334 |  | 
|  | 335 | /* | 
|  | 336 | * Unfortunately, some hardware-based IR decoders don't provide | 
|  | 337 | * all bits for the complete IR code. In general, they provide only | 
|  | 338 | * the command part of the IR code. Yet, as it is possible to replace | 
|  | 339 | * the provided IR with another one, it is needed to allow loading | 
|  | 340 | * IR tables from other remotes. So, we support specifying a mask to | 
|  | 341 | * indicate the valid bits of the scancodes. | 
|  | 342 | */ | 
|  | 343 | if (dev->scancode_mask) | 
|  | 344 | scancode &= dev->scancode_mask; | 
|  | 345 |  | 
|  | 346 | /* First check if we already have a mapping for this ir command */ | 
|  | 347 | for (i = 0; i < rc_map->len; i++) { | 
|  | 348 | if (rc_map->scan[i].scancode == scancode) | 
|  | 349 | return i; | 
|  | 350 |  | 
|  | 351 | /* Keytable is sorted from lowest to highest scancode */ | 
|  | 352 | if (rc_map->scan[i].scancode >= scancode) | 
|  | 353 | break; | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | /* No previous mapping found, we might need to grow the table */ | 
|  | 357 | if (rc_map->size == rc_map->len) { | 
|  | 358 | if (!resize || ir_resize_table(dev, rc_map, GFP_ATOMIC)) | 
|  | 359 | return -1U; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | /* i is the proper index to insert our new keycode */ | 
|  | 363 | if (i < rc_map->len) | 
|  | 364 | memmove(&rc_map->scan[i + 1], &rc_map->scan[i], | 
|  | 365 | (rc_map->len - i) * sizeof(struct rc_map_table)); | 
|  | 366 | rc_map->scan[i].scancode = scancode; | 
|  | 367 | rc_map->scan[i].keycode = KEY_RESERVED; | 
|  | 368 | rc_map->len++; | 
|  | 369 |  | 
|  | 370 | return i; | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | /** | 
|  | 374 | * ir_setkeycode() - set a keycode in the scancode->keycode table | 
|  | 375 | * @idev:	the struct input_dev device descriptor | 
|  | 376 | * @ke:		Input keymap entry | 
|  | 377 | * @old_keycode: result | 
|  | 378 | * | 
|  | 379 | * This routine is used to handle evdev EVIOCSKEY ioctl. | 
|  | 380 | * | 
|  | 381 | * return:	-EINVAL if the keycode could not be inserted, otherwise zero. | 
|  | 382 | */ | 
|  | 383 | static int ir_setkeycode(struct input_dev *idev, | 
|  | 384 | const struct input_keymap_entry *ke, | 
|  | 385 | unsigned int *old_keycode) | 
|  | 386 | { | 
|  | 387 | struct rc_dev *rdev = input_get_drvdata(idev); | 
|  | 388 | struct rc_map *rc_map = &rdev->rc_map; | 
|  | 389 | unsigned int index; | 
|  | 390 | unsigned int scancode; | 
|  | 391 | int retval = 0; | 
|  | 392 | unsigned long flags; | 
|  | 393 |  | 
|  | 394 | spin_lock_irqsave(&rc_map->lock, flags); | 
|  | 395 |  | 
|  | 396 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { | 
|  | 397 | index = ke->index; | 
|  | 398 | if (index >= rc_map->len) { | 
|  | 399 | retval = -EINVAL; | 
|  | 400 | goto out; | 
|  | 401 | } | 
|  | 402 | } else { | 
|  | 403 | retval = input_scancode_to_scalar(ke, &scancode); | 
|  | 404 | if (retval) | 
|  | 405 | goto out; | 
|  | 406 |  | 
|  | 407 | index = ir_establish_scancode(rdev, rc_map, scancode, true); | 
|  | 408 | if (index >= rc_map->len) { | 
|  | 409 | retval = -ENOMEM; | 
|  | 410 | goto out; | 
|  | 411 | } | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode); | 
|  | 415 |  | 
|  | 416 | out: | 
|  | 417 | spin_unlock_irqrestore(&rc_map->lock, flags); | 
|  | 418 | return retval; | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | /** | 
|  | 422 | * ir_setkeytable() - sets several entries in the scancode->keycode table | 
|  | 423 | * @dev:	the struct rc_dev device descriptor | 
|  | 424 | * @from:	the struct rc_map to copy entries from | 
|  | 425 | * | 
|  | 426 | * This routine is used to handle table initialization. | 
|  | 427 | * | 
|  | 428 | * return:	-ENOMEM if all keycodes could not be inserted, otherwise zero. | 
|  | 429 | */ | 
|  | 430 | static int ir_setkeytable(struct rc_dev *dev, | 
|  | 431 | const struct rc_map *from) | 
|  | 432 | { | 
|  | 433 | struct rc_map *rc_map = &dev->rc_map; | 
|  | 434 | unsigned int i, index; | 
|  | 435 | int rc; | 
|  | 436 |  | 
|  | 437 | rc = ir_create_table(dev, rc_map, from->name, from->rc_proto, | 
|  | 438 | from->size); | 
|  | 439 | if (rc) | 
|  | 440 | return rc; | 
|  | 441 |  | 
|  | 442 | for (i = 0; i < from->size; i++) { | 
|  | 443 | index = ir_establish_scancode(dev, rc_map, | 
|  | 444 | from->scan[i].scancode, false); | 
|  | 445 | if (index >= rc_map->len) { | 
|  | 446 | rc = -ENOMEM; | 
|  | 447 | break; | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | ir_update_mapping(dev, rc_map, index, | 
|  | 451 | from->scan[i].keycode); | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | if (rc) | 
|  | 455 | ir_free_table(rc_map); | 
|  | 456 |  | 
|  | 457 | return rc; | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | static int rc_map_cmp(const void *key, const void *elt) | 
|  | 461 | { | 
|  | 462 | const unsigned int *scancode = key; | 
|  | 463 | const struct rc_map_table *e = elt; | 
|  | 464 |  | 
|  | 465 | if (*scancode < e->scancode) | 
|  | 466 | return -1; | 
|  | 467 | else if (*scancode > e->scancode) | 
|  | 468 | return 1; | 
|  | 469 | return 0; | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | /** | 
|  | 473 | * ir_lookup_by_scancode() - locate mapping by scancode | 
|  | 474 | * @rc_map:	the struct rc_map to search | 
|  | 475 | * @scancode:	scancode to look for in the table | 
|  | 476 | * | 
|  | 477 | * This routine performs binary search in RC keykeymap table for | 
|  | 478 | * given scancode. | 
|  | 479 | * | 
|  | 480 | * return:	index in the table, -1U if not found | 
|  | 481 | */ | 
|  | 482 | static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map, | 
|  | 483 | unsigned int scancode) | 
|  | 484 | { | 
|  | 485 | struct rc_map_table *res; | 
|  | 486 |  | 
|  | 487 | res = bsearch(&scancode, rc_map->scan, rc_map->len, | 
|  | 488 | sizeof(struct rc_map_table), rc_map_cmp); | 
|  | 489 | if (!res) | 
|  | 490 | return -1U; | 
|  | 491 | else | 
|  | 492 | return res - rc_map->scan; | 
|  | 493 | } | 
|  | 494 |  | 
|  | 495 | /** | 
|  | 496 | * ir_getkeycode() - get a keycode from the scancode->keycode table | 
|  | 497 | * @idev:	the struct input_dev device descriptor | 
|  | 498 | * @ke:		Input keymap entry | 
|  | 499 | * | 
|  | 500 | * This routine is used to handle evdev EVIOCGKEY ioctl. | 
|  | 501 | * | 
|  | 502 | * return:	always returns zero. | 
|  | 503 | */ | 
|  | 504 | static int ir_getkeycode(struct input_dev *idev, | 
|  | 505 | struct input_keymap_entry *ke) | 
|  | 506 | { | 
|  | 507 | struct rc_dev *rdev = input_get_drvdata(idev); | 
|  | 508 | struct rc_map *rc_map = &rdev->rc_map; | 
|  | 509 | struct rc_map_table *entry; | 
|  | 510 | unsigned long flags; | 
|  | 511 | unsigned int index; | 
|  | 512 | unsigned int scancode; | 
|  | 513 | int retval; | 
|  | 514 |  | 
|  | 515 | spin_lock_irqsave(&rc_map->lock, flags); | 
|  | 516 |  | 
|  | 517 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { | 
|  | 518 | index = ke->index; | 
|  | 519 | } else { | 
|  | 520 | retval = input_scancode_to_scalar(ke, &scancode); | 
|  | 521 | if (retval) | 
|  | 522 | goto out; | 
|  | 523 |  | 
|  | 524 | index = ir_lookup_by_scancode(rc_map, scancode); | 
|  | 525 | } | 
|  | 526 |  | 
|  | 527 | if (index < rc_map->len) { | 
|  | 528 | entry = &rc_map->scan[index]; | 
|  | 529 |  | 
|  | 530 | ke->index = index; | 
|  | 531 | ke->keycode = entry->keycode; | 
|  | 532 | ke->len = sizeof(entry->scancode); | 
|  | 533 | memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode)); | 
|  | 534 |  | 
|  | 535 | } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) { | 
|  | 536 | /* | 
|  | 537 | * We do not really know the valid range of scancodes | 
|  | 538 | * so let's respond with KEY_RESERVED to anything we | 
|  | 539 | * do not have mapping for [yet]. | 
|  | 540 | */ | 
|  | 541 | ke->index = index; | 
|  | 542 | ke->keycode = KEY_RESERVED; | 
|  | 543 | } else { | 
|  | 544 | retval = -EINVAL; | 
|  | 545 | goto out; | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | retval = 0; | 
|  | 549 |  | 
|  | 550 | out: | 
|  | 551 | spin_unlock_irqrestore(&rc_map->lock, flags); | 
|  | 552 | return retval; | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | /** | 
|  | 556 | * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode | 
|  | 557 | * @dev:	the struct rc_dev descriptor of the device | 
|  | 558 | * @scancode:	the scancode to look for | 
|  | 559 | * | 
|  | 560 | * This routine is used by drivers which need to convert a scancode to a | 
|  | 561 | * keycode. Normally it should not be used since drivers should have no | 
|  | 562 | * interest in keycodes. | 
|  | 563 | * | 
|  | 564 | * return:	the corresponding keycode, or KEY_RESERVED | 
|  | 565 | */ | 
|  | 566 | u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode) | 
|  | 567 | { | 
|  | 568 | struct rc_map *rc_map = &dev->rc_map; | 
|  | 569 | unsigned int keycode; | 
|  | 570 | unsigned int index; | 
|  | 571 | unsigned long flags; | 
|  | 572 |  | 
|  | 573 | spin_lock_irqsave(&rc_map->lock, flags); | 
|  | 574 |  | 
|  | 575 | index = ir_lookup_by_scancode(rc_map, scancode); | 
|  | 576 | keycode = index < rc_map->len ? | 
|  | 577 | rc_map->scan[index].keycode : KEY_RESERVED; | 
|  | 578 |  | 
|  | 579 | spin_unlock_irqrestore(&rc_map->lock, flags); | 
|  | 580 |  | 
|  | 581 | if (keycode != KEY_RESERVED) | 
|  | 582 | dev_dbg(&dev->dev, "%s: scancode 0x%04x keycode 0x%02x\n", | 
|  | 583 | dev->device_name, scancode, keycode); | 
|  | 584 |  | 
|  | 585 | return keycode; | 
|  | 586 | } | 
|  | 587 | EXPORT_SYMBOL_GPL(rc_g_keycode_from_table); | 
|  | 588 |  | 
|  | 589 | /** | 
|  | 590 | * ir_do_keyup() - internal function to signal the release of a keypress | 
|  | 591 | * @dev:	the struct rc_dev descriptor of the device | 
|  | 592 | * @sync:	whether or not to call input_sync | 
|  | 593 | * | 
|  | 594 | * This function is used internally to release a keypress, it must be | 
|  | 595 | * called with keylock held. | 
|  | 596 | */ | 
|  | 597 | static void ir_do_keyup(struct rc_dev *dev, bool sync) | 
|  | 598 | { | 
|  | 599 | if (!dev->keypressed) | 
|  | 600 | return; | 
|  | 601 |  | 
|  | 602 | dev_dbg(&dev->dev, "keyup key 0x%04x\n", dev->last_keycode); | 
|  | 603 | del_timer(&dev->timer_repeat); | 
|  | 604 | input_report_key(dev->input_dev, dev->last_keycode, 0); | 
|  | 605 | led_trigger_event(led_feedback, LED_OFF); | 
|  | 606 | if (sync) | 
|  | 607 | input_sync(dev->input_dev); | 
|  | 608 | dev->keypressed = false; | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | /** | 
|  | 612 | * rc_keyup() - signals the release of a keypress | 
|  | 613 | * @dev:	the struct rc_dev descriptor of the device | 
|  | 614 | * | 
|  | 615 | * This routine is used to signal that a key has been released on the | 
|  | 616 | * remote control. | 
|  | 617 | */ | 
|  | 618 | void rc_keyup(struct rc_dev *dev) | 
|  | 619 | { | 
|  | 620 | unsigned long flags; | 
|  | 621 |  | 
|  | 622 | spin_lock_irqsave(&dev->keylock, flags); | 
|  | 623 | ir_do_keyup(dev, true); | 
|  | 624 | spin_unlock_irqrestore(&dev->keylock, flags); | 
|  | 625 | } | 
|  | 626 | EXPORT_SYMBOL_GPL(rc_keyup); | 
|  | 627 |  | 
|  | 628 | /** | 
|  | 629 | * ir_timer_keyup() - generates a keyup event after a timeout | 
|  | 630 | * | 
|  | 631 | * @t:		a pointer to the struct timer_list | 
|  | 632 | * | 
|  | 633 | * This routine will generate a keyup event some time after a keydown event | 
|  | 634 | * is generated when no further activity has been detected. | 
|  | 635 | */ | 
|  | 636 | static void ir_timer_keyup(struct timer_list *t) | 
|  | 637 | { | 
|  | 638 | struct rc_dev *dev = from_timer(dev, t, timer_keyup); | 
|  | 639 | unsigned long flags; | 
|  | 640 |  | 
|  | 641 | /* | 
|  | 642 | * ir->keyup_jiffies is used to prevent a race condition if a | 
|  | 643 | * hardware interrupt occurs at this point and the keyup timer | 
|  | 644 | * event is moved further into the future as a result. | 
|  | 645 | * | 
|  | 646 | * The timer will then be reactivated and this function called | 
|  | 647 | * again in the future. We need to exit gracefully in that case | 
|  | 648 | * to allow the input subsystem to do its auto-repeat magic or | 
|  | 649 | * a keyup event might follow immediately after the keydown. | 
|  | 650 | */ | 
|  | 651 | spin_lock_irqsave(&dev->keylock, flags); | 
|  | 652 | if (time_is_before_eq_jiffies(dev->keyup_jiffies)) | 
|  | 653 | ir_do_keyup(dev, true); | 
|  | 654 | spin_unlock_irqrestore(&dev->keylock, flags); | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | /** | 
|  | 658 | * ir_timer_repeat() - generates a repeat event after a timeout | 
|  | 659 | * | 
|  | 660 | * @t:		a pointer to the struct timer_list | 
|  | 661 | * | 
|  | 662 | * This routine will generate a soft repeat event every REP_PERIOD | 
|  | 663 | * milliseconds. | 
|  | 664 | */ | 
|  | 665 | static void ir_timer_repeat(struct timer_list *t) | 
|  | 666 | { | 
|  | 667 | struct rc_dev *dev = from_timer(dev, t, timer_repeat); | 
|  | 668 | struct input_dev *input = dev->input_dev; | 
|  | 669 | unsigned long flags; | 
|  | 670 |  | 
|  | 671 | spin_lock_irqsave(&dev->keylock, flags); | 
|  | 672 | if (dev->keypressed) { | 
|  | 673 | input_event(input, EV_KEY, dev->last_keycode, 2); | 
|  | 674 | input_sync(input); | 
|  | 675 | if (input->rep[REP_PERIOD]) | 
|  | 676 | mod_timer(&dev->timer_repeat, jiffies + | 
|  | 677 | msecs_to_jiffies(input->rep[REP_PERIOD])); | 
|  | 678 | } | 
|  | 679 | spin_unlock_irqrestore(&dev->keylock, flags); | 
|  | 680 | } | 
|  | 681 |  | 
|  | 682 | static unsigned int repeat_period(int protocol) | 
|  | 683 | { | 
|  | 684 | if (protocol >= ARRAY_SIZE(protocols)) | 
|  | 685 | return 100; | 
|  | 686 |  | 
|  | 687 | return protocols[protocol].repeat_period; | 
|  | 688 | } | 
|  | 689 |  | 
|  | 690 | /** | 
|  | 691 | * rc_repeat() - signals that a key is still pressed | 
|  | 692 | * @dev:	the struct rc_dev descriptor of the device | 
|  | 693 | * | 
|  | 694 | * This routine is used by IR decoders when a repeat message which does | 
|  | 695 | * not include the necessary bits to reproduce the scancode has been | 
|  | 696 | * received. | 
|  | 697 | */ | 
|  | 698 | void rc_repeat(struct rc_dev *dev) | 
|  | 699 | { | 
|  | 700 | unsigned long flags; | 
|  | 701 | unsigned int timeout = nsecs_to_jiffies(dev->timeout) + | 
|  | 702 | msecs_to_jiffies(repeat_period(dev->last_protocol)); | 
|  | 703 | struct lirc_scancode sc = { | 
|  | 704 | .scancode = dev->last_scancode, .rc_proto = dev->last_protocol, | 
|  | 705 | .keycode = dev->keypressed ? dev->last_keycode : KEY_RESERVED, | 
|  | 706 | .flags = LIRC_SCANCODE_FLAG_REPEAT | | 
|  | 707 | (dev->last_toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0) | 
|  | 708 | }; | 
|  | 709 |  | 
|  | 710 | if (dev->allowed_protocols != RC_PROTO_BIT_CEC) | 
|  | 711 | ir_lirc_scancode_event(dev, &sc); | 
|  | 712 |  | 
|  | 713 | spin_lock_irqsave(&dev->keylock, flags); | 
|  | 714 |  | 
|  | 715 | input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode); | 
|  | 716 | input_sync(dev->input_dev); | 
|  | 717 |  | 
|  | 718 | if (dev->keypressed) { | 
|  | 719 | dev->keyup_jiffies = jiffies + timeout; | 
|  | 720 | mod_timer(&dev->timer_keyup, dev->keyup_jiffies); | 
|  | 721 | } | 
|  | 722 |  | 
|  | 723 | spin_unlock_irqrestore(&dev->keylock, flags); | 
|  | 724 | } | 
|  | 725 | EXPORT_SYMBOL_GPL(rc_repeat); | 
|  | 726 |  | 
|  | 727 | /** | 
|  | 728 | * ir_do_keydown() - internal function to process a keypress | 
|  | 729 | * @dev:	the struct rc_dev descriptor of the device | 
|  | 730 | * @protocol:	the protocol of the keypress | 
|  | 731 | * @scancode:   the scancode of the keypress | 
|  | 732 | * @keycode:    the keycode of the keypress | 
|  | 733 | * @toggle:     the toggle value of the keypress | 
|  | 734 | * | 
|  | 735 | * This function is used internally to register a keypress, it must be | 
|  | 736 | * called with keylock held. | 
|  | 737 | */ | 
|  | 738 | static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol, | 
|  | 739 | u32 scancode, u32 keycode, u8 toggle) | 
|  | 740 | { | 
|  | 741 | bool new_event = (!dev->keypressed		 || | 
|  | 742 | dev->last_protocol != protocol || | 
|  | 743 | dev->last_scancode != scancode || | 
|  | 744 | dev->last_toggle   != toggle); | 
|  | 745 | struct lirc_scancode sc = { | 
|  | 746 | .scancode = scancode, .rc_proto = protocol, | 
|  | 747 | .flags = toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0, | 
|  | 748 | .keycode = keycode | 
|  | 749 | }; | 
|  | 750 |  | 
|  | 751 | if (dev->allowed_protocols != RC_PROTO_BIT_CEC) | 
|  | 752 | ir_lirc_scancode_event(dev, &sc); | 
|  | 753 |  | 
|  | 754 | if (new_event && dev->keypressed) | 
|  | 755 | ir_do_keyup(dev, false); | 
|  | 756 |  | 
|  | 757 | input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode); | 
|  | 758 |  | 
|  | 759 | dev->last_protocol = protocol; | 
|  | 760 | dev->last_scancode = scancode; | 
|  | 761 | dev->last_toggle = toggle; | 
|  | 762 | dev->last_keycode = keycode; | 
|  | 763 |  | 
|  | 764 | if (new_event && keycode != KEY_RESERVED) { | 
|  | 765 | /* Register a keypress */ | 
|  | 766 | dev->keypressed = true; | 
|  | 767 |  | 
|  | 768 | dev_dbg(&dev->dev, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n", | 
|  | 769 | dev->device_name, keycode, protocol, scancode); | 
|  | 770 | input_report_key(dev->input_dev, keycode, 1); | 
|  | 771 |  | 
|  | 772 | led_trigger_event(led_feedback, LED_FULL); | 
|  | 773 | } | 
|  | 774 |  | 
|  | 775 | /* | 
|  | 776 | * For CEC, start sending repeat messages as soon as the first | 
|  | 777 | * repeated message is sent, as long as REP_DELAY = 0 and REP_PERIOD | 
|  | 778 | * is non-zero. Otherwise, the input layer will generate repeat | 
|  | 779 | * messages. | 
|  | 780 | */ | 
|  | 781 | if (!new_event && keycode != KEY_RESERVED && | 
|  | 782 | dev->allowed_protocols == RC_PROTO_BIT_CEC && | 
|  | 783 | !timer_pending(&dev->timer_repeat) && | 
|  | 784 | dev->input_dev->rep[REP_PERIOD] && | 
|  | 785 | !dev->input_dev->rep[REP_DELAY]) { | 
|  | 786 | input_event(dev->input_dev, EV_KEY, keycode, 2); | 
|  | 787 | mod_timer(&dev->timer_repeat, jiffies + | 
|  | 788 | msecs_to_jiffies(dev->input_dev->rep[REP_PERIOD])); | 
|  | 789 | } | 
|  | 790 |  | 
|  | 791 | input_sync(dev->input_dev); | 
|  | 792 | } | 
|  | 793 |  | 
|  | 794 | /** | 
|  | 795 | * rc_keydown() - generates input event for a key press | 
|  | 796 | * @dev:	the struct rc_dev descriptor of the device | 
|  | 797 | * @protocol:	the protocol for the keypress | 
|  | 798 | * @scancode:	the scancode for the keypress | 
|  | 799 | * @toggle:     the toggle value (protocol dependent, if the protocol doesn't | 
|  | 800 | *              support toggle values, this should be set to zero) | 
|  | 801 | * | 
|  | 802 | * This routine is used to signal that a key has been pressed on the | 
|  | 803 | * remote control. | 
|  | 804 | */ | 
|  | 805 | void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode, | 
|  | 806 | u8 toggle) | 
|  | 807 | { | 
|  | 808 | unsigned long flags; | 
|  | 809 | u32 keycode = rc_g_keycode_from_table(dev, scancode); | 
|  | 810 |  | 
|  | 811 | spin_lock_irqsave(&dev->keylock, flags); | 
|  | 812 | ir_do_keydown(dev, protocol, scancode, keycode, toggle); | 
|  | 813 |  | 
|  | 814 | if (dev->keypressed) { | 
|  | 815 | dev->keyup_jiffies = jiffies + nsecs_to_jiffies(dev->timeout) + | 
|  | 816 | msecs_to_jiffies(repeat_period(protocol)); | 
|  | 817 | mod_timer(&dev->timer_keyup, dev->keyup_jiffies); | 
|  | 818 | } | 
|  | 819 | spin_unlock_irqrestore(&dev->keylock, flags); | 
|  | 820 | } | 
|  | 821 | EXPORT_SYMBOL_GPL(rc_keydown); | 
|  | 822 |  | 
|  | 823 | /** | 
|  | 824 | * rc_keydown_notimeout() - generates input event for a key press without | 
|  | 825 | *                          an automatic keyup event at a later time | 
|  | 826 | * @dev:	the struct rc_dev descriptor of the device | 
|  | 827 | * @protocol:	the protocol for the keypress | 
|  | 828 | * @scancode:	the scancode for the keypress | 
|  | 829 | * @toggle:     the toggle value (protocol dependent, if the protocol doesn't | 
|  | 830 | *              support toggle values, this should be set to zero) | 
|  | 831 | * | 
|  | 832 | * This routine is used to signal that a key has been pressed on the | 
|  | 833 | * remote control. The driver must manually call rc_keyup() at a later stage. | 
|  | 834 | */ | 
|  | 835 | void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol, | 
|  | 836 | u32 scancode, u8 toggle) | 
|  | 837 | { | 
|  | 838 | unsigned long flags; | 
|  | 839 | u32 keycode = rc_g_keycode_from_table(dev, scancode); | 
|  | 840 |  | 
|  | 841 | spin_lock_irqsave(&dev->keylock, flags); | 
|  | 842 | ir_do_keydown(dev, protocol, scancode, keycode, toggle); | 
|  | 843 | spin_unlock_irqrestore(&dev->keylock, flags); | 
|  | 844 | } | 
|  | 845 | EXPORT_SYMBOL_GPL(rc_keydown_notimeout); | 
|  | 846 |  | 
|  | 847 | /** | 
|  | 848 | * rc_validate_scancode() - checks that a scancode is valid for a protocol. | 
|  | 849 | *	For nec, it should do the opposite of ir_nec_bytes_to_scancode() | 
|  | 850 | * @proto:	protocol | 
|  | 851 | * @scancode:	scancode | 
|  | 852 | */ | 
|  | 853 | bool rc_validate_scancode(enum rc_proto proto, u32 scancode) | 
|  | 854 | { | 
|  | 855 | switch (proto) { | 
|  | 856 | /* | 
|  | 857 | * NECX has a 16-bit address; if the lower 8 bits match the upper | 
|  | 858 | * 8 bits inverted, then the address would match regular nec. | 
|  | 859 | */ | 
|  | 860 | case RC_PROTO_NECX: | 
|  | 861 | if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0) | 
|  | 862 | return false; | 
|  | 863 | break; | 
|  | 864 | /* | 
|  | 865 | * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits | 
|  | 866 | * of the command match the upper 8 bits inverted, then it would | 
|  | 867 | * be either NEC or NECX. | 
|  | 868 | */ | 
|  | 869 | case RC_PROTO_NEC32: | 
|  | 870 | if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0) | 
|  | 871 | return false; | 
|  | 872 | break; | 
|  | 873 | /* | 
|  | 874 | * If the customer code (top 32-bit) is 0x800f, it is MCE else it | 
|  | 875 | * is regular mode-6a 32 bit | 
|  | 876 | */ | 
|  | 877 | case RC_PROTO_RC6_MCE: | 
|  | 878 | if ((scancode & 0xffff0000) != 0x800f0000) | 
|  | 879 | return false; | 
|  | 880 | break; | 
|  | 881 | case RC_PROTO_RC6_6A_32: | 
|  | 882 | if ((scancode & 0xffff0000) == 0x800f0000) | 
|  | 883 | return false; | 
|  | 884 | break; | 
|  | 885 | default: | 
|  | 886 | break; | 
|  | 887 | } | 
|  | 888 |  | 
|  | 889 | return true; | 
|  | 890 | } | 
|  | 891 |  | 
|  | 892 | /** | 
|  | 893 | * rc_validate_filter() - checks that the scancode and mask are valid and | 
|  | 894 | *			  provides sensible defaults | 
|  | 895 | * @dev:	the struct rc_dev descriptor of the device | 
|  | 896 | * @filter:	the scancode and mask | 
|  | 897 | * | 
|  | 898 | * return:	0 or -EINVAL if the filter is not valid | 
|  | 899 | */ | 
|  | 900 | static int rc_validate_filter(struct rc_dev *dev, | 
|  | 901 | struct rc_scancode_filter *filter) | 
|  | 902 | { | 
|  | 903 | u32 mask, s = filter->data; | 
|  | 904 | enum rc_proto protocol = dev->wakeup_protocol; | 
|  | 905 |  | 
|  | 906 | if (protocol >= ARRAY_SIZE(protocols)) | 
|  | 907 | return -EINVAL; | 
|  | 908 |  | 
|  | 909 | mask = protocols[protocol].scancode_bits; | 
|  | 910 |  | 
|  | 911 | if (!rc_validate_scancode(protocol, s)) | 
|  | 912 | return -EINVAL; | 
|  | 913 |  | 
|  | 914 | filter->data &= mask; | 
|  | 915 | filter->mask &= mask; | 
|  | 916 |  | 
|  | 917 | /* | 
|  | 918 | * If we have to raw encode the IR for wakeup, we cannot have a mask | 
|  | 919 | */ | 
|  | 920 | if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask) | 
|  | 921 | return -EINVAL; | 
|  | 922 |  | 
|  | 923 | return 0; | 
|  | 924 | } | 
|  | 925 |  | 
|  | 926 | int rc_open(struct rc_dev *rdev) | 
|  | 927 | { | 
|  | 928 | int rval = 0; | 
|  | 929 |  | 
|  | 930 | if (!rdev) | 
|  | 931 | return -EINVAL; | 
|  | 932 |  | 
|  | 933 | mutex_lock(&rdev->lock); | 
|  | 934 |  | 
|  | 935 | if (!rdev->registered) { | 
|  | 936 | rval = -ENODEV; | 
|  | 937 | } else { | 
|  | 938 | if (!rdev->users++ && rdev->open) | 
|  | 939 | rval = rdev->open(rdev); | 
|  | 940 |  | 
|  | 941 | if (rval) | 
|  | 942 | rdev->users--; | 
|  | 943 | } | 
|  | 944 |  | 
|  | 945 | mutex_unlock(&rdev->lock); | 
|  | 946 |  | 
|  | 947 | return rval; | 
|  | 948 | } | 
|  | 949 |  | 
|  | 950 | static int ir_open(struct input_dev *idev) | 
|  | 951 | { | 
|  | 952 | struct rc_dev *rdev = input_get_drvdata(idev); | 
|  | 953 |  | 
|  | 954 | return rc_open(rdev); | 
|  | 955 | } | 
|  | 956 |  | 
|  | 957 | void rc_close(struct rc_dev *rdev) | 
|  | 958 | { | 
|  | 959 | if (rdev) { | 
|  | 960 | mutex_lock(&rdev->lock); | 
|  | 961 |  | 
|  | 962 | if (!--rdev->users && rdev->close && rdev->registered) | 
|  | 963 | rdev->close(rdev); | 
|  | 964 |  | 
|  | 965 | mutex_unlock(&rdev->lock); | 
|  | 966 | } | 
|  | 967 | } | 
|  | 968 |  | 
|  | 969 | static void ir_close(struct input_dev *idev) | 
|  | 970 | { | 
|  | 971 | struct rc_dev *rdev = input_get_drvdata(idev); | 
|  | 972 | rc_close(rdev); | 
|  | 973 | } | 
|  | 974 |  | 
|  | 975 | /* class for /sys/class/rc */ | 
|  | 976 | static char *rc_devnode(struct device *dev, umode_t *mode) | 
|  | 977 | { | 
|  | 978 | return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev)); | 
|  | 979 | } | 
|  | 980 |  | 
|  | 981 | static struct class rc_class = { | 
|  | 982 | .name		= "rc", | 
|  | 983 | .devnode	= rc_devnode, | 
|  | 984 | }; | 
|  | 985 |  | 
|  | 986 | /* | 
|  | 987 | * These are the protocol textual descriptions that are | 
|  | 988 | * used by the sysfs protocols file. Note that the order | 
|  | 989 | * of the entries is relevant. | 
|  | 990 | */ | 
|  | 991 | static const struct { | 
|  | 992 | u64	type; | 
|  | 993 | const char	*name; | 
|  | 994 | const char	*module_name; | 
|  | 995 | } proto_names[] = { | 
|  | 996 | { RC_PROTO_BIT_NONE,	"none",		NULL			}, | 
|  | 997 | { RC_PROTO_BIT_OTHER,	"other",	NULL			}, | 
|  | 998 | { RC_PROTO_BIT_UNKNOWN,	"unknown",	NULL			}, | 
|  | 999 | { RC_PROTO_BIT_RC5 | | 
|  | 1000 | RC_PROTO_BIT_RC5X_20,	"rc-5",		"ir-rc5-decoder"	}, | 
|  | 1001 | { RC_PROTO_BIT_NEC | | 
|  | 1002 | RC_PROTO_BIT_NECX | | 
|  | 1003 | RC_PROTO_BIT_NEC32,	"nec",		"ir-nec-decoder"	}, | 
|  | 1004 | { RC_PROTO_BIT_RC6_0 | | 
|  | 1005 | RC_PROTO_BIT_RC6_6A_20 | | 
|  | 1006 | RC_PROTO_BIT_RC6_6A_24 | | 
|  | 1007 | RC_PROTO_BIT_RC6_6A_32 | | 
|  | 1008 | RC_PROTO_BIT_RC6_MCE,	"rc-6",		"ir-rc6-decoder"	}, | 
|  | 1009 | { RC_PROTO_BIT_JVC,	"jvc",		"ir-jvc-decoder"	}, | 
|  | 1010 | { RC_PROTO_BIT_SONY12 | | 
|  | 1011 | RC_PROTO_BIT_SONY15 | | 
|  | 1012 | RC_PROTO_BIT_SONY20,	"sony",		"ir-sony-decoder"	}, | 
|  | 1013 | { RC_PROTO_BIT_RC5_SZ,	"rc-5-sz",	"ir-rc5-decoder"	}, | 
|  | 1014 | { RC_PROTO_BIT_SANYO,	"sanyo",	"ir-sanyo-decoder"	}, | 
|  | 1015 | { RC_PROTO_BIT_SHARP,	"sharp",	"ir-sharp-decoder"	}, | 
|  | 1016 | { RC_PROTO_BIT_MCIR2_KBD | | 
|  | 1017 | RC_PROTO_BIT_MCIR2_MSE, "mce_kbd",	"ir-mce_kbd-decoder"	}, | 
|  | 1018 | { RC_PROTO_BIT_XMP,	"xmp",		"ir-xmp-decoder"	}, | 
|  | 1019 | { RC_PROTO_BIT_CEC,	"cec",		NULL			}, | 
|  | 1020 | { RC_PROTO_BIT_IMON,	"imon",		"ir-imon-decoder"	}, | 
|  | 1021 | }; | 
|  | 1022 |  | 
|  | 1023 | /** | 
|  | 1024 | * struct rc_filter_attribute - Device attribute relating to a filter type. | 
|  | 1025 | * @attr:	Device attribute. | 
|  | 1026 | * @type:	Filter type. | 
|  | 1027 | * @mask:	false for filter value, true for filter mask. | 
|  | 1028 | */ | 
|  | 1029 | struct rc_filter_attribute { | 
|  | 1030 | struct device_attribute		attr; | 
|  | 1031 | enum rc_filter_type		type; | 
|  | 1032 | bool				mask; | 
|  | 1033 | }; | 
|  | 1034 | #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr) | 
|  | 1035 |  | 
|  | 1036 | #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask)	\ | 
|  | 1037 | struct rc_filter_attribute dev_attr_##_name = {			\ | 
|  | 1038 | .attr = __ATTR(_name, _mode, _show, _store),		\ | 
|  | 1039 | .type = (_type),					\ | 
|  | 1040 | .mask = (_mask),					\ | 
|  | 1041 | } | 
|  | 1042 |  | 
|  | 1043 | /** | 
|  | 1044 | * show_protocols() - shows the current IR protocol(s) | 
|  | 1045 | * @device:	the device descriptor | 
|  | 1046 | * @mattr:	the device attribute struct | 
|  | 1047 | * @buf:	a pointer to the output buffer | 
|  | 1048 | * | 
|  | 1049 | * This routine is a callback routine for input read the IR protocol type(s). | 
|  | 1050 | * it is trigged by reading /sys/class/rc/rc?/protocols. | 
|  | 1051 | * It returns the protocol names of supported protocols. | 
|  | 1052 | * Enabled protocols are printed in brackets. | 
|  | 1053 | * | 
|  | 1054 | * dev->lock is taken to guard against races between | 
|  | 1055 | * store_protocols and show_protocols. | 
|  | 1056 | */ | 
|  | 1057 | static ssize_t show_protocols(struct device *device, | 
|  | 1058 | struct device_attribute *mattr, char *buf) | 
|  | 1059 | { | 
|  | 1060 | struct rc_dev *dev = to_rc_dev(device); | 
|  | 1061 | u64 allowed, enabled; | 
|  | 1062 | char *tmp = buf; | 
|  | 1063 | int i; | 
|  | 1064 |  | 
|  | 1065 | mutex_lock(&dev->lock); | 
|  | 1066 |  | 
|  | 1067 | enabled = dev->enabled_protocols; | 
|  | 1068 | allowed = dev->allowed_protocols; | 
|  | 1069 | if (dev->raw && !allowed) | 
|  | 1070 | allowed = ir_raw_get_allowed_protocols(); | 
|  | 1071 |  | 
|  | 1072 | mutex_unlock(&dev->lock); | 
|  | 1073 |  | 
|  | 1074 | dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - 0x%llx\n", | 
|  | 1075 | __func__, (long long)allowed, (long long)enabled); | 
|  | 1076 |  | 
|  | 1077 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { | 
|  | 1078 | if (allowed & enabled & proto_names[i].type) | 
|  | 1079 | tmp += sprintf(tmp, "[%s] ", proto_names[i].name); | 
|  | 1080 | else if (allowed & proto_names[i].type) | 
|  | 1081 | tmp += sprintf(tmp, "%s ", proto_names[i].name); | 
|  | 1082 |  | 
|  | 1083 | if (allowed & proto_names[i].type) | 
|  | 1084 | allowed &= ~proto_names[i].type; | 
|  | 1085 | } | 
|  | 1086 |  | 
|  | 1087 | #ifdef CONFIG_LIRC | 
|  | 1088 | if (dev->driver_type == RC_DRIVER_IR_RAW) | 
|  | 1089 | tmp += sprintf(tmp, "[lirc] "); | 
|  | 1090 | #endif | 
|  | 1091 |  | 
|  | 1092 | if (tmp != buf) | 
|  | 1093 | tmp--; | 
|  | 1094 | *tmp = '\n'; | 
|  | 1095 |  | 
|  | 1096 | return tmp + 1 - buf; | 
|  | 1097 | } | 
|  | 1098 |  | 
|  | 1099 | /** | 
|  | 1100 | * parse_protocol_change() - parses a protocol change request | 
|  | 1101 | * @dev:	rc_dev device | 
|  | 1102 | * @protocols:	pointer to the bitmask of current protocols | 
|  | 1103 | * @buf:	pointer to the buffer with a list of changes | 
|  | 1104 | * | 
|  | 1105 | * Writing "+proto" will add a protocol to the protocol mask. | 
|  | 1106 | * Writing "-proto" will remove a protocol from protocol mask. | 
|  | 1107 | * Writing "proto" will enable only "proto". | 
|  | 1108 | * Writing "none" will disable all protocols. | 
|  | 1109 | * Returns the number of changes performed or a negative error code. | 
|  | 1110 | */ | 
|  | 1111 | static int parse_protocol_change(struct rc_dev *dev, u64 *protocols, | 
|  | 1112 | const char *buf) | 
|  | 1113 | { | 
|  | 1114 | const char *tmp; | 
|  | 1115 | unsigned count = 0; | 
|  | 1116 | bool enable, disable; | 
|  | 1117 | u64 mask; | 
|  | 1118 | int i; | 
|  | 1119 |  | 
|  | 1120 | while ((tmp = strsep((char **)&buf, " \n")) != NULL) { | 
|  | 1121 | if (!*tmp) | 
|  | 1122 | break; | 
|  | 1123 |  | 
|  | 1124 | if (*tmp == '+') { | 
|  | 1125 | enable = true; | 
|  | 1126 | disable = false; | 
|  | 1127 | tmp++; | 
|  | 1128 | } else if (*tmp == '-') { | 
|  | 1129 | enable = false; | 
|  | 1130 | disable = true; | 
|  | 1131 | tmp++; | 
|  | 1132 | } else { | 
|  | 1133 | enable = false; | 
|  | 1134 | disable = false; | 
|  | 1135 | } | 
|  | 1136 |  | 
|  | 1137 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { | 
|  | 1138 | if (!strcasecmp(tmp, proto_names[i].name)) { | 
|  | 1139 | mask = proto_names[i].type; | 
|  | 1140 | break; | 
|  | 1141 | } | 
|  | 1142 | } | 
|  | 1143 |  | 
|  | 1144 | if (i == ARRAY_SIZE(proto_names)) { | 
|  | 1145 | if (!strcasecmp(tmp, "lirc")) | 
|  | 1146 | mask = 0; | 
|  | 1147 | else { | 
|  | 1148 | dev_dbg(&dev->dev, "Unknown protocol: '%s'\n", | 
|  | 1149 | tmp); | 
|  | 1150 | return -EINVAL; | 
|  | 1151 | } | 
|  | 1152 | } | 
|  | 1153 |  | 
|  | 1154 | count++; | 
|  | 1155 |  | 
|  | 1156 | if (enable) | 
|  | 1157 | *protocols |= mask; | 
|  | 1158 | else if (disable) | 
|  | 1159 | *protocols &= ~mask; | 
|  | 1160 | else | 
|  | 1161 | *protocols = mask; | 
|  | 1162 | } | 
|  | 1163 |  | 
|  | 1164 | if (!count) { | 
|  | 1165 | dev_dbg(&dev->dev, "Protocol not specified\n"); | 
|  | 1166 | return -EINVAL; | 
|  | 1167 | } | 
|  | 1168 |  | 
|  | 1169 | return count; | 
|  | 1170 | } | 
|  | 1171 |  | 
|  | 1172 | void ir_raw_load_modules(u64 *protocols) | 
|  | 1173 | { | 
|  | 1174 | u64 available; | 
|  | 1175 | int i, ret; | 
|  | 1176 |  | 
|  | 1177 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { | 
|  | 1178 | if (proto_names[i].type == RC_PROTO_BIT_NONE || | 
|  | 1179 | proto_names[i].type & (RC_PROTO_BIT_OTHER | | 
|  | 1180 | RC_PROTO_BIT_UNKNOWN)) | 
|  | 1181 | continue; | 
|  | 1182 |  | 
|  | 1183 | available = ir_raw_get_allowed_protocols(); | 
|  | 1184 | if (!(*protocols & proto_names[i].type & ~available)) | 
|  | 1185 | continue; | 
|  | 1186 |  | 
|  | 1187 | if (!proto_names[i].module_name) { | 
|  | 1188 | pr_err("Can't enable IR protocol %s\n", | 
|  | 1189 | proto_names[i].name); | 
|  | 1190 | *protocols &= ~proto_names[i].type; | 
|  | 1191 | continue; | 
|  | 1192 | } | 
|  | 1193 |  | 
|  | 1194 | ret = request_module("%s", proto_names[i].module_name); | 
|  | 1195 | if (ret < 0) { | 
|  | 1196 | pr_err("Couldn't load IR protocol module %s\n", | 
|  | 1197 | proto_names[i].module_name); | 
|  | 1198 | *protocols &= ~proto_names[i].type; | 
|  | 1199 | continue; | 
|  | 1200 | } | 
|  | 1201 | msleep(20); | 
|  | 1202 | available = ir_raw_get_allowed_protocols(); | 
|  | 1203 | if (!(*protocols & proto_names[i].type & ~available)) | 
|  | 1204 | continue; | 
|  | 1205 |  | 
|  | 1206 | pr_err("Loaded IR protocol module %s, but protocol %s still not available\n", | 
|  | 1207 | proto_names[i].module_name, | 
|  | 1208 | proto_names[i].name); | 
|  | 1209 | *protocols &= ~proto_names[i].type; | 
|  | 1210 | } | 
|  | 1211 | } | 
|  | 1212 |  | 
|  | 1213 | /** | 
|  | 1214 | * store_protocols() - changes the current/wakeup IR protocol(s) | 
|  | 1215 | * @device:	the device descriptor | 
|  | 1216 | * @mattr:	the device attribute struct | 
|  | 1217 | * @buf:	a pointer to the input buffer | 
|  | 1218 | * @len:	length of the input buffer | 
|  | 1219 | * | 
|  | 1220 | * This routine is for changing the IR protocol type. | 
|  | 1221 | * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols. | 
|  | 1222 | * See parse_protocol_change() for the valid commands. | 
|  | 1223 | * Returns @len on success or a negative error code. | 
|  | 1224 | * | 
|  | 1225 | * dev->lock is taken to guard against races between | 
|  | 1226 | * store_protocols and show_protocols. | 
|  | 1227 | */ | 
|  | 1228 | static ssize_t store_protocols(struct device *device, | 
|  | 1229 | struct device_attribute *mattr, | 
|  | 1230 | const char *buf, size_t len) | 
|  | 1231 | { | 
|  | 1232 | struct rc_dev *dev = to_rc_dev(device); | 
|  | 1233 | u64 *current_protocols; | 
|  | 1234 | struct rc_scancode_filter *filter; | 
|  | 1235 | u64 old_protocols, new_protocols; | 
|  | 1236 | ssize_t rc; | 
|  | 1237 |  | 
|  | 1238 | dev_dbg(&dev->dev, "Normal protocol change requested\n"); | 
|  | 1239 | current_protocols = &dev->enabled_protocols; | 
|  | 1240 | filter = &dev->scancode_filter; | 
|  | 1241 |  | 
|  | 1242 | if (!dev->change_protocol) { | 
|  | 1243 | dev_dbg(&dev->dev, "Protocol switching not supported\n"); | 
|  | 1244 | return -EINVAL; | 
|  | 1245 | } | 
|  | 1246 |  | 
|  | 1247 | mutex_lock(&dev->lock); | 
|  | 1248 |  | 
|  | 1249 | old_protocols = *current_protocols; | 
|  | 1250 | new_protocols = old_protocols; | 
|  | 1251 | rc = parse_protocol_change(dev, &new_protocols, buf); | 
|  | 1252 | if (rc < 0) | 
|  | 1253 | goto out; | 
|  | 1254 |  | 
|  | 1255 | if (dev->driver_type == RC_DRIVER_IR_RAW) | 
|  | 1256 | ir_raw_load_modules(&new_protocols); | 
|  | 1257 |  | 
|  | 1258 | rc = dev->change_protocol(dev, &new_protocols); | 
|  | 1259 | if (rc < 0) { | 
|  | 1260 | dev_dbg(&dev->dev, "Error setting protocols to 0x%llx\n", | 
|  | 1261 | (long long)new_protocols); | 
|  | 1262 | goto out; | 
|  | 1263 | } | 
|  | 1264 |  | 
|  | 1265 | if (new_protocols != old_protocols) { | 
|  | 1266 | *current_protocols = new_protocols; | 
|  | 1267 | dev_dbg(&dev->dev, "Protocols changed to 0x%llx\n", | 
|  | 1268 | (long long)new_protocols); | 
|  | 1269 | } | 
|  | 1270 |  | 
|  | 1271 | /* | 
|  | 1272 | * If a protocol change was attempted the filter may need updating, even | 
|  | 1273 | * if the actual protocol mask hasn't changed (since the driver may have | 
|  | 1274 | * cleared the filter). | 
|  | 1275 | * Try setting the same filter with the new protocol (if any). | 
|  | 1276 | * Fall back to clearing the filter. | 
|  | 1277 | */ | 
|  | 1278 | if (dev->s_filter && filter->mask) { | 
|  | 1279 | if (new_protocols) | 
|  | 1280 | rc = dev->s_filter(dev, filter); | 
|  | 1281 | else | 
|  | 1282 | rc = -1; | 
|  | 1283 |  | 
|  | 1284 | if (rc < 0) { | 
|  | 1285 | filter->data = 0; | 
|  | 1286 | filter->mask = 0; | 
|  | 1287 | dev->s_filter(dev, filter); | 
|  | 1288 | } | 
|  | 1289 | } | 
|  | 1290 |  | 
|  | 1291 | rc = len; | 
|  | 1292 |  | 
|  | 1293 | out: | 
|  | 1294 | mutex_unlock(&dev->lock); | 
|  | 1295 | return rc; | 
|  | 1296 | } | 
|  | 1297 |  | 
|  | 1298 | /** | 
|  | 1299 | * show_filter() - shows the current scancode filter value or mask | 
|  | 1300 | * @device:	the device descriptor | 
|  | 1301 | * @attr:	the device attribute struct | 
|  | 1302 | * @buf:	a pointer to the output buffer | 
|  | 1303 | * | 
|  | 1304 | * This routine is a callback routine to read a scancode filter value or mask. | 
|  | 1305 | * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask]. | 
|  | 1306 | * It prints the current scancode filter value or mask of the appropriate filter | 
|  | 1307 | * type in hexadecimal into @buf and returns the size of the buffer. | 
|  | 1308 | * | 
|  | 1309 | * Bits of the filter value corresponding to set bits in the filter mask are | 
|  | 1310 | * compared against input scancodes and non-matching scancodes are discarded. | 
|  | 1311 | * | 
|  | 1312 | * dev->lock is taken to guard against races between | 
|  | 1313 | * store_filter and show_filter. | 
|  | 1314 | */ | 
|  | 1315 | static ssize_t show_filter(struct device *device, | 
|  | 1316 | struct device_attribute *attr, | 
|  | 1317 | char *buf) | 
|  | 1318 | { | 
|  | 1319 | struct rc_dev *dev = to_rc_dev(device); | 
|  | 1320 | struct rc_filter_attribute *fattr = to_rc_filter_attr(attr); | 
|  | 1321 | struct rc_scancode_filter *filter; | 
|  | 1322 | u32 val; | 
|  | 1323 |  | 
|  | 1324 | mutex_lock(&dev->lock); | 
|  | 1325 |  | 
|  | 1326 | if (fattr->type == RC_FILTER_NORMAL) | 
|  | 1327 | filter = &dev->scancode_filter; | 
|  | 1328 | else | 
|  | 1329 | filter = &dev->scancode_wakeup_filter; | 
|  | 1330 |  | 
|  | 1331 | if (fattr->mask) | 
|  | 1332 | val = filter->mask; | 
|  | 1333 | else | 
|  | 1334 | val = filter->data; | 
|  | 1335 | mutex_unlock(&dev->lock); | 
|  | 1336 |  | 
|  | 1337 | return sprintf(buf, "%#x\n", val); | 
|  | 1338 | } | 
|  | 1339 |  | 
|  | 1340 | /** | 
|  | 1341 | * store_filter() - changes the scancode filter value | 
|  | 1342 | * @device:	the device descriptor | 
|  | 1343 | * @attr:	the device attribute struct | 
|  | 1344 | * @buf:	a pointer to the input buffer | 
|  | 1345 | * @len:	length of the input buffer | 
|  | 1346 | * | 
|  | 1347 | * This routine is for changing a scancode filter value or mask. | 
|  | 1348 | * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask]. | 
|  | 1349 | * Returns -EINVAL if an invalid filter value for the current protocol was | 
|  | 1350 | * specified or if scancode filtering is not supported by the driver, otherwise | 
|  | 1351 | * returns @len. | 
|  | 1352 | * | 
|  | 1353 | * Bits of the filter value corresponding to set bits in the filter mask are | 
|  | 1354 | * compared against input scancodes and non-matching scancodes are discarded. | 
|  | 1355 | * | 
|  | 1356 | * dev->lock is taken to guard against races between | 
|  | 1357 | * store_filter and show_filter. | 
|  | 1358 | */ | 
|  | 1359 | static ssize_t store_filter(struct device *device, | 
|  | 1360 | struct device_attribute *attr, | 
|  | 1361 | const char *buf, size_t len) | 
|  | 1362 | { | 
|  | 1363 | struct rc_dev *dev = to_rc_dev(device); | 
|  | 1364 | struct rc_filter_attribute *fattr = to_rc_filter_attr(attr); | 
|  | 1365 | struct rc_scancode_filter new_filter, *filter; | 
|  | 1366 | int ret; | 
|  | 1367 | unsigned long val; | 
|  | 1368 | int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter); | 
|  | 1369 |  | 
|  | 1370 | ret = kstrtoul(buf, 0, &val); | 
|  | 1371 | if (ret < 0) | 
|  | 1372 | return ret; | 
|  | 1373 |  | 
|  | 1374 | if (fattr->type == RC_FILTER_NORMAL) { | 
|  | 1375 | set_filter = dev->s_filter; | 
|  | 1376 | filter = &dev->scancode_filter; | 
|  | 1377 | } else { | 
|  | 1378 | set_filter = dev->s_wakeup_filter; | 
|  | 1379 | filter = &dev->scancode_wakeup_filter; | 
|  | 1380 | } | 
|  | 1381 |  | 
|  | 1382 | if (!set_filter) | 
|  | 1383 | return -EINVAL; | 
|  | 1384 |  | 
|  | 1385 | mutex_lock(&dev->lock); | 
|  | 1386 |  | 
|  | 1387 | new_filter = *filter; | 
|  | 1388 | if (fattr->mask) | 
|  | 1389 | new_filter.mask = val; | 
|  | 1390 | else | 
|  | 1391 | new_filter.data = val; | 
|  | 1392 |  | 
|  | 1393 | if (fattr->type == RC_FILTER_WAKEUP) { | 
|  | 1394 | /* | 
|  | 1395 | * Refuse to set a filter unless a protocol is enabled | 
|  | 1396 | * and the filter is valid for that protocol | 
|  | 1397 | */ | 
|  | 1398 | if (dev->wakeup_protocol != RC_PROTO_UNKNOWN) | 
|  | 1399 | ret = rc_validate_filter(dev, &new_filter); | 
|  | 1400 | else | 
|  | 1401 | ret = -EINVAL; | 
|  | 1402 |  | 
|  | 1403 | if (ret != 0) | 
|  | 1404 | goto unlock; | 
|  | 1405 | } | 
|  | 1406 |  | 
|  | 1407 | if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols && | 
|  | 1408 | val) { | 
|  | 1409 | /* refuse to set a filter unless a protocol is enabled */ | 
|  | 1410 | ret = -EINVAL; | 
|  | 1411 | goto unlock; | 
|  | 1412 | } | 
|  | 1413 |  | 
|  | 1414 | ret = set_filter(dev, &new_filter); | 
|  | 1415 | if (ret < 0) | 
|  | 1416 | goto unlock; | 
|  | 1417 |  | 
|  | 1418 | *filter = new_filter; | 
|  | 1419 |  | 
|  | 1420 | unlock: | 
|  | 1421 | mutex_unlock(&dev->lock); | 
|  | 1422 | return (ret < 0) ? ret : len; | 
|  | 1423 | } | 
|  | 1424 |  | 
|  | 1425 | /** | 
|  | 1426 | * show_wakeup_protocols() - shows the wakeup IR protocol | 
|  | 1427 | * @device:	the device descriptor | 
|  | 1428 | * @mattr:	the device attribute struct | 
|  | 1429 | * @buf:	a pointer to the output buffer | 
|  | 1430 | * | 
|  | 1431 | * This routine is a callback routine for input read the IR protocol type(s). | 
|  | 1432 | * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols. | 
|  | 1433 | * It returns the protocol names of supported protocols. | 
|  | 1434 | * The enabled protocols are printed in brackets. | 
|  | 1435 | * | 
|  | 1436 | * dev->lock is taken to guard against races between | 
|  | 1437 | * store_wakeup_protocols and show_wakeup_protocols. | 
|  | 1438 | */ | 
|  | 1439 | static ssize_t show_wakeup_protocols(struct device *device, | 
|  | 1440 | struct device_attribute *mattr, | 
|  | 1441 | char *buf) | 
|  | 1442 | { | 
|  | 1443 | struct rc_dev *dev = to_rc_dev(device); | 
|  | 1444 | u64 allowed; | 
|  | 1445 | enum rc_proto enabled; | 
|  | 1446 | char *tmp = buf; | 
|  | 1447 | int i; | 
|  | 1448 |  | 
|  | 1449 | mutex_lock(&dev->lock); | 
|  | 1450 |  | 
|  | 1451 | allowed = dev->allowed_wakeup_protocols; | 
|  | 1452 | enabled = dev->wakeup_protocol; | 
|  | 1453 |  | 
|  | 1454 | mutex_unlock(&dev->lock); | 
|  | 1455 |  | 
|  | 1456 | dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - %d\n", | 
|  | 1457 | __func__, (long long)allowed, enabled); | 
|  | 1458 |  | 
|  | 1459 | for (i = 0; i < ARRAY_SIZE(protocols); i++) { | 
|  | 1460 | if (allowed & (1ULL << i)) { | 
|  | 1461 | if (i == enabled) | 
|  | 1462 | tmp += sprintf(tmp, "[%s] ", protocols[i].name); | 
|  | 1463 | else | 
|  | 1464 | tmp += sprintf(tmp, "%s ", protocols[i].name); | 
|  | 1465 | } | 
|  | 1466 | } | 
|  | 1467 |  | 
|  | 1468 | if (tmp != buf) | 
|  | 1469 | tmp--; | 
|  | 1470 | *tmp = '\n'; | 
|  | 1471 |  | 
|  | 1472 | return tmp + 1 - buf; | 
|  | 1473 | } | 
|  | 1474 |  | 
|  | 1475 | /** | 
|  | 1476 | * store_wakeup_protocols() - changes the wakeup IR protocol(s) | 
|  | 1477 | * @device:	the device descriptor | 
|  | 1478 | * @mattr:	the device attribute struct | 
|  | 1479 | * @buf:	a pointer to the input buffer | 
|  | 1480 | * @len:	length of the input buffer | 
|  | 1481 | * | 
|  | 1482 | * This routine is for changing the IR protocol type. | 
|  | 1483 | * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols. | 
|  | 1484 | * Returns @len on success or a negative error code. | 
|  | 1485 | * | 
|  | 1486 | * dev->lock is taken to guard against races between | 
|  | 1487 | * store_wakeup_protocols and show_wakeup_protocols. | 
|  | 1488 | */ | 
|  | 1489 | static ssize_t store_wakeup_protocols(struct device *device, | 
|  | 1490 | struct device_attribute *mattr, | 
|  | 1491 | const char *buf, size_t len) | 
|  | 1492 | { | 
|  | 1493 | struct rc_dev *dev = to_rc_dev(device); | 
|  | 1494 | enum rc_proto protocol; | 
|  | 1495 | ssize_t rc; | 
|  | 1496 | u64 allowed; | 
|  | 1497 | int i; | 
|  | 1498 |  | 
|  | 1499 | mutex_lock(&dev->lock); | 
|  | 1500 |  | 
|  | 1501 | allowed = dev->allowed_wakeup_protocols; | 
|  | 1502 |  | 
|  | 1503 | if (sysfs_streq(buf, "none")) { | 
|  | 1504 | protocol = RC_PROTO_UNKNOWN; | 
|  | 1505 | } else { | 
|  | 1506 | for (i = 0; i < ARRAY_SIZE(protocols); i++) { | 
|  | 1507 | if ((allowed & (1ULL << i)) && | 
|  | 1508 | sysfs_streq(buf, protocols[i].name)) { | 
|  | 1509 | protocol = i; | 
|  | 1510 | break; | 
|  | 1511 | } | 
|  | 1512 | } | 
|  | 1513 |  | 
|  | 1514 | if (i == ARRAY_SIZE(protocols)) { | 
|  | 1515 | rc = -EINVAL; | 
|  | 1516 | goto out; | 
|  | 1517 | } | 
|  | 1518 |  | 
|  | 1519 | if (dev->encode_wakeup) { | 
|  | 1520 | u64 mask = 1ULL << protocol; | 
|  | 1521 |  | 
|  | 1522 | ir_raw_load_modules(&mask); | 
|  | 1523 | if (!mask) { | 
|  | 1524 | rc = -EINVAL; | 
|  | 1525 | goto out; | 
|  | 1526 | } | 
|  | 1527 | } | 
|  | 1528 | } | 
|  | 1529 |  | 
|  | 1530 | if (dev->wakeup_protocol != protocol) { | 
|  | 1531 | dev->wakeup_protocol = protocol; | 
|  | 1532 | dev_dbg(&dev->dev, "Wakeup protocol changed to %d\n", protocol); | 
|  | 1533 |  | 
|  | 1534 | if (protocol == RC_PROTO_RC6_MCE) | 
|  | 1535 | dev->scancode_wakeup_filter.data = 0x800f0000; | 
|  | 1536 | else | 
|  | 1537 | dev->scancode_wakeup_filter.data = 0; | 
|  | 1538 | dev->scancode_wakeup_filter.mask = 0; | 
|  | 1539 |  | 
|  | 1540 | rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter); | 
|  | 1541 | if (rc == 0) | 
|  | 1542 | rc = len; | 
|  | 1543 | } else { | 
|  | 1544 | rc = len; | 
|  | 1545 | } | 
|  | 1546 |  | 
|  | 1547 | out: | 
|  | 1548 | mutex_unlock(&dev->lock); | 
|  | 1549 | return rc; | 
|  | 1550 | } | 
|  | 1551 |  | 
|  | 1552 | static void rc_dev_release(struct device *device) | 
|  | 1553 | { | 
|  | 1554 | struct rc_dev *dev = to_rc_dev(device); | 
|  | 1555 |  | 
|  | 1556 | kfree(dev); | 
|  | 1557 | } | 
|  | 1558 |  | 
|  | 1559 | #define ADD_HOTPLUG_VAR(fmt, val...)					\ | 
|  | 1560 | do {								\ | 
|  | 1561 | int err = add_uevent_var(env, fmt, val);		\ | 
|  | 1562 | if (err)						\ | 
|  | 1563 | return err;					\ | 
|  | 1564 | } while (0) | 
|  | 1565 |  | 
|  | 1566 | static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env) | 
|  | 1567 | { | 
|  | 1568 | struct rc_dev *dev = to_rc_dev(device); | 
|  | 1569 |  | 
|  | 1570 | if (dev->rc_map.name) | 
|  | 1571 | ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name); | 
|  | 1572 | if (dev->driver_name) | 
|  | 1573 | ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name); | 
|  | 1574 | if (dev->device_name) | 
|  | 1575 | ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name); | 
|  | 1576 |  | 
|  | 1577 | return 0; | 
|  | 1578 | } | 
|  | 1579 |  | 
|  | 1580 | /* | 
|  | 1581 | * Static device attribute struct with the sysfs attributes for IR's | 
|  | 1582 | */ | 
|  | 1583 | static struct device_attribute dev_attr_ro_protocols = | 
|  | 1584 | __ATTR(protocols, 0444, show_protocols, NULL); | 
|  | 1585 | static struct device_attribute dev_attr_rw_protocols = | 
|  | 1586 | __ATTR(protocols, 0644, show_protocols, store_protocols); | 
|  | 1587 | static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols, | 
|  | 1588 | store_wakeup_protocols); | 
|  | 1589 | static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR, | 
|  | 1590 | show_filter, store_filter, RC_FILTER_NORMAL, false); | 
|  | 1591 | static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR, | 
|  | 1592 | show_filter, store_filter, RC_FILTER_NORMAL, true); | 
|  | 1593 | static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR, | 
|  | 1594 | show_filter, store_filter, RC_FILTER_WAKEUP, false); | 
|  | 1595 | static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR, | 
|  | 1596 | show_filter, store_filter, RC_FILTER_WAKEUP, true); | 
|  | 1597 |  | 
|  | 1598 | static struct attribute *rc_dev_rw_protocol_attrs[] = { | 
|  | 1599 | &dev_attr_rw_protocols.attr, | 
|  | 1600 | NULL, | 
|  | 1601 | }; | 
|  | 1602 |  | 
|  | 1603 | static const struct attribute_group rc_dev_rw_protocol_attr_grp = { | 
|  | 1604 | .attrs	= rc_dev_rw_protocol_attrs, | 
|  | 1605 | }; | 
|  | 1606 |  | 
|  | 1607 | static struct attribute *rc_dev_ro_protocol_attrs[] = { | 
|  | 1608 | &dev_attr_ro_protocols.attr, | 
|  | 1609 | NULL, | 
|  | 1610 | }; | 
|  | 1611 |  | 
|  | 1612 | static const struct attribute_group rc_dev_ro_protocol_attr_grp = { | 
|  | 1613 | .attrs	= rc_dev_ro_protocol_attrs, | 
|  | 1614 | }; | 
|  | 1615 |  | 
|  | 1616 | static struct attribute *rc_dev_filter_attrs[] = { | 
|  | 1617 | &dev_attr_filter.attr.attr, | 
|  | 1618 | &dev_attr_filter_mask.attr.attr, | 
|  | 1619 | NULL, | 
|  | 1620 | }; | 
|  | 1621 |  | 
|  | 1622 | static const struct attribute_group rc_dev_filter_attr_grp = { | 
|  | 1623 | .attrs	= rc_dev_filter_attrs, | 
|  | 1624 | }; | 
|  | 1625 |  | 
|  | 1626 | static struct attribute *rc_dev_wakeup_filter_attrs[] = { | 
|  | 1627 | &dev_attr_wakeup_filter.attr.attr, | 
|  | 1628 | &dev_attr_wakeup_filter_mask.attr.attr, | 
|  | 1629 | &dev_attr_wakeup_protocols.attr, | 
|  | 1630 | NULL, | 
|  | 1631 | }; | 
|  | 1632 |  | 
|  | 1633 | static const struct attribute_group rc_dev_wakeup_filter_attr_grp = { | 
|  | 1634 | .attrs	= rc_dev_wakeup_filter_attrs, | 
|  | 1635 | }; | 
|  | 1636 |  | 
|  | 1637 | static const struct device_type rc_dev_type = { | 
|  | 1638 | .release	= rc_dev_release, | 
|  | 1639 | .uevent		= rc_dev_uevent, | 
|  | 1640 | }; | 
|  | 1641 |  | 
|  | 1642 | struct rc_dev *rc_allocate_device(enum rc_driver_type type) | 
|  | 1643 | { | 
|  | 1644 | struct rc_dev *dev; | 
|  | 1645 |  | 
|  | 1646 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | 
|  | 1647 | if (!dev) | 
|  | 1648 | return NULL; | 
|  | 1649 |  | 
|  | 1650 | if (type != RC_DRIVER_IR_RAW_TX) { | 
|  | 1651 | dev->input_dev = input_allocate_device(); | 
|  | 1652 | if (!dev->input_dev) { | 
|  | 1653 | kfree(dev); | 
|  | 1654 | return NULL; | 
|  | 1655 | } | 
|  | 1656 |  | 
|  | 1657 | dev->input_dev->getkeycode = ir_getkeycode; | 
|  | 1658 | dev->input_dev->setkeycode = ir_setkeycode; | 
|  | 1659 | input_set_drvdata(dev->input_dev, dev); | 
|  | 1660 |  | 
|  | 1661 | dev->timeout = IR_DEFAULT_TIMEOUT; | 
|  | 1662 | timer_setup(&dev->timer_keyup, ir_timer_keyup, 0); | 
|  | 1663 | timer_setup(&dev->timer_repeat, ir_timer_repeat, 0); | 
|  | 1664 |  | 
|  | 1665 | spin_lock_init(&dev->rc_map.lock); | 
|  | 1666 | spin_lock_init(&dev->keylock); | 
|  | 1667 | } | 
|  | 1668 | mutex_init(&dev->lock); | 
|  | 1669 |  | 
|  | 1670 | dev->dev.type = &rc_dev_type; | 
|  | 1671 | dev->dev.class = &rc_class; | 
|  | 1672 | device_initialize(&dev->dev); | 
|  | 1673 |  | 
|  | 1674 | dev->driver_type = type; | 
|  | 1675 |  | 
|  | 1676 | __module_get(THIS_MODULE); | 
|  | 1677 | return dev; | 
|  | 1678 | } | 
|  | 1679 | EXPORT_SYMBOL_GPL(rc_allocate_device); | 
|  | 1680 |  | 
|  | 1681 | void rc_free_device(struct rc_dev *dev) | 
|  | 1682 | { | 
|  | 1683 | if (!dev) | 
|  | 1684 | return; | 
|  | 1685 |  | 
|  | 1686 | input_free_device(dev->input_dev); | 
|  | 1687 |  | 
|  | 1688 | put_device(&dev->dev); | 
|  | 1689 |  | 
|  | 1690 | /* kfree(dev) will be called by the callback function | 
|  | 1691 | rc_dev_release() */ | 
|  | 1692 |  | 
|  | 1693 | module_put(THIS_MODULE); | 
|  | 1694 | } | 
|  | 1695 | EXPORT_SYMBOL_GPL(rc_free_device); | 
|  | 1696 |  | 
|  | 1697 | static void devm_rc_alloc_release(struct device *dev, void *res) | 
|  | 1698 | { | 
|  | 1699 | rc_free_device(*(struct rc_dev **)res); | 
|  | 1700 | } | 
|  | 1701 |  | 
|  | 1702 | struct rc_dev *devm_rc_allocate_device(struct device *dev, | 
|  | 1703 | enum rc_driver_type type) | 
|  | 1704 | { | 
|  | 1705 | struct rc_dev **dr, *rc; | 
|  | 1706 |  | 
|  | 1707 | dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL); | 
|  | 1708 | if (!dr) | 
|  | 1709 | return NULL; | 
|  | 1710 |  | 
|  | 1711 | rc = rc_allocate_device(type); | 
|  | 1712 | if (!rc) { | 
|  | 1713 | devres_free(dr); | 
|  | 1714 | return NULL; | 
|  | 1715 | } | 
|  | 1716 |  | 
|  | 1717 | rc->dev.parent = dev; | 
|  | 1718 | rc->managed_alloc = true; | 
|  | 1719 | *dr = rc; | 
|  | 1720 | devres_add(dev, dr); | 
|  | 1721 |  | 
|  | 1722 | return rc; | 
|  | 1723 | } | 
|  | 1724 | EXPORT_SYMBOL_GPL(devm_rc_allocate_device); | 
|  | 1725 |  | 
|  | 1726 | static int rc_prepare_rx_device(struct rc_dev *dev) | 
|  | 1727 | { | 
|  | 1728 | int rc; | 
|  | 1729 | struct rc_map *rc_map; | 
|  | 1730 | u64 rc_proto; | 
|  | 1731 |  | 
|  | 1732 | if (!dev->map_name) | 
|  | 1733 | return -EINVAL; | 
|  | 1734 |  | 
|  | 1735 | rc_map = rc_map_get(dev->map_name); | 
|  | 1736 | if (!rc_map) | 
|  | 1737 | rc_map = rc_map_get(RC_MAP_EMPTY); | 
|  | 1738 | if (!rc_map || !rc_map->scan || rc_map->size == 0) | 
|  | 1739 | return -EINVAL; | 
|  | 1740 |  | 
|  | 1741 | rc = ir_setkeytable(dev, rc_map); | 
|  | 1742 | if (rc) | 
|  | 1743 | return rc; | 
|  | 1744 |  | 
|  | 1745 | rc_proto = BIT_ULL(rc_map->rc_proto); | 
|  | 1746 |  | 
|  | 1747 | if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol) | 
|  | 1748 | dev->enabled_protocols = dev->allowed_protocols; | 
|  | 1749 |  | 
|  | 1750 | if (dev->driver_type == RC_DRIVER_IR_RAW) | 
|  | 1751 | ir_raw_load_modules(&rc_proto); | 
|  | 1752 |  | 
|  | 1753 | if (dev->change_protocol) { | 
|  | 1754 | rc = dev->change_protocol(dev, &rc_proto); | 
|  | 1755 | if (rc < 0) | 
|  | 1756 | goto out_table; | 
|  | 1757 | dev->enabled_protocols = rc_proto; | 
|  | 1758 | } | 
|  | 1759 |  | 
|  | 1760 | set_bit(EV_KEY, dev->input_dev->evbit); | 
|  | 1761 | set_bit(EV_REP, dev->input_dev->evbit); | 
|  | 1762 | set_bit(EV_MSC, dev->input_dev->evbit); | 
|  | 1763 | set_bit(MSC_SCAN, dev->input_dev->mscbit); | 
|  | 1764 | if (dev->open) | 
|  | 1765 | dev->input_dev->open = ir_open; | 
|  | 1766 | if (dev->close) | 
|  | 1767 | dev->input_dev->close = ir_close; | 
|  | 1768 |  | 
|  | 1769 | dev->input_dev->dev.parent = &dev->dev; | 
|  | 1770 | memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id)); | 
|  | 1771 | dev->input_dev->phys = dev->input_phys; | 
|  | 1772 | dev->input_dev->name = dev->device_name; | 
|  | 1773 |  | 
|  | 1774 | return 0; | 
|  | 1775 |  | 
|  | 1776 | out_table: | 
|  | 1777 | ir_free_table(&dev->rc_map); | 
|  | 1778 |  | 
|  | 1779 | return rc; | 
|  | 1780 | } | 
|  | 1781 |  | 
|  | 1782 | static int rc_setup_rx_device(struct rc_dev *dev) | 
|  | 1783 | { | 
|  | 1784 | int rc; | 
|  | 1785 |  | 
|  | 1786 | /* rc_open will be called here */ | 
|  | 1787 | rc = input_register_device(dev->input_dev); | 
|  | 1788 | if (rc) | 
|  | 1789 | return rc; | 
|  | 1790 |  | 
|  | 1791 | /* | 
|  | 1792 | * Default delay of 250ms is too short for some protocols, especially | 
|  | 1793 | * since the timeout is currently set to 250ms. Increase it to 500ms, | 
|  | 1794 | * to avoid wrong repetition of the keycodes. Note that this must be | 
|  | 1795 | * set after the call to input_register_device(). | 
|  | 1796 | */ | 
|  | 1797 | if (dev->allowed_protocols == RC_PROTO_BIT_CEC) | 
|  | 1798 | dev->input_dev->rep[REP_DELAY] = 0; | 
|  | 1799 | else | 
|  | 1800 | dev->input_dev->rep[REP_DELAY] = 500; | 
|  | 1801 |  | 
|  | 1802 | /* | 
|  | 1803 | * As a repeat event on protocols like RC-5 and NEC take as long as | 
|  | 1804 | * 110/114ms, using 33ms as a repeat period is not the right thing | 
|  | 1805 | * to do. | 
|  | 1806 | */ | 
|  | 1807 | dev->input_dev->rep[REP_PERIOD] = 125; | 
|  | 1808 |  | 
|  | 1809 | return 0; | 
|  | 1810 | } | 
|  | 1811 |  | 
|  | 1812 | static void rc_free_rx_device(struct rc_dev *dev) | 
|  | 1813 | { | 
|  | 1814 | if (!dev) | 
|  | 1815 | return; | 
|  | 1816 |  | 
|  | 1817 | if (dev->input_dev) { | 
|  | 1818 | input_unregister_device(dev->input_dev); | 
|  | 1819 | dev->input_dev = NULL; | 
|  | 1820 | } | 
|  | 1821 |  | 
|  | 1822 | ir_free_table(&dev->rc_map); | 
|  | 1823 | } | 
|  | 1824 |  | 
|  | 1825 | int rc_register_device(struct rc_dev *dev) | 
|  | 1826 | { | 
|  | 1827 | const char *path; | 
|  | 1828 | int attr = 0; | 
|  | 1829 | int minor; | 
|  | 1830 | int rc; | 
|  | 1831 |  | 
|  | 1832 | if (!dev) | 
|  | 1833 | return -EINVAL; | 
|  | 1834 |  | 
|  | 1835 | minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL); | 
|  | 1836 | if (minor < 0) | 
|  | 1837 | return minor; | 
|  | 1838 |  | 
|  | 1839 | dev->minor = minor; | 
|  | 1840 | dev_set_name(&dev->dev, "rc%u", dev->minor); | 
|  | 1841 | dev_set_drvdata(&dev->dev, dev); | 
|  | 1842 |  | 
|  | 1843 | dev->dev.groups = dev->sysfs_groups; | 
|  | 1844 | if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol) | 
|  | 1845 | dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp; | 
|  | 1846 | else if (dev->driver_type != RC_DRIVER_IR_RAW_TX) | 
|  | 1847 | dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp; | 
|  | 1848 | if (dev->s_filter) | 
|  | 1849 | dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp; | 
|  | 1850 | if (dev->s_wakeup_filter) | 
|  | 1851 | dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp; | 
|  | 1852 | dev->sysfs_groups[attr++] = NULL; | 
|  | 1853 |  | 
|  | 1854 | if (dev->driver_type == RC_DRIVER_IR_RAW) { | 
|  | 1855 | rc = ir_raw_event_prepare(dev); | 
|  | 1856 | if (rc < 0) | 
|  | 1857 | goto out_minor; | 
|  | 1858 | } | 
|  | 1859 |  | 
|  | 1860 | if (dev->driver_type != RC_DRIVER_IR_RAW_TX) { | 
|  | 1861 | rc = rc_prepare_rx_device(dev); | 
|  | 1862 | if (rc) | 
|  | 1863 | goto out_raw; | 
|  | 1864 | } | 
|  | 1865 |  | 
|  | 1866 | rc = device_add(&dev->dev); | 
|  | 1867 | if (rc) | 
|  | 1868 | goto out_rx_free; | 
|  | 1869 |  | 
|  | 1870 | path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); | 
|  | 1871 | dev_info(&dev->dev, "%s as %s\n", | 
|  | 1872 | dev->device_name ?: "Unspecified device", path ?: "N/A"); | 
|  | 1873 | kfree(path); | 
|  | 1874 |  | 
|  | 1875 | dev->registered = true; | 
|  | 1876 |  | 
|  | 1877 | if (dev->driver_type != RC_DRIVER_IR_RAW_TX) { | 
|  | 1878 | rc = rc_setup_rx_device(dev); | 
|  | 1879 | if (rc) | 
|  | 1880 | goto out_dev; | 
|  | 1881 | } | 
|  | 1882 |  | 
|  | 1883 | /* Ensure that the lirc kfifo is setup before we start the thread */ | 
|  | 1884 | if (dev->allowed_protocols != RC_PROTO_BIT_CEC) { | 
|  | 1885 | rc = ir_lirc_register(dev); | 
|  | 1886 | if (rc < 0) | 
|  | 1887 | goto out_rx; | 
|  | 1888 | } | 
|  | 1889 |  | 
|  | 1890 | if (dev->driver_type == RC_DRIVER_IR_RAW) { | 
|  | 1891 | rc = ir_raw_event_register(dev); | 
|  | 1892 | if (rc < 0) | 
|  | 1893 | goto out_lirc; | 
|  | 1894 | } | 
|  | 1895 |  | 
|  | 1896 | dev_dbg(&dev->dev, "Registered rc%u (driver: %s)\n", dev->minor, | 
|  | 1897 | dev->driver_name ? dev->driver_name : "unknown"); | 
|  | 1898 |  | 
|  | 1899 | return 0; | 
|  | 1900 |  | 
|  | 1901 | out_lirc: | 
|  | 1902 | if (dev->allowed_protocols != RC_PROTO_BIT_CEC) | 
|  | 1903 | ir_lirc_unregister(dev); | 
|  | 1904 | out_rx: | 
|  | 1905 | rc_free_rx_device(dev); | 
|  | 1906 | out_dev: | 
|  | 1907 | device_del(&dev->dev); | 
|  | 1908 | out_rx_free: | 
|  | 1909 | ir_free_table(&dev->rc_map); | 
|  | 1910 | out_raw: | 
|  | 1911 | ir_raw_event_free(dev); | 
|  | 1912 | out_minor: | 
|  | 1913 | ida_simple_remove(&rc_ida, minor); | 
|  | 1914 | return rc; | 
|  | 1915 | } | 
|  | 1916 | EXPORT_SYMBOL_GPL(rc_register_device); | 
|  | 1917 |  | 
|  | 1918 | static void devm_rc_release(struct device *dev, void *res) | 
|  | 1919 | { | 
|  | 1920 | rc_unregister_device(*(struct rc_dev **)res); | 
|  | 1921 | } | 
|  | 1922 |  | 
|  | 1923 | int devm_rc_register_device(struct device *parent, struct rc_dev *dev) | 
|  | 1924 | { | 
|  | 1925 | struct rc_dev **dr; | 
|  | 1926 | int ret; | 
|  | 1927 |  | 
|  | 1928 | dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL); | 
|  | 1929 | if (!dr) | 
|  | 1930 | return -ENOMEM; | 
|  | 1931 |  | 
|  | 1932 | ret = rc_register_device(dev); | 
|  | 1933 | if (ret) { | 
|  | 1934 | devres_free(dr); | 
|  | 1935 | return ret; | 
|  | 1936 | } | 
|  | 1937 |  | 
|  | 1938 | *dr = dev; | 
|  | 1939 | devres_add(parent, dr); | 
|  | 1940 |  | 
|  | 1941 | return 0; | 
|  | 1942 | } | 
|  | 1943 | EXPORT_SYMBOL_GPL(devm_rc_register_device); | 
|  | 1944 |  | 
|  | 1945 | void rc_unregister_device(struct rc_dev *dev) | 
|  | 1946 | { | 
|  | 1947 | if (!dev) | 
|  | 1948 | return; | 
|  | 1949 |  | 
|  | 1950 | if (dev->driver_type == RC_DRIVER_IR_RAW) | 
|  | 1951 | ir_raw_event_unregister(dev); | 
|  | 1952 |  | 
|  | 1953 | del_timer_sync(&dev->timer_keyup); | 
|  | 1954 | del_timer_sync(&dev->timer_repeat); | 
|  | 1955 |  | 
|  | 1956 | rc_free_rx_device(dev); | 
|  | 1957 |  | 
|  | 1958 | mutex_lock(&dev->lock); | 
|  | 1959 | if (dev->users && dev->close) | 
|  | 1960 | dev->close(dev); | 
|  | 1961 | dev->registered = false; | 
|  | 1962 | mutex_unlock(&dev->lock); | 
|  | 1963 |  | 
|  | 1964 | /* | 
|  | 1965 | * lirc device should be freed with dev->registered = false, so | 
|  | 1966 | * that userspace polling will get notified. | 
|  | 1967 | */ | 
|  | 1968 | if (dev->allowed_protocols != RC_PROTO_BIT_CEC) | 
|  | 1969 | ir_lirc_unregister(dev); | 
|  | 1970 |  | 
|  | 1971 | device_del(&dev->dev); | 
|  | 1972 |  | 
|  | 1973 | ida_simple_remove(&rc_ida, dev->minor); | 
|  | 1974 |  | 
|  | 1975 | if (!dev->managed_alloc) | 
|  | 1976 | rc_free_device(dev); | 
|  | 1977 | } | 
|  | 1978 |  | 
|  | 1979 | EXPORT_SYMBOL_GPL(rc_unregister_device); | 
|  | 1980 |  | 
|  | 1981 | /* | 
|  | 1982 | * Init/exit code for the module. Basically, creates/removes /sys/class/rc | 
|  | 1983 | */ | 
|  | 1984 |  | 
|  | 1985 | static int __init rc_core_init(void) | 
|  | 1986 | { | 
|  | 1987 | int rc = class_register(&rc_class); | 
|  | 1988 | if (rc) { | 
|  | 1989 | pr_err("rc_core: unable to register rc class\n"); | 
|  | 1990 | return rc; | 
|  | 1991 | } | 
|  | 1992 |  | 
|  | 1993 | rc = lirc_dev_init(); | 
|  | 1994 | if (rc) { | 
|  | 1995 | pr_err("rc_core: unable to init lirc\n"); | 
|  | 1996 | class_unregister(&rc_class); | 
|  | 1997 | return 0; | 
|  | 1998 | } | 
|  | 1999 |  | 
|  | 2000 | led_trigger_register_simple("rc-feedback", &led_feedback); | 
|  | 2001 | rc_map_register(&empty_map); | 
|  | 2002 |  | 
|  | 2003 | return 0; | 
|  | 2004 | } | 
|  | 2005 |  | 
|  | 2006 | static void __exit rc_core_exit(void) | 
|  | 2007 | { | 
|  | 2008 | lirc_dev_exit(); | 
|  | 2009 | class_unregister(&rc_class); | 
|  | 2010 | led_trigger_unregister_simple(led_feedback); | 
|  | 2011 | rc_map_unregister(&empty_map); | 
|  | 2012 | } | 
|  | 2013 |  | 
|  | 2014 | subsys_initcall(rc_core_init); | 
|  | 2015 | module_exit(rc_core_exit); | 
|  | 2016 |  | 
|  | 2017 | MODULE_AUTHOR("Mauro Carvalho Chehab"); | 
|  | 2018 | MODULE_LICENSE("GPL v2"); |