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