1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "cx88.h"
22
23#include <linux/init.h>
24#include <linux/hrtimer.h>
25#include <linux/pci.h>
26#include <linux/slab.h>
27#include <linux/module.h>
28
29#include <media/rc-core.h>
30
31#define MODULE_NAME "cx88xx"
32
33
34
35struct cx88_IR {
36 struct cx88_core *core;
37 struct rc_dev *dev;
38
39 int users;
40
41 char name[32];
42 char phys[32];
43
44
45 u32 sampling;
46
47
48 int polling;
49 struct hrtimer timer;
50 u32 gpio_addr;
51 u32 last_gpio;
52 u32 mask_keycode;
53 u32 mask_keydown;
54 u32 mask_keyup;
55};
56
57static unsigned int ir_samplerate = 4;
58module_param(ir_samplerate, uint, 0444);
59MODULE_PARM_DESC(ir_samplerate, "IR samplerate in kHz, 1 - 20, default 4");
60
61static int ir_debug;
62module_param(ir_debug, int, 0644);
63MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
64
65#define ir_dprintk(fmt, arg...) do { \
66 if (ir_debug) \
67 printk(KERN_DEBUG "%s IR: " fmt, ir->core->name, ##arg);\
68} while (0)
69
70#define dprintk(fmt, arg...) do { \
71 if (ir_debug) \
72 printk(KERN_DEBUG "cx88 IR: " fmt, ##arg); \
73} while (0)
74
75
76
77static void cx88_ir_handle_key(struct cx88_IR *ir)
78{
79 struct cx88_core *core = ir->core;
80 u32 gpio, data, auxgpio;
81
82
83 gpio = cx_read(ir->gpio_addr);
84 switch (core->boardnr) {
85 case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
86
87
88
89
90
91
92
93
94
95
96
97
98
99 auxgpio = cx_read(MO_GP1_IO);
100
101 gpio = (gpio & 0x7fd) + (auxgpio & 0xef);
102 break;
103 case CX88_BOARD_WINFAST_DTV1000:
104 case CX88_BOARD_WINFAST_DTV1800H:
105 case CX88_BOARD_WINFAST_DTV1800H_XC4000:
106 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
107 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
108 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36:
109 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43:
110 gpio = (gpio & 0x6ff) | ((cx_read(MO_GP1_IO) << 8) & 0x900);
111 auxgpio = gpio;
112 break;
113 default:
114 auxgpio = gpio;
115 }
116 if (ir->polling) {
117 if (ir->last_gpio == auxgpio)
118 return;
119 ir->last_gpio = auxgpio;
120 }
121
122
123 data = ir_extract_bits(gpio, ir->mask_keycode);
124 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
125 gpio, data,
126 ir->polling ? "poll" : "irq",
127 (gpio & ir->mask_keydown) ? " down" : "",
128 (gpio & ir->mask_keyup) ? " up" : "");
129
130 if (ir->core->boardnr == CX88_BOARD_NORWOOD_MICRO) {
131 u32 gpio_key = cx_read(MO_GP0_IO);
132
133 data = (data << 4) | ((gpio_key & 0xf0) >> 4);
134
135 rc_keydown(ir->dev, RC_PROTO_UNKNOWN, data, 0);
136
137 } else if (ir->core->boardnr == CX88_BOARD_PROLINK_PLAYTVPVR ||
138 ir->core->boardnr == CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO) {
139
140 u16 addr;
141 u8 cmd;
142 u32 scancode;
143
144 addr = (data >> 8) & 0xffff;
145 cmd = (data >> 0) & 0x00ff;
146 scancode = RC_SCANCODE_NECX(addr, cmd);
147
148 if (0 == (gpio & ir->mask_keyup))
149 rc_keydown_notimeout(ir->dev, RC_PROTO_NECX, scancode,
150 0);
151 else
152 rc_keyup(ir->dev);
153
154 } else if (ir->mask_keydown) {
155
156 if (gpio & ir->mask_keydown)
157 rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
158 0);
159 else
160 rc_keyup(ir->dev);
161
162 } else if (ir->mask_keyup) {
163
164 if (0 == (gpio & ir->mask_keyup))
165 rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
166 0);
167 else
168 rc_keyup(ir->dev);
169
170 } else {
171
172 rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data, 0);
173 rc_keyup(ir->dev);
174 }
175}
176
177static enum hrtimer_restart cx88_ir_work(struct hrtimer *timer)
178{
179 unsigned long missed;
180 struct cx88_IR *ir = container_of(timer, struct cx88_IR, timer);
181
182 cx88_ir_handle_key(ir);
183 missed = hrtimer_forward_now(&ir->timer, ir->polling * 1000000);
184 if (missed > 1)
185 ir_dprintk("Missed ticks %ld\n", missed - 1);
186
187 return HRTIMER_RESTART;
188}
189
190static int __cx88_ir_start(void *priv)
191{
192 struct cx88_core *core = priv;
193 struct cx88_IR *ir;
194
195 if (!core || !core->ir)
196 return -EINVAL;
197
198 ir = core->ir;
199
200 if (ir->polling) {
201 hrtimer_init(&ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
202 ir->timer.function = cx88_ir_work;
203 hrtimer_start(&ir->timer, ir->polling * 1000000,
204 HRTIMER_MODE_REL);
205 }
206 if (ir->sampling) {
207 core->pci_irqmask |= PCI_INT_IR_SMPINT;
208 cx_write(MO_DDS_IO, 0x33F286 * ir_samplerate);
209 cx_write(MO_DDSCFG_IO, 0x5);
210 }
211 return 0;
212}
213
214static void __cx88_ir_stop(void *priv)
215{
216 struct cx88_core *core = priv;
217 struct cx88_IR *ir;
218
219 if (!core || !core->ir)
220 return;
221
222 ir = core->ir;
223 if (ir->sampling) {
224 cx_write(MO_DDSCFG_IO, 0x0);
225 core->pci_irqmask &= ~PCI_INT_IR_SMPINT;
226 }
227
228 if (ir->polling)
229 hrtimer_cancel(&ir->timer);
230}
231
232int cx88_ir_start(struct cx88_core *core)
233{
234 if (core->ir->users)
235 return __cx88_ir_start(core);
236
237 return 0;
238}
239EXPORT_SYMBOL(cx88_ir_start);
240
241void cx88_ir_stop(struct cx88_core *core)
242{
243 if (core->ir->users)
244 __cx88_ir_stop(core);
245}
246EXPORT_SYMBOL(cx88_ir_stop);
247
248static int cx88_ir_open(struct rc_dev *rc)
249{
250 struct cx88_core *core = rc->priv;
251
252 core->ir->users++;
253 return __cx88_ir_start(core);
254}
255
256static void cx88_ir_close(struct rc_dev *rc)
257{
258 struct cx88_core *core = rc->priv;
259
260 core->ir->users--;
261 if (!core->ir->users)
262 __cx88_ir_stop(core);
263}
264
265
266
267int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
268{
269 struct cx88_IR *ir;
270 struct rc_dev *dev;
271 char *ir_codes = NULL;
272 u64 rc_proto = RC_PROTO_BIT_OTHER;
273 int err = -ENOMEM;
274 u32 hardware_mask = 0;
275
276
277
278 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
279 dev = rc_allocate_device(RC_DRIVER_IR_RAW);
280 if (!ir || !dev)
281 goto err_out_free;
282
283 ir->dev = dev;
284
285
286 switch (core->boardnr) {
287 case CX88_BOARD_DNTV_LIVE_DVB_T:
288 case CX88_BOARD_KWORLD_DVB_T:
289 case CX88_BOARD_KWORLD_DVB_T_CX22702:
290 ir_codes = RC_MAP_DNTV_LIVE_DVB_T;
291 ir->gpio_addr = MO_GP1_IO;
292 ir->mask_keycode = 0x1f;
293 ir->mask_keyup = 0x60;
294 ir->polling = 50;
295 break;
296 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
297 ir_codes = RC_MAP_CINERGY_1400;
298 ir->sampling = 0xeb04;
299 break;
300 case CX88_BOARD_HAUPPAUGE:
301 case CX88_BOARD_HAUPPAUGE_DVB_T1:
302 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
303 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
304 case CX88_BOARD_HAUPPAUGE_HVR1100:
305 case CX88_BOARD_HAUPPAUGE_HVR3000:
306 case CX88_BOARD_HAUPPAUGE_HVR4000:
307 case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
308 case CX88_BOARD_PCHDTV_HD3000:
309 case CX88_BOARD_PCHDTV_HD5500:
310 case CX88_BOARD_HAUPPAUGE_IRONLY:
311 ir_codes = RC_MAP_HAUPPAUGE;
312 ir->sampling = 1;
313 break;
314 case CX88_BOARD_WINFAST_DTV2000H:
315 case CX88_BOARD_WINFAST_DTV2000H_J:
316 case CX88_BOARD_WINFAST_DTV1800H:
317 case CX88_BOARD_WINFAST_DTV1800H_XC4000:
318 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
319 ir_codes = RC_MAP_WINFAST;
320 ir->gpio_addr = MO_GP0_IO;
321 ir->mask_keycode = 0x8f8;
322 ir->mask_keyup = 0x100;
323 ir->polling = 50;
324 break;
325 case CX88_BOARD_WINFAST2000XP_EXPERT:
326 case CX88_BOARD_WINFAST_DTV1000:
327 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
328 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36:
329 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43:
330 ir_codes = RC_MAP_WINFAST;
331 ir->gpio_addr = MO_GP0_IO;
332 ir->mask_keycode = 0x8f8;
333 ir->mask_keyup = 0x100;
334 ir->polling = 1;
335 break;
336 case CX88_BOARD_IODATA_GVBCTV7E:
337 ir_codes = RC_MAP_IODATA_BCTV7E;
338 ir->gpio_addr = MO_GP0_IO;
339 ir->mask_keycode = 0xfd;
340 ir->mask_keydown = 0x02;
341 ir->polling = 5;
342 break;
343 case CX88_BOARD_PROLINK_PLAYTVPVR:
344 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
345
346
347
348
349
350
351
352 ir_codes = RC_MAP_PIXELVIEW_MK12;
353 rc_proto = RC_PROTO_BIT_NECX;
354 ir->gpio_addr = MO_GP1_IO;
355 ir->mask_keyup = 0x80;
356 ir->polling = 10;
357 hardware_mask = 0x3f;
358 break;
359 case CX88_BOARD_PROLINK_PV_8000GT:
360 case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME:
361 ir_codes = RC_MAP_PIXELVIEW_NEW;
362 ir->gpio_addr = MO_GP1_IO;
363 ir->mask_keycode = 0x3f;
364 ir->mask_keyup = 0x80;
365 ir->polling = 1;
366 break;
367 case CX88_BOARD_KWORLD_LTV883:
368 ir_codes = RC_MAP_PIXELVIEW;
369 ir->gpio_addr = MO_GP1_IO;
370 ir->mask_keycode = 0x1f;
371 ir->mask_keyup = 0x60;
372 ir->polling = 1;
373 break;
374 case CX88_BOARD_ADSTECH_DVB_T_PCI:
375 ir_codes = RC_MAP_ADSTECH_DVB_T_PCI;
376 ir->gpio_addr = MO_GP1_IO;
377 ir->mask_keycode = 0xbf;
378 ir->mask_keyup = 0x40;
379 ir->polling = 50;
380 break;
381 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
382 ir_codes = RC_MAP_MSI_TVANYWHERE;
383 ir->gpio_addr = MO_GP1_IO;
384 ir->mask_keycode = 0x1f;
385 ir->mask_keyup = 0x40;
386 ir->polling = 1;
387 break;
388 case CX88_BOARD_AVERTV_303:
389 case CX88_BOARD_AVERTV_STUDIO_303:
390 ir_codes = RC_MAP_AVERTV_303;
391 ir->gpio_addr = MO_GP2_IO;
392 ir->mask_keycode = 0xfb;
393 ir->mask_keydown = 0x02;
394 ir->polling = 50;
395 break;
396 case CX88_BOARD_OMICOM_SS4_PCI:
397 case CX88_BOARD_SATTRADE_ST4200:
398 case CX88_BOARD_TBS_8920:
399 case CX88_BOARD_TBS_8910:
400 case CX88_BOARD_PROF_7300:
401 case CX88_BOARD_PROF_7301:
402 case CX88_BOARD_PROF_6200:
403 ir_codes = RC_MAP_TBS_NEC;
404 ir->sampling = 0xff00;
405 break;
406 case CX88_BOARD_TEVII_S464:
407 case CX88_BOARD_TEVII_S460:
408 case CX88_BOARD_TEVII_S420:
409 ir_codes = RC_MAP_TEVII_NEC;
410 ir->sampling = 0xff00;
411 break;
412 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
413 ir_codes = RC_MAP_DNTV_LIVE_DVBT_PRO;
414 ir->sampling = 0xff00;
415 break;
416 case CX88_BOARD_NORWOOD_MICRO:
417 ir_codes = RC_MAP_NORWOOD;
418 ir->gpio_addr = MO_GP1_IO;
419 ir->mask_keycode = 0x0e;
420 ir->mask_keyup = 0x80;
421 ir->polling = 50;
422 break;
423 case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
424 ir_codes = RC_MAP_NPGTECH;
425 ir->gpio_addr = MO_GP0_IO;
426 ir->mask_keycode = 0xfa;
427 ir->polling = 50;
428 break;
429 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
430 ir_codes = RC_MAP_PINNACLE_PCTV_HD;
431 ir->sampling = 1;
432 break;
433 case CX88_BOARD_POWERCOLOR_REAL_ANGEL:
434 ir_codes = RC_MAP_POWERCOLOR_REAL_ANGEL;
435 ir->gpio_addr = MO_GP2_IO;
436 ir->mask_keycode = 0x7e;
437 ir->polling = 100;
438 break;
439 case CX88_BOARD_TWINHAN_VP1027_DVBS:
440 ir_codes = RC_MAP_TWINHAN_VP1027_DVBS;
441 ir->sampling = 0xff00;
442 break;
443 }
444
445 if (!ir_codes) {
446 err = -ENODEV;
447 goto err_out_free;
448 }
449
450
451
452
453
454
455
456
457
458
459
460
461
462 if (hardware_mask && !ir->mask_keycode)
463 ir->mask_keycode = hardware_mask;
464
465
466 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)", core->board.name);
467 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
468
469 dev->device_name = ir->name;
470 dev->input_phys = ir->phys;
471 dev->input_id.bustype = BUS_PCI;
472 dev->input_id.version = 1;
473 if (pci->subsystem_vendor) {
474 dev->input_id.vendor = pci->subsystem_vendor;
475 dev->input_id.product = pci->subsystem_device;
476 } else {
477 dev->input_id.vendor = pci->vendor;
478 dev->input_id.product = pci->device;
479 }
480 dev->dev.parent = &pci->dev;
481 dev->map_name = ir_codes;
482 dev->driver_name = MODULE_NAME;
483 dev->priv = core;
484 dev->open = cx88_ir_open;
485 dev->close = cx88_ir_close;
486 dev->scancode_mask = hardware_mask;
487
488 if (ir->sampling) {
489 dev->timeout = 10 * 1000 * 1000;
490 } else {
491 dev->driver_type = RC_DRIVER_SCANCODE;
492 dev->allowed_protocols = rc_proto;
493 }
494
495 ir->core = core;
496 core->ir = ir;
497
498
499 err = rc_register_device(dev);
500 if (err)
501 goto err_out_free;
502
503 return 0;
504
505err_out_free:
506 rc_free_device(dev);
507 core->ir = NULL;
508 kfree(ir);
509 return err;
510}
511
512int cx88_ir_fini(struct cx88_core *core)
513{
514 struct cx88_IR *ir = core->ir;
515
516
517 if (!ir)
518 return 0;
519
520 cx88_ir_stop(core);
521 rc_unregister_device(ir->dev);
522 kfree(ir);
523
524
525 core->ir = NULL;
526 return 0;
527}
528
529
530
531void cx88_ir_irq(struct cx88_core *core)
532{
533 struct cx88_IR *ir = core->ir;
534 u32 samples;
535 unsigned int todo, bits;
536 struct ir_raw_event ev;
537
538 if (!ir || !ir->sampling)
539 return;
540
541
542
543
544
545
546 samples = cx_read(MO_SAMPLE_IO);
547
548 if (samples == 0xff && ir->dev->idle)
549 return;
550
551 init_ir_raw_event(&ev);
552 for (todo = 32; todo > 0; todo -= bits) {
553 ev.pulse = samples & 0x80000000 ? false : true;
554 bits = min(todo, 32U - fls(ev.pulse ? samples : ~samples));
555 ev.duration = (bits * (NSEC_PER_SEC / 1000)) / ir_samplerate;
556 ir_raw_event_store_with_filter(ir->dev, &ev);
557 samples <<= bits;
558 }
559 ir_raw_event_handle(ir->dev);
560}
561
562static int get_key_pvr2000(struct IR_i2c *ir, enum rc_proto *protocol,
563 u32 *scancode, u8 *toggle)
564{
565 int flags, code;
566
567
568 flags = i2c_smbus_read_byte_data(ir->c, 0x10);
569 if (flags < 0) {
570 dprintk("read error\n");
571 return 0;
572 }
573
574 if (0 == (flags & 0x80))
575 return 0;
576
577
578 code = i2c_smbus_read_byte_data(ir->c, 0x00);
579 if (code < 0) {
580 dprintk("read error\n");
581 return 0;
582 }
583
584 dprintk("IR Key/Flags: (0x%02x/0x%02x)\n",
585 code & 0xff, flags & 0xff);
586
587 *protocol = RC_PROTO_UNKNOWN;
588 *scancode = code & 0xff;
589 *toggle = 0;
590 return 1;
591}
592
593void cx88_i2c_init_ir(struct cx88_core *core)
594{
595 struct i2c_board_info info;
596 static const unsigned short default_addr_list[] = {
597 0x18, 0x6b, 0x71,
598 I2C_CLIENT_END
599 };
600 static const unsigned short pvr2000_addr_list[] = {
601 0x18, 0x1a,
602 I2C_CLIENT_END
603 };
604 const unsigned short *addr_list = default_addr_list;
605 const unsigned short *addrp;
606
607 if (core->i2c_rc != 0)
608 return;
609
610 memset(&info, 0, sizeof(struct i2c_board_info));
611 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
612
613 switch (core->boardnr) {
614 case CX88_BOARD_LEADTEK_PVR2000:
615 addr_list = pvr2000_addr_list;
616 core->init_data.name = "cx88 Leadtek PVR 2000 remote";
617 core->init_data.type = RC_PROTO_BIT_UNKNOWN;
618 core->init_data.get_key = get_key_pvr2000;
619 core->init_data.ir_codes = RC_MAP_EMPTY;
620 break;
621 }
622
623
624
625
626
627
628
629
630 for (addrp = addr_list; *addrp != I2C_CLIENT_END; addrp++) {
631 info.platform_data = NULL;
632 memset(&core->init_data, 0, sizeof(core->init_data));
633
634 if (*addrp == 0x71) {
635
636 core->init_data.name = "cx88 Hauppauge XVR remote";
637 core->init_data.ir_codes = RC_MAP_HAUPPAUGE;
638 core->init_data.type = RC_PROTO_BIT_RC5 |
639 RC_PROTO_BIT_RC6_MCE | RC_PROTO_BIT_RC6_6A_32;
640 core->init_data.internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
641
642 info.platform_data = &core->init_data;
643 }
644 if (i2c_smbus_xfer(&core->i2c_adap, *addrp, 0,
645 I2C_SMBUS_READ, 0,
646 I2C_SMBUS_QUICK, NULL) >= 0) {
647 info.addr = *addrp;
648 i2c_new_device(&core->i2c_adap, &info);
649 break;
650 }
651 }
652}
653
654
655
656MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
657MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
658MODULE_LICENSE("GPL");
659