Commit | Line | Data |
---|---|---|
ee820a64 OE |
1 | /* |
2 | * Driver for the remote control of SAA7146 based AV7110 cards | |
3 | * | |
4 | * Copyright (C) 1999-2003 Holger Waechtler <holger@convergence.de> | |
5 | * Copyright (C) 2003-2007 Oliver Endriss <o.endriss@gmx.de> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * as published by the Free Software Foundation; either version 2 | |
10 | * of the License, or (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 | * Or, point your browser to http://www.gnu.org/copyleft/gpl.html | |
21 | * | |
22 | */ | |
23 | ||
24 | ||
1da177e4 LT |
25 | #include <linux/types.h> |
26 | #include <linux/init.h> | |
27 | #include <linux/module.h> | |
28 | #include <linux/moduleparam.h> | |
1da177e4 | 29 | #include <linux/proc_fs.h> |
0496daa7 | 30 | #include <linux/kernel.h> |
1da177e4 LT |
31 | #include <asm/bitops.h> |
32 | ||
33 | #include "av7110.h" | |
03388ae3 | 34 | #include "av7110_hw.h" |
1da177e4 | 35 | |
1da177e4 | 36 | |
ee820a64 OE |
37 | #define AV_CNT 4 |
38 | ||
39 | #define IR_RC5 0 | |
40 | #define IR_RCMM 1 | |
41 | #define IR_RC5_EXT 2 /* internal only */ | |
42 | ||
43 | #define IR_ALL 0xffffffff | |
44 | ||
45 | #define UP_TIMEOUT (HZ*7/25) | |
46 | ||
47 | ||
48 | /* Note: enable ir debugging by or'ing debug with 16 */ | |
49 | ||
50 | static int ir_protocol[AV_CNT] = { IR_RCMM, IR_RCMM, IR_RCMM, IR_RCMM}; | |
51 | module_param_array(ir_protocol, int, NULL, 0644); | |
52 | MODULE_PARM_DESC(ir_protocol, "Infrared protocol: 0 RC5, 1 RCMM (default)"); | |
53 | ||
54 | static int ir_inversion[AV_CNT]; | |
55 | module_param_array(ir_inversion, int, NULL, 0644); | |
56 | MODULE_PARM_DESC(ir_inversion, "Inversion of infrared signal: 0 not inverted (default), 1 inverted"); | |
57 | ||
58 | static uint ir_device_mask[AV_CNT] = { IR_ALL, IR_ALL, IR_ALL, IR_ALL }; | |
59 | module_param_array(ir_device_mask, uint, NULL, 0644); | |
60 | MODULE_PARM_DESC(ir_device_mask, "Bitmask of infrared devices: bit 0..31 = device 0..31 (default: all)"); | |
1da177e4 | 61 | |
1da177e4 | 62 | |
ee820a64 OE |
63 | static int av_cnt; |
64 | static struct av7110 *av_list[AV_CNT]; | |
26a0f5db | 65 | |
ee820a64 | 66 | static u16 default_key_map [256] = { |
1da177e4 LT |
67 | KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, |
68 | KEY_8, KEY_9, KEY_BACK, 0, KEY_POWER, KEY_MUTE, 0, KEY_INFO, | |
69 | KEY_VOLUMEUP, KEY_VOLUMEDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
70 | KEY_CHANNELUP, KEY_CHANNELDOWN, 0, 0, 0, 0, 0, 0, | |
71 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
72 | 0, 0, 0, 0, KEY_TEXT, 0, 0, KEY_TV, 0, 0, 0, 0, 0, KEY_SETUP, 0, 0, | |
73 | 0, 0, 0, KEY_SUBTITLE, 0, 0, KEY_LANGUAGE, 0, | |
74 | KEY_RADIO, 0, 0, 0, 0, KEY_EXIT, 0, 0, | |
75 | KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK, 0, 0, 0, | |
76 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RED, KEY_GREEN, KEY_YELLOW, | |
77 | KEY_BLUE, 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_LIST, 0, 0, 0, 0, 0, 0, | |
78 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
79 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
80 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
81 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
82 | 0, 0, 0, 0, KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN, | |
83 | 0, 0, 0, 0, KEY_EPG, 0, 0, 0, | |
84 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
85 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
86 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_VCR | |
87 | }; | |
88 | ||
89 | ||
ee820a64 OE |
90 | /* key-up timer */ |
91 | static void av7110_emit_keyup(unsigned long parm) | |
1da177e4 | 92 | { |
ee820a64 OE |
93 | struct infrared *ir = (struct infrared *) parm; |
94 | ||
95 | if (!ir || !test_bit(ir->last_key, ir->input_dev->key)) | |
1da177e4 LT |
96 | return; |
97 | ||
ee820a64 OE |
98 | input_report_key(ir->input_dev, ir->last_key, 0); |
99 | input_sync(ir->input_dev); | |
1da177e4 LT |
100 | } |
101 | ||
102 | ||
ee820a64 | 103 | /* tasklet */ |
03388ae3 | 104 | static void av7110_emit_key(unsigned long parm) |
1da177e4 | 105 | { |
ee820a64 OE |
106 | struct infrared *ir = (struct infrared *) parm; |
107 | u32 ircom = ir->ir_command; | |
1da177e4 LT |
108 | u8 data; |
109 | u8 addr; | |
ee820a64 | 110 | u16 toggle; |
1da177e4 LT |
111 | u16 keycode; |
112 | ||
113 | /* extract device address and data */ | |
ee820a64 OE |
114 | switch (ir->protocol) { |
115 | case IR_RC5: /* RC5: 5 bits device address, 6 bits data */ | |
03388ae3 OE |
116 | data = ircom & 0x3f; |
117 | addr = (ircom >> 6) & 0x1f; | |
ee820a64 | 118 | toggle = ircom & 0x0800; |
03388ae3 OE |
119 | break; |
120 | ||
ee820a64 | 121 | case IR_RCMM: /* RCMM: ? bits device address, ? bits data */ |
1da177e4 | 122 | data = ircom & 0xff; |
ee820a64 OE |
123 | addr = (ircom >> 8) & 0x1f; |
124 | toggle = ircom & 0x8000; | |
03388ae3 OE |
125 | break; |
126 | ||
ee820a64 | 127 | case IR_RC5_EXT: /* extended RC5: 5 bits device address, 7 bits data */ |
1da177e4 LT |
128 | data = ircom & 0x3f; |
129 | addr = (ircom >> 6) & 0x1f; | |
03388ae3 OE |
130 | /* invert 7th data bit for backward compatibility with RC5 keymaps */ |
131 | if (!(ircom & 0x1000)) | |
132 | data |= 0x40; | |
ee820a64 | 133 | toggle = ircom & 0x0800; |
03388ae3 OE |
134 | break; |
135 | ||
136 | default: | |
ee820a64 | 137 | printk("%s invalid protocol %x\n", __FUNCTION__, ir->protocol); |
03388ae3 | 138 | return; |
1da177e4 LT |
139 | } |
140 | ||
ac52ea3c OE |
141 | input_event(ir->input_dev, EV_MSC, MSC_RAW, (addr << 16) | data); |
142 | input_event(ir->input_dev, EV_MSC, MSC_SCAN, data); | |
143 | ||
ee820a64 | 144 | keycode = ir->key_map[data]; |
1da177e4 | 145 | |
ee820a64 OE |
146 | dprintk(16, "%s: code %08x -> addr %i data 0x%02x -> keycode %i\n", |
147 | __FUNCTION__, ircom, addr, data, keycode); | |
1da177e4 | 148 | |
ee820a64 OE |
149 | /* check device address */ |
150 | if (!(ir->device_mask & (1 << addr))) | |
151 | return; | |
1da177e4 LT |
152 | |
153 | if (!keycode) { | |
ee820a64 OE |
154 | printk ("%s: code %08x -> addr %i data 0x%02x -> unknown key!\n", |
155 | __FUNCTION__, ircom, addr, data); | |
1da177e4 LT |
156 | return; |
157 | } | |
158 | ||
ee820a64 OE |
159 | if (timer_pending(&ir->keyup_timer)) { |
160 | del_timer(&ir->keyup_timer); | |
161 | if (ir->last_key != keycode || toggle != ir->last_toggle) { | |
162 | ir->delay_timer_finished = 0; | |
163 | input_event(ir->input_dev, EV_KEY, ir->last_key, 0); | |
164 | input_event(ir->input_dev, EV_KEY, keycode, 1); | |
165 | input_sync(ir->input_dev); | |
166 | } else if (ir->delay_timer_finished) { | |
167 | input_event(ir->input_dev, EV_KEY, keycode, 2); | |
168 | input_sync(ir->input_dev); | |
b07b4783 | 169 | } |
26a0f5db | 170 | } else { |
ee820a64 OE |
171 | ir->delay_timer_finished = 0; |
172 | input_event(ir->input_dev, EV_KEY, keycode, 1); | |
173 | input_sync(ir->input_dev); | |
26a0f5db | 174 | } |
1da177e4 | 175 | |
ee820a64 OE |
176 | ir->last_key = keycode; |
177 | ir->last_toggle = toggle; | |
1da177e4 | 178 | |
ee820a64 OE |
179 | ir->keyup_timer.expires = jiffies + UP_TIMEOUT; |
180 | add_timer(&ir->keyup_timer); | |
1da177e4 | 181 | |
1da177e4 LT |
182 | } |
183 | ||
ee820a64 OE |
184 | |
185 | /* register with input layer */ | |
186 | static void input_register_keys(struct infrared *ir) | |
1da177e4 LT |
187 | { |
188 | int i; | |
189 | ||
ee820a64 OE |
190 | set_bit(EV_KEY, ir->input_dev->evbit); |
191 | set_bit(EV_REP, ir->input_dev->evbit); | |
ac52ea3c OE |
192 | set_bit(EV_MSC, ir->input_dev->evbit); |
193 | ||
194 | set_bit(MSC_RAW, ir->input_dev->mscbit); | |
195 | set_bit(MSC_SCAN, ir->input_dev->mscbit); | |
1da177e4 | 196 | |
ee820a64 OE |
197 | memset(ir->input_dev->keybit, 0, sizeof(ir->input_dev->keybit)); |
198 | ||
199 | for (i = 0; i < ARRAY_SIZE(ir->key_map); i++) { | |
200 | if (ir->key_map[i] > KEY_MAX) | |
201 | ir->key_map[i] = 0; | |
202 | else if (ir->key_map[i] > KEY_RESERVED) | |
203 | set_bit(ir->key_map[i], ir->input_dev->keybit); | |
1da177e4 | 204 | } |
ee820a64 OE |
205 | |
206 | ir->input_dev->keycode = ir->key_map; | |
207 | ir->input_dev->keycodesize = sizeof(ir->key_map[0]); | |
208 | ir->input_dev->keycodemax = ARRAY_SIZE(ir->key_map); | |
1da177e4 LT |
209 | } |
210 | ||
211 | ||
ee820a64 OE |
212 | /* called by the input driver after rep[REP_DELAY] ms */ |
213 | static void input_repeat_key(unsigned long parm) | |
1da177e4 | 214 | { |
ee820a64 OE |
215 | struct infrared *ir = (struct infrared *) parm; |
216 | ||
217 | ir->delay_timer_finished = 1; | |
1da177e4 LT |
218 | } |
219 | ||
220 | ||
ee820a64 OE |
221 | /* check for configuration changes */ |
222 | int av7110_check_ir_config(struct av7110 *av7110, int force) | |
a22a6865 | 223 | { |
ee820a64 OE |
224 | int i; |
225 | int modified = force; | |
226 | int ret = -ENODEV; | |
a22a6865 | 227 | |
ee820a64 OE |
228 | for (i = 0; i < av_cnt; i++) |
229 | if (av7110 == av_list[i]) | |
230 | break; | |
231 | ||
232 | if (i < av_cnt && av7110) { | |
233 | if ((av7110->ir.protocol & 1) != ir_protocol[i] || | |
234 | av7110->ir.inversion != ir_inversion[i]) | |
235 | modified = true; | |
236 | ||
237 | if (modified) { | |
238 | /* protocol */ | |
239 | if (ir_protocol[i]) { | |
240 | ir_protocol[i] = 1; | |
241 | av7110->ir.protocol = IR_RCMM; | |
242 | av7110->ir.ir_config = 0x0001; | |
243 | } else if (FW_VERSION(av7110->arm_app) >= 0x2620) { | |
244 | av7110->ir.protocol = IR_RC5_EXT; | |
245 | av7110->ir.ir_config = 0x0002; | |
246 | } else { | |
247 | av7110->ir.protocol = IR_RC5; | |
248 | av7110->ir.ir_config = 0x0000; | |
249 | } | |
250 | /* inversion */ | |
251 | if (ir_inversion[i]) { | |
252 | ir_inversion[i] = 1; | |
253 | av7110->ir.ir_config |= 0x8000; | |
254 | } | |
255 | av7110->ir.inversion = ir_inversion[i]; | |
256 | /* update ARM */ | |
257 | ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1, | |
258 | av7110->ir.ir_config); | |
259 | } else | |
260 | ret = 0; | |
261 | ||
262 | /* address */ | |
263 | if (av7110->ir.device_mask != ir_device_mask[i]) | |
264 | av7110->ir.device_mask = ir_device_mask[i]; | |
a22a6865 | 265 | } |
ee820a64 | 266 | |
a22a6865 AB |
267 | return ret; |
268 | } | |
269 | ||
270 | ||
ee820a64 | 271 | /* /proc/av7110_ir interface */ |
1da177e4 LT |
272 | static int av7110_ir_write_proc(struct file *file, const char __user *buffer, |
273 | unsigned long count, void *data) | |
274 | { | |
275 | char *page; | |
03388ae3 | 276 | u32 ir_config; |
ee820a64 | 277 | int size = sizeof ir_config + sizeof av_list[0]->ir.key_map; |
03388ae3 | 278 | int i; |
1da177e4 LT |
279 | |
280 | if (count < size) | |
281 | return -EINVAL; | |
282 | ||
283 | page = (char *) vmalloc(size); | |
284 | if (!page) | |
285 | return -ENOMEM; | |
286 | ||
287 | if (copy_from_user(page, buffer, size)) { | |
288 | vfree(page); | |
289 | return -EFAULT; | |
290 | } | |
291 | ||
ee820a64 OE |
292 | memcpy(&ir_config, page, sizeof ir_config); |
293 | ||
294 | for (i = 0; i < av_cnt; i++) { | |
295 | /* keymap */ | |
296 | memcpy(av_list[i]->ir.key_map, page + sizeof ir_config, | |
297 | sizeof(av_list[i]->ir.key_map)); | |
298 | /* protocol, inversion, address */ | |
299 | ir_protocol[i] = ir_config & 0x0001; | |
300 | ir_inversion[i] = ir_config & 0x8000 ? 1 : 0; | |
301 | if (ir_config & 0x4000) | |
302 | ir_device_mask[i] = 1 << ((ir_config >> 16) & 0x1f); | |
303 | else | |
304 | ir_device_mask[i] = IR_ALL; | |
305 | /* update configuration */ | |
306 | av7110_check_ir_config(av_list[i], false); | |
307 | input_register_keys(&av_list[i]->ir); | |
308 | } | |
1da177e4 | 309 | vfree(page); |
1da177e4 LT |
310 | return count; |
311 | } | |
312 | ||
313 | ||
ee820a64 | 314 | /* interrupt handler */ |
03388ae3 OE |
315 | static void ir_handler(struct av7110 *av7110, u32 ircom) |
316 | { | |
ee820a64 OE |
317 | dprintk(4, "ir command = %08x\n", ircom); |
318 | av7110->ir.ir_command = ircom; | |
319 | tasklet_schedule(&av7110->ir.ir_tasklet); | |
03388ae3 | 320 | } |
1da177e4 | 321 | |
1da177e4 | 322 | |
11dc3ffa | 323 | int __devinit av7110_ir_init(struct av7110 *av7110) |
03388ae3 | 324 | { |
ee820a64 | 325 | struct input_dev *input_dev; |
03388ae3 | 326 | static struct proc_dir_entry *e; |
b07b4783 | 327 | int err; |
1da177e4 | 328 | |
0496daa7 | 329 | if (av_cnt >= ARRAY_SIZE(av_list)) |
03388ae3 | 330 | return -ENOSPC; |
1da177e4 | 331 | |
03388ae3 | 332 | av_list[av_cnt++] = av7110; |
1b245768 | 333 | av7110_check_ir_config(av7110, true); |
1da177e4 | 334 | |
ee820a64 OE |
335 | init_timer(&av7110->ir.keyup_timer); |
336 | av7110->ir.keyup_timer.function = av7110_emit_keyup; | |
337 | av7110->ir.keyup_timer.data = (unsigned long) &av7110->ir; | |
338 | ||
339 | input_dev = input_allocate_device(); | |
340 | if (!input_dev) | |
341 | return -ENOMEM; | |
342 | ||
343 | av7110->ir.input_dev = input_dev; | |
344 | snprintf(av7110->ir.input_phys, sizeof(av7110->ir.input_phys), | |
345 | "pci-%s/ir0", pci_name(av7110->dev->pci)); | |
03388ae3 | 346 | |
ee820a64 OE |
347 | input_dev->name = "DVB on-card IR receiver"; |
348 | ||
349 | input_dev->phys = av7110->ir.input_phys; | |
350 | input_dev->id.bustype = BUS_PCI; | |
351 | input_dev->id.version = 2; | |
352 | if (av7110->dev->pci->subsystem_vendor) { | |
353 | input_dev->id.vendor = av7110->dev->pci->subsystem_vendor; | |
354 | input_dev->id.product = av7110->dev->pci->subsystem_device; | |
355 | } else { | |
356 | input_dev->id.vendor = av7110->dev->pci->vendor; | |
357 | input_dev->id.product = av7110->dev->pci->device; | |
358 | } | |
2c8a3a33 | 359 | input_dev->dev.parent = &av7110->dev->pci->dev; |
ee820a64 OE |
360 | /* initial keymap */ |
361 | memcpy(av7110->ir.key_map, default_key_map, sizeof av7110->ir.key_map); | |
362 | input_register_keys(&av7110->ir); | |
363 | err = input_register_device(input_dev); | |
364 | if (err) { | |
365 | input_free_device(input_dev); | |
366 | return err; | |
367 | } | |
368 | input_dev->timer.function = input_repeat_key; | |
369 | input_dev->timer.data = (unsigned long) &av7110->ir; | |
370 | ||
371 | if (av_cnt == 1) { | |
03388ae3 OE |
372 | e = create_proc_entry("av7110_ir", S_IFREG | S_IRUGO | S_IWUSR, NULL); |
373 | if (e) { | |
374 | e->write_proc = av7110_ir_write_proc; | |
375 | e->size = 4 + 256 * sizeof(u16); | |
376 | } | |
1da177e4 LT |
377 | } |
378 | ||
ee820a64 OE |
379 | tasklet_init(&av7110->ir.ir_tasklet, av7110_emit_key, (unsigned long) &av7110->ir); |
380 | av7110->ir.ir_handler = ir_handler; | |
03388ae3 | 381 | |
1da177e4 LT |
382 | return 0; |
383 | } | |
384 | ||
385 | ||
11dc3ffa | 386 | void __devexit av7110_ir_exit(struct av7110 *av7110) |
1da177e4 | 387 | { |
03388ae3 OE |
388 | int i; |
389 | ||
390 | if (av_cnt == 0) | |
1da177e4 | 391 | return; |
03388ae3 | 392 | |
ee820a64 OE |
393 | del_timer_sync(&av7110->ir.keyup_timer); |
394 | av7110->ir.ir_handler = NULL; | |
395 | tasklet_kill(&av7110->ir.ir_tasklet); | |
396 | ||
03388ae3 OE |
397 | for (i = 0; i < av_cnt; i++) |
398 | if (av_list[i] == av7110) { | |
399 | av_list[i] = av_list[av_cnt-1]; | |
400 | av_list[av_cnt-1] = NULL; | |
401 | break; | |
402 | } | |
403 | ||
ee820a64 | 404 | if (av_cnt == 1) |
03388ae3 | 405 | remove_proc_entry("av7110_ir", NULL); |
ee820a64 OE |
406 | |
407 | input_unregister_device(av7110->ir.input_dev); | |
03388ae3 OE |
408 | |
409 | av_cnt--; | |
1da177e4 LT |
410 | } |
411 | ||
ee820a64 | 412 | //MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>, Oliver Endriss <o.endriss@gmx.de>"); |
1da177e4 | 413 | //MODULE_LICENSE("GPL"); |