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