diff options
Diffstat (limited to 'drivers/media/video/cx23885/cx23885-input.c')
-rw-r--r-- | drivers/media/video/cx23885/cx23885-input.c | 427 |
1 files changed, 427 insertions, 0 deletions
diff --git a/drivers/media/video/cx23885/cx23885-input.c b/drivers/media/video/cx23885/cx23885-input.c new file mode 100644 index 000000000000..469e083dd5f8 --- /dev/null +++ b/drivers/media/video/cx23885/cx23885-input.c | |||
@@ -0,0 +1,427 @@ | |||
1 | /* | ||
2 | * Driver for the Conexant CX23885/7/8 PCIe bridge | ||
3 | * | ||
4 | * Infrared remote control input device | ||
5 | * | ||
6 | * Most of this file is | ||
7 | * | ||
8 | * Copyright (C) 2009 Andy Walls <awalls@radix.net> | ||
9 | * | ||
10 | * However, the cx23885_input_{init,fini} functions contained herein are | ||
11 | * derived from Linux kernel files linux/media/video/.../...-input.c marked as: | ||
12 | * | ||
13 | * Copyright (C) 2008 <srinivasa.deevi at conexant dot com> | ||
14 | * Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> | ||
15 | * Markus Rechberger <mrechberger@gmail.com> | ||
16 | * Mauro Carvalho Chehab <mchehab@infradead.org> | ||
17 | * Sascha Sommer <saschasommer@freenet.de> | ||
18 | * Copyright (C) 2004, 2005 Chris Pascoe | ||
19 | * Copyright (C) 2003, 2004 Gerd Knorr | ||
20 | * Copyright (C) 2003 Pavel Machek | ||
21 | * | ||
22 | * This program is free software; you can redistribute it and/or | ||
23 | * modify it under the terms of the GNU General Public License | ||
24 | * as published by the Free Software Foundation; either version 2 | ||
25 | * of the License, or (at your option) any later version. | ||
26 | * | ||
27 | * This program is distributed in the hope that it will be useful, | ||
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
30 | * GNU General Public License for more details. | ||
31 | * | ||
32 | * You should have received a copy of the GNU General Public License | ||
33 | * along with this program; if not, write to the Free Software | ||
34 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
35 | * 02110-1301, USA. | ||
36 | */ | ||
37 | |||
38 | #include <linux/input.h> | ||
39 | #include <media/ir-common.h> | ||
40 | #include <media/v4l2-subdev.h> | ||
41 | |||
42 | #include "cx23885.h" | ||
43 | |||
44 | #define RC5_BITS 14 | ||
45 | #define RC5_HALF_BITS (2*RC5_BITS) | ||
46 | #define RC5_HALF_BITS_MASK ((1 << RC5_HALF_BITS) - 1) | ||
47 | |||
48 | #define RC5_START_BITS_NORMAL 0x3 /* Command range 0 - 63 */ | ||
49 | #define RC5_START_BITS_EXTENDED 0x2 /* Command range 64 - 127 */ | ||
50 | |||
51 | #define RC5_EXTENDED_COMMAND_OFFSET 64 | ||
52 | |||
53 | static inline unsigned int rc5_command(u32 rc5_baseband) | ||
54 | { | ||
55 | return RC5_INSTR(rc5_baseband) + | ||
56 | ((RC5_START(rc5_baseband) == RC5_START_BITS_EXTENDED) | ||
57 | ? RC5_EXTENDED_COMMAND_OFFSET : 0); | ||
58 | } | ||
59 | |||
60 | static void cx23885_input_process_raw_rc5(struct cx23885_dev *dev) | ||
61 | { | ||
62 | struct card_ir *ir_input = dev->ir_input; | ||
63 | unsigned int code, command; | ||
64 | u32 rc5; | ||
65 | |||
66 | /* Ignore codes that are too short to be valid RC-5 */ | ||
67 | if (ir_input->last_bit < (RC5_HALF_BITS - 1)) | ||
68 | return; | ||
69 | |||
70 | /* The library has the manchester coding backwards; XOR to adapt. */ | ||
71 | code = (ir_input->code & RC5_HALF_BITS_MASK) ^ RC5_HALF_BITS_MASK; | ||
72 | rc5 = ir_rc5_decode(code); | ||
73 | |||
74 | switch (RC5_START(rc5)) { | ||
75 | case RC5_START_BITS_NORMAL: | ||
76 | break; | ||
77 | case RC5_START_BITS_EXTENDED: | ||
78 | /* Don't allow if the remote only emits standard commands */ | ||
79 | if (ir_input->start == RC5_START_BITS_NORMAL) | ||
80 | return; | ||
81 | break; | ||
82 | default: | ||
83 | return; | ||
84 | } | ||
85 | |||
86 | if (ir_input->addr != RC5_ADDR(rc5)) | ||
87 | return; | ||
88 | |||
89 | /* Don't generate a keypress for RC-5 auto-repeated keypresses */ | ||
90 | command = rc5_command(rc5); | ||
91 | if (RC5_TOGGLE(rc5) != RC5_TOGGLE(ir_input->last_rc5) || | ||
92 | command != rc5_command(ir_input->last_rc5) || | ||
93 | /* Catch T == 0, CMD == 0 (e.g. '0') as first keypress after init */ | ||
94 | RC5_START(ir_input->last_rc5) == 0) { | ||
95 | /* This keypress is differnet: not an auto repeat */ | ||
96 | ir_input_nokey(ir_input->dev, &ir_input->ir); | ||
97 | ir_input_keydown(ir_input->dev, &ir_input->ir, command); | ||
98 | } | ||
99 | ir_input->last_rc5 = rc5; | ||
100 | |||
101 | /* Schedule when we should do the key up event: ir_input_nokey() */ | ||
102 | mod_timer(&ir_input->timer_keyup, | ||
103 | jiffies + msecs_to_jiffies(ir_input->rc5_key_timeout)); | ||
104 | } | ||
105 | |||
106 | static void cx23885_input_next_pulse_width_rc5(struct cx23885_dev *dev, | ||
107 | u32 ns_pulse) | ||
108 | { | ||
109 | const int rc5_quarterbit_ns = 444444; /* 32 cycles/36 kHz/2 = 444 us */ | ||
110 | struct card_ir *ir_input = dev->ir_input; | ||
111 | int i, level, quarterbits, halfbits; | ||
112 | |||
113 | if (!ir_input->active) { | ||
114 | ir_input->active = 1; | ||
115 | /* assume an initial space that we may not detect or measure */ | ||
116 | ir_input->code = 0; | ||
117 | ir_input->last_bit = 0; | ||
118 | } | ||
119 | |||
120 | if (ns_pulse == V4L2_SUBDEV_IR_PULSE_RX_SEQ_END) { | ||
121 | ir_input->last_bit++; /* Account for the final space */ | ||
122 | ir_input->active = 0; | ||
123 | cx23885_input_process_raw_rc5(dev); | ||
124 | return; | ||
125 | } | ||
126 | |||
127 | level = (ns_pulse & V4L2_SUBDEV_IR_PULSE_LEVEL_MASK) ? 1 : 0; | ||
128 | |||
129 | /* Skip any leading space to sync to the start bit */ | ||
130 | if (ir_input->last_bit == 0 && level == 0) | ||
131 | return; | ||
132 | |||
133 | /* | ||
134 | * With valid RC-5 we can get up to two consecutive half-bits in a | ||
135 | * single pulse measurment. Experiments have shown that the duration | ||
136 | * of a half-bit can vary. Make sure we always end up with an even | ||
137 | * number of quarter bits at the same level (mark or space). | ||
138 | */ | ||
139 | ns_pulse &= V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS; | ||
140 | quarterbits = ns_pulse / rc5_quarterbit_ns; | ||
141 | if (quarterbits & 1) | ||
142 | quarterbits++; | ||
143 | halfbits = quarterbits / 2; | ||
144 | |||
145 | for (i = 0; i < halfbits; i++) { | ||
146 | ir_input->last_bit++; | ||
147 | ir_input->code |= (level << ir_input->last_bit); | ||
148 | |||
149 | if (ir_input->last_bit >= RC5_HALF_BITS-1) { | ||
150 | ir_input->active = 0; | ||
151 | cx23885_input_process_raw_rc5(dev); | ||
152 | /* | ||
153 | * If level is 1, a leading mark is invalid for RC5. | ||
154 | * If level is 0, we scan past extra intial space. | ||
155 | * Either way we don't want to reactivate collecting | ||
156 | * marks or spaces here with any left over half-bits. | ||
157 | */ | ||
158 | break; | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | |||
163 | static void cx23885_input_process_pulse_widths_rc5(struct cx23885_dev *dev, | ||
164 | bool add_eom) | ||
165 | { | ||
166 | struct card_ir *ir_input = dev->ir_input; | ||
167 | struct ir_input_state *ir_input_state = &ir_input->ir; | ||
168 | |||
169 | u32 ns_pulse[RC5_HALF_BITS+1]; | ||
170 | ssize_t num = 0; | ||
171 | int count, i; | ||
172 | |||
173 | do { | ||
174 | v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ns_pulse, | ||
175 | sizeof(ns_pulse), &num); | ||
176 | |||
177 | count = num / sizeof(u32); | ||
178 | |||
179 | /* Append an end of Rx seq, if the caller requested */ | ||
180 | if (add_eom && count < ARRAY_SIZE(ns_pulse)) { | ||
181 | ns_pulse[count] = V4L2_SUBDEV_IR_PULSE_RX_SEQ_END; | ||
182 | count++; | ||
183 | } | ||
184 | |||
185 | /* Just drain the Rx FIFO, if we're called, but not RC-5 */ | ||
186 | if (ir_input_state->ir_type != IR_TYPE_RC5) | ||
187 | continue; | ||
188 | |||
189 | for (i = 0; i < count; i++) | ||
190 | cx23885_input_next_pulse_width_rc5(dev, ns_pulse[i]); | ||
191 | } while (num != 0); | ||
192 | } | ||
193 | |||
194 | void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events) | ||
195 | { | ||
196 | struct v4l2_subdev_ir_parameters params; | ||
197 | int overrun, data_available; | ||
198 | |||
199 | if (dev->sd_ir == NULL || events == 0) | ||
200 | return; | ||
201 | |||
202 | switch (dev->board) { | ||
203 | case CX23885_BOARD_HAUPPAUGE_HVR1850: | ||
204 | case CX23885_BOARD_HAUPPAUGE_HVR1290: | ||
205 | /* | ||
206 | * The only board we handle right now. However other boards | ||
207 | * using the CX2388x integrated IR controller should be similar | ||
208 | */ | ||
209 | break; | ||
210 | default: | ||
211 | return; | ||
212 | } | ||
213 | |||
214 | overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN | | ||
215 | V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN); | ||
216 | |||
217 | data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED | | ||
218 | V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ); | ||
219 | |||
220 | if (overrun) { | ||
221 | /* If there was a FIFO overrun, stop the device */ | ||
222 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); | ||
223 | params.enable = false; | ||
224 | /* Mitigate race with cx23885_input_ir_stop() */ | ||
225 | params.shutdown = atomic_read(&dev->ir_input_stopping); | ||
226 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); | ||
227 | } | ||
228 | |||
229 | if (data_available) | ||
230 | cx23885_input_process_pulse_widths_rc5(dev, overrun); | ||
231 | |||
232 | if (overrun) { | ||
233 | /* If there was a FIFO overrun, clear & restart the device */ | ||
234 | params.enable = true; | ||
235 | /* Mitigate race with cx23885_input_ir_stop() */ | ||
236 | params.shutdown = atomic_read(&dev->ir_input_stopping); | ||
237 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); | ||
238 | } | ||
239 | } | ||
240 | |||
241 | static void cx23885_input_ir_start(struct cx23885_dev *dev) | ||
242 | { | ||
243 | struct card_ir *ir_input = dev->ir_input; | ||
244 | struct ir_input_state *ir_input_state = &ir_input->ir; | ||
245 | struct v4l2_subdev_ir_parameters params; | ||
246 | |||
247 | if (dev->sd_ir == NULL) | ||
248 | return; | ||
249 | |||
250 | atomic_set(&dev->ir_input_stopping, 0); | ||
251 | |||
252 | /* keyup timer set up, if needed */ | ||
253 | switch (dev->board) { | ||
254 | case CX23885_BOARD_HAUPPAUGE_HVR1850: | ||
255 | case CX23885_BOARD_HAUPPAUGE_HVR1290: | ||
256 | setup_timer(&ir_input->timer_keyup, | ||
257 | ir_rc5_timer_keyup, /* Not actually RC-5 specific */ | ||
258 | (unsigned long) ir_input); | ||
259 | if (ir_input_state->ir_type == IR_TYPE_RC5) { | ||
260 | /* | ||
261 | * RC-5 repeats a held key every | ||
262 | * 64 bits * (2 * 32/36000) sec/bit = 113.778 ms | ||
263 | */ | ||
264 | ir_input->rc5_key_timeout = 115; | ||
265 | } | ||
266 | break; | ||
267 | } | ||
268 | |||
269 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); | ||
270 | switch (dev->board) { | ||
271 | case CX23885_BOARD_HAUPPAUGE_HVR1850: | ||
272 | case CX23885_BOARD_HAUPPAUGE_HVR1290: | ||
273 | /* | ||
274 | * The IR controller on this board only returns pulse widths. | ||
275 | * Any other mode setting will fail to set up the device. | ||
276 | */ | ||
277 | params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH; | ||
278 | params.enable = true; | ||
279 | params.interrupt_enable = true; | ||
280 | params.shutdown = false; | ||
281 | |||
282 | /* Setup for baseband compatible with both RC-5 and RC-6A */ | ||
283 | params.modulation = false; | ||
284 | /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/ | ||
285 | /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/ | ||
286 | params.max_pulse_width = 3333333; /* ns */ | ||
287 | /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */ | ||
288 | /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */ | ||
289 | params.noise_filter_min_width = 333333; /* ns */ | ||
290 | /* | ||
291 | * This board has inverted receive sense: | ||
292 | * mark is received as low logic level; | ||
293 | * falling edges are detected as rising edges; etc. | ||
294 | */ | ||
295 | params.invert = true; | ||
296 | break; | ||
297 | } | ||
298 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); | ||
299 | } | ||
300 | |||
301 | static void cx23885_input_ir_stop(struct cx23885_dev *dev) | ||
302 | { | ||
303 | struct card_ir *ir_input = dev->ir_input; | ||
304 | struct v4l2_subdev_ir_parameters params; | ||
305 | |||
306 | if (dev->sd_ir == NULL) | ||
307 | return; | ||
308 | |||
309 | /* | ||
310 | * Stop the sd_ir subdevice from generating notifications and | ||
311 | * scheduling work. | ||
312 | * It is shutdown this way in order to mitigate a race with | ||
313 | * cx23885_input_rx_work_handler() in the overrun case, which could | ||
314 | * re-enable the subdevice. | ||
315 | */ | ||
316 | atomic_set(&dev->ir_input_stopping, 1); | ||
317 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); | ||
318 | while (params.shutdown == false) { | ||
319 | params.enable = false; | ||
320 | params.interrupt_enable = false; | ||
321 | params.shutdown = true; | ||
322 | v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, ¶ms); | ||
323 | v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, ¶ms); | ||
324 | } | ||
325 | |||
326 | flush_scheduled_work(); | ||
327 | |||
328 | switch (dev->board) { | ||
329 | case CX23885_BOARD_HAUPPAUGE_HVR1850: | ||
330 | case CX23885_BOARD_HAUPPAUGE_HVR1290: | ||
331 | del_timer_sync(&ir_input->timer_keyup); | ||
332 | break; | ||
333 | } | ||
334 | } | ||
335 | |||
336 | int cx23885_input_init(struct cx23885_dev *dev) | ||
337 | { | ||
338 | struct card_ir *ir; | ||
339 | struct input_dev *input_dev; | ||
340 | struct ir_scancode_table *ir_codes = NULL; | ||
341 | int ir_type, ir_addr, ir_start; | ||
342 | int ret; | ||
343 | |||
344 | /* | ||
345 | * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't | ||
346 | * encapsulated in a v4l2_subdev, then I'm not going to deal with it. | ||
347 | */ | ||
348 | if (dev->sd_ir == NULL) | ||
349 | return -ENODEV; | ||
350 | |||
351 | switch (dev->board) { | ||
352 | case CX23885_BOARD_HAUPPAUGE_HVR1850: | ||
353 | case CX23885_BOARD_HAUPPAUGE_HVR1290: | ||
354 | /* Parameters for the grey Hauppauge remote for the HVR-1850 */ | ||
355 | ir_codes = &ir_codes_hauppauge_new_table; | ||
356 | ir_type = IR_TYPE_RC5; | ||
357 | ir_addr = 0x1e; /* RC-5 system bits emitted by the remote */ | ||
358 | ir_start = RC5_START_BITS_NORMAL; /* A basic RC-5 remote */ | ||
359 | break; | ||
360 | } | ||
361 | if (ir_codes == NULL) | ||
362 | return -ENODEV; | ||
363 | |||
364 | ir = kzalloc(sizeof(*ir), GFP_KERNEL); | ||
365 | input_dev = input_allocate_device(); | ||
366 | if (!ir || !input_dev) { | ||
367 | ret = -ENOMEM; | ||
368 | goto err_out_free; | ||
369 | } | ||
370 | |||
371 | ir->dev = input_dev; | ||
372 | ir->addr = ir_addr; | ||
373 | ir->start = ir_start; | ||
374 | |||
375 | /* init input device */ | ||
376 | snprintf(ir->name, sizeof(ir->name), "cx23885 IR (%s)", | ||
377 | cx23885_boards[dev->board].name); | ||
378 | snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(dev->pci)); | ||
379 | |||
380 | ret = ir_input_init(input_dev, &ir->ir, ir_type, ir_codes); | ||
381 | if (ret < 0) | ||
382 | goto err_out_free; | ||
383 | |||
384 | input_dev->name = ir->name; | ||
385 | input_dev->phys = ir->phys; | ||
386 | input_dev->id.bustype = BUS_PCI; | ||
387 | input_dev->id.version = 1; | ||
388 | if (dev->pci->subsystem_vendor) { | ||
389 | input_dev->id.vendor = dev->pci->subsystem_vendor; | ||
390 | input_dev->id.product = dev->pci->subsystem_device; | ||
391 | } else { | ||
392 | input_dev->id.vendor = dev->pci->vendor; | ||
393 | input_dev->id.product = dev->pci->device; | ||
394 | } | ||
395 | input_dev->dev.parent = &dev->pci->dev; | ||
396 | |||
397 | dev->ir_input = ir; | ||
398 | cx23885_input_ir_start(dev); | ||
399 | |||
400 | ret = input_register_device(ir->dev); | ||
401 | if (ret) | ||
402 | goto err_out_stop; | ||
403 | |||
404 | return 0; | ||
405 | |||
406 | err_out_stop: | ||
407 | cx23885_input_ir_stop(dev); | ||
408 | dev->ir_input = NULL; | ||
409 | err_out_free: | ||
410 | ir_input_free(input_dev); | ||
411 | input_free_device(input_dev); | ||
412 | kfree(ir); | ||
413 | return ret; | ||
414 | } | ||
415 | |||
416 | void cx23885_input_fini(struct cx23885_dev *dev) | ||
417 | { | ||
418 | /* Always stop the IR hardware from generating interrupts */ | ||
419 | cx23885_input_ir_stop(dev); | ||
420 | |||
421 | if (dev->ir_input == NULL) | ||
422 | return; | ||
423 | ir_input_free(dev->ir_input->dev); | ||
424 | input_unregister_device(dev->ir_input->dev); | ||
425 | kfree(dev->ir_input); | ||
426 | dev->ir_input = NULL; | ||
427 | } | ||