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