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