2 * budget-ci.c: driver for the SAA7146 based Budget DVB cards
4 * Compiled from various sources by Michael Hunold <michael@mihu.de>
6 * msp430 IR support contributed by Jack Thomasson <jkt@Helius.COM>
7 * partially based on the Siemens DVB driver by Ralph+Marcus Metzler
9 * CI interface support (c) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
29 * the project's page is at http://www.linuxtv.org/dvb/
34 #include <linux/module.h>
35 #include <linux/errno.h>
36 #include <linux/slab.h>
37 #include <linux/interrupt.h>
38 #include <linux/input.h>
39 #include <linux/spinlock.h>
41 #include "dvb_functions.h"
42 #include "dvb_ca_en50221.h"
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
45 #include "input_fake.h"
48 #define DEBIADDR_IR 0x1234
49 #define DEBIADDR_CICONTROL 0x0000
50 #define DEBIADDR_CIVERSION 0x4000
51 #define DEBIADDR_IO 0x1000
52 #define DEBIADDR_ATTR 0x3000
54 #define CICONTROL_RESET 0x01
55 #define CICONTROL_ENABLETS 0x02
56 #define CICONTROL_CAMDETECT 0x08
58 #define DEBICICTL 0x00420000
59 #define DEBICICAM 0x02420000
61 #define SLOTSTATUS_NONE 1
62 #define SLOTSTATUS_PRESENT 2
63 #define SLOTSTATUS_RESET 4
64 #define SLOTSTATUS_READY 8
65 #define SLOTSTATUS_OCCUPIED (SLOTSTATUS_PRESENT|SLOTSTATUS_RESET|SLOTSTATUS_READY)
69 struct input_dev input_dev;
70 struct tasklet_struct msp430_irq_tasklet;
71 struct tasklet_struct ciintf_irq_tasklet;
74 struct dvb_ca_en50221 ca;
78 static u32 budget_debiread (struct budget_ci* budget_ci, u32 config, int addr, int count)
80 struct saa7146_dev *saa = budget_ci->budget.dev;
83 if (count > 4 || count <= 0)
86 spin_lock(&budget_ci->debilock);
88 if (saa7146_wait_for_debi_done(saa) < 0) {
89 spin_unlock(&budget_ci->debilock);
93 saa7146_write (saa, DEBI_COMMAND,
94 (count << 17) | 0x10000 | (addr & 0xffff));
95 saa7146_write(saa, DEBI_CONFIG, config);
96 saa7146_write(saa, DEBI_PAGE, 0);
97 saa7146_write(saa, MC2, (2 << 16) | 2);
99 saa7146_wait_for_debi_done(saa);
101 result = saa7146_read(saa, 0x88);
102 result &= (0xffffffffUL >> ((4 - count) * 8));
104 spin_unlock(&budget_ci->debilock);
108 static u8 budget_debiwrite (struct budget_ci* budget_ci, u32 config, int addr, int count, u32 value)
110 struct saa7146_dev *saa = budget_ci->budget.dev;
112 if (count > 4 || count <= 0)
115 spin_lock(&budget_ci->debilock);
117 if (saa7146_wait_for_debi_done(saa) < 0) {
118 spin_unlock(&budget_ci->debilock);
122 saa7146_write (saa, DEBI_COMMAND,
123 (count << 17) | 0x00000 | (addr & 0xffff));
124 saa7146_write(saa, DEBI_CONFIG, config);
125 saa7146_write(saa, DEBI_PAGE, 0);
126 saa7146_write(saa, DEBI_AD, value);
127 saa7146_write(saa, MC2, (2 << 16) | 2);
129 saa7146_wait_for_debi_done(saa);
131 spin_unlock(&budget_ci->debilock);
136 /* from reading the following remotes:
137 Zenith Universal 7 / TV Mode 807 / VCR Mode 837
138 Hauppauge (from NOVA-CI-s box product)
139 i've taken a "middle of the road" approach and note the differences
141 static u16 key_map[64] = {
143 KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8,
147 KEY_POWER, /* RADIO on Hauppauge */
150 KEY_A, /* TV on Hauppauge */
152 KEY_VOLUMEUP, KEY_VOLUMEDOWN,
157 KEY_OPTION, /* RESERVED on Hauppauge */
160 KEY_CHANNELUP, KEY_CHANNELDOWN,
161 KEY_PREVIOUS, /* Prev. Ch on Zenith, SOURCE on Hauppauge */
162 0, KEY_RESTART, KEY_OK,
163 KEY_CYCLEWINDOWS, /* MINIMIZE on Hauppauge */
165 KEY_ENTER, /* VCR mode on Zenith */
170 KEY_MENU, /* FULL SCREEN on Hauppauge */
174 KEY_PREVIOUS, /* VCR mode on Zenith */
180 KEY_TUNER, /* TV/VCR on Zenith */
186 KEY_TUNER, /* VCR mode on Zenith */
191 static void msp430_ir_debounce (unsigned long data)
193 struct input_dev *dev = (struct input_dev *) data;
195 if (dev->rep[0] == 0 || dev->rep[0] == ~0) {
196 input_event(dev, EV_KEY, key_map[dev->repeat_key], !!0);
201 dev->timer.expires = jiffies + HZ * 350 / 1000;
202 add_timer(&dev->timer);
203 input_event(dev, EV_KEY, key_map[dev->repeat_key], 2); /* REPEAT */
208 static void msp430_ir_interrupt (unsigned long data)
210 struct budget_ci *budget_ci = (struct budget_ci*) data;
211 struct input_dev *dev = &budget_ci->input_dev;
212 unsigned int code = budget_debiread(budget_ci, DEBINOSWAP, DEBIADDR_IR, 2) >> 8;
217 if (timer_pending(&dev->timer)) {
218 if (code == dev->repeat_key) {
222 del_timer(&dev->timer);
223 input_event(dev, EV_KEY, key_map[dev->repeat_key], !!0);
226 if (!key_map[code]) {
227 printk ("DVB (%s): no key for %02x!\n",
232 /* initialize debounce and repeat */
233 dev->repeat_key = code;
234 /* Zenith remote _always_ sends 2 sequences */
236 /* 350 milliseconds */
237 dev->timer.expires = jiffies + HZ * 350 / 1000;
239 input_event(dev, EV_KEY, key_map[code], !0);
240 add_timer(&dev->timer);
245 static int msp430_ir_init (struct budget_ci *budget_ci)
247 struct saa7146_dev *saa = budget_ci->budget.dev;
250 memset(&budget_ci->input_dev, 0, sizeof(struct input_dev));
252 sprintf (budget_ci->ir_dev_name, "Budget-CI dvb ir receiver %s", saa->name);
253 budget_ci->input_dev.name = budget_ci->ir_dev_name;
255 set_bit(EV_KEY, budget_ci->input_dev.evbit);
257 for (i=0; i<sizeof(key_map)/sizeof(*key_map); i++)
259 set_bit(key_map[i], budget_ci->input_dev.keybit);
261 input_register_device(&budget_ci->input_dev);
263 budget_ci->input_dev.timer.function = msp430_ir_debounce;
265 saa7146_write(saa, IER, saa7146_read(saa, IER) | MASK_06);
267 saa7146_setgpio(saa, 3, SAA7146_GPIO_IRQHI);
273 static void msp430_ir_deinit (struct budget_ci *budget_ci)
275 struct saa7146_dev *saa = budget_ci->budget.dev;
276 struct input_dev *dev = &budget_ci->input_dev;
278 saa7146_write(saa, IER, saa7146_read(saa, IER) & ~MASK_06);
279 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
281 if (del_timer(&dev->timer))
282 input_event(dev, EV_KEY, key_map[dev->repeat_key], !!0);
284 input_unregister_device(dev);
287 static int ciintf_read_attribute_mem(struct dvb_ca_en50221* ca, int slot, int address) {
288 struct budget_ci* budget_ci = (struct budget_ci*) ca->data;
290 if (slot != 0) return -EINVAL;
292 return budget_debiread(budget_ci, DEBICICAM, DEBIADDR_ATTR | (address & 0xfff), 1);
295 static int ciintf_write_attribute_mem(struct dvb_ca_en50221* ca, int slot, int address, u8 value) {
296 struct budget_ci* budget_ci = (struct budget_ci*) ca->data;
298 if (slot != 0) return -EINVAL;
300 return budget_debiwrite(budget_ci, DEBICICAM, DEBIADDR_ATTR | (address & 0xfff), 1, value);
303 static int ciintf_read_cam_control(struct dvb_ca_en50221* ca, int slot, u8 address) {
304 struct budget_ci* budget_ci = (struct budget_ci*) ca->data;
306 if (slot != 0) return -EINVAL;
308 return budget_debiread(budget_ci, DEBICICAM, DEBIADDR_IO | (address & 3), 1);
311 static int ciintf_write_cam_control(struct dvb_ca_en50221* ca, int slot, u8 address, u8 value) {
312 struct budget_ci* budget_ci = (struct budget_ci*) ca->data;
314 if (slot != 0) return -EINVAL;
316 return budget_debiwrite(budget_ci, DEBICICAM, DEBIADDR_IO | (address & 3), 1, value);
319 static int ciintf_slot_reset(struct dvb_ca_en50221* ca, int slot) {
320 struct budget_ci* budget_ci = (struct budget_ci*) ca->data;
321 struct saa7146_dev *saa = budget_ci->budget.dev;
323 if (slot != 0) return -EINVAL;
325 // trigger on RISING edge during reset so we know when READY is re-asserted
326 saa7146_setgpio(saa, 0, SAA7146_GPIO_IRQHI);
327 budget_ci->slot_status = SLOTSTATUS_RESET;
328 budget_debiwrite(budget_ci, DEBICICTL, DEBIADDR_CICONTROL, 1, 0);
330 budget_debiwrite(budget_ci, DEBICICTL, DEBIADDR_CICONTROL, 1, CICONTROL_RESET);
332 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTHI);
333 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
337 static int ciintf_slot_shutdown(struct dvb_ca_en50221* ca, int slot) {
338 struct budget_ci* budget_ci = (struct budget_ci*) ca->data;
339 struct saa7146_dev *saa = budget_ci->budget.dev;
341 if (slot != 0) return -EINVAL;
343 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTHI);
344 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
348 static int ciintf_slot_ts_enable(struct dvb_ca_en50221* ca, int slot) {
349 struct budget_ci* budget_ci = (struct budget_ci*) ca->data;
350 struct saa7146_dev *saa = budget_ci->budget.dev;
353 if (slot != 0) return -EINVAL;
356 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
358 tmp = budget_debiread(budget_ci, DEBICICTL, DEBIADDR_CICONTROL, 1);
359 budget_debiwrite(budget_ci, DEBICICTL, DEBIADDR_CICONTROL, 1, tmp | CICONTROL_ENABLETS);
361 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
366 static void ciintf_interrupt (unsigned long data)
368 struct budget_ci *budget_ci = (struct budget_ci*) data;
369 struct saa7146_dev *saa = budget_ci->budget.dev;
372 // ensure we don't get spurious IRQs during initialisation
373 if (!budget_ci->budget.ci_present) return;
375 flags = budget_debiread(budget_ci, DEBICICTL, DEBIADDR_CICONTROL, 1);
377 // always set the GPIO mode back to "normal", in case the card is
378 // yanked at an inopportune moment
379 saa7146_setgpio(saa, 0, SAA7146_GPIO_IRQLO);
381 if (flags & CICONTROL_CAMDETECT) {
383 if (budget_ci->slot_status & SLOTSTATUS_NONE) {
385 budget_ci->slot_status = SLOTSTATUS_PRESENT;
386 dvb_ca_en50221_camchange_irq(&budget_ci->ca, 0, DVB_CA_EN50221_CAMCHANGE_INSERTED);
388 } else if (budget_ci->slot_status & SLOTSTATUS_RESET) {
389 // CAM ready (reset completed)
390 budget_ci->slot_status = SLOTSTATUS_READY;
391 dvb_ca_en50221_camready_irq(&budget_ci->ca, 0);
393 } else if (budget_ci->slot_status & SLOTSTATUS_READY) {
395 dvb_ca_en50221_frda_irq(&budget_ci->ca, 0);
398 if (budget_ci->slot_status & SLOTSTATUS_OCCUPIED) {
399 budget_ci->slot_status = SLOTSTATUS_NONE;
400 dvb_ca_en50221_camchange_irq(&budget_ci->ca, 0, DVB_CA_EN50221_CAMCHANGE_REMOVED);
405 static int ciintf_init(struct budget_ci* budget_ci)
407 struct saa7146_dev *saa = budget_ci->budget.dev;
411 memset(&budget_ci->ca, 0, sizeof(struct dvb_ca_en50221));
414 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16) | 0x800);
416 // test if it is there
417 if ((budget_debiread(budget_ci, DEBICICTL, DEBIADDR_CIVERSION, 1) & 0xa0) != 0xa0) {
422 // determine whether a CAM is present or not
423 flags = budget_debiread(budget_ci, DEBICICTL, DEBIADDR_CICONTROL, 1);
424 budget_ci->slot_status = SLOTSTATUS_NONE;
425 if (flags & CICONTROL_CAMDETECT) budget_ci->slot_status = SLOTSTATUS_PRESENT;
428 // register CI interface
429 budget_ci->ca.read_attribute_mem = ciintf_read_attribute_mem;
430 budget_ci->ca.write_attribute_mem = ciintf_write_attribute_mem;
431 budget_ci->ca.read_cam_control = ciintf_read_cam_control;
432 budget_ci->ca.write_cam_control = ciintf_write_cam_control;
433 budget_ci->ca.slot_reset = ciintf_slot_reset;
434 budget_ci->ca.slot_shutdown = ciintf_slot_shutdown;
435 budget_ci->ca.slot_ts_enable = ciintf_slot_ts_enable;
436 budget_ci->ca.data = budget_ci;
437 if ((result = dvb_ca_en50221_init(budget_ci->budget.dvb_adapter,
439 DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE |
440 DVB_CA_EN50221_FLAG_IRQ_FR |
441 DVB_CA_EN50221_FLAG_IRQ_DA,
443 printk("budget_ci: CI interface detected, but initialisation failed.\n");
448 tasklet_init (&budget_ci->ciintf_irq_tasklet, ciintf_interrupt, (unsigned long) budget_ci);
449 saa7146_setgpio(saa, 0, SAA7146_GPIO_IRQLO);
450 saa7146_write(saa, IER, saa7146_read(saa, IER) | MASK_03);
451 budget_debiwrite(budget_ci, DEBICICTL, DEBIADDR_CICONTROL, 1, CICONTROL_RESET);
454 printk("budget_ci: CI interface initialised\n");
455 budget_ci->budget.ci_present = 1;
457 // forge a fake CI IRQ so the CAM state is setup correctly
458 flags = DVB_CA_EN50221_CAMCHANGE_REMOVED;
459 if (budget_ci->slot_status != SLOTSTATUS_NONE) flags = DVB_CA_EN50221_CAMCHANGE_INSERTED;
460 dvb_ca_en50221_camchange_irq(&budget_ci->ca, 0, flags);
465 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
469 static void ciintf_deinit(struct budget_ci* budget_ci)
471 struct saa7146_dev *saa = budget_ci->budget.dev;
473 // disable CI interrupts
474 saa7146_write(saa, IER, saa7146_read(saa, IER) & ~MASK_03);
475 saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
476 tasklet_kill(&budget_ci->ciintf_irq_tasklet);
477 budget_debiwrite(budget_ci, DEBICICTL, DEBIADDR_CICONTROL, 1, 0);
479 budget_debiwrite(budget_ci, DEBICICTL, DEBIADDR_CICONTROL, 1, CICONTROL_RESET);
481 // disable TS data stream to CI interface
482 saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
484 // release the CA device
485 dvb_ca_en50221_release(&budget_ci->ca);
488 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
491 static void budget_ci_irq (struct saa7146_dev *dev, u32 *isr)
493 struct budget_ci *budget_ci = (struct budget_ci*) dev->ext_priv;
495 DEB_EE(("dev: %p, budget_ci: %p\n", dev, budget_ci));
498 tasklet_schedule (&budget_ci->msp430_irq_tasklet);
501 ttpci_budget_irq10_handler (dev, isr);
503 if ((*isr & MASK_03) && (budget_ci->budget.ci_present))
504 tasklet_schedule (&budget_ci->ciintf_irq_tasklet);
509 static int budget_ci_attach (struct saa7146_dev* dev,
510 struct saa7146_pci_extension_data *info)
512 struct budget_ci *budget_ci;
515 if (!(budget_ci = kmalloc (sizeof(struct budget_ci), GFP_KERNEL)))
518 DEB_EE(("budget_ci: %p\n", budget_ci));
520 spin_lock_init(&budget_ci->debilock);
521 budget_ci->budget.ci_present = 0;
523 if ((err = ttpci_budget_init (&budget_ci->budget, dev, info))) {
528 dev->ext_priv = budget_ci;
530 tasklet_init (&budget_ci->msp430_irq_tasklet, msp430_ir_interrupt,
531 (unsigned long) budget_ci);
533 msp430_ir_init (budget_ci);
535 // UNCOMMENT TO TEST CI INTERFACE
536 // ciintf_init(budget_ci);
543 static int budget_ci_detach (struct saa7146_dev* dev)
545 struct budget_ci *budget_ci = (struct budget_ci*) dev->ext_priv;
546 struct saa7146_dev *saa = budget_ci->budget.dev;
549 if (budget_ci->budget.ci_present) ciintf_deinit(budget_ci);
551 err = ttpci_budget_deinit (&budget_ci->budget);
553 tasklet_kill (&budget_ci->msp430_irq_tasklet);
555 msp430_ir_deinit (budget_ci);
557 // disable frontend and CI interface
558 saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
567 static struct saa7146_extension budget_extension;
569 MAKE_BUDGET_INFO(ttbci, "TT-Budget/WinTV-NOVA-CI PCI", BUDGET_TT_HW_DISEQC);
570 MAKE_BUDGET_INFO(ttbt2, "TT-Budget/WinTV-NOVA-T PCI", BUDGET_TT);
572 static struct pci_device_id pci_tbl[] = {
573 MAKE_EXTENSION_PCI(ttbci, 0x13c2, 0x100c),
574 MAKE_EXTENSION_PCI(ttbci, 0x13c2, 0x100f),
575 MAKE_EXTENSION_PCI(ttbt2, 0x13c2, 0x1011),
581 MODULE_DEVICE_TABLE(pci, pci_tbl);
583 static struct saa7146_extension budget_extension = {
584 .name = "budget_ci dvb\0",
587 .module = THIS_MODULE,
588 .pci_tbl = &pci_tbl[0],
589 .attach = budget_ci_attach,
590 .detach = budget_ci_detach,
592 .irq_mask = MASK_03 | MASK_06 | MASK_10,
593 .irq_func = budget_ci_irq,
597 static int __init budget_ci_init(void)
599 return saa7146_register_extension(&budget_extension);
603 static void __exit budget_ci_exit(void)
606 saa7146_unregister_extension(&budget_extension);
609 module_init(budget_ci_init);
610 module_exit(budget_ci_exit);
612 MODULE_LICENSE("GPL");
613 MODULE_AUTHOR("Michael Hunold, Jack Thomasson, Andrew de Quincey, others");
614 MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
615 "budget PCI DVB cards w/ CI-module produced by "
616 "Siemens, Technotrend, Hauppauge");