MAINTAINERS & files: Canonize the e-mails I use at files
[linux-2.6-block.git] / drivers / media / pci / cx23885 / cx23885-input.c
CommitLineData
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 *
6afdeaf8 8 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
dbda8f70
AW
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>
32590819 16 * Mauro Carvalho Chehab <mchehab@kernel.org>
dbda8f70
AW
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.
dbda8f70
AW
31 */
32
e39682b5
MCC
33#include "cx23885.h"
34#include "cx23885-input.h"
35
5a0e3ad6 36#include <linux/slab.h>
6bda9644 37#include <media/rc-core.h>
dbda8f70
AW
38#include <media/v4l2-subdev.h>
39
727e625c
MCC
40#define MODULE_NAME "cx23885"
41
43c24078
AW
42static void cx23885_input_process_measurements(struct cx23885_dev *dev,
43 bool overrun)
dbda8f70 44{
43c24078 45 struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir;
dbda8f70 46
43c24078 47 ssize_t num;
dbda8f70 48 int count, i;
43c24078 49 bool handle = false;
c02e0d12 50 struct ir_raw_event ir_core_event[64];
dbda8f70
AW
51
52 do {
43c24078 53 num = 0;
c02e0d12
AW
54 v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event,
55 sizeof(ir_core_event), &num);
dbda8f70 56
c02e0d12 57 count = num / sizeof(struct ir_raw_event);
dbda8f70 58
43c24078 59 for (i = 0; i < count; i++) {
d8b4b582 60 ir_raw_event_store(kernel_ir->rc,
c02e0d12 61 &ir_core_event[i]);
43c24078 62 handle = true;
dbda8f70 63 }
dbda8f70 64 } while (num != 0);
43c24078
AW
65
66 if (overrun)
d8b4b582 67 ir_raw_event_reset(kernel_ir->rc);
43c24078 68 else if (handle)
d8b4b582 69 ir_raw_event_handle(kernel_ir->rc);
dbda8f70
AW
70}
71
72void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)
73{
74 struct v4l2_subdev_ir_parameters params;
75 int overrun, data_available;
76
77 if (dev->sd_ir == NULL || events == 0)
78 return;
79
80 switch (dev->board) {
9b3d8ecc 81 case CX23885_BOARD_HAUPPAUGE_HVR1270:
dbda8f70 82 case CX23885_BOARD_HAUPPAUGE_HVR1850:
7fec6fee 83 case CX23885_BOARD_HAUPPAUGE_HVR1290:
076f0e35 84 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
98d109f9
AW
85 case CX23885_BOARD_TEVII_S470:
86 case CX23885_BOARD_HAUPPAUGE_HVR1250:
e5f670b7 87 case CX23885_BOARD_MYGICA_X8507:
e6001482
LA
88 case CX23885_BOARD_TBS_6980:
89 case CX23885_BOARD_TBS_6981:
d11a3835 90 case CX23885_BOARD_DVBSKY_T9580:
070e6661 91 case CX23885_BOARD_DVBSKY_T980C:
92 case CX23885_BOARD_DVBSKY_S950C:
61b103e8 93 case CX23885_BOARD_TT_CT2_4500_CI:
cba5480c 94 case CX23885_BOARD_DVBSKY_S950:
c29d6a83 95 case CX23885_BOARD_DVBSKY_S952:
c02ef64a 96 case CX23885_BOARD_DVBSKY_T982:
94f11518 97 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
dbda8f70 98 /*
98d109f9 99 * The only boards we handle right now. However other boards
dbda8f70
AW
100 * using the CX2388x integrated IR controller should be similar
101 */
102 break;
103 default:
104 return;
105 }
106
107 overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN |
108 V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN);
109
110 data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED |
111 V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ);
112
113 if (overrun) {
114 /* If there was a FIFO overrun, stop the device */
115 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
116 params.enable = false;
117 /* Mitigate race with cx23885_input_ir_stop() */
118 params.shutdown = atomic_read(&dev->ir_input_stopping);
119 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
120 }
121
122 if (data_available)
43c24078 123 cx23885_input_process_measurements(dev, overrun);
dbda8f70
AW
124
125 if (overrun) {
126 /* If there was a FIFO overrun, clear & restart the device */
127 params.enable = true;
128 /* Mitigate race with cx23885_input_ir_stop() */
129 params.shutdown = atomic_read(&dev->ir_input_stopping);
130 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
131 }
132}
133
43c24078 134static int cx23885_input_ir_start(struct cx23885_dev *dev)
dbda8f70 135{
dbda8f70
AW
136 struct v4l2_subdev_ir_parameters params;
137
138 if (dev->sd_ir == NULL)
43c24078 139 return -ENODEV;
dbda8f70
AW
140
141 atomic_set(&dev->ir_input_stopping, 0);
142
dbda8f70
AW
143 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
144 switch (dev->board) {
9b3d8ecc 145 case CX23885_BOARD_HAUPPAUGE_HVR1270:
dbda8f70 146 case CX23885_BOARD_HAUPPAUGE_HVR1850:
7fec6fee 147 case CX23885_BOARD_HAUPPAUGE_HVR1290:
98d109f9 148 case CX23885_BOARD_HAUPPAUGE_HVR1250:
e5f670b7 149 case CX23885_BOARD_MYGICA_X8507:
d11a3835 150 case CX23885_BOARD_DVBSKY_T9580:
070e6661 151 case CX23885_BOARD_DVBSKY_T980C:
152 case CX23885_BOARD_DVBSKY_S950C:
61b103e8 153 case CX23885_BOARD_TT_CT2_4500_CI:
cba5480c 154 case CX23885_BOARD_DVBSKY_S950:
c29d6a83 155 case CX23885_BOARD_DVBSKY_S952:
c02ef64a 156 case CX23885_BOARD_DVBSKY_T982:
94f11518 157 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
dbda8f70
AW
158 /*
159 * The IR controller on this board only returns pulse widths.
160 * Any other mode setting will fail to set up the device.
161 */
162 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
163 params.enable = true;
164 params.interrupt_enable = true;
165 params.shutdown = false;
166
167 /* Setup for baseband compatible with both RC-5 and RC-6A */
168 params.modulation = false;
169 /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
170 /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
171 params.max_pulse_width = 3333333; /* ns */
172 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
173 /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
174 params.noise_filter_min_width = 333333; /* ns */
175 /*
176 * This board has inverted receive sense:
177 * mark is received as low logic level;
178 * falling edges are detected as rising edges; etc.
179 */
5a28d9a3 180 params.invert_level = true;
dbda8f70 181 break;
076f0e35 182 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
98d109f9 183 case CX23885_BOARD_TEVII_S470:
e6001482
LA
184 case CX23885_BOARD_TBS_6980:
185 case CX23885_BOARD_TBS_6981:
98d109f9
AW
186 /*
187 * The IR controller on this board only returns pulse widths.
188 * Any other mode setting will fail to set up the device.
189 */
190 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
191 params.enable = true;
192 params.interrupt_enable = true;
193 params.shutdown = false;
194
195 /* Setup for a standard NEC protocol */
196 params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */
197 params.carrier_range_lower = 33000; /* Hz */
198 params.carrier_range_upper = 43000; /* Hz */
199 params.duty_cycle = 33; /* percent, 33 percent for NEC */
200
201 /*
202 * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units
203 * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns
204 */
205 params.max_pulse_width = 12378022; /* ns */
206
207 /*
208 * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit
209 * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns
210 */
211 params.noise_filter_min_width = 351648; /* ns */
212
213 params.modulation = false;
214 params.invert_level = true;
215 break;
dbda8f70
AW
216 }
217 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
43c24078
AW
218 return 0;
219}
220
d8b4b582 221static int cx23885_input_ir_open(struct rc_dev *rc)
43c24078 222{
d8b4b582 223 struct cx23885_kernel_ir *kernel_ir = rc->priv;
43c24078
AW
224
225 if (kernel_ir->cx == NULL)
226 return -ENODEV;
227
228 return cx23885_input_ir_start(kernel_ir->cx);
dbda8f70
AW
229}
230
231static void cx23885_input_ir_stop(struct cx23885_dev *dev)
232{
dbda8f70
AW
233 struct v4l2_subdev_ir_parameters params;
234
235 if (dev->sd_ir == NULL)
236 return;
237
238 /*
239 * Stop the sd_ir subdevice from generating notifications and
240 * scheduling work.
241 * It is shutdown this way in order to mitigate a race with
242 * cx23885_input_rx_work_handler() in the overrun case, which could
243 * re-enable the subdevice.
244 */
245 atomic_set(&dev->ir_input_stopping, 1);
246 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
247 while (params.shutdown == false) {
248 params.enable = false;
249 params.interrupt_enable = false;
250 params.shutdown = true;
251 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
252 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
253 }
43829731
TH
254 flush_work(&dev->cx25840_work);
255 flush_work(&dev->ir_rx_work);
256 flush_work(&dev->ir_tx_work);
43c24078 257}
dbda8f70 258
d8b4b582 259static void cx23885_input_ir_close(struct rc_dev *rc)
43c24078 260{
d8b4b582 261 struct cx23885_kernel_ir *kernel_ir = rc->priv;
43c24078
AW
262
263 if (kernel_ir->cx != NULL)
264 cx23885_input_ir_stop(kernel_ir->cx);
dbda8f70
AW
265}
266
267int cx23885_input_init(struct cx23885_dev *dev)
268{
43c24078 269 struct cx23885_kernel_ir *kernel_ir;
d8b4b582 270 struct rc_dev *rc;
43c24078 271 char *rc_map;
439be348 272 u64 allowed_protos;
43c24078 273
dbda8f70
AW
274 int ret;
275
276 /*
277 * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
278 * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
279 */
280 if (dev->sd_ir == NULL)
281 return -ENODEV;
282
283 switch (dev->board) {
9b3d8ecc 284 case CX23885_BOARD_HAUPPAUGE_HVR1270:
dbda8f70 285 case CX23885_BOARD_HAUPPAUGE_HVR1850:
7fec6fee 286 case CX23885_BOARD_HAUPPAUGE_HVR1290:
98d109f9 287 case CX23885_BOARD_HAUPPAUGE_HVR1250:
94f11518 288 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
98d109f9 289 /* Integrated CX2388[58] IR controller */
6d741bfe 290 allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
43c24078 291 /* The grey Hauppauge RC-5 remote */
15195d3a 292 rc_map = RC_MAP_HAUPPAUGE;
dbda8f70 293 break;
076f0e35
DB
294 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
295 /* Integrated CX23885 IR controller */
6d741bfe 296 allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
076f0e35
DB
297 /* The grey Terratec remote with orange buttons */
298 rc_map = RC_MAP_NEC_TERRATEC_CINERGY_XS;
299 break;
98d109f9
AW
300 case CX23885_BOARD_TEVII_S470:
301 /* Integrated CX23885 IR controller */
6d741bfe 302 allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
98d109f9
AW
303 /* A guess at the remote */
304 rc_map = RC_MAP_TEVII_NEC;
305 break;
e5f670b7
AJD
306 case CX23885_BOARD_MYGICA_X8507:
307 /* Integrated CX23885 IR controller */
6d741bfe 308 allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
e5f670b7
AJD
309 /* A guess at the remote */
310 rc_map = RC_MAP_TOTAL_MEDIA_IN_HAND_02;
311 break;
e6001482
LA
312 case CX23885_BOARD_TBS_6980:
313 case CX23885_BOARD_TBS_6981:
314 /* Integrated CX23885 IR controller */
6d741bfe 315 allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
e6001482
LA
316 /* A guess at the remote */
317 rc_map = RC_MAP_TBS_NEC;
318 break;
d11a3835 319 case CX23885_BOARD_DVBSKY_T9580:
070e6661 320 case CX23885_BOARD_DVBSKY_T980C:
321 case CX23885_BOARD_DVBSKY_S950C:
cba5480c 322 case CX23885_BOARD_DVBSKY_S950:
c29d6a83 323 case CX23885_BOARD_DVBSKY_S952:
c02ef64a 324 case CX23885_BOARD_DVBSKY_T982:
d11a3835 325 /* Integrated CX23885 IR controller */
6d741bfe 326 allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
d11a3835 327 rc_map = RC_MAP_DVBSKY;
328 break;
61b103e8
OS
329 case CX23885_BOARD_TT_CT2_4500_CI:
330 /* Integrated CX23885 IR controller */
6d741bfe 331 allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
61b103e8
OS
332 rc_map = RC_MAP_TT_1500;
333 break;
43c24078 334 default:
dbda8f70 335 return -ENODEV;
dbda8f70
AW
336 }
337
43c24078
AW
338 /* cx23885 board instance kernel IR state */
339 kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL);
340 if (kernel_ir == NULL)
341 return -ENOMEM;
dbda8f70 342
43c24078
AW
343 kernel_ir->cx = dev;
344 kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)",
345 cx23885_boards[dev->board].name);
1ee332ac
AY
346 if (!kernel_ir->name) {
347 ret = -ENOMEM;
348 goto err_out_free;
349 }
350
43c24078
AW
351 kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0",
352 pci_name(dev->pci));
1ee332ac
AY
353 if (!kernel_ir->phys) {
354 ret = -ENOMEM;
355 goto err_out_free_name;
356 }
dbda8f70 357
43c24078 358 /* input device */
0f7499fd 359 rc = rc_allocate_device(RC_DRIVER_IR_RAW);
d8b4b582 360 if (!rc) {
43c24078 361 ret = -ENOMEM;
1ee332ac 362 goto err_out_free_phys;
43c24078 363 }
055cd556 364
d8b4b582 365 kernel_ir->rc = rc;
518f4b26 366 rc->device_name = kernel_ir->name;
d8b4b582
DH
367 rc->input_phys = kernel_ir->phys;
368 rc->input_id.bustype = BUS_PCI;
369 rc->input_id.version = 1;
dbda8f70 370 if (dev->pci->subsystem_vendor) {
d8b4b582
DH
371 rc->input_id.vendor = dev->pci->subsystem_vendor;
372 rc->input_id.product = dev->pci->subsystem_device;
dbda8f70 373 } else {
d8b4b582
DH
374 rc->input_id.vendor = dev->pci->vendor;
375 rc->input_id.product = dev->pci->device;
dbda8f70 376 }
d8b4b582 377 rc->dev.parent = &dev->pci->dev;
c5540fbb 378 rc->allowed_protocols = allowed_protos;
d8b4b582
DH
379 rc->priv = kernel_ir;
380 rc->open = cx23885_input_ir_open;
381 rc->close = cx23885_input_ir_close;
382 rc->map_name = rc_map;
383 rc->driver_name = MODULE_NAME;
43c24078
AW
384
385 /* Go */
386 dev->kernel_ir = kernel_ir;
d8b4b582 387 ret = rc_register_device(rc);
dbda8f70
AW
388 if (ret)
389 goto err_out_stop;
390
391 return 0;
392
393err_out_stop:
394 cx23885_input_ir_stop(dev);
43c24078 395 dev->kernel_ir = NULL;
d8b4b582 396 rc_free_device(rc);
1ee332ac 397err_out_free_phys:
43c24078 398 kfree(kernel_ir->phys);
1ee332ac 399err_out_free_name:
43c24078 400 kfree(kernel_ir->name);
1ee332ac 401err_out_free:
43c24078 402 kfree(kernel_ir);
dbda8f70
AW
403 return ret;
404}
405
406void cx23885_input_fini(struct cx23885_dev *dev)
407{
408 /* Always stop the IR hardware from generating interrupts */
409 cx23885_input_ir_stop(dev);
410
43c24078 411 if (dev->kernel_ir == NULL)
dbda8f70 412 return;
d8b4b582 413 rc_unregister_device(dev->kernel_ir->rc);
43c24078
AW
414 kfree(dev->kernel_ir->phys);
415 kfree(dev->kernel_ir->name);
416 kfree(dev->kernel_ir);
417 dev->kernel_ir = NULL;
dbda8f70 418}