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