blob: 67a6bd5d94978e83f6bce2033ba8b67b4e850b53 [file] [log] [blame]
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001/* rc-core.c - handle IR scancode->keycode tables
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -03002 *
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03003 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030013 */
14
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030015#include <media/ir-core.h>
16#include <linux/spinlock.h>
17#include <linux/delay.h>
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030018#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030020#include <linux/device.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030021#include "rc-core-priv.h"
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030022
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030023#define IRRCV_NUM_DEVICES 256
24
25/* bit array to represent IR sysfs device number */
26static unsigned long ir_core_dev_number;
27
David Härdemanb3074c02010-04-02 15:58:28 -030028/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
29#define IR_TAB_MIN_SIZE 256
30#define IR_TAB_MAX_SIZE 8192
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030031
David Härdemana374fef2010-04-02 15:58:29 -030032/* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
33#define IR_KEYPRESS_TIMEOUT 250
34
David Härdeman4c7b3552010-11-10 11:04:19 -030035/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030036static LIST_HEAD(rc_map_list);
37static DEFINE_SPINLOCK(rc_map_lock);
38
David Härdeman4c7b3552010-11-10 11:04:19 -030039/* Forward declarations */
40static int ir_register_class(struct input_dev *input_dev);
41static void ir_unregister_class(struct input_dev *input_dev);
42static int ir_register_input(struct input_dev *input_dev);
43
44
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030045static struct rc_keymap *seek_rc_map(const char *name)
46{
47 struct rc_keymap *map = NULL;
48
49 spin_lock(&rc_map_lock);
50 list_for_each_entry(map, &rc_map_list, list) {
51 if (!strcmp(name, map->map.name)) {
52 spin_unlock(&rc_map_lock);
53 return map;
54 }
55 }
56 spin_unlock(&rc_map_lock);
57
58 return NULL;
59}
60
61struct ir_scancode_table *get_rc_map(const char *name)
62{
63
64 struct rc_keymap *map;
65
66 map = seek_rc_map(name);
67#ifdef MODULE
68 if (!map) {
69 int rc = request_module(name);
70 if (rc < 0) {
71 printk(KERN_ERR "Couldn't load IR keymap %s\n", name);
72 return NULL;
73 }
74 msleep(20); /* Give some time for IR to register */
75
76 map = seek_rc_map(name);
77 }
78#endif
79 if (!map) {
80 printk(KERN_ERR "IR keymap %s not found\n", name);
81 return NULL;
82 }
83
84 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
85
86 return &map->map;
87}
88EXPORT_SYMBOL_GPL(get_rc_map);
89
90int ir_register_map(struct rc_keymap *map)
91{
92 spin_lock(&rc_map_lock);
93 list_add_tail(&map->list, &rc_map_list);
94 spin_unlock(&rc_map_lock);
95 return 0;
96}
97EXPORT_SYMBOL_GPL(ir_register_map);
98
99void ir_unregister_map(struct rc_keymap *map)
100{
101 spin_lock(&rc_map_lock);
102 list_del(&map->list);
103 spin_unlock(&rc_map_lock);
104}
105EXPORT_SYMBOL_GPL(ir_unregister_map);
106
107
108static struct ir_scancode empty[] = {
109 { 0x2a, KEY_COFFEE },
110};
111
112static struct rc_keymap empty_map = {
113 .map = {
114 .scan = empty,
115 .size = ARRAY_SIZE(empty),
116 .ir_type = IR_TYPE_UNKNOWN, /* Legacy IR type */
117 .name = RC_MAP_EMPTY,
118 }
119};
120
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300121/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700122 * ir_create_table() - initializes a scancode table
123 * @rc_tab: the ir_scancode_table to initialize
124 * @name: name to assign to the table
125 * @ir_type: ir type to assign to the new table
126 * @size: initial size of the table
127 * @return: zero on success or a negative error code
128 *
129 * This routine will initialize the ir_scancode_table and will allocate
130 * memory to hold at least the specified number elements.
131 */
132static int ir_create_table(struct ir_scancode_table *rc_tab,
133 const char *name, u64 ir_type, size_t size)
134{
135 rc_tab->name = name;
136 rc_tab->ir_type = ir_type;
137 rc_tab->alloc = roundup_pow_of_two(size * sizeof(struct ir_scancode));
138 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
139 rc_tab->scan = kmalloc(rc_tab->alloc, GFP_KERNEL);
140 if (!rc_tab->scan)
141 return -ENOMEM;
142
143 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
144 rc_tab->size, rc_tab->alloc);
145 return 0;
146}
147
148/**
149 * ir_free_table() - frees memory allocated by a scancode table
150 * @rc_tab: the table whose mappings need to be freed
151 *
152 * This routine will free memory alloctaed for key mappings used by given
153 * scancode table.
154 */
155static void ir_free_table(struct ir_scancode_table *rc_tab)
156{
157 rc_tab->size = 0;
158 kfree(rc_tab->scan);
159 rc_tab->scan = NULL;
160}
161
162/**
David Härdemanb3074c02010-04-02 15:58:28 -0300163 * ir_resize_table() - resizes a scancode table if necessary
164 * @rc_tab: the ir_scancode_table to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700165 * @gfp_flags: gfp flags to use when allocating memory
David Härdemanb3074c02010-04-02 15:58:28 -0300166 * @return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300167 *
David Härdemanb3074c02010-04-02 15:58:28 -0300168 * This routine will shrink the ir_scancode_table if it has lots of
169 * unused entries and grow it if it is full.
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300170 */
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700171static int ir_resize_table(struct ir_scancode_table *rc_tab, gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300172{
173 unsigned int oldalloc = rc_tab->alloc;
174 unsigned int newalloc = oldalloc;
175 struct ir_scancode *oldscan = rc_tab->scan;
176 struct ir_scancode *newscan;
177
178 if (rc_tab->size == rc_tab->len) {
179 /* All entries in use -> grow keytable */
180 if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
181 return -ENOMEM;
182
183 newalloc *= 2;
184 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
185 }
186
187 if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
188 /* Less than 1/3 of entries in use -> shrink keytable */
189 newalloc /= 2;
190 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
191 }
192
193 if (newalloc == oldalloc)
194 return 0;
195
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700196 newscan = kmalloc(newalloc, gfp_flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300197 if (!newscan) {
198 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
199 return -ENOMEM;
200 }
201
202 memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
203 rc_tab->scan = newscan;
204 rc_tab->alloc = newalloc;
205 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
206 kfree(oldscan);
207 return 0;
208}
209
210/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700211 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemanb3074c02010-04-02 15:58:28 -0300212 * @dev: the struct input_dev device descriptor
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700213 * @rc_tab: scancode table to be adjusted
214 * @index: index of the mapping that needs to be updated
215 * @keycode: the desired keycode
216 * @return: previous keycode assigned to the mapping
David Härdemanb3074c02010-04-02 15:58:28 -0300217 *
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700218 * This routine is used to update scancode->keycopde mapping at given
219 * position.
David Härdemanb3074c02010-04-02 15:58:28 -0300220 */
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700221static unsigned int ir_update_mapping(struct input_dev *dev,
222 struct ir_scancode_table *rc_tab,
223 unsigned int index,
224 unsigned int new_keycode)
225{
226 int old_keycode = rc_tab->scan[index].keycode;
227 int i;
228
229 /* Did the user wish to remove the mapping? */
230 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
231 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
232 index, rc_tab->scan[index].scancode);
233 rc_tab->len--;
234 memmove(&rc_tab->scan[index], &rc_tab->scan[index+ 1],
235 (rc_tab->len - index) * sizeof(struct ir_scancode));
236 } else {
237 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
238 index,
239 old_keycode == KEY_RESERVED ? "New" : "Replacing",
240 rc_tab->scan[index].scancode, new_keycode);
241 rc_tab->scan[index].keycode = new_keycode;
242 __set_bit(new_keycode, dev->keybit);
243 }
244
245 if (old_keycode != KEY_RESERVED) {
246 /* A previous mapping was updated... */
247 __clear_bit(old_keycode, dev->keybit);
248 /* ... but another scancode might use the same keycode */
249 for (i = 0; i < rc_tab->len; i++) {
250 if (rc_tab->scan[i].keycode == old_keycode) {
251 __set_bit(old_keycode, dev->keybit);
252 break;
253 }
254 }
255
256 /* Possibly shrink the keytable, failure is not a problem */
257 ir_resize_table(rc_tab, GFP_ATOMIC);
258 }
259
260 return old_keycode;
261}
262
263/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300264 * ir_establish_scancode() - set a keycode in the scancode->keycode table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700265 * @ir_dev: the struct ir_input_dev device descriptor
266 * @rc_tab: scancode table to be searched
267 * @scancode: the desired scancode
268 * @resize: controls whether we allowed to resize the table to
269 * accomodate not yet present scancodes
270 * @return: index of the mapping containing scancode in question
271 * or -1U in case of failure.
272 *
273 * This routine is used to locate given scancode in ir_scancode_table.
274 * If scancode is not yet present the routine will allocate a new slot
275 * for it.
276 */
277static unsigned int ir_establish_scancode(struct ir_input_dev *ir_dev,
278 struct ir_scancode_table *rc_tab,
279 unsigned int scancode,
280 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300281{
282 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300283
284 /*
285 * Unfortunately, some hardware-based IR decoders don't provide
286 * all bits for the complete IR code. In general, they provide only
287 * the command part of the IR code. Yet, as it is possible to replace
288 * the provided IR with another one, it is needed to allow loading
289 * IR tables from other remotes. So,
290 */
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700291 if (ir_dev->props && ir_dev->props->scanmask)
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300292 scancode &= ir_dev->props->scanmask;
David Härdemanb3074c02010-04-02 15:58:28 -0300293
294 /* First check if we already have a mapping for this ir command */
295 for (i = 0; i < rc_tab->len; i++) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700296 if (rc_tab->scan[i].scancode == scancode)
297 return i;
298
David Härdemanb3074c02010-04-02 15:58:28 -0300299 /* Keytable is sorted from lowest to highest scancode */
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700300 if (rc_tab->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300301 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300302 }
303
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700304 /* No previous mapping found, we might need to grow the table */
305 if (rc_tab->size == rc_tab->len) {
306 if (!resize || ir_resize_table(rc_tab, GFP_ATOMIC))
307 return -1U;
308 }
David Härdemanb3074c02010-04-02 15:58:28 -0300309
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700310 /* i is the proper index to insert our new keycode */
311 if (i < rc_tab->len)
David Härdemanb3074c02010-04-02 15:58:28 -0300312 memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
313 (rc_tab->len - i) * sizeof(struct ir_scancode));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700314 rc_tab->scan[i].scancode = scancode;
315 rc_tab->scan[i].keycode = KEY_RESERVED;
316 rc_tab->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300317
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700318 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300319}
320
321/**
322 * ir_setkeycode() - set a keycode in the scancode->keycode table
323 * @dev: the struct input_dev device descriptor
324 * @scancode: the desired scancode
325 * @keycode: result
326 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
327 *
328 * This routine is used to handle evdev EVIOCSKEY ioctl.
329 */
330static int ir_setkeycode(struct input_dev *dev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700331 const struct input_keymap_entry *ke,
332 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300333{
David Härdemanb3074c02010-04-02 15:58:28 -0300334 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
335 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700336 unsigned int index;
337 unsigned int scancode;
338 int retval;
339 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300340
341 spin_lock_irqsave(&rc_tab->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700342
343 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
344 index = ke->index;
345 if (index >= rc_tab->len) {
346 retval = -EINVAL;
347 goto out;
348 }
349 } else {
350 retval = input_scancode_to_scalar(ke, &scancode);
351 if (retval)
352 goto out;
353
354 index = ir_establish_scancode(ir_dev, rc_tab, scancode, true);
355 if (index >= rc_tab->len) {
356 retval = -ENOMEM;
357 goto out;
358 }
359 }
360
361 *old_keycode = ir_update_mapping(dev, rc_tab, index, ke->keycode);
362
363out:
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300364 spin_unlock_irqrestore(&rc_tab->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700365 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300366}
367
368/**
David Härdemanb3074c02010-04-02 15:58:28 -0300369 * ir_setkeytable() - sets several entries in the scancode->keycode table
370 * @dev: the struct input_dev device descriptor
371 * @to: the struct ir_scancode_table to copy entries to
372 * @from: the struct ir_scancode_table to copy entries from
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700373 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300374 *
David Härdemanb3074c02010-04-02 15:58:28 -0300375 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300376 */
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700377static int ir_setkeytable(struct ir_input_dev *ir_dev,
David Härdemanb3074c02010-04-02 15:58:28 -0300378 const struct ir_scancode_table *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300379{
David Härdemanb3074c02010-04-02 15:58:28 -0300380 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700381 unsigned int i, index;
382 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300383
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700384 rc = ir_create_table(&ir_dev->rc_tab,
385 from->name, from->ir_type, from->size);
386 if (rc)
387 return rc;
388
389 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
390 rc_tab->size, rc_tab->alloc);
391
David Härdemanb3074c02010-04-02 15:58:28 -0300392 for (i = 0; i < from->size; i++) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700393 index = ir_establish_scancode(ir_dev, rc_tab,
394 from->scan[i].scancode, false);
395 if (index >= rc_tab->len) {
396 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300397 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700398 }
399
400 ir_update_mapping(ir_dev->input_dev, rc_tab, index,
401 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300402 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700403
404 if (rc)
405 ir_free_table(rc_tab);
406
David Härdemanb3074c02010-04-02 15:58:28 -0300407 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300408}
409
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300410/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700411 * ir_lookup_by_scancode() - locate mapping by scancode
412 * @rc_tab: the &struct ir_scancode_table to search
413 * @scancode: scancode to look for in the table
414 * @return: index in the table, -1U if not found
415 *
416 * This routine performs binary search in RC keykeymap table for
417 * given scancode.
418 */
419static unsigned int ir_lookup_by_scancode(const struct ir_scancode_table *rc_tab,
420 unsigned int scancode)
421{
David Härdeman0d070252010-10-30 22:17:44 +0200422 int start = 0;
423 int end = rc_tab->len - 1;
424 int mid;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700425
426 while (start <= end) {
427 mid = (start + end) / 2;
428 if (rc_tab->scan[mid].scancode < scancode)
429 start = mid + 1;
430 else if (rc_tab->scan[mid].scancode > scancode)
431 end = mid - 1;
432 else
433 return mid;
434 }
435
436 return -1U;
437}
438
439/**
David Härdemanb3074c02010-04-02 15:58:28 -0300440 * ir_getkeycode() - get a keycode from the scancode->keycode table
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300441 * @dev: the struct input_dev device descriptor
442 * @scancode: the desired scancode
David Härdemanb3074c02010-04-02 15:58:28 -0300443 * @keycode: used to return the keycode, if found, or KEY_RESERVED
444 * @return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300445 *
446 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300447 */
448static int ir_getkeycode(struct input_dev *dev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700449 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300450{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300451 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
452 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700453 struct ir_scancode *entry;
454 unsigned long flags;
455 unsigned int index;
456 unsigned int scancode;
457 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300458
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300459 spin_lock_irqsave(&rc_tab->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700460
461 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
462 index = ke->index;
463 } else {
464 retval = input_scancode_to_scalar(ke, &scancode);
465 if (retval)
466 goto out;
467
468 index = ir_lookup_by_scancode(rc_tab, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300469 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700470
471 if (index >= rc_tab->len) {
472 if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
473 IR_dprintk(1, "unknown key for scancode 0x%04x\n",
474 scancode);
475 retval = -EINVAL;
476 goto out;
477 }
478
479 entry = &rc_tab->scan[index];
480
481 ke->index = index;
482 ke->keycode = entry->keycode;
483 ke->len = sizeof(entry->scancode);
484 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
485
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700486 retval = 0;
487
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700488out:
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300489 spin_unlock_irqrestore(&rc_tab->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700490 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300491}
492
493/**
494 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300495 * @input_dev: the struct input_dev descriptor of the device
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300496 * @scancode: the scancode that we're seeking
497 *
498 * This routine is used by the input routines when a key is pressed at the
499 * IR. The scancode is received and needs to be converted into a keycode.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300500 * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300501 * corresponding keycode from the table.
502 */
503u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
504{
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700505 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
506 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
507 unsigned int keycode;
508 unsigned int index;
509 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300510
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700511 spin_lock_irqsave(&rc_tab->lock, flags);
512
513 index = ir_lookup_by_scancode(rc_tab, scancode);
514 keycode = index < rc_tab->len ?
515 rc_tab->scan[index].keycode : KEY_RESERVED;
516
517 spin_unlock_irqrestore(&rc_tab->lock, flags);
518
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300519 if (keycode != KEY_RESERVED)
520 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
521 dev->name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700522
David Härdemanb3074c02010-04-02 15:58:28 -0300523 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300524}
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -0300525EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300526
527/**
David Härdeman62c65032010-10-29 16:08:07 -0300528 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemana374fef2010-04-02 15:58:29 -0300529 * @ir: the struct ir_input_dev descriptor of the device
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300530 *
David Härdeman62c65032010-10-29 16:08:07 -0300531 * This function is used internally to release a keypress, it must be
532 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300533 */
David Härdeman62c65032010-10-29 16:08:07 -0300534static void ir_do_keyup(struct ir_input_dev *ir)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300535{
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300536 if (!ir->keypressed)
537 return;
538
David Härdemana374fef2010-04-02 15:58:29 -0300539 IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
540 input_report_key(ir->input_dev, ir->last_keycode, 0);
541 input_sync(ir->input_dev);
542 ir->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300543}
David Härdeman62c65032010-10-29 16:08:07 -0300544
545/**
546 * ir_keyup() - generates input event to signal the release of a keypress
547 * @dev: the struct input_dev descriptor of the device
548 *
549 * This routine is used to signal that a key has been released on the
550 * remote control.
551 */
552void ir_keyup(struct input_dev *dev)
553{
554 unsigned long flags;
555 struct ir_input_dev *ir = input_get_drvdata(dev);
556
557 spin_lock_irqsave(&ir->keylock, flags);
558 ir_do_keyup(ir);
559 spin_unlock_irqrestore(&ir->keylock, flags);
560}
Jarod Wilsonee089402010-09-15 15:31:12 -0300561EXPORT_SYMBOL_GPL(ir_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300562
563/**
564 * ir_timer_keyup() - generates a keyup event after a timeout
565 * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
566 *
567 * This routine will generate a keyup event some time after a keydown event
568 * is generated when no further activity has been detected.
569 */
570static void ir_timer_keyup(unsigned long cookie)
571{
572 struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
573 unsigned long flags;
574
575 /*
576 * ir->keyup_jiffies is used to prevent a race condition if a
577 * hardware interrupt occurs at this point and the keyup timer
578 * event is moved further into the future as a result.
579 *
580 * The timer will then be reactivated and this function called
581 * again in the future. We need to exit gracefully in that case
582 * to allow the input subsystem to do its auto-repeat magic or
583 * a keyup event might follow immediately after the keydown.
584 */
585 spin_lock_irqsave(&ir->keylock, flags);
Maxim Levitskye0172fd2010-09-06 18:26:09 -0300586 if (time_is_before_eq_jiffies(ir->keyup_jiffies))
David Härdeman62c65032010-10-29 16:08:07 -0300587 ir_do_keyup(ir);
David Härdemana374fef2010-04-02 15:58:29 -0300588 spin_unlock_irqrestore(&ir->keylock, flags);
589}
590
591/**
592 * ir_repeat() - notifies the IR core that a key is still pressed
593 * @dev: the struct input_dev descriptor of the device
594 *
595 * This routine is used by IR decoders when a repeat message which does
596 * not include the necessary bits to reproduce the scancode has been
597 * received.
598 */
599void ir_repeat(struct input_dev *dev)
600{
601 unsigned long flags;
602 struct ir_input_dev *ir = input_get_drvdata(dev);
603
604 spin_lock_irqsave(&ir->keylock, flags);
605
Maxim Levitskyed4d3872010-07-31 11:59:24 -0300606 input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
607
David Härdemana374fef2010-04-02 15:58:29 -0300608 if (!ir->keypressed)
609 goto out;
610
611 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
612 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
613
614out:
615 spin_unlock_irqrestore(&ir->keylock, flags);
616}
617EXPORT_SYMBOL_GPL(ir_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300618
619/**
David Härdeman62c65032010-10-29 16:08:07 -0300620 * ir_do_keydown() - internal function to process a keypress
621 * @dev: the struct input_dev descriptor of the device
622 * @scancode: the scancode of the keypress
623 * @keycode: the keycode of the keypress
624 * @toggle: the toggle value of the keypress
625 *
626 * This function is used internally to register a keypress, it must be
627 * called with keylock held.
628 */
629static void ir_do_keydown(struct input_dev *dev, int scancode,
630 u32 keycode, u8 toggle)
631{
632 struct ir_input_dev *ir = input_get_drvdata(dev);
633
634 input_event(dev, EV_MSC, MSC_SCAN, scancode);
635
636 /* Repeat event? */
637 if (ir->keypressed &&
638 ir->last_scancode == scancode &&
639 ir->last_toggle == toggle)
640 return;
641
642 /* Release old keypress */
643 ir_do_keyup(ir);
644
645 ir->last_scancode = scancode;
646 ir->last_toggle = toggle;
647 ir->last_keycode = keycode;
648
649 if (keycode == KEY_RESERVED)
650 return;
651
652 /* Register a keypress */
653 ir->keypressed = true;
654 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
655 dev->name, keycode, scancode);
656 input_report_key(dev, ir->last_keycode, 1);
657 input_sync(dev);
658}
659
660/**
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300661 * ir_keydown() - generates input event for a key press
David Härdemana374fef2010-04-02 15:58:29 -0300662 * @dev: the struct input_dev descriptor of the device
663 * @scancode: the scancode that we're seeking
664 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
665 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300666 *
667 * This routine is used by the input routines when a key is pressed at the
668 * IR. It gets the keycode for a scancode and reports an input event via
669 * input_report_key().
670 */
David Härdemana374fef2010-04-02 15:58:29 -0300671void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300672{
David Härdemana374fef2010-04-02 15:58:29 -0300673 unsigned long flags;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300674 struct ir_input_dev *ir = input_get_drvdata(dev);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300675 u32 keycode = ir_g_keycode_from_table(dev, scancode);
676
David Härdemana374fef2010-04-02 15:58:29 -0300677 spin_lock_irqsave(&ir->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300678 ir_do_keydown(dev, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300679
David Härdeman62c65032010-10-29 16:08:07 -0300680 if (ir->keypressed) {
681 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
682 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
683 }
David Härdemana374fef2010-04-02 15:58:29 -0300684 spin_unlock_irqrestore(&ir->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300685}
686EXPORT_SYMBOL_GPL(ir_keydown);
687
David Härdeman62c65032010-10-29 16:08:07 -0300688/**
689 * ir_keydown_notimeout() - generates input event for a key press without
690 * an automatic keyup event at a later time
691 * @dev: the struct input_dev descriptor of the device
692 * @scancode: the scancode that we're seeking
693 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
694 * support toggle values, this should be set to zero)
695 *
696 * This routine is used by the input routines when a key is pressed at the
697 * IR. It gets the keycode for a scancode and reports an input event via
698 * input_report_key(). The driver must manually call ir_keyup() at a later
699 * stage.
700 */
701void ir_keydown_notimeout(struct input_dev *dev, int scancode, u8 toggle)
702{
703 unsigned long flags;
704 struct ir_input_dev *ir = input_get_drvdata(dev);
705 u32 keycode = ir_g_keycode_from_table(dev, scancode);
706
707 spin_lock_irqsave(&ir->keylock, flags);
708 ir_do_keydown(dev, scancode, keycode, toggle);
709 spin_unlock_irqrestore(&ir->keylock, flags);
710}
711EXPORT_SYMBOL_GPL(ir_keydown_notimeout);
712
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300713static int ir_open(struct input_dev *input_dev)
714{
715 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
716
717 return ir_dev->props->open(ir_dev->props->priv);
718}
719
720static void ir_close(struct input_dev *input_dev)
721{
722 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
723
724 ir_dev->props->close(ir_dev->props->priv);
725}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300726
727/**
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300728 * __ir_input_register() - sets the IR keycode table and add the handlers
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300729 * for keymap table get/set
730 * @input_dev: the struct input_dev descriptor of the device
731 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
732 *
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300733 * This routine is used to initialize the input infrastructure
734 * to work with an IR.
735 * It will register the input/evdev interface for the device and
736 * register the syfs code for IR class
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300737 */
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300738int __ir_input_register(struct input_dev *input_dev,
Mauro Carvalho Chehabe93854d2009-12-14 00:16:55 -0300739 const struct ir_scancode_table *rc_tab,
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300740 struct ir_dev_props *props,
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -0300741 const char *driver_name)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300742{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300743 struct ir_input_dev *ir_dev;
David Härdemanb3074c02010-04-02 15:58:28 -0300744 int rc;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300745
746 if (rc_tab->scan == NULL || !rc_tab->size)
747 return -EINVAL;
748
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300749 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
750 if (!ir_dev)
751 return -ENOMEM;
752
David Härdemanb3074c02010-04-02 15:58:28 -0300753 ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
754 if (!ir_dev->driver_name) {
755 rc = -ENOMEM;
756 goto out_dev;
Alexander Beregalov82311522010-01-09 13:51:14 -0300757 }
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300758
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700759 input_dev->getkeycode_new = ir_getkeycode;
760 input_dev->setkeycode_new = ir_setkeycode;
David Härdemanb3074c02010-04-02 15:58:28 -0300761 input_set_drvdata(input_dev, ir_dev);
David Härdemana374fef2010-04-02 15:58:29 -0300762 ir_dev->input_dev = input_dev;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300763
David Härdemanb3074c02010-04-02 15:58:28 -0300764 spin_lock_init(&ir_dev->rc_tab.lock);
David Härdemana374fef2010-04-02 15:58:29 -0300765 spin_lock_init(&ir_dev->keylock);
766 setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
767
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300768 if (props) {
769 ir_dev->props = props;
770 if (props->open)
771 input_dev->open = ir_open;
772 if (props->close)
773 input_dev->close = ir_close;
774 }
David Härdemanb3074c02010-04-02 15:58:28 -0300775
David Härdemanb3074c02010-04-02 15:58:28 -0300776 set_bit(EV_KEY, input_dev->evbit);
David Härdemana374fef2010-04-02 15:58:29 -0300777 set_bit(EV_REP, input_dev->evbit);
Maxim Levitskyed4d3872010-07-31 11:59:24 -0300778 set_bit(EV_MSC, input_dev->evbit);
779 set_bit(MSC_SCAN, input_dev->mscbit);
David Härdemana374fef2010-04-02 15:58:29 -0300780
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700781 rc = ir_setkeytable(ir_dev, rc_tab);
782 if (rc)
783 goto out_name;
David Härdemanb3074c02010-04-02 15:58:28 -0300784
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300785 rc = ir_register_class(input_dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300786 if (rc < 0)
David Härdemanb3074c02010-04-02 15:58:28 -0300787 goto out_table;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300788
Igor M. Liplianin84b14f12010-05-26 23:31:21 -0300789 if (ir_dev->props)
790 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
791 rc = ir_raw_event_register(input_dev);
792 if (rc < 0)
793 goto out_event;
794 }
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300795
Maxim Levitsky58b3dd42010-09-06 18:26:07 -0300796 rc = ir_register_input(input_dev);
Mauro Carvalho Chehab991369e2010-10-14 17:49:33 -0300797 if (rc < 0)
798 goto out_event;
Maxim Levitsky58b3dd42010-09-06 18:26:07 -0300799
Mauro Carvalho Chehab844a9e92010-08-01 17:19:29 -0300800 IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
801 driver_name, rc_tab->name,
Dan Carpenterede67a32010-08-06 03:31:00 -0300802 (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
803 " in raw mode" : "");
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300804
Mauro Carvalho Chehab04cab1312010-09-08 12:58:12 -0300805 /*
806 * Default delay of 250ms is too short for some protocols, expecially
807 * since the timeout is currently set to 250ms. Increase it to 500ms,
808 * to avoid wrong repetition of the keycodes.
809 */
810 input_dev->rep[REP_DELAY] = 500;
811
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300812 return 0;
813
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300814out_event:
815 ir_unregister_class(input_dev);
David Härdemanb3074c02010-04-02 15:58:28 -0300816out_table:
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700817 ir_free_table(&ir_dev->rc_tab);
David Härdemanb3074c02010-04-02 15:58:28 -0300818out_name:
819 kfree(ir_dev->driver_name);
820out_dev:
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300821 kfree(ir_dev);
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300822 return rc;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300823}
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300824EXPORT_SYMBOL_GPL(__ir_input_register);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300825
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300826/**
827 * ir_input_unregister() - unregisters IR and frees resources
828 * @input_dev: the struct input_dev descriptor of the device
829
830 * This routine is used to free memory and de-register interfaces.
831 */
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300832void ir_input_unregister(struct input_dev *input_dev)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300833{
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300834 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300835
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300836 if (!ir_dev)
Mauro Carvalho Chehab05395a32009-12-06 08:32:49 -0300837 return;
838
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300839 IR_dprintk(1, "Freed keycode table\n");
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300840
David Härdemana374fef2010-04-02 15:58:29 -0300841 del_timer_sync(&ir_dev->timer_keyup);
Igor M. Liplianin84b14f12010-05-26 23:31:21 -0300842 if (ir_dev->props)
843 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
844 ir_raw_event_unregister(input_dev);
845
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700846 ir_free_table(&ir_dev->rc_tab);
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300847
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300848 ir_unregister_class(input_dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300849
David Härdemanb3074c02010-04-02 15:58:28 -0300850 kfree(ir_dev->driver_name);
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300851 kfree(ir_dev);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300852}
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300853EXPORT_SYMBOL_GPL(ir_input_unregister);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300854
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300855/* class for /sys/class/rc */
856static char *ir_devnode(struct device *dev, mode_t *mode)
857{
858 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
859}
860
861static struct class ir_input_class = {
862 .name = "rc",
863 .devnode = ir_devnode,
864};
865
866static struct {
867 u64 type;
868 char *name;
869} proto_names[] = {
870 { IR_TYPE_UNKNOWN, "unknown" },
871 { IR_TYPE_RC5, "rc-5" },
872 { IR_TYPE_NEC, "nec" },
873 { IR_TYPE_RC6, "rc-6" },
874 { IR_TYPE_JVC, "jvc" },
875 { IR_TYPE_SONY, "sony" },
876 { IR_TYPE_RC5_SZ, "rc-5-sz" },
877 { IR_TYPE_LIRC, "lirc" },
878};
879
880#define PROTO_NONE "none"
881
882/**
883 * show_protocols() - shows the current IR protocol(s)
884 * @d: the device descriptor
885 * @mattr: the device attribute struct (unused)
886 * @buf: a pointer to the output buffer
887 *
888 * This routine is a callback routine for input read the IR protocol type(s).
889 * it is trigged by reading /sys/class/rc/rc?/protocols.
890 * It returns the protocol names of supported protocols.
891 * Enabled protocols are printed in brackets.
892 */
893static ssize_t show_protocols(struct device *d,
894 struct device_attribute *mattr, char *buf)
895{
896 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
897 u64 allowed, enabled;
898 char *tmp = buf;
899 int i;
900
901 /* Device is being removed */
902 if (!ir_dev)
903 return -EINVAL;
904
905 if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
906 enabled = ir_dev->rc_tab.ir_type;
907 allowed = ir_dev->props->allowed_protos;
908 } else if (ir_dev->raw) {
909 enabled = ir_dev->raw->enabled_protocols;
910 allowed = ir_raw_get_allowed_protocols();
911 } else
912 return sprintf(tmp, "[builtin]\n");
913
914 IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
915 (long long)allowed,
916 (long long)enabled);
917
918 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
919 if (allowed & enabled & proto_names[i].type)
920 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
921 else if (allowed & proto_names[i].type)
922 tmp += sprintf(tmp, "%s ", proto_names[i].name);
923 }
924
925 if (tmp != buf)
926 tmp--;
927 *tmp = '\n';
928 return tmp + 1 - buf;
929}
930
931/**
932 * store_protocols() - changes the current IR protocol(s)
933 * @d: the device descriptor
934 * @mattr: the device attribute struct (unused)
935 * @buf: a pointer to the input buffer
936 * @len: length of the input buffer
937 *
938 * This routine is a callback routine for changing the IR protocol type.
939 * It is trigged by writing to /sys/class/rc/rc?/protocols.
940 * Writing "+proto" will add a protocol to the list of enabled protocols.
941 * Writing "-proto" will remove a protocol from the list of enabled protocols.
942 * Writing "proto" will enable only "proto".
943 * Writing "none" will disable all protocols.
944 * Returns -EINVAL if an invalid protocol combination or unknown protocol name
945 * is used, otherwise @len.
946 */
947static ssize_t store_protocols(struct device *d,
948 struct device_attribute *mattr,
949 const char *data,
950 size_t len)
951{
952 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
953 bool enable, disable;
954 const char *tmp;
955 u64 type;
956 u64 mask;
957 int rc, i, count = 0;
958 unsigned long flags;
959
960 /* Device is being removed */
961 if (!ir_dev)
962 return -EINVAL;
963
964 if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
965 type = ir_dev->rc_tab.ir_type;
966 else if (ir_dev->raw)
967 type = ir_dev->raw->enabled_protocols;
968 else {
969 IR_dprintk(1, "Protocol switching not supported\n");
970 return -EINVAL;
971 }
972
973 while ((tmp = strsep((char **) &data, " \n")) != NULL) {
974 if (!*tmp)
975 break;
976
977 if (*tmp == '+') {
978 enable = true;
979 disable = false;
980 tmp++;
981 } else if (*tmp == '-') {
982 enable = false;
983 disable = true;
984 tmp++;
985 } else {
986 enable = false;
987 disable = false;
988 }
989
990 if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
991 tmp += sizeof(PROTO_NONE);
992 mask = 0;
993 count++;
994 } else {
995 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
996 if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
997 tmp += strlen(proto_names[i].name);
998 mask = proto_names[i].type;
999 break;
1000 }
1001 }
1002 if (i == ARRAY_SIZE(proto_names)) {
1003 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1004 return -EINVAL;
1005 }
1006 count++;
1007 }
1008
1009 if (enable)
1010 type |= mask;
1011 else if (disable)
1012 type &= ~mask;
1013 else
1014 type = mask;
1015 }
1016
1017 if (!count) {
1018 IR_dprintk(1, "Protocol not specified\n");
1019 return -EINVAL;
1020 }
1021
1022 if (ir_dev->props && ir_dev->props->change_protocol) {
1023 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
1024 type);
1025 if (rc < 0) {
1026 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1027 (long long)type);
1028 return -EINVAL;
1029 }
1030 }
1031
1032 if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
1033 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
1034 ir_dev->rc_tab.ir_type = type;
1035 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
1036 } else {
1037 ir_dev->raw->enabled_protocols = type;
1038 }
1039
1040 IR_dprintk(1, "Current protocol(s): 0x%llx\n",
1041 (long long)type);
1042
1043 return len;
1044}
1045
1046#define ADD_HOTPLUG_VAR(fmt, val...) \
1047 do { \
1048 int err = add_uevent_var(env, fmt, val); \
1049 if (err) \
1050 return err; \
1051 } while (0)
1052
1053static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1054{
1055 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
1056
1057 if (ir_dev->rc_tab.name)
1058 ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
1059 if (ir_dev->driver_name)
1060 ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
1061
1062 return 0;
1063}
1064
1065/*
1066 * Static device attribute struct with the sysfs attributes for IR's
1067 */
1068static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
1069 show_protocols, store_protocols);
1070
1071static struct attribute *rc_dev_attrs[] = {
1072 &dev_attr_protocols.attr,
1073 NULL,
1074};
1075
1076static struct attribute_group rc_dev_attr_grp = {
1077 .attrs = rc_dev_attrs,
1078};
1079
1080static const struct attribute_group *rc_dev_attr_groups[] = {
1081 &rc_dev_attr_grp,
1082 NULL
1083};
1084
1085static struct device_type rc_dev_type = {
1086 .groups = rc_dev_attr_groups,
1087 .uevent = rc_dev_uevent,
1088};
1089
1090/**
1091 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
1092 * @input_dev: the struct input_dev descriptor of the device
1093 *
1094 * This routine is used to register the syfs code for IR class
1095 */
David Härdeman4c7b3552010-11-10 11:04:19 -03001096static int ir_register_class(struct input_dev *input_dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001097{
1098 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1099 int devno = find_first_zero_bit(&ir_core_dev_number,
1100 IRRCV_NUM_DEVICES);
1101
1102 if (unlikely(devno < 0))
1103 return devno;
1104
1105 ir_dev->dev.type = &rc_dev_type;
1106 ir_dev->devno = devno;
1107
1108 ir_dev->dev.class = &ir_input_class;
1109 ir_dev->dev.parent = input_dev->dev.parent;
1110 input_dev->dev.parent = &ir_dev->dev;
1111 dev_set_name(&ir_dev->dev, "rc%d", devno);
1112 dev_set_drvdata(&ir_dev->dev, ir_dev);
1113 return device_register(&ir_dev->dev);
1114};
1115
1116/**
1117 * ir_register_input - registers ir input device with input subsystem
1118 * @input_dev: the struct input_dev descriptor of the device
1119 */
1120
David Härdeman4c7b3552010-11-10 11:04:19 -03001121static int ir_register_input(struct input_dev *input_dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001122{
1123 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1124 int rc;
1125 const char *path;
1126
1127
1128 rc = input_register_device(input_dev);
1129 if (rc < 0) {
1130 device_del(&ir_dev->dev);
1131 return rc;
1132 }
1133
1134 __module_get(THIS_MODULE);
1135
1136 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
1137 printk(KERN_INFO "%s: %s as %s\n",
1138 dev_name(&ir_dev->dev),
1139 input_dev->name ? input_dev->name : "Unspecified device",
1140 path ? path : "N/A");
1141 kfree(path);
1142
1143 set_bit(ir_dev->devno, &ir_core_dev_number);
1144 return 0;
1145}
1146
1147/**
1148 * ir_unregister_class() - removes the sysfs for sysfs for
1149 * /sys/class/rc/rc?
1150 * @input_dev: the struct input_dev descriptor of the device
1151 *
1152 * This routine is used to unregister the syfs code for IR class
1153 */
David Härdeman4c7b3552010-11-10 11:04:19 -03001154static void ir_unregister_class(struct input_dev *input_dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001155{
1156 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1157
1158 input_set_drvdata(input_dev, NULL);
1159 clear_bit(ir_dev->devno, &ir_core_dev_number);
1160 input_unregister_device(input_dev);
1161 device_del(&ir_dev->dev);
1162
1163 module_put(THIS_MODULE);
1164}
1165
1166/*
1167 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1168 */
1169
1170static int __init ir_core_init(void)
1171{
1172 int rc = class_register(&ir_input_class);
1173 if (rc) {
1174 printk(KERN_ERR "ir_core: unable to register rc class\n");
1175 return rc;
1176 }
1177
1178 /* Initialize/load the decoders/keymap code that will be used */
1179 ir_raw_init();
David Härdeman4c7b3552010-11-10 11:04:19 -03001180 ir_register_map(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001181
1182 return 0;
1183}
1184
1185static void __exit ir_core_exit(void)
1186{
1187 class_unregister(&ir_input_class);
David Härdeman4c7b3552010-11-10 11:04:19 -03001188 ir_unregister_map(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001189}
1190
1191module_init(ir_core_init);
1192module_exit(ir_core_exit);
1193
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001194int ir_core_debug; /* ir_debug level (0,1,2) */
1195EXPORT_SYMBOL_GPL(ir_core_debug);
1196module_param_named(debug, ir_core_debug, int, 0644);
1197
1198MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1199MODULE_LICENSE("GPL");