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