blob: 3f924e21b9575f7d67d99d71c8585d41828aabfe [file] [log] [blame]
Andy Wallsdbda8f72009-09-27 20:55:41 -03001/*
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 *
Andy Walls6afdeaf2010-05-23 18:53:35 -03008 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
Andy Wallsdbda8f72009-09-27 20:55:41 -03009 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Andy Walls43c24072010-06-27 23:15:35 -030040#include <media/ir-core.h>
Andy Wallsdbda8f72009-09-27 20:55:41 -030041#include <media/v4l2-subdev.h>
42
43#include "cx23885.h"
44
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -030045#define MODULE_NAME "cx23885"
46
Andy Walls43c24072010-06-27 23:15:35 -030047static void convert_measurement(u32 x, struct ir_raw_event *y)
Andy Wallsdbda8f72009-09-27 20:55:41 -030048{
Andy Walls43c24072010-06-27 23:15:35 -030049 if (x == V4L2_SUBDEV_IR_PULSE_RX_SEQ_END) {
50 y->pulse = false;
51 y->duration = V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS;
52 return;
53 }
54
55 y->pulse = (x & V4L2_SUBDEV_IR_PULSE_LEVEL_MASK) ? true : false;
56 y->duration = x & V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS;
Andy Wallsdbda8f72009-09-27 20:55:41 -030057}
58
Andy Walls43c24072010-06-27 23:15:35 -030059static void cx23885_input_process_measurements(struct cx23885_dev *dev,
60 bool overrun)
Andy Wallsdbda8f72009-09-27 20:55:41 -030061{
Andy Walls43c24072010-06-27 23:15:35 -030062 struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir;
63 struct ir_raw_event kernel_ir_event;
Andy Wallsdbda8f72009-09-27 20:55:41 -030064
Andy Walls43c24072010-06-27 23:15:35 -030065 u32 sd_ir_data[64];
66 ssize_t num;
Andy Wallsdbda8f72009-09-27 20:55:41 -030067 int count, i;
Andy Walls43c24072010-06-27 23:15:35 -030068 bool handle = false;
Andy Wallsdbda8f72009-09-27 20:55:41 -030069
70 do {
Andy Walls43c24072010-06-27 23:15:35 -030071 num = 0;
72 v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) sd_ir_data,
73 sizeof(sd_ir_data), &num);
Andy Wallsdbda8f72009-09-27 20:55:41 -030074
75 count = num / sizeof(u32);
76
Andy Walls43c24072010-06-27 23:15:35 -030077 for (i = 0; i < count; i++) {
78 convert_measurement(sd_ir_data[i], &kernel_ir_event);
79 ir_raw_event_store(kernel_ir->inp_dev,
80 &kernel_ir_event);
81 handle = true;
Andy Wallsdbda8f72009-09-27 20:55:41 -030082 }
Andy Wallsdbda8f72009-09-27 20:55:41 -030083 } while (num != 0);
Andy Walls43c24072010-06-27 23:15:35 -030084
85 if (overrun)
86 ir_raw_event_reset(kernel_ir->inp_dev);
87 else if (handle)
88 ir_raw_event_handle(kernel_ir->inp_dev);
Andy Wallsdbda8f72009-09-27 20:55:41 -030089}
90
91void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)
92{
93 struct v4l2_subdev_ir_parameters params;
94 int overrun, data_available;
95
96 if (dev->sd_ir == NULL || events == 0)
97 return;
98
99 switch (dev->board) {
100 case CX23885_BOARD_HAUPPAUGE_HVR1850:
Michael Krufky7fec6fe2009-11-11 15:46:09 -0300101 case CX23885_BOARD_HAUPPAUGE_HVR1290:
Andy Walls98d109f2010-07-19 00:41:41 -0300102 case CX23885_BOARD_TEVII_S470:
103 case CX23885_BOARD_HAUPPAUGE_HVR1250:
Andy Wallsdbda8f72009-09-27 20:55:41 -0300104 /*
Andy Walls98d109f2010-07-19 00:41:41 -0300105 * The only boards we handle right now. However other boards
Andy Wallsdbda8f72009-09-27 20:55:41 -0300106 * using the CX2388x integrated IR controller should be similar
107 */
108 break;
109 default:
110 return;
111 }
112
113 overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN |
114 V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN);
115
116 data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED |
117 V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ);
118
119 if (overrun) {
120 /* If there was a FIFO overrun, stop the device */
121 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
122 params.enable = false;
123 /* Mitigate race with cx23885_input_ir_stop() */
124 params.shutdown = atomic_read(&dev->ir_input_stopping);
125 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
126 }
127
128 if (data_available)
Andy Walls43c24072010-06-27 23:15:35 -0300129 cx23885_input_process_measurements(dev, overrun);
Andy Wallsdbda8f72009-09-27 20:55:41 -0300130
131 if (overrun) {
132 /* If there was a FIFO overrun, clear & restart the device */
133 params.enable = true;
134 /* Mitigate race with cx23885_input_ir_stop() */
135 params.shutdown = atomic_read(&dev->ir_input_stopping);
136 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
137 }
138}
139
Andy Walls43c24072010-06-27 23:15:35 -0300140static int cx23885_input_ir_start(struct cx23885_dev *dev)
Andy Wallsdbda8f72009-09-27 20:55:41 -0300141{
Andy Wallsdbda8f72009-09-27 20:55:41 -0300142 struct v4l2_subdev_ir_parameters params;
143
144 if (dev->sd_ir == NULL)
Andy Walls43c24072010-06-27 23:15:35 -0300145 return -ENODEV;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300146
147 atomic_set(&dev->ir_input_stopping, 0);
148
Andy Wallsdbda8f72009-09-27 20:55:41 -0300149 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
150 switch (dev->board) {
151 case CX23885_BOARD_HAUPPAUGE_HVR1850:
Michael Krufky7fec6fe2009-11-11 15:46:09 -0300152 case CX23885_BOARD_HAUPPAUGE_HVR1290:
Andy Walls98d109f2010-07-19 00:41:41 -0300153 case CX23885_BOARD_HAUPPAUGE_HVR1250:
Andy Wallsdbda8f72009-09-27 20:55:41 -0300154 /*
155 * The IR controller on this board only returns pulse widths.
156 * Any other mode setting will fail to set up the device.
157 */
158 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
159 params.enable = true;
160 params.interrupt_enable = true;
161 params.shutdown = false;
162
163 /* Setup for baseband compatible with both RC-5 and RC-6A */
164 params.modulation = false;
165 /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
166 /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
167 params.max_pulse_width = 3333333; /* ns */
168 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
169 /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
170 params.noise_filter_min_width = 333333; /* ns */
171 /*
172 * This board has inverted receive sense:
173 * mark is received as low logic level;
174 * falling edges are detected as rising edges; etc.
175 */
Andy Walls5a28d9a2010-07-18 19:57:25 -0300176 params.invert_level = true;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300177 break;
Andy Walls98d109f2010-07-19 00:41:41 -0300178 case CX23885_BOARD_TEVII_S470:
179 /*
180 * The IR controller on this board only returns pulse widths.
181 * Any other mode setting will fail to set up the device.
182 */
183 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
184 params.enable = true;
185 params.interrupt_enable = true;
186 params.shutdown = false;
187
188 /* Setup for a standard NEC protocol */
189 params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */
190 params.carrier_range_lower = 33000; /* Hz */
191 params.carrier_range_upper = 43000; /* Hz */
192 params.duty_cycle = 33; /* percent, 33 percent for NEC */
193
194 /*
195 * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units
196 * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns
197 */
198 params.max_pulse_width = 12378022; /* ns */
199
200 /*
201 * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit
202 * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns
203 */
204 params.noise_filter_min_width = 351648; /* ns */
205
206 params.modulation = false;
207 params.invert_level = true;
208 break;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300209 }
210 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
Andy Walls43c24072010-06-27 23:15:35 -0300211 return 0;
212}
213
214static int cx23885_input_ir_open(void *priv)
215{
216 struct cx23885_kernel_ir *kernel_ir = priv;
217
218 if (kernel_ir->cx == NULL)
219 return -ENODEV;
220
221 return cx23885_input_ir_start(kernel_ir->cx);
Andy Wallsdbda8f72009-09-27 20:55:41 -0300222}
223
224static void cx23885_input_ir_stop(struct cx23885_dev *dev)
225{
Andy Wallsdbda8f72009-09-27 20:55:41 -0300226 struct v4l2_subdev_ir_parameters params;
227
228 if (dev->sd_ir == NULL)
229 return;
230
231 /*
232 * Stop the sd_ir subdevice from generating notifications and
233 * scheduling work.
234 * It is shutdown this way in order to mitigate a race with
235 * cx23885_input_rx_work_handler() in the overrun case, which could
236 * re-enable the subdevice.
237 */
238 atomic_set(&dev->ir_input_stopping, 1);
239 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
240 while (params.shutdown == false) {
241 params.enable = false;
242 params.interrupt_enable = false;
243 params.shutdown = true;
244 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
245 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
246 }
247
248 flush_scheduled_work();
Andy Walls43c24072010-06-27 23:15:35 -0300249}
Andy Wallsdbda8f72009-09-27 20:55:41 -0300250
Andy Walls43c24072010-06-27 23:15:35 -0300251static void cx23885_input_ir_close(void *priv)
252{
253 struct cx23885_kernel_ir *kernel_ir = priv;
254
255 if (kernel_ir->cx != NULL)
256 cx23885_input_ir_stop(kernel_ir->cx);
Andy Wallsdbda8f72009-09-27 20:55:41 -0300257}
258
259int cx23885_input_init(struct cx23885_dev *dev)
260{
Andy Walls43c24072010-06-27 23:15:35 -0300261 struct cx23885_kernel_ir *kernel_ir;
262 struct input_dev *inp_dev;
263 struct ir_dev_props *props;
264
265 char *rc_map;
266 enum rc_driver_type driver_type;
267 unsigned long allowed_protos;
268
Andy Wallsdbda8f72009-09-27 20:55:41 -0300269 int ret;
270
271 /*
272 * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
273 * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
274 */
275 if (dev->sd_ir == NULL)
276 return -ENODEV;
277
278 switch (dev->board) {
279 case CX23885_BOARD_HAUPPAUGE_HVR1850:
Michael Krufky7fec6fe2009-11-11 15:46:09 -0300280 case CX23885_BOARD_HAUPPAUGE_HVR1290:
Andy Walls98d109f2010-07-19 00:41:41 -0300281 case CX23885_BOARD_HAUPPAUGE_HVR1250:
282 /* Integrated CX2388[58] IR controller */
Andy Walls43c24072010-06-27 23:15:35 -0300283 driver_type = RC_DRIVER_IR_RAW;
284 allowed_protos = IR_TYPE_ALL;
285 /* The grey Hauppauge RC-5 remote */
286 rc_map = RC_MAP_RC5_HAUPPAUGE_NEW;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300287 break;
Andy Walls98d109f2010-07-19 00:41:41 -0300288 case CX23885_BOARD_TEVII_S470:
289 /* Integrated CX23885 IR controller */
290 driver_type = RC_DRIVER_IR_RAW;
291 allowed_protos = IR_TYPE_ALL;
292 /* A guess at the remote */
293 rc_map = RC_MAP_TEVII_NEC;
294 break;
Andy Walls43c24072010-06-27 23:15:35 -0300295 default:
Andy Wallsdbda8f72009-09-27 20:55:41 -0300296 return -ENODEV;
Andy Walls43c24072010-06-27 23:15:35 -0300297 }
Andy Wallsdbda8f72009-09-27 20:55:41 -0300298
Andy Walls43c24072010-06-27 23:15:35 -0300299 /* cx23885 board instance kernel IR state */
300 kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL);
301 if (kernel_ir == NULL)
302 return -ENOMEM;
303
304 kernel_ir->cx = dev;
305 kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)",
306 cx23885_boards[dev->board].name);
307 kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0",
308 pci_name(dev->pci));
309
310 /* input device */
311 inp_dev = input_allocate_device();
312 if (inp_dev == NULL) {
Andy Wallsdbda8f72009-09-27 20:55:41 -0300313 ret = -ENOMEM;
314 goto err_out_free;
315 }
316
Andy Walls43c24072010-06-27 23:15:35 -0300317 kernel_ir->inp_dev = inp_dev;
318 inp_dev->name = kernel_ir->name;
319 inp_dev->phys = kernel_ir->phys;
320 inp_dev->id.bustype = BUS_PCI;
321 inp_dev->id.version = 1;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300322 if (dev->pci->subsystem_vendor) {
Andy Walls43c24072010-06-27 23:15:35 -0300323 inp_dev->id.vendor = dev->pci->subsystem_vendor;
324 inp_dev->id.product = dev->pci->subsystem_device;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300325 } else {
Andy Walls43c24072010-06-27 23:15:35 -0300326 inp_dev->id.vendor = dev->pci->vendor;
327 inp_dev->id.product = dev->pci->device;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300328 }
Andy Walls43c24072010-06-27 23:15:35 -0300329 inp_dev->dev.parent = &dev->pci->dev;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300330
Andy Walls43c24072010-06-27 23:15:35 -0300331 /* kernel ir device properties */
332 props = &kernel_ir->props;
333 props->driver_type = driver_type;
334 props->allowed_protos = allowed_protos;
335 props->priv = kernel_ir;
336 props->open = cx23885_input_ir_open;
337 props->close = cx23885_input_ir_close;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300338
Andy Walls43c24072010-06-27 23:15:35 -0300339 /* Go */
340 dev->kernel_ir = kernel_ir;
341 ret = ir_input_register(inp_dev, rc_map, props, MODULE_NAME);
Andy Wallsdbda8f72009-09-27 20:55:41 -0300342 if (ret)
343 goto err_out_stop;
344
345 return 0;
346
347err_out_stop:
348 cx23885_input_ir_stop(dev);
Andy Walls43c24072010-06-27 23:15:35 -0300349 dev->kernel_ir = NULL;
350 /* TODO: double check clean-up of kernel_ir->inp_dev */
Andy Wallsdbda8f72009-09-27 20:55:41 -0300351err_out_free:
Andy Walls43c24072010-06-27 23:15:35 -0300352 kfree(kernel_ir->phys);
353 kfree(kernel_ir->name);
354 kfree(kernel_ir);
Andy Wallsdbda8f72009-09-27 20:55:41 -0300355 return ret;
356}
357
358void cx23885_input_fini(struct cx23885_dev *dev)
359{
360 /* Always stop the IR hardware from generating interrupts */
361 cx23885_input_ir_stop(dev);
362
Andy Walls43c24072010-06-27 23:15:35 -0300363 if (dev->kernel_ir == NULL)
Andy Wallsdbda8f72009-09-27 20:55:41 -0300364 return;
Andy Walls43c24072010-06-27 23:15:35 -0300365 ir_input_unregister(dev->kernel_ir->inp_dev);
366 kfree(dev->kernel_ir->phys);
367 kfree(dev->kernel_ir->name);
368 kfree(dev->kernel_ir);
369 dev->kernel_ir = NULL;
Andy Wallsdbda8f72009-09-27 20:55:41 -0300370}