cx23885-input.c 10.9 KB
Newer Older
1 2 3 4 5 6 7
/*
 *  Driver for the Conexant CX23885/7/8 PCIe bridge
 *
 *  Infrared remote control input device
 *
 *  Most of this file is
 *
8
 *  Copyright (C) 2009  Andy Walls <awalls@md.metrocast.net>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 *  However, the cx23885_input_{init,fini} functions contained herein are
 *  derived from Linux kernel files linux/media/video/.../...-input.c marked as:
 *
 *  Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
 *  Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
 *		       Markus Rechberger <mrechberger@gmail.com>
 *		       Mauro Carvalho Chehab <mchehab@infradead.org>
 *		       Sascha Sommer <saschasommer@freenet.de>
 *  Copyright (C) 2004, 2005 Chris Pascoe
 *  Copyright (C) 2003, 2004 Gerd Knorr
 *  Copyright (C) 2003 Pavel Machek
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 */

38
#include <linux/slab.h>
39
#include <media/rc-core.h>
40 41 42
#include <media/v4l2-subdev.h>

#include "cx23885.h"
43
#include "cx23885-input.h"
44

45 46
#define MODULE_NAME "cx23885"

47 48
static void cx23885_input_process_measurements(struct cx23885_dev *dev,
					       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 60
		v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event,
				 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 78 79 80 81 82 83 84 85
}

void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)
{
	struct v4l2_subdev_ir_parameters params;
	int overrun, data_available;

	if (dev->sd_ir == NULL || events == 0)
		return;

	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 91
	case CX23885_BOARD_TEVII_S470:
	case CX23885_BOARD_HAUPPAUGE_HVR1250:
92
	case CX23885_BOARD_MYGICA_X8507:
93 94
	case CX23885_BOARD_TBS_6980:
	case CX23885_BOARD_TBS_6981:
95
		/*
96
		 * The only boards we handle right now.  However other boards
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		 * using the CX2388x integrated IR controller should be similar
		 */
		break;
	default:
		return;
	}

	overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN |
			    V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN);

	data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED |
				   V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ);

	if (overrun) {
		/* If there was a FIFO overrun, stop the device */
		v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
		params.enable = false;
		/* Mitigate race with cx23885_input_ir_stop() */
		params.shutdown = atomic_read(&dev->ir_input_stopping);
		v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
	}

	if (data_available)
120
		cx23885_input_process_measurements(dev, overrun);
121 122 123 124 125 126 127 128 129 130

	if (overrun) {
		/* If there was a FIFO overrun, clear & restart the device */
		params.enable = true;
		/* Mitigate race with cx23885_input_ir_stop() */
		params.shutdown = atomic_read(&dev->ir_input_stopping);
		v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
	}
}

131
static int cx23885_input_ir_start(struct cx23885_dev *dev)
132 133 134 135
{
	struct v4l2_subdev_ir_parameters params;

	if (dev->sd_ir == NULL)
136
		return -ENODEV;
137 138 139 140 141

	atomic_set(&dev->ir_input_stopping, 0);

	v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
	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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		/*
		 * The IR controller on this board only returns pulse widths.
		 * Any other mode setting will fail to set up the device.
		*/
		params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
		params.enable = true;
		params.interrupt_enable = true;
		params.shutdown = false;

		/* Setup for baseband compatible with both RC-5 and RC-6A */
		params.modulation = false;
		/* RC-5:  2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
		/* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
		params.max_pulse_width = 3333333; /* ns */
		/* RC-5:    666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
		/* RC-6A:   333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
		params.noise_filter_min_width = 333333; /* ns */
		/*
		 * This board has inverted receive sense:
		 * mark is received as low logic level;
		 * falling edges are detected as rising edges; etc.
		 */
169
		params.invert_level = true;
170
		break;
171
	case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
172
	case CX23885_BOARD_TEVII_S470:
173 174
	case CX23885_BOARD_TBS_6980:
	case CX23885_BOARD_TBS_6981:
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
		/*
		 * The IR controller on this board only returns pulse widths.
		 * Any other mode setting will fail to set up the device.
		 */
		params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
		params.enable = true;
		params.interrupt_enable = true;
		params.shutdown = false;

		/* Setup for a standard NEC protocol */
		params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */
		params.carrier_range_lower = 33000; /* Hz */
		params.carrier_range_upper = 43000; /* Hz */
		params.duty_cycle = 33; /* percent, 33 percent for NEC */

		/*
		 * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units
		 * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns
		 */
		params.max_pulse_width = 12378022; /* ns */

		/*
		 * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit
		 * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns
		 */
		params.noise_filter_min_width = 351648; /* ns */

		params.modulation = false;
		params.invert_level = true;
		break;
205 206
	}
	v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
207 208 209
	return 0;
}

210
static int cx23885_input_ir_open(struct rc_dev *rc)
211
{
212
	struct cx23885_kernel_ir *kernel_ir = rc->priv;
213 214 215 216 217

	if (kernel_ir->cx == NULL)
		return -ENODEV;

	return cx23885_input_ir_start(kernel_ir->cx);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
}

static void cx23885_input_ir_stop(struct cx23885_dev *dev)
{
	struct v4l2_subdev_ir_parameters params;

	if (dev->sd_ir == NULL)
		return;

	/*
	 * Stop the sd_ir subdevice from generating notifications and
	 * scheduling work.
	 * It is shutdown this way in order to mitigate a race with
	 * cx23885_input_rx_work_handler() in the overrun case, which could
	 * re-enable the subdevice.
	 */
	atomic_set(&dev->ir_input_stopping, 1);
	v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
	while (params.shutdown == false) {
		params.enable = false;
		params.interrupt_enable = false;
		params.shutdown = true;
		v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
		v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
	}
243 244 245
	flush_work(&dev->cx25840_work);
	flush_work(&dev->ir_rx_work);
	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 253

	if (kernel_ir->cx != NULL)
		cx23885_input_ir_stop(kernel_ir->cx);
254 255 256 257
}

int cx23885_input_init(struct cx23885_dev *dev)
{
258
	struct cx23885_kernel_ir *kernel_ir;
259
	struct rc_dev *rc;
260 261 262 263
	char *rc_map;
	enum rc_driver_type driver_type;
	unsigned long allowed_protos;

264 265 266 267 268 269 270 271 272 273
	int ret;

	/*
	 * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
	 * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
	 */
	if (dev->sd_ir == NULL)
		return -ENODEV;

	switch (dev->board) {
274
	case CX23885_BOARD_HAUPPAUGE_HVR1270:
275
	case CX23885_BOARD_HAUPPAUGE_HVR1850:
276
	case CX23885_BOARD_HAUPPAUGE_HVR1290:
277 278
	case CX23885_BOARD_HAUPPAUGE_HVR1250:
		/* 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 285 286
	case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
		/* Integrated CX23885 IR controller */
		driver_type = RC_DRIVER_IR_RAW;
287
		allowed_protos = RC_BIT_NEC;
288 289 290
		/* The grey Terratec remote with orange buttons */
		rc_map = RC_MAP_NEC_TERRATEC_CINERGY_XS;
		break;
291 292 293
	case CX23885_BOARD_TEVII_S470:
		/* Integrated CX23885 IR controller */
		driver_type = RC_DRIVER_IR_RAW;
294
		allowed_protos = RC_BIT_ALL;
295 296 297
		/* A guess at the remote */
		rc_map = RC_MAP_TEVII_NEC;
		break;
298 299 300 301 302 303 304
	case CX23885_BOARD_MYGICA_X8507:
		/* Integrated CX23885 IR controller */
		driver_type = RC_DRIVER_IR_RAW;
		allowed_protos = RC_BIT_ALL;
		/* A guess at the remote */
		rc_map = RC_MAP_TOTAL_MEDIA_IN_HAND_02;
		break;
305 306 307 308 309 310 311 312
	case CX23885_BOARD_TBS_6980:
	case CX23885_BOARD_TBS_6981:
		/* Integrated CX23885 IR controller */
		driver_type = RC_DRIVER_IR_RAW;
		allowed_protos = RC_BIT_ALL;
		/* A guess at the remote */
		rc_map = RC_MAP_TBS_NEC;
		break;
313
	default:
314 315 316
		return -ENODEV;
	}

317 318 319 320
	/* cx23885 board instance kernel IR state */
	kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL);
	if (kernel_ir == NULL)
		return -ENOMEM;
321

322 323 324 325 326
	kernel_ir->cx = dev;
	kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)",
				    cx23885_boards[dev->board].name);
	kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0",
				    pci_name(dev->pci));
327

328
	/* input device */
329 330
	rc = rc_allocate_device();
	if (!rc) {
331
		ret = -ENOMEM;
332
		goto err_out_free;
333
	}
334

335 336 337 338 339
	kernel_ir->rc = rc;
	rc->input_name = kernel_ir->name;
	rc->input_phys = kernel_ir->phys;
	rc->input_id.bustype = BUS_PCI;
	rc->input_id.version = 1;
340
	if (dev->pci->subsystem_vendor) {
341 342
		rc->input_id.vendor  = dev->pci->subsystem_vendor;
		rc->input_id.product = dev->pci->subsystem_device;
343
	} else {
344 345
		rc->input_id.vendor  = dev->pci->vendor;
		rc->input_id.product = dev->pci->device;
346
	}
347 348
	rc->dev.parent = &dev->pci->dev;
	rc->driver_type = driver_type;
349
	rc_set_allowed_protocols(rc, allowed_protos);
350 351 352 353 354
	rc->priv = kernel_ir;
	rc->open = cx23885_input_ir_open;
	rc->close = cx23885_input_ir_close;
	rc->map_name = rc_map;
	rc->driver_name = MODULE_NAME;
355 356 357

	/* Go */
	dev->kernel_ir = kernel_ir;
358
	ret = rc_register_device(rc);
359 360 361 362 363 364 365
	if (ret)
		goto err_out_stop;

	return 0;

err_out_stop:
	cx23885_input_ir_stop(dev);
366
	dev->kernel_ir = NULL;
367
	rc_free_device(rc);
368
err_out_free:
369 370 371
	kfree(kernel_ir->phys);
	kfree(kernel_ir->name);
	kfree(kernel_ir);
372 373 374 375 376 377 378 379
	return ret;
}

void cx23885_input_fini(struct cx23885_dev *dev)
{
	/* Always stop the IR hardware from generating interrupts */
	cx23885_input_ir_stop(dev);

380
	if (dev->kernel_ir == NULL)
381
		return;
382
	rc_unregister_device(dev->kernel_ir->rc);
383 384 385 386
	kfree(dev->kernel_ir->phys);
	kfree(dev->kernel_ir->name);
	kfree(dev->kernel_ir);
	dev->kernel_ir = NULL;
387
}