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