blob: d7726776f937c186ef6ce44185678da645139304 [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 Chehabef53a112009-11-27 22:01:23 -030015
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030016#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030018#include <linux/device.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030019#include "rc-core-priv.h"
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030020
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030021#define IRRCV_NUM_DEVICES 256
22
23/* bit array to represent IR sysfs device number */
24static unsigned long ir_core_dev_number;
25
David Härdemanb3074c02010-04-02 15:58:28 -030026/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
27#define IR_TAB_MIN_SIZE 256
28#define IR_TAB_MAX_SIZE 8192
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030029
David Härdemana374fef2010-04-02 15:58:29 -030030/* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
31#define IR_KEYPRESS_TIMEOUT 250
32
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030033/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -070034 * ir_create_table() - initializes a scancode table
35 * @rc_tab: the ir_scancode_table to initialize
36 * @name: name to assign to the table
37 * @ir_type: ir type to assign to the new table
38 * @size: initial size of the table
39 * @return: zero on success or a negative error code
40 *
41 * This routine will initialize the ir_scancode_table and will allocate
42 * memory to hold at least the specified number elements.
43 */
44static int ir_create_table(struct ir_scancode_table *rc_tab,
45 const char *name, u64 ir_type, size_t size)
46{
47 rc_tab->name = name;
48 rc_tab->ir_type = ir_type;
49 rc_tab->alloc = roundup_pow_of_two(size * sizeof(struct ir_scancode));
50 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
51 rc_tab->scan = kmalloc(rc_tab->alloc, GFP_KERNEL);
52 if (!rc_tab->scan)
53 return -ENOMEM;
54
55 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
56 rc_tab->size, rc_tab->alloc);
57 return 0;
58}
59
60/**
61 * ir_free_table() - frees memory allocated by a scancode table
62 * @rc_tab: the table whose mappings need to be freed
63 *
64 * This routine will free memory alloctaed for key mappings used by given
65 * scancode table.
66 */
67static void ir_free_table(struct ir_scancode_table *rc_tab)
68{
69 rc_tab->size = 0;
70 kfree(rc_tab->scan);
71 rc_tab->scan = NULL;
72}
73
74/**
David Härdemanb3074c02010-04-02 15:58:28 -030075 * ir_resize_table() - resizes a scancode table if necessary
76 * @rc_tab: the ir_scancode_table to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -070077 * @gfp_flags: gfp flags to use when allocating memory
David Härdemanb3074c02010-04-02 15:58:28 -030078 * @return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -030079 *
David Härdemanb3074c02010-04-02 15:58:28 -030080 * This routine will shrink the ir_scancode_table if it has lots of
81 * unused entries and grow it if it is full.
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -030082 */
Dmitry Torokhov9f470092010-09-09 21:59:11 -070083static int ir_resize_table(struct ir_scancode_table *rc_tab, gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -030084{
85 unsigned int oldalloc = rc_tab->alloc;
86 unsigned int newalloc = oldalloc;
87 struct ir_scancode *oldscan = rc_tab->scan;
88 struct ir_scancode *newscan;
89
90 if (rc_tab->size == rc_tab->len) {
91 /* All entries in use -> grow keytable */
92 if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
93 return -ENOMEM;
94
95 newalloc *= 2;
96 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
97 }
98
99 if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
100 /* Less than 1/3 of entries in use -> shrink keytable */
101 newalloc /= 2;
102 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
103 }
104
105 if (newalloc == oldalloc)
106 return 0;
107
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700108 newscan = kmalloc(newalloc, gfp_flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300109 if (!newscan) {
110 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
111 return -ENOMEM;
112 }
113
114 memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
115 rc_tab->scan = newscan;
116 rc_tab->alloc = newalloc;
117 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
118 kfree(oldscan);
119 return 0;
120}
121
122/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700123 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemanb3074c02010-04-02 15:58:28 -0300124 * @dev: the struct input_dev device descriptor
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700125 * @rc_tab: scancode table to be adjusted
126 * @index: index of the mapping that needs to be updated
127 * @keycode: the desired keycode
128 * @return: previous keycode assigned to the mapping
David Härdemanb3074c02010-04-02 15:58:28 -0300129 *
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700130 * This routine is used to update scancode->keycopde mapping at given
131 * position.
David Härdemanb3074c02010-04-02 15:58:28 -0300132 */
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700133static unsigned int ir_update_mapping(struct input_dev *dev,
134 struct ir_scancode_table *rc_tab,
135 unsigned int index,
136 unsigned int new_keycode)
137{
138 int old_keycode = rc_tab->scan[index].keycode;
139 int i;
140
141 /* Did the user wish to remove the mapping? */
142 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
143 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
144 index, rc_tab->scan[index].scancode);
145 rc_tab->len--;
146 memmove(&rc_tab->scan[index], &rc_tab->scan[index+ 1],
147 (rc_tab->len - index) * sizeof(struct ir_scancode));
148 } else {
149 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
150 index,
151 old_keycode == KEY_RESERVED ? "New" : "Replacing",
152 rc_tab->scan[index].scancode, new_keycode);
153 rc_tab->scan[index].keycode = new_keycode;
154 __set_bit(new_keycode, dev->keybit);
155 }
156
157 if (old_keycode != KEY_RESERVED) {
158 /* A previous mapping was updated... */
159 __clear_bit(old_keycode, dev->keybit);
160 /* ... but another scancode might use the same keycode */
161 for (i = 0; i < rc_tab->len; i++) {
162 if (rc_tab->scan[i].keycode == old_keycode) {
163 __set_bit(old_keycode, dev->keybit);
164 break;
165 }
166 }
167
168 /* Possibly shrink the keytable, failure is not a problem */
169 ir_resize_table(rc_tab, GFP_ATOMIC);
170 }
171
172 return old_keycode;
173}
174
175/**
176 * ir_locate_scancode() - set a keycode in the scancode->keycode table
177 * @ir_dev: the struct ir_input_dev device descriptor
178 * @rc_tab: scancode table to be searched
179 * @scancode: the desired scancode
180 * @resize: controls whether we allowed to resize the table to
181 * accomodate not yet present scancodes
182 * @return: index of the mapping containing scancode in question
183 * or -1U in case of failure.
184 *
185 * This routine is used to locate given scancode in ir_scancode_table.
186 * If scancode is not yet present the routine will allocate a new slot
187 * for it.
188 */
189static unsigned int ir_establish_scancode(struct ir_input_dev *ir_dev,
190 struct ir_scancode_table *rc_tab,
191 unsigned int scancode,
192 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300193{
194 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300195
196 /*
197 * Unfortunately, some hardware-based IR decoders don't provide
198 * all bits for the complete IR code. In general, they provide only
199 * the command part of the IR code. Yet, as it is possible to replace
200 * the provided IR with another one, it is needed to allow loading
201 * IR tables from other remotes. So,
202 */
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700203 if (ir_dev->props && ir_dev->props->scanmask)
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300204 scancode &= ir_dev->props->scanmask;
David Härdemanb3074c02010-04-02 15:58:28 -0300205
206 /* First check if we already have a mapping for this ir command */
207 for (i = 0; i < rc_tab->len; i++) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700208 if (rc_tab->scan[i].scancode == scancode)
209 return i;
210
David Härdemanb3074c02010-04-02 15:58:28 -0300211 /* Keytable is sorted from lowest to highest scancode */
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700212 if (rc_tab->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300213 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300214 }
215
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700216 /* No previous mapping found, we might need to grow the table */
217 if (rc_tab->size == rc_tab->len) {
218 if (!resize || ir_resize_table(rc_tab, GFP_ATOMIC))
219 return -1U;
220 }
David Härdemanb3074c02010-04-02 15:58:28 -0300221
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700222 /* i is the proper index to insert our new keycode */
223 if (i < rc_tab->len)
David Härdemanb3074c02010-04-02 15:58:28 -0300224 memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
225 (rc_tab->len - i) * sizeof(struct ir_scancode));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700226 rc_tab->scan[i].scancode = scancode;
227 rc_tab->scan[i].keycode = KEY_RESERVED;
228 rc_tab->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300229
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700230 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300231}
232
233/**
234 * ir_setkeycode() - set a keycode in the scancode->keycode table
235 * @dev: the struct input_dev device descriptor
236 * @scancode: the desired scancode
237 * @keycode: result
238 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
239 *
240 * This routine is used to handle evdev EVIOCSKEY ioctl.
241 */
242static int ir_setkeycode(struct input_dev *dev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700243 const struct input_keymap_entry *ke,
244 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300245{
David Härdemanb3074c02010-04-02 15:58:28 -0300246 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
247 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700248 unsigned int index;
249 unsigned int scancode;
250 int retval;
251 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300252
253 spin_lock_irqsave(&rc_tab->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700254
255 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
256 index = ke->index;
257 if (index >= rc_tab->len) {
258 retval = -EINVAL;
259 goto out;
260 }
261 } else {
262 retval = input_scancode_to_scalar(ke, &scancode);
263 if (retval)
264 goto out;
265
266 index = ir_establish_scancode(ir_dev, rc_tab, scancode, true);
267 if (index >= rc_tab->len) {
268 retval = -ENOMEM;
269 goto out;
270 }
271 }
272
273 *old_keycode = ir_update_mapping(dev, rc_tab, index, ke->keycode);
274
275out:
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300276 spin_unlock_irqrestore(&rc_tab->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700277 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300278}
279
280/**
David Härdemanb3074c02010-04-02 15:58:28 -0300281 * ir_setkeytable() - sets several entries in the scancode->keycode table
282 * @dev: the struct input_dev device descriptor
283 * @to: the struct ir_scancode_table to copy entries to
284 * @from: the struct ir_scancode_table to copy entries from
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700285 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300286 *
David Härdemanb3074c02010-04-02 15:58:28 -0300287 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300288 */
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700289static int ir_setkeytable(struct ir_input_dev *ir_dev,
David Härdemanb3074c02010-04-02 15:58:28 -0300290 const struct ir_scancode_table *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300291{
David Härdemanb3074c02010-04-02 15:58:28 -0300292 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700293 unsigned int i, index;
294 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300295
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700296 rc = ir_create_table(&ir_dev->rc_tab,
297 from->name, from->ir_type, from->size);
298 if (rc)
299 return rc;
300
301 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
302 rc_tab->size, rc_tab->alloc);
303
David Härdemanb3074c02010-04-02 15:58:28 -0300304 for (i = 0; i < from->size; i++) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700305 index = ir_establish_scancode(ir_dev, rc_tab,
306 from->scan[i].scancode, false);
307 if (index >= rc_tab->len) {
308 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300309 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700310 }
311
312 ir_update_mapping(ir_dev->input_dev, rc_tab, index,
313 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300314 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700315
316 if (rc)
317 ir_free_table(rc_tab);
318
David Härdemanb3074c02010-04-02 15:58:28 -0300319 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300320}
321
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300322/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700323 * ir_lookup_by_scancode() - locate mapping by scancode
324 * @rc_tab: the &struct ir_scancode_table to search
325 * @scancode: scancode to look for in the table
326 * @return: index in the table, -1U if not found
327 *
328 * This routine performs binary search in RC keykeymap table for
329 * given scancode.
330 */
331static unsigned int ir_lookup_by_scancode(const struct ir_scancode_table *rc_tab,
332 unsigned int scancode)
333{
David Härdeman0d070252010-10-30 22:17:44 +0200334 int start = 0;
335 int end = rc_tab->len - 1;
336 int mid;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700337
338 while (start <= end) {
339 mid = (start + end) / 2;
340 if (rc_tab->scan[mid].scancode < scancode)
341 start = mid + 1;
342 else if (rc_tab->scan[mid].scancode > scancode)
343 end = mid - 1;
344 else
345 return mid;
346 }
347
348 return -1U;
349}
350
351/**
David Härdemanb3074c02010-04-02 15:58:28 -0300352 * ir_getkeycode() - get a keycode from the scancode->keycode table
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300353 * @dev: the struct input_dev device descriptor
354 * @scancode: the desired scancode
David Härdemanb3074c02010-04-02 15:58:28 -0300355 * @keycode: used to return the keycode, if found, or KEY_RESERVED
356 * @return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300357 *
358 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300359 */
360static int ir_getkeycode(struct input_dev *dev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700361 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300362{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300363 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
364 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700365 struct ir_scancode *entry;
366 unsigned long flags;
367 unsigned int index;
368 unsigned int scancode;
369 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300370
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300371 spin_lock_irqsave(&rc_tab->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700372
373 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
374 index = ke->index;
375 } else {
376 retval = input_scancode_to_scalar(ke, &scancode);
377 if (retval)
378 goto out;
379
380 index = ir_lookup_by_scancode(rc_tab, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300381 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700382
383 if (index >= rc_tab->len) {
384 if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
385 IR_dprintk(1, "unknown key for scancode 0x%04x\n",
386 scancode);
387 retval = -EINVAL;
388 goto out;
389 }
390
391 entry = &rc_tab->scan[index];
392
393 ke->index = index;
394 ke->keycode = entry->keycode;
395 ke->len = sizeof(entry->scancode);
396 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
397
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700398 retval = 0;
399
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700400out:
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300401 spin_unlock_irqrestore(&rc_tab->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700402 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300403}
404
405/**
406 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300407 * @input_dev: the struct input_dev descriptor of the device
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300408 * @scancode: the scancode that we're seeking
409 *
410 * This routine is used by the input routines when a key is pressed at the
411 * IR. The scancode is received and needs to be converted into a keycode.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300412 * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300413 * corresponding keycode from the table.
414 */
415u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
416{
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700417 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
418 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
419 unsigned int keycode;
420 unsigned int index;
421 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300422
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700423 spin_lock_irqsave(&rc_tab->lock, flags);
424
425 index = ir_lookup_by_scancode(rc_tab, scancode);
426 keycode = index < rc_tab->len ?
427 rc_tab->scan[index].keycode : KEY_RESERVED;
428
429 spin_unlock_irqrestore(&rc_tab->lock, flags);
430
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300431 if (keycode != KEY_RESERVED)
432 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
433 dev->name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700434
David Härdemanb3074c02010-04-02 15:58:28 -0300435 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300436}
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -0300437EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300438
439/**
David Härdeman62c65032010-10-29 16:08:07 -0300440 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemana374fef2010-04-02 15:58:29 -0300441 * @ir: the struct ir_input_dev descriptor of the device
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300442 *
David Härdeman62c65032010-10-29 16:08:07 -0300443 * This function is used internally to release a keypress, it must be
444 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300445 */
David Härdeman62c65032010-10-29 16:08:07 -0300446static void ir_do_keyup(struct ir_input_dev *ir)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300447{
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300448 if (!ir->keypressed)
449 return;
450
David Härdemana374fef2010-04-02 15:58:29 -0300451 IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
452 input_report_key(ir->input_dev, ir->last_keycode, 0);
453 input_sync(ir->input_dev);
454 ir->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300455}
David Härdeman62c65032010-10-29 16:08:07 -0300456
457/**
458 * ir_keyup() - generates input event to signal the release of a keypress
459 * @dev: the struct input_dev descriptor of the device
460 *
461 * This routine is used to signal that a key has been released on the
462 * remote control.
463 */
464void ir_keyup(struct input_dev *dev)
465{
466 unsigned long flags;
467 struct ir_input_dev *ir = input_get_drvdata(dev);
468
469 spin_lock_irqsave(&ir->keylock, flags);
470 ir_do_keyup(ir);
471 spin_unlock_irqrestore(&ir->keylock, flags);
472}
Jarod Wilsonee089402010-09-15 15:31:12 -0300473EXPORT_SYMBOL_GPL(ir_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300474
475/**
476 * ir_timer_keyup() - generates a keyup event after a timeout
477 * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
478 *
479 * This routine will generate a keyup event some time after a keydown event
480 * is generated when no further activity has been detected.
481 */
482static void ir_timer_keyup(unsigned long cookie)
483{
484 struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
485 unsigned long flags;
486
487 /*
488 * ir->keyup_jiffies is used to prevent a race condition if a
489 * hardware interrupt occurs at this point and the keyup timer
490 * event is moved further into the future as a result.
491 *
492 * The timer will then be reactivated and this function called
493 * again in the future. We need to exit gracefully in that case
494 * to allow the input subsystem to do its auto-repeat magic or
495 * a keyup event might follow immediately after the keydown.
496 */
497 spin_lock_irqsave(&ir->keylock, flags);
Maxim Levitskye0172fd2010-09-06 18:26:09 -0300498 if (time_is_before_eq_jiffies(ir->keyup_jiffies))
David Härdeman62c65032010-10-29 16:08:07 -0300499 ir_do_keyup(ir);
David Härdemana374fef2010-04-02 15:58:29 -0300500 spin_unlock_irqrestore(&ir->keylock, flags);
501}
502
503/**
504 * ir_repeat() - notifies the IR core that a key is still pressed
505 * @dev: the struct input_dev descriptor of the device
506 *
507 * This routine is used by IR decoders when a repeat message which does
508 * not include the necessary bits to reproduce the scancode has been
509 * received.
510 */
511void ir_repeat(struct input_dev *dev)
512{
513 unsigned long flags;
514 struct ir_input_dev *ir = input_get_drvdata(dev);
515
516 spin_lock_irqsave(&ir->keylock, flags);
517
Maxim Levitskyed4d3872010-07-31 11:59:24 -0300518 input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
519
David Härdemana374fef2010-04-02 15:58:29 -0300520 if (!ir->keypressed)
521 goto out;
522
523 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
524 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
525
526out:
527 spin_unlock_irqrestore(&ir->keylock, flags);
528}
529EXPORT_SYMBOL_GPL(ir_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300530
531/**
David Härdeman62c65032010-10-29 16:08:07 -0300532 * ir_do_keydown() - internal function to process a keypress
533 * @dev: the struct input_dev descriptor of the device
534 * @scancode: the scancode of the keypress
535 * @keycode: the keycode of the keypress
536 * @toggle: the toggle value of the keypress
537 *
538 * This function is used internally to register a keypress, it must be
539 * called with keylock held.
540 */
541static void ir_do_keydown(struct input_dev *dev, int scancode,
542 u32 keycode, u8 toggle)
543{
544 struct ir_input_dev *ir = input_get_drvdata(dev);
545
546 input_event(dev, EV_MSC, MSC_SCAN, scancode);
547
548 /* Repeat event? */
549 if (ir->keypressed &&
550 ir->last_scancode == scancode &&
551 ir->last_toggle == toggle)
552 return;
553
554 /* Release old keypress */
555 ir_do_keyup(ir);
556
557 ir->last_scancode = scancode;
558 ir->last_toggle = toggle;
559 ir->last_keycode = keycode;
560
561 if (keycode == KEY_RESERVED)
562 return;
563
564 /* Register a keypress */
565 ir->keypressed = true;
566 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
567 dev->name, keycode, scancode);
568 input_report_key(dev, ir->last_keycode, 1);
569 input_sync(dev);
570}
571
572/**
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300573 * ir_keydown() - generates input event for a key press
David Härdemana374fef2010-04-02 15:58:29 -0300574 * @dev: the struct input_dev descriptor of the device
575 * @scancode: the scancode that we're seeking
576 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
577 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300578 *
579 * This routine is used by the input routines when a key is pressed at the
580 * IR. It gets the keycode for a scancode and reports an input event via
581 * input_report_key().
582 */
David Härdemana374fef2010-04-02 15:58:29 -0300583void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300584{
David Härdemana374fef2010-04-02 15:58:29 -0300585 unsigned long flags;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300586 struct ir_input_dev *ir = input_get_drvdata(dev);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300587 u32 keycode = ir_g_keycode_from_table(dev, scancode);
588
David Härdemana374fef2010-04-02 15:58:29 -0300589 spin_lock_irqsave(&ir->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300590 ir_do_keydown(dev, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300591
David Härdeman62c65032010-10-29 16:08:07 -0300592 if (ir->keypressed) {
593 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
594 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
595 }
David Härdemana374fef2010-04-02 15:58:29 -0300596 spin_unlock_irqrestore(&ir->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300597}
598EXPORT_SYMBOL_GPL(ir_keydown);
599
David Härdeman62c65032010-10-29 16:08:07 -0300600/**
601 * ir_keydown_notimeout() - generates input event for a key press without
602 * an automatic keyup event at a later time
603 * @dev: the struct input_dev descriptor of the device
604 * @scancode: the scancode that we're seeking
605 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
606 * support toggle values, this should be set to zero)
607 *
608 * This routine is used by the input routines when a key is pressed at the
609 * IR. It gets the keycode for a scancode and reports an input event via
610 * input_report_key(). The driver must manually call ir_keyup() at a later
611 * stage.
612 */
613void ir_keydown_notimeout(struct input_dev *dev, int scancode, u8 toggle)
614{
615 unsigned long flags;
616 struct ir_input_dev *ir = input_get_drvdata(dev);
617 u32 keycode = ir_g_keycode_from_table(dev, scancode);
618
619 spin_lock_irqsave(&ir->keylock, flags);
620 ir_do_keydown(dev, scancode, keycode, toggle);
621 spin_unlock_irqrestore(&ir->keylock, flags);
622}
623EXPORT_SYMBOL_GPL(ir_keydown_notimeout);
624
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300625static int ir_open(struct input_dev *input_dev)
626{
627 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
628
629 return ir_dev->props->open(ir_dev->props->priv);
630}
631
632static void ir_close(struct input_dev *input_dev)
633{
634 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
635
636 ir_dev->props->close(ir_dev->props->priv);
637}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300638
639/**
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300640 * __ir_input_register() - sets the IR keycode table and add the handlers
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300641 * for keymap table get/set
642 * @input_dev: the struct input_dev descriptor of the device
643 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
644 *
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300645 * This routine is used to initialize the input infrastructure
646 * to work with an IR.
647 * It will register the input/evdev interface for the device and
648 * register the syfs code for IR class
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300649 */
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300650int __ir_input_register(struct input_dev *input_dev,
Mauro Carvalho Chehabe93854d2009-12-14 00:16:55 -0300651 const struct ir_scancode_table *rc_tab,
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300652 struct ir_dev_props *props,
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -0300653 const char *driver_name)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300654{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300655 struct ir_input_dev *ir_dev;
David Härdemanb3074c02010-04-02 15:58:28 -0300656 int rc;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300657
658 if (rc_tab->scan == NULL || !rc_tab->size)
659 return -EINVAL;
660
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300661 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
662 if (!ir_dev)
663 return -ENOMEM;
664
David Härdemanb3074c02010-04-02 15:58:28 -0300665 ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
666 if (!ir_dev->driver_name) {
667 rc = -ENOMEM;
668 goto out_dev;
Alexander Beregalov82311522010-01-09 13:51:14 -0300669 }
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300670
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700671 input_dev->getkeycode_new = ir_getkeycode;
672 input_dev->setkeycode_new = ir_setkeycode;
David Härdemanb3074c02010-04-02 15:58:28 -0300673 input_set_drvdata(input_dev, ir_dev);
David Härdemana374fef2010-04-02 15:58:29 -0300674 ir_dev->input_dev = input_dev;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300675
David Härdemanb3074c02010-04-02 15:58:28 -0300676 spin_lock_init(&ir_dev->rc_tab.lock);
David Härdemana374fef2010-04-02 15:58:29 -0300677 spin_lock_init(&ir_dev->keylock);
678 setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
679
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300680 if (props) {
681 ir_dev->props = props;
682 if (props->open)
683 input_dev->open = ir_open;
684 if (props->close)
685 input_dev->close = ir_close;
686 }
David Härdemanb3074c02010-04-02 15:58:28 -0300687
David Härdemanb3074c02010-04-02 15:58:28 -0300688 set_bit(EV_KEY, input_dev->evbit);
David Härdemana374fef2010-04-02 15:58:29 -0300689 set_bit(EV_REP, input_dev->evbit);
Maxim Levitskyed4d3872010-07-31 11:59:24 -0300690 set_bit(EV_MSC, input_dev->evbit);
691 set_bit(MSC_SCAN, input_dev->mscbit);
David Härdemana374fef2010-04-02 15:58:29 -0300692
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700693 rc = ir_setkeytable(ir_dev, rc_tab);
694 if (rc)
695 goto out_name;
David Härdemanb3074c02010-04-02 15:58:28 -0300696
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300697 rc = ir_register_class(input_dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300698 if (rc < 0)
David Härdemanb3074c02010-04-02 15:58:28 -0300699 goto out_table;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300700
Igor M. Liplianin84b14f12010-05-26 23:31:21 -0300701 if (ir_dev->props)
702 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
703 rc = ir_raw_event_register(input_dev);
704 if (rc < 0)
705 goto out_event;
706 }
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300707
Maxim Levitsky58b3dd42010-09-06 18:26:07 -0300708 rc = ir_register_input(input_dev);
Mauro Carvalho Chehab991369e2010-10-14 17:49:33 -0300709 if (rc < 0)
710 goto out_event;
Maxim Levitsky58b3dd42010-09-06 18:26:07 -0300711
Mauro Carvalho Chehab844a9e92010-08-01 17:19:29 -0300712 IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
713 driver_name, rc_tab->name,
Dan Carpenterede67a32010-08-06 03:31:00 -0300714 (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
715 " in raw mode" : "");
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300716
Mauro Carvalho Chehab04cab1312010-09-08 12:58:12 -0300717 /*
718 * Default delay of 250ms is too short for some protocols, expecially
719 * since the timeout is currently set to 250ms. Increase it to 500ms,
720 * to avoid wrong repetition of the keycodes.
721 */
722 input_dev->rep[REP_DELAY] = 500;
723
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300724 return 0;
725
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300726out_event:
727 ir_unregister_class(input_dev);
David Härdemanb3074c02010-04-02 15:58:28 -0300728out_table:
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700729 ir_free_table(&ir_dev->rc_tab);
David Härdemanb3074c02010-04-02 15:58:28 -0300730out_name:
731 kfree(ir_dev->driver_name);
732out_dev:
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300733 kfree(ir_dev);
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300734 return rc;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300735}
Mauro Carvalho Chehabb2245ba2010-04-02 13:18:42 -0300736EXPORT_SYMBOL_GPL(__ir_input_register);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300737
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300738/**
739 * ir_input_unregister() - unregisters IR and frees resources
740 * @input_dev: the struct input_dev descriptor of the device
741
742 * This routine is used to free memory and de-register interfaces.
743 */
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300744void ir_input_unregister(struct input_dev *input_dev)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300745{
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300746 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300747
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300748 if (!ir_dev)
Mauro Carvalho Chehab05395a32009-12-06 08:32:49 -0300749 return;
750
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300751 IR_dprintk(1, "Freed keycode table\n");
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300752
David Härdemana374fef2010-04-02 15:58:29 -0300753 del_timer_sync(&ir_dev->timer_keyup);
Igor M. Liplianin84b14f12010-05-26 23:31:21 -0300754 if (ir_dev->props)
755 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
756 ir_raw_event_unregister(input_dev);
757
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700758 ir_free_table(&ir_dev->rc_tab);
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300759
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300760 ir_unregister_class(input_dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300761
David Härdemanb3074c02010-04-02 15:58:28 -0300762 kfree(ir_dev->driver_name);
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300763 kfree(ir_dev);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300764}
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300765EXPORT_SYMBOL_GPL(ir_input_unregister);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300766
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300767/* class for /sys/class/rc */
768static char *ir_devnode(struct device *dev, mode_t *mode)
769{
770 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
771}
772
773static struct class ir_input_class = {
774 .name = "rc",
775 .devnode = ir_devnode,
776};
777
778static struct {
779 u64 type;
780 char *name;
781} proto_names[] = {
782 { IR_TYPE_UNKNOWN, "unknown" },
783 { IR_TYPE_RC5, "rc-5" },
784 { IR_TYPE_NEC, "nec" },
785 { IR_TYPE_RC6, "rc-6" },
786 { IR_TYPE_JVC, "jvc" },
787 { IR_TYPE_SONY, "sony" },
788 { IR_TYPE_RC5_SZ, "rc-5-sz" },
789 { IR_TYPE_LIRC, "lirc" },
790};
791
792#define PROTO_NONE "none"
793
794/**
795 * show_protocols() - shows the current IR protocol(s)
796 * @d: the device descriptor
797 * @mattr: the device attribute struct (unused)
798 * @buf: a pointer to the output buffer
799 *
800 * This routine is a callback routine for input read the IR protocol type(s).
801 * it is trigged by reading /sys/class/rc/rc?/protocols.
802 * It returns the protocol names of supported protocols.
803 * Enabled protocols are printed in brackets.
804 */
805static ssize_t show_protocols(struct device *d,
806 struct device_attribute *mattr, char *buf)
807{
808 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
809 u64 allowed, enabled;
810 char *tmp = buf;
811 int i;
812
813 /* Device is being removed */
814 if (!ir_dev)
815 return -EINVAL;
816
817 if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
818 enabled = ir_dev->rc_tab.ir_type;
819 allowed = ir_dev->props->allowed_protos;
820 } else if (ir_dev->raw) {
821 enabled = ir_dev->raw->enabled_protocols;
822 allowed = ir_raw_get_allowed_protocols();
823 } else
824 return sprintf(tmp, "[builtin]\n");
825
826 IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
827 (long long)allowed,
828 (long long)enabled);
829
830 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
831 if (allowed & enabled & proto_names[i].type)
832 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
833 else if (allowed & proto_names[i].type)
834 tmp += sprintf(tmp, "%s ", proto_names[i].name);
835 }
836
837 if (tmp != buf)
838 tmp--;
839 *tmp = '\n';
840 return tmp + 1 - buf;
841}
842
843/**
844 * store_protocols() - changes the current IR protocol(s)
845 * @d: the device descriptor
846 * @mattr: the device attribute struct (unused)
847 * @buf: a pointer to the input buffer
848 * @len: length of the input buffer
849 *
850 * This routine is a callback routine for changing the IR protocol type.
851 * It is trigged by writing to /sys/class/rc/rc?/protocols.
852 * Writing "+proto" will add a protocol to the list of enabled protocols.
853 * Writing "-proto" will remove a protocol from the list of enabled protocols.
854 * Writing "proto" will enable only "proto".
855 * Writing "none" will disable all protocols.
856 * Returns -EINVAL if an invalid protocol combination or unknown protocol name
857 * is used, otherwise @len.
858 */
859static ssize_t store_protocols(struct device *d,
860 struct device_attribute *mattr,
861 const char *data,
862 size_t len)
863{
864 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
865 bool enable, disable;
866 const char *tmp;
867 u64 type;
868 u64 mask;
869 int rc, i, count = 0;
870 unsigned long flags;
871
872 /* Device is being removed */
873 if (!ir_dev)
874 return -EINVAL;
875
876 if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
877 type = ir_dev->rc_tab.ir_type;
878 else if (ir_dev->raw)
879 type = ir_dev->raw->enabled_protocols;
880 else {
881 IR_dprintk(1, "Protocol switching not supported\n");
882 return -EINVAL;
883 }
884
885 while ((tmp = strsep((char **) &data, " \n")) != NULL) {
886 if (!*tmp)
887 break;
888
889 if (*tmp == '+') {
890 enable = true;
891 disable = false;
892 tmp++;
893 } else if (*tmp == '-') {
894 enable = false;
895 disable = true;
896 tmp++;
897 } else {
898 enable = false;
899 disable = false;
900 }
901
902 if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
903 tmp += sizeof(PROTO_NONE);
904 mask = 0;
905 count++;
906 } else {
907 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
908 if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
909 tmp += strlen(proto_names[i].name);
910 mask = proto_names[i].type;
911 break;
912 }
913 }
914 if (i == ARRAY_SIZE(proto_names)) {
915 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
916 return -EINVAL;
917 }
918 count++;
919 }
920
921 if (enable)
922 type |= mask;
923 else if (disable)
924 type &= ~mask;
925 else
926 type = mask;
927 }
928
929 if (!count) {
930 IR_dprintk(1, "Protocol not specified\n");
931 return -EINVAL;
932 }
933
934 if (ir_dev->props && ir_dev->props->change_protocol) {
935 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
936 type);
937 if (rc < 0) {
938 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
939 (long long)type);
940 return -EINVAL;
941 }
942 }
943
944 if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
945 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
946 ir_dev->rc_tab.ir_type = type;
947 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
948 } else {
949 ir_dev->raw->enabled_protocols = type;
950 }
951
952 IR_dprintk(1, "Current protocol(s): 0x%llx\n",
953 (long long)type);
954
955 return len;
956}
957
958#define ADD_HOTPLUG_VAR(fmt, val...) \
959 do { \
960 int err = add_uevent_var(env, fmt, val); \
961 if (err) \
962 return err; \
963 } while (0)
964
965static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
966{
967 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
968
969 if (ir_dev->rc_tab.name)
970 ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
971 if (ir_dev->driver_name)
972 ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
973
974 return 0;
975}
976
977/*
978 * Static device attribute struct with the sysfs attributes for IR's
979 */
980static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
981 show_protocols, store_protocols);
982
983static struct attribute *rc_dev_attrs[] = {
984 &dev_attr_protocols.attr,
985 NULL,
986};
987
988static struct attribute_group rc_dev_attr_grp = {
989 .attrs = rc_dev_attrs,
990};
991
992static const struct attribute_group *rc_dev_attr_groups[] = {
993 &rc_dev_attr_grp,
994 NULL
995};
996
997static struct device_type rc_dev_type = {
998 .groups = rc_dev_attr_groups,
999 .uevent = rc_dev_uevent,
1000};
1001
1002/**
1003 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
1004 * @input_dev: the struct input_dev descriptor of the device
1005 *
1006 * This routine is used to register the syfs code for IR class
1007 */
1008int ir_register_class(struct input_dev *input_dev)
1009{
1010 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1011 int devno = find_first_zero_bit(&ir_core_dev_number,
1012 IRRCV_NUM_DEVICES);
1013
1014 if (unlikely(devno < 0))
1015 return devno;
1016
1017 ir_dev->dev.type = &rc_dev_type;
1018 ir_dev->devno = devno;
1019
1020 ir_dev->dev.class = &ir_input_class;
1021 ir_dev->dev.parent = input_dev->dev.parent;
1022 input_dev->dev.parent = &ir_dev->dev;
1023 dev_set_name(&ir_dev->dev, "rc%d", devno);
1024 dev_set_drvdata(&ir_dev->dev, ir_dev);
1025 return device_register(&ir_dev->dev);
1026};
1027
1028/**
1029 * ir_register_input - registers ir input device with input subsystem
1030 * @input_dev: the struct input_dev descriptor of the device
1031 */
1032
1033int ir_register_input(struct input_dev *input_dev)
1034{
1035 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1036 int rc;
1037 const char *path;
1038
1039
1040 rc = input_register_device(input_dev);
1041 if (rc < 0) {
1042 device_del(&ir_dev->dev);
1043 return rc;
1044 }
1045
1046 __module_get(THIS_MODULE);
1047
1048 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
1049 printk(KERN_INFO "%s: %s as %s\n",
1050 dev_name(&ir_dev->dev),
1051 input_dev->name ? input_dev->name : "Unspecified device",
1052 path ? path : "N/A");
1053 kfree(path);
1054
1055 set_bit(ir_dev->devno, &ir_core_dev_number);
1056 return 0;
1057}
1058
1059/**
1060 * ir_unregister_class() - removes the sysfs for sysfs for
1061 * /sys/class/rc/rc?
1062 * @input_dev: the struct input_dev descriptor of the device
1063 *
1064 * This routine is used to unregister the syfs code for IR class
1065 */
1066void ir_unregister_class(struct input_dev *input_dev)
1067{
1068 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1069
1070 input_set_drvdata(input_dev, NULL);
1071 clear_bit(ir_dev->devno, &ir_core_dev_number);
1072 input_unregister_device(input_dev);
1073 device_del(&ir_dev->dev);
1074
1075 module_put(THIS_MODULE);
1076}
1077
1078/*
1079 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1080 */
1081
1082static int __init ir_core_init(void)
1083{
1084 int rc = class_register(&ir_input_class);
1085 if (rc) {
1086 printk(KERN_ERR "ir_core: unable to register rc class\n");
1087 return rc;
1088 }
1089
1090 /* Initialize/load the decoders/keymap code that will be used */
1091 ir_raw_init();
1092 ir_rcmap_init();
1093
1094 return 0;
1095}
1096
1097static void __exit ir_core_exit(void)
1098{
1099 class_unregister(&ir_input_class);
1100 ir_rcmap_cleanup();
1101}
1102
1103module_init(ir_core_init);
1104module_exit(ir_core_exit);
1105
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001106int ir_core_debug; /* ir_debug level (0,1,2) */
1107EXPORT_SYMBOL_GPL(ir_core_debug);
1108module_param_named(debug, ir_core_debug, int, 0644);
1109
1110MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1111MODULE_LICENSE("GPL");