cx88-input.c 11.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 *
 * Device driver for GPIO attached remote control interfaces
 * on Conexant 2388x based TV/DVB cards.
 *
 * Copyright (c) 2003 Pavel Machek
 * Copyright (c) 2004 Gerd Knorr
8
 * Copyright (c) 2004, 2005 Chris Pascoe
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/init.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/pci.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

#include "cx88.h"
33
#include <media/ir-common.h>
L
Linus Torvalds 已提交
34 35 36 37

/* ---------------------------------------------------------------------- */

struct cx88_IR {
M
Mauro Carvalho Chehab 已提交
38
	struct cx88_core *core;
39
	struct input_dev *input;
M
Mauro Carvalho Chehab 已提交
40 41 42
	struct ir_input_state ir;
	char name[32];
	char phys[32];
L
Linus Torvalds 已提交
43 44

	/* sample from gpio pin 16 */
45
	u32 sampling;
M
Mauro Carvalho Chehab 已提交
46 47 48
	u32 samples[16];
	int scount;
	unsigned long release;
L
Linus Torvalds 已提交
49 50

	/* poll external decoder */
M
Mauro Carvalho Chehab 已提交
51 52 53 54 55 56 57 58
	int polling;
	struct work_struct work;
	struct timer_list timer;
	u32 gpio_addr;
	u32 last_gpio;
	u32 mask_keycode;
	u32 mask_keydown;
	u32 mask_keyup;
L
Linus Torvalds 已提交
59 60 61
};

static int ir_debug = 0;
M
Mauro Carvalho Chehab 已提交
62
module_param(ir_debug, int, 0644);	/* debug level [IR] */
L
Linus Torvalds 已提交
63 64 65
MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");

#define ir_dprintk(fmt, arg...)	if (ir_debug) \
66
	printk(KERN_DEBUG "%s IR: " fmt , ir->core->name , ##arg)
L
Linus Torvalds 已提交
67 68 69 70 71 72

/* ---------------------------------------------------------------------- */

static void cx88_ir_handle_key(struct cx88_IR *ir)
{
	struct cx88_core *core = ir->core;
73
	u32 gpio, data, auxgpio;
L
Linus Torvalds 已提交
74 75 76

	/* read gpio value */
	gpio = cx_read(ir->gpio_addr);
77
	if (core->board == CX88_BOARD_NPGTECH_REALTV_TOP10FM) {
78 79 80 81 82 83 84 85 86 87 88 89 90 91
		/* This board apparently uses a combination of 2 GPIO
		   to represent the keys. Additionally, the second GPIO
		   can be used for parity.

		   Example:

		   for key "5"
			gpio = 0x758, auxgpio = 0xe5 or 0xf5
		   for key "Power"
			gpio = 0x758, auxgpio = 0xed or 0xfd
		 */

		auxgpio = cx_read(MO_GP1_IO);
		/* Take out the parity part */
92
		gpio=(gpio & 0x7fd) + (auxgpio & 0xef);
93 94 95
	} else
		auxgpio = gpio;

L
Linus Torvalds 已提交
96
	if (ir->polling) {
97
		if (ir->last_gpio == auxgpio)
L
Linus Torvalds 已提交
98
			return;
99
		ir->last_gpio = auxgpio;
L
Linus Torvalds 已提交
100 101 102 103 104
	}

	/* extract data */
	data = ir_extract_bits(gpio, ir->mask_keycode);
	ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
M
Mauro Carvalho Chehab 已提交
105 106 107 108
		   gpio, data,
		   ir->polling ? "poll" : "irq",
		   (gpio & ir->mask_keydown) ? " down" : "",
		   (gpio & ir->mask_keyup) ? " up" : "");
L
Linus Torvalds 已提交
109

110 111 112 113 114 115 116 117 118
	if (ir->core->board == CX88_BOARD_NORWOOD_MICRO) {
		u32 gpio_key = cx_read(MO_GP0_IO);

		data = (data << 4) | ((gpio_key & 0xf0) >> 4);

		ir_input_keydown(ir->input, &ir->ir, data, data);
		ir_input_nokey(ir->input, &ir->ir);

	} else if (ir->mask_keydown) {
L
Linus Torvalds 已提交
119 120
		/* bit set on keydown */
		if (gpio & ir->mask_keydown) {
121
			ir_input_keydown(ir->input, &ir->ir, data, data);
L
Linus Torvalds 已提交
122
		} else {
123
			ir_input_nokey(ir->input, &ir->ir);
L
Linus Torvalds 已提交
124 125 126 127 128
		}

	} else if (ir->mask_keyup) {
		/* bit cleared on keydown */
		if (0 == (gpio & ir->mask_keyup)) {
129
			ir_input_keydown(ir->input, &ir->ir, data, data);
L
Linus Torvalds 已提交
130
		} else {
131
			ir_input_nokey(ir->input, &ir->ir);
L
Linus Torvalds 已提交
132 133 134 135
		}

	} else {
		/* can't distinguish keydown/up :-/ */
136 137
		ir_input_keydown(ir->input, &ir->ir, data, data);
		ir_input_nokey(ir->input, &ir->ir);
L
Linus Torvalds 已提交
138 139 140 141 142
	}
}

static void ir_timer(unsigned long data)
{
M
Mauro Carvalho Chehab 已提交
143
	struct cx88_IR *ir = (struct cx88_IR *)data;
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

	schedule_work(&ir->work);
}

static void cx88_ir_work(void *data)
{
	struct cx88_IR *ir = data;
	unsigned long timeout;

	cx88_ir_handle_key(ir);
	timeout = jiffies + (ir->polling * HZ / 1000);
	mod_timer(&ir->timer, timeout);
}

/* ---------------------------------------------------------------------- */

int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
{
	struct cx88_IR *ir;
163
	struct input_dev *input_dev;
L
Linus Torvalds 已提交
164 165 166
	IR_KEYTAB_TYPE *ir_codes = NULL;
	int ir_type = IR_TYPE_OTHER;

167 168 169 170 171
	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!ir || !input_dev) {
		kfree(ir);
		input_free_device(input_dev);
L
Linus Torvalds 已提交
172
		return -ENOMEM;
173 174 175
	}

	ir->input = input_dev;
L
Linus Torvalds 已提交
176 177 178 179

	/* detect & configure */
	switch (core->board) {
	case CX88_BOARD_DNTV_LIVE_DVB_T:
180
	case CX88_BOARD_KWORLD_DVB_T:
181
	case CX88_BOARD_KWORLD_DVB_T_CX22702:
M
Mauro Carvalho Chehab 已提交
182 183
		ir_codes = ir_codes_dntv_live_dvb_t;
		ir->gpio_addr = MO_GP1_IO;
L
Linus Torvalds 已提交
184
		ir->mask_keycode = 0x1f;
M
Mauro Carvalho Chehab 已提交
185 186
		ir->mask_keyup = 0x60;
		ir->polling = 50; /* ms */
L
Linus Torvalds 已提交
187
		break;
188 189 190
	case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
		ir_codes = ir_codes_cinergy_1400;
		ir_type = IR_TYPE_PD;
191
		ir->sampling = 0xeb04; /* address */
192
		break;
L
Linus Torvalds 已提交
193 194
	case CX88_BOARD_HAUPPAUGE:
	case CX88_BOARD_HAUPPAUGE_DVB_T1:
195 196
	case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
	case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
197
	case CX88_BOARD_HAUPPAUGE_HVR1100:
198
	case CX88_BOARD_HAUPPAUGE_HVR1300:
M
Mauro Carvalho Chehab 已提交
199 200 201
		ir_codes = ir_codes_hauppauge_new;
		ir_type = IR_TYPE_RC5;
		ir->sampling = 1;
L
Linus Torvalds 已提交
202
		break;
203
	case CX88_BOARD_WINFAST_DTV2000H:
L
Linus Torvalds 已提交
204
	case CX88_BOARD_WINFAST2000XP_EXPERT:
M
Mauro Carvalho Chehab 已提交
205 206
		ir_codes = ir_codes_winfast;
		ir->gpio_addr = MO_GP0_IO;
L
Linus Torvalds 已提交
207
		ir->mask_keycode = 0x8f8;
M
Mauro Carvalho Chehab 已提交
208
		ir->mask_keyup = 0x100;
209
		ir->polling = 50; /* ms */
L
Linus Torvalds 已提交
210 211
		break;
	case CX88_BOARD_IODATA_GVBCTV7E:
M
Mauro Carvalho Chehab 已提交
212 213
		ir_codes = ir_codes_iodata_bctv7e;
		ir->gpio_addr = MO_GP0_IO;
L
Linus Torvalds 已提交
214 215
		ir->mask_keycode = 0xfd;
		ir->mask_keydown = 0x02;
M
Mauro Carvalho Chehab 已提交
216
		ir->polling = 5; /* ms */
L
Linus Torvalds 已提交
217
		break;
218
       case CX88_BOARD_PROLINK_PLAYTVPVR:
219
	case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
M
Mauro Carvalho Chehab 已提交
220 221
		ir_codes = ir_codes_pixelview;
		ir->gpio_addr = MO_GP1_IO;
222
		ir->mask_keycode = 0x1f;
M
Mauro Carvalho Chehab 已提交
223 224
		ir->mask_keyup = 0x80;
		ir->polling = 1; /* ms */
225
		break;
226 227 228 229 230 231 232
	case CX88_BOARD_KWORLD_LTV883:
		ir_codes = ir_codes_pixelview;
		ir->gpio_addr = MO_GP1_IO;
		ir->mask_keycode = 0x1f;
		ir->mask_keyup = 0x60;
		ir->polling = 1; /* ms */
		break;
M
Mauro Carvalho Chehab 已提交
233
	case CX88_BOARD_ADSTECH_DVB_T_PCI:
M
Mauro Carvalho Chehab 已提交
234 235
		ir_codes = ir_codes_adstech_dvb_t_pci;
		ir->gpio_addr = MO_GP1_IO;
M
Mauro Carvalho Chehab 已提交
236
		ir->mask_keycode = 0xbf;
M
Mauro Carvalho Chehab 已提交
237 238 239 240 241 242 243 244 245
		ir->mask_keyup = 0x40;
		ir->polling = 50; /* ms */
		break;
	case CX88_BOARD_MSI_TVANYWHERE_MASTER:
		ir_codes = ir_codes_msi_tvanywhere;
		ir->gpio_addr = MO_GP1_IO;
		ir->mask_keycode = 0x1f;
		ir->mask_keyup = 0x40;
		ir->polling = 1; /* ms */
M
Mauro Carvalho Chehab 已提交
246
		break;
247
	case CX88_BOARD_AVERTV_303:
248
	case CX88_BOARD_AVERTV_STUDIO_303:
249 250 251 252 253 254
		ir_codes         = ir_codes_avertv_303;
		ir->gpio_addr    = MO_GP2_IO;
		ir->mask_keycode = 0xfb;
		ir->mask_keydown = 0x02;
		ir->polling      = 50; /* ms */
		break;
255 256 257 258 259
	case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
		ir_codes = ir_codes_dntv_live_dvbt_pro;
		ir_type = IR_TYPE_PD;
		ir->sampling = 0xff00; /* address */
		break;
260 261 262 263 264 265 266
	case CX88_BOARD_NORWOOD_MICRO:
		ir_codes         = ir_codes_norwood;
		ir->gpio_addr    = MO_GP1_IO;
		ir->mask_keycode = 0x0e;
		ir->mask_keyup   = 0x80;
		ir->polling      = 50; /* ms */
		break;
267
	case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
268 269 270 271 272
		ir_codes = ir_codes_npgtech;
		ir->gpio_addr = MO_GP0_IO;
		ir->mask_keycode = 0xfa;
		ir->polling = 50; /* ms */
		break;
L
Linus Torvalds 已提交
273
	}
274

L
Linus Torvalds 已提交
275 276
	if (NULL == ir_codes) {
		kfree(ir);
277
		input_free_device(input_dev);
L
Linus Torvalds 已提交
278 279 280 281 282 283
		return -ENODEV;
	}

	/* init input device */
	snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
		 cx88_boards[core->board].name);
M
Mauro Carvalho Chehab 已提交
284
	snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
L
Linus Torvalds 已提交
285

286 287 288 289 290
	ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
	input_dev->name = ir->name;
	input_dev->phys = ir->phys;
	input_dev->id.bustype = BUS_PCI;
	input_dev->id.version = 1;
L
Linus Torvalds 已提交
291
	if (pci->subsystem_vendor) {
292 293
		input_dev->id.vendor = pci->subsystem_vendor;
		input_dev->id.product = pci->subsystem_device;
L
Linus Torvalds 已提交
294
	} else {
295 296
		input_dev->id.vendor = pci->vendor;
		input_dev->id.product = pci->device;
L
Linus Torvalds 已提交
297
	}
298
	input_dev->cdev.dev = &pci->dev;
L
Linus Torvalds 已提交
299 300 301 302 303 304 305 306
	/* record handles to ourself */
	ir->core = core;
	core->ir = ir;

	if (ir->polling) {
		INIT_WORK(&ir->work, cx88_ir_work, ir);
		init_timer(&ir->timer);
		ir->timer.function = ir_timer;
M
Mauro Carvalho Chehab 已提交
307
		ir->timer.data = (unsigned long)ir;
L
Linus Torvalds 已提交
308 309 310
		schedule_work(&ir->work);
	}
	if (ir->sampling) {
M
Mauro Carvalho Chehab 已提交
311 312 313
		core->pci_irqmask |= (1 << 18);	/* IR_SMP_INT */
		cx_write(MO_DDS_IO, 0xa80a80);	/* 4 kHz sample rate */
		cx_write(MO_DDSCFG_IO, 0x5);	/* enable */
L
Linus Torvalds 已提交
314 315 316
	}

	/* all done */
317
	input_register_device(ir->input);
L
Linus Torvalds 已提交
318 319 320 321 322 323 324 325 326 327 328 329

	return 0;
}

int cx88_ir_fini(struct cx88_core *core)
{
	struct cx88_IR *ir = core->ir;

	/* skip detach on non attached boards */
	if (NULL == ir)
		return 0;

330 331 332 333
	if (ir->sampling) {
		cx_write(MO_DDSCFG_IO, 0x0);
		core->pci_irqmask &= ~(1 << 18);
	}
L
Linus Torvalds 已提交
334 335 336 337 338
	if (ir->polling) {
		del_timer(&ir->timer);
		flush_scheduled_work();
	}

339
	input_unregister_device(ir->input);
L
Linus Torvalds 已提交
340 341 342 343 344 345 346 347 348 349 350 351
	kfree(ir);

	/* done */
	core->ir = NULL;
	return 0;
}

/* ---------------------------------------------------------------------- */

void cx88_ir_irq(struct cx88_core *core)
{
	struct cx88_IR *ir = core->ir;
352
	u32 samples, ircode;
L
Linus Torvalds 已提交
353 354 355 356 357 358 359 360
	int i;

	if (NULL == ir)
		return;
	if (!ir->sampling)
		return;

	samples = cx_read(MO_SAMPLE_IO);
M
Mauro Carvalho Chehab 已提交
361
	if (0 != samples && 0xffffffff != samples) {
L
Linus Torvalds 已提交
362 363 364 365 366 367 368
		/* record sample data */
		if (ir->scount < ARRAY_SIZE(ir->samples))
			ir->samples[ir->scount++] = samples;
		return;
	}
	if (!ir->scount) {
		/* nothing to sample */
M
Mauro Carvalho Chehab 已提交
369
		if (ir->ir.keypressed && time_after(jiffies, ir->release))
370
			ir_input_nokey(ir->input, &ir->ir);
L
Linus Torvalds 已提交
371 372 373 374 375 376 377 378 379
		return;
	}

	/* have a complete sample */
	if (ir->scount < ARRAY_SIZE(ir->samples))
		ir->samples[ir->scount++] = samples;
	for (i = 0; i < ir->scount; i++)
		ir->samples[i] = ~ir->samples[i];
	if (ir_debug)
M
Mauro Carvalho Chehab 已提交
380
		ir_dump_samples(ir->samples, ir->scount);
L
Linus Torvalds 已提交
381 382 383

	/* decode it */
	switch (core->board) {
384
	case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
385
	case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
		ircode = ir_decode_pulsedistance(ir->samples, ir->scount, 1, 4);

		if (ircode == 0xffffffff) { /* decoding error */
			ir_dprintk("pulse distance decoding error\n");
			break;
		}

		ir_dprintk("pulse distance decoded: %x\n", ircode);

		if (ircode == 0) { /* key still pressed */
			ir_dprintk("pulse distance decoded repeat code\n");
			ir->release = jiffies + msecs_to_jiffies(120);
			break;
		}

401
		if ((ircode & 0xffff) != (ir->sampling & 0xffff)) { /* wrong address */
402
			ir_dprintk("pulse distance decoded wrong address\n");
403
			break;
404 405 406 407 408 409 410 411 412
		}

		if (((~ircode >> 24) & 0xff) != ((ircode >> 16) & 0xff)) { /* wrong checksum */
			ir_dprintk("pulse distance decoded wrong check sum\n");
			break;
		}

		ir_dprintk("Key Code: %x\n", (ircode >> 16) & 0x7f);

413
		ir_input_keydown(ir->input, &ir->ir, (ircode >> 16) & 0x7f, (ircode >> 16) & 0xff);
414 415
		ir->release = jiffies + msecs_to_jiffies(120);
		break;
L
Linus Torvalds 已提交
416 417
	case CX88_BOARD_HAUPPAUGE:
	case CX88_BOARD_HAUPPAUGE_DVB_T1:
418 419
	case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
	case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
C
 
Chris Pascoe 已提交
420
	case CX88_BOARD_HAUPPAUGE_HVR1100:
421
	case CX88_BOARD_HAUPPAUGE_HVR1300:
422 423 424
		ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
		ir_dprintk("biphase decoded: %x\n", ircode);
		if ((ircode & 0xfffff000) != 0x3000)
L
Linus Torvalds 已提交
425
			break;
426
		ir_input_keydown(ir->input, &ir->ir, ircode & 0x3f, ircode);
L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
		ir->release = jiffies + msecs_to_jiffies(120);
		break;
	}

	ir->scount = 0;
	return;
}

/* ---------------------------------------------------------------------- */

MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
MODULE_LICENSE("GPL");
/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */