aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb/ttpci
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/dvb/ttpci')
-rw-r--r--drivers/media/dvb/ttpci/Kconfig159
-rw-r--r--drivers/media/dvb/ttpci/Makefile21
-rw-r--r--drivers/media/dvb/ttpci/av7110.c2920
-rw-r--r--drivers/media/dvb/ttpci/av7110.h315
-rw-r--r--drivers/media/dvb/ttpci/av7110_av.c1626
-rw-r--r--drivers/media/dvb/ttpci/av7110_av.h30
-rw-r--r--drivers/media/dvb/ttpci/av7110_ca.c387
-rw-r--r--drivers/media/dvb/ttpci/av7110_ca.h14
-rw-r--r--drivers/media/dvb/ttpci/av7110_hw.c1208
-rw-r--r--drivers/media/dvb/ttpci/av7110_hw.h495
-rw-r--r--drivers/media/dvb/ttpci/av7110_ipack.c403
-rw-r--r--drivers/media/dvb/ttpci/av7110_ipack.h12
-rw-r--r--drivers/media/dvb/ttpci/av7110_ir.c415
-rw-r--r--drivers/media/dvb/ttpci/av7110_v4l.c932
-rw-r--r--drivers/media/dvb/ttpci/budget-av.c1635
-rw-r--r--drivers/media/dvb/ttpci/budget-ci.c1589
-rw-r--r--drivers/media/dvb/ttpci/budget-core.c600
-rw-r--r--drivers/media/dvb/ttpci/budget-patch.c673
-rw-r--r--drivers/media/dvb/ttpci/budget.c793
-rw-r--r--drivers/media/dvb/ttpci/budget.h123
-rw-r--r--drivers/media/dvb/ttpci/ttpci-eeprom.c147
-rw-r--r--drivers/media/dvb/ttpci/ttpci-eeprom.h33
22 files changed, 14530 insertions, 0 deletions
diff --git a/drivers/media/dvb/ttpci/Kconfig b/drivers/media/dvb/ttpci/Kconfig
new file mode 100644
index 00000000000..9d83ced69dd
--- /dev/null
+++ b/drivers/media/dvb/ttpci/Kconfig
@@ -0,0 +1,159 @@
1config TTPCI_EEPROM
2 tristate
3 depends on I2C
4 default n
5
6config DVB_AV7110
7 tristate "AV7110 cards"
8 depends on DVB_CORE && PCI && I2C
9 select TTPCI_EEPROM
10 select VIDEO_SAA7146_VV
11 depends on VIDEO_DEV # dependencies of VIDEO_SAA7146_VV
12 select DVB_VES1820 if !DVB_FE_CUSTOMISE
13 select DVB_VES1X93 if !DVB_FE_CUSTOMISE
14 select DVB_STV0299 if !DVB_FE_CUSTOMISE
15 select DVB_TDA8083 if !DVB_FE_CUSTOMISE
16 select DVB_SP8870 if !DVB_FE_CUSTOMISE
17 select DVB_STV0297 if !DVB_FE_CUSTOMISE
18 select DVB_L64781 if !DVB_FE_CUSTOMISE
19 select DVB_LNBP21 if !DVB_FE_CUSTOMISE
20 help
21 Support for SAA7146 and AV7110 based DVB cards as produced
22 by Fujitsu-Siemens, Technotrend, Hauppauge and others.
23
24 This driver only supports the fullfeatured cards with
25 onboard MPEG2 decoder.
26
27 This driver needs an external firmware. Please use the script
28 "<kerneldir>/Documentation/dvb/get_dvb_firmware av7110" to
29 download/extract it, and then copy it to /usr/lib/hotplug/firmware
30 or /lib/firmware (depending on configuration of firmware hotplug).
31
32 Alternatively, you can download the file and use the kernel's
33 EXTRA_FIRMWARE configuration option to build it into your
34 kernel image by adding the filename to the EXTRA_FIRMWARE
35 configuration option string.
36
37 Say Y if you own such a card and want to use it.
38
39config DVB_AV7110_OSD
40 bool "AV7110 OSD support"
41 depends on DVB_AV7110
42 default y if DVB_AV7110=y || DVB_AV7110=m
43 help
44 The AV7110 firmware provides some code to generate an OnScreenDisplay
45 on the video output. This is kind of nonstandard and not guaranteed to
46 be maintained.
47
48 Anyway, some popular DVB software like VDR uses this OSD to render
49 its menus, so say Y if you want to use this software.
50
51 All other people say N.
52
53config DVB_BUDGET_CORE
54 tristate "SAA7146 DVB cards (aka Budget, Nova-PCI)"
55 depends on DVB_CORE && PCI && I2C
56 select VIDEO_SAA7146
57 select TTPCI_EEPROM
58 help
59 Support for simple SAA7146 based DVB cards
60 (so called Budget- or Nova-PCI cards) without onboard
61 MPEG2 decoder.
62
63config DVB_BUDGET
64 tristate "Budget cards"
65 depends on DVB_BUDGET_CORE && I2C
66 select DVB_STV0299 if !DVB_FE_CUSTOMISE
67 select DVB_VES1X93 if !DVB_FE_CUSTOMISE
68 select DVB_VES1820 if !DVB_FE_CUSTOMISE
69 select DVB_L64781 if !DVB_FE_CUSTOMISE
70 select DVB_TDA8083 if !DVB_FE_CUSTOMISE
71 select DVB_S5H1420 if !DVB_FE_CUSTOMISE
72 select DVB_TDA10086 if !DVB_FE_CUSTOMISE
73 select DVB_TDA826X if !DVB_FE_CUSTOMISE
74 select DVB_LNBP21 if !DVB_FE_CUSTOMISE
75 select DVB_TDA1004X if !DVB_FE_CUSTOMISE
76 select DVB_ISL6423 if !DVB_FE_CUSTOMISE
77 select DVB_STV090x if !DVB_FE_CUSTOMISE
78 select DVB_STV6110x if !DVB_FE_CUSTOMISE
79 help
80 Support for simple SAA7146 based DVB cards (so called Budget-
81 or Nova-PCI cards) without onboard MPEG2 decoder, and without
82 analog inputs or an onboard Common Interface connector.
83
84 Say Y if you own such a card and want to use it.
85
86 To compile this driver as a module, choose M here: the
87 module will be called budget.
88
89config DVB_BUDGET_CI
90 tristate "Budget cards with onboard CI connector"
91 depends on DVB_BUDGET_CORE && I2C
92 select DVB_STV0297 if !DVB_FE_CUSTOMISE
93 select DVB_STV0299 if !DVB_FE_CUSTOMISE
94 select DVB_TDA1004X if !DVB_FE_CUSTOMISE
95 select DVB_STB0899 if !DVB_FE_CUSTOMISE
96 select DVB_STB6100 if !DVB_FE_CUSTOMISE
97 select DVB_LNBP21 if !DVB_FE_CUSTOMISE
98 select DVB_STV0288 if !DVB_FE_CUSTOMISE
99 select DVB_STB6000 if !DVB_FE_CUSTOMISE
100 select DVB_TDA10023 if !DVB_FE_CUSTOMISE
101 select MEDIA_TUNER_TDA827X if !MEDIA_TUNER_CUSTOMISE
102 depends on RC_CORE
103 help
104 Support for simple SAA7146 based DVB cards
105 (so called Budget- or Nova-PCI cards) without onboard
106 MPEG2 decoder, but with onboard Common Interface connector.
107
108 Note: The Common Interface is not yet supported by this driver
109 due to lack of information from the vendor.
110
111 Say Y if you own such a card and want to use it.
112
113 To compile this driver as a module, choose M here: the
114 module will be called budget-ci.
115
116config DVB_BUDGET_AV
117 tristate "Budget cards with analog video inputs"
118 depends on DVB_BUDGET_CORE && I2C
119 select VIDEO_SAA7146_VV
120 depends on VIDEO_DEV # dependencies of VIDEO_SAA7146_VV
121 select DVB_PLL if !DVB_FE_CUSTOMISE
122 select DVB_STV0299 if !DVB_FE_CUSTOMISE
123 select DVB_TDA1004X if !DVB_FE_CUSTOMISE
124 select DVB_TDA10021 if !DVB_FE_CUSTOMISE
125 select DVB_TDA10023 if !DVB_FE_CUSTOMISE
126 select DVB_STB0899 if !DVB_FE_CUSTOMISE
127 select DVB_TDA8261 if !DVB_FE_CUSTOMISE
128 select DVB_TUA6100 if !DVB_FE_CUSTOMISE
129 help
130 Support for simple SAA7146 based DVB cards
131 (so called Budget- or Nova-PCI cards) without onboard
132 MPEG2 decoder, but with one or more analog video inputs.
133
134 Say Y if you own such a card and want to use it.
135
136 To compile this driver as a module, choose M here: the
137 module will be called budget-av.
138
139config DVB_BUDGET_PATCH
140 tristate "AV7110 cards with Budget Patch"
141 depends on DVB_BUDGET_CORE && I2C
142 depends on DVB_AV7110
143 select DVB_STV0299 if !DVB_FE_CUSTOMISE
144 select DVB_VES1X93 if !DVB_FE_CUSTOMISE
145 select DVB_TDA8083 if !DVB_FE_CUSTOMISE
146 help
147 Support for Budget Patch (full TS) modification on
148 SAA7146+AV7110 based cards (DVB-S cards). This
149 driver doesn't use onboard MPEG2 decoder. The
150 card is driven in Budget-only mode. Card is
151 required to have loaded firmware to tune properly.
152 Firmware can be loaded by insertion and removal of
153 standard AV7110 driver prior to loading this
154 driver.
155
156 Say Y if you own such a card and want to use it.
157
158 To compile this driver as a module, choose M here: the
159 module will be called budget-patch.
diff --git a/drivers/media/dvb/ttpci/Makefile b/drivers/media/dvb/ttpci/Makefile
new file mode 100644
index 00000000000..8a4d5bb20a5
--- /dev/null
+++ b/drivers/media/dvb/ttpci/Makefile
@@ -0,0 +1,21 @@
1#
2# Makefile for the kernel SAA7146 FULL TS DVB device driver
3# and the AV7110 DVB device driver
4#
5
6dvb-ttpci-objs := av7110_hw.o av7110_v4l.o av7110_av.o av7110_ca.o av7110.o av7110_ipack.o
7
8ifdef CONFIG_INPUT_EVDEV
9dvb-ttpci-objs += av7110_ir.o
10endif
11
12obj-$(CONFIG_TTPCI_EEPROM) += ttpci-eeprom.o
13obj-$(CONFIG_DVB_BUDGET_CORE) += budget-core.o
14obj-$(CONFIG_DVB_BUDGET) += budget.o
15obj-$(CONFIG_DVB_BUDGET_AV) += budget-av.o
16obj-$(CONFIG_DVB_BUDGET_CI) += budget-ci.o
17obj-$(CONFIG_DVB_BUDGET_PATCH) += budget-patch.o
18obj-$(CONFIG_DVB_AV7110) += dvb-ttpci.o
19
20EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core/ -Idrivers/media/dvb/frontends/
21EXTRA_CFLAGS += -Idrivers/media/common/tuners
diff --git a/drivers/media/dvb/ttpci/av7110.c b/drivers/media/dvb/ttpci/av7110.c
new file mode 100644
index 00000000000..3d20719fce1
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110.c
@@ -0,0 +1,2920 @@
1/*
2 * driver for the SAA7146 based AV110 cards (like the Fujitsu-Siemens DVB)
3 * av7110.c: initialization and demux stuff
4 *
5 * Copyright (C) 1999-2002 Ralph Metzler
6 * & Marcus Metzler for convergence integrated media GmbH
7 *
8 * originally based on code by:
9 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
10 *
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.
15 *
16 *
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.
21 *
22 *
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
27 *
28 *
29 * the project's page is at http://www.linuxtv.org/
30 */
31
32
33#include <linux/module.h>
34#include <linux/kmod.h>
35#include <linux/delay.h>
36#include <linux/fs.h>
37#include <linux/timer.h>
38#include <linux/poll.h>
39
40#include <linux/kernel.h>
41#include <linux/sched.h>
42#include <linux/types.h>
43#include <linux/fcntl.h>
44#include <linux/interrupt.h>
45#include <linux/string.h>
46#include <linux/pci.h>
47#include <linux/vmalloc.h>
48#include <linux/firmware.h>
49#include <linux/crc32.h>
50#include <linux/i2c.h>
51#include <linux/kthread.h>
52#include <linux/slab.h>
53#include <asm/unaligned.h>
54#include <asm/byteorder.h>
55
56#include <asm/system.h>
57
58#include <linux/dvb/frontend.h>
59
60#include "dvb_frontend.h"
61
62#include "ttpci-eeprom.h"
63#include "av7110.h"
64#include "av7110_hw.h"
65#include "av7110_av.h"
66#include "av7110_ca.h"
67#include "av7110_ipack.h"
68
69#include "bsbe1.h"
70#include "lnbp21.h"
71#include "bsru6.h"
72
73#define TS_WIDTH 376
74#define TS_HEIGHT 512
75#define TS_BUFLEN (TS_WIDTH*TS_HEIGHT)
76#define TS_MAX_PACKETS (TS_BUFLEN/TS_SIZE)
77
78
79int av7110_debug;
80
81static int vidmode = CVBS_RGB_OUT;
82static int pids_off;
83static int adac = DVB_ADAC_TI;
84static int hw_sections;
85static int rgb_on;
86static int volume = 255;
87static int budgetpatch;
88static int wss_cfg_4_3 = 0x4008;
89static int wss_cfg_16_9 = 0x0007;
90static int tv_standard;
91static int full_ts;
92
93module_param_named(debug, av7110_debug, int, 0644);
94MODULE_PARM_DESC(debug, "debug level (bitmask, default 0)");
95module_param(vidmode, int, 0444);
96MODULE_PARM_DESC(vidmode,"analog video out: 0 off, 1 CVBS+RGB (default), 2 CVBS+YC, 3 YC");
97module_param(pids_off, int, 0444);
98MODULE_PARM_DESC(pids_off,"clear video/audio/PCR PID filters when demux is closed");
99module_param(adac, int, 0444);
100MODULE_PARM_DESC(adac,"audio DAC type: 0 TI, 1 CRYSTAL, 2 MSP (use if autodetection fails)");
101module_param(hw_sections, int, 0444);
102MODULE_PARM_DESC(hw_sections, "0 use software section filter, 1 use hardware");
103module_param(rgb_on, int, 0444);
104MODULE_PARM_DESC(rgb_on, "For Siemens DVB-C cards only: Enable RGB control"
105 " signal on SCART pin 16 to switch SCART video mode from CVBS to RGB");
106module_param(volume, int, 0444);
107MODULE_PARM_DESC(volume, "initial volume: default 255 (range 0-255)");
108module_param(budgetpatch, int, 0444);
109MODULE_PARM_DESC(budgetpatch, "use budget-patch hardware modification: default 0 (0 no, 1 autodetect, 2 always)");
110module_param(full_ts, int, 0444);
111MODULE_PARM_DESC(full_ts, "enable code for full-ts hardware modification: 0 disable (default), 1 enable");
112module_param(wss_cfg_4_3, int, 0444);
113MODULE_PARM_DESC(wss_cfg_4_3, "WSS 4:3 - default 0x4008 - bit 15: disable, 14: burst mode, 13..0: wss data");
114module_param(wss_cfg_16_9, int, 0444);
115MODULE_PARM_DESC(wss_cfg_16_9, "WSS 16:9 - default 0x0007 - bit 15: disable, 14: burst mode, 13..0: wss data");
116module_param(tv_standard, int, 0444);
117MODULE_PARM_DESC(tv_standard, "TV standard: 0 PAL (default), 1 NTSC");
118
119DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
120
121static void restart_feeds(struct av7110 *av7110);
122static int budget_start_feed(struct dvb_demux_feed *feed);
123static int budget_stop_feed(struct dvb_demux_feed *feed);
124
125static int av7110_num;
126
127#define FE_FUNC_OVERRIDE(fe_func, av7110_copy, av7110_func) \
128{\
129 if (fe_func != NULL) { \
130 av7110_copy = fe_func; \
131 fe_func = av7110_func; \
132 } \
133}
134
135
136static void init_av7110_av(struct av7110 *av7110)
137{
138 int ret;
139 struct saa7146_dev *dev = av7110->dev;
140
141 /* set internal volume control to maximum */
142 av7110->adac_type = DVB_ADAC_TI;
143 ret = av7110_set_volume(av7110, av7110->mixer.volume_left, av7110->mixer.volume_right);
144 if (ret < 0)
145 printk("dvb-ttpci:cannot set internal volume to maximum:%d\n",ret);
146
147 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
148 1, (u16) av7110->display_ar);
149 if (ret < 0)
150 printk("dvb-ttpci: unable to set aspect ratio\n");
151 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
152 1, av7110->display_panscan);
153 if (ret < 0)
154 printk("dvb-ttpci: unable to set pan scan\n");
155
156 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 2, wss_cfg_4_3);
157 if (ret < 0)
158 printk("dvb-ttpci: unable to configure 4:3 wss\n");
159 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 3, wss_cfg_16_9);
160 if (ret < 0)
161 printk("dvb-ttpci: unable to configure 16:9 wss\n");
162
163 ret = av7710_set_video_mode(av7110, vidmode);
164 if (ret < 0)
165 printk("dvb-ttpci:cannot set video mode:%d\n",ret);
166
167 /* handle different card types */
168 /* remaining inits according to card and frontend type */
169 av7110->analog_tuner_flags = 0;
170 av7110->current_input = 0;
171 if (dev->pci->subsystem_vendor == 0x13c2 && dev->pci->subsystem_device == 0x000a)
172 av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, 0); // SPDIF on
173 if (i2c_writereg(av7110, 0x20, 0x00, 0x00) == 1) {
174 printk ("dvb-ttpci: Crystal audio DAC @ card %d detected\n",
175 av7110->dvb_adapter.num);
176 av7110->adac_type = DVB_ADAC_CRYSTAL;
177 i2c_writereg(av7110, 0x20, 0x01, 0xd2);
178 i2c_writereg(av7110, 0x20, 0x02, 0x49);
179 i2c_writereg(av7110, 0x20, 0x03, 0x00);
180 i2c_writereg(av7110, 0x20, 0x04, 0x00);
181
182 /**
183 * some special handling for the Siemens DVB-C cards...
184 */
185 } else if (0 == av7110_init_analog_module(av7110)) {
186 /* done. */
187 }
188 else if (dev->pci->subsystem_vendor == 0x110a) {
189 printk("dvb-ttpci: DVB-C w/o analog module @ card %d detected\n",
190 av7110->dvb_adapter.num);
191 av7110->adac_type = DVB_ADAC_NONE;
192 }
193 else {
194 av7110->adac_type = adac;
195 printk("dvb-ttpci: adac type set to %d @ card %d\n",
196 av7110->adac_type, av7110->dvb_adapter.num);
197 }
198
199 if (av7110->adac_type == DVB_ADAC_NONE || av7110->adac_type == DVB_ADAC_MSP34x0) {
200 // switch DVB SCART on
201 ret = av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, MainSwitch, 1, 0);
202 if (ret < 0)
203 printk("dvb-ttpci:cannot switch on SCART(Main):%d\n",ret);
204 ret = av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, 1);
205 if (ret < 0)
206 printk("dvb-ttpci:cannot switch on SCART(AD):%d\n",ret);
207 if (rgb_on &&
208 ((av7110->dev->pci->subsystem_vendor == 0x110a) ||
209 (av7110->dev->pci->subsystem_vendor == 0x13c2)) &&
210 (av7110->dev->pci->subsystem_device == 0x0000)) {
211 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // RGB on, SCART pin 16
212 //saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // SCARTpin 8
213 }
214 }
215
216 if (dev->pci->subsystem_vendor == 0x13c2 && dev->pci->subsystem_device == 0x000e)
217 av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, SpdifSwitch, 1, 0); // SPDIF on
218
219 ret = av7110_set_volume(av7110, av7110->mixer.volume_left, av7110->mixer.volume_right);
220 if (ret < 0)
221 printk("dvb-ttpci:cannot set volume :%d\n",ret);
222}
223
224static void recover_arm(struct av7110 *av7110)
225{
226 dprintk(4, "%p\n",av7110);
227
228 av7110_bootarm(av7110);
229 msleep(100);
230
231 init_av7110_av(av7110);
232
233 /* card-specific recovery */
234 if (av7110->recover)
235 av7110->recover(av7110);
236
237 restart_feeds(av7110);
238
239#if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
240 av7110_check_ir_config(av7110, true);
241#endif
242}
243
244static void av7110_arm_sync(struct av7110 *av7110)
245{
246 if (av7110->arm_thread)
247 kthread_stop(av7110->arm_thread);
248
249 av7110->arm_thread = NULL;
250}
251
252static int arm_thread(void *data)
253{
254 struct av7110 *av7110 = data;
255 u16 newloops = 0;
256 int timeout;
257
258 dprintk(4, "%p\n",av7110);
259
260 for (;;) {
261 timeout = wait_event_interruptible_timeout(av7110->arm_wait,
262 kthread_should_stop(), 5 * HZ);
263
264 if (-ERESTARTSYS == timeout || kthread_should_stop()) {
265 /* got signal or told to quit*/
266 break;
267 }
268
269 if (!av7110->arm_ready)
270 continue;
271
272#if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
273 av7110_check_ir_config(av7110, false);
274#endif
275
276 if (mutex_lock_interruptible(&av7110->dcomlock))
277 break;
278 newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2);
279 mutex_unlock(&av7110->dcomlock);
280
281 if (newloops == av7110->arm_loops || av7110->arm_errors > 3) {
282 printk(KERN_ERR "dvb-ttpci: ARM crashed @ card %d\n",
283 av7110->dvb_adapter.num);
284
285 recover_arm(av7110);
286
287 if (mutex_lock_interruptible(&av7110->dcomlock))
288 break;
289 newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2) - 1;
290 mutex_unlock(&av7110->dcomlock);
291 }
292 av7110->arm_loops = newloops;
293 av7110->arm_errors = 0;
294 }
295
296 return 0;
297}
298
299
300/****************************************************************************
301 * IRQ handling
302 ****************************************************************************/
303
304static int DvbDmxFilterCallback(u8 *buffer1, size_t buffer1_len,
305 u8 *buffer2, size_t buffer2_len,
306 struct dvb_demux_filter *dvbdmxfilter,
307 enum dmx_success success,
308 struct av7110 *av7110)
309{
310 if (!dvbdmxfilter->feed->demux->dmx.frontend)
311 return 0;
312 if (dvbdmxfilter->feed->demux->dmx.frontend->source == DMX_MEMORY_FE)
313 return 0;
314
315 switch (dvbdmxfilter->type) {
316 case DMX_TYPE_SEC:
317 if ((((buffer1[1] << 8) | buffer1[2]) & 0xfff) + 3 != buffer1_len)
318 return 0;
319 if (dvbdmxfilter->doneq) {
320 struct dmx_section_filter *filter = &dvbdmxfilter->filter;
321 int i;
322 u8 xor, neq = 0;
323
324 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
325 xor = filter->filter_value[i] ^ buffer1[i];
326 neq |= dvbdmxfilter->maskandnotmode[i] & xor;
327 }
328 if (!neq)
329 return 0;
330 }
331 return dvbdmxfilter->feed->cb.sec(buffer1, buffer1_len,
332 buffer2, buffer2_len,
333 &dvbdmxfilter->filter,
334 DMX_OK);
335 case DMX_TYPE_TS:
336 if (!(dvbdmxfilter->feed->ts_type & TS_PACKET))
337 return 0;
338 if (dvbdmxfilter->feed->ts_type & TS_PAYLOAD_ONLY)
339 return dvbdmxfilter->feed->cb.ts(buffer1, buffer1_len,
340 buffer2, buffer2_len,
341 &dvbdmxfilter->feed->feed.ts,
342 DMX_OK);
343 else
344 av7110_p2t_write(buffer1, buffer1_len,
345 dvbdmxfilter->feed->pid,
346 &av7110->p2t_filter[dvbdmxfilter->index]);
347 default:
348 return 0;
349 }
350}
351
352
353//#define DEBUG_TIMING
354static inline void print_time(char *s)
355{
356#ifdef DEBUG_TIMING
357 struct timeval tv;
358 do_gettimeofday(&tv);
359 printk("%s: %d.%d\n", s, (int)tv.tv_sec, (int)tv.tv_usec);
360#endif
361}
362
363#define DEBI_READ 0
364#define DEBI_WRITE 1
365static inline void start_debi_dma(struct av7110 *av7110, int dir,
366 unsigned long addr, unsigned int len)
367{
368 dprintk(8, "%c %08lx %u\n", dir == DEBI_READ ? 'R' : 'W', addr, len);
369 if (saa7146_wait_for_debi_done(av7110->dev, 0)) {
370 printk(KERN_ERR "%s: saa7146_wait_for_debi_done timed out\n", __func__);
371 return;
372 }
373
374 SAA7146_ISR_CLEAR(av7110->dev, MASK_19); /* for good measure */
375 SAA7146_IER_ENABLE(av7110->dev, MASK_19);
376 if (len < 5)
377 len = 5; /* we want a real DEBI DMA */
378 if (dir == DEBI_WRITE)
379 iwdebi(av7110, DEBISWAB, addr, 0, (len + 3) & ~3);
380 else
381 irdebi(av7110, DEBISWAB, addr, 0, len);
382}
383
384static void debiirq(unsigned long cookie)
385{
386 struct av7110 *av7110 = (struct av7110 *)cookie;
387 int type = av7110->debitype;
388 int handle = (type >> 8) & 0x1f;
389 unsigned int xfer = 0;
390
391 print_time("debi");
392 dprintk(4, "type 0x%04x\n", type);
393
394 if (type == -1) {
395 printk("DEBI irq oops @ %ld, psr:0x%08x, ssr:0x%08x\n",
396 jiffies, saa7146_read(av7110->dev, PSR),
397 saa7146_read(av7110->dev, SSR));
398 goto debi_done;
399 }
400 av7110->debitype = -1;
401
402 switch (type & 0xff) {
403
404 case DATA_TS_RECORD:
405 dvb_dmx_swfilter_packets(&av7110->demux,
406 (const u8 *) av7110->debi_virt,
407 av7110->debilen / 188);
408 xfer = RX_BUFF;
409 break;
410
411 case DATA_PES_RECORD:
412 if (av7110->demux.recording)
413 av7110_record_cb(&av7110->p2t[handle],
414 (u8 *) av7110->debi_virt,
415 av7110->debilen);
416 xfer = RX_BUFF;
417 break;
418
419 case DATA_IPMPE:
420 case DATA_FSECTION:
421 case DATA_PIPING:
422 if (av7110->handle2filter[handle])
423 DvbDmxFilterCallback((u8 *)av7110->debi_virt,
424 av7110->debilen, NULL, 0,
425 av7110->handle2filter[handle],
426 DMX_OK, av7110);
427 xfer = RX_BUFF;
428 break;
429
430 case DATA_CI_GET:
431 {
432 u8 *data = av7110->debi_virt;
433
434 if ((data[0] < 2) && data[2] == 0xff) {
435 int flags = 0;
436 if (data[5] > 0)
437 flags |= CA_CI_MODULE_PRESENT;
438 if (data[5] > 5)
439 flags |= CA_CI_MODULE_READY;
440 av7110->ci_slot[data[0]].flags = flags;
441 } else
442 ci_get_data(&av7110->ci_rbuffer,
443 av7110->debi_virt,
444 av7110->debilen);
445 xfer = RX_BUFF;
446 break;
447 }
448
449 case DATA_COMMON_INTERFACE:
450 CI_handle(av7110, (u8 *)av7110->debi_virt, av7110->debilen);
451#if 0
452 {
453 int i;
454
455 printk("av7110%d: ", av7110->num);
456 printk("%02x ", *(u8 *)av7110->debi_virt);
457 printk("%02x ", *(1+(u8 *)av7110->debi_virt));
458 for (i = 2; i < av7110->debilen; i++)
459 printk("%02x ", (*(i+(unsigned char *)av7110->debi_virt)));
460 for (i = 2; i < av7110->debilen; i++)
461 printk("%c", chtrans(*(i+(unsigned char *)av7110->debi_virt)));
462
463 printk("\n");
464 }
465#endif
466 xfer = RX_BUFF;
467 break;
468
469 case DATA_DEBUG_MESSAGE:
470 ((s8*)av7110->debi_virt)[Reserved_SIZE - 1] = 0;
471 printk("%s\n", (s8 *) av7110->debi_virt);
472 xfer = RX_BUFF;
473 break;
474
475 case DATA_CI_PUT:
476 dprintk(4, "debi DATA_CI_PUT\n");
477 case DATA_MPEG_PLAY:
478 dprintk(4, "debi DATA_MPEG_PLAY\n");
479 case DATA_BMP_LOAD:
480 dprintk(4, "debi DATA_BMP_LOAD\n");
481 xfer = TX_BUFF;
482 break;
483 default:
484 break;
485 }
486debi_done:
487 spin_lock(&av7110->debilock);
488 if (xfer)
489 iwdebi(av7110, DEBINOSWAP, xfer, 0, 2);
490 ARM_ClearMailBox(av7110);
491 spin_unlock(&av7110->debilock);
492}
493
494/* irq from av7110 firmware writing the mailbox register in the DPRAM */
495static void gpioirq(unsigned long cookie)
496{
497 struct av7110 *av7110 = (struct av7110 *)cookie;
498 u32 rxbuf, txbuf;
499 int len;
500
501 if (av7110->debitype != -1)
502 /* we shouldn't get any irq while a debi xfer is running */
503 printk("dvb-ttpci: GPIO0 irq oops @ %ld, psr:0x%08x, ssr:0x%08x\n",
504 jiffies, saa7146_read(av7110->dev, PSR),
505 saa7146_read(av7110->dev, SSR));
506
507 if (saa7146_wait_for_debi_done(av7110->dev, 0)) {
508 printk(KERN_ERR "%s: saa7146_wait_for_debi_done timed out\n", __func__);
509 BUG(); /* maybe we should try resetting the debi? */
510 }
511
512 spin_lock(&av7110->debilock);
513 ARM_ClearIrq(av7110);
514
515 /* see what the av7110 wants */
516 av7110->debitype = irdebi(av7110, DEBINOSWAP, IRQ_STATE, 0, 2);
517 av7110->debilen = irdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
518 rxbuf = irdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
519 txbuf = irdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
520 len = (av7110->debilen + 3) & ~3;
521
522 print_time("gpio");
523 dprintk(8, "GPIO0 irq 0x%04x %d\n", av7110->debitype, av7110->debilen);
524
525 switch (av7110->debitype & 0xff) {
526
527 case DATA_TS_PLAY:
528 case DATA_PES_PLAY:
529 break;
530
531 case DATA_MPEG_VIDEO_EVENT:
532 {
533 u32 h_ar;
534 struct video_event event;
535
536 av7110->video_size.w = irdebi(av7110, DEBINOSWAP, STATUS_MPEG_WIDTH, 0, 2);
537 h_ar = irdebi(av7110, DEBINOSWAP, STATUS_MPEG_HEIGHT_AR, 0, 2);
538
539 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
540 iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
541
542 av7110->video_size.h = h_ar & 0xfff;
543
544 event.type = VIDEO_EVENT_SIZE_CHANGED;
545 event.u.size.w = av7110->video_size.w;
546 event.u.size.h = av7110->video_size.h;
547 switch ((h_ar >> 12) & 0xf)
548 {
549 case 3:
550 av7110->video_size.aspect_ratio = VIDEO_FORMAT_16_9;
551 event.u.size.aspect_ratio = VIDEO_FORMAT_16_9;
552 av7110->videostate.video_format = VIDEO_FORMAT_16_9;
553 break;
554 case 4:
555 av7110->video_size.aspect_ratio = VIDEO_FORMAT_221_1;
556 event.u.size.aspect_ratio = VIDEO_FORMAT_221_1;
557 av7110->videostate.video_format = VIDEO_FORMAT_221_1;
558 break;
559 default:
560 av7110->video_size.aspect_ratio = VIDEO_FORMAT_4_3;
561 event.u.size.aspect_ratio = VIDEO_FORMAT_4_3;
562 av7110->videostate.video_format = VIDEO_FORMAT_4_3;
563 }
564
565 dprintk(8, "GPIO0 irq: DATA_MPEG_VIDEO_EVENT: w/h/ar = %u/%u/%u\n",
566 av7110->video_size.w, av7110->video_size.h,
567 av7110->video_size.aspect_ratio);
568
569 dvb_video_add_event(av7110, &event);
570 break;
571 }
572
573 case DATA_CI_PUT:
574 {
575 int avail;
576 struct dvb_ringbuffer *cibuf = &av7110->ci_wbuffer;
577
578 avail = dvb_ringbuffer_avail(cibuf);
579 if (avail <= 2) {
580 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
581 iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
582 iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
583 break;
584 }
585 len = DVB_RINGBUFFER_PEEK(cibuf, 0) << 8;
586 len |= DVB_RINGBUFFER_PEEK(cibuf, 1);
587 if (avail < len + 2) {
588 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
589 iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
590 iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
591 break;
592 }
593 DVB_RINGBUFFER_SKIP(cibuf, 2);
594
595 dvb_ringbuffer_read(cibuf, av7110->debi_virt, len);
596
597 iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
598 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
599 dprintk(8, "DMA: CI\n");
600 start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE + txbuf, len);
601 spin_unlock(&av7110->debilock);
602 wake_up(&cibuf->queue);
603 return;
604 }
605
606 case DATA_MPEG_PLAY:
607 if (!av7110->playing) {
608 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
609 iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
610 iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
611 break;
612 }
613 len = 0;
614 if (av7110->debitype & 0x100) {
615 spin_lock(&av7110->aout.lock);
616 len = av7110_pes_play(av7110->debi_virt, &av7110->aout, 2048);
617 spin_unlock(&av7110->aout.lock);
618 }
619 if (len <= 0 && (av7110->debitype & 0x200)
620 &&av7110->videostate.play_state != VIDEO_FREEZED) {
621 spin_lock(&av7110->avout.lock);
622 len = av7110_pes_play(av7110->debi_virt, &av7110->avout, 2048);
623 spin_unlock(&av7110->avout.lock);
624 }
625 if (len <= 0) {
626 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
627 iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
628 iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
629 break;
630 }
631 dprintk(8, "GPIO0 PES_PLAY len=%04x\n", len);
632 iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
633 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
634 dprintk(8, "DMA: MPEG_PLAY\n");
635 start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE + txbuf, len);
636 spin_unlock(&av7110->debilock);
637 return;
638
639 case DATA_BMP_LOAD:
640 len = av7110->debilen;
641 dprintk(8, "gpio DATA_BMP_LOAD len %d\n", len);
642 if (!len) {
643 av7110->bmp_state = BMP_LOADED;
644 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
645 iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
646 iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
647 wake_up(&av7110->bmpq);
648 dprintk(8, "gpio DATA_BMP_LOAD done\n");
649 break;
650 }
651 if (len > av7110->bmplen)
652 len = av7110->bmplen;
653 if (len > 2 * 1024)
654 len = 2 * 1024;
655 iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
656 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
657 memcpy(av7110->debi_virt, av7110->bmpbuf+av7110->bmpp, len);
658 av7110->bmpp += len;
659 av7110->bmplen -= len;
660 dprintk(8, "gpio DATA_BMP_LOAD DMA len %d\n", len);
661 start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE+txbuf, len);
662 spin_unlock(&av7110->debilock);
663 return;
664
665 case DATA_CI_GET:
666 case DATA_COMMON_INTERFACE:
667 case DATA_FSECTION:
668 case DATA_IPMPE:
669 case DATA_PIPING:
670 if (!len || len > 4 * 1024) {
671 iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
672 break;
673 }
674 /* fall through */
675
676 case DATA_TS_RECORD:
677 case DATA_PES_RECORD:
678 dprintk(8, "DMA: TS_REC etc.\n");
679 start_debi_dma(av7110, DEBI_READ, DPRAM_BASE+rxbuf, len);
680 spin_unlock(&av7110->debilock);
681 return;
682
683 case DATA_DEBUG_MESSAGE:
684 if (!len || len > 0xff) {
685 iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
686 break;
687 }
688 start_debi_dma(av7110, DEBI_READ, Reserved, len);
689 spin_unlock(&av7110->debilock);
690 return;
691
692 case DATA_IRCOMMAND:
693 if (av7110->ir.ir_handler)
694 av7110->ir.ir_handler(av7110,
695 swahw32(irdebi(av7110, DEBINOSWAP, Reserved, 0, 4)));
696 iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
697 break;
698
699 default:
700 printk("dvb-ttpci: gpioirq unknown type=%d len=%d\n",
701 av7110->debitype, av7110->debilen);
702 break;
703 }
704 av7110->debitype = -1;
705 ARM_ClearMailBox(av7110);
706 spin_unlock(&av7110->debilock);
707}
708
709
710#ifdef CONFIG_DVB_AV7110_OSD
711static int dvb_osd_ioctl(struct file *file,
712 unsigned int cmd, void *parg)
713{
714 struct dvb_device *dvbdev = file->private_data;
715 struct av7110 *av7110 = dvbdev->priv;
716
717 dprintk(4, "%p\n", av7110);
718
719 if (cmd == OSD_SEND_CMD)
720 return av7110_osd_cmd(av7110, (osd_cmd_t *) parg);
721 if (cmd == OSD_GET_CAPABILITY)
722 return av7110_osd_capability(av7110, (osd_cap_t *) parg);
723
724 return -EINVAL;
725}
726
727
728static const struct file_operations dvb_osd_fops = {
729 .owner = THIS_MODULE,
730 .unlocked_ioctl = dvb_generic_ioctl,
731 .open = dvb_generic_open,
732 .release = dvb_generic_release,
733 .llseek = noop_llseek,
734};
735
736static struct dvb_device dvbdev_osd = {
737 .priv = NULL,
738 .users = 1,
739 .writers = 1,
740 .fops = &dvb_osd_fops,
741 .kernel_ioctl = dvb_osd_ioctl,
742};
743#endif /* CONFIG_DVB_AV7110_OSD */
744
745
746static inline int SetPIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
747 u16 subpid, u16 pcrpid)
748{
749 u16 aflags = 0;
750
751 dprintk(4, "%p\n", av7110);
752
753 if (vpid == 0x1fff || apid == 0x1fff ||
754 ttpid == 0x1fff || subpid == 0x1fff || pcrpid == 0x1fff) {
755 vpid = apid = ttpid = subpid = pcrpid = 0;
756 av7110->pids[DMX_PES_VIDEO] = 0;
757 av7110->pids[DMX_PES_AUDIO] = 0;
758 av7110->pids[DMX_PES_TELETEXT] = 0;
759 av7110->pids[DMX_PES_PCR] = 0;
760 }
761
762 if (av7110->audiostate.bypass_mode)
763 aflags |= 0x8000;
764
765 return av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, MultiPID, 6,
766 pcrpid, vpid, apid, ttpid, subpid, aflags);
767}
768
769int ChangePIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
770 u16 subpid, u16 pcrpid)
771{
772 int ret = 0;
773 dprintk(4, "%p\n", av7110);
774
775 if (mutex_lock_interruptible(&av7110->pid_mutex))
776 return -ERESTARTSYS;
777
778 if (!(vpid & 0x8000))
779 av7110->pids[DMX_PES_VIDEO] = vpid;
780 if (!(apid & 0x8000))
781 av7110->pids[DMX_PES_AUDIO] = apid;
782 if (!(ttpid & 0x8000))
783 av7110->pids[DMX_PES_TELETEXT] = ttpid;
784 if (!(pcrpid & 0x8000))
785 av7110->pids[DMX_PES_PCR] = pcrpid;
786
787 av7110->pids[DMX_PES_SUBTITLE] = 0;
788
789 if (av7110->fe_synced) {
790 pcrpid = av7110->pids[DMX_PES_PCR];
791 ret = SetPIDs(av7110, vpid, apid, ttpid, subpid, pcrpid);
792 }
793
794 mutex_unlock(&av7110->pid_mutex);
795 return ret;
796}
797
798
799/******************************************************************************
800 * hardware filter functions
801 ******************************************************************************/
802
803static int StartHWFilter(struct dvb_demux_filter *dvbdmxfilter)
804{
805 struct dvb_demux_feed *dvbdmxfeed = dvbdmxfilter->feed;
806 struct av7110 *av7110 = dvbdmxfeed->demux->priv;
807 u16 buf[20];
808 int ret, i;
809 u16 handle;
810// u16 mode = 0x0320;
811 u16 mode = 0xb96a;
812
813 dprintk(4, "%p\n", av7110);
814
815 if (av7110->full_ts)
816 return 0;
817
818 if (dvbdmxfilter->type == DMX_TYPE_SEC) {
819 if (hw_sections) {
820 buf[4] = (dvbdmxfilter->filter.filter_value[0] << 8) |
821 dvbdmxfilter->maskandmode[0];
822 for (i = 3; i < 18; i++)
823 buf[i + 4 - 2] =
824 (dvbdmxfilter->filter.filter_value[i] << 8) |
825 dvbdmxfilter->maskandmode[i];
826 mode = 4;
827 }
828 } else if ((dvbdmxfeed->ts_type & TS_PACKET) &&
829 !(dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)) {
830 av7110_p2t_init(&av7110->p2t_filter[dvbdmxfilter->index], dvbdmxfeed);
831 }
832
833 buf[0] = (COMTYPE_PID_FILTER << 8) + AddPIDFilter;
834 buf[1] = 16;
835 buf[2] = dvbdmxfeed->pid;
836 buf[3] = mode;
837
838 ret = av7110_fw_request(av7110, buf, 20, &handle, 1);
839 if (ret != 0 || handle >= 32) {
840 printk("dvb-ttpci: %s error buf %04x %04x %04x %04x "
841 "ret %d handle %04x\n",
842 __func__, buf[0], buf[1], buf[2], buf[3],
843 ret, handle);
844 dvbdmxfilter->hw_handle = 0xffff;
845 if (!ret)
846 ret = -1;
847 return ret;
848 }
849
850 av7110->handle2filter[handle] = dvbdmxfilter;
851 dvbdmxfilter->hw_handle = handle;
852
853 return ret;
854}
855
856static int StopHWFilter(struct dvb_demux_filter *dvbdmxfilter)
857{
858 struct av7110 *av7110 = dvbdmxfilter->feed->demux->priv;
859 u16 buf[3];
860 u16 answ[2];
861 int ret;
862 u16 handle;
863
864 dprintk(4, "%p\n", av7110);
865
866 if (av7110->full_ts)
867 return 0;
868
869 handle = dvbdmxfilter->hw_handle;
870 if (handle >= 32) {
871 printk("%s tried to stop invalid filter %04x, filter type = %x\n",
872 __func__, handle, dvbdmxfilter->type);
873 return -EINVAL;
874 }
875
876 av7110->handle2filter[handle] = NULL;
877
878 buf[0] = (COMTYPE_PID_FILTER << 8) + DelPIDFilter;
879 buf[1] = 1;
880 buf[2] = handle;
881 ret = av7110_fw_request(av7110, buf, 3, answ, 2);
882 if (ret != 0 || answ[1] != handle) {
883 printk("dvb-ttpci: %s error cmd %04x %04x %04x ret %x "
884 "resp %04x %04x pid %d\n",
885 __func__, buf[0], buf[1], buf[2], ret,
886 answ[0], answ[1], dvbdmxfilter->feed->pid);
887 if (!ret)
888 ret = -1;
889 }
890 return ret;
891}
892
893
894static int dvb_feed_start_pid(struct dvb_demux_feed *dvbdmxfeed)
895{
896 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
897 struct av7110 *av7110 = dvbdmx->priv;
898 u16 *pid = dvbdmx->pids, npids[5];
899 int i;
900 int ret = 0;
901
902 dprintk(4, "%p\n", av7110);
903
904 npids[0] = npids[1] = npids[2] = npids[3] = npids[4] = 0xffff;
905 i = dvbdmxfeed->pes_type;
906 npids[i] = (pid[i]&0x8000) ? 0 : pid[i];
907 if ((i == 2) && npids[i] && (dvbdmxfeed->ts_type & TS_PACKET)) {
908 npids[i] = 0;
909 ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
910 if (!ret)
911 ret = StartHWFilter(dvbdmxfeed->filter);
912 return ret;
913 }
914 if (dvbdmxfeed->pes_type <= 2 || dvbdmxfeed->pes_type == 4) {
915 ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
916 if (ret)
917 return ret;
918 }
919
920 if (dvbdmxfeed->pes_type < 2 && npids[0])
921 if (av7110->fe_synced)
922 {
923 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
924 if (ret)
925 return ret;
926 }
927
928 if ((dvbdmxfeed->ts_type & TS_PACKET) && !av7110->full_ts) {
929 if (dvbdmxfeed->pes_type == 0 && !(dvbdmx->pids[0] & 0x8000))
930 ret = av7110_av_start_record(av7110, RP_AUDIO, dvbdmxfeed);
931 if (dvbdmxfeed->pes_type == 1 && !(dvbdmx->pids[1] & 0x8000))
932 ret = av7110_av_start_record(av7110, RP_VIDEO, dvbdmxfeed);
933 }
934 return ret;
935}
936
937static int dvb_feed_stop_pid(struct dvb_demux_feed *dvbdmxfeed)
938{
939 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
940 struct av7110 *av7110 = dvbdmx->priv;
941 u16 *pid = dvbdmx->pids, npids[5];
942 int i;
943
944 int ret = 0;
945
946 dprintk(4, "%p\n", av7110);
947
948 if (dvbdmxfeed->pes_type <= 1) {
949 ret = av7110_av_stop(av7110, dvbdmxfeed->pes_type ? RP_VIDEO : RP_AUDIO);
950 if (ret)
951 return ret;
952 if (!av7110->rec_mode)
953 dvbdmx->recording = 0;
954 if (!av7110->playing)
955 dvbdmx->playing = 0;
956 }
957 npids[0] = npids[1] = npids[2] = npids[3] = npids[4] = 0xffff;
958 i = dvbdmxfeed->pes_type;
959 switch (i) {
960 case 2: //teletext
961 if (dvbdmxfeed->ts_type & TS_PACKET)
962 ret = StopHWFilter(dvbdmxfeed->filter);
963 npids[2] = 0;
964 break;
965 case 0:
966 case 1:
967 case 4:
968 if (!pids_off)
969 return 0;
970 npids[i] = (pid[i]&0x8000) ? 0 : pid[i];
971 break;
972 }
973 if (!ret)
974 ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
975 return ret;
976}
977
978static int av7110_start_feed(struct dvb_demux_feed *feed)
979{
980 struct dvb_demux *demux = feed->demux;
981 struct av7110 *av7110 = demux->priv;
982 int ret = 0;
983
984 dprintk(4, "%p\n", av7110);
985
986 if (!demux->dmx.frontend)
987 return -EINVAL;
988
989 if (!av7110->full_ts && feed->pid > 0x1fff)
990 return -EINVAL;
991
992 if (feed->type == DMX_TYPE_TS) {
993 if ((feed->ts_type & TS_DECODER) &&
994 (feed->pes_type < DMX_TS_PES_OTHER)) {
995 switch (demux->dmx.frontend->source) {
996 case DMX_MEMORY_FE:
997 if (feed->ts_type & TS_DECODER)
998 if (feed->pes_type < 2 &&
999 !(demux->pids[0] & 0x8000) &&
1000 !(demux->pids[1] & 0x8000)) {
1001 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1002 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1003 ret = av7110_av_start_play(av7110,RP_AV);
1004 if (!ret)
1005 demux->playing = 1;
1006 }
1007 break;
1008 default:
1009 ret = dvb_feed_start_pid(feed);
1010 break;
1011 }
1012 } else if ((feed->ts_type & TS_PACKET) &&
1013 (demux->dmx.frontend->source != DMX_MEMORY_FE)) {
1014 ret = StartHWFilter(feed->filter);
1015 }
1016 }
1017
1018 if (av7110->full_ts) {
1019 budget_start_feed(feed);
1020 return ret;
1021 }
1022
1023 if (feed->type == DMX_TYPE_SEC) {
1024 int i;
1025
1026 for (i = 0; i < demux->filternum; i++) {
1027 if (demux->filter[i].state != DMX_STATE_READY)
1028 continue;
1029 if (demux->filter[i].type != DMX_TYPE_SEC)
1030 continue;
1031 if (demux->filter[i].filter.parent != &feed->feed.sec)
1032 continue;
1033 demux->filter[i].state = DMX_STATE_GO;
1034 if (demux->dmx.frontend->source != DMX_MEMORY_FE) {
1035 ret = StartHWFilter(&demux->filter[i]);
1036 if (ret)
1037 break;
1038 }
1039 }
1040 }
1041
1042 return ret;
1043}
1044
1045
1046static int av7110_stop_feed(struct dvb_demux_feed *feed)
1047{
1048 struct dvb_demux *demux = feed->demux;
1049 struct av7110 *av7110 = demux->priv;
1050 int i, rc, ret = 0;
1051 dprintk(4, "%p\n", av7110);
1052
1053 if (feed->type == DMX_TYPE_TS) {
1054 if (feed->ts_type & TS_DECODER) {
1055 if (feed->pes_type >= DMX_TS_PES_OTHER ||
1056 !demux->pesfilter[feed->pes_type])
1057 return -EINVAL;
1058 demux->pids[feed->pes_type] |= 0x8000;
1059 demux->pesfilter[feed->pes_type] = NULL;
1060 }
1061 if (feed->ts_type & TS_DECODER &&
1062 feed->pes_type < DMX_TS_PES_OTHER) {
1063 ret = dvb_feed_stop_pid(feed);
1064 } else
1065 if ((feed->ts_type & TS_PACKET) &&
1066 (demux->dmx.frontend->source != DMX_MEMORY_FE))
1067 ret = StopHWFilter(feed->filter);
1068 }
1069
1070 if (av7110->full_ts) {
1071 budget_stop_feed(feed);
1072 return ret;
1073 }
1074
1075 if (feed->type == DMX_TYPE_SEC) {
1076 for (i = 0; i<demux->filternum; i++) {
1077 if (demux->filter[i].state == DMX_STATE_GO &&
1078 demux->filter[i].filter.parent == &feed->feed.sec) {
1079 demux->filter[i].state = DMX_STATE_READY;
1080 if (demux->dmx.frontend->source != DMX_MEMORY_FE) {
1081 rc = StopHWFilter(&demux->filter[i]);
1082 if (!ret)
1083 ret = rc;
1084 /* keep going, stop as many filters as possible */
1085 }
1086 }
1087 }
1088 }
1089
1090 return ret;
1091}
1092
1093
1094static void restart_feeds(struct av7110 *av7110)
1095{
1096 struct dvb_demux *dvbdmx = &av7110->demux;
1097 struct dvb_demux_feed *feed;
1098 int mode;
1099 int feeding;
1100 int i, j;
1101
1102 dprintk(4, "%p\n", av7110);
1103
1104 mode = av7110->playing;
1105 av7110->playing = 0;
1106 av7110->rec_mode = 0;
1107
1108 feeding = av7110->feeding1; /* full_ts mod */
1109
1110 for (i = 0; i < dvbdmx->feednum; i++) {
1111 feed = &dvbdmx->feed[i];
1112 if (feed->state == DMX_STATE_GO) {
1113 if (feed->type == DMX_TYPE_SEC) {
1114 for (j = 0; j < dvbdmx->filternum; j++) {
1115 if (dvbdmx->filter[j].type != DMX_TYPE_SEC)
1116 continue;
1117 if (dvbdmx->filter[j].filter.parent != &feed->feed.sec)
1118 continue;
1119 if (dvbdmx->filter[j].state == DMX_STATE_GO)
1120 dvbdmx->filter[j].state = DMX_STATE_READY;
1121 }
1122 }
1123 av7110_start_feed(feed);
1124 }
1125 }
1126
1127 av7110->feeding1 = feeding; /* full_ts mod */
1128
1129 if (mode)
1130 av7110_av_start_play(av7110, mode);
1131}
1132
1133static int dvb_get_stc(struct dmx_demux *demux, unsigned int num,
1134 uint64_t *stc, unsigned int *base)
1135{
1136 int ret;
1137 u16 fwstc[4];
1138 u16 tag = ((COMTYPE_REQUEST << 8) + ReqSTC);
1139 struct dvb_demux *dvbdemux;
1140 struct av7110 *av7110;
1141
1142 /* pointer casting paranoia... */
1143 BUG_ON(!demux);
1144 dvbdemux = demux->priv;
1145 BUG_ON(!dvbdemux);
1146 av7110 = dvbdemux->priv;
1147
1148 dprintk(4, "%p\n", av7110);
1149
1150 if (num != 0)
1151 return -EINVAL;
1152
1153 ret = av7110_fw_request(av7110, &tag, 0, fwstc, 4);
1154 if (ret) {
1155 printk(KERN_ERR "%s: av7110_fw_request error\n", __func__);
1156 return ret;
1157 }
1158 dprintk(2, "fwstc = %04hx %04hx %04hx %04hx\n",
1159 fwstc[0], fwstc[1], fwstc[2], fwstc[3]);
1160
1161 *stc = (((uint64_t) ((fwstc[3] & 0x8000) >> 15)) << 32) |
1162 (((uint64_t) fwstc[1]) << 16) | ((uint64_t) fwstc[0]);
1163 *base = 1;
1164
1165 dprintk(4, "stc = %lu\n", (unsigned long)*stc);
1166
1167 return 0;
1168}
1169
1170
1171/******************************************************************************
1172 * SEC device file operations
1173 ******************************************************************************/
1174
1175
1176static int av7110_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
1177{
1178 struct av7110* av7110 = fe->dvb->priv;
1179
1180 switch (tone) {
1181 case SEC_TONE_ON:
1182 return Set22K(av7110, 1);
1183
1184 case SEC_TONE_OFF:
1185 return Set22K(av7110, 0);
1186
1187 default:
1188 return -EINVAL;
1189 }
1190}
1191
1192static int av7110_diseqc_send_master_cmd(struct dvb_frontend* fe,
1193 struct dvb_diseqc_master_cmd* cmd)
1194{
1195 struct av7110* av7110 = fe->dvb->priv;
1196
1197 return av7110_diseqc_send(av7110, cmd->msg_len, cmd->msg, -1);
1198}
1199
1200static int av7110_diseqc_send_burst(struct dvb_frontend* fe,
1201 fe_sec_mini_cmd_t minicmd)
1202{
1203 struct av7110* av7110 = fe->dvb->priv;
1204
1205 return av7110_diseqc_send(av7110, 0, NULL, minicmd);
1206}
1207
1208/* simplified code from budget-core.c */
1209static int stop_ts_capture(struct av7110 *budget)
1210{
1211 dprintk(2, "budget: %p\n", budget);
1212
1213 if (--budget->feeding1)
1214 return budget->feeding1;
1215 saa7146_write(budget->dev, MC1, MASK_20); /* DMA3 off */
1216 SAA7146_IER_DISABLE(budget->dev, MASK_10);
1217 SAA7146_ISR_CLEAR(budget->dev, MASK_10);
1218 return 0;
1219}
1220
1221static int start_ts_capture(struct av7110 *budget)
1222{
1223 dprintk(2, "budget: %p\n", budget);
1224
1225 if (budget->feeding1)
1226 return ++budget->feeding1;
1227 memset(budget->grabbing, 0x00, TS_BUFLEN);
1228 budget->ttbp = 0;
1229 SAA7146_ISR_CLEAR(budget->dev, MASK_10); /* VPE */
1230 SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
1231 saa7146_write(budget->dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
1232 return ++budget->feeding1;
1233}
1234
1235static int budget_start_feed(struct dvb_demux_feed *feed)
1236{
1237 struct dvb_demux *demux = feed->demux;
1238 struct av7110 *budget = demux->priv;
1239 int status;
1240
1241 dprintk(2, "av7110: %p\n", budget);
1242
1243 spin_lock(&budget->feedlock1);
1244 feed->pusi_seen = 0; /* have a clean section start */
1245 status = start_ts_capture(budget);
1246 spin_unlock(&budget->feedlock1);
1247 return status;
1248}
1249
1250static int budget_stop_feed(struct dvb_demux_feed *feed)
1251{
1252 struct dvb_demux *demux = feed->demux;
1253 struct av7110 *budget = demux->priv;
1254 int status;
1255
1256 dprintk(2, "budget: %p\n", budget);
1257
1258 spin_lock(&budget->feedlock1);
1259 status = stop_ts_capture(budget);
1260 spin_unlock(&budget->feedlock1);
1261 return status;
1262}
1263
1264static void vpeirq(unsigned long cookie)
1265{
1266 struct av7110 *budget = (struct av7110 *)cookie;
1267 u8 *mem = (u8 *) (budget->grabbing);
1268 u32 olddma = budget->ttbp;
1269 u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
1270 struct dvb_demux *demux = budget->full_ts ? &budget->demux : &budget->demux1;
1271
1272 /* nearest lower position divisible by 188 */
1273 newdma -= newdma % 188;
1274
1275 if (newdma >= TS_BUFLEN)
1276 return;
1277
1278 budget->ttbp = newdma;
1279
1280 if (!budget->feeding1 || (newdma == olddma))
1281 return;
1282
1283 /* Ensure streamed PCI data is synced to CPU */
1284 pci_dma_sync_sg_for_cpu(budget->dev->pci, budget->pt.slist, budget->pt.nents, PCI_DMA_FROMDEVICE);
1285
1286#if 0
1287 /* track rps1 activity */
1288 printk("vpeirq: %02x Event Counter 1 0x%04x\n",
1289 mem[olddma],
1290 saa7146_read(budget->dev, EC1R) & 0x3fff);
1291#endif
1292
1293 if (newdma > olddma)
1294 /* no wraparound, dump olddma..newdma */
1295 dvb_dmx_swfilter_packets(demux, mem + olddma, (newdma - olddma) / 188);
1296 else {
1297 /* wraparound, dump olddma..buflen and 0..newdma */
1298 dvb_dmx_swfilter_packets(demux, mem + olddma, (TS_BUFLEN - olddma) / 188);
1299 dvb_dmx_swfilter_packets(demux, mem, newdma / 188);
1300 }
1301}
1302
1303static int av7110_register(struct av7110 *av7110)
1304{
1305 int ret, i;
1306 struct dvb_demux *dvbdemux = &av7110->demux;
1307 struct dvb_demux *dvbdemux1 = &av7110->demux1;
1308
1309 dprintk(4, "%p\n", av7110);
1310
1311 if (av7110->registered)
1312 return -1;
1313
1314 av7110->registered = 1;
1315
1316 dvbdemux->priv = (void *) av7110;
1317
1318 for (i = 0; i < 32; i++)
1319 av7110->handle2filter[i] = NULL;
1320
1321 dvbdemux->filternum = (av7110->full_ts) ? 256 : 32;
1322 dvbdemux->feednum = (av7110->full_ts) ? 256 : 32;
1323 dvbdemux->start_feed = av7110_start_feed;
1324 dvbdemux->stop_feed = av7110_stop_feed;
1325 dvbdemux->write_to_decoder = av7110_write_to_decoder;
1326 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1327 DMX_MEMORY_BASED_FILTERING);
1328
1329 dvb_dmx_init(&av7110->demux);
1330 av7110->demux.dmx.get_stc = dvb_get_stc;
1331
1332 av7110->dmxdev.filternum = (av7110->full_ts) ? 256 : 32;
1333 av7110->dmxdev.demux = &dvbdemux->dmx;
1334 av7110->dmxdev.capabilities = 0;
1335
1336 dvb_dmxdev_init(&av7110->dmxdev, &av7110->dvb_adapter);
1337
1338 av7110->hw_frontend.source = DMX_FRONTEND_0;
1339
1340 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->hw_frontend);
1341
1342 if (ret < 0)
1343 return ret;
1344
1345 av7110->mem_frontend.source = DMX_MEMORY_FE;
1346
1347 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->mem_frontend);
1348
1349 if (ret < 0)
1350 return ret;
1351
1352 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx,
1353 &av7110->hw_frontend);
1354 if (ret < 0)
1355 return ret;
1356
1357 av7110_av_register(av7110);
1358 av7110_ca_register(av7110);
1359
1360#ifdef CONFIG_DVB_AV7110_OSD
1361 dvb_register_device(&av7110->dvb_adapter, &av7110->osd_dev,
1362 &dvbdev_osd, av7110, DVB_DEVICE_OSD);
1363#endif
1364
1365 dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net, &dvbdemux->dmx);
1366
1367 if (budgetpatch) {
1368 /* initialize software demux1 without its own frontend
1369 * demux1 hardware is connected to frontend0 of demux0
1370 */
1371 dvbdemux1->priv = (void *) av7110;
1372
1373 dvbdemux1->filternum = 256;
1374 dvbdemux1->feednum = 256;
1375 dvbdemux1->start_feed = budget_start_feed;
1376 dvbdemux1->stop_feed = budget_stop_feed;
1377 dvbdemux1->write_to_decoder = NULL;
1378
1379 dvbdemux1->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1380 DMX_MEMORY_BASED_FILTERING);
1381
1382 dvb_dmx_init(&av7110->demux1);
1383
1384 av7110->dmxdev1.filternum = 256;
1385 av7110->dmxdev1.demux = &dvbdemux1->dmx;
1386 av7110->dmxdev1.capabilities = 0;
1387
1388 dvb_dmxdev_init(&av7110->dmxdev1, &av7110->dvb_adapter);
1389
1390 dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net1, &dvbdemux1->dmx);
1391 printk("dvb-ttpci: additional demux1 for budget-patch registered\n");
1392 }
1393 return 0;
1394}
1395
1396
1397static void dvb_unregister(struct av7110 *av7110)
1398{
1399 struct dvb_demux *dvbdemux = &av7110->demux;
1400 struct dvb_demux *dvbdemux1 = &av7110->demux1;
1401
1402 dprintk(4, "%p\n", av7110);
1403
1404 if (!av7110->registered)
1405 return;
1406
1407 if (budgetpatch) {
1408 dvb_net_release(&av7110->dvb_net1);
1409 dvbdemux->dmx.close(&dvbdemux1->dmx);
1410 dvb_dmxdev_release(&av7110->dmxdev1);
1411 dvb_dmx_release(&av7110->demux1);
1412 }
1413
1414 dvb_net_release(&av7110->dvb_net);
1415
1416 dvbdemux->dmx.close(&dvbdemux->dmx);
1417 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &av7110->hw_frontend);
1418 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &av7110->mem_frontend);
1419
1420 dvb_dmxdev_release(&av7110->dmxdev);
1421 dvb_dmx_release(&av7110->demux);
1422
1423 if (av7110->fe != NULL) {
1424 dvb_unregister_frontend(av7110->fe);
1425 dvb_frontend_detach(av7110->fe);
1426 }
1427 dvb_unregister_device(av7110->osd_dev);
1428 av7110_av_unregister(av7110);
1429 av7110_ca_unregister(av7110);
1430}
1431
1432
1433/****************************************************************************
1434 * I2C client commands
1435 ****************************************************************************/
1436
1437int i2c_writereg(struct av7110 *av7110, u8 id, u8 reg, u8 val)
1438{
1439 u8 msg[2] = { reg, val };
1440 struct i2c_msg msgs;
1441
1442 msgs.flags = 0;
1443 msgs.addr = id / 2;
1444 msgs.len = 2;
1445 msgs.buf = msg;
1446 return i2c_transfer(&av7110->i2c_adap, &msgs, 1);
1447}
1448
1449u8 i2c_readreg(struct av7110 *av7110, u8 id, u8 reg)
1450{
1451 u8 mm1[] = {0x00};
1452 u8 mm2[] = {0x00};
1453 struct i2c_msg msgs[2];
1454
1455 msgs[0].flags = 0;
1456 msgs[1].flags = I2C_M_RD;
1457 msgs[0].addr = msgs[1].addr = id / 2;
1458 mm1[0] = reg;
1459 msgs[0].len = 1; msgs[1].len = 1;
1460 msgs[0].buf = mm1; msgs[1].buf = mm2;
1461 i2c_transfer(&av7110->i2c_adap, msgs, 2);
1462
1463 return mm2[0];
1464}
1465
1466/****************************************************************************
1467 * INITIALIZATION
1468 ****************************************************************************/
1469
1470
1471static int check_firmware(struct av7110* av7110)
1472{
1473 u32 crc = 0, len = 0;
1474 unsigned char *ptr;
1475
1476 /* check for firmware magic */
1477 ptr = av7110->bin_fw;
1478 if (ptr[0] != 'A' || ptr[1] != 'V' ||
1479 ptr[2] != 'F' || ptr[3] != 'W') {
1480 printk("dvb-ttpci: this is not an av7110 firmware\n");
1481 return -EINVAL;
1482 }
1483 ptr += 4;
1484
1485 /* check dpram file */
1486 crc = get_unaligned_be32(ptr);
1487 ptr += 4;
1488 len = get_unaligned_be32(ptr);
1489 ptr += 4;
1490 if (len >= 512) {
1491 printk("dvb-ttpci: dpram file is way too big.\n");
1492 return -EINVAL;
1493 }
1494 if (crc != crc32_le(0, ptr, len)) {
1495 printk("dvb-ttpci: crc32 of dpram file does not match.\n");
1496 return -EINVAL;
1497 }
1498 av7110->bin_dpram = ptr;
1499 av7110->size_dpram = len;
1500 ptr += len;
1501
1502 /* check root file */
1503 crc = get_unaligned_be32(ptr);
1504 ptr += 4;
1505 len = get_unaligned_be32(ptr);
1506 ptr += 4;
1507
1508 if (len <= 200000 || len >= 300000 ||
1509 len > ((av7110->bin_fw + av7110->size_fw) - ptr)) {
1510 printk("dvb-ttpci: root file has strange size (%d). aborting.\n", len);
1511 return -EINVAL;
1512 }
1513 if( crc != crc32_le(0, ptr, len)) {
1514 printk("dvb-ttpci: crc32 of root file does not match.\n");
1515 return -EINVAL;
1516 }
1517 av7110->bin_root = ptr;
1518 av7110->size_root = len;
1519 return 0;
1520}
1521
1522static void put_firmware(struct av7110* av7110)
1523{
1524 vfree(av7110->bin_fw);
1525}
1526
1527static int get_firmware(struct av7110* av7110)
1528{
1529 int ret;
1530 const struct firmware *fw;
1531
1532 /* request the av7110 firmware, this will block until someone uploads it */
1533 ret = request_firmware(&fw, "dvb-ttpci-01.fw", &av7110->dev->pci->dev);
1534 if (ret) {
1535 if (ret == -ENOENT) {
1536 printk(KERN_ERR "dvb-ttpci: could not load firmware,"
1537 " file not found: dvb-ttpci-01.fw\n");
1538 printk(KERN_ERR "dvb-ttpci: usually this should be in "
1539 "/usr/lib/hotplug/firmware or /lib/firmware\n");
1540 printk(KERN_ERR "dvb-ttpci: and can be downloaded from"
1541 " http://www.linuxtv.org/download/dvb/firmware/\n");
1542 } else
1543 printk(KERN_ERR "dvb-ttpci: cannot request firmware"
1544 " (error %i)\n", ret);
1545 return -EINVAL;
1546 }
1547
1548 if (fw->size <= 200000) {
1549 printk("dvb-ttpci: this firmware is way too small.\n");
1550 release_firmware(fw);
1551 return -EINVAL;
1552 }
1553
1554 /* check if the firmware is available */
1555 av7110->bin_fw = vmalloc(fw->size);
1556 if (NULL == av7110->bin_fw) {
1557 dprintk(1, "out of memory\n");
1558 release_firmware(fw);
1559 return -ENOMEM;
1560 }
1561
1562 memcpy(av7110->bin_fw, fw->data, fw->size);
1563 av7110->size_fw = fw->size;
1564 if ((ret = check_firmware(av7110)))
1565 vfree(av7110->bin_fw);
1566
1567 release_firmware(fw);
1568 return ret;
1569}
1570
1571static int alps_bsrv2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1572{
1573 struct av7110* av7110 = fe->dvb->priv;
1574 u8 pwr = 0;
1575 u8 buf[4];
1576 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };
1577 u32 div = (params->frequency + 479500) / 125;
1578
1579 if (params->frequency > 2000000) pwr = 3;
1580 else if (params->frequency > 1800000) pwr = 2;
1581 else if (params->frequency > 1600000) pwr = 1;
1582 else if (params->frequency > 1200000) pwr = 0;
1583 else if (params->frequency >= 1100000) pwr = 1;
1584 else pwr = 2;
1585
1586 buf[0] = (div >> 8) & 0x7f;
1587 buf[1] = div & 0xff;
1588 buf[2] = ((div & 0x18000) >> 10) | 0x95;
1589 buf[3] = (pwr << 6) | 0x30;
1590
1591 // NOTE: since we're using a prescaler of 2, we set the
1592 // divisor frequency to 62.5kHz and divide by 125 above
1593
1594 if (fe->ops.i2c_gate_ctrl)
1595 fe->ops.i2c_gate_ctrl(fe, 1);
1596 if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1)
1597 return -EIO;
1598 return 0;
1599}
1600
1601static struct ves1x93_config alps_bsrv2_config = {
1602 .demod_address = 0x08,
1603 .xin = 90100000UL,
1604 .invert_pwm = 0,
1605};
1606
1607static int alps_tdbe2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1608{
1609 struct av7110* av7110 = fe->dvb->priv;
1610 u32 div;
1611 u8 data[4];
1612 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1613
1614 div = (params->frequency + 35937500 + 31250) / 62500;
1615
1616 data[0] = (div >> 8) & 0x7f;
1617 data[1] = div & 0xff;
1618 data[2] = 0x85 | ((div >> 10) & 0x60);
1619 data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
1620
1621 if (fe->ops.i2c_gate_ctrl)
1622 fe->ops.i2c_gate_ctrl(fe, 1);
1623 if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1624 return -EIO;
1625 return 0;
1626}
1627
1628static struct ves1820_config alps_tdbe2_config = {
1629 .demod_address = 0x09,
1630 .xin = 57840000UL,
1631 .invert = 1,
1632 .selagc = VES1820_SELAGC_SIGNAMPERR,
1633};
1634
1635
1636
1637
1638static int grundig_29504_451_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1639{
1640 struct av7110* av7110 = fe->dvb->priv;
1641 u32 div;
1642 u8 data[4];
1643 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1644
1645 div = params->frequency / 125;
1646 data[0] = (div >> 8) & 0x7f;
1647 data[1] = div & 0xff;
1648 data[2] = 0x8e;
1649 data[3] = 0x00;
1650
1651 if (fe->ops.i2c_gate_ctrl)
1652 fe->ops.i2c_gate_ctrl(fe, 1);
1653 if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1654 return -EIO;
1655 return 0;
1656}
1657
1658static struct tda8083_config grundig_29504_451_config = {
1659 .demod_address = 0x68,
1660};
1661
1662
1663
1664static int philips_cd1516_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1665{
1666 struct av7110* av7110 = fe->dvb->priv;
1667 u32 div;
1668 u32 f = params->frequency;
1669 u8 data[4];
1670 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1671
1672 div = (f + 36125000 + 31250) / 62500;
1673
1674 data[0] = (div >> 8) & 0x7f;
1675 data[1] = div & 0xff;
1676 data[2] = 0x8e;
1677 data[3] = (f < 174000000 ? 0xa1 : f < 470000000 ? 0x92 : 0x34);
1678
1679 if (fe->ops.i2c_gate_ctrl)
1680 fe->ops.i2c_gate_ctrl(fe, 1);
1681 if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1682 return -EIO;
1683 return 0;
1684}
1685
1686static struct ves1820_config philips_cd1516_config = {
1687 .demod_address = 0x09,
1688 .xin = 57840000UL,
1689 .invert = 1,
1690 .selagc = VES1820_SELAGC_SIGNAMPERR,
1691};
1692
1693
1694
1695static int alps_tdlb7_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1696{
1697 struct av7110* av7110 = fe->dvb->priv;
1698 u32 div, pwr;
1699 u8 data[4];
1700 struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = data, .len = sizeof(data) };
1701
1702 div = (params->frequency + 36200000) / 166666;
1703
1704 if (params->frequency <= 782000000)
1705 pwr = 1;
1706 else
1707 pwr = 2;
1708
1709 data[0] = (div >> 8) & 0x7f;
1710 data[1] = div & 0xff;
1711 data[2] = 0x85;
1712 data[3] = pwr << 6;
1713
1714 if (fe->ops.i2c_gate_ctrl)
1715 fe->ops.i2c_gate_ctrl(fe, 1);
1716 if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1717 return -EIO;
1718 return 0;
1719}
1720
1721static int alps_tdlb7_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1722{
1723#if defined(CONFIG_DVB_SP8870) || defined(CONFIG_DVB_SP8870_MODULE)
1724 struct av7110* av7110 = fe->dvb->priv;
1725
1726 return request_firmware(fw, name, &av7110->dev->pci->dev);
1727#else
1728 return -EINVAL;
1729#endif
1730}
1731
1732static struct sp8870_config alps_tdlb7_config = {
1733
1734 .demod_address = 0x71,
1735 .request_firmware = alps_tdlb7_request_firmware,
1736};
1737
1738
1739static u8 nexusca_stv0297_inittab[] = {
1740 0x80, 0x01,
1741 0x80, 0x00,
1742 0x81, 0x01,
1743 0x81, 0x00,
1744 0x00, 0x09,
1745 0x01, 0x69,
1746 0x03, 0x00,
1747 0x04, 0x00,
1748 0x07, 0x00,
1749 0x08, 0x00,
1750 0x20, 0x00,
1751 0x21, 0x40,
1752 0x22, 0x00,
1753 0x23, 0x00,
1754 0x24, 0x40,
1755 0x25, 0x88,
1756 0x30, 0xff,
1757 0x31, 0x00,
1758 0x32, 0xff,
1759 0x33, 0x00,
1760 0x34, 0x50,
1761 0x35, 0x7f,
1762 0x36, 0x00,
1763 0x37, 0x20,
1764 0x38, 0x00,
1765 0x40, 0x1c,
1766 0x41, 0xff,
1767 0x42, 0x29,
1768 0x43, 0x00,
1769 0x44, 0xff,
1770 0x45, 0x00,
1771 0x46, 0x00,
1772 0x49, 0x04,
1773 0x4a, 0x00,
1774 0x4b, 0x7b,
1775 0x52, 0x30,
1776 0x55, 0xae,
1777 0x56, 0x47,
1778 0x57, 0xe1,
1779 0x58, 0x3a,
1780 0x5a, 0x1e,
1781 0x5b, 0x34,
1782 0x60, 0x00,
1783 0x63, 0x00,
1784 0x64, 0x00,
1785 0x65, 0x00,
1786 0x66, 0x00,
1787 0x67, 0x00,
1788 0x68, 0x00,
1789 0x69, 0x00,
1790 0x6a, 0x02,
1791 0x6b, 0x00,
1792 0x70, 0xff,
1793 0x71, 0x00,
1794 0x72, 0x00,
1795 0x73, 0x00,
1796 0x74, 0x0c,
1797 0x80, 0x00,
1798 0x81, 0x00,
1799 0x82, 0x00,
1800 0x83, 0x00,
1801 0x84, 0x04,
1802 0x85, 0x80,
1803 0x86, 0x24,
1804 0x87, 0x78,
1805 0x88, 0x10,
1806 0x89, 0x00,
1807 0x90, 0x01,
1808 0x91, 0x01,
1809 0xa0, 0x04,
1810 0xa1, 0x00,
1811 0xa2, 0x00,
1812 0xb0, 0x91,
1813 0xb1, 0x0b,
1814 0xc0, 0x53,
1815 0xc1, 0x70,
1816 0xc2, 0x12,
1817 0xd0, 0x00,
1818 0xd1, 0x00,
1819 0xd2, 0x00,
1820 0xd3, 0x00,
1821 0xd4, 0x00,
1822 0xd5, 0x00,
1823 0xde, 0x00,
1824 0xdf, 0x00,
1825 0x61, 0x49,
1826 0x62, 0x0b,
1827 0x53, 0x08,
1828 0x59, 0x08,
1829 0xff, 0xff,
1830};
1831
1832static int nexusca_stv0297_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1833{
1834 struct av7110* av7110 = fe->dvb->priv;
1835 u32 div;
1836 u8 data[4];
1837 struct i2c_msg msg = { .addr = 0x63, .flags = 0, .buf = data, .len = sizeof(data) };
1838 struct i2c_msg readmsg = { .addr = 0x63, .flags = I2C_M_RD, .buf = data, .len = 1 };
1839 int i;
1840
1841 div = (params->frequency + 36150000 + 31250) / 62500;
1842
1843 data[0] = (div >> 8) & 0x7f;
1844 data[1] = div & 0xff;
1845 data[2] = 0xce;
1846
1847 if (params->frequency < 45000000)
1848 return -EINVAL;
1849 else if (params->frequency < 137000000)
1850 data[3] = 0x01;
1851 else if (params->frequency < 403000000)
1852 data[3] = 0x02;
1853 else if (params->frequency < 860000000)
1854 data[3] = 0x04;
1855 else
1856 return -EINVAL;
1857
1858 if (fe->ops.i2c_gate_ctrl)
1859 fe->ops.i2c_gate_ctrl(fe, 1);
1860 if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1) {
1861 printk("nexusca: pll transfer failed!\n");
1862 return -EIO;
1863 }
1864
1865 // wait for PLL lock
1866 for(i = 0; i < 20; i++) {
1867 if (fe->ops.i2c_gate_ctrl)
1868 fe->ops.i2c_gate_ctrl(fe, 1);
1869 if (i2c_transfer(&av7110->i2c_adap, &readmsg, 1) == 1)
1870 if (data[0] & 0x40) break;
1871 msleep(10);
1872 }
1873
1874 return 0;
1875}
1876
1877static struct stv0297_config nexusca_stv0297_config = {
1878
1879 .demod_address = 0x1C,
1880 .inittab = nexusca_stv0297_inittab,
1881 .invert = 1,
1882 .stop_during_read = 1,
1883};
1884
1885
1886
1887static int grundig_29504_401_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1888{
1889 struct av7110* av7110 = fe->dvb->priv;
1890 u32 div;
1891 u8 cfg, cpump, band_select;
1892 u8 data[4];
1893 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1894
1895 div = (36125000 + params->frequency) / 166666;
1896
1897 cfg = 0x88;
1898
1899 if (params->frequency < 175000000) cpump = 2;
1900 else if (params->frequency < 390000000) cpump = 1;
1901 else if (params->frequency < 470000000) cpump = 2;
1902 else if (params->frequency < 750000000) cpump = 1;
1903 else cpump = 3;
1904
1905 if (params->frequency < 175000000) band_select = 0x0e;
1906 else if (params->frequency < 470000000) band_select = 0x05;
1907 else band_select = 0x03;
1908
1909 data[0] = (div >> 8) & 0x7f;
1910 data[1] = div & 0xff;
1911 data[2] = ((div >> 10) & 0x60) | cfg;
1912 data[3] = (cpump << 6) | band_select;
1913
1914 if (fe->ops.i2c_gate_ctrl)
1915 fe->ops.i2c_gate_ctrl(fe, 1);
1916 if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1) return -EIO;
1917 return 0;
1918}
1919
1920static struct l64781_config grundig_29504_401_config = {
1921 .demod_address = 0x55,
1922};
1923
1924
1925
1926static int av7110_fe_lock_fix(struct av7110* av7110, fe_status_t status)
1927{
1928 int ret = 0;
1929 int synced = (status & FE_HAS_LOCK) ? 1 : 0;
1930
1931 av7110->fe_status = status;
1932
1933 if (av7110->fe_synced == synced)
1934 return 0;
1935
1936 if (av7110->playing) {
1937 av7110->fe_synced = synced;
1938 return 0;
1939 }
1940
1941 if (mutex_lock_interruptible(&av7110->pid_mutex))
1942 return -ERESTARTSYS;
1943
1944 if (synced) {
1945 ret = SetPIDs(av7110, av7110->pids[DMX_PES_VIDEO],
1946 av7110->pids[DMX_PES_AUDIO],
1947 av7110->pids[DMX_PES_TELETEXT], 0,
1948 av7110->pids[DMX_PES_PCR]);
1949 if (!ret)
1950 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
1951 } else {
1952 ret = SetPIDs(av7110, 0, 0, 0, 0, 0);
1953 if (!ret) {
1954 ret = av7110_fw_cmd(av7110, COMTYPE_PID_FILTER, FlushTSQueue, 0);
1955 if (!ret)
1956 ret = av7110_wait_msgstate(av7110, GPMQBusy);
1957 }
1958 }
1959
1960 if (!ret)
1961 av7110->fe_synced = synced;
1962
1963 mutex_unlock(&av7110->pid_mutex);
1964 return ret;
1965}
1966
1967static int av7110_fe_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1968{
1969 struct av7110* av7110 = fe->dvb->priv;
1970
1971 int ret = av7110_fe_lock_fix(av7110, 0);
1972 if (!ret) {
1973 av7110->saved_fe_params = *params;
1974 ret = av7110->fe_set_frontend(fe, params);
1975 }
1976 return ret;
1977}
1978
1979static int av7110_fe_init(struct dvb_frontend* fe)
1980{
1981 struct av7110* av7110 = fe->dvb->priv;
1982
1983 int ret = av7110_fe_lock_fix(av7110, 0);
1984 if (!ret)
1985 ret = av7110->fe_init(fe);
1986 return ret;
1987}
1988
1989static int av7110_fe_read_status(struct dvb_frontend* fe, fe_status_t* status)
1990{
1991 struct av7110* av7110 = fe->dvb->priv;
1992
1993 /* call the real implementation */
1994 int ret = av7110->fe_read_status(fe, status);
1995 if (!ret)
1996 if (((*status ^ av7110->fe_status) & FE_HAS_LOCK) && (*status & FE_HAS_LOCK))
1997 ret = av7110_fe_lock_fix(av7110, *status);
1998 return ret;
1999}
2000
2001static int av7110_fe_diseqc_reset_overload(struct dvb_frontend* fe)
2002{
2003 struct av7110* av7110 = fe->dvb->priv;
2004
2005 int ret = av7110_fe_lock_fix(av7110, 0);
2006 if (!ret)
2007 ret = av7110->fe_diseqc_reset_overload(fe);
2008 return ret;
2009}
2010
2011static int av7110_fe_diseqc_send_master_cmd(struct dvb_frontend* fe,
2012 struct dvb_diseqc_master_cmd* cmd)
2013{
2014 struct av7110* av7110 = fe->dvb->priv;
2015
2016 int ret = av7110_fe_lock_fix(av7110, 0);
2017 if (!ret) {
2018 av7110->saved_master_cmd = *cmd;
2019 ret = av7110->fe_diseqc_send_master_cmd(fe, cmd);
2020 }
2021 return ret;
2022}
2023
2024static int av7110_fe_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
2025{
2026 struct av7110* av7110 = fe->dvb->priv;
2027
2028 int ret = av7110_fe_lock_fix(av7110, 0);
2029 if (!ret) {
2030 av7110->saved_minicmd = minicmd;
2031 ret = av7110->fe_diseqc_send_burst(fe, minicmd);
2032 }
2033 return ret;
2034}
2035
2036static int av7110_fe_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
2037{
2038 struct av7110* av7110 = fe->dvb->priv;
2039
2040 int ret = av7110_fe_lock_fix(av7110, 0);
2041 if (!ret) {
2042 av7110->saved_tone = tone;
2043 ret = av7110->fe_set_tone(fe, tone);
2044 }
2045 return ret;
2046}
2047
2048static int av7110_fe_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
2049{
2050 struct av7110* av7110 = fe->dvb->priv;
2051
2052 int ret = av7110_fe_lock_fix(av7110, 0);
2053 if (!ret) {
2054 av7110->saved_voltage = voltage;
2055 ret = av7110->fe_set_voltage(fe, voltage);
2056 }
2057 return ret;
2058}
2059
2060static int av7110_fe_dishnetwork_send_legacy_command(struct dvb_frontend* fe, unsigned long cmd)
2061{
2062 struct av7110* av7110 = fe->dvb->priv;
2063
2064 int ret = av7110_fe_lock_fix(av7110, 0);
2065 if (!ret)
2066 ret = av7110->fe_dishnetwork_send_legacy_command(fe, cmd);
2067 return ret;
2068}
2069
2070static void dvb_s_recover(struct av7110* av7110)
2071{
2072 av7110_fe_init(av7110->fe);
2073
2074 av7110_fe_set_voltage(av7110->fe, av7110->saved_voltage);
2075 if (av7110->saved_master_cmd.msg_len) {
2076 msleep(20);
2077 av7110_fe_diseqc_send_master_cmd(av7110->fe, &av7110->saved_master_cmd);
2078 }
2079 msleep(20);
2080 av7110_fe_diseqc_send_burst(av7110->fe, av7110->saved_minicmd);
2081 msleep(20);
2082 av7110_fe_set_tone(av7110->fe, av7110->saved_tone);
2083
2084 av7110_fe_set_frontend(av7110->fe, &av7110->saved_fe_params);
2085}
2086
2087static u8 read_pwm(struct av7110* av7110)
2088{
2089 u8 b = 0xff;
2090 u8 pwm;
2091 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
2092 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
2093
2094 if ((i2c_transfer(&av7110->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
2095 pwm = 0x48;
2096
2097 return pwm;
2098}
2099
2100static int frontend_init(struct av7110 *av7110)
2101{
2102 int ret;
2103
2104 if (av7110->dev->pci->subsystem_vendor == 0x110a) {
2105 switch(av7110->dev->pci->subsystem_device) {
2106 case 0x0000: // Fujitsu/Siemens DVB-Cable (ves1820/Philips CD1516(??))
2107 av7110->fe = dvb_attach(ves1820_attach, &philips_cd1516_config,
2108 &av7110->i2c_adap, read_pwm(av7110));
2109 if (av7110->fe) {
2110 av7110->fe->ops.tuner_ops.set_params = philips_cd1516_tuner_set_params;
2111 }
2112 break;
2113 }
2114
2115 } else if (av7110->dev->pci->subsystem_vendor == 0x13c2) {
2116 switch(av7110->dev->pci->subsystem_device) {
2117 case 0x0000: // Hauppauge/TT WinTV DVB-S rev1.X
2118 case 0x0003: // Hauppauge/TT WinTV Nexus-S Rev 2.X
2119 case 0x1002: // Hauppauge/TT WinTV DVB-S rev1.3SE
2120
2121 // try the ALPS BSRV2 first of all
2122 av7110->fe = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &av7110->i2c_adap);
2123 if (av7110->fe) {
2124 av7110->fe->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
2125 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2126 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2127 av7110->fe->ops.set_tone = av7110_set_tone;
2128 av7110->recover = dvb_s_recover;
2129 break;
2130 }
2131
2132 // try the ALPS BSRU6 now
2133 av7110->fe = dvb_attach(stv0299_attach, &alps_bsru6_config, &av7110->i2c_adap);
2134 if (av7110->fe) {
2135 av7110->fe->ops.tuner_ops.set_params = alps_bsru6_tuner_set_params;
2136 av7110->fe->tuner_priv = &av7110->i2c_adap;
2137
2138 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2139 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2140 av7110->fe->ops.set_tone = av7110_set_tone;
2141 av7110->recover = dvb_s_recover;
2142 break;
2143 }
2144
2145 // Try the grundig 29504-451
2146 av7110->fe = dvb_attach(tda8083_attach, &grundig_29504_451_config, &av7110->i2c_adap);
2147 if (av7110->fe) {
2148 av7110->fe->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
2149 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2150 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2151 av7110->fe->ops.set_tone = av7110_set_tone;
2152 av7110->recover = dvb_s_recover;
2153 break;
2154 }
2155
2156 /* Try DVB-C cards */
2157 switch(av7110->dev->pci->subsystem_device) {
2158 case 0x0000:
2159 /* Siemens DVB-C (full-length card) VES1820/Philips CD1516 */
2160 av7110->fe = dvb_attach(ves1820_attach, &philips_cd1516_config, &av7110->i2c_adap,
2161 read_pwm(av7110));
2162 if (av7110->fe) {
2163 av7110->fe->ops.tuner_ops.set_params = philips_cd1516_tuner_set_params;
2164 }
2165 break;
2166 case 0x0003:
2167 /* Hauppauge DVB-C 2.1 VES1820/ALPS TDBE2 */
2168 av7110->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &av7110->i2c_adap,
2169 read_pwm(av7110));
2170 if (av7110->fe) {
2171 av7110->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
2172 }
2173 break;
2174 }
2175 break;
2176
2177 case 0x0001: // Hauppauge/TT Nexus-T premium rev1.X
2178 // try ALPS TDLB7 first, then Grundig 29504-401
2179 av7110->fe = dvb_attach(sp8870_attach, &alps_tdlb7_config, &av7110->i2c_adap);
2180 if (av7110->fe) {
2181 av7110->fe->ops.tuner_ops.set_params = alps_tdlb7_tuner_set_params;
2182 break;
2183 }
2184 /* fall-thru */
2185
2186 case 0x0008: // Hauppauge/TT DVB-T
2187 // Grundig 29504-401
2188 av7110->fe = dvb_attach(l64781_attach, &grundig_29504_401_config, &av7110->i2c_adap);
2189 if (av7110->fe)
2190 av7110->fe->ops.tuner_ops.set_params = grundig_29504_401_tuner_set_params;
2191 break;
2192
2193 case 0x0002: // Hauppauge/TT DVB-C premium rev2.X
2194
2195 av7110->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &av7110->i2c_adap, read_pwm(av7110));
2196 if (av7110->fe) {
2197 av7110->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
2198 }
2199 break;
2200
2201 case 0x0004: // Galaxis DVB-S rev1.3
2202 /* ALPS BSRV2 */
2203 av7110->fe = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &av7110->i2c_adap);
2204 if (av7110->fe) {
2205 av7110->fe->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
2206 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2207 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2208 av7110->fe->ops.set_tone = av7110_set_tone;
2209 av7110->recover = dvb_s_recover;
2210 }
2211 break;
2212
2213 case 0x0006: /* Fujitsu-Siemens DVB-S rev 1.6 */
2214 /* Grundig 29504-451 */
2215 av7110->fe = dvb_attach(tda8083_attach, &grundig_29504_451_config, &av7110->i2c_adap);
2216 if (av7110->fe) {
2217 av7110->fe->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
2218 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2219 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2220 av7110->fe->ops.set_tone = av7110_set_tone;
2221 av7110->recover = dvb_s_recover;
2222 }
2223 break;
2224
2225 case 0x000A: // Hauppauge/TT Nexus-CA rev1.X
2226
2227 av7110->fe = dvb_attach(stv0297_attach, &nexusca_stv0297_config, &av7110->i2c_adap);
2228 if (av7110->fe) {
2229 av7110->fe->ops.tuner_ops.set_params = nexusca_stv0297_tuner_set_params;
2230
2231 /* set TDA9819 into DVB mode */
2232 saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
2233 saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
2234
2235 /* tuner on this needs a slower i2c bus speed */
2236 av7110->dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
2237 break;
2238 }
2239 break;
2240
2241 case 0x000E: /* Hauppauge/TT Nexus-S rev 2.3 */
2242 /* ALPS BSBE1 */
2243 av7110->fe = dvb_attach(stv0299_attach, &alps_bsbe1_config, &av7110->i2c_adap);
2244 if (av7110->fe) {
2245 av7110->fe->ops.tuner_ops.set_params = alps_bsbe1_tuner_set_params;
2246 av7110->fe->tuner_priv = &av7110->i2c_adap;
2247
2248 if (dvb_attach(lnbp21_attach, av7110->fe, &av7110->i2c_adap, 0, 0) == NULL) {
2249 printk("dvb-ttpci: LNBP21 not found!\n");
2250 if (av7110->fe->ops.release)
2251 av7110->fe->ops.release(av7110->fe);
2252 av7110->fe = NULL;
2253 } else {
2254 av7110->fe->ops.dishnetwork_send_legacy_command = NULL;
2255 av7110->recover = dvb_s_recover;
2256 }
2257 }
2258 break;
2259 }
2260 }
2261
2262 if (!av7110->fe) {
2263 /* FIXME: propagate the failure code from the lower layers */
2264 ret = -ENOMEM;
2265 printk("dvb-ttpci: A frontend driver was not found for device [%04x:%04x] subsystem [%04x:%04x]\n",
2266 av7110->dev->pci->vendor,
2267 av7110->dev->pci->device,
2268 av7110->dev->pci->subsystem_vendor,
2269 av7110->dev->pci->subsystem_device);
2270 } else {
2271 FE_FUNC_OVERRIDE(av7110->fe->ops.init, av7110->fe_init, av7110_fe_init);
2272 FE_FUNC_OVERRIDE(av7110->fe->ops.read_status, av7110->fe_read_status, av7110_fe_read_status);
2273 FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_reset_overload, av7110->fe_diseqc_reset_overload, av7110_fe_diseqc_reset_overload);
2274 FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_send_master_cmd, av7110->fe_diseqc_send_master_cmd, av7110_fe_diseqc_send_master_cmd);
2275 FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_send_burst, av7110->fe_diseqc_send_burst, av7110_fe_diseqc_send_burst);
2276 FE_FUNC_OVERRIDE(av7110->fe->ops.set_tone, av7110->fe_set_tone, av7110_fe_set_tone);
2277 FE_FUNC_OVERRIDE(av7110->fe->ops.set_voltage, av7110->fe_set_voltage, av7110_fe_set_voltage);
2278 FE_FUNC_OVERRIDE(av7110->fe->ops.dishnetwork_send_legacy_command, av7110->fe_dishnetwork_send_legacy_command, av7110_fe_dishnetwork_send_legacy_command);
2279 FE_FUNC_OVERRIDE(av7110->fe->ops.set_frontend, av7110->fe_set_frontend, av7110_fe_set_frontend);
2280
2281 ret = dvb_register_frontend(&av7110->dvb_adapter, av7110->fe);
2282 if (ret < 0) {
2283 printk("av7110: Frontend registration failed!\n");
2284 dvb_frontend_detach(av7110->fe);
2285 av7110->fe = NULL;
2286 }
2287 }
2288 return ret;
2289}
2290
2291/* Budgetpatch note:
2292 * Original hardware design by Roberto Deza:
2293 * There is a DVB_Wiki at
2294 * http://www.linuxtv.org/
2295 *
2296 * New software triggering design by Emard that works on
2297 * original Roberto Deza's hardware:
2298 *
2299 * rps1 code for budgetpatch will copy internal HS event to GPIO3 pin.
2300 * GPIO3 is in budget-patch hardware connectd to port B VSYNC
2301 * HS is an internal event of 7146, accessible with RPS
2302 * and temporarily raised high every n lines
2303 * (n in defined in the RPS_THRESH1 counter threshold)
2304 * I think HS is raised high on the beginning of the n-th line
2305 * and remains high until this n-th line that triggered
2306 * it is completely received. When the receiption of n-th line
2307 * ends, HS is lowered.
2308 *
2309 * To transmit data over DMA, 7146 needs changing state at
2310 * port B VSYNC pin. Any changing of port B VSYNC will
2311 * cause some DMA data transfer, with more or less packets loss.
2312 * It depends on the phase and frequency of VSYNC and
2313 * the way of 7146 is instructed to trigger on port B (defined
2314 * in DD1_INIT register, 3rd nibble from the right valid
2315 * numbers are 0-7, see datasheet)
2316 *
2317 * The correct triggering can minimize packet loss,
2318 * dvbtraffic should give this stable bandwidths:
2319 * 22k transponder = 33814 kbit/s
2320 * 27.5k transponder = 38045 kbit/s
2321 * by experiment it is found that the best results
2322 * (stable bandwidths and almost no packet loss)
2323 * are obtained using DD1_INIT triggering number 2
2324 * (Va at rising edge of VS Fa = HS x VS-failing forced toggle)
2325 * and a VSYNC phase that occurs in the middle of DMA transfer
2326 * (about byte 188*512=96256 in the DMA window).
2327 *
2328 * Phase of HS is still not clear to me how to control,
2329 * It just happens to be so. It can be seen if one enables
2330 * RPS_IRQ and print Event Counter 1 in vpeirq(). Every
2331 * time RPS_INTERRUPT is called, the Event Counter 1 will
2332 * increment. That's how the 7146 is programmed to do event
2333 * counting in this budget-patch.c
2334 * I *think* HPS setting has something to do with the phase
2335 * of HS but I can't be 100% sure in that.
2336 *
2337 * hardware debug note: a working budget card (including budget patch)
2338 * with vpeirq() interrupt setup in mode "0x90" (every 64K) will
2339 * generate 3 interrupts per 25-Hz DMA frame of 2*188*512 bytes
2340 * and that means 3*25=75 Hz of interrupt freqency, as seen by
2341 * watch cat /proc/interrupts
2342 *
2343 * If this frequency is 3x lower (and data received in the DMA
2344 * buffer don't start with 0x47, but in the middle of packets,
2345 * whose lengths appear to be like 188 292 188 104 etc.
2346 * this means VSYNC line is not connected in the hardware.
2347 * (check soldering pcb and pins)
2348 * The same behaviour of missing VSYNC can be duplicated on budget
2349 * cards, by seting DD1_INIT trigger mode 7 in 3rd nibble.
2350 */
2351static int __devinit av7110_attach(struct saa7146_dev* dev,
2352 struct saa7146_pci_extension_data *pci_ext)
2353{
2354 const int length = TS_WIDTH * TS_HEIGHT;
2355 struct pci_dev *pdev = dev->pci;
2356 struct av7110 *av7110;
2357 struct task_struct *thread;
2358 int ret, count = 0;
2359
2360 dprintk(4, "dev: %p\n", dev);
2361
2362 /* Set RPS_IRQ to 1 to track rps1 activity.
2363 * Enabling this won't send any interrupt to PC CPU.
2364 */
2365#define RPS_IRQ 0
2366
2367 if (budgetpatch == 1) {
2368 budgetpatch = 0;
2369 /* autodetect the presence of budget patch
2370 * this only works if saa7146 has been recently
2371 * reset with with MASK_31 to MC1
2372 *
2373 * will wait for VBI_B event (vertical blank at port B)
2374 * and will reset GPIO3 after VBI_B is detected.
2375 * (GPIO3 should be raised high by CPU to
2376 * test if GPIO3 will generate vertical blank signal
2377 * in budget patch GPIO3 is connected to VSYNC_B
2378 */
2379
2380 /* RESET SAA7146 */
2381 saa7146_write(dev, MC1, MASK_31);
2382 /* autodetection success seems to be time-dependend after reset */
2383
2384 /* Fix VSYNC level */
2385 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2386 /* set vsync_b triggering */
2387 saa7146_write(dev, DD1_STREAM_B, 0);
2388 /* port B VSYNC at rising edge */
2389 saa7146_write(dev, DD1_INIT, 0x00000200);
2390 saa7146_write(dev, BRS_CTRL, 0x00000000); // VBI
2391 saa7146_write(dev, MC2,
2392 1 * (MASK_08 | MASK_24) | // BRS control
2393 0 * (MASK_09 | MASK_25) | // a
2394 1 * (MASK_10 | MASK_26) | // b
2395 0 * (MASK_06 | MASK_22) | // HPS_CTRL1
2396 0 * (MASK_05 | MASK_21) | // HPS_CTRL2
2397 0 * (MASK_01 | MASK_15) // DEBI
2398 );
2399
2400 /* start writing RPS1 code from beginning */
2401 count = 0;
2402 /* Disable RPS1 */
2403 saa7146_write(dev, MC1, MASK_29);
2404 /* RPS1 timeout disable */
2405 saa7146_write(dev, RPS_TOV1, 0);
2406 WRITE_RPS1(CMD_PAUSE | EVT_VBI_B);
2407 WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
2408 WRITE_RPS1(GPIO3_MSK);
2409 WRITE_RPS1(SAA7146_GPIO_OUTLO<<24);
2410#if RPS_IRQ
2411 /* issue RPS1 interrupt to increment counter */
2412 WRITE_RPS1(CMD_INTERRUPT);
2413#endif
2414 WRITE_RPS1(CMD_STOP);
2415 /* Jump to begin of RPS program as safety measure (p37) */
2416 WRITE_RPS1(CMD_JUMP);
2417 WRITE_RPS1(dev->d_rps1.dma_handle);
2418
2419#if RPS_IRQ
2420 /* set event counter 1 source as RPS1 interrupt (0x03) (rE4 p53)
2421 * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2422 * use 0x15 to track VPE interrupts - increase by 1 every vpeirq() is called
2423 */
2424 saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2425 /* set event counter 1 threshold to maximum allowed value (rEC p55) */
2426 saa7146_write(dev, ECT1R, 0x3fff );
2427#endif
2428 /* Set RPS1 Address register to point to RPS code (r108 p42) */
2429 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2430 /* Enable RPS1, (rFC p33) */
2431 saa7146_write(dev, MC1, (MASK_13 | MASK_29 ));
2432
2433 mdelay(10);
2434 /* now send VSYNC_B to rps1 by rising GPIO3 */
2435 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
2436 mdelay(10);
2437 /* if rps1 responded by lowering the GPIO3,
2438 * then we have budgetpatch hardware
2439 */
2440 if ((saa7146_read(dev, GPIO_CTRL) & 0x10000000) == 0) {
2441 budgetpatch = 1;
2442 printk("dvb-ttpci: BUDGET-PATCH DETECTED.\n");
2443 }
2444 /* Disable RPS1 */
2445 saa7146_write(dev, MC1, ( MASK_29 ));
2446#if RPS_IRQ
2447 printk("dvb-ttpci: Event Counter 1 0x%04x\n", saa7146_read(dev, EC1R) & 0x3fff );
2448#endif
2449 }
2450
2451 /* prepare the av7110 device struct */
2452 av7110 = kzalloc(sizeof(struct av7110), GFP_KERNEL);
2453 if (!av7110) {
2454 dprintk(1, "out of memory\n");
2455 return -ENOMEM;
2456 }
2457
2458 av7110->card_name = (char*) pci_ext->ext_priv;
2459 av7110->dev = dev;
2460 dev->ext_priv = av7110;
2461
2462 ret = get_firmware(av7110);
2463 if (ret < 0)
2464 goto err_kfree_0;
2465
2466 ret = dvb_register_adapter(&av7110->dvb_adapter, av7110->card_name,
2467 THIS_MODULE, &dev->pci->dev, adapter_nr);
2468 if (ret < 0)
2469 goto err_put_firmware_1;
2470
2471 /* the Siemens DVB needs this if you want to have the i2c chips
2472 get recognized before the main driver is fully loaded */
2473 saa7146_write(dev, GPIO_CTRL, 0x500000);
2474
2475 strlcpy(av7110->i2c_adap.name, pci_ext->ext_priv, sizeof(av7110->i2c_adap.name));
2476
2477 saa7146_i2c_adapter_prepare(dev, &av7110->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120); /* 275 kHz */
2478
2479 ret = i2c_add_adapter(&av7110->i2c_adap);
2480 if (ret < 0)
2481 goto err_dvb_unregister_adapter_2;
2482
2483 ttpci_eeprom_parse_mac(&av7110->i2c_adap,
2484 av7110->dvb_adapter.proposed_mac);
2485 ret = -ENOMEM;
2486
2487 /* full-ts mod? */
2488 if (full_ts)
2489 av7110->full_ts = true;
2490
2491 /* check for full-ts flag in eeprom */
2492 if (i2c_readreg(av7110, 0xaa, 0) == 0x4f && i2c_readreg(av7110, 0xaa, 1) == 0x45) {
2493 u8 flags = i2c_readreg(av7110, 0xaa, 2);
2494 if (flags != 0xff && (flags & 0x01))
2495 av7110->full_ts = true;
2496 }
2497
2498 if (av7110->full_ts) {
2499 printk(KERN_INFO "dvb-ttpci: full-ts mode enabled for saa7146 port B\n");
2500 spin_lock_init(&av7110->feedlock1);
2501 av7110->grabbing = saa7146_vmalloc_build_pgtable(pdev, length,
2502 &av7110->pt);
2503 if (!av7110->grabbing)
2504 goto err_i2c_del_3;
2505
2506 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2507 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
2508
2509 saa7146_write(dev, DD1_INIT, 0x00000600);
2510 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2511
2512 saa7146_write(dev, BRS_CTRL, 0x60000000);
2513 saa7146_write(dev, MC2, MASK_08 | MASK_24);
2514
2515 /* dma3 */
2516 saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
2517 saa7146_write(dev, BASE_ODD3, 0);
2518 saa7146_write(dev, BASE_EVEN3, 0);
2519 saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
2520 saa7146_write(dev, PITCH3, TS_WIDTH);
2521 saa7146_write(dev, BASE_PAGE3, av7110->pt.dma | ME1 | 0x90);
2522 saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
2523 saa7146_write(dev, MC2, MASK_04 | MASK_20);
2524
2525 tasklet_init(&av7110->vpe_tasklet, vpeirq, (unsigned long) av7110);
2526
2527 } else if (budgetpatch) {
2528 spin_lock_init(&av7110->feedlock1);
2529 av7110->grabbing = saa7146_vmalloc_build_pgtable(pdev, length,
2530 &av7110->pt);
2531 if (!av7110->grabbing)
2532 goto err_i2c_del_3;
2533
2534 saa7146_write(dev, PCI_BT_V1, 0x1c1f101f);
2535 saa7146_write(dev, BCS_CTRL, 0x80400040);
2536 /* set dd1 stream a & b */
2537 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2538 saa7146_write(dev, DD1_INIT, 0x03000200);
2539 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2540 saa7146_write(dev, BRS_CTRL, 0x60000000);
2541 saa7146_write(dev, BASE_ODD3, 0);
2542 saa7146_write(dev, BASE_EVEN3, 0);
2543 saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
2544 saa7146_write(dev, BASE_PAGE3, av7110->pt.dma | ME1 | 0x90);
2545
2546 saa7146_write(dev, PITCH3, TS_WIDTH);
2547 saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
2548
2549 /* upload all */
2550 saa7146_write(dev, MC2, 0x077c077c);
2551 saa7146_write(dev, GPIO_CTRL, 0x000000);
2552#if RPS_IRQ
2553 /* set event counter 1 source as RPS1 interrupt (0x03) (rE4 p53)
2554 * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2555 * use 0x15 to track VPE interrupts - increase by 1 every vpeirq() is called
2556 */
2557 saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2558 /* set event counter 1 threshold to maximum allowed value (rEC p55) */
2559 saa7146_write(dev, ECT1R, 0x3fff );
2560#endif
2561 /* Setup BUDGETPATCH MAIN RPS1 "program" (p35) */
2562 count = 0;
2563
2564 /* Wait Source Line Counter Threshold (p36) */
2565 WRITE_RPS1(CMD_PAUSE | EVT_HS);
2566 /* Set GPIO3=1 (p42) */
2567 WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
2568 WRITE_RPS1(GPIO3_MSK);
2569 WRITE_RPS1(SAA7146_GPIO_OUTHI<<24);
2570#if RPS_IRQ
2571 /* issue RPS1 interrupt */
2572 WRITE_RPS1(CMD_INTERRUPT);
2573#endif
2574 /* Wait reset Source Line Counter Threshold (p36) */
2575 WRITE_RPS1(CMD_PAUSE | RPS_INV | EVT_HS);
2576 /* Set GPIO3=0 (p42) */
2577 WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
2578 WRITE_RPS1(GPIO3_MSK);
2579 WRITE_RPS1(SAA7146_GPIO_OUTLO<<24);
2580#if RPS_IRQ
2581 /* issue RPS1 interrupt */
2582 WRITE_RPS1(CMD_INTERRUPT);
2583#endif
2584 /* Jump to begin of RPS program (p37) */
2585 WRITE_RPS1(CMD_JUMP);
2586 WRITE_RPS1(dev->d_rps1.dma_handle);
2587
2588 /* Fix VSYNC level */
2589 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2590 /* Set RPS1 Address register to point to RPS code (r108 p42) */
2591 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2592 /* Set Source Line Counter Threshold, using BRS (rCC p43)
2593 * It generates HS event every TS_HEIGHT lines
2594 * this is related to TS_WIDTH set in register
2595 * NUM_LINE_BYTE3. If NUM_LINE_BYTE low 16 bits
2596 * are set to TS_WIDTH bytes (TS_WIDTH=2*188),
2597 * then RPS_THRESH1 should be set to trigger
2598 * every TS_HEIGHT (512) lines.
2599 */
2600 saa7146_write(dev, RPS_THRESH1, (TS_HEIGHT*1) | MASK_12 );
2601
2602 /* Enable RPS1 (rFC p33) */
2603 saa7146_write(dev, MC1, (MASK_13 | MASK_29));
2604
2605 /* end of budgetpatch register initialization */
2606 tasklet_init (&av7110->vpe_tasklet, vpeirq, (unsigned long) av7110);
2607 } else {
2608 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
2609 saa7146_write(dev, BCS_CTRL, 0x80400040);
2610
2611 /* set dd1 stream a & b */
2612 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2613 saa7146_write(dev, DD1_INIT, 0x03000000);
2614 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2615
2616 /* upload all */
2617 saa7146_write(dev, MC2, 0x077c077c);
2618 saa7146_write(dev, GPIO_CTRL, 0x000000);
2619 }
2620
2621 tasklet_init (&av7110->debi_tasklet, debiirq, (unsigned long) av7110);
2622 tasklet_init (&av7110->gpio_tasklet, gpioirq, (unsigned long) av7110);
2623
2624 mutex_init(&av7110->pid_mutex);
2625
2626 /* locks for data transfers from/to AV7110 */
2627 spin_lock_init(&av7110->debilock);
2628 mutex_init(&av7110->dcomlock);
2629 av7110->debitype = -1;
2630
2631 /* default OSD window */
2632 av7110->osdwin = 1;
2633 mutex_init(&av7110->osd_mutex);
2634
2635 /* TV standard */
2636 av7110->vidmode = tv_standard == 1 ? AV7110_VIDEO_MODE_NTSC
2637 : AV7110_VIDEO_MODE_PAL;
2638
2639 /* ARM "watchdog" */
2640 init_waitqueue_head(&av7110->arm_wait);
2641 av7110->arm_thread = NULL;
2642
2643 /* allocate and init buffers */
2644 av7110->debi_virt = pci_alloc_consistent(pdev, 8192, &av7110->debi_bus);
2645 if (!av7110->debi_virt)
2646 goto err_saa71466_vfree_4;
2647
2648
2649 av7110->iobuf = vmalloc(AVOUTLEN+AOUTLEN+BMPLEN+4*IPACKS);
2650 if (!av7110->iobuf)
2651 goto err_pci_free_5;
2652
2653 ret = av7110_av_init(av7110);
2654 if (ret < 0)
2655 goto err_iobuf_vfree_6;
2656
2657 /* init BMP buffer */
2658 av7110->bmpbuf = av7110->iobuf+AVOUTLEN+AOUTLEN;
2659 init_waitqueue_head(&av7110->bmpq);
2660
2661 ret = av7110_ca_init(av7110);
2662 if (ret < 0)
2663 goto err_av7110_av_exit_7;
2664
2665 /* load firmware into AV7110 cards */
2666 ret = av7110_bootarm(av7110);
2667 if (ret < 0)
2668 goto err_av7110_ca_exit_8;
2669
2670 ret = av7110_firmversion(av7110);
2671 if (ret < 0)
2672 goto err_stop_arm_9;
2673
2674 if (FW_VERSION(av7110->arm_app)<0x2501)
2675 printk ("dvb-ttpci: Warning, firmware version 0x%04x is too old. "
2676 "System might be unstable!\n", FW_VERSION(av7110->arm_app));
2677
2678 thread = kthread_run(arm_thread, (void *) av7110, "arm_mon");
2679 if (IS_ERR(thread)) {
2680 ret = PTR_ERR(thread);
2681 goto err_stop_arm_9;
2682 }
2683 av7110->arm_thread = thread;
2684
2685 /* set initial volume in mixer struct */
2686 av7110->mixer.volume_left = volume;
2687 av7110->mixer.volume_right = volume;
2688
2689 ret = av7110_register(av7110);
2690 if (ret < 0)
2691 goto err_arm_thread_stop_10;
2692
2693 init_av7110_av(av7110);
2694
2695 /* special case DVB-C: these cards have an analog tuner
2696 plus need some special handling, so we have separate
2697 saa7146_ext_vv data for these... */
2698 ret = av7110_init_v4l(av7110);
2699 if (ret < 0)
2700 goto err_av7110_unregister_11;
2701
2702 av7110->dvb_adapter.priv = av7110;
2703 ret = frontend_init(av7110);
2704 if (ret < 0)
2705 goto err_av7110_exit_v4l_12;
2706
2707#if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
2708 av7110_ir_init(av7110);
2709#endif
2710 printk(KERN_INFO "dvb-ttpci: found av7110-%d.\n", av7110_num);
2711 av7110_num++;
2712out:
2713 return ret;
2714
2715err_av7110_exit_v4l_12:
2716 av7110_exit_v4l(av7110);
2717err_av7110_unregister_11:
2718 dvb_unregister(av7110);
2719err_arm_thread_stop_10:
2720 av7110_arm_sync(av7110);
2721err_stop_arm_9:
2722 /* Nothing to do. Rejoice. */
2723err_av7110_ca_exit_8:
2724 av7110_ca_exit(av7110);
2725err_av7110_av_exit_7:
2726 av7110_av_exit(av7110);
2727err_iobuf_vfree_6:
2728 vfree(av7110->iobuf);
2729err_pci_free_5:
2730 pci_free_consistent(pdev, 8192, av7110->debi_virt, av7110->debi_bus);
2731err_saa71466_vfree_4:
2732 if (av7110->grabbing)
2733 saa7146_vfree_destroy_pgtable(pdev, av7110->grabbing, &av7110->pt);
2734err_i2c_del_3:
2735 i2c_del_adapter(&av7110->i2c_adap);
2736err_dvb_unregister_adapter_2:
2737 dvb_unregister_adapter(&av7110->dvb_adapter);
2738err_put_firmware_1:
2739 put_firmware(av7110);
2740err_kfree_0:
2741 kfree(av7110);
2742 goto out;
2743}
2744
2745static int __devexit av7110_detach(struct saa7146_dev* saa)
2746{
2747 struct av7110 *av7110 = saa->ext_priv;
2748 dprintk(4, "%p\n", av7110);
2749
2750#if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
2751 av7110_ir_exit(av7110);
2752#endif
2753 if (budgetpatch || av7110->full_ts) {
2754 if (budgetpatch) {
2755 /* Disable RPS1 */
2756 saa7146_write(saa, MC1, MASK_29);
2757 /* VSYNC LOW (inactive) */
2758 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
2759 }
2760 saa7146_write(saa, MC1, MASK_20); /* DMA3 off */
2761 SAA7146_IER_DISABLE(saa, MASK_10);
2762 SAA7146_ISR_CLEAR(saa, MASK_10);
2763 msleep(50);
2764 tasklet_kill(&av7110->vpe_tasklet);
2765 saa7146_vfree_destroy_pgtable(saa->pci, av7110->grabbing, &av7110->pt);
2766 }
2767 av7110_exit_v4l(av7110);
2768
2769 av7110_arm_sync(av7110);
2770
2771 tasklet_kill(&av7110->debi_tasklet);
2772 tasklet_kill(&av7110->gpio_tasklet);
2773
2774 dvb_unregister(av7110);
2775
2776 SAA7146_IER_DISABLE(saa, MASK_19 | MASK_03);
2777 SAA7146_ISR_CLEAR(saa, MASK_19 | MASK_03);
2778
2779 av7110_ca_exit(av7110);
2780 av7110_av_exit(av7110);
2781
2782 vfree(av7110->iobuf);
2783 pci_free_consistent(saa->pci, 8192, av7110->debi_virt,
2784 av7110->debi_bus);
2785
2786 i2c_del_adapter(&av7110->i2c_adap);
2787
2788 dvb_unregister_adapter (&av7110->dvb_adapter);
2789
2790 av7110_num--;
2791
2792 put_firmware(av7110);
2793
2794 kfree(av7110);
2795
2796 saa->ext_priv = NULL;
2797
2798 return 0;
2799}
2800
2801
2802static void av7110_irq(struct saa7146_dev* dev, u32 *isr)
2803{
2804 struct av7110 *av7110 = dev->ext_priv;
2805
2806 //print_time("av7110_irq");
2807
2808 /* Note: Don't try to handle the DEBI error irq (MASK_18), in
2809 * intel mode the timeout is asserted all the time...
2810 */
2811
2812 if (*isr & MASK_19) {
2813 //printk("av7110_irq: DEBI\n");
2814 /* Note 1: The DEBI irq is level triggered: We must enable it
2815 * only after we started a DMA xfer, and disable it here
2816 * immediately, or it will be signalled all the time while
2817 * DEBI is idle.
2818 * Note 2: You would think that an irq which is masked is
2819 * not signalled by the hardware. Not so for the SAA7146:
2820 * An irq is signalled as long as the corresponding bit
2821 * in the ISR is set, and disabling irqs just prevents the
2822 * hardware from setting the ISR bit. This means a) that we
2823 * must clear the ISR *after* disabling the irq (which is why
2824 * we must do it here even though saa7146_core did it already),
2825 * and b) that if we were to disable an edge triggered irq
2826 * (like the gpio irqs sadly are) temporarily we would likely
2827 * loose some. This sucks :-(
2828 */
2829 SAA7146_IER_DISABLE(av7110->dev, MASK_19);
2830 SAA7146_ISR_CLEAR(av7110->dev, MASK_19);
2831 tasklet_schedule(&av7110->debi_tasklet);
2832 }
2833
2834 if (*isr & MASK_03) {
2835 //printk("av7110_irq: GPIO\n");
2836 tasklet_schedule(&av7110->gpio_tasklet);
2837 }
2838
2839 if (*isr & MASK_10)
2840 tasklet_schedule(&av7110->vpe_tasklet);
2841}
2842
2843
2844static struct saa7146_extension av7110_extension_driver;
2845
2846#define MAKE_AV7110_INFO(x_var,x_name) \
2847static struct saa7146_pci_extension_data x_var = { \
2848 .ext_priv = x_name, \
2849 .ext = &av7110_extension_driver }
2850
2851MAKE_AV7110_INFO(tts_1_X_fsc,"Technotrend/Hauppauge WinTV DVB-S rev1.X or Fujitsu Siemens DVB-C");
2852MAKE_AV7110_INFO(ttt_1_X, "Technotrend/Hauppauge WinTV DVB-T rev1.X");
2853MAKE_AV7110_INFO(ttc_1_X, "Technotrend/Hauppauge WinTV Nexus-CA rev1.X");
2854MAKE_AV7110_INFO(ttc_2_X, "Technotrend/Hauppauge WinTV DVB-C rev2.X");
2855MAKE_AV7110_INFO(tts_2_X, "Technotrend/Hauppauge WinTV Nexus-S rev2.X");
2856MAKE_AV7110_INFO(tts_2_3, "Technotrend/Hauppauge WinTV Nexus-S rev2.3");
2857MAKE_AV7110_INFO(tts_1_3se, "Technotrend/Hauppauge WinTV DVB-S rev1.3 SE");
2858MAKE_AV7110_INFO(ttt, "Technotrend/Hauppauge DVB-T");
2859MAKE_AV7110_INFO(fsc, "Fujitsu Siemens DVB-C");
2860MAKE_AV7110_INFO(fss, "Fujitsu Siemens DVB-S rev1.6");
2861MAKE_AV7110_INFO(gxs_1_3, "Galaxis DVB-S rev1.3");
2862
2863static struct pci_device_id pci_tbl[] = {
2864 MAKE_EXTENSION_PCI(fsc, 0x110a, 0x0000),
2865 MAKE_EXTENSION_PCI(tts_1_X_fsc, 0x13c2, 0x0000),
2866 MAKE_EXTENSION_PCI(ttt_1_X, 0x13c2, 0x0001),
2867 MAKE_EXTENSION_PCI(ttc_2_X, 0x13c2, 0x0002),
2868 MAKE_EXTENSION_PCI(tts_2_X, 0x13c2, 0x0003),
2869 MAKE_EXTENSION_PCI(gxs_1_3, 0x13c2, 0x0004),
2870 MAKE_EXTENSION_PCI(fss, 0x13c2, 0x0006),
2871 MAKE_EXTENSION_PCI(ttt, 0x13c2, 0x0008),
2872 MAKE_EXTENSION_PCI(ttc_1_X, 0x13c2, 0x000a),
2873 MAKE_EXTENSION_PCI(tts_2_3, 0x13c2, 0x000e),
2874 MAKE_EXTENSION_PCI(tts_1_3se, 0x13c2, 0x1002),
2875
2876/* MAKE_EXTENSION_PCI(???, 0x13c2, 0x0005), UNDEFINED CARD */ // Technisat SkyStar1
2877/* MAKE_EXTENSION_PCI(???, 0x13c2, 0x0009), UNDEFINED CARD */ // TT/Hauppauge WinTV Nexus-CA v????
2878
2879 {
2880 .vendor = 0,
2881 }
2882};
2883
2884MODULE_DEVICE_TABLE(pci, pci_tbl);
2885
2886
2887static struct saa7146_extension av7110_extension_driver = {
2888 .name = "av7110",
2889 .flags = SAA7146_USE_I2C_IRQ,
2890
2891 .module = THIS_MODULE,
2892 .pci_tbl = &pci_tbl[0],
2893 .attach = av7110_attach,
2894 .detach = __devexit_p(av7110_detach),
2895
2896 .irq_mask = MASK_19 | MASK_03 | MASK_10,
2897 .irq_func = av7110_irq,
2898};
2899
2900
2901static int __init av7110_init(void)
2902{
2903 int retval;
2904 retval = saa7146_register_extension(&av7110_extension_driver);
2905 return retval;
2906}
2907
2908
2909static void __exit av7110_exit(void)
2910{
2911 saa7146_unregister_extension(&av7110_extension_driver);
2912}
2913
2914module_init(av7110_init);
2915module_exit(av7110_exit);
2916
2917MODULE_DESCRIPTION("driver for the SAA7146 based AV110 PCI DVB cards by "
2918 "Siemens, Technotrend, Hauppauge");
2919MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
2920MODULE_LICENSE("GPL");
diff --git a/drivers/media/dvb/ttpci/av7110.h b/drivers/media/dvb/ttpci/av7110.h
new file mode 100644
index 00000000000..d85b8512ac3
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110.h
@@ -0,0 +1,315 @@
1#ifndef _AV7110_H_
2#define _AV7110_H_
3
4#include <linux/interrupt.h>
5#include <linux/socket.h>
6#include <linux/netdevice.h>
7#include <linux/i2c.h>
8#include <linux/input.h>
9
10#include <linux/dvb/video.h>
11#include <linux/dvb/audio.h>
12#include <linux/dvb/dmx.h>
13#include <linux/dvb/ca.h>
14#include <linux/dvb/osd.h>
15#include <linux/dvb/net.h>
16#include <linux/mutex.h>
17
18#include "dvbdev.h"
19#include "demux.h"
20#include "dvb_demux.h"
21#include "dmxdev.h"
22#include "dvb_filter.h"
23#include "dvb_net.h"
24#include "dvb_ringbuffer.h"
25#include "dvb_frontend.h"
26#include "ves1820.h"
27#include "ves1x93.h"
28#include "stv0299.h"
29#include "tda8083.h"
30#include "sp8870.h"
31#include "stv0297.h"
32#include "l64781.h"
33
34#include <media/saa7146_vv.h>
35
36
37#define ANALOG_TUNER_VES1820 1
38#define ANALOG_TUNER_STV0297 2
39
40extern int av7110_debug;
41
42#define dprintk(level,args...) \
43 do { if ((av7110_debug & level)) { printk("dvb-ttpci: %s(): ", __func__); printk(args); } } while (0)
44
45#define MAXFILT 32
46
47enum {AV_PES_STREAM, PS_STREAM, TS_STREAM, PES_STREAM};
48
49enum av7110_video_mode {
50 AV7110_VIDEO_MODE_PAL = 0,
51 AV7110_VIDEO_MODE_NTSC = 1
52};
53
54struct av7110_p2t {
55 u8 pes[TS_SIZE];
56 u8 counter;
57 long int pos;
58 int frags;
59 struct dvb_demux_feed *feed;
60};
61
62/* video MPEG decoder events: */
63/* (code copied from dvb_frontend.c, should maybe be factored out...) */
64#define MAX_VIDEO_EVENT 8
65struct dvb_video_events {
66 struct video_event events[MAX_VIDEO_EVENT];
67 int eventw;
68 int eventr;
69 int overflow;
70 wait_queue_head_t wait_queue;
71 spinlock_t lock;
72};
73
74
75struct av7110;
76
77/* infrared remote control */
78struct infrared {
79 u16 key_map[256];
80 struct input_dev *input_dev;
81 char input_phys[32];
82 struct timer_list keyup_timer;
83 struct tasklet_struct ir_tasklet;
84 void (*ir_handler)(struct av7110 *av7110, u32 ircom);
85 u32 ir_command;
86 u32 ir_config;
87 u32 device_mask;
88 u8 protocol;
89 u8 inversion;
90 u16 last_key;
91 u16 last_toggle;
92 u8 delay_timer_finished;
93};
94
95
96/* place to store all the necessary device information */
97struct av7110 {
98
99 /* devices */
100
101 struct dvb_device dvb_dev;
102 struct dvb_net dvb_net;
103
104 struct video_device *v4l_dev;
105 struct video_device *vbi_dev;
106
107 struct saa7146_dev *dev;
108
109 struct i2c_adapter i2c_adap;
110
111 char *card_name;
112
113 /* support for analog module of dvb-c */
114 int analog_tuner_flags;
115 int current_input;
116 u32 current_freq;
117
118 struct tasklet_struct debi_tasklet;
119 struct tasklet_struct gpio_tasklet;
120
121 int adac_type; /* audio DAC type */
122#define DVB_ADAC_TI 0
123#define DVB_ADAC_CRYSTAL 1
124#define DVB_ADAC_MSP34x0 2
125#define DVB_ADAC_MSP34x5 3
126#define DVB_ADAC_NONE -1
127
128
129 /* buffers */
130
131 void *iobuf; /* memory for all buffers */
132 struct dvb_ringbuffer avout; /* buffer for video or A/V mux */
133#define AVOUTLEN (128*1024)
134 struct dvb_ringbuffer aout; /* buffer for audio */
135#define AOUTLEN (64*1024)
136 void *bmpbuf;
137#define BMPLEN (8*32768+1024)
138
139 /* bitmap buffers and states */
140
141 int bmpp;
142 int bmplen;
143 volatile int bmp_state;
144#define BMP_NONE 0
145#define BMP_LOADING 1
146#define BMP_LOADED 2
147 wait_queue_head_t bmpq;
148
149
150 /* DEBI and polled command interface */
151
152 spinlock_t debilock;
153 struct mutex dcomlock;
154 volatile int debitype;
155 volatile int debilen;
156
157
158 /* Recording and playback flags */
159
160 int rec_mode;
161 int playing;
162#define RP_NONE 0
163#define RP_VIDEO 1
164#define RP_AUDIO 2
165#define RP_AV 3
166
167
168 /* OSD */
169
170 int osdwin; /* currently active window */
171 u16 osdbpp[8];
172 struct mutex osd_mutex;
173
174 /* CA */
175
176 ca_slot_info_t ci_slot[2];
177
178 enum av7110_video_mode vidmode;
179 struct dmxdev dmxdev;
180 struct dvb_demux demux;
181
182 struct dmx_frontend hw_frontend;
183 struct dmx_frontend mem_frontend;
184
185 /* for budget mode demux1 */
186 struct dmxdev dmxdev1;
187 struct dvb_demux demux1;
188 struct dvb_net dvb_net1;
189 spinlock_t feedlock1;
190 int feeding1;
191 u32 ttbp;
192 unsigned char *grabbing;
193 struct saa7146_pgtable pt;
194 struct tasklet_struct vpe_tasklet;
195 bool full_ts;
196
197 int fe_synced;
198 struct mutex pid_mutex;
199
200 int video_blank;
201 struct video_status videostate;
202 u16 display_panscan;
203 int display_ar;
204 int trickmode;
205#define TRICK_NONE 0
206#define TRICK_FAST 1
207#define TRICK_SLOW 2
208#define TRICK_FREEZE 3
209 struct audio_status audiostate;
210
211 struct dvb_demux_filter *handle2filter[32];
212 struct av7110_p2t p2t_filter[MAXFILT];
213 struct dvb_filter_pes2ts p2t[2];
214 struct ipack ipack[2];
215 u8 *kbuf[2];
216
217 int sinfo;
218 int feeding;
219
220 int arm_errors;
221 int registered;
222
223
224 /* AV711X */
225
226 u32 arm_fw;
227 u32 arm_rtsl;
228 u32 arm_vid;
229 u32 arm_app;
230 u32 avtype;
231 int arm_ready;
232 struct task_struct *arm_thread;
233 wait_queue_head_t arm_wait;
234 u16 arm_loops;
235
236 void *debi_virt;
237 dma_addr_t debi_bus;
238
239 u16 pids[DMX_PES_OTHER];
240
241 struct dvb_ringbuffer ci_rbuffer;
242 struct dvb_ringbuffer ci_wbuffer;
243
244 struct audio_mixer mixer;
245
246 struct dvb_adapter dvb_adapter;
247 struct dvb_device *video_dev;
248 struct dvb_device *audio_dev;
249 struct dvb_device *ca_dev;
250 struct dvb_device *osd_dev;
251
252 struct dvb_video_events video_events;
253 video_size_t video_size;
254
255 u16 wssMode;
256 u16 wssData;
257
258 struct infrared ir;
259
260 /* firmware stuff */
261 unsigned char *bin_fw;
262 unsigned long size_fw;
263
264 unsigned char *bin_dpram;
265 unsigned long size_dpram;
266
267 unsigned char *bin_root;
268 unsigned long size_root;
269
270 struct dvb_frontend* fe;
271 fe_status_t fe_status;
272
273 /* crash recovery */
274 void (*recover)(struct av7110* av7110);
275 struct dvb_frontend_parameters saved_fe_params;
276 fe_sec_voltage_t saved_voltage;
277 fe_sec_tone_mode_t saved_tone;
278 struct dvb_diseqc_master_cmd saved_master_cmd;
279 fe_sec_mini_cmd_t saved_minicmd;
280
281 int (*fe_init)(struct dvb_frontend* fe);
282 int (*fe_read_status)(struct dvb_frontend* fe, fe_status_t* status);
283 int (*fe_diseqc_reset_overload)(struct dvb_frontend* fe);
284 int (*fe_diseqc_send_master_cmd)(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd* cmd);
285 int (*fe_diseqc_send_burst)(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd);
286 int (*fe_set_tone)(struct dvb_frontend* fe, fe_sec_tone_mode_t tone);
287 int (*fe_set_voltage)(struct dvb_frontend* fe, fe_sec_voltage_t voltage);
288 int (*fe_dishnetwork_send_legacy_command)(struct dvb_frontend* fe, unsigned long cmd);
289 int (*fe_set_frontend)(struct dvb_frontend* fe, struct dvb_frontend_parameters* params);
290};
291
292
293extern int ChangePIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
294 u16 subpid, u16 pcrpid);
295
296extern int av7110_check_ir_config(struct av7110 *av7110, int force);
297extern int av7110_ir_init(struct av7110 *av7110);
298extern void av7110_ir_exit(struct av7110 *av7110);
299
300/* msp3400 i2c subaddresses */
301#define MSP_WR_DEM 0x10
302#define MSP_RD_DEM 0x11
303#define MSP_WR_DSP 0x12
304#define MSP_RD_DSP 0x13
305
306extern int i2c_writereg(struct av7110 *av7110, u8 id, u8 reg, u8 val);
307extern u8 i2c_readreg(struct av7110 *av7110, u8 id, u8 reg);
308extern int msp_writereg(struct av7110 *av7110, u8 dev, u16 reg, u16 val);
309
310
311extern int av7110_init_analog_module(struct av7110 *av7110);
312extern int av7110_init_v4l(struct av7110 *av7110);
313extern int av7110_exit_v4l(struct av7110 *av7110);
314
315#endif /* _AV7110_H_ */
diff --git a/drivers/media/dvb/ttpci/av7110_av.c b/drivers/media/dvb/ttpci/av7110_av.c
new file mode 100644
index 00000000000..952b33dbac4
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_av.c
@@ -0,0 +1,1626 @@
1/*
2 * av7110_av.c: audio and video MPEG decoder stuff
3 *
4 * Copyright (C) 1999-2002 Ralph Metzler
5 * & Marcus Metzler for convergence integrated media GmbH
6 *
7 * originally based on code by:
8 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26 *
27 *
28 * the project's page is at http://www.linuxtv.org/
29 */
30
31#include <linux/types.h>
32#include <linux/kernel.h>
33#include <linux/string.h>
34#include <linux/delay.h>
35#include <linux/fs.h>
36
37#include "av7110.h"
38#include "av7110_hw.h"
39#include "av7110_av.h"
40#include "av7110_ipack.h"
41
42/* MPEG-2 (ISO 13818 / H.222.0) stream types */
43#define PROG_STREAM_MAP 0xBC
44#define PRIVATE_STREAM1 0xBD
45#define PADDING_STREAM 0xBE
46#define PRIVATE_STREAM2 0xBF
47#define AUDIO_STREAM_S 0xC0
48#define AUDIO_STREAM_E 0xDF
49#define VIDEO_STREAM_S 0xE0
50#define VIDEO_STREAM_E 0xEF
51#define ECM_STREAM 0xF0
52#define EMM_STREAM 0xF1
53#define DSM_CC_STREAM 0xF2
54#define ISO13522_STREAM 0xF3
55#define PROG_STREAM_DIR 0xFF
56
57#define PTS_DTS_FLAGS 0xC0
58
59//pts_dts flags
60#define PTS_ONLY 0x80
61#define PTS_DTS 0xC0
62#define TS_SIZE 188
63#define TRANS_ERROR 0x80
64#define PAY_START 0x40
65#define TRANS_PRIO 0x20
66#define PID_MASK_HI 0x1F
67//flags
68#define TRANS_SCRMBL1 0x80
69#define TRANS_SCRMBL2 0x40
70#define ADAPT_FIELD 0x20
71#define PAYLOAD 0x10
72#define COUNT_MASK 0x0F
73
74// adaptation flags
75#define DISCON_IND 0x80
76#define RAND_ACC_IND 0x40
77#define ES_PRI_IND 0x20
78#define PCR_FLAG 0x10
79#define OPCR_FLAG 0x08
80#define SPLICE_FLAG 0x04
81#define TRANS_PRIV 0x02
82#define ADAP_EXT_FLAG 0x01
83
84// adaptation extension flags
85#define LTW_FLAG 0x80
86#define PIECE_RATE 0x40
87#define SEAM_SPLICE 0x20
88
89
90static void p_to_t(u8 const *buf, long int length, u16 pid,
91 u8 *counter, struct dvb_demux_feed *feed);
92static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len);
93
94
95int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len)
96{
97 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) p2t->priv;
98
99 if (!(dvbdmxfeed->ts_type & TS_PACKET))
100 return 0;
101 if (buf[3] == 0xe0) // video PES do not have a length in TS
102 buf[4] = buf[5] = 0;
103 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
104 return dvbdmxfeed->cb.ts(buf, len, NULL, 0,
105 &dvbdmxfeed->feed.ts, DMX_OK);
106 else
107 return dvb_filter_pes2ts(p2t, buf, len, 1);
108}
109
110static int dvb_filter_pes2ts_cb(void *priv, unsigned char *data)
111{
112 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) priv;
113
114 dvbdmxfeed->cb.ts(data, 188, NULL, 0,
115 &dvbdmxfeed->feed.ts, DMX_OK);
116 return 0;
117}
118
119int av7110_av_start_record(struct av7110 *av7110, int av,
120 struct dvb_demux_feed *dvbdmxfeed)
121{
122 int ret = 0;
123 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
124
125 dprintk(2, "av7110:%p, , dvb_demux_feed:%p\n", av7110, dvbdmxfeed);
126
127 if (av7110->playing || (av7110->rec_mode & av))
128 return -EBUSY;
129 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
130 dvbdmx->recording = 1;
131 av7110->rec_mode |= av;
132
133 switch (av7110->rec_mode) {
134 case RP_AUDIO:
135 dvb_filter_pes2ts_init(&av7110->p2t[0],
136 dvbdmx->pesfilter[0]->pid,
137 dvb_filter_pes2ts_cb,
138 (void *) dvbdmx->pesfilter[0]);
139 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
140 break;
141
142 case RP_VIDEO:
143 dvb_filter_pes2ts_init(&av7110->p2t[1],
144 dvbdmx->pesfilter[1]->pid,
145 dvb_filter_pes2ts_cb,
146 (void *) dvbdmx->pesfilter[1]);
147 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
148 break;
149
150 case RP_AV:
151 dvb_filter_pes2ts_init(&av7110->p2t[0],
152 dvbdmx->pesfilter[0]->pid,
153 dvb_filter_pes2ts_cb,
154 (void *) dvbdmx->pesfilter[0]);
155 dvb_filter_pes2ts_init(&av7110->p2t[1],
156 dvbdmx->pesfilter[1]->pid,
157 dvb_filter_pes2ts_cb,
158 (void *) dvbdmx->pesfilter[1]);
159 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AV_PES, 0);
160 break;
161 }
162 return ret;
163}
164
165int av7110_av_start_play(struct av7110 *av7110, int av)
166{
167 int ret = 0;
168 dprintk(2, "av7110:%p, \n", av7110);
169
170 if (av7110->rec_mode)
171 return -EBUSY;
172 if (av7110->playing & av)
173 return -EBUSY;
174
175 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
176
177 if (av7110->playing == RP_NONE) {
178 av7110_ipack_reset(&av7110->ipack[0]);
179 av7110_ipack_reset(&av7110->ipack[1]);
180 }
181
182 av7110->playing |= av;
183 switch (av7110->playing) {
184 case RP_AUDIO:
185 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
186 break;
187 case RP_VIDEO:
188 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
189 av7110->sinfo = 0;
190 break;
191 case RP_AV:
192 av7110->sinfo = 0;
193 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AV_PES, 0);
194 break;
195 }
196 return ret;
197}
198
199int av7110_av_stop(struct av7110 *av7110, int av)
200{
201 int ret = 0;
202 dprintk(2, "av7110:%p, \n", av7110);
203
204 if (!(av7110->playing & av) && !(av7110->rec_mode & av))
205 return 0;
206 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
207 if (av7110->playing) {
208 av7110->playing &= ~av;
209 switch (av7110->playing) {
210 case RP_AUDIO:
211 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
212 break;
213 case RP_VIDEO:
214 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
215 break;
216 case RP_NONE:
217 ret = av7110_set_vidmode(av7110, av7110->vidmode);
218 break;
219 }
220 } else {
221 av7110->rec_mode &= ~av;
222 switch (av7110->rec_mode) {
223 case RP_AUDIO:
224 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
225 break;
226 case RP_VIDEO:
227 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
228 break;
229 case RP_NONE:
230 break;
231 }
232 }
233 return ret;
234}
235
236
237int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen)
238{
239 int len;
240 u32 sync;
241 u16 blen;
242
243 if (!dlen) {
244 wake_up(&buf->queue);
245 return -1;
246 }
247 while (1) {
248 len = dvb_ringbuffer_avail(buf);
249 if (len < 6) {
250 wake_up(&buf->queue);
251 return -1;
252 }
253 sync = DVB_RINGBUFFER_PEEK(buf, 0) << 24;
254 sync |= DVB_RINGBUFFER_PEEK(buf, 1) << 16;
255 sync |= DVB_RINGBUFFER_PEEK(buf, 2) << 8;
256 sync |= DVB_RINGBUFFER_PEEK(buf, 3);
257
258 if (((sync &~ 0x0f) == 0x000001e0) ||
259 ((sync &~ 0x1f) == 0x000001c0) ||
260 (sync == 0x000001bd))
261 break;
262 printk("resync\n");
263 DVB_RINGBUFFER_SKIP(buf, 1);
264 }
265 blen = DVB_RINGBUFFER_PEEK(buf, 4) << 8;
266 blen |= DVB_RINGBUFFER_PEEK(buf, 5);
267 blen += 6;
268 if (len < blen || blen > dlen) {
269 //printk("buffer empty - avail %d blen %u dlen %d\n", len, blen, dlen);
270 wake_up(&buf->queue);
271 return -1;
272 }
273
274 dvb_ringbuffer_read(buf, dest, (size_t) blen);
275
276 dprintk(2, "pread=0x%08lx, pwrite=0x%08lx\n",
277 (unsigned long) buf->pread, (unsigned long) buf->pwrite);
278 wake_up(&buf->queue);
279 return blen;
280}
281
282
283int av7110_set_volume(struct av7110 *av7110, int volleft, int volright)
284{
285 int err, vol, val, balance = 0;
286
287 dprintk(2, "av7110:%p, \n", av7110);
288
289 av7110->mixer.volume_left = volleft;
290 av7110->mixer.volume_right = volright;
291
292 switch (av7110->adac_type) {
293 case DVB_ADAC_TI:
294 volleft = (volleft * 256) / 1036;
295 volright = (volright * 256) / 1036;
296 if (volleft > 0x3f)
297 volleft = 0x3f;
298 if (volright > 0x3f)
299 volright = 0x3f;
300 if ((err = SendDAC(av7110, 3, 0x80 + volleft)))
301 return err;
302 return SendDAC(av7110, 4, volright);
303
304 case DVB_ADAC_CRYSTAL:
305 volleft = 127 - volleft / 2;
306 volright = 127 - volright / 2;
307 i2c_writereg(av7110, 0x20, 0x03, volleft);
308 i2c_writereg(av7110, 0x20, 0x04, volright);
309 return 0;
310
311 case DVB_ADAC_MSP34x0:
312 vol = (volleft > volright) ? volleft : volright;
313 val = (vol * 0x73 / 255) << 8;
314 if (vol > 0)
315 balance = ((volright - volleft) * 127) / vol;
316 msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
317 msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
318 msp_writereg(av7110, MSP_WR_DSP, 0x0006, val); /* headphonesr */
319 return 0;
320
321 case DVB_ADAC_MSP34x5:
322 vol = (volleft > volright) ? volleft : volright;
323 val = (vol * 0x73 / 255) << 8;
324 if (vol > 0)
325 balance = ((volright - volleft) * 127) / vol;
326 msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
327 msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
328 return 0;
329 }
330
331 return 0;
332}
333
334int av7110_set_vidmode(struct av7110 *av7110, enum av7110_video_mode mode)
335{
336 int ret;
337 dprintk(2, "av7110:%p, \n", av7110);
338
339 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, LoadVidCode, 1, mode);
340
341 if (!ret && !av7110->playing) {
342 ret = ChangePIDs(av7110, av7110->pids[DMX_PES_VIDEO],
343 av7110->pids[DMX_PES_AUDIO],
344 av7110->pids[DMX_PES_TELETEXT],
345 0, av7110->pids[DMX_PES_PCR]);
346 if (!ret)
347 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
348 }
349 return ret;
350}
351
352
353static enum av7110_video_mode sw2mode[16] = {
354 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
355 AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_PAL,
356 AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_NTSC,
357 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
358 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
359 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
360 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
361 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
362};
363
364static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
365{
366 int i;
367 int hsize, vsize;
368 int sw;
369 u8 *p;
370 int ret = 0;
371
372 dprintk(2, "av7110:%p, \n", av7110);
373
374 if (av7110->sinfo)
375 return 0;
376 for (i = 7; i < count - 10; i++) {
377 p = buf + i;
378 if (p[0] || p[1] || p[2] != 0x01 || p[3] != 0xb3)
379 continue;
380 p += 4;
381 hsize = ((p[1] &0xF0) >> 4) | (p[0] << 4);
382 vsize = ((p[1] &0x0F) << 8) | (p[2]);
383 sw = (p[3] & 0x0F);
384 ret = av7110_set_vidmode(av7110, sw2mode[sw]);
385 if (!ret) {
386 dprintk(2, "playback %dx%d fr=%d\n", hsize, vsize, sw);
387 av7110->sinfo = 1;
388 }
389 break;
390 }
391 return ret;
392}
393
394
395/****************************************************************************
396 * I/O buffer management and control
397 ****************************************************************************/
398
399static inline long aux_ring_buffer_write(struct dvb_ringbuffer *rbuf,
400 const u8 *buf, unsigned long count)
401{
402 unsigned long todo = count;
403 int free;
404
405 while (todo > 0) {
406 if (dvb_ringbuffer_free(rbuf) < 2048) {
407 if (wait_event_interruptible(rbuf->queue,
408 (dvb_ringbuffer_free(rbuf) >= 2048)))
409 return count - todo;
410 }
411 free = dvb_ringbuffer_free(rbuf);
412 if (free > todo)
413 free = todo;
414 dvb_ringbuffer_write(rbuf, buf, free);
415 todo -= free;
416 buf += free;
417 }
418
419 return count - todo;
420}
421
422static void play_video_cb(u8 *buf, int count, void *priv)
423{
424 struct av7110 *av7110 = (struct av7110 *) priv;
425 dprintk(2, "av7110:%p, \n", av7110);
426
427 if ((buf[3] & 0xe0) == 0xe0) {
428 get_video_format(av7110, buf, count);
429 aux_ring_buffer_write(&av7110->avout, buf, count);
430 } else
431 aux_ring_buffer_write(&av7110->aout, buf, count);
432}
433
434static void play_audio_cb(u8 *buf, int count, void *priv)
435{
436 struct av7110 *av7110 = (struct av7110 *) priv;
437 dprintk(2, "av7110:%p, \n", av7110);
438
439 aux_ring_buffer_write(&av7110->aout, buf, count);
440}
441
442
443#define FREE_COND_TS (dvb_ringbuffer_free(rb) >= 4096)
444
445static ssize_t ts_play(struct av7110 *av7110, const char __user *buf,
446 unsigned long count, int nonblock, int type)
447{
448 struct dvb_ringbuffer *rb;
449 u8 *kb;
450 unsigned long todo = count;
451
452 dprintk(2, "%s: type %d cnt %lu\n", __func__, type, count);
453
454 rb = (type) ? &av7110->avout : &av7110->aout;
455 kb = av7110->kbuf[type];
456
457 if (!kb)
458 return -ENOBUFS;
459
460 if (nonblock && !FREE_COND_TS)
461 return -EWOULDBLOCK;
462
463 while (todo >= TS_SIZE) {
464 if (!FREE_COND_TS) {
465 if (nonblock)
466 return count - todo;
467 if (wait_event_interruptible(rb->queue, FREE_COND_TS))
468 return count - todo;
469 }
470 if (copy_from_user(kb, buf, TS_SIZE))
471 return -EFAULT;
472 write_ts_to_decoder(av7110, type, kb, TS_SIZE);
473 todo -= TS_SIZE;
474 buf += TS_SIZE;
475 }
476
477 return count - todo;
478}
479
480
481#define FREE_COND (dvb_ringbuffer_free(&av7110->avout) >= 20 * 1024 && \
482 dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
483
484static ssize_t dvb_play(struct av7110 *av7110, const char __user *buf,
485 unsigned long count, int nonblock, int type)
486{
487 unsigned long todo = count, n;
488 dprintk(2, "av7110:%p, \n", av7110);
489
490 if (!av7110->kbuf[type])
491 return -ENOBUFS;
492
493 if (nonblock && !FREE_COND)
494 return -EWOULDBLOCK;
495
496 while (todo > 0) {
497 if (!FREE_COND) {
498 if (nonblock)
499 return count - todo;
500 if (wait_event_interruptible(av7110->avout.queue,
501 FREE_COND))
502 return count - todo;
503 }
504 n = todo;
505 if (n > IPACKS * 2)
506 n = IPACKS * 2;
507 if (copy_from_user(av7110->kbuf[type], buf, n))
508 return -EFAULT;
509 av7110_ipack_instant_repack(av7110->kbuf[type], n,
510 &av7110->ipack[type]);
511 todo -= n;
512 buf += n;
513 }
514 return count - todo;
515}
516
517static ssize_t dvb_play_kernel(struct av7110 *av7110, const u8 *buf,
518 unsigned long count, int nonblock, int type)
519{
520 unsigned long todo = count, n;
521 dprintk(2, "av7110:%p, \n", av7110);
522
523 if (!av7110->kbuf[type])
524 return -ENOBUFS;
525
526 if (nonblock && !FREE_COND)
527 return -EWOULDBLOCK;
528
529 while (todo > 0) {
530 if (!FREE_COND) {
531 if (nonblock)
532 return count - todo;
533 if (wait_event_interruptible(av7110->avout.queue,
534 FREE_COND))
535 return count - todo;
536 }
537 n = todo;
538 if (n > IPACKS * 2)
539 n = IPACKS * 2;
540 av7110_ipack_instant_repack(buf, n, &av7110->ipack[type]);
541 todo -= n;
542 buf += n;
543 }
544 return count - todo;
545}
546
547static ssize_t dvb_aplay(struct av7110 *av7110, const char __user *buf,
548 unsigned long count, int nonblock, int type)
549{
550 unsigned long todo = count, n;
551 dprintk(2, "av7110:%p, \n", av7110);
552
553 if (!av7110->kbuf[type])
554 return -ENOBUFS;
555 if (nonblock && dvb_ringbuffer_free(&av7110->aout) < 20 * 1024)
556 return -EWOULDBLOCK;
557
558 while (todo > 0) {
559 if (dvb_ringbuffer_free(&av7110->aout) < 20 * 1024) {
560 if (nonblock)
561 return count - todo;
562 if (wait_event_interruptible(av7110->aout.queue,
563 (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)))
564 return count-todo;
565 }
566 n = todo;
567 if (n > IPACKS * 2)
568 n = IPACKS * 2;
569 if (copy_from_user(av7110->kbuf[type], buf, n))
570 return -EFAULT;
571 av7110_ipack_instant_repack(av7110->kbuf[type], n,
572 &av7110->ipack[type]);
573 todo -= n;
574 buf += n;
575 }
576 return count - todo;
577}
578
579void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed)
580{
581 memset(p->pes, 0, TS_SIZE);
582 p->counter = 0;
583 p->pos = 0;
584 p->frags = 0;
585 if (feed)
586 p->feed = feed;
587}
588
589static void clear_p2t(struct av7110_p2t *p)
590{
591 memset(p->pes, 0, TS_SIZE);
592// p->counter = 0;
593 p->pos = 0;
594 p->frags = 0;
595}
596
597
598static int find_pes_header(u8 const *buf, long int length, int *frags)
599{
600 int c = 0;
601 int found = 0;
602
603 *frags = 0;
604
605 while (c < length - 3 && !found) {
606 if (buf[c] == 0x00 && buf[c + 1] == 0x00 &&
607 buf[c + 2] == 0x01) {
608 switch ( buf[c + 3] ) {
609 case PROG_STREAM_MAP:
610 case PRIVATE_STREAM2:
611 case PROG_STREAM_DIR:
612 case ECM_STREAM :
613 case EMM_STREAM :
614 case PADDING_STREAM :
615 case DSM_CC_STREAM :
616 case ISO13522_STREAM:
617 case PRIVATE_STREAM1:
618 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
619 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
620 found = 1;
621 break;
622
623 default:
624 c++;
625 break;
626 }
627 } else
628 c++;
629 }
630 if (c == length - 3 && !found) {
631 if (buf[length - 1] == 0x00)
632 *frags = 1;
633 if (buf[length - 2] == 0x00 &&
634 buf[length - 1] == 0x00)
635 *frags = 2;
636 if (buf[length - 3] == 0x00 &&
637 buf[length - 2] == 0x00 &&
638 buf[length - 1] == 0x01)
639 *frags = 3;
640 return -1;
641 }
642
643 return c;
644}
645
646void av7110_p2t_write(u8 const *buf, long int length, u16 pid, struct av7110_p2t *p)
647{
648 int c, c2, l, add;
649 int check, rest;
650
651 c = 0;
652 c2 = 0;
653 if (p->frags){
654 check = 0;
655 switch(p->frags) {
656 case 1:
657 if (buf[c] == 0x00 && buf[c + 1] == 0x01) {
658 check = 1;
659 c += 2;
660 }
661 break;
662 case 2:
663 if (buf[c] == 0x01) {
664 check = 1;
665 c++;
666 }
667 break;
668 case 3:
669 check = 1;
670 }
671 if (check) {
672 switch (buf[c]) {
673 case PROG_STREAM_MAP:
674 case PRIVATE_STREAM2:
675 case PROG_STREAM_DIR:
676 case ECM_STREAM :
677 case EMM_STREAM :
678 case PADDING_STREAM :
679 case DSM_CC_STREAM :
680 case ISO13522_STREAM:
681 case PRIVATE_STREAM1:
682 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
683 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
684 p->pes[0] = 0x00;
685 p->pes[1] = 0x00;
686 p->pes[2] = 0x01;
687 p->pes[3] = buf[c];
688 p->pos = 4;
689 memcpy(p->pes + p->pos, buf + c, (TS_SIZE - 4) - p->pos);
690 c += (TS_SIZE - 4) - p->pos;
691 p_to_t(p->pes, (TS_SIZE - 4), pid, &p->counter, p->feed);
692 clear_p2t(p);
693 break;
694
695 default:
696 c = 0;
697 break;
698 }
699 }
700 p->frags = 0;
701 }
702
703 if (p->pos) {
704 c2 = find_pes_header(buf + c, length - c, &p->frags);
705 if (c2 >= 0 && c2 < (TS_SIZE - 4) - p->pos)
706 l = c2+c;
707 else
708 l = (TS_SIZE - 4) - p->pos;
709 memcpy(p->pes + p->pos, buf, l);
710 c += l;
711 p->pos += l;
712 p_to_t(p->pes, p->pos, pid, &p->counter, p->feed);
713 clear_p2t(p);
714 }
715
716 add = 0;
717 while (c < length) {
718 c2 = find_pes_header(buf + c + add, length - c - add, &p->frags);
719 if (c2 >= 0) {
720 c2 += c + add;
721 if (c2 > c){
722 p_to_t(buf + c, c2 - c, pid, &p->counter, p->feed);
723 c = c2;
724 clear_p2t(p);
725 add = 0;
726 } else
727 add = 1;
728 } else {
729 l = length - c;
730 rest = l % (TS_SIZE - 4);
731 l -= rest;
732 p_to_t(buf + c, l, pid, &p->counter, p->feed);
733 memcpy(p->pes, buf + c + l, rest);
734 p->pos = rest;
735 c = length;
736 }
737 }
738}
739
740
741static int write_ts_header2(u16 pid, u8 *counter, int pes_start, u8 *buf, u8 length)
742{
743 int i;
744 int c = 0;
745 int fill;
746 u8 tshead[4] = { 0x47, 0x00, 0x00, 0x10 };
747
748 fill = (TS_SIZE - 4) - length;
749 if (pes_start)
750 tshead[1] = 0x40;
751 if (fill)
752 tshead[3] = 0x30;
753 tshead[1] |= (u8)((pid & 0x1F00) >> 8);
754 tshead[2] |= (u8)(pid & 0x00FF);
755 tshead[3] |= ((*counter)++ & 0x0F);
756 memcpy(buf, tshead, 4);
757 c += 4;
758
759 if (fill) {
760 buf[4] = fill - 1;
761 c++;
762 if (fill > 1) {
763 buf[5] = 0x00;
764 c++;
765 }
766 for (i = 6; i < fill + 4; i++) {
767 buf[i] = 0xFF;
768 c++;
769 }
770 }
771
772 return c;
773}
774
775
776static void p_to_t(u8 const *buf, long int length, u16 pid, u8 *counter,
777 struct dvb_demux_feed *feed)
778{
779 int l, pes_start;
780 u8 obuf[TS_SIZE];
781 long c = 0;
782
783 pes_start = 0;
784 if (length > 3 &&
785 buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
786 switch (buf[3]) {
787 case PROG_STREAM_MAP:
788 case PRIVATE_STREAM2:
789 case PROG_STREAM_DIR:
790 case ECM_STREAM :
791 case EMM_STREAM :
792 case PADDING_STREAM :
793 case DSM_CC_STREAM :
794 case ISO13522_STREAM:
795 case PRIVATE_STREAM1:
796 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
797 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
798 pes_start = 1;
799 break;
800
801 default:
802 break;
803 }
804
805 while (c < length) {
806 memset(obuf, 0, TS_SIZE);
807 if (length - c >= (TS_SIZE - 4)){
808 l = write_ts_header2(pid, counter, pes_start,
809 obuf, (TS_SIZE - 4));
810 memcpy(obuf + l, buf + c, TS_SIZE - l);
811 c += TS_SIZE - l;
812 } else {
813 l = write_ts_header2(pid, counter, pes_start,
814 obuf, length - c);
815 memcpy(obuf + l, buf + c, TS_SIZE - l);
816 c = length;
817 }
818 feed->cb.ts(obuf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
819 pes_start = 0;
820 }
821}
822
823
824static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len)
825{
826 struct ipack *ipack = &av7110->ipack[type];
827
828 if (buf[1] & TRANS_ERROR) {
829 av7110_ipack_reset(ipack);
830 return -1;
831 }
832
833 if (!(buf[3] & PAYLOAD))
834 return -1;
835
836 if (buf[1] & PAY_START)
837 av7110_ipack_flush(ipack);
838
839 if (buf[3] & ADAPT_FIELD) {
840 len -= buf[4] + 1;
841 buf += buf[4] + 1;
842 if (!len)
843 return 0;
844 }
845
846 av7110_ipack_instant_repack(buf + 4, len - 4, ipack);
847 return 0;
848}
849
850
851int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len)
852{
853 struct dvb_demux *demux = feed->demux;
854 struct av7110 *av7110 = (struct av7110 *) demux->priv;
855
856 dprintk(2, "av7110:%p, \n", av7110);
857
858 if (av7110->full_ts && demux->dmx.frontend->source != DMX_MEMORY_FE)
859 return 0;
860
861 switch (feed->pes_type) {
862 case 0:
863 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
864 return -EINVAL;
865 break;
866 case 1:
867 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
868 return -EINVAL;
869 break;
870 default:
871 return -1;
872 }
873
874 return write_ts_to_decoder(av7110, feed->pes_type, buf, len);
875}
876
877
878
879/******************************************************************************
880 * Video MPEG decoder events
881 ******************************************************************************/
882void dvb_video_add_event(struct av7110 *av7110, struct video_event *event)
883{
884 struct dvb_video_events *events = &av7110->video_events;
885 int wp;
886
887 spin_lock_bh(&events->lock);
888
889 wp = (events->eventw + 1) % MAX_VIDEO_EVENT;
890 if (wp == events->eventr) {
891 events->overflow = 1;
892 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
893 }
894
895 //FIXME: timestamp?
896 memcpy(&events->events[events->eventw], event, sizeof(struct video_event));
897 events->eventw = wp;
898
899 spin_unlock_bh(&events->lock);
900
901 wake_up_interruptible(&events->wait_queue);
902}
903
904
905static int dvb_video_get_event (struct av7110 *av7110, struct video_event *event, int flags)
906{
907 struct dvb_video_events *events = &av7110->video_events;
908
909 if (events->overflow) {
910 events->overflow = 0;
911 return -EOVERFLOW;
912 }
913 if (events->eventw == events->eventr) {
914 int ret;
915
916 if (flags & O_NONBLOCK)
917 return -EWOULDBLOCK;
918
919 ret = wait_event_interruptible(events->wait_queue,
920 events->eventw != events->eventr);
921 if (ret < 0)
922 return ret;
923 }
924
925 spin_lock_bh(&events->lock);
926
927 memcpy(event, &events->events[events->eventr],
928 sizeof(struct video_event));
929 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
930
931 spin_unlock_bh(&events->lock);
932
933 return 0;
934}
935
936
937/******************************************************************************
938 * DVB device file operations
939 ******************************************************************************/
940
941static unsigned int dvb_video_poll(struct file *file, poll_table *wait)
942{
943 struct dvb_device *dvbdev = file->private_data;
944 struct av7110 *av7110 = dvbdev->priv;
945 unsigned int mask = 0;
946
947 dprintk(2, "av7110:%p, \n", av7110);
948
949 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
950 poll_wait(file, &av7110->avout.queue, wait);
951
952 poll_wait(file, &av7110->video_events.wait_queue, wait);
953
954 if (av7110->video_events.eventw != av7110->video_events.eventr)
955 mask = POLLPRI;
956
957 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
958 if (av7110->playing) {
959 if (FREE_COND)
960 mask |= (POLLOUT | POLLWRNORM);
961 } else /* if not playing: may play if asked for */
962 mask |= (POLLOUT | POLLWRNORM);
963 }
964
965 return mask;
966}
967
968static ssize_t dvb_video_write(struct file *file, const char __user *buf,
969 size_t count, loff_t *ppos)
970{
971 struct dvb_device *dvbdev = file->private_data;
972 struct av7110 *av7110 = dvbdev->priv;
973 unsigned char c;
974
975 dprintk(2, "av7110:%p, \n", av7110);
976
977 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
978 return -EPERM;
979
980 if (av7110->videostate.stream_source != VIDEO_SOURCE_MEMORY)
981 return -EPERM;
982
983 if (get_user(c, buf))
984 return -EFAULT;
985 if (c == 0x47 && count % TS_SIZE == 0)
986 return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
987 else
988 return dvb_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
989}
990
991static unsigned int dvb_audio_poll(struct file *file, poll_table *wait)
992{
993 struct dvb_device *dvbdev = file->private_data;
994 struct av7110 *av7110 = dvbdev->priv;
995 unsigned int mask = 0;
996
997 dprintk(2, "av7110:%p, \n", av7110);
998
999 poll_wait(file, &av7110->aout.queue, wait);
1000
1001 if (av7110->playing) {
1002 if (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
1003 mask |= (POLLOUT | POLLWRNORM);
1004 } else /* if not playing: may play if asked for */
1005 mask = (POLLOUT | POLLWRNORM);
1006
1007 return mask;
1008}
1009
1010static ssize_t dvb_audio_write(struct file *file, const char __user *buf,
1011 size_t count, loff_t *ppos)
1012{
1013 struct dvb_device *dvbdev = file->private_data;
1014 struct av7110 *av7110 = dvbdev->priv;
1015 unsigned char c;
1016
1017 dprintk(2, "av7110:%p, \n", av7110);
1018
1019 if (av7110->audiostate.stream_source != AUDIO_SOURCE_MEMORY) {
1020 printk(KERN_ERR "not audio source memory\n");
1021 return -EPERM;
1022 }
1023
1024 if (get_user(c, buf))
1025 return -EFAULT;
1026 if (c == 0x47 && count % TS_SIZE == 0)
1027 return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
1028 else
1029 return dvb_aplay(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
1030}
1031
1032static u8 iframe_header[] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
1033
1034#define MIN_IFRAME 400000
1035
1036static int play_iframe(struct av7110 *av7110, char __user *buf, unsigned int len, int nonblock)
1037{
1038 unsigned i, n;
1039 int progressive = 0;
1040 int match = 0;
1041
1042 dprintk(2, "av7110:%p, \n", av7110);
1043
1044 if (!(av7110->playing & RP_VIDEO)) {
1045 if (av7110_av_start_play(av7110, RP_VIDEO) < 0)
1046 return -EBUSY;
1047 }
1048
1049 /* search in buf for instances of 00 00 01 b5 1? */
1050 for (i = 0; i < len; i++) {
1051 unsigned char c;
1052 if (get_user(c, buf + i))
1053 return -EFAULT;
1054 if (match == 5) {
1055 progressive = c & 0x08;
1056 match = 0;
1057 }
1058 if (c == 0x00) {
1059 match = (match == 1 || match == 2) ? 2 : 1;
1060 continue;
1061 }
1062 switch (match++) {
1063 case 2: if (c == 0x01)
1064 continue;
1065 break;
1066 case 3: if (c == 0xb5)
1067 continue;
1068 break;
1069 case 4: if ((c & 0xf0) == 0x10)
1070 continue;
1071 break;
1072 }
1073 match = 0;
1074 }
1075
1076 /* setting n always > 1, fixes problems when playing stillframes
1077 consisting of I- and P-Frames */
1078 n = MIN_IFRAME / len + 1;
1079
1080 /* FIXME: nonblock? */
1081 dvb_play_kernel(av7110, iframe_header, sizeof(iframe_header), 0, 1);
1082
1083 for (i = 0; i < n; i++)
1084 dvb_play(av7110, buf, len, 0, 1);
1085
1086 av7110_ipack_flush(&av7110->ipack[1]);
1087
1088 if (progressive)
1089 return vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
1090 else
1091 return 0;
1092}
1093
1094
1095static int dvb_video_ioctl(struct file *file,
1096 unsigned int cmd, void *parg)
1097{
1098 struct dvb_device *dvbdev = file->private_data;
1099 struct av7110 *av7110 = dvbdev->priv;
1100 unsigned long arg = (unsigned long) parg;
1101 int ret = 0;
1102
1103 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1104
1105 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1106 if ( cmd != VIDEO_GET_STATUS && cmd != VIDEO_GET_EVENT &&
1107 cmd != VIDEO_GET_SIZE ) {
1108 return -EPERM;
1109 }
1110 }
1111
1112 switch (cmd) {
1113 case VIDEO_STOP:
1114 av7110->videostate.play_state = VIDEO_STOPPED;
1115 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
1116 ret = av7110_av_stop(av7110, RP_VIDEO);
1117 else
1118 ret = vidcom(av7110, AV_VIDEO_CMD_STOP,
1119 av7110->videostate.video_blank ? 0 : 1);
1120 if (!ret)
1121 av7110->trickmode = TRICK_NONE;
1122 break;
1123
1124 case VIDEO_PLAY:
1125 av7110->trickmode = TRICK_NONE;
1126 if (av7110->videostate.play_state == VIDEO_FREEZED) {
1127 av7110->videostate.play_state = VIDEO_PLAYING;
1128 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1129 if (ret)
1130 break;
1131 }
1132 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY) {
1133 if (av7110->playing == RP_AV) {
1134 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
1135 if (ret)
1136 break;
1137 av7110->playing &= ~RP_VIDEO;
1138 }
1139 ret = av7110_av_start_play(av7110, RP_VIDEO);
1140 }
1141 if (!ret)
1142 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1143 if (!ret)
1144 av7110->videostate.play_state = VIDEO_PLAYING;
1145 break;
1146
1147 case VIDEO_FREEZE:
1148 av7110->videostate.play_state = VIDEO_FREEZED;
1149 if (av7110->playing & RP_VIDEO)
1150 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Pause, 0);
1151 else
1152 ret = vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
1153 if (!ret)
1154 av7110->trickmode = TRICK_FREEZE;
1155 break;
1156
1157 case VIDEO_CONTINUE:
1158 if (av7110->playing & RP_VIDEO)
1159 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Continue, 0);
1160 if (!ret)
1161 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1162 if (!ret) {
1163 av7110->videostate.play_state = VIDEO_PLAYING;
1164 av7110->trickmode = TRICK_NONE;
1165 }
1166 break;
1167
1168 case VIDEO_SELECT_SOURCE:
1169 av7110->videostate.stream_source = (video_stream_source_t) arg;
1170 break;
1171
1172 case VIDEO_SET_BLANK:
1173 av7110->videostate.video_blank = (int) arg;
1174 break;
1175
1176 case VIDEO_GET_STATUS:
1177 memcpy(parg, &av7110->videostate, sizeof(struct video_status));
1178 break;
1179
1180 case VIDEO_GET_EVENT:
1181 ret = dvb_video_get_event(av7110, parg, file->f_flags);
1182 break;
1183
1184 case VIDEO_GET_SIZE:
1185 memcpy(parg, &av7110->video_size, sizeof(video_size_t));
1186 break;
1187
1188 case VIDEO_SET_DISPLAY_FORMAT:
1189 {
1190 video_displayformat_t format = (video_displayformat_t) arg;
1191 switch (format) {
1192 case VIDEO_PAN_SCAN:
1193 av7110->display_panscan = VID_PAN_SCAN_PREF;
1194 break;
1195 case VIDEO_LETTER_BOX:
1196 av7110->display_panscan = VID_VC_AND_PS_PREF;
1197 break;
1198 case VIDEO_CENTER_CUT_OUT:
1199 av7110->display_panscan = VID_CENTRE_CUT_PREF;
1200 break;
1201 default:
1202 ret = -EINVAL;
1203 }
1204 if (ret < 0)
1205 break;
1206 av7110->videostate.display_format = format;
1207 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
1208 1, av7110->display_panscan);
1209 break;
1210 }
1211
1212 case VIDEO_SET_FORMAT:
1213 if (arg > 1) {
1214 ret = -EINVAL;
1215 break;
1216 }
1217 av7110->display_ar = arg;
1218 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
1219 1, (u16) arg);
1220 break;
1221
1222 case VIDEO_STILLPICTURE:
1223 {
1224 struct video_still_picture *pic =
1225 (struct video_still_picture *) parg;
1226 av7110->videostate.stream_source = VIDEO_SOURCE_MEMORY;
1227 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1228 ret = play_iframe(av7110, pic->iFrame, pic->size,
1229 file->f_flags & O_NONBLOCK);
1230 break;
1231 }
1232
1233 case VIDEO_FAST_FORWARD:
1234 //note: arg is ignored by firmware
1235 if (av7110->playing & RP_VIDEO)
1236 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1237 __Scan_I, 2, AV_PES, 0);
1238 else
1239 ret = vidcom(av7110, AV_VIDEO_CMD_FFWD, arg);
1240 if (!ret) {
1241 av7110->trickmode = TRICK_FAST;
1242 av7110->videostate.play_state = VIDEO_PLAYING;
1243 }
1244 break;
1245
1246 case VIDEO_SLOWMOTION:
1247 if (av7110->playing&RP_VIDEO) {
1248 if (av7110->trickmode != TRICK_SLOW)
1249 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Slow, 2, 0, 0);
1250 if (!ret)
1251 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1252 } else {
1253 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1254 if (!ret)
1255 ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 0);
1256 if (!ret)
1257 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1258 }
1259 if (!ret) {
1260 av7110->trickmode = TRICK_SLOW;
1261 av7110->videostate.play_state = VIDEO_PLAYING;
1262 }
1263 break;
1264
1265 case VIDEO_GET_CAPABILITIES:
1266 *(int *)parg = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 |
1267 VIDEO_CAP_SYS | VIDEO_CAP_PROG;
1268 break;
1269
1270 case VIDEO_CLEAR_BUFFER:
1271 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1272 av7110_ipack_reset(&av7110->ipack[1]);
1273 if (av7110->playing == RP_AV) {
1274 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1275 __Play, 2, AV_PES, 0);
1276 if (ret)
1277 break;
1278 if (av7110->trickmode == TRICK_FAST)
1279 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1280 __Scan_I, 2, AV_PES, 0);
1281 if (av7110->trickmode == TRICK_SLOW) {
1282 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1283 __Slow, 2, 0, 0);
1284 if (!ret)
1285 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1286 }
1287 if (av7110->trickmode == TRICK_FREEZE)
1288 ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 1);
1289 }
1290 break;
1291
1292 case VIDEO_SET_STREAMTYPE:
1293 break;
1294
1295 default:
1296 ret = -ENOIOCTLCMD;
1297 break;
1298 }
1299
1300 return ret;
1301}
1302
1303static int dvb_audio_ioctl(struct file *file,
1304 unsigned int cmd, void *parg)
1305{
1306 struct dvb_device *dvbdev = file->private_data;
1307 struct av7110 *av7110 = dvbdev->priv;
1308 unsigned long arg = (unsigned long) parg;
1309 int ret = 0;
1310
1311 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1312
1313 if (((file->f_flags & O_ACCMODE) == O_RDONLY) &&
1314 (cmd != AUDIO_GET_STATUS))
1315 return -EPERM;
1316
1317 switch (cmd) {
1318 case AUDIO_STOP:
1319 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1320 ret = av7110_av_stop(av7110, RP_AUDIO);
1321 else
1322 ret = audcom(av7110, AUDIO_CMD_MUTE);
1323 if (!ret)
1324 av7110->audiostate.play_state = AUDIO_STOPPED;
1325 break;
1326
1327 case AUDIO_PLAY:
1328 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1329 ret = av7110_av_start_play(av7110, RP_AUDIO);
1330 if (!ret)
1331 ret = audcom(av7110, AUDIO_CMD_UNMUTE);
1332 if (!ret)
1333 av7110->audiostate.play_state = AUDIO_PLAYING;
1334 break;
1335
1336 case AUDIO_PAUSE:
1337 ret = audcom(av7110, AUDIO_CMD_MUTE);
1338 if (!ret)
1339 av7110->audiostate.play_state = AUDIO_PAUSED;
1340 break;
1341
1342 case AUDIO_CONTINUE:
1343 if (av7110->audiostate.play_state == AUDIO_PAUSED) {
1344 av7110->audiostate.play_state = AUDIO_PLAYING;
1345 ret = audcom(av7110, AUDIO_CMD_UNMUTE | AUDIO_CMD_PCM16);
1346 }
1347 break;
1348
1349 case AUDIO_SELECT_SOURCE:
1350 av7110->audiostate.stream_source = (audio_stream_source_t) arg;
1351 break;
1352
1353 case AUDIO_SET_MUTE:
1354 {
1355 ret = audcom(av7110, arg ? AUDIO_CMD_MUTE : AUDIO_CMD_UNMUTE);
1356 if (!ret)
1357 av7110->audiostate.mute_state = (int) arg;
1358 break;
1359 }
1360
1361 case AUDIO_SET_AV_SYNC:
1362 av7110->audiostate.AV_sync_state = (int) arg;
1363 ret = audcom(av7110, arg ? AUDIO_CMD_SYNC_ON : AUDIO_CMD_SYNC_OFF);
1364 break;
1365
1366 case AUDIO_SET_BYPASS_MODE:
1367 if (FW_VERSION(av7110->arm_app) < 0x2621)
1368 ret = -EINVAL;
1369 av7110->audiostate.bypass_mode = (int)arg;
1370 break;
1371
1372 case AUDIO_CHANNEL_SELECT:
1373 av7110->audiostate.channel_select = (audio_channel_select_t) arg;
1374 switch(av7110->audiostate.channel_select) {
1375 case AUDIO_STEREO:
1376 ret = audcom(av7110, AUDIO_CMD_STEREO);
1377 if (!ret) {
1378 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1379 i2c_writereg(av7110, 0x20, 0x02, 0x49);
1380 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1381 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220);
1382 }
1383 break;
1384 case AUDIO_MONO_LEFT:
1385 ret = audcom(av7110, AUDIO_CMD_MONO_L);
1386 if (!ret) {
1387 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1388 i2c_writereg(av7110, 0x20, 0x02, 0x4a);
1389 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1390 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0200);
1391 }
1392 break;
1393 case AUDIO_MONO_RIGHT:
1394 ret = audcom(av7110, AUDIO_CMD_MONO_R);
1395 if (!ret) {
1396 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1397 i2c_writereg(av7110, 0x20, 0x02, 0x45);
1398 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1399 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0210);
1400 }
1401 break;
1402 default:
1403 ret = -EINVAL;
1404 break;
1405 }
1406 break;
1407
1408 case AUDIO_GET_STATUS:
1409 memcpy(parg, &av7110->audiostate, sizeof(struct audio_status));
1410 break;
1411
1412 case AUDIO_GET_CAPABILITIES:
1413 if (FW_VERSION(av7110->arm_app) < 0x2621)
1414 *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1415 else
1416 *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_DTS | AUDIO_CAP_AC3 |
1417 AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1418 break;
1419
1420 case AUDIO_CLEAR_BUFFER:
1421 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1422 av7110_ipack_reset(&av7110->ipack[0]);
1423 if (av7110->playing == RP_AV)
1424 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1425 __Play, 2, AV_PES, 0);
1426 break;
1427
1428 case AUDIO_SET_ID:
1429 break;
1430
1431 case AUDIO_SET_MIXER:
1432 {
1433 struct audio_mixer *amix = (struct audio_mixer *)parg;
1434 ret = av7110_set_volume(av7110, amix->volume_left, amix->volume_right);
1435 break;
1436 }
1437
1438 case AUDIO_SET_STREAMTYPE:
1439 break;
1440
1441 default:
1442 ret = -ENOIOCTLCMD;
1443 }
1444
1445 return ret;
1446}
1447
1448
1449static int dvb_video_open(struct inode *inode, struct file *file)
1450{
1451 struct dvb_device *dvbdev = file->private_data;
1452 struct av7110 *av7110 = dvbdev->priv;
1453 int err;
1454
1455 dprintk(2, "av7110:%p, \n", av7110);
1456
1457 if ((err = dvb_generic_open(inode, file)) < 0)
1458 return err;
1459
1460 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1461 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1462 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1463 av7110->video_blank = 1;
1464 av7110->audiostate.AV_sync_state = 1;
1465 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1466
1467 /* empty event queue */
1468 av7110->video_events.eventr = av7110->video_events.eventw = 0;
1469 }
1470
1471 return 0;
1472}
1473
1474static int dvb_video_release(struct inode *inode, struct file *file)
1475{
1476 struct dvb_device *dvbdev = file->private_data;
1477 struct av7110 *av7110 = dvbdev->priv;
1478
1479 dprintk(2, "av7110:%p, \n", av7110);
1480
1481 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1482 av7110_av_stop(av7110, RP_VIDEO);
1483 }
1484
1485 return dvb_generic_release(inode, file);
1486}
1487
1488static int dvb_audio_open(struct inode *inode, struct file *file)
1489{
1490 struct dvb_device *dvbdev = file->private_data;
1491 struct av7110 *av7110 = dvbdev->priv;
1492 int err = dvb_generic_open(inode, file);
1493
1494 dprintk(2, "av7110:%p, \n", av7110);
1495
1496 if (err < 0)
1497 return err;
1498 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1499 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1500 return 0;
1501}
1502
1503static int dvb_audio_release(struct inode *inode, struct file *file)
1504{
1505 struct dvb_device *dvbdev = file->private_data;
1506 struct av7110 *av7110 = dvbdev->priv;
1507
1508 dprintk(2, "av7110:%p, \n", av7110);
1509
1510 av7110_av_stop(av7110, RP_AUDIO);
1511 return dvb_generic_release(inode, file);
1512}
1513
1514
1515
1516/******************************************************************************
1517 * driver registration
1518 ******************************************************************************/
1519
1520static const struct file_operations dvb_video_fops = {
1521 .owner = THIS_MODULE,
1522 .write = dvb_video_write,
1523 .unlocked_ioctl = dvb_generic_ioctl,
1524 .open = dvb_video_open,
1525 .release = dvb_video_release,
1526 .poll = dvb_video_poll,
1527 .llseek = noop_llseek,
1528};
1529
1530static struct dvb_device dvbdev_video = {
1531 .priv = NULL,
1532 .users = 6,
1533 .readers = 5, /* arbitrary */
1534 .writers = 1,
1535 .fops = &dvb_video_fops,
1536 .kernel_ioctl = dvb_video_ioctl,
1537};
1538
1539static const struct file_operations dvb_audio_fops = {
1540 .owner = THIS_MODULE,
1541 .write = dvb_audio_write,
1542 .unlocked_ioctl = dvb_generic_ioctl,
1543 .open = dvb_audio_open,
1544 .release = dvb_audio_release,
1545 .poll = dvb_audio_poll,
1546 .llseek = noop_llseek,
1547};
1548
1549static struct dvb_device dvbdev_audio = {
1550 .priv = NULL,
1551 .users = 1,
1552 .writers = 1,
1553 .fops = &dvb_audio_fops,
1554 .kernel_ioctl = dvb_audio_ioctl,
1555};
1556
1557
1558int av7110_av_register(struct av7110 *av7110)
1559{
1560 av7110->audiostate.AV_sync_state = 0;
1561 av7110->audiostate.mute_state = 0;
1562 av7110->audiostate.play_state = AUDIO_STOPPED;
1563 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1564 av7110->audiostate.channel_select = AUDIO_STEREO;
1565 av7110->audiostate.bypass_mode = 0;
1566
1567 av7110->videostate.video_blank = 0;
1568 av7110->videostate.play_state = VIDEO_STOPPED;
1569 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1570 av7110->videostate.video_format = VIDEO_FORMAT_4_3;
1571 av7110->videostate.display_format = VIDEO_LETTER_BOX;
1572 av7110->display_ar = VIDEO_FORMAT_4_3;
1573 av7110->display_panscan = VID_VC_AND_PS_PREF;
1574
1575 init_waitqueue_head(&av7110->video_events.wait_queue);
1576 spin_lock_init(&av7110->video_events.lock);
1577 av7110->video_events.eventw = av7110->video_events.eventr = 0;
1578 av7110->video_events.overflow = 0;
1579 memset(&av7110->video_size, 0, sizeof (video_size_t));
1580
1581 dvb_register_device(&av7110->dvb_adapter, &av7110->video_dev,
1582 &dvbdev_video, av7110, DVB_DEVICE_VIDEO);
1583
1584 dvb_register_device(&av7110->dvb_adapter, &av7110->audio_dev,
1585 &dvbdev_audio, av7110, DVB_DEVICE_AUDIO);
1586
1587 return 0;
1588}
1589
1590void av7110_av_unregister(struct av7110 *av7110)
1591{
1592 dvb_unregister_device(av7110->audio_dev);
1593 dvb_unregister_device(av7110->video_dev);
1594}
1595
1596int av7110_av_init(struct av7110 *av7110)
1597{
1598 void (*play[])(u8 *, int, void *) = { play_audio_cb, play_video_cb };
1599 int i, ret;
1600
1601 for (i = 0; i < 2; i++) {
1602 struct ipack *ipack = av7110->ipack + i;
1603
1604 ret = av7110_ipack_init(ipack, IPACKS, play[i]);
1605 if (ret < 0) {
1606 if (i)
1607 av7110_ipack_free(--ipack);
1608 goto out;
1609 }
1610 ipack->data = av7110;
1611 }
1612
1613 dvb_ringbuffer_init(&av7110->avout, av7110->iobuf, AVOUTLEN);
1614 dvb_ringbuffer_init(&av7110->aout, av7110->iobuf + AVOUTLEN, AOUTLEN);
1615
1616 av7110->kbuf[0] = (u8 *)(av7110->iobuf + AVOUTLEN + AOUTLEN + BMPLEN);
1617 av7110->kbuf[1] = av7110->kbuf[0] + 2 * IPACKS;
1618out:
1619 return ret;
1620}
1621
1622void av7110_av_exit(struct av7110 *av7110)
1623{
1624 av7110_ipack_free(&av7110->ipack[0]);
1625 av7110_ipack_free(&av7110->ipack[1]);
1626}
diff --git a/drivers/media/dvb/ttpci/av7110_av.h b/drivers/media/dvb/ttpci/av7110_av.h
new file mode 100644
index 00000000000..5f02ef85e47
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_av.h
@@ -0,0 +1,30 @@
1#ifndef _AV7110_AV_H_
2#define _AV7110_AV_H_
3
4struct av7110;
5
6extern int av7110_set_vidmode(struct av7110 *av7110,
7 enum av7110_video_mode mode);
8
9extern int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len);
10extern int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen);
11extern int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len);
12
13extern int av7110_set_volume(struct av7110 *av7110, int volleft, int volright);
14extern int av7110_av_stop(struct av7110 *av7110, int av);
15extern int av7110_av_start_record(struct av7110 *av7110, int av,
16 struct dvb_demux_feed *dvbdmxfeed);
17extern int av7110_av_start_play(struct av7110 *av7110, int av);
18
19extern void dvb_video_add_event(struct av7110 *av7110, struct video_event *event);
20
21extern void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed);
22extern void av7110_p2t_write(u8 const *buf, long int length, u16 pid, struct av7110_p2t *p);
23
24extern int av7110_av_register(struct av7110 *av7110);
25extern void av7110_av_unregister(struct av7110 *av7110);
26extern int av7110_av_init(struct av7110 *av7110);
27extern void av7110_av_exit(struct av7110 *av7110);
28
29
30#endif /* _AV7110_AV_H_ */
diff --git a/drivers/media/dvb/ttpci/av7110_ca.c b/drivers/media/dvb/ttpci/av7110_ca.c
new file mode 100644
index 00000000000..9fc1dd0ba4c
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_ca.c
@@ -0,0 +1,387 @@
1/*
2 * av7110_ca.c: CA and CI stuff
3 *
4 * Copyright (C) 1999-2002 Ralph Metzler
5 * & Marcus Metzler for convergence integrated media GmbH
6 *
7 * originally based on code by:
8 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26 *
27 *
28 * the project's page is at http://www.linuxtv.org/
29 */
30
31#include <linux/kernel.h>
32#include <linux/types.h>
33#include <linux/delay.h>
34#include <linux/fs.h>
35#include <linux/timer.h>
36#include <linux/poll.h>
37#include <linux/gfp.h>
38
39#include "av7110.h"
40#include "av7110_hw.h"
41#include "av7110_ca.h"
42
43
44void CI_handle(struct av7110 *av7110, u8 *data, u16 len)
45{
46 dprintk(8, "av7110:%p\n",av7110);
47
48 if (len < 3)
49 return;
50 switch (data[0]) {
51 case CI_MSG_CI_INFO:
52 if (data[2] != 1 && data[2] != 2)
53 break;
54 switch (data[1]) {
55 case 0:
56 av7110->ci_slot[data[2] - 1].flags = 0;
57 break;
58 case 1:
59 av7110->ci_slot[data[2] - 1].flags |= CA_CI_MODULE_PRESENT;
60 break;
61 case 2:
62 av7110->ci_slot[data[2] - 1].flags |= CA_CI_MODULE_READY;
63 break;
64 }
65 break;
66 case CI_SWITCH_PRG_REPLY:
67 //av7110->ci_stat=data[1];
68 break;
69 default:
70 break;
71 }
72}
73
74
75void ci_get_data(struct dvb_ringbuffer *cibuf, u8 *data, int len)
76{
77 if (dvb_ringbuffer_free(cibuf) < len + 2)
78 return;
79
80 DVB_RINGBUFFER_WRITE_BYTE(cibuf, len >> 8);
81 DVB_RINGBUFFER_WRITE_BYTE(cibuf, len & 0xff);
82 dvb_ringbuffer_write(cibuf, data, len);
83 wake_up_interruptible(&cibuf->queue);
84}
85
86
87/******************************************************************************
88 * CI link layer file ops
89 ******************************************************************************/
90
91static int ci_ll_init(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf, int size)
92{
93 struct dvb_ringbuffer *tab[] = { cirbuf, ciwbuf, NULL }, **p;
94 void *data;
95
96 for (p = tab; *p; p++) {
97 data = vmalloc(size);
98 if (!data) {
99 while (p-- != tab) {
100 vfree(p[0]->data);
101 p[0]->data = NULL;
102 }
103 return -ENOMEM;
104 }
105 dvb_ringbuffer_init(*p, data, size);
106 }
107 return 0;
108}
109
110static void ci_ll_flush(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf)
111{
112 dvb_ringbuffer_flush_spinlock_wakeup(cirbuf);
113 dvb_ringbuffer_flush_spinlock_wakeup(ciwbuf);
114}
115
116static void ci_ll_release(struct dvb_ringbuffer *cirbuf, struct dvb_ringbuffer *ciwbuf)
117{
118 vfree(cirbuf->data);
119 cirbuf->data = NULL;
120 vfree(ciwbuf->data);
121 ciwbuf->data = NULL;
122}
123
124static int ci_ll_reset(struct dvb_ringbuffer *cibuf, struct file *file,
125 int slots, ca_slot_info_t *slot)
126{
127 int i;
128 int len = 0;
129 u8 msg[8] = { 0x00, 0x06, 0x00, 0x00, 0xff, 0x02, 0x00, 0x00 };
130
131 for (i = 0; i < 2; i++) {
132 if (slots & (1 << i))
133 len += 8;
134 }
135
136 if (dvb_ringbuffer_free(cibuf) < len)
137 return -EBUSY;
138
139 for (i = 0; i < 2; i++) {
140 if (slots & (1 << i)) {
141 msg[2] = i;
142 dvb_ringbuffer_write(cibuf, msg, 8);
143 slot[i].flags = 0;
144 }
145 }
146
147 return 0;
148}
149
150static ssize_t ci_ll_write(struct dvb_ringbuffer *cibuf, struct file *file,
151 const char __user *buf, size_t count, loff_t *ppos)
152{
153 int free;
154 int non_blocking = file->f_flags & O_NONBLOCK;
155 u8 *page = (u8 *)__get_free_page(GFP_USER);
156 int res;
157
158 if (!page)
159 return -ENOMEM;
160
161 res = -EINVAL;
162 if (count > 2048)
163 goto out;
164
165 res = -EFAULT;
166 if (copy_from_user(page, buf, count))
167 goto out;
168
169 free = dvb_ringbuffer_free(cibuf);
170 if (count + 2 > free) {
171 res = -EWOULDBLOCK;
172 if (non_blocking)
173 goto out;
174 res = -ERESTARTSYS;
175 if (wait_event_interruptible(cibuf->queue,
176 (dvb_ringbuffer_free(cibuf) >= count + 2)))
177 goto out;
178 }
179
180 DVB_RINGBUFFER_WRITE_BYTE(cibuf, count >> 8);
181 DVB_RINGBUFFER_WRITE_BYTE(cibuf, count & 0xff);
182
183 res = dvb_ringbuffer_write(cibuf, page, count);
184out:
185 free_page((unsigned long)page);
186 return res;
187}
188
189static ssize_t ci_ll_read(struct dvb_ringbuffer *cibuf, struct file *file,
190 char __user *buf, size_t count, loff_t *ppos)
191{
192 int avail;
193 int non_blocking = file->f_flags & O_NONBLOCK;
194 ssize_t len;
195
196 if (!cibuf->data || !count)
197 return 0;
198 if (non_blocking && (dvb_ringbuffer_empty(cibuf)))
199 return -EWOULDBLOCK;
200 if (wait_event_interruptible(cibuf->queue,
201 !dvb_ringbuffer_empty(cibuf)))
202 return -ERESTARTSYS;
203 avail = dvb_ringbuffer_avail(cibuf);
204 if (avail < 4)
205 return 0;
206 len = DVB_RINGBUFFER_PEEK(cibuf, 0) << 8;
207 len |= DVB_RINGBUFFER_PEEK(cibuf, 1);
208 if (avail < len + 2 || count < len)
209 return -EINVAL;
210 DVB_RINGBUFFER_SKIP(cibuf, 2);
211
212 return dvb_ringbuffer_read_user(cibuf, buf, len);
213}
214
215static int dvb_ca_open(struct inode *inode, struct file *file)
216{
217 struct dvb_device *dvbdev = file->private_data;
218 struct av7110 *av7110 = dvbdev->priv;
219 int err = dvb_generic_open(inode, file);
220
221 dprintk(8, "av7110:%p\n",av7110);
222
223 if (err < 0)
224 return err;
225 ci_ll_flush(&av7110->ci_rbuffer, &av7110->ci_wbuffer);
226 return 0;
227}
228
229static unsigned int dvb_ca_poll (struct file *file, poll_table *wait)
230{
231 struct dvb_device *dvbdev = file->private_data;
232 struct av7110 *av7110 = dvbdev->priv;
233 struct dvb_ringbuffer *rbuf = &av7110->ci_rbuffer;
234 struct dvb_ringbuffer *wbuf = &av7110->ci_wbuffer;
235 unsigned int mask = 0;
236
237 dprintk(8, "av7110:%p\n",av7110);
238
239 poll_wait(file, &rbuf->queue, wait);
240 poll_wait(file, &wbuf->queue, wait);
241
242 if (!dvb_ringbuffer_empty(rbuf))
243 mask |= (POLLIN | POLLRDNORM);
244
245 if (dvb_ringbuffer_free(wbuf) > 1024)
246 mask |= (POLLOUT | POLLWRNORM);
247
248 return mask;
249}
250
251static int dvb_ca_ioctl(struct file *file, unsigned int cmd, void *parg)
252{
253 struct dvb_device *dvbdev = file->private_data;
254 struct av7110 *av7110 = dvbdev->priv;
255 unsigned long arg = (unsigned long) parg;
256
257 dprintk(8, "av7110:%p\n",av7110);
258
259 switch (cmd) {
260 case CA_RESET:
261 return ci_ll_reset(&av7110->ci_wbuffer, file, arg, &av7110->ci_slot[0]);
262 break;
263 case CA_GET_CAP:
264 {
265 ca_caps_t cap;
266
267 cap.slot_num = 2;
268 cap.slot_type = (FW_CI_LL_SUPPORT(av7110->arm_app) ?
269 CA_CI_LINK : CA_CI) | CA_DESCR;
270 cap.descr_num = 16;
271 cap.descr_type = CA_ECD;
272 memcpy(parg, &cap, sizeof(cap));
273 break;
274 }
275
276 case CA_GET_SLOT_INFO:
277 {
278 ca_slot_info_t *info=(ca_slot_info_t *)parg;
279
280 if (info->num < 0 || info->num > 1)
281 return -EINVAL;
282 av7110->ci_slot[info->num].num = info->num;
283 av7110->ci_slot[info->num].type = FW_CI_LL_SUPPORT(av7110->arm_app) ?
284 CA_CI_LINK : CA_CI;
285 memcpy(info, &av7110->ci_slot[info->num], sizeof(ca_slot_info_t));
286 break;
287 }
288
289 case CA_GET_MSG:
290 break;
291
292 case CA_SEND_MSG:
293 break;
294
295 case CA_GET_DESCR_INFO:
296 {
297 ca_descr_info_t info;
298
299 info.num = 16;
300 info.type = CA_ECD;
301 memcpy(parg, &info, sizeof (info));
302 break;
303 }
304
305 case CA_SET_DESCR:
306 {
307 ca_descr_t *descr = (ca_descr_t*) parg;
308
309 if (descr->index >= 16)
310 return -EINVAL;
311 if (descr->parity > 1)
312 return -EINVAL;
313 av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetDescr, 5,
314 (descr->index<<8)|descr->parity,
315 (descr->cw[0]<<8)|descr->cw[1],
316 (descr->cw[2]<<8)|descr->cw[3],
317 (descr->cw[4]<<8)|descr->cw[5],
318 (descr->cw[6]<<8)|descr->cw[7]);
319 break;
320 }
321
322 default:
323 return -EINVAL;
324 }
325 return 0;
326}
327
328static ssize_t dvb_ca_write(struct file *file, const char __user *buf,
329 size_t count, loff_t *ppos)
330{
331 struct dvb_device *dvbdev = file->private_data;
332 struct av7110 *av7110 = dvbdev->priv;
333
334 dprintk(8, "av7110:%p\n",av7110);
335 return ci_ll_write(&av7110->ci_wbuffer, file, buf, count, ppos);
336}
337
338static ssize_t dvb_ca_read(struct file *file, char __user *buf,
339 size_t count, loff_t *ppos)
340{
341 struct dvb_device *dvbdev = file->private_data;
342 struct av7110 *av7110 = dvbdev->priv;
343
344 dprintk(8, "av7110:%p\n",av7110);
345 return ci_ll_read(&av7110->ci_rbuffer, file, buf, count, ppos);
346}
347
348static const struct file_operations dvb_ca_fops = {
349 .owner = THIS_MODULE,
350 .read = dvb_ca_read,
351 .write = dvb_ca_write,
352 .unlocked_ioctl = dvb_generic_ioctl,
353 .open = dvb_ca_open,
354 .release = dvb_generic_release,
355 .poll = dvb_ca_poll,
356 .llseek = default_llseek,
357};
358
359static struct dvb_device dvbdev_ca = {
360 .priv = NULL,
361 .users = 1,
362 .writers = 1,
363 .fops = &dvb_ca_fops,
364 .kernel_ioctl = dvb_ca_ioctl,
365};
366
367
368int av7110_ca_register(struct av7110 *av7110)
369{
370 return dvb_register_device(&av7110->dvb_adapter, &av7110->ca_dev,
371 &dvbdev_ca, av7110, DVB_DEVICE_CA);
372}
373
374void av7110_ca_unregister(struct av7110 *av7110)
375{
376 dvb_unregister_device(av7110->ca_dev);
377}
378
379int av7110_ca_init(struct av7110* av7110)
380{
381 return ci_ll_init(&av7110->ci_rbuffer, &av7110->ci_wbuffer, 8192);
382}
383
384void av7110_ca_exit(struct av7110* av7110)
385{
386 ci_ll_release(&av7110->ci_rbuffer, &av7110->ci_wbuffer);
387}
diff --git a/drivers/media/dvb/ttpci/av7110_ca.h b/drivers/media/dvb/ttpci/av7110_ca.h
new file mode 100644
index 00000000000..70ee855ece1
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_ca.h
@@ -0,0 +1,14 @@
1#ifndef _AV7110_CA_H_
2#define _AV7110_CA_H_
3
4struct av7110;
5
6extern void CI_handle(struct av7110 *av7110, u8 *data, u16 len);
7extern void ci_get_data(struct dvb_ringbuffer *cibuf, u8 *data, int len);
8
9extern int av7110_ca_register(struct av7110 *av7110);
10extern void av7110_ca_unregister(struct av7110 *av7110);
11extern int av7110_ca_init(struct av7110* av7110);
12extern void av7110_ca_exit(struct av7110* av7110);
13
14#endif /* _AV7110_CA_H_ */
diff --git a/drivers/media/dvb/ttpci/av7110_hw.c b/drivers/media/dvb/ttpci/av7110_hw.c
new file mode 100644
index 00000000000..f1cbfe52698
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_hw.c
@@ -0,0 +1,1208 @@
1/*
2 * av7110_hw.c: av7110 low level hardware access and firmware interface
3 *
4 * Copyright (C) 1999-2002 Ralph Metzler
5 * & Marcus Metzler for convergence integrated media GmbH
6 *
7 * originally based on code by:
8 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
24 *
25 * the project's page is at http://www.linuxtv.org/
26 */
27
28/* for debugging ARM communication: */
29//#define COM_DEBUG
30
31#include <stdarg.h>
32#include <linux/types.h>
33#include <linux/kernel.h>
34#include <linux/string.h>
35#include <linux/delay.h>
36#include <linux/fs.h>
37
38#include "av7110.h"
39#include "av7110_hw.h"
40
41#define _NOHANDSHAKE
42
43/****************************************************************************
44 * DEBI functions
45 ****************************************************************************/
46
47/* This DEBI code is based on the Stradis driver
48 by Nathan Laredo <laredo@gnu.org> */
49
50int av7110_debiwrite(struct av7110 *av7110, u32 config,
51 int addr, u32 val, int count)
52{
53 struct saa7146_dev *dev = av7110->dev;
54
55 if (count <= 0 || count > 32764) {
56 printk("%s: invalid count %d\n", __func__, count);
57 return -1;
58 }
59 if (saa7146_wait_for_debi_done(av7110->dev, 0) < 0) {
60 printk("%s: wait_for_debi_done failed\n", __func__);
61 return -1;
62 }
63 saa7146_write(dev, DEBI_CONFIG, config);
64 if (count <= 4) /* immediate transfer */
65 saa7146_write(dev, DEBI_AD, val);
66 else /* block transfer */
67 saa7146_write(dev, DEBI_AD, av7110->debi_bus);
68 saa7146_write(dev, DEBI_COMMAND, (count << 17) | (addr & 0xffff));
69 saa7146_write(dev, MC2, (2 << 16) | 2);
70 return 0;
71}
72
73u32 av7110_debiread(struct av7110 *av7110, u32 config, int addr, int count)
74{
75 struct saa7146_dev *dev = av7110->dev;
76 u32 result = 0;
77
78 if (count > 32764 || count <= 0) {
79 printk("%s: invalid count %d\n", __func__, count);
80 return 0;
81 }
82 if (saa7146_wait_for_debi_done(av7110->dev, 0) < 0) {
83 printk("%s: wait_for_debi_done #1 failed\n", __func__);
84 return 0;
85 }
86 saa7146_write(dev, DEBI_AD, av7110->debi_bus);
87 saa7146_write(dev, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
88
89 saa7146_write(dev, DEBI_CONFIG, config);
90 saa7146_write(dev, MC2, (2 << 16) | 2);
91 if (count > 4)
92 return count;
93 if (saa7146_wait_for_debi_done(av7110->dev, 0) < 0) {
94 printk("%s: wait_for_debi_done #2 failed\n", __func__);
95 return 0;
96 }
97
98 result = saa7146_read(dev, DEBI_AD);
99 result &= (0xffffffffUL >> ((4 - count) * 8));
100 return result;
101}
102
103
104
105/* av7110 ARM core boot stuff */
106#if 0
107void av7110_reset_arm(struct av7110 *av7110)
108{
109 saa7146_setgpio(av7110->dev, RESET_LINE, SAA7146_GPIO_OUTLO);
110
111 /* Disable DEBI and GPIO irq */
112 SAA7146_IER_DISABLE(av7110->dev, MASK_19 | MASK_03);
113 SAA7146_ISR_CLEAR(av7110->dev, MASK_19 | MASK_03);
114
115 saa7146_setgpio(av7110->dev, RESET_LINE, SAA7146_GPIO_OUTHI);
116 msleep(30); /* the firmware needs some time to initialize */
117
118 ARM_ResetMailBox(av7110);
119
120 SAA7146_ISR_CLEAR(av7110->dev, MASK_19 | MASK_03);
121 SAA7146_IER_ENABLE(av7110->dev, MASK_03);
122
123 av7110->arm_ready = 1;
124 dprintk(1, "reset ARM\n");
125}
126#endif /* 0 */
127
128static int waitdebi(struct av7110 *av7110, int adr, int state)
129{
130 int k;
131
132 dprintk(4, "%p\n", av7110);
133
134 for (k = 0; k < 100; k++) {
135 if (irdebi(av7110, DEBINOSWAP, adr, 0, 2) == state)
136 return 0;
137 udelay(5);
138 }
139 return -ETIMEDOUT;
140}
141
142static int load_dram(struct av7110 *av7110, u32 *data, int len)
143{
144 int i;
145 int blocks, rest;
146 u32 base, bootblock = AV7110_BOOT_BLOCK;
147
148 dprintk(4, "%p\n", av7110);
149
150 blocks = len / AV7110_BOOT_MAX_SIZE;
151 rest = len % AV7110_BOOT_MAX_SIZE;
152 base = DRAM_START_CODE;
153
154 for (i = 0; i < blocks; i++) {
155 if (waitdebi(av7110, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_EMPTY) < 0) {
156 printk(KERN_ERR "dvb-ttpci: load_dram(): timeout at block %d\n", i);
157 return -ETIMEDOUT;
158 }
159 dprintk(4, "writing DRAM block %d\n", i);
160 mwdebi(av7110, DEBISWAB, bootblock,
161 ((u8 *)data) + i * AV7110_BOOT_MAX_SIZE, AV7110_BOOT_MAX_SIZE);
162 bootblock ^= 0x1400;
163 iwdebi(av7110, DEBISWAB, AV7110_BOOT_BASE, swab32(base), 4);
164 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_SIZE, AV7110_BOOT_MAX_SIZE, 2);
165 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_FULL, 2);
166 base += AV7110_BOOT_MAX_SIZE;
167 }
168
169 if (rest > 0) {
170 if (waitdebi(av7110, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_EMPTY) < 0) {
171 printk(KERN_ERR "dvb-ttpci: load_dram(): timeout at last block\n");
172 return -ETIMEDOUT;
173 }
174 if (rest > 4)
175 mwdebi(av7110, DEBISWAB, bootblock,
176 ((u8 *)data) + i * AV7110_BOOT_MAX_SIZE, rest);
177 else
178 mwdebi(av7110, DEBISWAB, bootblock,
179 ((u8 *)data) + i * AV7110_BOOT_MAX_SIZE - 4, rest + 4);
180
181 iwdebi(av7110, DEBISWAB, AV7110_BOOT_BASE, swab32(base), 4);
182 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_SIZE, rest, 2);
183 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_FULL, 2);
184 }
185 if (waitdebi(av7110, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_EMPTY) < 0) {
186 printk(KERN_ERR "dvb-ttpci: load_dram(): timeout after last block\n");
187 return -ETIMEDOUT;
188 }
189 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_SIZE, 0, 2);
190 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_FULL, 2);
191 if (waitdebi(av7110, AV7110_BOOT_STATE, BOOTSTATE_AV7110_BOOT_COMPLETE) < 0) {
192 printk(KERN_ERR "dvb-ttpci: load_dram(): final handshake timeout\n");
193 return -ETIMEDOUT;
194 }
195 return 0;
196}
197
198
199/* we cannot write av7110 DRAM directly, so load a bootloader into
200 * the DPRAM which implements a simple boot protocol */
201int av7110_bootarm(struct av7110 *av7110)
202{
203 const struct firmware *fw;
204 const char *fw_name = "av7110/bootcode.bin";
205 struct saa7146_dev *dev = av7110->dev;
206 u32 ret;
207 int i;
208
209 dprintk(4, "%p\n", av7110);
210
211 av7110->arm_ready = 0;
212
213 saa7146_setgpio(dev, RESET_LINE, SAA7146_GPIO_OUTLO);
214
215 /* Disable DEBI and GPIO irq */
216 SAA7146_IER_DISABLE(av7110->dev, MASK_03 | MASK_19);
217 SAA7146_ISR_CLEAR(av7110->dev, MASK_19 | MASK_03);
218
219 /* enable DEBI */
220 saa7146_write(av7110->dev, MC1, 0x08800880);
221 saa7146_write(av7110->dev, DD1_STREAM_B, 0x00000000);
222 saa7146_write(av7110->dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
223
224 /* test DEBI */
225 iwdebi(av7110, DEBISWAP, DPRAM_BASE, 0x76543210, 4);
226 /* FIXME: Why does Nexus CA require 2x iwdebi for first init? */
227 iwdebi(av7110, DEBISWAP, DPRAM_BASE, 0x76543210, 4);
228
229 if ((ret=irdebi(av7110, DEBINOSWAP, DPRAM_BASE, 0, 4)) != 0x10325476) {
230 printk(KERN_ERR "dvb-ttpci: debi test in av7110_bootarm() failed: "
231 "%08x != %08x (check your BIOS 'Plug&Play OS' settings)\n",
232 ret, 0x10325476);
233 return -1;
234 }
235 for (i = 0; i < 8192; i += 4)
236 iwdebi(av7110, DEBISWAP, DPRAM_BASE + i, 0x00, 4);
237 dprintk(2, "debi test OK\n");
238
239 /* boot */
240 dprintk(1, "load boot code\n");
241 saa7146_setgpio(dev, ARM_IRQ_LINE, SAA7146_GPIO_IRQLO);
242 //saa7146_setgpio(dev, DEBI_DONE_LINE, SAA7146_GPIO_INPUT);
243 //saa7146_setgpio(dev, 3, SAA7146_GPIO_INPUT);
244
245 ret = request_firmware(&fw, fw_name, &dev->pci->dev);
246 if (ret) {
247 printk(KERN_ERR "dvb-ttpci: Failed to load firmware \"%s\"\n",
248 fw_name);
249 return ret;
250 }
251
252 mwdebi(av7110, DEBISWAB, DPRAM_BASE, fw->data, fw->size);
253 release_firmware(fw);
254 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_FULL, 2);
255
256 if (saa7146_wait_for_debi_done(av7110->dev, 1)) {
257 printk(KERN_ERR "dvb-ttpci: av7110_bootarm(): "
258 "saa7146_wait_for_debi_done() timed out\n");
259 return -ETIMEDOUT;
260 }
261 saa7146_setgpio(dev, RESET_LINE, SAA7146_GPIO_OUTHI);
262 mdelay(1);
263
264 dprintk(1, "load dram code\n");
265 if (load_dram(av7110, (u32 *)av7110->bin_root, av7110->size_root) < 0) {
266 printk(KERN_ERR "dvb-ttpci: av7110_bootarm(): "
267 "load_dram() failed\n");
268 return -1;
269 }
270
271 saa7146_setgpio(dev, RESET_LINE, SAA7146_GPIO_OUTLO);
272 mdelay(1);
273
274 dprintk(1, "load dpram code\n");
275 mwdebi(av7110, DEBISWAB, DPRAM_BASE, av7110->bin_dpram, av7110->size_dpram);
276
277 if (saa7146_wait_for_debi_done(av7110->dev, 1)) {
278 printk(KERN_ERR "dvb-ttpci: av7110_bootarm(): "
279 "saa7146_wait_for_debi_done() timed out after loading DRAM\n");
280 return -ETIMEDOUT;
281 }
282 saa7146_setgpio(dev, RESET_LINE, SAA7146_GPIO_OUTHI);
283 msleep(30); /* the firmware needs some time to initialize */
284
285 //ARM_ClearIrq(av7110);
286 ARM_ResetMailBox(av7110);
287 SAA7146_ISR_CLEAR(av7110->dev, MASK_19 | MASK_03);
288 SAA7146_IER_ENABLE(av7110->dev, MASK_03);
289
290 av7110->arm_errors = 0;
291 av7110->arm_ready = 1;
292 return 0;
293}
294MODULE_FIRMWARE("av7110/bootcode.bin");
295
296/****************************************************************************
297 * DEBI command polling
298 ****************************************************************************/
299
300int av7110_wait_msgstate(struct av7110 *av7110, u16 flags)
301{
302 unsigned long start;
303 u32 stat;
304 int err;
305
306 if (FW_VERSION(av7110->arm_app) <= 0x261c) {
307 /* not supported by old firmware */
308 msleep(50);
309 return 0;
310 }
311
312 /* new firmware */
313 start = jiffies;
314 for (;;) {
315 err = time_after(jiffies, start + ARM_WAIT_FREE);
316 if (mutex_lock_interruptible(&av7110->dcomlock))
317 return -ERESTARTSYS;
318 stat = rdebi(av7110, DEBINOSWAP, MSGSTATE, 0, 2);
319 mutex_unlock(&av7110->dcomlock);
320 if ((stat & flags) == 0)
321 break;
322 if (err) {
323 printk(KERN_ERR "%s: timeout waiting for MSGSTATE %04x\n",
324 __func__, stat & flags);
325 return -ETIMEDOUT;
326 }
327 msleep(1);
328 }
329 return 0;
330}
331
332static int __av7110_send_fw_cmd(struct av7110 *av7110, u16* buf, int length)
333{
334 int i;
335 unsigned long start;
336 char *type = NULL;
337 u16 flags[2] = {0, 0};
338 u32 stat;
339 int err;
340
341// dprintk(4, "%p\n", av7110);
342
343 if (!av7110->arm_ready) {
344 dprintk(1, "arm not ready.\n");
345 return -ENXIO;
346 }
347
348 start = jiffies;
349 while (1) {
350 err = time_after(jiffies, start + ARM_WAIT_FREE);
351 if (rdebi(av7110, DEBINOSWAP, COMMAND, 0, 2) == 0)
352 break;
353 if (err) {
354 printk(KERN_ERR "dvb-ttpci: %s(): timeout waiting for COMMAND idle\n", __func__);
355 av7110->arm_errors++;
356 return -ETIMEDOUT;
357 }
358 msleep(1);
359 }
360
361 if (FW_VERSION(av7110->arm_app) <= 0x261f)
362 wdebi(av7110, DEBINOSWAP, COM_IF_LOCK, 0xffff, 2);
363
364#ifndef _NOHANDSHAKE
365 start = jiffies;
366 while (1) {
367 err = time_after(jiffies, start + ARM_WAIT_SHAKE);
368 if (rdebi(av7110, DEBINOSWAP, HANDSHAKE_REG, 0, 2) == 0)
369 break;
370 if (err) {
371 printk(KERN_ERR "dvb-ttpci: %s(): timeout waiting for HANDSHAKE_REG\n", __func__);
372 return -ETIMEDOUT;
373 }
374 msleep(1);
375 }
376#endif
377
378 switch ((buf[0] >> 8) & 0xff) {
379 case COMTYPE_PIDFILTER:
380 case COMTYPE_ENCODER:
381 case COMTYPE_REC_PLAY:
382 case COMTYPE_MPEGDECODER:
383 type = "MSG";
384 flags[0] = GPMQOver;
385 flags[1] = GPMQFull;
386 break;
387 case COMTYPE_OSD:
388 type = "OSD";
389 flags[0] = OSDQOver;
390 flags[1] = OSDQFull;
391 break;
392 case COMTYPE_MISC:
393 if (FW_VERSION(av7110->arm_app) >= 0x261d) {
394 type = "MSG";
395 flags[0] = GPMQOver;
396 flags[1] = GPMQBusy;
397 }
398 break;
399 default:
400 break;
401 }
402
403 if (type != NULL) {
404 /* non-immediate COMMAND type */
405 start = jiffies;
406 for (;;) {
407 err = time_after(jiffies, start + ARM_WAIT_FREE);
408 stat = rdebi(av7110, DEBINOSWAP, MSGSTATE, 0, 2);
409 if (stat & flags[0]) {
410 printk(KERN_ERR "%s: %s QUEUE overflow\n",
411 __func__, type);
412 return -1;
413 }
414 if ((stat & flags[1]) == 0)
415 break;
416 if (err) {
417 printk(KERN_ERR "%s: timeout waiting on busy %s QUEUE\n",
418 __func__, type);
419 av7110->arm_errors++;
420 return -ETIMEDOUT;
421 }
422 msleep(1);
423 }
424 }
425
426 for (i = 2; i < length; i++)
427 wdebi(av7110, DEBINOSWAP, COMMAND + 2 * i, (u32) buf[i], 2);
428
429 if (length)
430 wdebi(av7110, DEBINOSWAP, COMMAND + 2, (u32) buf[1], 2);
431 else
432 wdebi(av7110, DEBINOSWAP, COMMAND + 2, 0, 2);
433
434 wdebi(av7110, DEBINOSWAP, COMMAND, (u32) buf[0], 2);
435
436 if (FW_VERSION(av7110->arm_app) <= 0x261f)
437 wdebi(av7110, DEBINOSWAP, COM_IF_LOCK, 0x0000, 2);
438
439#ifdef COM_DEBUG
440 start = jiffies;
441 while (1) {
442 err = time_after(jiffies, start + ARM_WAIT_FREE);
443 if (rdebi(av7110, DEBINOSWAP, COMMAND, 0, 2) == 0)
444 break;
445 if (err) {
446 printk(KERN_ERR "dvb-ttpci: %s(): timeout waiting for COMMAND %d to complete\n",
447 __func__, (buf[0] >> 8) & 0xff);
448 return -ETIMEDOUT;
449 }
450 msleep(1);
451 }
452
453 stat = rdebi(av7110, DEBINOSWAP, MSGSTATE, 0, 2);
454 if (stat & GPMQOver) {
455 printk(KERN_ERR "dvb-ttpci: %s(): GPMQOver\n", __func__);
456 return -ENOSPC;
457 }
458 else if (stat & OSDQOver) {
459 printk(KERN_ERR "dvb-ttpci: %s(): OSDQOver\n", __func__);
460 return -ENOSPC;
461 }
462#endif
463
464 return 0;
465}
466
467static int av7110_send_fw_cmd(struct av7110 *av7110, u16* buf, int length)
468{
469 int ret;
470
471// dprintk(4, "%p\n", av7110);
472
473 if (!av7110->arm_ready) {
474 dprintk(1, "arm not ready.\n");
475 return -1;
476 }
477 if (mutex_lock_interruptible(&av7110->dcomlock))
478 return -ERESTARTSYS;
479
480 ret = __av7110_send_fw_cmd(av7110, buf, length);
481 mutex_unlock(&av7110->dcomlock);
482 if (ret && ret!=-ERESTARTSYS)
483 printk(KERN_ERR "dvb-ttpci: %s(): av7110_send_fw_cmd error %d\n",
484 __func__, ret);
485 return ret;
486}
487
488int av7110_fw_cmd(struct av7110 *av7110, int type, int com, int num, ...)
489{
490 va_list args;
491 u16 buf[num + 2];
492 int i, ret;
493
494// dprintk(4, "%p\n", av7110);
495
496 buf[0] = ((type << 8) | com);
497 buf[1] = num;
498
499 if (num) {
500 va_start(args, num);
501 for (i = 0; i < num; i++)
502 buf[i + 2] = va_arg(args, u32);
503 va_end(args);
504 }
505
506 ret = av7110_send_fw_cmd(av7110, buf, num + 2);
507 if (ret && ret != -ERESTARTSYS)
508 printk(KERN_ERR "dvb-ttpci: av7110_fw_cmd error %d\n", ret);
509 return ret;
510}
511
512#if 0
513int av7110_send_ci_cmd(struct av7110 *av7110, u8 subcom, u8 *buf, u8 len)
514{
515 int i, ret;
516 u16 cmd[18] = { ((COMTYPE_COMMON_IF << 8) + subcom),
517 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
518
519 dprintk(4, "%p\n", av7110);
520
521 for(i = 0; i < len && i < 32; i++)
522 {
523 if(i % 2 == 0)
524 cmd[(i / 2) + 2] = (u16)(buf[i]) << 8;
525 else
526 cmd[(i / 2) + 2] |= buf[i];
527 }
528
529 ret = av7110_send_fw_cmd(av7110, cmd, 18);
530 if (ret && ret != -ERESTARTSYS)
531 printk(KERN_ERR "dvb-ttpci: av7110_send_ci_cmd error %d\n", ret);
532 return ret;
533}
534#endif /* 0 */
535
536int av7110_fw_request(struct av7110 *av7110, u16 *request_buf,
537 int request_buf_len, u16 *reply_buf, int reply_buf_len)
538{
539 int err;
540 s16 i;
541 unsigned long start;
542#ifdef COM_DEBUG
543 u32 stat;
544#endif
545
546 dprintk(4, "%p\n", av7110);
547
548 if (!av7110->arm_ready) {
549 dprintk(1, "arm not ready.\n");
550 return -1;
551 }
552
553 if (mutex_lock_interruptible(&av7110->dcomlock))
554 return -ERESTARTSYS;
555
556 if ((err = __av7110_send_fw_cmd(av7110, request_buf, request_buf_len)) < 0) {
557 mutex_unlock(&av7110->dcomlock);
558 printk(KERN_ERR "dvb-ttpci: av7110_fw_request error %d\n", err);
559 return err;
560 }
561
562 start = jiffies;
563 while (1) {
564 err = time_after(jiffies, start + ARM_WAIT_FREE);
565 if (rdebi(av7110, DEBINOSWAP, COMMAND, 0, 2) == 0)
566 break;
567 if (err) {
568 printk(KERN_ERR "%s: timeout waiting for COMMAND to complete\n", __func__);
569 mutex_unlock(&av7110->dcomlock);
570 return -ETIMEDOUT;
571 }
572#ifdef _NOHANDSHAKE
573 msleep(1);
574#endif
575 }
576
577#ifndef _NOHANDSHAKE
578 start = jiffies;
579 while (1) {
580 err = time_after(jiffies, start + ARM_WAIT_SHAKE);
581 if (rdebi(av7110, DEBINOSWAP, HANDSHAKE_REG, 0, 2) == 0)
582 break;
583 if (err) {
584 printk(KERN_ERR "%s: timeout waiting for HANDSHAKE_REG\n", __func__);
585 mutex_unlock(&av7110->dcomlock);
586 return -ETIMEDOUT;
587 }
588 msleep(1);
589 }
590#endif
591
592#ifdef COM_DEBUG
593 stat = rdebi(av7110, DEBINOSWAP, MSGSTATE, 0, 2);
594 if (stat & GPMQOver) {
595 printk(KERN_ERR "%s: GPMQOver\n", __func__);
596 mutex_unlock(&av7110->dcomlock);
597 return -1;
598 }
599 else if (stat & OSDQOver) {
600 printk(KERN_ERR "%s: OSDQOver\n", __func__);
601 mutex_unlock(&av7110->dcomlock);
602 return -1;
603 }
604#endif
605
606 for (i = 0; i < reply_buf_len; i++)
607 reply_buf[i] = rdebi(av7110, DEBINOSWAP, COM_BUFF + 2 * i, 0, 2);
608
609 mutex_unlock(&av7110->dcomlock);
610 return 0;
611}
612
613static int av7110_fw_query(struct av7110 *av7110, u16 tag, u16* buf, s16 length)
614{
615 int ret;
616 ret = av7110_fw_request(av7110, &tag, 0, buf, length);
617 if (ret)
618 printk(KERN_ERR "dvb-ttpci: av7110_fw_query error %d\n", ret);
619 return ret;
620}
621
622
623/****************************************************************************
624 * Firmware commands
625 ****************************************************************************/
626
627/* get version of the firmware ROM, RTSL, video ucode and ARM application */
628int av7110_firmversion(struct av7110 *av7110)
629{
630 u16 buf[20];
631 u16 tag = ((COMTYPE_REQUEST << 8) + ReqVersion);
632
633 dprintk(4, "%p\n", av7110);
634
635 if (av7110_fw_query(av7110, tag, buf, 16)) {
636 printk("dvb-ttpci: failed to boot firmware @ card %d\n",
637 av7110->dvb_adapter.num);
638 return -EIO;
639 }
640
641 av7110->arm_fw = (buf[0] << 16) + buf[1];
642 av7110->arm_rtsl = (buf[2] << 16) + buf[3];
643 av7110->arm_vid = (buf[4] << 16) + buf[5];
644 av7110->arm_app = (buf[6] << 16) + buf[7];
645 av7110->avtype = (buf[8] << 16) + buf[9];
646
647 printk("dvb-ttpci: info @ card %d: firm %08x, rtsl %08x, vid %08x, app %08x\n",
648 av7110->dvb_adapter.num, av7110->arm_fw,
649 av7110->arm_rtsl, av7110->arm_vid, av7110->arm_app);
650
651 /* print firmware capabilities */
652 if (FW_CI_LL_SUPPORT(av7110->arm_app))
653 printk("dvb-ttpci: firmware @ card %d supports CI link layer interface\n",
654 av7110->dvb_adapter.num);
655 else
656 printk("dvb-ttpci: no firmware support for CI link layer interface @ card %d\n",
657 av7110->dvb_adapter.num);
658
659 return 0;
660}
661
662
663int av7110_diseqc_send(struct av7110 *av7110, int len, u8 *msg, unsigned long burst)
664{
665 int i, ret;
666 u16 buf[18] = { ((COMTYPE_AUDIODAC << 8) + SendDiSEqC),
667 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
668
669 dprintk(4, "%p\n", av7110);
670
671 if (len > 10)
672 len = 10;
673
674 buf[1] = len + 2;
675 buf[2] = len;
676
677 if (burst != -1)
678 buf[3] = burst ? 0x01 : 0x00;
679 else
680 buf[3] = 0xffff;
681
682 for (i = 0; i < len; i++)
683 buf[i + 4] = msg[i];
684
685 ret = av7110_send_fw_cmd(av7110, buf, 18);
686 if (ret && ret!=-ERESTARTSYS)
687 printk(KERN_ERR "dvb-ttpci: av7110_diseqc_send error %d\n", ret);
688 return ret;
689}
690
691
692#ifdef CONFIG_DVB_AV7110_OSD
693
694static inline int SetColorBlend(struct av7110 *av7110, u8 windownr)
695{
696 return av7110_fw_cmd(av7110, COMTYPE_OSD, SetCBlend, 1, windownr);
697}
698
699static inline int SetBlend_(struct av7110 *av7110, u8 windownr,
700 enum av7110_osd_palette_type colordepth, u16 index, u8 blending)
701{
702 return av7110_fw_cmd(av7110, COMTYPE_OSD, SetBlend, 4,
703 windownr, colordepth, index, blending);
704}
705
706static inline int SetColor_(struct av7110 *av7110, u8 windownr,
707 enum av7110_osd_palette_type colordepth, u16 index, u16 colorhi, u16 colorlo)
708{
709 return av7110_fw_cmd(av7110, COMTYPE_OSD, SetColor, 5,
710 windownr, colordepth, index, colorhi, colorlo);
711}
712
713static inline int SetFont(struct av7110 *av7110, u8 windownr, u8 fontsize,
714 u16 colorfg, u16 colorbg)
715{
716 return av7110_fw_cmd(av7110, COMTYPE_OSD, Set_Font, 4,
717 windownr, fontsize, colorfg, colorbg);
718}
719
720static int FlushText(struct av7110 *av7110)
721{
722 unsigned long start;
723 int err;
724
725 if (mutex_lock_interruptible(&av7110->dcomlock))
726 return -ERESTARTSYS;
727 start = jiffies;
728 while (1) {
729 err = time_after(jiffies, start + ARM_WAIT_OSD);
730 if (rdebi(av7110, DEBINOSWAP, BUFF1_BASE, 0, 2) == 0)
731 break;
732 if (err) {
733 printk(KERN_ERR "dvb-ttpci: %s(): timeout waiting for BUFF1_BASE == 0\n",
734 __func__);
735 mutex_unlock(&av7110->dcomlock);
736 return -ETIMEDOUT;
737 }
738 msleep(1);
739 }
740 mutex_unlock(&av7110->dcomlock);
741 return 0;
742}
743
744static int WriteText(struct av7110 *av7110, u8 win, u16 x, u16 y, char *buf)
745{
746 int i, ret;
747 unsigned long start;
748 int length = strlen(buf) + 1;
749 u16 cbuf[5] = { (COMTYPE_OSD << 8) + DText, 3, win, x, y };
750
751 if (mutex_lock_interruptible(&av7110->dcomlock))
752 return -ERESTARTSYS;
753
754 start = jiffies;
755 while (1) {
756 ret = time_after(jiffies, start + ARM_WAIT_OSD);
757 if (rdebi(av7110, DEBINOSWAP, BUFF1_BASE, 0, 2) == 0)
758 break;
759 if (ret) {
760 printk(KERN_ERR "dvb-ttpci: %s: timeout waiting for BUFF1_BASE == 0\n",
761 __func__);
762 mutex_unlock(&av7110->dcomlock);
763 return -ETIMEDOUT;
764 }
765 msleep(1);
766 }
767#ifndef _NOHANDSHAKE
768 start = jiffies;
769 while (1) {
770 ret = time_after(jiffies, start + ARM_WAIT_SHAKE);
771 if (rdebi(av7110, DEBINOSWAP, HANDSHAKE_REG, 0, 2) == 0)
772 break;
773 if (ret) {
774 printk(KERN_ERR "dvb-ttpci: %s: timeout waiting for HANDSHAKE_REG\n",
775 __func__);
776 mutex_unlock(&av7110->dcomlock);
777 return -ETIMEDOUT;
778 }
779 msleep(1);
780 }
781#endif
782 for (i = 0; i < length / 2; i++)
783 wdebi(av7110, DEBINOSWAP, BUFF1_BASE + i * 2,
784 swab16(*(u16 *)(buf + 2 * i)), 2);
785 if (length & 1)
786 wdebi(av7110, DEBINOSWAP, BUFF1_BASE + i * 2, 0, 2);
787 ret = __av7110_send_fw_cmd(av7110, cbuf, 5);
788 mutex_unlock(&av7110->dcomlock);
789 if (ret && ret!=-ERESTARTSYS)
790 printk(KERN_ERR "dvb-ttpci: WriteText error %d\n", ret);
791 return ret;
792}
793
794static inline int DrawLine(struct av7110 *av7110, u8 windownr,
795 u16 x, u16 y, u16 dx, u16 dy, u16 color)
796{
797 return av7110_fw_cmd(av7110, COMTYPE_OSD, DLine, 6,
798 windownr, x, y, dx, dy, color);
799}
800
801static inline int DrawBlock(struct av7110 *av7110, u8 windownr,
802 u16 x, u16 y, u16 dx, u16 dy, u16 color)
803{
804 return av7110_fw_cmd(av7110, COMTYPE_OSD, DBox, 6,
805 windownr, x, y, dx, dy, color);
806}
807
808static inline int HideWindow(struct av7110 *av7110, u8 windownr)
809{
810 return av7110_fw_cmd(av7110, COMTYPE_OSD, WHide, 1, windownr);
811}
812
813static inline int MoveWindowRel(struct av7110 *av7110, u8 windownr, u16 x, u16 y)
814{
815 return av7110_fw_cmd(av7110, COMTYPE_OSD, WMoveD, 3, windownr, x, y);
816}
817
818static inline int MoveWindowAbs(struct av7110 *av7110, u8 windownr, u16 x, u16 y)
819{
820 return av7110_fw_cmd(av7110, COMTYPE_OSD, WMoveA, 3, windownr, x, y);
821}
822
823static inline int DestroyOSDWindow(struct av7110 *av7110, u8 windownr)
824{
825 return av7110_fw_cmd(av7110, COMTYPE_OSD, WDestroy, 1, windownr);
826}
827
828static inline int CreateOSDWindow(struct av7110 *av7110, u8 windownr,
829 osd_raw_window_t disptype,
830 u16 width, u16 height)
831{
832 return av7110_fw_cmd(av7110, COMTYPE_OSD, WCreate, 4,
833 windownr, disptype, width, height);
834}
835
836
837static enum av7110_osd_palette_type bpp2pal[8] = {
838 Pal1Bit, Pal2Bit, 0, Pal4Bit, 0, 0, 0, Pal8Bit
839};
840static osd_raw_window_t bpp2bit[8] = {
841 OSD_BITMAP1, OSD_BITMAP2, 0, OSD_BITMAP4, 0, 0, 0, OSD_BITMAP8
842};
843
844static inline int WaitUntilBmpLoaded(struct av7110 *av7110)
845{
846 int ret = wait_event_timeout(av7110->bmpq,
847 av7110->bmp_state != BMP_LOADING, 10*HZ);
848 if (ret == 0) {
849 printk("dvb-ttpci: warning: timeout waiting in LoadBitmap: %d, %d\n",
850 ret, av7110->bmp_state);
851 av7110->bmp_state = BMP_NONE;
852 return -ETIMEDOUT;
853 }
854 return 0;
855}
856
857static inline int LoadBitmap(struct av7110 *av7110,
858 u16 dx, u16 dy, int inc, u8 __user * data)
859{
860 u16 format;
861 int bpp;
862 int i;
863 int d, delta;
864 u8 c;
865 int ret;
866
867 dprintk(4, "%p\n", av7110);
868
869 format = bpp2bit[av7110->osdbpp[av7110->osdwin]];
870
871 av7110->bmp_state = BMP_LOADING;
872 if (format == OSD_BITMAP8) {
873 bpp=8; delta = 1;
874 } else if (format == OSD_BITMAP4) {
875 bpp=4; delta = 2;
876 } else if (format == OSD_BITMAP2) {
877 bpp=2; delta = 4;
878 } else if (format == OSD_BITMAP1) {
879 bpp=1; delta = 8;
880 } else {
881 av7110->bmp_state = BMP_NONE;
882 return -EINVAL;
883 }
884 av7110->bmplen = ((dx * dy * bpp + 7) & ~7) / 8;
885 av7110->bmpp = 0;
886 if (av7110->bmplen > 32768) {
887 av7110->bmp_state = BMP_NONE;
888 return -EINVAL;
889 }
890 for (i = 0; i < dy; i++) {
891 if (copy_from_user(av7110->bmpbuf + 1024 + i * dx, data + i * inc, dx)) {
892 av7110->bmp_state = BMP_NONE;
893 return -EINVAL;
894 }
895 }
896 if (format != OSD_BITMAP8) {
897 for (i = 0; i < dx * dy / delta; i++) {
898 c = ((u8 *)av7110->bmpbuf)[1024 + i * delta + delta - 1];
899 for (d = delta - 2; d >= 0; d--) {
900 c |= (((u8 *)av7110->bmpbuf)[1024 + i * delta + d]
901 << ((delta - d - 1) * bpp));
902 ((u8 *)av7110->bmpbuf)[1024 + i] = c;
903 }
904 }
905 }
906 av7110->bmplen += 1024;
907 dprintk(4, "av7110_fw_cmd: LoadBmp size %d\n", av7110->bmplen);
908 ret = av7110_fw_cmd(av7110, COMTYPE_OSD, LoadBmp, 3, format, dx, dy);
909 if (!ret)
910 ret = WaitUntilBmpLoaded(av7110);
911 return ret;
912}
913
914static int BlitBitmap(struct av7110 *av7110, u16 x, u16 y)
915{
916 dprintk(4, "%p\n", av7110);
917
918 return av7110_fw_cmd(av7110, COMTYPE_OSD, BlitBmp, 4, av7110->osdwin, x, y, 0);
919}
920
921static inline int ReleaseBitmap(struct av7110 *av7110)
922{
923 dprintk(4, "%p\n", av7110);
924
925 if (av7110->bmp_state != BMP_LOADED && FW_VERSION(av7110->arm_app) < 0x261e)
926 return -1;
927 if (av7110->bmp_state == BMP_LOADING)
928 dprintk(1,"ReleaseBitmap called while BMP_LOADING\n");
929 av7110->bmp_state = BMP_NONE;
930 return av7110_fw_cmd(av7110, COMTYPE_OSD, ReleaseBmp, 0);
931}
932
933static u32 RGB2YUV(u16 R, u16 G, u16 B)
934{
935 u16 y, u, v;
936 u16 Y, Cr, Cb;
937
938 y = R * 77 + G * 150 + B * 29; /* Luma=0.299R+0.587G+0.114B 0..65535 */
939 u = 2048 + B * 8 -(y >> 5); /* Cr 0..4095 */
940 v = 2048 + R * 8 -(y >> 5); /* Cb 0..4095 */
941
942 Y = y / 256;
943 Cb = u / 16;
944 Cr = v / 16;
945
946 return Cr | (Cb << 16) | (Y << 8);
947}
948
949static int OSDSetColor(struct av7110 *av7110, u8 color, u8 r, u8 g, u8 b, u8 blend)
950{
951 int ret;
952
953 u16 ch, cl;
954 u32 yuv;
955
956 yuv = blend ? RGB2YUV(r,g,b) : 0;
957 cl = (yuv & 0xffff);
958 ch = ((yuv >> 16) & 0xffff);
959 ret = SetColor_(av7110, av7110->osdwin, bpp2pal[av7110->osdbpp[av7110->osdwin]],
960 color, ch, cl);
961 if (!ret)
962 ret = SetBlend_(av7110, av7110->osdwin, bpp2pal[av7110->osdbpp[av7110->osdwin]],
963 color, ((blend >> 4) & 0x0f));
964 return ret;
965}
966
967static int OSDSetPalette(struct av7110 *av7110, u32 __user * colors, u8 first, u8 last)
968{
969 int i;
970 int length = last - first + 1;
971
972 if (length * 4 > DATA_BUFF3_SIZE)
973 return -EINVAL;
974
975 for (i = 0; i < length; i++) {
976 u32 color, blend, yuv;
977
978 if (get_user(color, colors + i))
979 return -EFAULT;
980 blend = (color & 0xF0000000) >> 4;
981 yuv = blend ? RGB2YUV(color & 0xFF, (color >> 8) & 0xFF,
982 (color >> 16) & 0xFF) | blend : 0;
983 yuv = ((yuv & 0xFFFF0000) >> 16) | ((yuv & 0x0000FFFF) << 16);
984 wdebi(av7110, DEBINOSWAP, DATA_BUFF3_BASE + i * 4, yuv, 4);
985 }
986 return av7110_fw_cmd(av7110, COMTYPE_OSD, Set_Palette, 4,
987 av7110->osdwin,
988 bpp2pal[av7110->osdbpp[av7110->osdwin]],
989 first, last);
990}
991
992static int OSDSetBlock(struct av7110 *av7110, int x0, int y0,
993 int x1, int y1, int inc, u8 __user * data)
994{
995 uint w, h, bpp, bpl, size, lpb, bnum, brest;
996 int i;
997 int rc,release_rc;
998
999 w = x1 - x0 + 1;
1000 h = y1 - y0 + 1;
1001 if (inc <= 0)
1002 inc = w;
1003 if (w <= 0 || w > 720 || h <= 0 || h > 576)
1004 return -EINVAL;
1005 bpp = av7110->osdbpp[av7110->osdwin] + 1;
1006 bpl = ((w * bpp + 7) & ~7) / 8;
1007 size = h * bpl;
1008 lpb = (32 * 1024) / bpl;
1009 bnum = size / (lpb * bpl);
1010 brest = size - bnum * lpb * bpl;
1011
1012 if (av7110->bmp_state == BMP_LOADING) {
1013 /* possible if syscall is repeated by -ERESTARTSYS and if firmware cannot abort */
1014 BUG_ON (FW_VERSION(av7110->arm_app) >= 0x261e);
1015 rc = WaitUntilBmpLoaded(av7110);
1016 if (rc)
1017 return rc;
1018 /* just continue. This should work for all fw versions
1019 * if bnum==1 && !brest && LoadBitmap was successful
1020 */
1021 }
1022
1023 rc = 0;
1024 for (i = 0; i < bnum; i++) {
1025 rc = LoadBitmap(av7110, w, lpb, inc, data);
1026 if (rc)
1027 break;
1028 rc = BlitBitmap(av7110, x0, y0 + i * lpb);
1029 if (rc)
1030 break;
1031 data += lpb * inc;
1032 }
1033 if (!rc && brest) {
1034 rc = LoadBitmap(av7110, w, brest / bpl, inc, data);
1035 if (!rc)
1036 rc = BlitBitmap(av7110, x0, y0 + bnum * lpb);
1037 }
1038 release_rc = ReleaseBitmap(av7110);
1039 if (!rc)
1040 rc = release_rc;
1041 if (rc)
1042 dprintk(1,"returns %d\n",rc);
1043 return rc;
1044}
1045
1046int av7110_osd_cmd(struct av7110 *av7110, osd_cmd_t *dc)
1047{
1048 int ret;
1049
1050 if (mutex_lock_interruptible(&av7110->osd_mutex))
1051 return -ERESTARTSYS;
1052
1053 switch (dc->cmd) {
1054 case OSD_Close:
1055 ret = DestroyOSDWindow(av7110, av7110->osdwin);
1056 break;
1057 case OSD_Open:
1058 av7110->osdbpp[av7110->osdwin] = (dc->color - 1) & 7;
1059 ret = CreateOSDWindow(av7110, av7110->osdwin,
1060 bpp2bit[av7110->osdbpp[av7110->osdwin]],
1061 dc->x1 - dc->x0 + 1, dc->y1 - dc->y0 + 1);
1062 if (ret)
1063 break;
1064 if (!dc->data) {
1065 ret = MoveWindowAbs(av7110, av7110->osdwin, dc->x0, dc->y0);
1066 if (ret)
1067 break;
1068 ret = SetColorBlend(av7110, av7110->osdwin);
1069 }
1070 break;
1071 case OSD_Show:
1072 ret = MoveWindowRel(av7110, av7110->osdwin, 0, 0);
1073 break;
1074 case OSD_Hide:
1075 ret = HideWindow(av7110, av7110->osdwin);
1076 break;
1077 case OSD_Clear:
1078 ret = DrawBlock(av7110, av7110->osdwin, 0, 0, 720, 576, 0);
1079 break;
1080 case OSD_Fill:
1081 ret = DrawBlock(av7110, av7110->osdwin, 0, 0, 720, 576, dc->color);
1082 break;
1083 case OSD_SetColor:
1084 ret = OSDSetColor(av7110, dc->color, dc->x0, dc->y0, dc->x1, dc->y1);
1085 break;
1086 case OSD_SetPalette:
1087 if (FW_VERSION(av7110->arm_app) >= 0x2618)
1088 ret = OSDSetPalette(av7110, dc->data, dc->color, dc->x0);
1089 else {
1090 int i, len = dc->x0-dc->color+1;
1091 u8 __user *colors = (u8 __user *)dc->data;
1092 u8 r, g = 0, b = 0, blend = 0;
1093 ret = 0;
1094 for (i = 0; i<len; i++) {
1095 if (get_user(r, colors + i * 4) ||
1096 get_user(g, colors + i * 4 + 1) ||
1097 get_user(b, colors + i * 4 + 2) ||
1098 get_user(blend, colors + i * 4 + 3)) {
1099 ret = -EFAULT;
1100 break;
1101 }
1102 ret = OSDSetColor(av7110, dc->color + i, r, g, b, blend);
1103 if (ret)
1104 break;
1105 }
1106 }
1107 break;
1108 case OSD_SetPixel:
1109 ret = DrawLine(av7110, av7110->osdwin,
1110 dc->x0, dc->y0, 0, 0, dc->color);
1111 break;
1112 case OSD_SetRow:
1113 dc->y1 = dc->y0;
1114 /* fall through */
1115 case OSD_SetBlock:
1116 ret = OSDSetBlock(av7110, dc->x0, dc->y0, dc->x1, dc->y1, dc->color, dc->data);
1117 break;
1118 case OSD_FillRow:
1119 ret = DrawBlock(av7110, av7110->osdwin, dc->x0, dc->y0,
1120 dc->x1-dc->x0+1, dc->y1, dc->color);
1121 break;
1122 case OSD_FillBlock:
1123 ret = DrawBlock(av7110, av7110->osdwin, dc->x0, dc->y0,
1124 dc->x1 - dc->x0 + 1, dc->y1 - dc->y0 + 1, dc->color);
1125 break;
1126 case OSD_Line:
1127 ret = DrawLine(av7110, av7110->osdwin,
1128 dc->x0, dc->y0, dc->x1 - dc->x0, dc->y1 - dc->y0, dc->color);
1129 break;
1130 case OSD_Text:
1131 {
1132 char textbuf[240];
1133
1134 if (strncpy_from_user(textbuf, dc->data, 240) < 0) {
1135 ret = -EFAULT;
1136 break;
1137 }
1138 textbuf[239] = 0;
1139 if (dc->x1 > 3)
1140 dc->x1 = 3;
1141 ret = SetFont(av7110, av7110->osdwin, dc->x1,
1142 (u16) (dc->color & 0xffff), (u16) (dc->color >> 16));
1143 if (!ret)
1144 ret = FlushText(av7110);
1145 if (!ret)
1146 ret = WriteText(av7110, av7110->osdwin, dc->x0, dc->y0, textbuf);
1147 break;
1148 }
1149 case OSD_SetWindow:
1150 if (dc->x0 < 1 || dc->x0 > 7)
1151 ret = -EINVAL;
1152 else {
1153 av7110->osdwin = dc->x0;
1154 ret = 0;
1155 }
1156 break;
1157 case OSD_MoveWindow:
1158 ret = MoveWindowAbs(av7110, av7110->osdwin, dc->x0, dc->y0);
1159 if (!ret)
1160 ret = SetColorBlend(av7110, av7110->osdwin);
1161 break;
1162 case OSD_OpenRaw:
1163 if (dc->color < OSD_BITMAP1 || dc->color > OSD_CURSOR) {
1164 ret = -EINVAL;
1165 break;
1166 }
1167 if (dc->color >= OSD_BITMAP1 && dc->color <= OSD_BITMAP8HR)
1168 av7110->osdbpp[av7110->osdwin] = (1 << (dc->color & 3)) - 1;
1169 else
1170 av7110->osdbpp[av7110->osdwin] = 0;
1171 ret = CreateOSDWindow(av7110, av7110->osdwin, (osd_raw_window_t)dc->color,
1172 dc->x1 - dc->x0 + 1, dc->y1 - dc->y0 + 1);
1173 if (ret)
1174 break;
1175 if (!dc->data) {
1176 ret = MoveWindowAbs(av7110, av7110->osdwin, dc->x0, dc->y0);
1177 if (!ret)
1178 ret = SetColorBlend(av7110, av7110->osdwin);
1179 }
1180 break;
1181 default:
1182 ret = -EINVAL;
1183 break;
1184 }
1185
1186 mutex_unlock(&av7110->osd_mutex);
1187 if (ret==-ERESTARTSYS)
1188 dprintk(1, "av7110_osd_cmd(%d) returns with -ERESTARTSYS\n",dc->cmd);
1189 else if (ret)
1190 dprintk(1, "av7110_osd_cmd(%d) returns with %d\n",dc->cmd,ret);
1191
1192 return ret;
1193}
1194
1195int av7110_osd_capability(struct av7110 *av7110, osd_cap_t *cap)
1196{
1197 switch (cap->cmd) {
1198 case OSD_CAP_MEMSIZE:
1199 if (FW_4M_SDRAM(av7110->arm_app))
1200 cap->val = 1000000;
1201 else
1202 cap->val = 92000;
1203 return 0;
1204 default:
1205 return -EINVAL;
1206 }
1207}
1208#endif /* CONFIG_DVB_AV7110_OSD */
diff --git a/drivers/media/dvb/ttpci/av7110_hw.h b/drivers/media/dvb/ttpci/av7110_hw.h
new file mode 100644
index 00000000000..1634aba5cb8
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_hw.h
@@ -0,0 +1,495 @@
1#ifndef _AV7110_HW_H_
2#define _AV7110_HW_H_
3
4#include "av7110.h"
5
6/* DEBI transfer mode defs */
7
8#define DEBINOSWAP 0x000e0000
9#define DEBISWAB 0x001e0000
10#define DEBISWAP 0x002e0000
11
12#define ARM_WAIT_FREE (HZ)
13#define ARM_WAIT_SHAKE (HZ/5)
14#define ARM_WAIT_OSD (HZ)
15
16
17enum av7110_bootstate
18{
19 BOOTSTATE_BUFFER_EMPTY = 0,
20 BOOTSTATE_BUFFER_FULL = 1,
21 BOOTSTATE_AV7110_BOOT_COMPLETE = 2
22};
23
24enum av7110_type_rec_play_format
25{ RP_None,
26 AudioPES,
27 AudioMp2,
28 AudioPCM,
29 VideoPES,
30 AV_PES
31};
32
33enum av7110_osd_palette_type
34{
35 NoPalet = 0, /* No palette */
36 Pal1Bit = 2, /* 2 colors for 1 Bit Palette */
37 Pal2Bit = 4, /* 4 colors for 2 bit palette */
38 Pal4Bit = 16, /* 16 colors for 4 bit palette */
39 Pal8Bit = 256 /* 256 colors for 16 bit palette */
40};
41
42/* switch defines */
43#define SB_GPIO 3
44#define SB_OFF SAA7146_GPIO_OUTLO /* SlowBlank off (TV-Mode) */
45#define SB_ON SAA7146_GPIO_INPUT /* SlowBlank on (AV-Mode) */
46#define SB_WIDE SAA7146_GPIO_OUTHI /* SlowBlank 6V (16/9-Mode) (not implemented) */
47
48#define FB_GPIO 1
49#define FB_OFF SAA7146_GPIO_LO /* FastBlank off (CVBS-Mode) */
50#define FB_ON SAA7146_GPIO_OUTHI /* FastBlank on (RGB-Mode) */
51#define FB_LOOP SAA7146_GPIO_INPUT /* FastBlank loop-through (PC graphics ???) */
52
53enum av7110_video_output_mode
54{
55 NO_OUT = 0, /* disable analog output */
56 CVBS_RGB_OUT = 1,
57 CVBS_YC_OUT = 2,
58 YC_OUT = 3
59};
60
61/* firmware internal msg q status: */
62#define GPMQFull 0x0001 /* Main Message Queue Full */
63#define GPMQOver 0x0002 /* Main Message Queue Overflow */
64#define HPQFull 0x0004 /* High Priority Msg Queue Full */
65#define HPQOver 0x0008
66#define OSDQFull 0x0010 /* OSD Queue Full */
67#define OSDQOver 0x0020
68#define GPMQBusy 0x0040 /* Queue not empty, FW >= 261d */
69#define HPQBusy 0x0080
70#define OSDQBusy 0x0100
71
72/* hw section filter flags */
73#define SECTION_EIT 0x01
74#define SECTION_SINGLE 0x00
75#define SECTION_CYCLE 0x02
76#define SECTION_CONTINUOS 0x04
77#define SECTION_MODE 0x06
78#define SECTION_IPMPE 0x0C /* size up to 4k */
79#define SECTION_HIGH_SPEED 0x1C /* larger buffer */
80#define DATA_PIPING_FLAG 0x20 /* for Data Piping Filter */
81
82#define PBUFSIZE_NONE 0x0000
83#define PBUFSIZE_1P 0x0100
84#define PBUFSIZE_2P 0x0200
85#define PBUFSIZE_1K 0x0300
86#define PBUFSIZE_2K 0x0400
87#define PBUFSIZE_4K 0x0500
88#define PBUFSIZE_8K 0x0600
89#define PBUFSIZE_16K 0x0700
90#define PBUFSIZE_32K 0x0800
91
92
93/* firmware command codes */
94enum av7110_osd_command {
95 WCreate,
96 WDestroy,
97 WMoveD,
98 WMoveA,
99 WHide,
100 WTop,
101 DBox,
102 DLine,
103 DText,
104 Set_Font,
105 SetColor,
106 SetBlend,
107 SetWBlend,
108 SetCBlend,
109 SetNonBlend,
110 LoadBmp,
111 BlitBmp,
112 ReleaseBmp,
113 SetWTrans,
114 SetWNoTrans,
115 Set_Palette
116};
117
118enum av7110_pid_command {
119 MultiPID,
120 VideoPID,
121 AudioPID,
122 InitFilt,
123 FiltError,
124 NewVersion,
125 CacheError,
126 AddPIDFilter,
127 DelPIDFilter,
128 Scan,
129 SetDescr,
130 SetIR,
131 FlushTSQueue
132};
133
134enum av7110_mpeg_command {
135 SelAudChannels
136};
137
138enum av7110_audio_command {
139 AudioDAC,
140 CabADAC,
141 ON22K,
142 OFF22K,
143 MainSwitch,
144 ADSwitch,
145 SendDiSEqC,
146 SetRegister,
147 SpdifSwitch
148};
149
150enum av7110_request_command {
151 AudioState,
152 AudioBuffState,
153 VideoState1,
154 VideoState2,
155 VideoState3,
156 CrashCounter,
157 ReqVersion,
158 ReqVCXO,
159 ReqRegister,
160 ReqSecFilterError,
161 ReqSTC
162};
163
164enum av7110_encoder_command {
165 SetVidMode,
166 SetTestMode,
167 LoadVidCode,
168 SetMonitorType,
169 SetPanScanType,
170 SetFreezeMode,
171 SetWSSConfig
172};
173
174enum av7110_rec_play_state {
175 __Record,
176 __Stop,
177 __Play,
178 __Pause,
179 __Slow,
180 __FF_IP,
181 __Scan_I,
182 __Continue
183};
184
185enum av7110_fw_cmd_misc {
186 AV7110_FW_VIDEO_ZOOM = 1,
187 AV7110_FW_VIDEO_COMMAND,
188 AV7110_FW_AUDIO_COMMAND
189};
190
191enum av7110_command_type {
192 COMTYPE_NOCOM,
193 COMTYPE_PIDFILTER,
194 COMTYPE_MPEGDECODER,
195 COMTYPE_OSD,
196 COMTYPE_BMP,
197 COMTYPE_ENCODER,
198 COMTYPE_AUDIODAC,
199 COMTYPE_REQUEST,
200 COMTYPE_SYSTEM,
201 COMTYPE_REC_PLAY,
202 COMTYPE_COMMON_IF,
203 COMTYPE_PID_FILTER,
204 COMTYPE_PES,
205 COMTYPE_TS,
206 COMTYPE_VIDEO,
207 COMTYPE_AUDIO,
208 COMTYPE_CI_LL,
209 COMTYPE_MISC = 0x80
210};
211
212#define VID_NONE_PREF 0x00 /* No aspect ration processing preferred */
213#define VID_PAN_SCAN_PREF 0x01 /* Pan and Scan Display preferred */
214#define VID_VERT_COMP_PREF 0x02 /* Vertical compression display preferred */
215#define VID_VC_AND_PS_PREF 0x03 /* PanScan and vertical Compression if allowed */
216#define VID_CENTRE_CUT_PREF 0x05 /* PanScan with zero vector */
217
218/* MPEG video decoder commands */
219#define AV_VIDEO_CMD_STOP 0x000e
220#define AV_VIDEO_CMD_PLAY 0x000d
221#define AV_VIDEO_CMD_FREEZE 0x0102
222#define AV_VIDEO_CMD_FFWD 0x0016
223#define AV_VIDEO_CMD_SLOW 0x0022
224
225/* MPEG audio decoder commands */
226#define AUDIO_CMD_MUTE 0x0001
227#define AUDIO_CMD_UNMUTE 0x0002
228#define AUDIO_CMD_PCM16 0x0010
229#define AUDIO_CMD_STEREO 0x0080
230#define AUDIO_CMD_MONO_L 0x0100
231#define AUDIO_CMD_MONO_R 0x0200
232#define AUDIO_CMD_SYNC_OFF 0x000e
233#define AUDIO_CMD_SYNC_ON 0x000f
234
235/* firmware data interface codes */
236#define DATA_NONE 0x00
237#define DATA_FSECTION 0x01
238#define DATA_IPMPE 0x02
239#define DATA_MPEG_RECORD 0x03
240#define DATA_DEBUG_MESSAGE 0x04
241#define DATA_COMMON_INTERFACE 0x05
242#define DATA_MPEG_PLAY 0x06
243#define DATA_BMP_LOAD 0x07
244#define DATA_IRCOMMAND 0x08
245#define DATA_PIPING 0x09
246#define DATA_STREAMING 0x0a
247#define DATA_CI_GET 0x0b
248#define DATA_CI_PUT 0x0c
249#define DATA_MPEG_VIDEO_EVENT 0x0d
250
251#define DATA_PES_RECORD 0x10
252#define DATA_PES_PLAY 0x11
253#define DATA_TS_RECORD 0x12
254#define DATA_TS_PLAY 0x13
255
256/* ancient CI command codes, only two are actually still used
257 * by the link level CI firmware */
258#define CI_CMD_ERROR 0x00
259#define CI_CMD_ACK 0x01
260#define CI_CMD_SYSTEM_READY 0x02
261#define CI_CMD_KEYPRESS 0x03
262#define CI_CMD_ON_TUNED 0x04
263#define CI_CMD_ON_SWITCH_PROGRAM 0x05
264#define CI_CMD_SECTION_ARRIVED 0x06
265#define CI_CMD_SECTION_TIMEOUT 0x07
266#define CI_CMD_TIME 0x08
267#define CI_CMD_ENTER_MENU 0x09
268#define CI_CMD_FAST_PSI 0x0a
269#define CI_CMD_GET_SLOT_INFO 0x0b
270
271#define CI_MSG_NONE 0x00
272#define CI_MSG_CI_INFO 0x01
273#define CI_MSG_MENU 0x02
274#define CI_MSG_LIST 0x03
275#define CI_MSG_TEXT 0x04
276#define CI_MSG_REQUEST_INPUT 0x05
277#define CI_MSG_INPUT_COMPLETE 0x06
278#define CI_MSG_LIST_MORE 0x07
279#define CI_MSG_MENU_MORE 0x08
280#define CI_MSG_CLOSE_MMI_IMM 0x09
281#define CI_MSG_SECTION_REQUEST 0x0a
282#define CI_MSG_CLOSE_FILTER 0x0b
283#define CI_PSI_COMPLETE 0x0c
284#define CI_MODULE_READY 0x0d
285#define CI_SWITCH_PRG_REPLY 0x0e
286#define CI_MSG_TEXT_MORE 0x0f
287
288#define CI_MSG_CA_PMT 0xe0
289#define CI_MSG_ERROR 0xf0
290
291
292/* base address of the dual ported RAM which serves as communication
293 * area between PCI bus and av7110,
294 * as seen by the DEBI bus of the saa7146 */
295#define DPRAM_BASE 0x4000
296
297/* boot protocol area */
298#define AV7110_BOOT_STATE (DPRAM_BASE + 0x3F8)
299#define AV7110_BOOT_SIZE (DPRAM_BASE + 0x3FA)
300#define AV7110_BOOT_BASE (DPRAM_BASE + 0x3FC)
301#define AV7110_BOOT_BLOCK (DPRAM_BASE + 0x400)
302#define AV7110_BOOT_MAX_SIZE 0xc00
303
304/* firmware command protocol area */
305#define IRQ_STATE (DPRAM_BASE + 0x0F4)
306#define IRQ_STATE_EXT (DPRAM_BASE + 0x0F6)
307#define MSGSTATE (DPRAM_BASE + 0x0F8)
308#define COMMAND (DPRAM_BASE + 0x0FC)
309#define COM_BUFF (DPRAM_BASE + 0x100)
310#define COM_BUFF_SIZE 0x20
311
312/* various data buffers */
313#define BUFF1_BASE (DPRAM_BASE + 0x120)
314#define BUFF1_SIZE 0xE0
315
316#define DATA_BUFF0_BASE (DPRAM_BASE + 0x200)
317#define DATA_BUFF0_SIZE 0x0800
318
319#define DATA_BUFF1_BASE (DATA_BUFF0_BASE+DATA_BUFF0_SIZE)
320#define DATA_BUFF1_SIZE 0x0800
321
322#define DATA_BUFF2_BASE (DATA_BUFF1_BASE+DATA_BUFF1_SIZE)
323#define DATA_BUFF2_SIZE 0x0800
324
325#define DATA_BUFF3_BASE (DATA_BUFF2_BASE+DATA_BUFF2_SIZE)
326#define DATA_BUFF3_SIZE 0x0400
327
328#define Reserved (DPRAM_BASE + 0x1E00)
329#define Reserved_SIZE 0x1C0
330
331
332/* firmware status area */
333#define STATUS_BASE (DPRAM_BASE + 0x1FC0)
334#define STATUS_LOOPS (STATUS_BASE + 0x08)
335
336#define STATUS_MPEG_WIDTH (STATUS_BASE + 0x0C)
337/* ((aspect_ratio & 0xf) << 12) | (height & 0xfff) */
338#define STATUS_MPEG_HEIGHT_AR (STATUS_BASE + 0x0E)
339
340/* firmware data protocol area */
341#define RX_TYPE (DPRAM_BASE + 0x1FE8)
342#define RX_LEN (DPRAM_BASE + 0x1FEA)
343#define TX_TYPE (DPRAM_BASE + 0x1FEC)
344#define TX_LEN (DPRAM_BASE + 0x1FEE)
345
346#define RX_BUFF (DPRAM_BASE + 0x1FF4)
347#define TX_BUFF (DPRAM_BASE + 0x1FF6)
348
349#define HANDSHAKE_REG (DPRAM_BASE + 0x1FF8)
350#define COM_IF_LOCK (DPRAM_BASE + 0x1FFA)
351
352#define IRQ_RX (DPRAM_BASE + 0x1FFC)
353#define IRQ_TX (DPRAM_BASE + 0x1FFE)
354
355/* used by boot protocol to load firmware into av7110 DRAM */
356#define DRAM_START_CODE 0x2e000404
357#define DRAM_MAX_CODE_SIZE 0x00100000
358
359/* saa7146 gpio lines */
360#define RESET_LINE 2
361#define DEBI_DONE_LINE 1
362#define ARM_IRQ_LINE 0
363
364
365
366extern int av7110_bootarm(struct av7110 *av7110);
367extern int av7110_firmversion(struct av7110 *av7110);
368#define FW_CI_LL_SUPPORT(arm_app) ((arm_app) & 0x80000000)
369#define FW_4M_SDRAM(arm_app) ((arm_app) & 0x40000000)
370#define FW_VERSION(arm_app) ((arm_app) & 0x0000FFFF)
371
372extern int av7110_wait_msgstate(struct av7110 *av7110, u16 flags);
373extern int av7110_fw_cmd(struct av7110 *av7110, int type, int com, int num, ...);
374extern int av7110_fw_request(struct av7110 *av7110, u16 *request_buf,
375 int request_buf_len, u16 *reply_buf, int reply_buf_len);
376
377
378/* DEBI (saa7146 data extension bus interface) access */
379extern int av7110_debiwrite(struct av7110 *av7110, u32 config,
380 int addr, u32 val, int count);
381extern u32 av7110_debiread(struct av7110 *av7110, u32 config,
382 int addr, int count);
383
384
385/* DEBI during interrupt */
386/* single word writes */
387static inline void iwdebi(struct av7110 *av7110, u32 config, int addr, u32 val, int count)
388{
389 av7110_debiwrite(av7110, config, addr, val, count);
390}
391
392/* buffer writes */
393static inline void mwdebi(struct av7110 *av7110, u32 config, int addr,
394 const u8 *val, int count)
395{
396 memcpy(av7110->debi_virt, val, count);
397 av7110_debiwrite(av7110, config, addr, 0, count);
398}
399
400static inline u32 irdebi(struct av7110 *av7110, u32 config, int addr, u32 val, int count)
401{
402 u32 res;
403
404 res=av7110_debiread(av7110, config, addr, count);
405 if (count<=4)
406 memcpy(av7110->debi_virt, (char *) &res, count);
407 return res;
408}
409
410/* DEBI outside interrupts, only for count <= 4! */
411static inline void wdebi(struct av7110 *av7110, u32 config, int addr, u32 val, int count)
412{
413 unsigned long flags;
414
415 spin_lock_irqsave(&av7110->debilock, flags);
416 av7110_debiwrite(av7110, config, addr, val, count);
417 spin_unlock_irqrestore(&av7110->debilock, flags);
418}
419
420static inline u32 rdebi(struct av7110 *av7110, u32 config, int addr, u32 val, int count)
421{
422 unsigned long flags;
423 u32 res;
424
425 spin_lock_irqsave(&av7110->debilock, flags);
426 res=av7110_debiread(av7110, config, addr, count);
427 spin_unlock_irqrestore(&av7110->debilock, flags);
428 return res;
429}
430
431/* handle mailbox registers of the dual ported RAM */
432static inline void ARM_ResetMailBox(struct av7110 *av7110)
433{
434 unsigned long flags;
435
436 spin_lock_irqsave(&av7110->debilock, flags);
437 av7110_debiread(av7110, DEBINOSWAP, IRQ_RX, 2);
438 av7110_debiwrite(av7110, DEBINOSWAP, IRQ_RX, 0, 2);
439 spin_unlock_irqrestore(&av7110->debilock, flags);
440}
441
442static inline void ARM_ClearMailBox(struct av7110 *av7110)
443{
444 iwdebi(av7110, DEBINOSWAP, IRQ_RX, 0, 2);
445}
446
447static inline void ARM_ClearIrq(struct av7110 *av7110)
448{
449 irdebi(av7110, DEBINOSWAP, IRQ_RX, 0, 2);
450}
451
452/****************************************************************************
453 * Firmware commands
454 ****************************************************************************/
455
456static inline int SendDAC(struct av7110 *av7110, u8 addr, u8 data)
457{
458 return av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, AudioDAC, 2, addr, data);
459}
460
461static inline int av7710_set_video_mode(struct av7110 *av7110, int mode)
462{
463 return av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetVidMode, 1, mode);
464}
465
466static inline int vidcom(struct av7110 *av7110, u32 com, u32 arg)
467{
468 return av7110_fw_cmd(av7110, COMTYPE_MISC, AV7110_FW_VIDEO_COMMAND, 4,
469 (com>>16), (com&0xffff),
470 (arg>>16), (arg&0xffff));
471}
472
473static inline int audcom(struct av7110 *av7110, u32 com)
474{
475 return av7110_fw_cmd(av7110, COMTYPE_MISC, AV7110_FW_AUDIO_COMMAND, 2,
476 (com>>16), (com&0xffff));
477}
478
479static inline int Set22K(struct av7110 *av7110, int state)
480{
481 return av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, (state ? ON22K : OFF22K), 0);
482}
483
484
485extern int av7110_diseqc_send(struct av7110 *av7110, int len, u8 *msg, unsigned long burst);
486
487
488#ifdef CONFIG_DVB_AV7110_OSD
489extern int av7110_osd_cmd(struct av7110 *av7110, osd_cmd_t *dc);
490extern int av7110_osd_capability(struct av7110 *av7110, osd_cap_t *cap);
491#endif /* CONFIG_DVB_AV7110_OSD */
492
493
494
495#endif /* _AV7110_HW_H_ */
diff --git a/drivers/media/dvb/ttpci/av7110_ipack.c b/drivers/media/dvb/ttpci/av7110_ipack.c
new file mode 100644
index 00000000000..699ef8b5b99
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_ipack.c
@@ -0,0 +1,403 @@
1#include "dvb_filter.h"
2#include "av7110_ipack.h"
3#include <linux/string.h> /* for memcpy() */
4#include <linux/vmalloc.h>
5
6
7void av7110_ipack_reset(struct ipack *p)
8{
9 p->found = 0;
10 p->cid = 0;
11 p->plength = 0;
12 p->flag1 = 0;
13 p->flag2 = 0;
14 p->hlength = 0;
15 p->mpeg = 0;
16 p->check = 0;
17 p->which = 0;
18 p->done = 0;
19 p->count = 0;
20}
21
22
23int av7110_ipack_init(struct ipack *p, int size,
24 void (*func)(u8 *buf, int size, void *priv))
25{
26 if (!(p->buf = vmalloc(size*sizeof(u8)))) {
27 printk(KERN_WARNING "Couldn't allocate memory for ipack\n");
28 return -ENOMEM;
29 }
30 p->size = size;
31 p->func = func;
32 p->repack_subids = 0;
33 av7110_ipack_reset(p);
34 return 0;
35}
36
37
38void av7110_ipack_free(struct ipack *p)
39{
40 vfree(p->buf);
41}
42
43
44static void send_ipack(struct ipack *p)
45{
46 int off;
47 struct dvb_audio_info ai;
48 int ac3_off = 0;
49 int streamid = 0;
50 int nframes = 0;
51 int f = 0;
52
53 switch (p->mpeg) {
54 case 2:
55 if (p->count < 10)
56 return;
57 p->buf[3] = p->cid;
58 p->buf[4] = (u8)(((p->count - 6) & 0xff00) >> 8);
59 p->buf[5] = (u8)((p->count - 6) & 0x00ff);
60 if (p->repack_subids && p->cid == PRIVATE_STREAM1) {
61 off = 9 + p->buf[8];
62 streamid = p->buf[off];
63 if ((streamid & 0xf8) == 0x80) {
64 ai.off = 0;
65 ac3_off = ((p->buf[off + 2] << 8)|
66 p->buf[off + 3]);
67 if (ac3_off < p->count)
68 f = dvb_filter_get_ac3info(p->buf + off + 3 + ac3_off,
69 p->count - ac3_off, &ai, 0);
70 if (!f) {
71 nframes = (p->count - off - 3 - ac3_off) /
72 ai.framesize + 1;
73 p->buf[off + 2] = (ac3_off >> 8) & 0xff;
74 p->buf[off + 3] = (ac3_off) & 0xff;
75 p->buf[off + 1] = nframes;
76 ac3_off += nframes * ai.framesize - p->count;
77 }
78 }
79 }
80 p->func(p->buf, p->count, p->data);
81
82 p->buf[6] = 0x80;
83 p->buf[7] = 0x00;
84 p->buf[8] = 0x00;
85 p->count = 9;
86 if (p->repack_subids && p->cid == PRIVATE_STREAM1
87 && (streamid & 0xf8) == 0x80) {
88 p->count += 4;
89 p->buf[9] = streamid;
90 p->buf[10] = (ac3_off >> 8) & 0xff;
91 p->buf[11] = (ac3_off) & 0xff;
92 p->buf[12] = 0;
93 }
94 break;
95
96 case 1:
97 if (p->count < 8)
98 return;
99 p->buf[3] = p->cid;
100 p->buf[4] = (u8)(((p->count - 6) & 0xff00) >> 8);
101 p->buf[5] = (u8)((p->count - 6) & 0x00ff);
102 p->func(p->buf, p->count, p->data);
103
104 p->buf[6] = 0x0f;
105 p->count = 7;
106 break;
107 }
108}
109
110
111void av7110_ipack_flush(struct ipack *p)
112{
113 if (p->plength != MMAX_PLENGTH - 6 || p->found <= 6)
114 return;
115 p->plength = p->found - 6;
116 p->found = 0;
117 send_ipack(p);
118 av7110_ipack_reset(p);
119}
120
121
122static void write_ipack(struct ipack *p, const u8 *data, int count)
123{
124 u8 headr[3] = { 0x00, 0x00, 0x01 };
125
126 if (p->count < 6) {
127 memcpy(p->buf, headr, 3);
128 p->count = 6;
129 }
130
131 if (p->count + count < p->size){
132 memcpy(p->buf+p->count, data, count);
133 p->count += count;
134 } else {
135 int rest = p->size - p->count;
136 memcpy(p->buf+p->count, data, rest);
137 p->count += rest;
138 send_ipack(p);
139 if (count - rest > 0)
140 write_ipack(p, data + rest, count - rest);
141 }
142}
143
144
145int av7110_ipack_instant_repack (const u8 *buf, int count, struct ipack *p)
146{
147 int l;
148 int c = 0;
149
150 while (c < count && (p->mpeg == 0 ||
151 (p->mpeg == 1 && p->found < 7) ||
152 (p->mpeg == 2 && p->found < 9))
153 && (p->found < 5 || !p->done)) {
154 switch (p->found) {
155 case 0:
156 case 1:
157 if (buf[c] == 0x00)
158 p->found++;
159 else
160 p->found = 0;
161 c++;
162 break;
163 case 2:
164 if (buf[c] == 0x01)
165 p->found++;
166 else if (buf[c] == 0)
167 p->found = 2;
168 else
169 p->found = 0;
170 c++;
171 break;
172 case 3:
173 p->cid = 0;
174 switch (buf[c]) {
175 case PROG_STREAM_MAP:
176 case PRIVATE_STREAM2:
177 case PROG_STREAM_DIR:
178 case ECM_STREAM :
179 case EMM_STREAM :
180 case PADDING_STREAM :
181 case DSM_CC_STREAM :
182 case ISO13522_STREAM:
183 p->done = 1;
184 /* fall through */
185 case PRIVATE_STREAM1:
186 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
187 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
188 p->found++;
189 p->cid = buf[c];
190 c++;
191 break;
192 default:
193 p->found = 0;
194 break;
195 }
196 break;
197
198 case 4:
199 if (count-c > 1) {
200 p->plen[0] = buf[c];
201 c++;
202 p->plen[1] = buf[c];
203 c++;
204 p->found += 2;
205 p->plength = (p->plen[0] << 8) | p->plen[1];
206 } else {
207 p->plen[0] = buf[c];
208 p->found++;
209 return count;
210 }
211 break;
212 case 5:
213 p->plen[1] = buf[c];
214 c++;
215 p->found++;
216 p->plength = (p->plen[0] << 8) | p->plen[1];
217 break;
218 case 6:
219 if (!p->done) {
220 p->flag1 = buf[c];
221 c++;
222 p->found++;
223 if ((p->flag1 & 0xc0) == 0x80)
224 p->mpeg = 2;
225 else {
226 p->hlength = 0;
227 p->which = 0;
228 p->mpeg = 1;
229 p->flag2 = 0;
230 }
231 }
232 break;
233
234 case 7:
235 if (!p->done && p->mpeg == 2) {
236 p->flag2 = buf[c];
237 c++;
238 p->found++;
239 }
240 break;
241
242 case 8:
243 if (!p->done && p->mpeg == 2) {
244 p->hlength = buf[c];
245 c++;
246 p->found++;
247 }
248 break;
249 }
250 }
251
252 if (c == count)
253 return count;
254
255 if (!p->plength)
256 p->plength = MMAX_PLENGTH - 6;
257
258 if (p->done || ((p->mpeg == 2 && p->found >= 9) ||
259 (p->mpeg == 1 && p->found >= 7))) {
260 switch (p->cid) {
261 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
262 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
263 case PRIVATE_STREAM1:
264 if (p->mpeg == 2 && p->found == 9) {
265 write_ipack(p, &p->flag1, 1);
266 write_ipack(p, &p->flag2, 1);
267 write_ipack(p, &p->hlength, 1);
268 }
269
270 if (p->mpeg == 1 && p->found == 7)
271 write_ipack(p, &p->flag1, 1);
272
273 if (p->mpeg == 2 && (p->flag2 & PTS_ONLY) &&
274 p->found < 14) {
275 while (c < count && p->found < 14) {
276 p->pts[p->found - 9] = buf[c];
277 write_ipack(p, buf + c, 1);
278 c++;
279 p->found++;
280 }
281 if (c == count)
282 return count;
283 }
284
285 if (p->mpeg == 1 && p->which < 2000) {
286
287 if (p->found == 7) {
288 p->check = p->flag1;
289 p->hlength = 1;
290 }
291
292 while (!p->which && c < count &&
293 p->check == 0xff){
294 p->check = buf[c];
295 write_ipack(p, buf + c, 1);
296 c++;
297 p->found++;
298 p->hlength++;
299 }
300
301 if (c == count)
302 return count;
303
304 if ((p->check & 0xc0) == 0x40 && !p->which) {
305 p->check = buf[c];
306 write_ipack(p, buf + c, 1);
307 c++;
308 p->found++;
309 p->hlength++;
310
311 p->which = 1;
312 if (c == count)
313 return count;
314 p->check = buf[c];
315 write_ipack(p, buf + c, 1);
316 c++;
317 p->found++;
318 p->hlength++;
319 p->which = 2;
320 if (c == count)
321 return count;
322 }
323
324 if (p->which == 1) {
325 p->check = buf[c];
326 write_ipack(p, buf + c, 1);
327 c++;
328 p->found++;
329 p->hlength++;
330 p->which = 2;
331 if (c == count)
332 return count;
333 }
334
335 if ((p->check & 0x30) && p->check != 0xff) {
336 p->flag2 = (p->check & 0xf0) << 2;
337 p->pts[0] = p->check;
338 p->which = 3;
339 }
340
341 if (c == count)
342 return count;
343 if (p->which > 2){
344 if ((p->flag2 & PTS_DTS_FLAGS) == PTS_ONLY) {
345 while (c < count && p->which < 7) {
346 p->pts[p->which - 2] = buf[c];
347 write_ipack(p, buf + c, 1);
348 c++;
349 p->found++;
350 p->which++;
351 p->hlength++;
352 }
353 if (c == count)
354 return count;
355 } else if ((p->flag2 & PTS_DTS_FLAGS) == PTS_DTS) {
356 while (c < count && p->which < 12) {
357 if (p->which < 7)
358 p->pts[p->which - 2] = buf[c];
359 write_ipack(p, buf + c, 1);
360 c++;
361 p->found++;
362 p->which++;
363 p->hlength++;
364 }
365 if (c == count)
366 return count;
367 }
368 p->which = 2000;
369 }
370
371 }
372
373 while (c < count && p->found < p->plength + 6) {
374 l = count - c;
375 if (l + p->found > p->plength + 6)
376 l = p->plength + 6 - p->found;
377 write_ipack(p, buf + c, l);
378 p->found += l;
379 c += l;
380 }
381 break;
382 }
383
384
385 if (p->done) {
386 if (p->found + count - c < p->plength + 6) {
387 p->found += count - c;
388 c = count;
389 } else {
390 c += p->plength + 6 - p->found;
391 p->found = p->plength + 6;
392 }
393 }
394
395 if (p->plength && p->found == p->plength + 6) {
396 send_ipack(p);
397 av7110_ipack_reset(p);
398 if (c < count)
399 av7110_ipack_instant_repack(buf + c, count - c, p);
400 }
401 }
402 return count;
403}
diff --git a/drivers/media/dvb/ttpci/av7110_ipack.h b/drivers/media/dvb/ttpci/av7110_ipack.h
new file mode 100644
index 00000000000..becf94d3fdf
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_ipack.h
@@ -0,0 +1,12 @@
1#ifndef _AV7110_IPACK_H_
2#define _AV7110_IPACK_H_
3
4extern int av7110_ipack_init(struct ipack *p, int size,
5 void (*func)(u8 *buf, int size, void *priv));
6
7extern void av7110_ipack_reset(struct ipack *p);
8extern int av7110_ipack_instant_repack(const u8 *buf, int count, struct ipack *p);
9extern void av7110_ipack_free(struct ipack * p);
10extern void av7110_ipack_flush(struct ipack *p);
11
12#endif
diff --git a/drivers/media/dvb/ttpci/av7110_ir.c b/drivers/media/dvb/ttpci/av7110_ir.c
new file mode 100644
index 00000000000..908f272fe26
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_ir.c
@@ -0,0 +1,415 @@
1/*
2 * Driver for the remote control of SAA7146 based AV7110 cards
3 *
4 * Copyright (C) 1999-2003 Holger Waechtler <holger@convergence.de>
5 * Copyright (C) 2003-2007 Oliver Endriss <o.endriss@gmx.de>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
21 *
22 */
23
24
25#include <linux/types.h>
26#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/proc_fs.h>
29#include <linux/kernel.h>
30#include <linux/bitops.h>
31
32#include "av7110.h"
33#include "av7110_hw.h"
34
35
36#define AV_CNT 4
37
38#define IR_RC5 0
39#define IR_RCMM 1
40#define IR_RC5_EXT 2 /* internal only */
41
42#define IR_ALL 0xffffffff
43
44#define UP_TIMEOUT (HZ*7/25)
45
46
47/* Note: enable ir debugging by or'ing debug with 16 */
48
49static int ir_protocol[AV_CNT] = { IR_RCMM, IR_RCMM, IR_RCMM, IR_RCMM};
50module_param_array(ir_protocol, int, NULL, 0644);
51MODULE_PARM_DESC(ir_protocol, "Infrared protocol: 0 RC5, 1 RCMM (default)");
52
53static int ir_inversion[AV_CNT];
54module_param_array(ir_inversion, int, NULL, 0644);
55MODULE_PARM_DESC(ir_inversion, "Inversion of infrared signal: 0 not inverted (default), 1 inverted");
56
57static uint ir_device_mask[AV_CNT] = { IR_ALL, IR_ALL, IR_ALL, IR_ALL };
58module_param_array(ir_device_mask, uint, NULL, 0644);
59MODULE_PARM_DESC(ir_device_mask, "Bitmask of infrared devices: bit 0..31 = device 0..31 (default: all)");
60
61
62static int av_cnt;
63static struct av7110 *av_list[AV_CNT];
64
65static u16 default_key_map [256] = {
66 KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7,
67 KEY_8, KEY_9, KEY_BACK, 0, KEY_POWER, KEY_MUTE, 0, KEY_INFO,
68 KEY_VOLUMEUP, KEY_VOLUMEDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 KEY_CHANNELUP, KEY_CHANNELDOWN, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, KEY_TEXT, 0, 0, KEY_TV, 0, 0, 0, 0, 0, KEY_SETUP, 0, 0,
72 0, 0, 0, KEY_SUBTITLE, 0, 0, KEY_LANGUAGE, 0,
73 KEY_RADIO, 0, 0, 0, 0, KEY_EXIT, 0, 0,
74 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RED, KEY_GREEN, KEY_YELLOW,
76 KEY_BLUE, 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_LIST, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN,
82 0, 0, 0, 0, KEY_EPG, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_VCR
86};
87
88
89/* key-up timer */
90static void av7110_emit_keyup(unsigned long parm)
91{
92 struct infrared *ir = (struct infrared *) parm;
93
94 if (!ir || !test_bit(ir->last_key, ir->input_dev->key))
95 return;
96
97 input_report_key(ir->input_dev, ir->last_key, 0);
98 input_sync(ir->input_dev);
99}
100
101
102/* tasklet */
103static void av7110_emit_key(unsigned long parm)
104{
105 struct infrared *ir = (struct infrared *) parm;
106 u32 ircom = ir->ir_command;
107 u8 data;
108 u8 addr;
109 u16 toggle;
110 u16 keycode;
111
112 /* extract device address and data */
113 switch (ir->protocol) {
114 case IR_RC5: /* RC5: 5 bits device address, 6 bits data */
115 data = ircom & 0x3f;
116 addr = (ircom >> 6) & 0x1f;
117 toggle = ircom & 0x0800;
118 break;
119
120 case IR_RCMM: /* RCMM: ? bits device address, ? bits data */
121 data = ircom & 0xff;
122 addr = (ircom >> 8) & 0x1f;
123 toggle = ircom & 0x8000;
124 break;
125
126 case IR_RC5_EXT: /* extended RC5: 5 bits device address, 7 bits data */
127 data = ircom & 0x3f;
128 addr = (ircom >> 6) & 0x1f;
129 /* invert 7th data bit for backward compatibility with RC5 keymaps */
130 if (!(ircom & 0x1000))
131 data |= 0x40;
132 toggle = ircom & 0x0800;
133 break;
134
135 default:
136 printk("%s invalid protocol %x\n", __func__, ir->protocol);
137 return;
138 }
139
140 input_event(ir->input_dev, EV_MSC, MSC_RAW, (addr << 16) | data);
141 input_event(ir->input_dev, EV_MSC, MSC_SCAN, data);
142
143 keycode = ir->key_map[data];
144
145 dprintk(16, "%s: code %08x -> addr %i data 0x%02x -> keycode %i\n",
146 __func__, ircom, addr, data, keycode);
147
148 /* check device address */
149 if (!(ir->device_mask & (1 << addr)))
150 return;
151
152 if (!keycode) {
153 printk ("%s: code %08x -> addr %i data 0x%02x -> unknown key!\n",
154 __func__, ircom, addr, data);
155 return;
156 }
157
158 if (timer_pending(&ir->keyup_timer)) {
159 del_timer(&ir->keyup_timer);
160 if (ir->last_key != keycode || toggle != ir->last_toggle) {
161 ir->delay_timer_finished = 0;
162 input_event(ir->input_dev, EV_KEY, ir->last_key, 0);
163 input_event(ir->input_dev, EV_KEY, keycode, 1);
164 input_sync(ir->input_dev);
165 } else if (ir->delay_timer_finished) {
166 input_event(ir->input_dev, EV_KEY, keycode, 2);
167 input_sync(ir->input_dev);
168 }
169 } else {
170 ir->delay_timer_finished = 0;
171 input_event(ir->input_dev, EV_KEY, keycode, 1);
172 input_sync(ir->input_dev);
173 }
174
175 ir->last_key = keycode;
176 ir->last_toggle = toggle;
177
178 ir->keyup_timer.expires = jiffies + UP_TIMEOUT;
179 add_timer(&ir->keyup_timer);
180
181}
182
183
184/* register with input layer */
185static void input_register_keys(struct infrared *ir)
186{
187 int i;
188
189 set_bit(EV_KEY, ir->input_dev->evbit);
190 set_bit(EV_REP, ir->input_dev->evbit);
191 set_bit(EV_MSC, ir->input_dev->evbit);
192
193 set_bit(MSC_RAW, ir->input_dev->mscbit);
194 set_bit(MSC_SCAN, ir->input_dev->mscbit);
195
196 memset(ir->input_dev->keybit, 0, sizeof(ir->input_dev->keybit));
197
198 for (i = 0; i < ARRAY_SIZE(ir->key_map); i++) {
199 if (ir->key_map[i] > KEY_MAX)
200 ir->key_map[i] = 0;
201 else if (ir->key_map[i] > KEY_RESERVED)
202 set_bit(ir->key_map[i], ir->input_dev->keybit);
203 }
204
205 ir->input_dev->keycode = ir->key_map;
206 ir->input_dev->keycodesize = sizeof(ir->key_map[0]);
207 ir->input_dev->keycodemax = ARRAY_SIZE(ir->key_map);
208}
209
210
211/* called by the input driver after rep[REP_DELAY] ms */
212static void input_repeat_key(unsigned long parm)
213{
214 struct infrared *ir = (struct infrared *) parm;
215
216 ir->delay_timer_finished = 1;
217}
218
219
220/* check for configuration changes */
221int av7110_check_ir_config(struct av7110 *av7110, int force)
222{
223 int i;
224 int modified = force;
225 int ret = -ENODEV;
226
227 for (i = 0; i < av_cnt; i++)
228 if (av7110 == av_list[i])
229 break;
230
231 if (i < av_cnt && av7110) {
232 if ((av7110->ir.protocol & 1) != ir_protocol[i] ||
233 av7110->ir.inversion != ir_inversion[i])
234 modified = true;
235
236 if (modified) {
237 /* protocol */
238 if (ir_protocol[i]) {
239 ir_protocol[i] = 1;
240 av7110->ir.protocol = IR_RCMM;
241 av7110->ir.ir_config = 0x0001;
242 } else if (FW_VERSION(av7110->arm_app) >= 0x2620) {
243 av7110->ir.protocol = IR_RC5_EXT;
244 av7110->ir.ir_config = 0x0002;
245 } else {
246 av7110->ir.protocol = IR_RC5;
247 av7110->ir.ir_config = 0x0000;
248 }
249 /* inversion */
250 if (ir_inversion[i]) {
251 ir_inversion[i] = 1;
252 av7110->ir.ir_config |= 0x8000;
253 }
254 av7110->ir.inversion = ir_inversion[i];
255 /* update ARM */
256 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1,
257 av7110->ir.ir_config);
258 } else
259 ret = 0;
260
261 /* address */
262 if (av7110->ir.device_mask != ir_device_mask[i])
263 av7110->ir.device_mask = ir_device_mask[i];
264 }
265
266 return ret;
267}
268
269
270/* /proc/av7110_ir interface */
271static ssize_t av7110_ir_proc_write(struct file *file, const char __user *buffer,
272 size_t count, loff_t *pos)
273{
274 char *page;
275 u32 ir_config;
276 int size = sizeof ir_config + sizeof av_list[0]->ir.key_map;
277 int i;
278
279 if (count < size)
280 return -EINVAL;
281
282 page = vmalloc(size);
283 if (!page)
284 return -ENOMEM;
285
286 if (copy_from_user(page, buffer, size)) {
287 vfree(page);
288 return -EFAULT;
289 }
290
291 memcpy(&ir_config, page, sizeof ir_config);
292
293 for (i = 0; i < av_cnt; i++) {
294 /* keymap */
295 memcpy(av_list[i]->ir.key_map, page + sizeof ir_config,
296 sizeof(av_list[i]->ir.key_map));
297 /* protocol, inversion, address */
298 ir_protocol[i] = ir_config & 0x0001;
299 ir_inversion[i] = ir_config & 0x8000 ? 1 : 0;
300 if (ir_config & 0x4000)
301 ir_device_mask[i] = 1 << ((ir_config >> 16) & 0x1f);
302 else
303 ir_device_mask[i] = IR_ALL;
304 /* update configuration */
305 av7110_check_ir_config(av_list[i], false);
306 input_register_keys(&av_list[i]->ir);
307 }
308 vfree(page);
309 return count;
310}
311
312static const struct file_operations av7110_ir_proc_fops = {
313 .owner = THIS_MODULE,
314 .write = av7110_ir_proc_write,
315 .llseek = noop_llseek,
316};
317
318/* interrupt handler */
319static void ir_handler(struct av7110 *av7110, u32 ircom)
320{
321 dprintk(4, "ir command = %08x\n", ircom);
322 av7110->ir.ir_command = ircom;
323 tasklet_schedule(&av7110->ir.ir_tasklet);
324}
325
326
327int __devinit av7110_ir_init(struct av7110 *av7110)
328{
329 struct input_dev *input_dev;
330 static struct proc_dir_entry *e;
331 int err;
332
333 if (av_cnt >= ARRAY_SIZE(av_list))
334 return -ENOSPC;
335
336 av_list[av_cnt++] = av7110;
337 av7110_check_ir_config(av7110, true);
338
339 init_timer(&av7110->ir.keyup_timer);
340 av7110->ir.keyup_timer.function = av7110_emit_keyup;
341 av7110->ir.keyup_timer.data = (unsigned long) &av7110->ir;
342
343 input_dev = input_allocate_device();
344 if (!input_dev)
345 return -ENOMEM;
346
347 av7110->ir.input_dev = input_dev;
348 snprintf(av7110->ir.input_phys, sizeof(av7110->ir.input_phys),
349 "pci-%s/ir0", pci_name(av7110->dev->pci));
350
351 input_dev->name = "DVB on-card IR receiver";
352
353 input_dev->phys = av7110->ir.input_phys;
354 input_dev->id.bustype = BUS_PCI;
355 input_dev->id.version = 2;
356 if (av7110->dev->pci->subsystem_vendor) {
357 input_dev->id.vendor = av7110->dev->pci->subsystem_vendor;
358 input_dev->id.product = av7110->dev->pci->subsystem_device;
359 } else {
360 input_dev->id.vendor = av7110->dev->pci->vendor;
361 input_dev->id.product = av7110->dev->pci->device;
362 }
363 input_dev->dev.parent = &av7110->dev->pci->dev;
364 /* initial keymap */
365 memcpy(av7110->ir.key_map, default_key_map, sizeof av7110->ir.key_map);
366 input_register_keys(&av7110->ir);
367 err = input_register_device(input_dev);
368 if (err) {
369 input_free_device(input_dev);
370 return err;
371 }
372 input_dev->timer.function = input_repeat_key;
373 input_dev->timer.data = (unsigned long) &av7110->ir;
374
375 if (av_cnt == 1) {
376 e = proc_create("av7110_ir", S_IWUSR, NULL, &av7110_ir_proc_fops);
377 if (e)
378 e->size = 4 + 256 * sizeof(u16);
379 }
380
381 tasklet_init(&av7110->ir.ir_tasklet, av7110_emit_key, (unsigned long) &av7110->ir);
382 av7110->ir.ir_handler = ir_handler;
383
384 return 0;
385}
386
387
388void __devexit av7110_ir_exit(struct av7110 *av7110)
389{
390 int i;
391
392 if (av_cnt == 0)
393 return;
394
395 del_timer_sync(&av7110->ir.keyup_timer);
396 av7110->ir.ir_handler = NULL;
397 tasklet_kill(&av7110->ir.ir_tasklet);
398
399 for (i = 0; i < av_cnt; i++)
400 if (av_list[i] == av7110) {
401 av_list[i] = av_list[av_cnt-1];
402 av_list[av_cnt-1] = NULL;
403 break;
404 }
405
406 if (av_cnt == 1)
407 remove_proc_entry("av7110_ir", NULL);
408
409 input_unregister_device(av7110->ir.input_dev);
410
411 av_cnt--;
412}
413
414//MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>, Oliver Endriss <o.endriss@gmx.de>");
415//MODULE_LICENSE("GPL");
diff --git a/drivers/media/dvb/ttpci/av7110_v4l.c b/drivers/media/dvb/ttpci/av7110_v4l.c
new file mode 100644
index 00000000000..cdd31cae46c
--- /dev/null
+++ b/drivers/media/dvb/ttpci/av7110_v4l.c
@@ -0,0 +1,932 @@
1/*
2 * av7110_v4l.c: av7110 video4linux interface for DVB and Siemens DVB-C analog module
3 *
4 * Copyright (C) 1999-2002 Ralph Metzler
5 * & Marcus Metzler for convergence integrated media GmbH
6 *
7 * originally based on code by:
8 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
24 *
25 * the project's page is at http://www.linuxtv.org/
26 */
27
28#include <linux/kernel.h>
29#include <linux/types.h>
30#include <linux/delay.h>
31#include <linux/fs.h>
32#include <linux/timer.h>
33#include <linux/poll.h>
34
35#include "av7110.h"
36#include "av7110_hw.h"
37#include "av7110_av.h"
38
39int msp_writereg(struct av7110 *av7110, u8 dev, u16 reg, u16 val)
40{
41 u8 msg[5] = { dev, reg >> 8, reg & 0xff, val >> 8 , val & 0xff };
42 struct i2c_msg msgs = { .flags = 0, .len = 5, .buf = msg };
43
44 switch (av7110->adac_type) {
45 case DVB_ADAC_MSP34x0:
46 msgs.addr = 0x40;
47 break;
48 case DVB_ADAC_MSP34x5:
49 msgs.addr = 0x42;
50 break;
51 default:
52 return 0;
53 }
54
55 if (i2c_transfer(&av7110->i2c_adap, &msgs, 1) != 1) {
56 dprintk(1, "dvb-ttpci: failed @ card %d, %u = %u\n",
57 av7110->dvb_adapter.num, reg, val);
58 return -EIO;
59 }
60 return 0;
61}
62
63static int msp_readreg(struct av7110 *av7110, u8 dev, u16 reg, u16 *val)
64{
65 u8 msg1[3] = { dev, reg >> 8, reg & 0xff };
66 u8 msg2[2];
67 struct i2c_msg msgs[2] = {
68 { .flags = 0 , .len = 3, .buf = msg1 },
69 { .flags = I2C_M_RD, .len = 2, .buf = msg2 }
70 };
71
72 switch (av7110->adac_type) {
73 case DVB_ADAC_MSP34x0:
74 msgs[0].addr = 0x40;
75 msgs[1].addr = 0x40;
76 break;
77 case DVB_ADAC_MSP34x5:
78 msgs[0].addr = 0x42;
79 msgs[1].addr = 0x42;
80 break;
81 default:
82 return 0;
83 }
84
85 if (i2c_transfer(&av7110->i2c_adap, &msgs[0], 2) != 2) {
86 dprintk(1, "dvb-ttpci: failed @ card %d, %u\n",
87 av7110->dvb_adapter.num, reg);
88 return -EIO;
89 }
90 *val = (msg2[0] << 8) | msg2[1];
91 return 0;
92}
93
94static struct v4l2_input inputs[4] = {
95 {
96 .index = 0,
97 .name = "DVB",
98 .type = V4L2_INPUT_TYPE_CAMERA,
99 .audioset = 1,
100 .tuner = 0, /* ignored */
101 .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
102 .status = 0,
103 .capabilities = V4L2_IN_CAP_STD,
104 }, {
105 .index = 1,
106 .name = "Television",
107 .type = V4L2_INPUT_TYPE_TUNER,
108 .audioset = 2,
109 .tuner = 0,
110 .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
111 .status = 0,
112 .capabilities = V4L2_IN_CAP_STD,
113 }, {
114 .index = 2,
115 .name = "Video",
116 .type = V4L2_INPUT_TYPE_CAMERA,
117 .audioset = 0,
118 .tuner = 0,
119 .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
120 .status = 0,
121 .capabilities = V4L2_IN_CAP_STD,
122 }, {
123 .index = 3,
124 .name = "Y/C",
125 .type = V4L2_INPUT_TYPE_CAMERA,
126 .audioset = 0,
127 .tuner = 0,
128 .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
129 .status = 0,
130 .capabilities = V4L2_IN_CAP_STD,
131 }
132};
133
134static int ves1820_writereg(struct saa7146_dev *dev, u8 addr, u8 reg, u8 data)
135{
136 struct av7110 *av7110 = dev->ext_priv;
137 u8 buf[] = { 0x00, reg, data };
138 struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 3 };
139
140 dprintk(4, "dev: %p\n", dev);
141
142 if (1 != i2c_transfer(&av7110->i2c_adap, &msg, 1))
143 return -1;
144 return 0;
145}
146
147static int tuner_write(struct saa7146_dev *dev, u8 addr, u8 data [4])
148{
149 struct av7110 *av7110 = dev->ext_priv;
150 struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = data, .len = 4 };
151
152 dprintk(4, "dev: %p\n", dev);
153
154 if (1 != i2c_transfer(&av7110->i2c_adap, &msg, 1))
155 return -1;
156 return 0;
157}
158
159static int ves1820_set_tv_freq(struct saa7146_dev *dev, u32 freq)
160{
161 u32 div;
162 u8 config;
163 u8 buf[4];
164
165 dprintk(4, "freq: 0x%08x\n", freq);
166
167 /* magic number: 614. tuning with the frequency given by v4l2
168 is always off by 614*62.5 = 38375 kHz...*/
169 div = freq + 614;
170
171 buf[0] = (div >> 8) & 0x7f;
172 buf[1] = div & 0xff;
173 buf[2] = 0x8e;
174
175 if (freq < (u32) (16 * 168.25))
176 config = 0xa0;
177 else if (freq < (u32) (16 * 447.25))
178 config = 0x90;
179 else
180 config = 0x30;
181 config &= ~0x02;
182
183 buf[3] = config;
184
185 return tuner_write(dev, 0x61, buf);
186}
187
188static int stv0297_set_tv_freq(struct saa7146_dev *dev, u32 freq)
189{
190 struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
191 u32 div;
192 u8 data[4];
193
194 div = (freq + 38900000 + 31250) / 62500;
195
196 data[0] = (div >> 8) & 0x7f;
197 data[1] = div & 0xff;
198 data[2] = 0xce;
199
200 if (freq < 45000000)
201 return -EINVAL;
202 else if (freq < 137000000)
203 data[3] = 0x01;
204 else if (freq < 403000000)
205 data[3] = 0x02;
206 else if (freq < 860000000)
207 data[3] = 0x04;
208 else
209 return -EINVAL;
210
211 if (av7110->fe->ops.i2c_gate_ctrl)
212 av7110->fe->ops.i2c_gate_ctrl(av7110->fe, 1);
213 return tuner_write(dev, 0x63, data);
214}
215
216
217
218static struct saa7146_standard analog_standard[];
219static struct saa7146_standard dvb_standard[];
220static struct saa7146_standard standard[];
221
222static struct v4l2_audio msp3400_v4l2_audio = {
223 .index = 0,
224 .name = "Television",
225 .capability = V4L2_AUDCAP_STEREO
226};
227
228static int av7110_dvb_c_switch(struct saa7146_fh *fh)
229{
230 struct saa7146_dev *dev = fh->dev;
231 struct saa7146_vv *vv = dev->vv_data;
232 struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
233 u16 adswitch;
234 int source, sync, err;
235
236 dprintk(4, "%p\n", av7110);
237
238 if ((vv->video_status & STATUS_OVERLAY) != 0) {
239 vv->ov_suspend = vv->video_fh;
240 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
241 if (err != 0) {
242 dprintk(2, "suspending video failed\n");
243 vv->ov_suspend = NULL;
244 }
245 }
246
247 if (0 != av7110->current_input) {
248 dprintk(1, "switching to analog TV:\n");
249 adswitch = 1;
250 source = SAA7146_HPS_SOURCE_PORT_B;
251 sync = SAA7146_HPS_SYNC_PORT_B;
252 memcpy(standard, analog_standard, sizeof(struct saa7146_standard) * 2);
253
254 switch (av7110->current_input) {
255 case 1:
256 dprintk(1, "switching SAA7113 to Analog Tuner Input.\n");
257 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0000); // loudspeaker source
258 msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0000); // headphone source
259 msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0000); // SCART 1 source
260 msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
261 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); // loudspeaker + headphone
262 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); // SCART 1 volume
263
264 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
265 if (ves1820_writereg(dev, 0x09, 0x0f, 0x60))
266 dprintk(1, "setting band in demodulator failed.\n");
267 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
268 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // TDA9819 pin9(STD)
269 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI); // TDA9819 pin30(VIF)
270 }
271 if (i2c_writereg(av7110, 0x48, 0x02, 0xd0) != 1)
272 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
273 break;
274 case 2:
275 dprintk(1, "switching SAA7113 to Video AV CVBS Input.\n");
276 if (i2c_writereg(av7110, 0x48, 0x02, 0xd2) != 1)
277 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
278 break;
279 case 3:
280 dprintk(1, "switching SAA7113 to Video AV Y/C Input.\n");
281 if (i2c_writereg(av7110, 0x48, 0x02, 0xd9) != 1)
282 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
283 break;
284 default:
285 dprintk(1, "switching SAA7113 to Input: AV7110: SAA7113: invalid input.\n");
286 }
287 } else {
288 adswitch = 0;
289 source = SAA7146_HPS_SOURCE_PORT_A;
290 sync = SAA7146_HPS_SYNC_PORT_A;
291 memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
292 dprintk(1, "switching DVB mode\n");
293 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
294 msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
295 msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
296 msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
297 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
298 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
299
300 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
301 if (ves1820_writereg(dev, 0x09, 0x0f, 0x20))
302 dprintk(1, "setting band in demodulator failed.\n");
303 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
304 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
305 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
306 }
307 }
308
309 /* hmm, this does not do anything!? */
310 if (av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, adswitch))
311 dprintk(1, "ADSwitch error\n");
312
313 saa7146_set_hps_source_and_sync(dev, source, sync);
314
315 if (vv->ov_suspend != NULL) {
316 saa7146_start_preview(vv->ov_suspend);
317 vv->ov_suspend = NULL;
318 }
319
320 return 0;
321}
322
323static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *t)
324{
325 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
326 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
327 u16 stereo_det;
328 s8 stereo;
329
330 dprintk(2, "VIDIOC_G_TUNER: %d\n", t->index);
331
332 if (!av7110->analog_tuner_flags || t->index != 0)
333 return -EINVAL;
334
335 memset(t, 0, sizeof(*t));
336 strcpy((char *)t->name, "Television");
337
338 t->type = V4L2_TUNER_ANALOG_TV;
339 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
340 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
341 t->rangelow = 772; /* 48.25 MHZ / 62.5 kHz = 772, see fi1216mk2-specs, page 2 */
342 t->rangehigh = 13684; /* 855.25 MHz / 62.5 kHz = 13684 */
343 /* FIXME: add the real signal strength here */
344 t->signal = 0xffff;
345 t->afc = 0;
346
347 /* FIXME: standard / stereo detection is still broken */
348 msp_readreg(av7110, MSP_RD_DEM, 0x007e, &stereo_det);
349 dprintk(1, "VIDIOC_G_TUNER: msp3400 TV standard detection: 0x%04x\n", stereo_det);
350 msp_readreg(av7110, MSP_RD_DSP, 0x0018, &stereo_det);
351 dprintk(1, "VIDIOC_G_TUNER: msp3400 stereo detection: 0x%04x\n", stereo_det);
352 stereo = (s8)(stereo_det >> 8);
353 if (stereo > 0x10) {
354 /* stereo */
355 t->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
356 t->audmode = V4L2_TUNER_MODE_STEREO;
357 } else if (stereo < -0x10) {
358 /* bilingual */
359 t->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
360 t->audmode = V4L2_TUNER_MODE_LANG1;
361 } else /* mono */
362 t->rxsubchans = V4L2_TUNER_SUB_MONO;
363
364 return 0;
365}
366
367static int vidioc_s_tuner(struct file *file, void *fh, struct v4l2_tuner *t)
368{
369 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
370 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
371 u16 fm_matrix, src;
372 dprintk(2, "VIDIOC_S_TUNER: %d\n", t->index);
373
374 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
375 return -EINVAL;
376
377 switch (t->audmode) {
378 case V4L2_TUNER_MODE_STEREO:
379 dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_STEREO\n");
380 fm_matrix = 0x3001; /* stereo */
381 src = 0x0020;
382 break;
383 case V4L2_TUNER_MODE_LANG1_LANG2:
384 dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1_LANG2\n");
385 fm_matrix = 0x3000; /* bilingual */
386 src = 0x0020;
387 break;
388 case V4L2_TUNER_MODE_LANG1:
389 dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1\n");
390 fm_matrix = 0x3000; /* mono */
391 src = 0x0000;
392 break;
393 case V4L2_TUNER_MODE_LANG2:
394 dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG2\n");
395 fm_matrix = 0x3000; /* mono */
396 src = 0x0010;
397 break;
398 default: /* case V4L2_TUNER_MODE_MONO: */
399 dprintk(2, "VIDIOC_S_TUNER: TDA9840_SET_MONO\n");
400 fm_matrix = 0x3000; /* mono */
401 src = 0x0030;
402 break;
403 }
404 msp_writereg(av7110, MSP_WR_DSP, 0x000e, fm_matrix);
405 msp_writereg(av7110, MSP_WR_DSP, 0x0008, src);
406 msp_writereg(av7110, MSP_WR_DSP, 0x0009, src);
407 msp_writereg(av7110, MSP_WR_DSP, 0x000a, src);
408 return 0;
409}
410
411static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *f)
412{
413 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
414 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
415
416 dprintk(2, "VIDIOC_G_FREQ: freq:0x%08x.\n", f->frequency);
417
418 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
419 return -EINVAL;
420
421 memset(f, 0, sizeof(*f));
422 f->type = V4L2_TUNER_ANALOG_TV;
423 f->frequency = av7110->current_freq;
424 return 0;
425}
426
427static int vidioc_s_frequency(struct file *file, void *fh, struct v4l2_frequency *f)
428{
429 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
430 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
431
432 dprintk(2, "VIDIOC_S_FREQUENCY: freq:0x%08x.\n", f->frequency);
433
434 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
435 return -EINVAL;
436
437 if (V4L2_TUNER_ANALOG_TV != f->type)
438 return -EINVAL;
439
440 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0xffe0); /* fast mute */
441 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0xffe0);
442
443 /* tune in desired frequency */
444 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820)
445 ves1820_set_tv_freq(dev, f->frequency);
446 else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297)
447 stv0297_set_tv_freq(dev, f->frequency);
448 av7110->current_freq = f->frequency;
449
450 msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x003f); /* start stereo detection */
451 msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x0000);
452 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); /* loudspeaker + headphone */
453 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); /* SCART 1 volume */
454 return 0;
455}
456
457static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
458{
459 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
460 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
461
462 dprintk(2, "VIDIOC_ENUMINPUT: %d\n", i->index);
463
464 if (av7110->analog_tuner_flags) {
465 if (i->index >= 4)
466 return -EINVAL;
467 } else {
468 if (i->index != 0)
469 return -EINVAL;
470 }
471
472 memcpy(i, &inputs[i->index], sizeof(struct v4l2_input));
473
474 return 0;
475}
476
477static int vidioc_g_input(struct file *file, void *fh, unsigned int *input)
478{
479 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
480 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
481
482 *input = av7110->current_input;
483 dprintk(2, "VIDIOC_G_INPUT: %d\n", *input);
484 return 0;
485}
486
487static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
488{
489 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
490 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
491
492 dprintk(2, "VIDIOC_S_INPUT: %d\n", input);
493
494 if (!av7110->analog_tuner_flags)
495 return 0;
496
497 if (input >= 4)
498 return -EINVAL;
499
500 av7110->current_input = input;
501 return av7110_dvb_c_switch(fh);
502}
503
504static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
505{
506 dprintk(2, "VIDIOC_G_AUDIO: %d\n", a->index);
507 if (a->index != 0)
508 return -EINVAL;
509 memcpy(a, &msp3400_v4l2_audio, sizeof(struct v4l2_audio));
510 return 0;
511}
512
513static int vidioc_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
514{
515 dprintk(2, "VIDIOC_S_AUDIO: %d\n", a->index);
516 return 0;
517}
518
519static int vidioc_g_sliced_vbi_cap(struct file *file, void *fh,
520 struct v4l2_sliced_vbi_cap *cap)
521{
522 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
523 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
524
525 dprintk(2, "VIDIOC_G_SLICED_VBI_CAP\n");
526 if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
527 return -EINVAL;
528 if (FW_VERSION(av7110->arm_app) >= 0x2623) {
529 cap->service_set = V4L2_SLICED_WSS_625;
530 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
531 }
532 return 0;
533}
534
535static int vidioc_g_fmt_sliced_vbi_out(struct file *file, void *fh,
536 struct v4l2_format *f)
537{
538 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
539 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
540
541 dprintk(2, "VIDIOC_G_FMT:\n");
542 if (FW_VERSION(av7110->arm_app) < 0x2623)
543 return -EINVAL;
544 memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
545 if (av7110->wssMode) {
546 f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
547 f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
548 f->fmt.sliced.io_size = sizeof(struct v4l2_sliced_vbi_data);
549 }
550 return 0;
551}
552
553static int vidioc_s_fmt_sliced_vbi_out(struct file *file, void *fh,
554 struct v4l2_format *f)
555{
556 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
557 struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
558
559 dprintk(2, "VIDIOC_S_FMT\n");
560 if (FW_VERSION(av7110->arm_app) < 0x2623)
561 return -EINVAL;
562 if (f->fmt.sliced.service_set != V4L2_SLICED_WSS_625 &&
563 f->fmt.sliced.service_lines[0][23] != V4L2_SLICED_WSS_625) {
564 memset(&f->fmt.sliced, 0, sizeof(f->fmt.sliced));
565 /* WSS controlled by firmware */
566 av7110->wssMode = 0;
567 av7110->wssData = 0;
568 return av7110_fw_cmd(av7110, COMTYPE_ENCODER,
569 SetWSSConfig, 1, 0);
570 } else {
571 memset(&f->fmt.sliced, 0, sizeof(f->fmt.sliced));
572 f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
573 f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
574 f->fmt.sliced.io_size = sizeof(struct v4l2_sliced_vbi_data);
575 /* WSS controlled by userspace */
576 av7110->wssMode = 1;
577 av7110->wssData = 0;
578 }
579 return 0;
580}
581
582static int av7110_vbi_reset(struct file *file)
583{
584 struct saa7146_fh *fh = file->private_data;
585 struct saa7146_dev *dev = fh->dev;
586 struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
587
588 dprintk(2, "%s\n", __func__);
589 av7110->wssMode = 0;
590 av7110->wssData = 0;
591 if (FW_VERSION(av7110->arm_app) < 0x2623)
592 return 0;
593 else
594 return av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 1, 0);
595}
596
597static ssize_t av7110_vbi_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
598{
599 struct saa7146_fh *fh = file->private_data;
600 struct saa7146_dev *dev = fh->dev;
601 struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
602 struct v4l2_sliced_vbi_data d;
603 int rc;
604
605 dprintk(2, "%s\n", __func__);
606 if (FW_VERSION(av7110->arm_app) < 0x2623 || !av7110->wssMode || count != sizeof d)
607 return -EINVAL;
608 if (copy_from_user(&d, data, count))
609 return -EFAULT;
610 if ((d.id != 0 && d.id != V4L2_SLICED_WSS_625) || d.field != 0 || d.line != 23)
611 return -EINVAL;
612 if (d.id)
613 av7110->wssData = ((d.data[1] << 8) & 0x3f00) | d.data[0];
614 else
615 av7110->wssData = 0x8000;
616 rc = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 1, av7110->wssData);
617 return (rc < 0) ? rc : count;
618}
619
620/****************************************************************************
621 * INITIALIZATION
622 ****************************************************************************/
623
624static u8 saa7113_init_regs[] = {
625 0x02, 0xd0,
626 0x03, 0x23,
627 0x04, 0x00,
628 0x05, 0x00,
629 0x06, 0xe9,
630 0x07, 0x0d,
631 0x08, 0x98,
632 0x09, 0x02,
633 0x0a, 0x80,
634 0x0b, 0x40,
635 0x0c, 0x40,
636 0x0d, 0x00,
637 0x0e, 0x01,
638 0x0f, 0x7c,
639 0x10, 0x48,
640 0x11, 0x0c,
641 0x12, 0x8b,
642 0x13, 0x1a,
643 0x14, 0x00,
644 0x15, 0x00,
645 0x16, 0x00,
646 0x17, 0x00,
647 0x18, 0x00,
648 0x19, 0x00,
649 0x1a, 0x00,
650 0x1b, 0x00,
651 0x1c, 0x00,
652 0x1d, 0x00,
653 0x1e, 0x00,
654
655 0x41, 0x77,
656 0x42, 0x77,
657 0x43, 0x77,
658 0x44, 0x77,
659 0x45, 0x77,
660 0x46, 0x77,
661 0x47, 0x77,
662 0x48, 0x77,
663 0x49, 0x77,
664 0x4a, 0x77,
665 0x4b, 0x77,
666 0x4c, 0x77,
667 0x4d, 0x77,
668 0x4e, 0x77,
669 0x4f, 0x77,
670 0x50, 0x77,
671 0x51, 0x77,
672 0x52, 0x77,
673 0x53, 0x77,
674 0x54, 0x77,
675 0x55, 0x77,
676 0x56, 0x77,
677 0x57, 0xff,
678
679 0xff
680};
681
682
683static struct saa7146_ext_vv av7110_vv_data_st;
684static struct saa7146_ext_vv av7110_vv_data_c;
685
686int av7110_init_analog_module(struct av7110 *av7110)
687{
688 u16 version1, version2;
689
690 if (i2c_writereg(av7110, 0x80, 0x0, 0x80) == 1 &&
691 i2c_writereg(av7110, 0x80, 0x0, 0) == 1) {
692 printk("dvb-ttpci: DVB-C analog module @ card %d detected, initializing MSP3400\n",
693 av7110->dvb_adapter.num);
694 av7110->adac_type = DVB_ADAC_MSP34x0;
695 } else if (i2c_writereg(av7110, 0x84, 0x0, 0x80) == 1 &&
696 i2c_writereg(av7110, 0x84, 0x0, 0) == 1) {
697 printk("dvb-ttpci: DVB-C analog module @ card %d detected, initializing MSP3415\n",
698 av7110->dvb_adapter.num);
699 av7110->adac_type = DVB_ADAC_MSP34x5;
700 } else
701 return -ENODEV;
702
703 msleep(100); // the probing above resets the msp...
704 msp_readreg(av7110, MSP_RD_DSP, 0x001e, &version1);
705 msp_readreg(av7110, MSP_RD_DSP, 0x001f, &version2);
706 dprintk(1, "dvb-ttpci: @ card %d MSP34xx version 0x%04x 0x%04x\n",
707 av7110->dvb_adapter.num, version1, version2);
708 msp_writereg(av7110, MSP_WR_DSP, 0x0013, 0x0c00);
709 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
710 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
711 msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
712 msp_writereg(av7110, MSP_WR_DSP, 0x0004, 0x7f00); // loudspeaker volume
713 msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
714 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
715 msp_writereg(av7110, MSP_WR_DSP, 0x000d, 0x1900); // prescale SCART
716
717 if (i2c_writereg(av7110, 0x48, 0x01, 0x00)!=1) {
718 INFO(("saa7113 not accessible.\n"));
719 } else {
720 u8 *i = saa7113_init_regs;
721
722 if ((av7110->dev->pci->subsystem_vendor == 0x110a) && (av7110->dev->pci->subsystem_device == 0x0000)) {
723 /* Fujitsu/Siemens DVB-Cable */
724 av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
725 } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x0002)) {
726 /* Hauppauge/TT DVB-C premium */
727 av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
728 } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x000A)) {
729 /* Hauppauge/TT DVB-C premium */
730 av7110->analog_tuner_flags |= ANALOG_TUNER_STV0297;
731 }
732
733 /* setup for DVB by default */
734 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
735 if (ves1820_writereg(av7110->dev, 0x09, 0x0f, 0x20))
736 dprintk(1, "setting band in demodulator failed.\n");
737 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
738 saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
739 saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
740 }
741
742 /* init the saa7113 */
743 while (*i != 0xff) {
744 if (i2c_writereg(av7110, 0x48, i[0], i[1]) != 1) {
745 dprintk(1, "saa7113 initialization failed @ card %d", av7110->dvb_adapter.num);
746 break;
747 }
748 i += 2;
749 }
750 /* setup msp for analog sound: B/G Dual-FM */
751 msp_writereg(av7110, MSP_WR_DEM, 0x00bb, 0x02d0); // AD_CV
752 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 3); // FIR1
753 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 18); // FIR1
754 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 27); // FIR1
755 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 48); // FIR1
756 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 66); // FIR1
757 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 72); // FIR1
758 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 4); // FIR2
759 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 64); // FIR2
760 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 0); // FIR2
761 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 3); // FIR2
762 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 18); // FIR2
763 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 27); // FIR2
764 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 48); // FIR2
765 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 66); // FIR2
766 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 72); // FIR2
767 msp_writereg(av7110, MSP_WR_DEM, 0x0083, 0xa000); // MODE_REG
768 msp_writereg(av7110, MSP_WR_DEM, 0x0093, 0x00aa); // DCO1_LO 5.74MHz
769 msp_writereg(av7110, MSP_WR_DEM, 0x009b, 0x04fc); // DCO1_HI
770 msp_writereg(av7110, MSP_WR_DEM, 0x00a3, 0x038e); // DCO2_LO 5.5MHz
771 msp_writereg(av7110, MSP_WR_DEM, 0x00ab, 0x04c6); // DCO2_HI
772 msp_writereg(av7110, MSP_WR_DEM, 0x0056, 0); // LOAD_REG 1/2
773 }
774
775 memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
776 /* set dd1 stream a & b */
777 saa7146_write(av7110->dev, DD1_STREAM_B, 0x00000000);
778 saa7146_write(av7110->dev, DD1_INIT, 0x03000700);
779 saa7146_write(av7110->dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
780
781 return 0;
782}
783
784int av7110_init_v4l(struct av7110 *av7110)
785{
786 struct saa7146_dev* dev = av7110->dev;
787 struct saa7146_ext_vv *vv_data;
788 int ret;
789
790 /* special case DVB-C: these cards have an analog tuner
791 plus need some special handling, so we have separate
792 saa7146_ext_vv data for these... */
793 if (av7110->analog_tuner_flags)
794 vv_data = &av7110_vv_data_c;
795 else
796 vv_data = &av7110_vv_data_st;
797 ret = saa7146_vv_init(dev, vv_data);
798
799 if (ret) {
800 ERR(("cannot init capture device. skipping.\n"));
801 return -ENODEV;
802 }
803 vv_data->ops.vidioc_enum_input = vidioc_enum_input;
804 vv_data->ops.vidioc_g_input = vidioc_g_input;
805 vv_data->ops.vidioc_s_input = vidioc_s_input;
806 vv_data->ops.vidioc_g_tuner = vidioc_g_tuner;
807 vv_data->ops.vidioc_s_tuner = vidioc_s_tuner;
808 vv_data->ops.vidioc_g_frequency = vidioc_g_frequency;
809 vv_data->ops.vidioc_s_frequency = vidioc_s_frequency;
810 vv_data->ops.vidioc_g_audio = vidioc_g_audio;
811 vv_data->ops.vidioc_s_audio = vidioc_s_audio;
812 vv_data->ops.vidioc_g_sliced_vbi_cap = vidioc_g_sliced_vbi_cap;
813 vv_data->ops.vidioc_g_fmt_sliced_vbi_out = vidioc_g_fmt_sliced_vbi_out;
814 vv_data->ops.vidioc_s_fmt_sliced_vbi_out = vidioc_s_fmt_sliced_vbi_out;
815
816 if (saa7146_register_device(&av7110->v4l_dev, dev, "av7110", VFL_TYPE_GRABBER)) {
817 ERR(("cannot register capture device. skipping.\n"));
818 saa7146_vv_release(dev);
819 return -ENODEV;
820 }
821 if (saa7146_register_device(&av7110->vbi_dev, dev, "av7110", VFL_TYPE_VBI))
822 ERR(("cannot register vbi v4l2 device. skipping.\n"));
823 return 0;
824}
825
826int av7110_exit_v4l(struct av7110 *av7110)
827{
828 struct saa7146_dev* dev = av7110->dev;
829
830 saa7146_unregister_device(&av7110->v4l_dev, av7110->dev);
831 saa7146_unregister_device(&av7110->vbi_dev, av7110->dev);
832
833 saa7146_vv_release(dev);
834
835 return 0;
836}
837
838
839
840/* FIXME: these values are experimental values that look better than the
841 values from the latest "official" driver -- at least for me... (MiHu) */
842static struct saa7146_standard standard[] = {
843 {
844 .name = "PAL", .id = V4L2_STD_PAL_BG,
845 .v_offset = 0x15, .v_field = 288,
846 .h_offset = 0x48, .h_pixels = 708,
847 .v_max_out = 576, .h_max_out = 768,
848 }, {
849 .name = "NTSC", .id = V4L2_STD_NTSC,
850 .v_offset = 0x10, .v_field = 244,
851 .h_offset = 0x40, .h_pixels = 708,
852 .v_max_out = 480, .h_max_out = 640,
853 }
854};
855
856static struct saa7146_standard analog_standard[] = {
857 {
858 .name = "PAL", .id = V4L2_STD_PAL_BG,
859 .v_offset = 0x1b, .v_field = 288,
860 .h_offset = 0x08, .h_pixels = 708,
861 .v_max_out = 576, .h_max_out = 768,
862 }, {
863 .name = "NTSC", .id = V4L2_STD_NTSC,
864 .v_offset = 0x10, .v_field = 244,
865 .h_offset = 0x40, .h_pixels = 708,
866 .v_max_out = 480, .h_max_out = 640,
867 }
868};
869
870static struct saa7146_standard dvb_standard[] = {
871 {
872 .name = "PAL", .id = V4L2_STD_PAL_BG,
873 .v_offset = 0x14, .v_field = 288,
874 .h_offset = 0x48, .h_pixels = 708,
875 .v_max_out = 576, .h_max_out = 768,
876 }, {
877 .name = "NTSC", .id = V4L2_STD_NTSC,
878 .v_offset = 0x10, .v_field = 244,
879 .h_offset = 0x40, .h_pixels = 708,
880 .v_max_out = 480, .h_max_out = 640,
881 }
882};
883
884static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
885{
886 struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
887
888 if (std->id & V4L2_STD_PAL) {
889 av7110->vidmode = AV7110_VIDEO_MODE_PAL;
890 av7110_set_vidmode(av7110, av7110->vidmode);
891 }
892 else if (std->id & V4L2_STD_NTSC) {
893 av7110->vidmode = AV7110_VIDEO_MODE_NTSC;
894 av7110_set_vidmode(av7110, av7110->vidmode);
895 }
896 else
897 return -1;
898
899 return 0;
900}
901
902
903static struct saa7146_ext_vv av7110_vv_data_st = {
904 .inputs = 1,
905 .audios = 1,
906 .capabilities = V4L2_CAP_SLICED_VBI_OUTPUT,
907 .flags = 0,
908
909 .stds = &standard[0],
910 .num_stds = ARRAY_SIZE(standard),
911 .std_callback = &std_callback,
912
913 .vbi_fops.open = av7110_vbi_reset,
914 .vbi_fops.release = av7110_vbi_reset,
915 .vbi_fops.write = av7110_vbi_write,
916};
917
918static struct saa7146_ext_vv av7110_vv_data_c = {
919 .inputs = 1,
920 .audios = 1,
921 .capabilities = V4L2_CAP_TUNER | V4L2_CAP_SLICED_VBI_OUTPUT,
922 .flags = SAA7146_USE_PORT_B_FOR_VBI,
923
924 .stds = &standard[0],
925 .num_stds = ARRAY_SIZE(standard),
926 .std_callback = &std_callback,
927
928 .vbi_fops.open = av7110_vbi_reset,
929 .vbi_fops.release = av7110_vbi_reset,
930 .vbi_fops.write = av7110_vbi_write,
931};
932
diff --git a/drivers/media/dvb/ttpci/budget-av.c b/drivers/media/dvb/ttpci/budget-av.c
new file mode 100644
index 00000000000..e957d7690bc
--- /dev/null
+++ b/drivers/media/dvb/ttpci/budget-av.c
@@ -0,0 +1,1635 @@
1/*
2 * budget-av.c: driver for the SAA7146 based Budget DVB cards
3 * with analog video in
4 *
5 * Compiled from various sources by Michael Hunold <michael@mihu.de>
6 *
7 * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
8 * Andrew de Quincey <adq_dvb@lidskialf.net>
9 *
10 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
11 *
12 * Copyright (C) 1999-2002 Ralph Metzler
13 * & Marcus Metzler for convergence integrated media GmbH
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
31 *
32 *
33 * the project's page is at http://www.linuxtv.org/
34 */
35
36#include "budget.h"
37#include "stv0299.h"
38#include "stb0899_drv.h"
39#include "stb0899_reg.h"
40#include "stb0899_cfg.h"
41#include "tda8261.h"
42#include "tda8261_cfg.h"
43#include "tda1002x.h"
44#include "tda1004x.h"
45#include "tua6100.h"
46#include "dvb-pll.h"
47#include <media/saa7146_vv.h>
48#include <linux/module.h>
49#include <linux/errno.h>
50#include <linux/slab.h>
51#include <linux/interrupt.h>
52#include <linux/input.h>
53#include <linux/spinlock.h>
54
55#include "dvb_ca_en50221.h"
56
57#define DEBICICAM 0x02420000
58
59#define SLOTSTATUS_NONE 1
60#define SLOTSTATUS_PRESENT 2
61#define SLOTSTATUS_RESET 4
62#define SLOTSTATUS_READY 8
63#define SLOTSTATUS_OCCUPIED (SLOTSTATUS_PRESENT|SLOTSTATUS_RESET|SLOTSTATUS_READY)
64
65DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
66
67struct budget_av {
68 struct budget budget;
69 struct video_device *vd;
70 int cur_input;
71 int has_saa7113;
72 struct tasklet_struct ciintf_irq_tasklet;
73 int slot_status;
74 struct dvb_ca_en50221 ca;
75 u8 reinitialise_demod:1;
76};
77
78static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot);
79
80
81/* GPIO Connections:
82 * 0 - Vcc/Reset (Reset is controlled by capacitor). Resets the frontend *AS WELL*!
83 * 1 - CI memory select 0=>IO memory, 1=>Attribute Memory
84 * 2 - CI Card Enable (Active Low)
85 * 3 - CI Card Detect
86 */
87
88/****************************************************************************
89 * INITIALIZATION
90 ****************************************************************************/
91
92static u8 i2c_readreg(struct i2c_adapter *i2c, u8 id, u8 reg)
93{
94 u8 mm1[] = { 0x00 };
95 u8 mm2[] = { 0x00 };
96 struct i2c_msg msgs[2];
97
98 msgs[0].flags = 0;
99 msgs[1].flags = I2C_M_RD;
100 msgs[0].addr = msgs[1].addr = id / 2;
101 mm1[0] = reg;
102 msgs[0].len = 1;
103 msgs[1].len = 1;
104 msgs[0].buf = mm1;
105 msgs[1].buf = mm2;
106
107 i2c_transfer(i2c, msgs, 2);
108
109 return mm2[0];
110}
111
112static int i2c_readregs(struct i2c_adapter *i2c, u8 id, u8 reg, u8 * buf, u8 len)
113{
114 u8 mm1[] = { reg };
115 struct i2c_msg msgs[2] = {
116 {.addr = id / 2,.flags = 0,.buf = mm1,.len = 1},
117 {.addr = id / 2,.flags = I2C_M_RD,.buf = buf,.len = len}
118 };
119
120 if (i2c_transfer(i2c, msgs, 2) != 2)
121 return -EIO;
122
123 return 0;
124}
125
126static int i2c_writereg(struct i2c_adapter *i2c, u8 id, u8 reg, u8 val)
127{
128 u8 msg[2] = { reg, val };
129 struct i2c_msg msgs;
130
131 msgs.flags = 0;
132 msgs.addr = id / 2;
133 msgs.len = 2;
134 msgs.buf = msg;
135 return i2c_transfer(i2c, &msgs, 1);
136}
137
138static int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address)
139{
140 struct budget_av *budget_av = (struct budget_av *) ca->data;
141 int result;
142
143 if (slot != 0)
144 return -EINVAL;
145
146 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
147 udelay(1);
148
149 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 1);
150 if (result == -ETIMEDOUT) {
151 ciintf_slot_shutdown(ca, slot);
152 printk(KERN_INFO "budget-av: cam ejected 1\n");
153 }
154 return result;
155}
156
157static int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address, u8 value)
158{
159 struct budget_av *budget_av = (struct budget_av *) ca->data;
160 int result;
161
162 if (slot != 0)
163 return -EINVAL;
164
165 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
166 udelay(1);
167
168 result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 1);
169 if (result == -ETIMEDOUT) {
170 ciintf_slot_shutdown(ca, slot);
171 printk(KERN_INFO "budget-av: cam ejected 2\n");
172 }
173 return result;
174}
175
176static int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address)
177{
178 struct budget_av *budget_av = (struct budget_av *) ca->data;
179 int result;
180
181 if (slot != 0)
182 return -EINVAL;
183
184 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
185 udelay(1);
186
187 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 3, 1, 0, 0);
188 if (result == -ETIMEDOUT) {
189 ciintf_slot_shutdown(ca, slot);
190 printk(KERN_INFO "budget-av: cam ejected 3\n");
191 return -ETIMEDOUT;
192 }
193 return result;
194}
195
196static int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address, u8 value)
197{
198 struct budget_av *budget_av = (struct budget_av *) ca->data;
199 int result;
200
201 if (slot != 0)
202 return -EINVAL;
203
204 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
205 udelay(1);
206
207 result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 3, 1, value, 0, 0);
208 if (result == -ETIMEDOUT) {
209 ciintf_slot_shutdown(ca, slot);
210 printk(KERN_INFO "budget-av: cam ejected 5\n");
211 }
212 return result;
213}
214
215static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
216{
217 struct budget_av *budget_av = (struct budget_av *) ca->data;
218 struct saa7146_dev *saa = budget_av->budget.dev;
219
220 if (slot != 0)
221 return -EINVAL;
222
223 dprintk(1, "ciintf_slot_reset\n");
224 budget_av->slot_status = SLOTSTATUS_RESET;
225
226 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
227
228 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
229 msleep(2);
230 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO); /* Vcc on */
231 msleep(20); /* 20 ms Vcc settling time */
232
233 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO); /* enable card */
234 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
235 msleep(20);
236
237 /* reinitialise the frontend if necessary */
238 if (budget_av->reinitialise_demod)
239 dvb_frontend_reinitialise(budget_av->budget.dvb_frontend);
240
241 return 0;
242}
243
244static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
245{
246 struct budget_av *budget_av = (struct budget_av *) ca->data;
247 struct saa7146_dev *saa = budget_av->budget.dev;
248
249 if (slot != 0)
250 return -EINVAL;
251
252 dprintk(1, "ciintf_slot_shutdown\n");
253
254 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
255 budget_av->slot_status = SLOTSTATUS_NONE;
256
257 return 0;
258}
259
260static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
261{
262 struct budget_av *budget_av = (struct budget_av *) ca->data;
263 struct saa7146_dev *saa = budget_av->budget.dev;
264
265 if (slot != 0)
266 return -EINVAL;
267
268 dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
269
270 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
271
272 return 0;
273}
274
275static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
276{
277 struct budget_av *budget_av = (struct budget_av *) ca->data;
278 struct saa7146_dev *saa = budget_av->budget.dev;
279 int result;
280
281 if (slot != 0)
282 return -EINVAL;
283
284 /* test the card detect line - needs to be done carefully
285 * since it never goes high for some CAMs on this interface (e.g. topuptv) */
286 if (budget_av->slot_status == SLOTSTATUS_NONE) {
287 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
288 udelay(1);
289 if (saa7146_read(saa, PSR) & MASK_06) {
290 if (budget_av->slot_status == SLOTSTATUS_NONE) {
291 budget_av->slot_status = SLOTSTATUS_PRESENT;
292 printk(KERN_INFO "budget-av: cam inserted A\n");
293 }
294 }
295 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
296 }
297
298 /* We also try and read from IO memory to work round the above detection bug. If
299 * there is no CAM, we will get a timeout. Only done if there is no cam
300 * present, since this test actually breaks some cams :(
301 *
302 * if the CI interface is not open, we also do the above test since we
303 * don't care if the cam has problems - we'll be resetting it on open() anyway */
304 if ((budget_av->slot_status == SLOTSTATUS_NONE) || (!open)) {
305 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
306 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1);
307 if ((result >= 0) && (budget_av->slot_status == SLOTSTATUS_NONE)) {
308 budget_av->slot_status = SLOTSTATUS_PRESENT;
309 printk(KERN_INFO "budget-av: cam inserted B\n");
310 } else if (result < 0) {
311 if (budget_av->slot_status != SLOTSTATUS_NONE) {
312 ciintf_slot_shutdown(ca, slot);
313 printk(KERN_INFO "budget-av: cam ejected 5\n");
314 return 0;
315 }
316 }
317 }
318
319 /* read from attribute memory in reset/ready state to know when the CAM is ready */
320 if (budget_av->slot_status == SLOTSTATUS_RESET) {
321 result = ciintf_read_attribute_mem(ca, slot, 0);
322 if (result == 0x1d) {
323 budget_av->slot_status = SLOTSTATUS_READY;
324 }
325 }
326
327 /* work out correct return code */
328 if (budget_av->slot_status != SLOTSTATUS_NONE) {
329 if (budget_av->slot_status & SLOTSTATUS_READY) {
330 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
331 }
332 return DVB_CA_EN50221_POLL_CAM_PRESENT;
333 }
334 return 0;
335}
336
337static int ciintf_init(struct budget_av *budget_av)
338{
339 struct saa7146_dev *saa = budget_av->budget.dev;
340 int result;
341
342 memset(&budget_av->ca, 0, sizeof(struct dvb_ca_en50221));
343
344 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
345 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
346 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
347 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
348
349 /* Enable DEBI pins */
350 saa7146_write(saa, MC1, MASK_27 | MASK_11);
351
352 /* register CI interface */
353 budget_av->ca.owner = THIS_MODULE;
354 budget_av->ca.read_attribute_mem = ciintf_read_attribute_mem;
355 budget_av->ca.write_attribute_mem = ciintf_write_attribute_mem;
356 budget_av->ca.read_cam_control = ciintf_read_cam_control;
357 budget_av->ca.write_cam_control = ciintf_write_cam_control;
358 budget_av->ca.slot_reset = ciintf_slot_reset;
359 budget_av->ca.slot_shutdown = ciintf_slot_shutdown;
360 budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
361 budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
362 budget_av->ca.data = budget_av;
363 budget_av->budget.ci_present = 1;
364 budget_av->slot_status = SLOTSTATUS_NONE;
365
366 if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
367 &budget_av->ca, 0, 1)) != 0) {
368 printk(KERN_ERR "budget-av: ci initialisation failed.\n");
369 goto error;
370 }
371
372 printk(KERN_INFO "budget-av: ci interface initialised.\n");
373 return 0;
374
375error:
376 saa7146_write(saa, MC1, MASK_27);
377 return result;
378}
379
380static void ciintf_deinit(struct budget_av *budget_av)
381{
382 struct saa7146_dev *saa = budget_av->budget.dev;
383
384 saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
385 saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
386 saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
387 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
388
389 /* release the CA device */
390 dvb_ca_en50221_release(&budget_av->ca);
391
392 /* disable DEBI pins */
393 saa7146_write(saa, MC1, MASK_27);
394}
395
396
397static const u8 saa7113_tab[] = {
398 0x01, 0x08,
399 0x02, 0xc0,
400 0x03, 0x33,
401 0x04, 0x00,
402 0x05, 0x00,
403 0x06, 0xeb,
404 0x07, 0xe0,
405 0x08, 0x28,
406 0x09, 0x00,
407 0x0a, 0x80,
408 0x0b, 0x47,
409 0x0c, 0x40,
410 0x0d, 0x00,
411 0x0e, 0x01,
412 0x0f, 0x44,
413
414 0x10, 0x08,
415 0x11, 0x0c,
416 0x12, 0x7b,
417 0x13, 0x00,
418 0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
419
420 0x57, 0xff,
421 0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
422 0x5b, 0x83, 0x5e, 0x00,
423 0xff
424};
425
426static int saa7113_init(struct budget_av *budget_av)
427{
428 struct budget *budget = &budget_av->budget;
429 struct saa7146_dev *saa = budget->dev;
430 const u8 *data = saa7113_tab;
431
432 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
433 msleep(200);
434
435 if (i2c_writereg(&budget->i2c_adap, 0x4a, 0x01, 0x08) != 1) {
436 dprintk(1, "saa7113 not found on KNC card\n");
437 return -ENODEV;
438 }
439
440 dprintk(1, "saa7113 detected and initializing\n");
441
442 while (*data != 0xff) {
443 i2c_writereg(&budget->i2c_adap, 0x4a, *data, *(data + 1));
444 data += 2;
445 }
446
447 dprintk(1, "saa7113 status=%02x\n", i2c_readreg(&budget->i2c_adap, 0x4a, 0x1f));
448
449 return 0;
450}
451
452static int saa7113_setinput(struct budget_av *budget_av, int input)
453{
454 struct budget *budget = &budget_av->budget;
455
456 if (1 != budget_av->has_saa7113)
457 return -ENODEV;
458
459 if (input == 1) {
460 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc7);
461 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x80);
462 } else if (input == 0) {
463 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc0);
464 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x00);
465 } else
466 return -EINVAL;
467
468 budget_av->cur_input = input;
469 return 0;
470}
471
472
473static int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
474{
475 u8 aclk = 0;
476 u8 bclk = 0;
477 u8 m1;
478
479 aclk = 0xb5;
480 if (srate < 2000000)
481 bclk = 0x86;
482 else if (srate < 5000000)
483 bclk = 0x89;
484 else if (srate < 15000000)
485 bclk = 0x8f;
486 else if (srate < 45000000)
487 bclk = 0x95;
488
489 m1 = 0x14;
490 if (srate < 4000000)
491 m1 = 0x10;
492
493 stv0299_writereg(fe, 0x13, aclk);
494 stv0299_writereg(fe, 0x14, bclk);
495 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
496 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
497 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
498 stv0299_writereg(fe, 0x0f, 0x80 | m1);
499
500 return 0;
501}
502
503static int philips_su1278_ty_ci_tuner_set_params(struct dvb_frontend *fe,
504 struct dvb_frontend_parameters *params)
505{
506 u32 div;
507 u8 buf[4];
508 struct budget *budget = (struct budget *) fe->dvb->priv;
509 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
510
511 if ((params->frequency < 950000) || (params->frequency > 2150000))
512 return -EINVAL;
513
514 div = (params->frequency + (125 - 1)) / 125; // round correctly
515 buf[0] = (div >> 8) & 0x7f;
516 buf[1] = div & 0xff;
517 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
518 buf[3] = 0x20;
519
520 if (params->u.qpsk.symbol_rate < 4000000)
521 buf[3] |= 1;
522
523 if (params->frequency < 1250000)
524 buf[3] |= 0;
525 else if (params->frequency < 1550000)
526 buf[3] |= 0x40;
527 else if (params->frequency < 2050000)
528 buf[3] |= 0x80;
529 else if (params->frequency < 2150000)
530 buf[3] |= 0xC0;
531
532 if (fe->ops.i2c_gate_ctrl)
533 fe->ops.i2c_gate_ctrl(fe, 1);
534 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
535 return -EIO;
536 return 0;
537}
538
539static u8 typhoon_cinergy1200s_inittab[] = {
540 0x01, 0x15,
541 0x02, 0x30,
542 0x03, 0x00,
543 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
544 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
545 0x06, 0x40, /* DAC not used, set to high impendance mode */
546 0x07, 0x00, /* DAC LSB */
547 0x08, 0x40, /* DiSEqC off */
548 0x09, 0x00, /* FIFO */
549 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
550 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
551 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
552 0x10, 0x3f, // AGC2 0x3d
553 0x11, 0x84,
554 0x12, 0xb9,
555 0x15, 0xc9, // lock detector threshold
556 0x16, 0x00,
557 0x17, 0x00,
558 0x18, 0x00,
559 0x19, 0x00,
560 0x1a, 0x00,
561 0x1f, 0x50,
562 0x20, 0x00,
563 0x21, 0x00,
564 0x22, 0x00,
565 0x23, 0x00,
566 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
567 0x29, 0x1e, // 1/2 threshold
568 0x2a, 0x14, // 2/3 threshold
569 0x2b, 0x0f, // 3/4 threshold
570 0x2c, 0x09, // 5/6 threshold
571 0x2d, 0x05, // 7/8 threshold
572 0x2e, 0x01,
573 0x31, 0x1f, // test all FECs
574 0x32, 0x19, // viterbi and synchro search
575 0x33, 0xfc, // rs control
576 0x34, 0x93, // error control
577 0x0f, 0x92,
578 0xff, 0xff
579};
580
581static struct stv0299_config typhoon_config = {
582 .demod_address = 0x68,
583 .inittab = typhoon_cinergy1200s_inittab,
584 .mclk = 88000000UL,
585 .invert = 0,
586 .skip_reinit = 0,
587 .lock_output = STV0299_LOCKOUTPUT_1,
588 .volt13_op0_op1 = STV0299_VOLT13_OP0,
589 .min_delay_ms = 100,
590 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
591};
592
593
594static struct stv0299_config cinergy_1200s_config = {
595 .demod_address = 0x68,
596 .inittab = typhoon_cinergy1200s_inittab,
597 .mclk = 88000000UL,
598 .invert = 0,
599 .skip_reinit = 0,
600 .lock_output = STV0299_LOCKOUTPUT_0,
601 .volt13_op0_op1 = STV0299_VOLT13_OP0,
602 .min_delay_ms = 100,
603 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
604};
605
606static struct stv0299_config cinergy_1200s_1894_0010_config = {
607 .demod_address = 0x68,
608 .inittab = typhoon_cinergy1200s_inittab,
609 .mclk = 88000000UL,
610 .invert = 1,
611 .skip_reinit = 0,
612 .lock_output = STV0299_LOCKOUTPUT_1,
613 .volt13_op0_op1 = STV0299_VOLT13_OP0,
614 .min_delay_ms = 100,
615 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
616};
617
618static int philips_cu1216_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
619{
620 struct budget *budget = (struct budget *) fe->dvb->priv;
621 u8 buf[6];
622 struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
623 int i;
624
625#define CU1216_IF 36125000
626#define TUNER_MUL 62500
627
628 u32 div = (params->frequency + CU1216_IF + TUNER_MUL / 2) / TUNER_MUL;
629
630 buf[0] = (div >> 8) & 0x7f;
631 buf[1] = div & 0xff;
632 buf[2] = 0xce;
633 buf[3] = (params->frequency < 150000000 ? 0x01 :
634 params->frequency < 445000000 ? 0x02 : 0x04);
635 buf[4] = 0xde;
636 buf[5] = 0x20;
637
638 if (fe->ops.i2c_gate_ctrl)
639 fe->ops.i2c_gate_ctrl(fe, 1);
640 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
641 return -EIO;
642
643 /* wait for the pll lock */
644 msg.flags = I2C_M_RD;
645 msg.len = 1;
646 for (i = 0; i < 20; i++) {
647 if (fe->ops.i2c_gate_ctrl)
648 fe->ops.i2c_gate_ctrl(fe, 1);
649 if (i2c_transfer(&budget->i2c_adap, &msg, 1) == 1 && (buf[0] & 0x40))
650 break;
651 msleep(10);
652 }
653
654 /* switch the charge pump to the lower current */
655 msg.flags = 0;
656 msg.len = 2;
657 msg.buf = &buf[2];
658 buf[2] &= ~0x40;
659 if (fe->ops.i2c_gate_ctrl)
660 fe->ops.i2c_gate_ctrl(fe, 1);
661 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
662 return -EIO;
663
664 return 0;
665}
666
667static struct tda1002x_config philips_cu1216_config = {
668 .demod_address = 0x0c,
669 .invert = 1,
670};
671
672static struct tda1002x_config philips_cu1216_config_altaddress = {
673 .demod_address = 0x0d,
674 .invert = 0,
675};
676
677static struct tda10023_config philips_cu1216_tda10023_config = {
678 .demod_address = 0x0c,
679 .invert = 1,
680};
681
682static int philips_tu1216_tuner_init(struct dvb_frontend *fe)
683{
684 struct budget *budget = (struct budget *) fe->dvb->priv;
685 static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
686 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
687
688 // setup PLL configuration
689 if (fe->ops.i2c_gate_ctrl)
690 fe->ops.i2c_gate_ctrl(fe, 1);
691 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
692 return -EIO;
693 msleep(1);
694
695 return 0;
696}
697
698static int philips_tu1216_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
699{
700 struct budget *budget = (struct budget *) fe->dvb->priv;
701 u8 tuner_buf[4];
702 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
703 sizeof(tuner_buf) };
704 int tuner_frequency = 0;
705 u8 band, cp, filter;
706
707 // determine charge pump
708 tuner_frequency = params->frequency + 36166000;
709 if (tuner_frequency < 87000000)
710 return -EINVAL;
711 else if (tuner_frequency < 130000000)
712 cp = 3;
713 else if (tuner_frequency < 160000000)
714 cp = 5;
715 else if (tuner_frequency < 200000000)
716 cp = 6;
717 else if (tuner_frequency < 290000000)
718 cp = 3;
719 else if (tuner_frequency < 420000000)
720 cp = 5;
721 else if (tuner_frequency < 480000000)
722 cp = 6;
723 else if (tuner_frequency < 620000000)
724 cp = 3;
725 else if (tuner_frequency < 830000000)
726 cp = 5;
727 else if (tuner_frequency < 895000000)
728 cp = 7;
729 else
730 return -EINVAL;
731
732 // determine band
733 if (params->frequency < 49000000)
734 return -EINVAL;
735 else if (params->frequency < 161000000)
736 band = 1;
737 else if (params->frequency < 444000000)
738 band = 2;
739 else if (params->frequency < 861000000)
740 band = 4;
741 else
742 return -EINVAL;
743
744 // setup PLL filter
745 switch (params->u.ofdm.bandwidth) {
746 case BANDWIDTH_6_MHZ:
747 filter = 0;
748 break;
749
750 case BANDWIDTH_7_MHZ:
751 filter = 0;
752 break;
753
754 case BANDWIDTH_8_MHZ:
755 filter = 1;
756 break;
757
758 default:
759 return -EINVAL;
760 }
761
762 // calculate divisor
763 // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
764 tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
765
766 // setup tuner buffer
767 tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
768 tuner_buf[1] = tuner_frequency & 0xff;
769 tuner_buf[2] = 0xca;
770 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
771
772 if (fe->ops.i2c_gate_ctrl)
773 fe->ops.i2c_gate_ctrl(fe, 1);
774 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
775 return -EIO;
776
777 msleep(1);
778 return 0;
779}
780
781static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
782 const struct firmware **fw, char *name)
783{
784 struct budget *budget = (struct budget *) fe->dvb->priv;
785
786 return request_firmware(fw, name, &budget->dev->pci->dev);
787}
788
789static struct tda1004x_config philips_tu1216_config = {
790
791 .demod_address = 0x8,
792 .invert = 1,
793 .invert_oclk = 1,
794 .xtal_freq = TDA10046_XTAL_4M,
795 .agc_config = TDA10046_AGC_DEFAULT,
796 .if_freq = TDA10046_FREQ_3617,
797 .request_firmware = philips_tu1216_request_firmware,
798};
799
800static u8 philips_sd1878_inittab[] = {
801 0x01, 0x15,
802 0x02, 0x30,
803 0x03, 0x00,
804 0x04, 0x7d,
805 0x05, 0x35,
806 0x06, 0x40,
807 0x07, 0x00,
808 0x08, 0x43,
809 0x09, 0x02,
810 0x0C, 0x51,
811 0x0D, 0x82,
812 0x0E, 0x23,
813 0x10, 0x3f,
814 0x11, 0x84,
815 0x12, 0xb9,
816 0x15, 0xc9,
817 0x16, 0x19,
818 0x17, 0x8c,
819 0x18, 0x59,
820 0x19, 0xf8,
821 0x1a, 0xfe,
822 0x1c, 0x7f,
823 0x1d, 0x00,
824 0x1e, 0x00,
825 0x1f, 0x50,
826 0x20, 0x00,
827 0x21, 0x00,
828 0x22, 0x00,
829 0x23, 0x00,
830 0x28, 0x00,
831 0x29, 0x28,
832 0x2a, 0x14,
833 0x2b, 0x0f,
834 0x2c, 0x09,
835 0x2d, 0x09,
836 0x31, 0x1f,
837 0x32, 0x19,
838 0x33, 0xfc,
839 0x34, 0x93,
840 0xff, 0xff
841};
842
843static int philips_sd1878_ci_set_symbol_rate(struct dvb_frontend *fe,
844 u32 srate, u32 ratio)
845{
846 u8 aclk = 0;
847 u8 bclk = 0;
848 u8 m1;
849
850 aclk = 0xb5;
851 if (srate < 2000000)
852 bclk = 0x86;
853 else if (srate < 5000000)
854 bclk = 0x89;
855 else if (srate < 15000000)
856 bclk = 0x8f;
857 else if (srate < 45000000)
858 bclk = 0x95;
859
860 m1 = 0x14;
861 if (srate < 4000000)
862 m1 = 0x10;
863
864 stv0299_writereg(fe, 0x0e, 0x23);
865 stv0299_writereg(fe, 0x0f, 0x94);
866 stv0299_writereg(fe, 0x10, 0x39);
867 stv0299_writereg(fe, 0x13, aclk);
868 stv0299_writereg(fe, 0x14, bclk);
869 stv0299_writereg(fe, 0x15, 0xc9);
870 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
871 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
872 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
873 stv0299_writereg(fe, 0x0f, 0x80 | m1);
874
875 return 0;
876}
877
878static struct stv0299_config philips_sd1878_config = {
879 .demod_address = 0x68,
880 .inittab = philips_sd1878_inittab,
881 .mclk = 88000000UL,
882 .invert = 0,
883 .skip_reinit = 0,
884 .lock_output = STV0299_LOCKOUTPUT_1,
885 .volt13_op0_op1 = STV0299_VOLT13_OP0,
886 .min_delay_ms = 100,
887 .set_symbol_rate = philips_sd1878_ci_set_symbol_rate,
888};
889
890/* KNC1 DVB-S (STB0899) Inittab */
891static const struct stb0899_s1_reg knc1_stb0899_s1_init_1[] = {
892
893 { STB0899_DEV_ID , 0x81 },
894 { STB0899_DISCNTRL1 , 0x32 },
895 { STB0899_DISCNTRL2 , 0x80 },
896 { STB0899_DISRX_ST0 , 0x04 },
897 { STB0899_DISRX_ST1 , 0x00 },
898 { STB0899_DISPARITY , 0x00 },
899 { STB0899_DISFIFO , 0x00 },
900 { STB0899_DISSTATUS , 0x20 },
901 { STB0899_DISF22 , 0x8c },
902 { STB0899_DISF22RX , 0x9a },
903 { STB0899_SYSREG , 0x0b },
904 { STB0899_ACRPRESC , 0x11 },
905 { STB0899_ACRDIV1 , 0x0a },
906 { STB0899_ACRDIV2 , 0x05 },
907 { STB0899_DACR1 , 0x00 },
908 { STB0899_DACR2 , 0x00 },
909 { STB0899_OUTCFG , 0x00 },
910 { STB0899_MODECFG , 0x00 },
911 { STB0899_IRQSTATUS_3 , 0x30 },
912 { STB0899_IRQSTATUS_2 , 0x00 },
913 { STB0899_IRQSTATUS_1 , 0x00 },
914 { STB0899_IRQSTATUS_0 , 0x00 },
915 { STB0899_IRQMSK_3 , 0xf3 },
916 { STB0899_IRQMSK_2 , 0xfc },
917 { STB0899_IRQMSK_1 , 0xff },
918 { STB0899_IRQMSK_0 , 0xff },
919 { STB0899_IRQCFG , 0x00 },
920 { STB0899_I2CCFG , 0x88 },
921 { STB0899_I2CRPT , 0x58 }, /* Repeater=8, Stop=disabled */
922 { STB0899_IOPVALUE5 , 0x00 },
923 { STB0899_IOPVALUE4 , 0x20 },
924 { STB0899_IOPVALUE3 , 0xc9 },
925 { STB0899_IOPVALUE2 , 0x90 },
926 { STB0899_IOPVALUE1 , 0x40 },
927 { STB0899_IOPVALUE0 , 0x00 },
928 { STB0899_GPIO00CFG , 0x82 },
929 { STB0899_GPIO01CFG , 0x82 },
930 { STB0899_GPIO02CFG , 0x82 },
931 { STB0899_GPIO03CFG , 0x82 },
932 { STB0899_GPIO04CFG , 0x82 },
933 { STB0899_GPIO05CFG , 0x82 },
934 { STB0899_GPIO06CFG , 0x82 },
935 { STB0899_GPIO07CFG , 0x82 },
936 { STB0899_GPIO08CFG , 0x82 },
937 { STB0899_GPIO09CFG , 0x82 },
938 { STB0899_GPIO10CFG , 0x82 },
939 { STB0899_GPIO11CFG , 0x82 },
940 { STB0899_GPIO12CFG , 0x82 },
941 { STB0899_GPIO13CFG , 0x82 },
942 { STB0899_GPIO14CFG , 0x82 },
943 { STB0899_GPIO15CFG , 0x82 },
944 { STB0899_GPIO16CFG , 0x82 },
945 { STB0899_GPIO17CFG , 0x82 },
946 { STB0899_GPIO18CFG , 0x82 },
947 { STB0899_GPIO19CFG , 0x82 },
948 { STB0899_GPIO20CFG , 0x82 },
949 { STB0899_SDATCFG , 0xb8 },
950 { STB0899_SCLTCFG , 0xba },
951 { STB0899_AGCRFCFG , 0x08 }, /* 0x1c */
952 { STB0899_GPIO22 , 0x82 }, /* AGCBB2CFG */
953 { STB0899_GPIO21 , 0x91 }, /* AGCBB1CFG */
954 { STB0899_DIRCLKCFG , 0x82 },
955 { STB0899_CLKOUT27CFG , 0x7e },
956 { STB0899_STDBYCFG , 0x82 },
957 { STB0899_CS0CFG , 0x82 },
958 { STB0899_CS1CFG , 0x82 },
959 { STB0899_DISEQCOCFG , 0x20 },
960 { STB0899_GPIO32CFG , 0x82 },
961 { STB0899_GPIO33CFG , 0x82 },
962 { STB0899_GPIO34CFG , 0x82 },
963 { STB0899_GPIO35CFG , 0x82 },
964 { STB0899_GPIO36CFG , 0x82 },
965 { STB0899_GPIO37CFG , 0x82 },
966 { STB0899_GPIO38CFG , 0x82 },
967 { STB0899_GPIO39CFG , 0x82 },
968 { STB0899_NCOARSE , 0x15 }, /* 0x15 = 27 Mhz Clock, F/3 = 198MHz, F/6 = 99MHz */
969 { STB0899_SYNTCTRL , 0x02 }, /* 0x00 = CLK from CLKI, 0x02 = CLK from XTALI */
970 { STB0899_FILTCTRL , 0x00 },
971 { STB0899_SYSCTRL , 0x00 },
972 { STB0899_STOPCLK1 , 0x20 },
973 { STB0899_STOPCLK2 , 0x00 },
974 { STB0899_INTBUFSTATUS , 0x00 },
975 { STB0899_INTBUFCTRL , 0x0a },
976 { 0xffff , 0xff },
977};
978
979static const struct stb0899_s1_reg knc1_stb0899_s1_init_3[] = {
980 { STB0899_DEMOD , 0x00 },
981 { STB0899_RCOMPC , 0xc9 },
982 { STB0899_AGC1CN , 0x41 },
983 { STB0899_AGC1REF , 0x08 },
984 { STB0899_RTC , 0x7a },
985 { STB0899_TMGCFG , 0x4e },
986 { STB0899_AGC2REF , 0x33 },
987 { STB0899_TLSR , 0x84 },
988 { STB0899_CFD , 0xee },
989 { STB0899_ACLC , 0x87 },
990 { STB0899_BCLC , 0x94 },
991 { STB0899_EQON , 0x41 },
992 { STB0899_LDT , 0xdd },
993 { STB0899_LDT2 , 0xc9 },
994 { STB0899_EQUALREF , 0xb4 },
995 { STB0899_TMGRAMP , 0x10 },
996 { STB0899_TMGTHD , 0x30 },
997 { STB0899_IDCCOMP , 0xfb },
998 { STB0899_QDCCOMP , 0x03 },
999 { STB0899_POWERI , 0x3b },
1000 { STB0899_POWERQ , 0x3d },
1001 { STB0899_RCOMP , 0x81 },
1002 { STB0899_AGCIQIN , 0x80 },
1003 { STB0899_AGC2I1 , 0x04 },
1004 { STB0899_AGC2I2 , 0xf5 },
1005 { STB0899_TLIR , 0x25 },
1006 { STB0899_RTF , 0x80 },
1007 { STB0899_DSTATUS , 0x00 },
1008 { STB0899_LDI , 0xca },
1009 { STB0899_CFRM , 0xf1 },
1010 { STB0899_CFRL , 0xf3 },
1011 { STB0899_NIRM , 0x2a },
1012 { STB0899_NIRL , 0x05 },
1013 { STB0899_ISYMB , 0x17 },
1014 { STB0899_QSYMB , 0xfa },
1015 { STB0899_SFRH , 0x2f },
1016 { STB0899_SFRM , 0x68 },
1017 { STB0899_SFRL , 0x40 },
1018 { STB0899_SFRUPH , 0x2f },
1019 { STB0899_SFRUPM , 0x68 },
1020 { STB0899_SFRUPL , 0x40 },
1021 { STB0899_EQUAI1 , 0xfd },
1022 { STB0899_EQUAQ1 , 0x04 },
1023 { STB0899_EQUAI2 , 0x0f },
1024 { STB0899_EQUAQ2 , 0xff },
1025 { STB0899_EQUAI3 , 0xdf },
1026 { STB0899_EQUAQ3 , 0xfa },
1027 { STB0899_EQUAI4 , 0x37 },
1028 { STB0899_EQUAQ4 , 0x0d },
1029 { STB0899_EQUAI5 , 0xbd },
1030 { STB0899_EQUAQ5 , 0xf7 },
1031 { STB0899_DSTATUS2 , 0x00 },
1032 { STB0899_VSTATUS , 0x00 },
1033 { STB0899_VERROR , 0xff },
1034 { STB0899_IQSWAP , 0x2a },
1035 { STB0899_ECNT1M , 0x00 },
1036 { STB0899_ECNT1L , 0x00 },
1037 { STB0899_ECNT2M , 0x00 },
1038 { STB0899_ECNT2L , 0x00 },
1039 { STB0899_ECNT3M , 0x00 },
1040 { STB0899_ECNT3L , 0x00 },
1041 { STB0899_FECAUTO1 , 0x06 },
1042 { STB0899_FECM , 0x01 },
1043 { STB0899_VTH12 , 0xf0 },
1044 { STB0899_VTH23 , 0xa0 },
1045 { STB0899_VTH34 , 0x78 },
1046 { STB0899_VTH56 , 0x4e },
1047 { STB0899_VTH67 , 0x48 },
1048 { STB0899_VTH78 , 0x38 },
1049 { STB0899_PRVIT , 0xff },
1050 { STB0899_VITSYNC , 0x19 },
1051 { STB0899_RSULC , 0xb1 }, /* DVB = 0xb1, DSS = 0xa1 */
1052 { STB0899_TSULC , 0x42 },
1053 { STB0899_RSLLC , 0x40 },
1054 { STB0899_TSLPL , 0x12 },
1055 { STB0899_TSCFGH , 0x0c },
1056 { STB0899_TSCFGM , 0x00 },
1057 { STB0899_TSCFGL , 0x0c },
1058 { STB0899_TSOUT , 0x4d }, /* 0x0d for CAM */
1059 { STB0899_RSSYNCDEL , 0x00 },
1060 { STB0899_TSINHDELH , 0x02 },
1061 { STB0899_TSINHDELM , 0x00 },
1062 { STB0899_TSINHDELL , 0x00 },
1063 { STB0899_TSLLSTKM , 0x00 },
1064 { STB0899_TSLLSTKL , 0x00 },
1065 { STB0899_TSULSTKM , 0x00 },
1066 { STB0899_TSULSTKL , 0xab },
1067 { STB0899_PCKLENUL , 0x00 },
1068 { STB0899_PCKLENLL , 0xcc },
1069 { STB0899_RSPCKLEN , 0xcc },
1070 { STB0899_TSSTATUS , 0x80 },
1071 { STB0899_ERRCTRL1 , 0xb6 },
1072 { STB0899_ERRCTRL2 , 0x96 },
1073 { STB0899_ERRCTRL3 , 0x89 },
1074 { STB0899_DMONMSK1 , 0x27 },
1075 { STB0899_DMONMSK0 , 0x03 },
1076 { STB0899_DEMAPVIT , 0x5c },
1077 { STB0899_PLPARM , 0x1f },
1078 { STB0899_PDELCTRL , 0x48 },
1079 { STB0899_PDELCTRL2 , 0x00 },
1080 { STB0899_BBHCTRL1 , 0x00 },
1081 { STB0899_BBHCTRL2 , 0x00 },
1082 { STB0899_HYSTTHRESH , 0x77 },
1083 { STB0899_MATCSTM , 0x00 },
1084 { STB0899_MATCSTL , 0x00 },
1085 { STB0899_UPLCSTM , 0x00 },
1086 { STB0899_UPLCSTL , 0x00 },
1087 { STB0899_DFLCSTM , 0x00 },
1088 { STB0899_DFLCSTL , 0x00 },
1089 { STB0899_SYNCCST , 0x00 },
1090 { STB0899_SYNCDCSTM , 0x00 },
1091 { STB0899_SYNCDCSTL , 0x00 },
1092 { STB0899_ISI_ENTRY , 0x00 },
1093 { STB0899_ISI_BIT_EN , 0x00 },
1094 { STB0899_MATSTRM , 0x00 },
1095 { STB0899_MATSTRL , 0x00 },
1096 { STB0899_UPLSTRM , 0x00 },
1097 { STB0899_UPLSTRL , 0x00 },
1098 { STB0899_DFLSTRM , 0x00 },
1099 { STB0899_DFLSTRL , 0x00 },
1100 { STB0899_SYNCSTR , 0x00 },
1101 { STB0899_SYNCDSTRM , 0x00 },
1102 { STB0899_SYNCDSTRL , 0x00 },
1103 { STB0899_CFGPDELSTATUS1 , 0x10 },
1104 { STB0899_CFGPDELSTATUS2 , 0x00 },
1105 { STB0899_BBFERRORM , 0x00 },
1106 { STB0899_BBFERRORL , 0x00 },
1107 { STB0899_UPKTERRORM , 0x00 },
1108 { STB0899_UPKTERRORL , 0x00 },
1109 { 0xffff , 0xff },
1110};
1111
1112/* STB0899 demodulator config for the KNC1 and clones */
1113static struct stb0899_config knc1_dvbs2_config = {
1114 .init_dev = knc1_stb0899_s1_init_1,
1115 .init_s2_demod = stb0899_s2_init_2,
1116 .init_s1_demod = knc1_stb0899_s1_init_3,
1117 .init_s2_fec = stb0899_s2_init_4,
1118 .init_tst = stb0899_s1_init_5,
1119
1120 .postproc = NULL,
1121
1122 .demod_address = 0x68,
1123// .ts_output_mode = STB0899_OUT_PARALLEL, /* types = SERIAL/PARALLEL */
1124 .block_sync_mode = STB0899_SYNC_FORCED, /* DSS, SYNC_FORCED/UNSYNCED */
1125// .ts_pfbit_toggle = STB0899_MPEG_NORMAL, /* DirecTV, MPEG toggling seq */
1126
1127 .xtal_freq = 27000000,
1128 .inversion = IQ_SWAP_OFF, /* 1 */
1129
1130 .lo_clk = 76500000,
1131 .hi_clk = 90000000,
1132
1133 .esno_ave = STB0899_DVBS2_ESNO_AVE,
1134 .esno_quant = STB0899_DVBS2_ESNO_QUANT,
1135 .avframes_coarse = STB0899_DVBS2_AVFRAMES_COARSE,
1136 .avframes_fine = STB0899_DVBS2_AVFRAMES_FINE,
1137 .miss_threshold = STB0899_DVBS2_MISS_THRESHOLD,
1138 .uwp_threshold_acq = STB0899_DVBS2_UWP_THRESHOLD_ACQ,
1139 .uwp_threshold_track = STB0899_DVBS2_UWP_THRESHOLD_TRACK,
1140 .uwp_threshold_sof = STB0899_DVBS2_UWP_THRESHOLD_SOF,
1141 .sof_search_timeout = STB0899_DVBS2_SOF_SEARCH_TIMEOUT,
1142
1143 .btr_nco_bits = STB0899_DVBS2_BTR_NCO_BITS,
1144 .btr_gain_shift_offset = STB0899_DVBS2_BTR_GAIN_SHIFT_OFFSET,
1145 .crl_nco_bits = STB0899_DVBS2_CRL_NCO_BITS,
1146 .ldpc_max_iter = STB0899_DVBS2_LDPC_MAX_ITER,
1147
1148 .tuner_get_frequency = tda8261_get_frequency,
1149 .tuner_set_frequency = tda8261_set_frequency,
1150 .tuner_set_bandwidth = NULL,
1151 .tuner_get_bandwidth = tda8261_get_bandwidth,
1152 .tuner_set_rfsiggain = NULL
1153};
1154
1155/*
1156 * SD1878/SHA tuner config
1157 * 1F, Single I/P, Horizontal mount, High Sensitivity
1158 */
1159static const struct tda8261_config sd1878c_config = {
1160// .name = "SD1878/SHA",
1161 .addr = 0x60,
1162 .step_size = TDA8261_STEP_1000 /* kHz */
1163};
1164
1165static u8 read_pwm(struct budget_av *budget_av)
1166{
1167 u8 b = 0xff;
1168 u8 pwm;
1169 struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
1170 {.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
1171 };
1172
1173 if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
1174 || (pwm == 0xff))
1175 pwm = 0x48;
1176
1177 return pwm;
1178}
1179
1180#define SUBID_DVBS_KNC1 0x0010
1181#define SUBID_DVBS_KNC1_PLUS 0x0011
1182#define SUBID_DVBS_TYPHOON 0x4f56
1183#define SUBID_DVBS_CINERGY1200 0x1154
1184#define SUBID_DVBS_CYNERGY1200N 0x1155
1185#define SUBID_DVBS_TV_STAR 0x0014
1186#define SUBID_DVBS_TV_STAR_PLUS_X4 0x0015
1187#define SUBID_DVBS_TV_STAR_CI 0x0016
1188#define SUBID_DVBS2_KNC1 0x0018
1189#define SUBID_DVBS2_KNC1_OEM 0x0019
1190#define SUBID_DVBS_EASYWATCH_1 0x001a
1191#define SUBID_DVBS_EASYWATCH_2 0x001b
1192#define SUBID_DVBS2_EASYWATCH 0x001d
1193#define SUBID_DVBS_EASYWATCH 0x001e
1194
1195#define SUBID_DVBC_EASYWATCH 0x002a
1196#define SUBID_DVBC_EASYWATCH_MK3 0x002c
1197#define SUBID_DVBC_KNC1 0x0020
1198#define SUBID_DVBC_KNC1_PLUS 0x0021
1199#define SUBID_DVBC_KNC1_MK3 0x0022
1200#define SUBID_DVBC_KNC1_PLUS_MK3 0x0023
1201#define SUBID_DVBC_CINERGY1200 0x1156
1202#define SUBID_DVBC_CINERGY1200_MK3 0x1176
1203
1204#define SUBID_DVBT_EASYWATCH 0x003a
1205#define SUBID_DVBT_KNC1_PLUS 0x0031
1206#define SUBID_DVBT_KNC1 0x0030
1207#define SUBID_DVBT_CINERGY1200 0x1157
1208
1209static void frontend_init(struct budget_av *budget_av)
1210{
1211 struct saa7146_dev * saa = budget_av->budget.dev;
1212 struct dvb_frontend * fe = NULL;
1213
1214 /* Enable / PowerON Frontend */
1215 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
1216
1217 /* Wait for PowerON */
1218 msleep(100);
1219
1220 /* additional setup necessary for the PLUS cards */
1221 switch (saa->pci->subsystem_device) {
1222 case SUBID_DVBS_KNC1_PLUS:
1223 case SUBID_DVBC_KNC1_PLUS:
1224 case SUBID_DVBT_KNC1_PLUS:
1225 case SUBID_DVBC_EASYWATCH:
1226 case SUBID_DVBC_KNC1_PLUS_MK3:
1227 case SUBID_DVBS2_KNC1:
1228 case SUBID_DVBS2_KNC1_OEM:
1229 case SUBID_DVBS2_EASYWATCH:
1230 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
1231 break;
1232 }
1233
1234 switch (saa->pci->subsystem_device) {
1235
1236 case SUBID_DVBS_KNC1:
1237 /*
1238 * maybe that setting is needed for other dvb-s cards as well,
1239 * but so far it has been only confirmed for this type
1240 */
1241 budget_av->reinitialise_demod = 1;
1242 /* fall through */
1243 case SUBID_DVBS_KNC1_PLUS:
1244 case SUBID_DVBS_EASYWATCH_1:
1245 if (saa->pci->subsystem_vendor == 0x1894) {
1246 fe = dvb_attach(stv0299_attach, &cinergy_1200s_1894_0010_config,
1247 &budget_av->budget.i2c_adap);
1248 if (fe) {
1249 dvb_attach(tua6100_attach, fe, 0x60, &budget_av->budget.i2c_adap);
1250 }
1251 } else {
1252 fe = dvb_attach(stv0299_attach, &typhoon_config,
1253 &budget_av->budget.i2c_adap);
1254 if (fe) {
1255 fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
1256 }
1257 }
1258 break;
1259
1260 case SUBID_DVBS_TV_STAR:
1261 case SUBID_DVBS_TV_STAR_PLUS_X4:
1262 case SUBID_DVBS_TV_STAR_CI:
1263 case SUBID_DVBS_CYNERGY1200N:
1264 case SUBID_DVBS_EASYWATCH:
1265 case SUBID_DVBS_EASYWATCH_2:
1266 fe = dvb_attach(stv0299_attach, &philips_sd1878_config,
1267 &budget_av->budget.i2c_adap);
1268 if (fe) {
1269 dvb_attach(dvb_pll_attach, fe, 0x60,
1270 &budget_av->budget.i2c_adap,
1271 DVB_PLL_PHILIPS_SD1878_TDA8261);
1272 }
1273 break;
1274
1275 case SUBID_DVBS_TYPHOON:
1276 fe = dvb_attach(stv0299_attach, &typhoon_config,
1277 &budget_av->budget.i2c_adap);
1278 if (fe) {
1279 fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
1280 }
1281 break;
1282 case SUBID_DVBS2_KNC1:
1283 case SUBID_DVBS2_KNC1_OEM:
1284 case SUBID_DVBS2_EASYWATCH:
1285 budget_av->reinitialise_demod = 1;
1286 if ((fe = dvb_attach(stb0899_attach, &knc1_dvbs2_config, &budget_av->budget.i2c_adap)))
1287 dvb_attach(tda8261_attach, fe, &sd1878c_config, &budget_av->budget.i2c_adap);
1288
1289 break;
1290 case SUBID_DVBS_CINERGY1200:
1291 fe = dvb_attach(stv0299_attach, &cinergy_1200s_config,
1292 &budget_av->budget.i2c_adap);
1293 if (fe) {
1294 fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
1295 }
1296 break;
1297
1298 case SUBID_DVBC_KNC1:
1299 case SUBID_DVBC_KNC1_PLUS:
1300 case SUBID_DVBC_CINERGY1200:
1301 case SUBID_DVBC_EASYWATCH:
1302 budget_av->reinitialise_demod = 1;
1303 budget_av->budget.dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
1304 fe = dvb_attach(tda10021_attach, &philips_cu1216_config,
1305 &budget_av->budget.i2c_adap,
1306 read_pwm(budget_av));
1307 if (fe == NULL)
1308 fe = dvb_attach(tda10021_attach, &philips_cu1216_config_altaddress,
1309 &budget_av->budget.i2c_adap,
1310 read_pwm(budget_av));
1311 if (fe) {
1312 fe->ops.tuner_ops.set_params = philips_cu1216_tuner_set_params;
1313 }
1314 break;
1315
1316 case SUBID_DVBC_EASYWATCH_MK3:
1317 case SUBID_DVBC_CINERGY1200_MK3:
1318 case SUBID_DVBC_KNC1_MK3:
1319 case SUBID_DVBC_KNC1_PLUS_MK3:
1320 budget_av->reinitialise_demod = 1;
1321 budget_av->budget.dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
1322 fe = dvb_attach(tda10023_attach,
1323 &philips_cu1216_tda10023_config,
1324 &budget_av->budget.i2c_adap,
1325 read_pwm(budget_av));
1326 if (fe) {
1327 fe->ops.tuner_ops.set_params = philips_cu1216_tuner_set_params;
1328 }
1329 break;
1330
1331 case SUBID_DVBT_EASYWATCH:
1332 case SUBID_DVBT_KNC1:
1333 case SUBID_DVBT_KNC1_PLUS:
1334 case SUBID_DVBT_CINERGY1200:
1335 budget_av->reinitialise_demod = 1;
1336 fe = dvb_attach(tda10046_attach, &philips_tu1216_config,
1337 &budget_av->budget.i2c_adap);
1338 if (fe) {
1339 fe->ops.tuner_ops.init = philips_tu1216_tuner_init;
1340 fe->ops.tuner_ops.set_params = philips_tu1216_tuner_set_params;
1341 }
1342 break;
1343 }
1344
1345 if (fe == NULL) {
1346 printk(KERN_ERR "budget-av: A frontend driver was not found "
1347 "for device [%04x:%04x] subsystem [%04x:%04x]\n",
1348 saa->pci->vendor,
1349 saa->pci->device,
1350 saa->pci->subsystem_vendor,
1351 saa->pci->subsystem_device);
1352 return;
1353 }
1354
1355 budget_av->budget.dvb_frontend = fe;
1356
1357 if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
1358 budget_av->budget.dvb_frontend)) {
1359 printk(KERN_ERR "budget-av: Frontend registration failed!\n");
1360 dvb_frontend_detach(budget_av->budget.dvb_frontend);
1361 budget_av->budget.dvb_frontend = NULL;
1362 }
1363}
1364
1365
1366static void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
1367{
1368 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1369
1370 dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
1371
1372 if (*isr & MASK_10)
1373 ttpci_budget_irq10_handler(dev, isr);
1374}
1375
1376static int budget_av_detach(struct saa7146_dev *dev)
1377{
1378 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1379 int err;
1380
1381 dprintk(2, "dev: %p\n", dev);
1382
1383 if (1 == budget_av->has_saa7113) {
1384 saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
1385
1386 msleep(200);
1387
1388 saa7146_unregister_device(&budget_av->vd, dev);
1389
1390 saa7146_vv_release(dev);
1391 }
1392
1393 if (budget_av->budget.ci_present)
1394 ciintf_deinit(budget_av);
1395
1396 if (budget_av->budget.dvb_frontend != NULL) {
1397 dvb_unregister_frontend(budget_av->budget.dvb_frontend);
1398 dvb_frontend_detach(budget_av->budget.dvb_frontend);
1399 }
1400 err = ttpci_budget_deinit(&budget_av->budget);
1401
1402 kfree(budget_av);
1403
1404 return err;
1405}
1406
1407#define KNC1_INPUTS 2
1408static struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
1409 { 0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0,
1410 V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
1411 { 1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0,
1412 V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
1413};
1414
1415static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
1416{
1417 dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i->index);
1418 if (i->index >= KNC1_INPUTS)
1419 return -EINVAL;
1420 memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
1421 return 0;
1422}
1423
1424static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
1425{
1426 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
1427 struct budget_av *budget_av = (struct budget_av *)dev->ext_priv;
1428
1429 *i = budget_av->cur_input;
1430
1431 dprintk(1, "VIDIOC_G_INPUT %d.\n", *i);
1432 return 0;
1433}
1434
1435static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
1436{
1437 struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
1438 struct budget_av *budget_av = (struct budget_av *)dev->ext_priv;
1439
1440 dprintk(1, "VIDIOC_S_INPUT %d.\n", input);
1441 return saa7113_setinput(budget_av, input);
1442}
1443
1444static struct saa7146_ext_vv vv_data;
1445
1446static int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
1447{
1448 struct budget_av *budget_av;
1449 u8 *mac;
1450 int err;
1451
1452 dprintk(2, "dev: %p\n", dev);
1453
1454 if (!(budget_av = kzalloc(sizeof(struct budget_av), GFP_KERNEL)))
1455 return -ENOMEM;
1456
1457 budget_av->has_saa7113 = 0;
1458 budget_av->budget.ci_present = 0;
1459
1460 dev->ext_priv = budget_av;
1461
1462 err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE,
1463 adapter_nr);
1464 if (err) {
1465 kfree(budget_av);
1466 return err;
1467 }
1468
1469 /* knc1 initialization */
1470 saa7146_write(dev, DD1_STREAM_B, 0x04000000);
1471 saa7146_write(dev, DD1_INIT, 0x07000600);
1472 saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
1473
1474 if (saa7113_init(budget_av) == 0) {
1475 budget_av->has_saa7113 = 1;
1476
1477 if (0 != saa7146_vv_init(dev, &vv_data)) {
1478 /* fixme: proper cleanup here */
1479 ERR(("cannot init vv subsystem.\n"));
1480 return err;
1481 }
1482 vv_data.ops.vidioc_enum_input = vidioc_enum_input;
1483 vv_data.ops.vidioc_g_input = vidioc_g_input;
1484 vv_data.ops.vidioc_s_input = vidioc_s_input;
1485
1486 if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_GRABBER))) {
1487 /* fixme: proper cleanup here */
1488 ERR(("cannot register capture v4l2 device.\n"));
1489 saa7146_vv_release(dev);
1490 return err;
1491 }
1492
1493 /* beware: this modifies dev->vv ... */
1494 saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
1495 SAA7146_HPS_SYNC_PORT_A);
1496
1497 saa7113_setinput(budget_av, 0);
1498 }
1499
1500 /* fixme: find some sane values here... */
1501 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
1502
1503 mac = budget_av->budget.dvb_adapter.proposed_mac;
1504 if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
1505 printk(KERN_ERR "KNC1-%d: Could not read MAC from KNC1 card\n",
1506 budget_av->budget.dvb_adapter.num);
1507 memset(mac, 0, 6);
1508 } else {
1509 printk(KERN_INFO "KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
1510 budget_av->budget.dvb_adapter.num,
1511 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1512 }
1513
1514 budget_av->budget.dvb_adapter.priv = budget_av;
1515 frontend_init(budget_av);
1516 ciintf_init(budget_av);
1517
1518 ttpci_budget_init_hooks(&budget_av->budget);
1519
1520 return 0;
1521}
1522
1523static struct saa7146_standard standard[] = {
1524 {.name = "PAL",.id = V4L2_STD_PAL,
1525 .v_offset = 0x17,.v_field = 288,
1526 .h_offset = 0x14,.h_pixels = 680,
1527 .v_max_out = 576,.h_max_out = 768 },
1528
1529 {.name = "NTSC",.id = V4L2_STD_NTSC,
1530 .v_offset = 0x16,.v_field = 240,
1531 .h_offset = 0x06,.h_pixels = 708,
1532 .v_max_out = 480,.h_max_out = 640, },
1533};
1534
1535static struct saa7146_ext_vv vv_data = {
1536 .inputs = 2,
1537 .capabilities = 0, // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
1538 .flags = 0,
1539 .stds = &standard[0],
1540 .num_stds = ARRAY_SIZE(standard),
1541};
1542
1543static struct saa7146_extension budget_extension;
1544
1545MAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
1546MAKE_BUDGET_INFO(knc1s2,"KNC1 DVB-S2", BUDGET_KNC1S2);
1547MAKE_BUDGET_INFO(sates2,"Satelco EasyWatch DVB-S2", BUDGET_KNC1S2);
1548MAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
1549MAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
1550MAKE_BUDGET_INFO(kncxs, "KNC TV STAR DVB-S", BUDGET_TVSTAR);
1551MAKE_BUDGET_INFO(satewpls, "Satelco EasyWatch DVB-S light", BUDGET_TVSTAR);
1552MAKE_BUDGET_INFO(satewpls1, "Satelco EasyWatch DVB-S light", BUDGET_KNC1S);
1553MAKE_BUDGET_INFO(satewps, "Satelco EasyWatch DVB-S", BUDGET_KNC1S);
1554MAKE_BUDGET_INFO(satewplc, "Satelco EasyWatch DVB-C", BUDGET_KNC1CP);
1555MAKE_BUDGET_INFO(satewcmk3, "Satelco EasyWatch DVB-C MK3", BUDGET_KNC1C_MK3);
1556MAKE_BUDGET_INFO(satewt, "Satelco EasyWatch DVB-T", BUDGET_KNC1T);
1557MAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
1558MAKE_BUDGET_INFO(knc1spx4, "KNC1 DVB-S Plus X4", BUDGET_KNC1SP);
1559MAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
1560MAKE_BUDGET_INFO(knc1cmk3, "KNC1 DVB-C MK3", BUDGET_KNC1C_MK3);
1561MAKE_BUDGET_INFO(knc1cpmk3, "KNC1 DVB-C Plus MK3", BUDGET_KNC1CP_MK3);
1562MAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
1563MAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1564MAKE_BUDGET_INFO(cin1200sn, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1565MAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
1566MAKE_BUDGET_INFO(cin1200cmk3, "Terratec Cinergy 1200 DVB-C MK3", BUDGET_CIN1200C_MK3);
1567MAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
1568
1569static struct pci_device_id pci_tbl[] = {
1570 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
1571 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
1572 MAKE_EXTENSION_PCI(knc1s, 0x1894, 0x0010),
1573 MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
1574 MAKE_EXTENSION_PCI(knc1sp, 0x1894, 0x0011),
1575 MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0014),
1576 MAKE_EXTENSION_PCI(knc1spx4, 0x1894, 0x0015),
1577 MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0016),
1578 MAKE_EXTENSION_PCI(knc1s2, 0x1894, 0x0018),
1579 MAKE_EXTENSION_PCI(knc1s2, 0x1894, 0x0019),
1580 MAKE_EXTENSION_PCI(sates2, 0x1894, 0x001d),
1581 MAKE_EXTENSION_PCI(satewpls, 0x1894, 0x001e),
1582 MAKE_EXTENSION_PCI(satewpls1, 0x1894, 0x001a),
1583 MAKE_EXTENSION_PCI(satewps, 0x1894, 0x001b),
1584 MAKE_EXTENSION_PCI(satewplc, 0x1894, 0x002a),
1585 MAKE_EXTENSION_PCI(satewcmk3, 0x1894, 0x002c),
1586 MAKE_EXTENSION_PCI(satewt, 0x1894, 0x003a),
1587 MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
1588 MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
1589 MAKE_EXTENSION_PCI(knc1cmk3, 0x1894, 0x0022),
1590 MAKE_EXTENSION_PCI(knc1cpmk3, 0x1894, 0x0023),
1591 MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
1592 MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
1593 MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
1594 MAKE_EXTENSION_PCI(cin1200sn, 0x153b, 0x1155),
1595 MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
1596 MAKE_EXTENSION_PCI(cin1200cmk3, 0x153b, 0x1176),
1597 MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
1598 {
1599 .vendor = 0,
1600 }
1601};
1602
1603MODULE_DEVICE_TABLE(pci, pci_tbl);
1604
1605static struct saa7146_extension budget_extension = {
1606 .name = "budget_av",
1607 .flags = SAA7146_USE_I2C_IRQ,
1608
1609 .pci_tbl = pci_tbl,
1610
1611 .module = THIS_MODULE,
1612 .attach = budget_av_attach,
1613 .detach = budget_av_detach,
1614
1615 .irq_mask = MASK_10,
1616 .irq_func = budget_av_irq,
1617};
1618
1619static int __init budget_av_init(void)
1620{
1621 return saa7146_register_extension(&budget_extension);
1622}
1623
1624static void __exit budget_av_exit(void)
1625{
1626 saa7146_unregister_extension(&budget_extension);
1627}
1628
1629module_init(budget_av_init);
1630module_exit(budget_av_exit);
1631
1632MODULE_LICENSE("GPL");
1633MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
1634MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1635 "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");
diff --git a/drivers/media/dvb/ttpci/budget-ci.c b/drivers/media/dvb/ttpci/budget-ci.c
new file mode 100644
index 00000000000..926f299b522
--- /dev/null
+++ b/drivers/media/dvb/ttpci/budget-ci.c
@@ -0,0 +1,1589 @@
1/*
2 * budget-ci.c: driver for the SAA7146 based Budget DVB cards
3 *
4 * Compiled from various sources by Michael Hunold <michael@mihu.de>
5 *
6 * msp430 IR support contributed by Jack Thomasson <jkt@Helius.COM>
7 * partially based on the Siemens DVB driver by Ralph+Marcus Metzler
8 *
9 * CI interface support (c) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
10 *
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.
15 *
16 *
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.
21 *
22 *
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
27 *
28 *
29 * the project's page is at http://www.linuxtv.org/
30 */
31
32#include <linux/module.h>
33#include <linux/errno.h>
34#include <linux/slab.h>
35#include <linux/interrupt.h>
36#include <linux/spinlock.h>
37#include <media/rc-core.h>
38
39#include "budget.h"
40
41#include "dvb_ca_en50221.h"
42#include "stv0299.h"
43#include "stv0297.h"
44#include "tda1004x.h"
45#include "stb0899_drv.h"
46#include "stb0899_reg.h"
47#include "stb0899_cfg.h"
48#include "stb6100.h"
49#include "stb6100_cfg.h"
50#include "lnbp21.h"
51#include "bsbe1.h"
52#include "bsru6.h"
53#include "tda1002x.h"
54#include "tda827x.h"
55#include "bsbe1-d01a.h"
56
57#define MODULE_NAME "budget_ci"
58
59/*
60 * Regarding DEBIADDR_IR:
61 * Some CI modules hang if random addresses are read.
62 * Using address 0x4000 for the IR read means that we
63 * use the same address as for CI version, which should
64 * be a safe default.
65 */
66#define DEBIADDR_IR 0x4000
67#define DEBIADDR_CICONTROL 0x0000
68#define DEBIADDR_CIVERSION 0x4000
69#define DEBIADDR_IO 0x1000
70#define DEBIADDR_ATTR 0x3000
71
72#define CICONTROL_RESET 0x01
73#define CICONTROL_ENABLETS 0x02
74#define CICONTROL_CAMDETECT 0x08
75
76#define DEBICICTL 0x00420000
77#define DEBICICAM 0x02420000
78
79#define SLOTSTATUS_NONE 1
80#define SLOTSTATUS_PRESENT 2
81#define SLOTSTATUS_RESET 4
82#define SLOTSTATUS_READY 8
83#define SLOTSTATUS_OCCUPIED (SLOTSTATUS_PRESENT|SLOTSTATUS_RESET|SLOTSTATUS_READY)
84
85/* RC5 device wildcard */
86#define IR_DEVICE_ANY 255
87
88static int rc5_device = -1;
89module_param(rc5_device, int, 0644);
90MODULE_PARM_DESC(rc5_device, "only IR commands to given RC5 device (device = 0 - 31, any device = 255, default: autodetect)");
91
92static int ir_debug;
93module_param(ir_debug, int, 0644);
94MODULE_PARM_DESC(ir_debug, "enable debugging information for IR decoding");
95
96DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
97
98struct budget_ci_ir {
99 struct rc_dev *dev;
100 struct tasklet_struct msp430_irq_tasklet;
101 char name[72]; /* 40 + 32 for (struct saa7146_dev).name */
102 char phys[32];
103 int rc5_device;
104 u32 ir_key;
105 bool have_command;
106 bool full_rc5; /* Outputs a full RC5 code */
107};
108
109struct budget_ci {
110 struct budget budget;
111 struct tasklet_struct ciintf_irq_tasklet;
112 int slot_status;
113 int ci_irq;
114 struct dvb_ca_en50221 ca;
115 struct budget_ci_ir ir;
116 u8 tuner_pll_address; /* used for philips_tdm1316l configs */
117};
118
119static void msp430_ir_interrupt(unsigned long data)
120{
121 struct budget_ci *budget_ci = (struct budget_ci *) data;
122 struct rc_dev *dev = budget_ci->ir.dev;
123 u32 command = ttpci_budget_debiread(&budget_ci->budget, DEBINOSWAP, DEBIADDR_IR, 2, 1, 0) >> 8;
124
125 /*
126 * The msp430 chip can generate two different bytes, command and device
127 *
128 * type1: X1CCCCCC, C = command bits (0 - 63)
129 * type2: X0TDDDDD, D = device bits (0 - 31), T = RC5 toggle bit
130 *
131 * Each signal from the remote control can generate one or more command
132 * bytes and one or more device bytes. For the repeated bytes, the
133 * highest bit (X) is set. The first command byte is always generated
134 * before the first device byte. Other than that, no specific order
135 * seems to apply. To make life interesting, bytes can also be lost.
136 *
137 * Only when we have a command and device byte, a keypress is
138 * generated.
139 */
140
141 if (ir_debug)
142 printk("budget_ci: received byte 0x%02x\n", command);
143
144 /* Remove repeat bit, we use every command */
145 command = command & 0x7f;
146
147 /* Is this a RC5 command byte? */
148 if (command & 0x40) {
149 budget_ci->ir.have_command = true;
150 budget_ci->ir.ir_key = command & 0x3f;
151 return;
152 }
153
154 /* It's a RC5 device byte */
155 if (!budget_ci->ir.have_command)
156 return;
157 budget_ci->ir.have_command = false;
158
159 if (budget_ci->ir.rc5_device != IR_DEVICE_ANY &&
160 budget_ci->ir.rc5_device != (command & 0x1f))
161 return;
162
163 if (budget_ci->ir.full_rc5) {
164 rc_keydown(dev,
165 budget_ci->ir.rc5_device <<8 | budget_ci->ir.ir_key,
166 (command & 0x20) ? 1 : 0);
167 return;
168 }
169
170 /* FIXME: We should generate complete scancodes for all devices */
171 rc_keydown(dev, budget_ci->ir.ir_key, (command & 0x20) ? 1 : 0);
172}
173
174static int msp430_ir_init(struct budget_ci *budget_ci)
175{
176 struct saa7146_dev *saa = budget_ci->budget.dev;
177 struct rc_dev *dev;
178 int error;
179
180 dev = rc_allocate_device();
181 if (!dev) {
182 printk(KERN_ERR "budget_ci: IR interface initialisation failed\n");
183 return -ENOMEM;
184 }
185
186 snprintf(budget_ci->ir.name, sizeof(budget_ci->ir.name),
187 "Budget-CI dvb ir receiver %s", saa->name);
188 snprintf(budget_ci->ir.phys, sizeof(budget_ci->ir.phys),
189 "pci-%s/ir0", pci_name(saa->pci));
190
191 dev->driver_name = MODULE_NAME;
192 dev->input_name = budget_ci->ir.name;
193 dev->input_phys = budget_ci->ir.phys;
194 dev->input_id.bustype = BUS_PCI;
195 dev->input_id.version = 1;
196 dev->scanmask = 0xff;
197 if (saa->pci->subsystem_vendor) {
198 dev->input_id.vendor = saa->pci->subsystem_vendor;
199 dev->input_id.product = saa->pci->subsystem_device;
200 } else {
201 dev->input_id.vendor = saa->pci->vendor;
202 dev->input_id.product = saa->pci->device;
203 }
204 dev->dev.parent = &saa->pci->dev;
205
206 if (rc5_device < 0)
207 budget_ci->ir.rc5_device = IR_DEVICE_ANY;
208 else
209 budget_ci->ir.rc5_device = rc5_device;
210
211 /* Select keymap and address */
212 switch (budget_ci->budget.dev->pci->subsystem_device) {
213 case 0x100c:
214 case 0x100f:
215 case 0x1011:
216 case 0x1012:
217 /* The hauppauge keymap is a superset of these remotes */
218 dev->map_name = RC_MAP_HAUPPAUGE;
219 budget_ci->ir.full_rc5 = true;
220
221 if (rc5_device < 0)
222 budget_ci->ir.rc5_device = 0x1f;
223 break;
224 case 0x1010:
225 case 0x1017:
226 case 0x1019:
227 case 0x101a:
228 case 0x101b:
229 /* for the Technotrend 1500 bundled remote */
230 dev->map_name = RC_MAP_TT_1500;
231 break;
232 default:
233 /* unknown remote */
234 dev->map_name = RC_MAP_BUDGET_CI_OLD;
235 break;
236 }
237
238 error = rc_register_device(dev);
239 if (error) {
240 printk(KERN_ERR "budget_ci: could not init driver for IR device (code %d)\n", error);
241 rc_free_device(dev);
242 return error;
243 }
244
245 budget_ci->ir.dev = dev;
246
247 tasklet_init(&budget_ci->ir.msp430_irq_tasklet, msp430_ir_interrupt,
248 (unsigned long) budget_ci);
249
250 SAA7146_IER_ENABLE(saa, MASK_06);
251 saa7146_setgpio(saa, 3, SAA7146_GPIO_IRQHI);
252
253 return 0;
254}
255
256static void msp430_ir_deinit(struct budget_ci *budget_ci)
257{
258 struct saa7146_dev *saa = budget_ci->budget.dev;
259
260 SAA7146_IER_DISABLE(saa, MASK_06);
261 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
262 tasklet_kill(&budget_ci->ir.msp430_irq_tasklet);
263
264 rc_unregister_device(budget_ci->ir.dev);
265}
266
267static int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address)
268{
269 struct budget_ci *budget_ci = (struct budget_ci *) ca->data;
270
271 if (slot != 0)
272 return -EINVAL;
273
274 return ttpci_budget_debiread(&budget_ci->budget, DEBICICAM,
275 DEBIADDR_ATTR | (address & 0xfff), 1, 1, 0);
276}
277
278static int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address, u8 value)
279{
280 struct budget_ci *budget_ci = (struct budget_ci *) ca->data;
281
282 if (slot != 0)
283 return -EINVAL;
284
285 return ttpci_budget_debiwrite(&budget_ci->budget, DEBICICAM,
286 DEBIADDR_ATTR | (address & 0xfff), 1, value, 1, 0);
287}
288
289static int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address)
290{
291 struct budget_ci *budget_ci = (struct budget_ci *) ca->data;
292
293 if (slot != 0)
294 return -EINVAL;
295
296 return ttpci_budget_debiread(&budget_ci->budget, DEBICICAM,
297 DEBIADDR_IO | (address & 3), 1, 1, 0);
298}
299
300static int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address, u8 value)
301{
302 struct budget_ci *budget_ci = (struct budget_ci *) ca->data;
303
304 if (slot != 0)
305 return -EINVAL;
306
307 return ttpci_budget_debiwrite(&budget_ci->budget, DEBICICAM,
308 DEBIADDR_IO | (address & 3), 1, value, 1, 0);
309}
310
311static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
312{
313 struct budget_ci *budget_ci = (struct budget_ci *) ca->data;
314 struct saa7146_dev *saa = budget_ci->budget.dev;
315
316 if (slot != 0)
317 return -EINVAL;
318
319 if (budget_ci->ci_irq) {
320 // trigger on RISING edge during reset so we know when READY is re-asserted
321 saa7146_setgpio(saa, 0, SAA7146_GPIO_IRQHI);
322 }
323 budget_ci->slot_status = SLOTSTATUS_RESET;
324 ttpci_budget_debiwrite(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1, 0, 1, 0);
325 msleep(1);
326 ttpci_budget_debiwrite(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1,
327 CICONTROL_RESET, 1, 0);
328
329 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTHI);
330 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
331 return 0;
332}
333
334static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
335{
336 struct budget_ci *budget_ci = (struct budget_ci *) ca->data;
337 struct saa7146_dev *saa = budget_ci->budget.dev;
338
339 if (slot != 0)
340 return -EINVAL;
341
342 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTHI);
343 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
344 return 0;
345}
346
347static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
348{
349 struct budget_ci *budget_ci = (struct budget_ci *) ca->data;
350 struct saa7146_dev *saa = budget_ci->budget.dev;
351 int tmp;
352
353 if (slot != 0)
354 return -EINVAL;
355
356 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
357
358 tmp = ttpci_budget_debiread(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1, 1, 0);
359 ttpci_budget_debiwrite(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1,
360 tmp | CICONTROL_ENABLETS, 1, 0);
361
362 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
363 return 0;
364}
365
366static void ciintf_interrupt(unsigned long data)
367{
368 struct budget_ci *budget_ci = (struct budget_ci *) data;
369 struct saa7146_dev *saa = budget_ci->budget.dev;
370 unsigned int flags;
371
372 // ensure we don't get spurious IRQs during initialisation
373 if (!budget_ci->budget.ci_present)
374 return;
375
376 // read the CAM status
377 flags = ttpci_budget_debiread(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1, 1, 0);
378 if (flags & CICONTROL_CAMDETECT) {
379
380 // GPIO should be set to trigger on falling edge if a CAM is present
381 saa7146_setgpio(saa, 0, SAA7146_GPIO_IRQLO);
382
383 if (budget_ci->slot_status & SLOTSTATUS_NONE) {
384 // CAM insertion IRQ
385 budget_ci->slot_status = SLOTSTATUS_PRESENT;
386 dvb_ca_en50221_camchange_irq(&budget_ci->ca, 0,
387 DVB_CA_EN50221_CAMCHANGE_INSERTED);
388
389 } else if (budget_ci->slot_status & SLOTSTATUS_RESET) {
390 // CAM ready (reset completed)
391 budget_ci->slot_status = SLOTSTATUS_READY;
392 dvb_ca_en50221_camready_irq(&budget_ci->ca, 0);
393
394 } else if (budget_ci->slot_status & SLOTSTATUS_READY) {
395 // FR/DA IRQ
396 dvb_ca_en50221_frda_irq(&budget_ci->ca, 0);
397 }
398 } else {
399
400 // trigger on rising edge if a CAM is not present - when a CAM is inserted, we
401 // only want to get the IRQ when it sets READY. If we trigger on the falling edge,
402 // the CAM might not actually be ready yet.
403 saa7146_setgpio(saa, 0, SAA7146_GPIO_IRQHI);
404
405 // generate a CAM removal IRQ if we haven't already
406 if (budget_ci->slot_status & SLOTSTATUS_OCCUPIED) {
407 // CAM removal IRQ
408 budget_ci->slot_status = SLOTSTATUS_NONE;
409 dvb_ca_en50221_camchange_irq(&budget_ci->ca, 0,
410 DVB_CA_EN50221_CAMCHANGE_REMOVED);
411 }
412 }
413}
414
415static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
416{
417 struct budget_ci *budget_ci = (struct budget_ci *) ca->data;
418 unsigned int flags;
419
420 // ensure we don't get spurious IRQs during initialisation
421 if (!budget_ci->budget.ci_present)
422 return -EINVAL;
423
424 // read the CAM status
425 flags = ttpci_budget_debiread(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1, 1, 0);
426 if (flags & CICONTROL_CAMDETECT) {
427 // mark it as present if it wasn't before
428 if (budget_ci->slot_status & SLOTSTATUS_NONE) {
429 budget_ci->slot_status = SLOTSTATUS_PRESENT;
430 }
431
432 // during a RESET, we check if we can read from IO memory to see when CAM is ready
433 if (budget_ci->slot_status & SLOTSTATUS_RESET) {
434 if (ciintf_read_attribute_mem(ca, slot, 0) == 0x1d) {
435 budget_ci->slot_status = SLOTSTATUS_READY;
436 }
437 }
438 } else {
439 budget_ci->slot_status = SLOTSTATUS_NONE;
440 }
441
442 if (budget_ci->slot_status != SLOTSTATUS_NONE) {
443 if (budget_ci->slot_status & SLOTSTATUS_READY) {
444 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
445 }
446 return DVB_CA_EN50221_POLL_CAM_PRESENT;
447 }
448
449 return 0;
450}
451
452static int ciintf_init(struct budget_ci *budget_ci)
453{
454 struct saa7146_dev *saa = budget_ci->budget.dev;
455 int flags;
456 int result;
457 int ci_version;
458 int ca_flags;
459
460 memset(&budget_ci->ca, 0, sizeof(struct dvb_ca_en50221));
461
462 // enable DEBI pins
463 saa7146_write(saa, MC1, MASK_27 | MASK_11);
464
465 // test if it is there
466 ci_version = ttpci_budget_debiread(&budget_ci->budget, DEBICICTL, DEBIADDR_CIVERSION, 1, 1, 0);
467 if ((ci_version & 0xa0) != 0xa0) {
468 result = -ENODEV;
469 goto error;
470 }
471
472 // determine whether a CAM is present or not
473 flags = ttpci_budget_debiread(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1, 1, 0);
474 budget_ci->slot_status = SLOTSTATUS_NONE;
475 if (flags & CICONTROL_CAMDETECT)
476 budget_ci->slot_status = SLOTSTATUS_PRESENT;
477
478 // version 0xa2 of the CI firmware doesn't generate interrupts
479 if (ci_version == 0xa2) {
480 ca_flags = 0;
481 budget_ci->ci_irq = 0;
482 } else {
483 ca_flags = DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE |
484 DVB_CA_EN50221_FLAG_IRQ_FR |
485 DVB_CA_EN50221_FLAG_IRQ_DA;
486 budget_ci->ci_irq = 1;
487 }
488
489 // register CI interface
490 budget_ci->ca.owner = THIS_MODULE;
491 budget_ci->ca.read_attribute_mem = ciintf_read_attribute_mem;
492 budget_ci->ca.write_attribute_mem = ciintf_write_attribute_mem;
493 budget_ci->ca.read_cam_control = ciintf_read_cam_control;
494 budget_ci->ca.write_cam_control = ciintf_write_cam_control;
495 budget_ci->ca.slot_reset = ciintf_slot_reset;
496 budget_ci->ca.slot_shutdown = ciintf_slot_shutdown;
497 budget_ci->ca.slot_ts_enable = ciintf_slot_ts_enable;
498 budget_ci->ca.poll_slot_status = ciintf_poll_slot_status;
499 budget_ci->ca.data = budget_ci;
500 if ((result = dvb_ca_en50221_init(&budget_ci->budget.dvb_adapter,
501 &budget_ci->ca,
502 ca_flags, 1)) != 0) {
503 printk("budget_ci: CI interface detected, but initialisation failed.\n");
504 goto error;
505 }
506
507 // Setup CI slot IRQ
508 if (budget_ci->ci_irq) {
509 tasklet_init(&budget_ci->ciintf_irq_tasklet, ciintf_interrupt, (unsigned long) budget_ci);
510 if (budget_ci->slot_status != SLOTSTATUS_NONE) {
511 saa7146_setgpio(saa, 0, SAA7146_GPIO_IRQLO);
512 } else {
513 saa7146_setgpio(saa, 0, SAA7146_GPIO_IRQHI);
514 }
515 SAA7146_IER_ENABLE(saa, MASK_03);
516 }
517
518 // enable interface
519 ttpci_budget_debiwrite(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1,
520 CICONTROL_RESET, 1, 0);
521
522 // success!
523 printk("budget_ci: CI interface initialised\n");
524 budget_ci->budget.ci_present = 1;
525
526 // forge a fake CI IRQ so the CAM state is setup correctly
527 if (budget_ci->ci_irq) {
528 flags = DVB_CA_EN50221_CAMCHANGE_REMOVED;
529 if (budget_ci->slot_status != SLOTSTATUS_NONE)
530 flags = DVB_CA_EN50221_CAMCHANGE_INSERTED;
531 dvb_ca_en50221_camchange_irq(&budget_ci->ca, 0, flags);
532 }
533
534 return 0;
535
536error:
537 saa7146_write(saa, MC1, MASK_27);
538 return result;
539}
540
541static void ciintf_deinit(struct budget_ci *budget_ci)
542{
543 struct saa7146_dev *saa = budget_ci->budget.dev;
544
545 // disable CI interrupts
546 if (budget_ci->ci_irq) {
547 SAA7146_IER_DISABLE(saa, MASK_03);
548 saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
549 tasklet_kill(&budget_ci->ciintf_irq_tasklet);
550 }
551
552 // reset interface
553 ttpci_budget_debiwrite(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1, 0, 1, 0);
554 msleep(1);
555 ttpci_budget_debiwrite(&budget_ci->budget, DEBICICTL, DEBIADDR_CICONTROL, 1,
556 CICONTROL_RESET, 1, 0);
557
558 // disable TS data stream to CI interface
559 saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
560
561 // release the CA device
562 dvb_ca_en50221_release(&budget_ci->ca);
563
564 // disable DEBI pins
565 saa7146_write(saa, MC1, MASK_27);
566}
567
568static void budget_ci_irq(struct saa7146_dev *dev, u32 * isr)
569{
570 struct budget_ci *budget_ci = (struct budget_ci *) dev->ext_priv;
571
572 dprintk(8, "dev: %p, budget_ci: %p\n", dev, budget_ci);
573
574 if (*isr & MASK_06)
575 tasklet_schedule(&budget_ci->ir.msp430_irq_tasklet);
576
577 if (*isr & MASK_10)
578 ttpci_budget_irq10_handler(dev, isr);
579
580 if ((*isr & MASK_03) && (budget_ci->budget.ci_present) && (budget_ci->ci_irq))
581 tasklet_schedule(&budget_ci->ciintf_irq_tasklet);
582}
583
584static u8 philips_su1278_tt_inittab[] = {
585 0x01, 0x0f,
586 0x02, 0x30,
587 0x03, 0x00,
588 0x04, 0x5b,
589 0x05, 0x85,
590 0x06, 0x02,
591 0x07, 0x00,
592 0x08, 0x02,
593 0x09, 0x00,
594 0x0C, 0x01,
595 0x0D, 0x81,
596 0x0E, 0x44,
597 0x0f, 0x14,
598 0x10, 0x3c,
599 0x11, 0x84,
600 0x12, 0xda,
601 0x13, 0x97,
602 0x14, 0x95,
603 0x15, 0xc9,
604 0x16, 0x19,
605 0x17, 0x8c,
606 0x18, 0x59,
607 0x19, 0xf8,
608 0x1a, 0xfe,
609 0x1c, 0x7f,
610 0x1d, 0x00,
611 0x1e, 0x00,
612 0x1f, 0x50,
613 0x20, 0x00,
614 0x21, 0x00,
615 0x22, 0x00,
616 0x23, 0x00,
617 0x28, 0x00,
618 0x29, 0x28,
619 0x2a, 0x14,
620 0x2b, 0x0f,
621 0x2c, 0x09,
622 0x2d, 0x09,
623 0x31, 0x1f,
624 0x32, 0x19,
625 0x33, 0xfc,
626 0x34, 0x93,
627 0xff, 0xff
628};
629
630static int philips_su1278_tt_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
631{
632 stv0299_writereg(fe, 0x0e, 0x44);
633 if (srate >= 10000000) {
634 stv0299_writereg(fe, 0x13, 0x97);
635 stv0299_writereg(fe, 0x14, 0x95);
636 stv0299_writereg(fe, 0x15, 0xc9);
637 stv0299_writereg(fe, 0x17, 0x8c);
638 stv0299_writereg(fe, 0x1a, 0xfe);
639 stv0299_writereg(fe, 0x1c, 0x7f);
640 stv0299_writereg(fe, 0x2d, 0x09);
641 } else {
642 stv0299_writereg(fe, 0x13, 0x99);
643 stv0299_writereg(fe, 0x14, 0x8d);
644 stv0299_writereg(fe, 0x15, 0xce);
645 stv0299_writereg(fe, 0x17, 0x43);
646 stv0299_writereg(fe, 0x1a, 0x1d);
647 stv0299_writereg(fe, 0x1c, 0x12);
648 stv0299_writereg(fe, 0x2d, 0x05);
649 }
650 stv0299_writereg(fe, 0x0e, 0x23);
651 stv0299_writereg(fe, 0x0f, 0x94);
652 stv0299_writereg(fe, 0x10, 0x39);
653 stv0299_writereg(fe, 0x15, 0xc9);
654
655 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
656 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
657 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
658
659 return 0;
660}
661
662static int philips_su1278_tt_tuner_set_params(struct dvb_frontend *fe,
663 struct dvb_frontend_parameters *params)
664{
665 struct budget_ci *budget_ci = (struct budget_ci *) fe->dvb->priv;
666 u32 div;
667 u8 buf[4];
668 struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
669
670 if ((params->frequency < 950000) || (params->frequency > 2150000))
671 return -EINVAL;
672
673 div = (params->frequency + (500 - 1)) / 500; // round correctly
674 buf[0] = (div >> 8) & 0x7f;
675 buf[1] = div & 0xff;
676 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 2;
677 buf[3] = 0x20;
678
679 if (params->u.qpsk.symbol_rate < 4000000)
680 buf[3] |= 1;
681
682 if (params->frequency < 1250000)
683 buf[3] |= 0;
684 else if (params->frequency < 1550000)
685 buf[3] |= 0x40;
686 else if (params->frequency < 2050000)
687 buf[3] |= 0x80;
688 else if (params->frequency < 2150000)
689 buf[3] |= 0xC0;
690
691 if (fe->ops.i2c_gate_ctrl)
692 fe->ops.i2c_gate_ctrl(fe, 1);
693 if (i2c_transfer(&budget_ci->budget.i2c_adap, &msg, 1) != 1)
694 return -EIO;
695 return 0;
696}
697
698static struct stv0299_config philips_su1278_tt_config = {
699
700 .demod_address = 0x68,
701 .inittab = philips_su1278_tt_inittab,
702 .mclk = 64000000UL,
703 .invert = 0,
704 .skip_reinit = 1,
705 .lock_output = STV0299_LOCKOUTPUT_1,
706 .volt13_op0_op1 = STV0299_VOLT13_OP1,
707 .min_delay_ms = 50,
708 .set_symbol_rate = philips_su1278_tt_set_symbol_rate,
709};
710
711
712
713static int philips_tdm1316l_tuner_init(struct dvb_frontend *fe)
714{
715 struct budget_ci *budget_ci = (struct budget_ci *) fe->dvb->priv;
716 static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
717 static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
718 struct i2c_msg tuner_msg = {.addr = budget_ci->tuner_pll_address,.flags = 0,.buf = td1316_init,.len =
719 sizeof(td1316_init) };
720
721 // setup PLL configuration
722 if (fe->ops.i2c_gate_ctrl)
723 fe->ops.i2c_gate_ctrl(fe, 1);
724 if (i2c_transfer(&budget_ci->budget.i2c_adap, &tuner_msg, 1) != 1)
725 return -EIO;
726 msleep(1);
727
728 // disable the mc44BC374c (do not check for errors)
729 tuner_msg.addr = 0x65;
730 tuner_msg.buf = disable_mc44BC374c;
731 tuner_msg.len = sizeof(disable_mc44BC374c);
732 if (fe->ops.i2c_gate_ctrl)
733 fe->ops.i2c_gate_ctrl(fe, 1);
734 if (i2c_transfer(&budget_ci->budget.i2c_adap, &tuner_msg, 1) != 1) {
735 if (fe->ops.i2c_gate_ctrl)
736 fe->ops.i2c_gate_ctrl(fe, 1);
737 i2c_transfer(&budget_ci->budget.i2c_adap, &tuner_msg, 1);
738 }
739
740 return 0;
741}
742
743static int philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
744{
745 struct budget_ci *budget_ci = (struct budget_ci *) fe->dvb->priv;
746 u8 tuner_buf[4];
747 struct i2c_msg tuner_msg = {.addr = budget_ci->tuner_pll_address,.flags = 0,.buf = tuner_buf,.len = sizeof(tuner_buf) };
748 int tuner_frequency = 0;
749 u8 band, cp, filter;
750
751 // determine charge pump
752 tuner_frequency = params->frequency + 36130000;
753 if (tuner_frequency < 87000000)
754 return -EINVAL;
755 else if (tuner_frequency < 130000000)
756 cp = 3;
757 else if (tuner_frequency < 160000000)
758 cp = 5;
759 else if (tuner_frequency < 200000000)
760 cp = 6;
761 else if (tuner_frequency < 290000000)
762 cp = 3;
763 else if (tuner_frequency < 420000000)
764 cp = 5;
765 else if (tuner_frequency < 480000000)
766 cp = 6;
767 else if (tuner_frequency < 620000000)
768 cp = 3;
769 else if (tuner_frequency < 830000000)
770 cp = 5;
771 else if (tuner_frequency < 895000000)
772 cp = 7;
773 else
774 return -EINVAL;
775
776 // determine band
777 if (params->frequency < 49000000)
778 return -EINVAL;
779 else if (params->frequency < 159000000)
780 band = 1;
781 else if (params->frequency < 444000000)
782 band = 2;
783 else if (params->frequency < 861000000)
784 band = 4;
785 else
786 return -EINVAL;
787
788 // setup PLL filter and TDA9889
789 switch (params->u.ofdm.bandwidth) {
790 case BANDWIDTH_6_MHZ:
791 tda1004x_writereg(fe, 0x0C, 0x14);
792 filter = 0;
793 break;
794
795 case BANDWIDTH_7_MHZ:
796 tda1004x_writereg(fe, 0x0C, 0x80);
797 filter = 0;
798 break;
799
800 case BANDWIDTH_8_MHZ:
801 tda1004x_writereg(fe, 0x0C, 0x14);
802 filter = 1;
803 break;
804
805 default:
806 return -EINVAL;
807 }
808
809 // calculate divisor
810 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
811 tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
812
813 // setup tuner buffer
814 tuner_buf[0] = tuner_frequency >> 8;
815 tuner_buf[1] = tuner_frequency & 0xff;
816 tuner_buf[2] = 0xca;
817 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
818
819 if (fe->ops.i2c_gate_ctrl)
820 fe->ops.i2c_gate_ctrl(fe, 1);
821 if (i2c_transfer(&budget_ci->budget.i2c_adap, &tuner_msg, 1) != 1)
822 return -EIO;
823
824 msleep(1);
825 return 0;
826}
827
828static int philips_tdm1316l_request_firmware(struct dvb_frontend *fe,
829 const struct firmware **fw, char *name)
830{
831 struct budget_ci *budget_ci = (struct budget_ci *) fe->dvb->priv;
832
833 return request_firmware(fw, name, &budget_ci->budget.dev->pci->dev);
834}
835
836static struct tda1004x_config philips_tdm1316l_config = {
837
838 .demod_address = 0x8,
839 .invert = 0,
840 .invert_oclk = 0,
841 .xtal_freq = TDA10046_XTAL_4M,
842 .agc_config = TDA10046_AGC_DEFAULT,
843 .if_freq = TDA10046_FREQ_3617,
844 .request_firmware = philips_tdm1316l_request_firmware,
845};
846
847static struct tda1004x_config philips_tdm1316l_config_invert = {
848
849 .demod_address = 0x8,
850 .invert = 1,
851 .invert_oclk = 0,
852 .xtal_freq = TDA10046_XTAL_4M,
853 .agc_config = TDA10046_AGC_DEFAULT,
854 .if_freq = TDA10046_FREQ_3617,
855 .request_firmware = philips_tdm1316l_request_firmware,
856};
857
858static int dvbc_philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
859{
860 struct budget_ci *budget_ci = (struct budget_ci *) fe->dvb->priv;
861 u8 tuner_buf[5];
862 struct i2c_msg tuner_msg = {.addr = budget_ci->tuner_pll_address,
863 .flags = 0,
864 .buf = tuner_buf,
865 .len = sizeof(tuner_buf) };
866 int tuner_frequency = 0;
867 u8 band, cp, filter;
868
869 // determine charge pump
870 tuner_frequency = params->frequency + 36125000;
871 if (tuner_frequency < 87000000)
872 return -EINVAL;
873 else if (tuner_frequency < 130000000) {
874 cp = 3;
875 band = 1;
876 } else if (tuner_frequency < 160000000) {
877 cp = 5;
878 band = 1;
879 } else if (tuner_frequency < 200000000) {
880 cp = 6;
881 band = 1;
882 } else if (tuner_frequency < 290000000) {
883 cp = 3;
884 band = 2;
885 } else if (tuner_frequency < 420000000) {
886 cp = 5;
887 band = 2;
888 } else if (tuner_frequency < 480000000) {
889 cp = 6;
890 band = 2;
891 } else if (tuner_frequency < 620000000) {
892 cp = 3;
893 band = 4;
894 } else if (tuner_frequency < 830000000) {
895 cp = 5;
896 band = 4;
897 } else if (tuner_frequency < 895000000) {
898 cp = 7;
899 band = 4;
900 } else
901 return -EINVAL;
902
903 // assume PLL filter should always be 8MHz for the moment.
904 filter = 1;
905
906 // calculate divisor
907 tuner_frequency = (params->frequency + 36125000 + (62500/2)) / 62500;
908
909 // setup tuner buffer
910 tuner_buf[0] = tuner_frequency >> 8;
911 tuner_buf[1] = tuner_frequency & 0xff;
912 tuner_buf[2] = 0xc8;
913 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
914 tuner_buf[4] = 0x80;
915
916 if (fe->ops.i2c_gate_ctrl)
917 fe->ops.i2c_gate_ctrl(fe, 1);
918 if (i2c_transfer(&budget_ci->budget.i2c_adap, &tuner_msg, 1) != 1)
919 return -EIO;
920
921 msleep(50);
922
923 if (fe->ops.i2c_gate_ctrl)
924 fe->ops.i2c_gate_ctrl(fe, 1);
925 if (i2c_transfer(&budget_ci->budget.i2c_adap, &tuner_msg, 1) != 1)
926 return -EIO;
927
928 msleep(1);
929
930 return 0;
931}
932
933static u8 dvbc_philips_tdm1316l_inittab[] = {
934 0x80, 0x01,
935 0x80, 0x00,
936 0x81, 0x01,
937 0x81, 0x00,
938 0x00, 0x09,
939 0x01, 0x69,
940 0x03, 0x00,
941 0x04, 0x00,
942 0x07, 0x00,
943 0x08, 0x00,
944 0x20, 0x00,
945 0x21, 0x40,
946 0x22, 0x00,
947 0x23, 0x00,
948 0x24, 0x40,
949 0x25, 0x88,
950 0x30, 0xff,
951 0x31, 0x00,
952 0x32, 0xff,
953 0x33, 0x00,
954 0x34, 0x50,
955 0x35, 0x7f,
956 0x36, 0x00,
957 0x37, 0x20,
958 0x38, 0x00,
959 0x40, 0x1c,
960 0x41, 0xff,
961 0x42, 0x29,
962 0x43, 0x20,
963 0x44, 0xff,
964 0x45, 0x00,
965 0x46, 0x00,
966 0x49, 0x04,
967 0x4a, 0x00,
968 0x4b, 0x7b,
969 0x52, 0x30,
970 0x55, 0xae,
971 0x56, 0x47,
972 0x57, 0xe1,
973 0x58, 0x3a,
974 0x5a, 0x1e,
975 0x5b, 0x34,
976 0x60, 0x00,
977 0x63, 0x00,
978 0x64, 0x00,
979 0x65, 0x00,
980 0x66, 0x00,
981 0x67, 0x00,
982 0x68, 0x00,
983 0x69, 0x00,
984 0x6a, 0x02,
985 0x6b, 0x00,
986 0x70, 0xff,
987 0x71, 0x00,
988 0x72, 0x00,
989 0x73, 0x00,
990 0x74, 0x0c,
991 0x80, 0x00,
992 0x81, 0x00,
993 0x82, 0x00,
994 0x83, 0x00,
995 0x84, 0x04,
996 0x85, 0x80,
997 0x86, 0x24,
998 0x87, 0x78,
999 0x88, 0x10,
1000 0x89, 0x00,
1001 0x90, 0x01,
1002 0x91, 0x01,
1003 0xa0, 0x04,
1004 0xa1, 0x00,
1005 0xa2, 0x00,
1006 0xb0, 0x91,
1007 0xb1, 0x0b,
1008 0xc0, 0x53,
1009 0xc1, 0x70,
1010 0xc2, 0x12,
1011 0xd0, 0x00,
1012 0xd1, 0x00,
1013 0xd2, 0x00,
1014 0xd3, 0x00,
1015 0xd4, 0x00,
1016 0xd5, 0x00,
1017 0xde, 0x00,
1018 0xdf, 0x00,
1019 0x61, 0x38,
1020 0x62, 0x0a,
1021 0x53, 0x13,
1022 0x59, 0x08,
1023 0xff, 0xff,
1024};
1025
1026static struct stv0297_config dvbc_philips_tdm1316l_config = {
1027 .demod_address = 0x1c,
1028 .inittab = dvbc_philips_tdm1316l_inittab,
1029 .invert = 0,
1030 .stop_during_read = 1,
1031};
1032
1033static struct tda10023_config tda10023_config = {
1034 .demod_address = 0xc,
1035 .invert = 0,
1036 .xtal = 16000000,
1037 .pll_m = 11,
1038 .pll_p = 3,
1039 .pll_n = 1,
1040 .deltaf = 0xa511,
1041};
1042
1043static struct tda827x_config tda827x_config = {
1044 .config = 0,
1045};
1046
1047/* TT S2-3200 DVB-S (STB0899) Inittab */
1048static const struct stb0899_s1_reg tt3200_stb0899_s1_init_1[] = {
1049
1050 { STB0899_DEV_ID , 0x81 },
1051 { STB0899_DISCNTRL1 , 0x32 },
1052 { STB0899_DISCNTRL2 , 0x80 },
1053 { STB0899_DISRX_ST0 , 0x04 },
1054 { STB0899_DISRX_ST1 , 0x00 },
1055 { STB0899_DISPARITY , 0x00 },
1056 { STB0899_DISFIFO , 0x00 },
1057 { STB0899_DISSTATUS , 0x20 },
1058 { STB0899_DISF22 , 0x8c },
1059 { STB0899_DISF22RX , 0x9a },
1060 { STB0899_SYSREG , 0x0b },
1061 { STB0899_ACRPRESC , 0x11 },
1062 { STB0899_ACRDIV1 , 0x0a },
1063 { STB0899_ACRDIV2 , 0x05 },
1064 { STB0899_DACR1 , 0x00 },
1065 { STB0899_DACR2 , 0x00 },
1066 { STB0899_OUTCFG , 0x00 },
1067 { STB0899_MODECFG , 0x00 },
1068 { STB0899_IRQSTATUS_3 , 0x30 },
1069 { STB0899_IRQSTATUS_2 , 0x00 },
1070 { STB0899_IRQSTATUS_1 , 0x00 },
1071 { STB0899_IRQSTATUS_0 , 0x00 },
1072 { STB0899_IRQMSK_3 , 0xf3 },
1073 { STB0899_IRQMSK_2 , 0xfc },
1074 { STB0899_IRQMSK_1 , 0xff },
1075 { STB0899_IRQMSK_0 , 0xff },
1076 { STB0899_IRQCFG , 0x00 },
1077 { STB0899_I2CCFG , 0x88 },
1078 { STB0899_I2CRPT , 0x48 }, /* 12k Pullup, Repeater=16, Stop=disabled */
1079 { STB0899_IOPVALUE5 , 0x00 },
1080 { STB0899_IOPVALUE4 , 0x20 },
1081 { STB0899_IOPVALUE3 , 0xc9 },
1082 { STB0899_IOPVALUE2 , 0x90 },
1083 { STB0899_IOPVALUE1 , 0x40 },
1084 { STB0899_IOPVALUE0 , 0x00 },
1085 { STB0899_GPIO00CFG , 0x82 },
1086 { STB0899_GPIO01CFG , 0x82 },
1087 { STB0899_GPIO02CFG , 0x82 },
1088 { STB0899_GPIO03CFG , 0x82 },
1089 { STB0899_GPIO04CFG , 0x82 },
1090 { STB0899_GPIO05CFG , 0x82 },
1091 { STB0899_GPIO06CFG , 0x82 },
1092 { STB0899_GPIO07CFG , 0x82 },
1093 { STB0899_GPIO08CFG , 0x82 },
1094 { STB0899_GPIO09CFG , 0x82 },
1095 { STB0899_GPIO10CFG , 0x82 },
1096 { STB0899_GPIO11CFG , 0x82 },
1097 { STB0899_GPIO12CFG , 0x82 },
1098 { STB0899_GPIO13CFG , 0x82 },
1099 { STB0899_GPIO14CFG , 0x82 },
1100 { STB0899_GPIO15CFG , 0x82 },
1101 { STB0899_GPIO16CFG , 0x82 },
1102 { STB0899_GPIO17CFG , 0x82 },
1103 { STB0899_GPIO18CFG , 0x82 },
1104 { STB0899_GPIO19CFG , 0x82 },
1105 { STB0899_GPIO20CFG , 0x82 },
1106 { STB0899_SDATCFG , 0xb8 },
1107 { STB0899_SCLTCFG , 0xba },
1108 { STB0899_AGCRFCFG , 0x1c }, /* 0x11 */
1109 { STB0899_GPIO22 , 0x82 }, /* AGCBB2CFG */
1110 { STB0899_GPIO21 , 0x91 }, /* AGCBB1CFG */
1111 { STB0899_DIRCLKCFG , 0x82 },
1112 { STB0899_CLKOUT27CFG , 0x7e },
1113 { STB0899_STDBYCFG , 0x82 },
1114 { STB0899_CS0CFG , 0x82 },
1115 { STB0899_CS1CFG , 0x82 },
1116 { STB0899_DISEQCOCFG , 0x20 },
1117 { STB0899_GPIO32CFG , 0x82 },
1118 { STB0899_GPIO33CFG , 0x82 },
1119 { STB0899_GPIO34CFG , 0x82 },
1120 { STB0899_GPIO35CFG , 0x82 },
1121 { STB0899_GPIO36CFG , 0x82 },
1122 { STB0899_GPIO37CFG , 0x82 },
1123 { STB0899_GPIO38CFG , 0x82 },
1124 { STB0899_GPIO39CFG , 0x82 },
1125 { STB0899_NCOARSE , 0x15 }, /* 0x15 = 27 Mhz Clock, F/3 = 198MHz, F/6 = 99MHz */
1126 { STB0899_SYNTCTRL , 0x02 }, /* 0x00 = CLK from CLKI, 0x02 = CLK from XTALI */
1127 { STB0899_FILTCTRL , 0x00 },
1128 { STB0899_SYSCTRL , 0x00 },
1129 { STB0899_STOPCLK1 , 0x20 },
1130 { STB0899_STOPCLK2 , 0x00 },
1131 { STB0899_INTBUFSTATUS , 0x00 },
1132 { STB0899_INTBUFCTRL , 0x0a },
1133 { 0xffff , 0xff },
1134};
1135
1136static const struct stb0899_s1_reg tt3200_stb0899_s1_init_3[] = {
1137 { STB0899_DEMOD , 0x00 },
1138 { STB0899_RCOMPC , 0xc9 },
1139 { STB0899_AGC1CN , 0x41 },
1140 { STB0899_AGC1REF , 0x10 },
1141 { STB0899_RTC , 0x7a },
1142 { STB0899_TMGCFG , 0x4e },
1143 { STB0899_AGC2REF , 0x34 },
1144 { STB0899_TLSR , 0x84 },
1145 { STB0899_CFD , 0xc7 },
1146 { STB0899_ACLC , 0x87 },
1147 { STB0899_BCLC , 0x94 },
1148 { STB0899_EQON , 0x41 },
1149 { STB0899_LDT , 0xdd },
1150 { STB0899_LDT2 , 0xc9 },
1151 { STB0899_EQUALREF , 0xb4 },
1152 { STB0899_TMGRAMP , 0x10 },
1153 { STB0899_TMGTHD , 0x30 },
1154 { STB0899_IDCCOMP , 0xfb },
1155 { STB0899_QDCCOMP , 0x03 },
1156 { STB0899_POWERI , 0x3b },
1157 { STB0899_POWERQ , 0x3d },
1158 { STB0899_RCOMP , 0x81 },
1159 { STB0899_AGCIQIN , 0x80 },
1160 { STB0899_AGC2I1 , 0x04 },
1161 { STB0899_AGC2I2 , 0xf5 },
1162 { STB0899_TLIR , 0x25 },
1163 { STB0899_RTF , 0x80 },
1164 { STB0899_DSTATUS , 0x00 },
1165 { STB0899_LDI , 0xca },
1166 { STB0899_CFRM , 0xf1 },
1167 { STB0899_CFRL , 0xf3 },
1168 { STB0899_NIRM , 0x2a },
1169 { STB0899_NIRL , 0x05 },
1170 { STB0899_ISYMB , 0x17 },
1171 { STB0899_QSYMB , 0xfa },
1172 { STB0899_SFRH , 0x2f },
1173 { STB0899_SFRM , 0x68 },
1174 { STB0899_SFRL , 0x40 },
1175 { STB0899_SFRUPH , 0x2f },
1176 { STB0899_SFRUPM , 0x68 },
1177 { STB0899_SFRUPL , 0x40 },
1178 { STB0899_EQUAI1 , 0xfd },
1179 { STB0899_EQUAQ1 , 0x04 },
1180 { STB0899_EQUAI2 , 0x0f },
1181 { STB0899_EQUAQ2 , 0xff },
1182 { STB0899_EQUAI3 , 0xdf },
1183 { STB0899_EQUAQ3 , 0xfa },
1184 { STB0899_EQUAI4 , 0x37 },
1185 { STB0899_EQUAQ4 , 0x0d },
1186 { STB0899_EQUAI5 , 0xbd },
1187 { STB0899_EQUAQ5 , 0xf7 },
1188 { STB0899_DSTATUS2 , 0x00 },
1189 { STB0899_VSTATUS , 0x00 },
1190 { STB0899_VERROR , 0xff },
1191 { STB0899_IQSWAP , 0x2a },
1192 { STB0899_ECNT1M , 0x00 },
1193 { STB0899_ECNT1L , 0x00 },
1194 { STB0899_ECNT2M , 0x00 },
1195 { STB0899_ECNT2L , 0x00 },
1196 { STB0899_ECNT3M , 0x00 },
1197 { STB0899_ECNT3L , 0x00 },
1198 { STB0899_FECAUTO1 , 0x06 },
1199 { STB0899_FECM , 0x01 },
1200 { STB0899_VTH12 , 0xf0 },
1201 { STB0899_VTH23 , 0xa0 },
1202 { STB0899_VTH34 , 0x78 },
1203 { STB0899_VTH56 , 0x4e },
1204 { STB0899_VTH67 , 0x48 },
1205 { STB0899_VTH78 , 0x38 },
1206 { STB0899_PRVIT , 0xff },
1207 { STB0899_VITSYNC , 0x19 },
1208 { STB0899_RSULC , 0xb1 }, /* DVB = 0xb1, DSS = 0xa1 */
1209 { STB0899_TSULC , 0x42 },
1210 { STB0899_RSLLC , 0x40 },
1211 { STB0899_TSLPL , 0x12 },
1212 { STB0899_TSCFGH , 0x0c },
1213 { STB0899_TSCFGM , 0x00 },
1214 { STB0899_TSCFGL , 0x0c },
1215 { STB0899_TSOUT , 0x4d }, /* 0x0d for CAM */
1216 { STB0899_RSSYNCDEL , 0x00 },
1217 { STB0899_TSINHDELH , 0x02 },
1218 { STB0899_TSINHDELM , 0x00 },
1219 { STB0899_TSINHDELL , 0x00 },
1220 { STB0899_TSLLSTKM , 0x00 },
1221 { STB0899_TSLLSTKL , 0x00 },
1222 { STB0899_TSULSTKM , 0x00 },
1223 { STB0899_TSULSTKL , 0xab },
1224 { STB0899_PCKLENUL , 0x00 },
1225 { STB0899_PCKLENLL , 0xcc },
1226 { STB0899_RSPCKLEN , 0xcc },
1227 { STB0899_TSSTATUS , 0x80 },
1228 { STB0899_ERRCTRL1 , 0xb6 },
1229 { STB0899_ERRCTRL2 , 0x96 },
1230 { STB0899_ERRCTRL3 , 0x89 },
1231 { STB0899_DMONMSK1 , 0x27 },
1232 { STB0899_DMONMSK0 , 0x03 },
1233 { STB0899_DEMAPVIT , 0x5c },
1234 { STB0899_PLPARM , 0x1f },
1235 { STB0899_PDELCTRL , 0x48 },
1236 { STB0899_PDELCTRL2 , 0x00 },
1237 { STB0899_BBHCTRL1 , 0x00 },
1238 { STB0899_BBHCTRL2 , 0x00 },
1239 { STB0899_HYSTTHRESH , 0x77 },
1240 { STB0899_MATCSTM , 0x00 },
1241 { STB0899_MATCSTL , 0x00 },
1242 { STB0899_UPLCSTM , 0x00 },
1243 { STB0899_UPLCSTL , 0x00 },
1244 { STB0899_DFLCSTM , 0x00 },
1245 { STB0899_DFLCSTL , 0x00 },
1246 { STB0899_SYNCCST , 0x00 },
1247 { STB0899_SYNCDCSTM , 0x00 },
1248 { STB0899_SYNCDCSTL , 0x00 },
1249 { STB0899_ISI_ENTRY , 0x00 },
1250 { STB0899_ISI_BIT_EN , 0x00 },
1251 { STB0899_MATSTRM , 0x00 },
1252 { STB0899_MATSTRL , 0x00 },
1253 { STB0899_UPLSTRM , 0x00 },
1254 { STB0899_UPLSTRL , 0x00 },
1255 { STB0899_DFLSTRM , 0x00 },
1256 { STB0899_DFLSTRL , 0x00 },
1257 { STB0899_SYNCSTR , 0x00 },
1258 { STB0899_SYNCDSTRM , 0x00 },
1259 { STB0899_SYNCDSTRL , 0x00 },
1260 { STB0899_CFGPDELSTATUS1 , 0x10 },
1261 { STB0899_CFGPDELSTATUS2 , 0x00 },
1262 { STB0899_BBFERRORM , 0x00 },
1263 { STB0899_BBFERRORL , 0x00 },
1264 { STB0899_UPKTERRORM , 0x00 },
1265 { STB0899_UPKTERRORL , 0x00 },
1266 { 0xffff , 0xff },
1267};
1268
1269static struct stb0899_config tt3200_config = {
1270 .init_dev = tt3200_stb0899_s1_init_1,
1271 .init_s2_demod = stb0899_s2_init_2,
1272 .init_s1_demod = tt3200_stb0899_s1_init_3,
1273 .init_s2_fec = stb0899_s2_init_4,
1274 .init_tst = stb0899_s1_init_5,
1275
1276 .postproc = NULL,
1277
1278 .demod_address = 0x68,
1279
1280 .xtal_freq = 27000000,
1281 .inversion = IQ_SWAP_ON, /* 1 */
1282
1283 .lo_clk = 76500000,
1284 .hi_clk = 99000000,
1285
1286 .esno_ave = STB0899_DVBS2_ESNO_AVE,
1287 .esno_quant = STB0899_DVBS2_ESNO_QUANT,
1288 .avframes_coarse = STB0899_DVBS2_AVFRAMES_COARSE,
1289 .avframes_fine = STB0899_DVBS2_AVFRAMES_FINE,
1290 .miss_threshold = STB0899_DVBS2_MISS_THRESHOLD,
1291 .uwp_threshold_acq = STB0899_DVBS2_UWP_THRESHOLD_ACQ,
1292 .uwp_threshold_track = STB0899_DVBS2_UWP_THRESHOLD_TRACK,
1293 .uwp_threshold_sof = STB0899_DVBS2_UWP_THRESHOLD_SOF,
1294 .sof_search_timeout = STB0899_DVBS2_SOF_SEARCH_TIMEOUT,
1295
1296 .btr_nco_bits = STB0899_DVBS2_BTR_NCO_BITS,
1297 .btr_gain_shift_offset = STB0899_DVBS2_BTR_GAIN_SHIFT_OFFSET,
1298 .crl_nco_bits = STB0899_DVBS2_CRL_NCO_BITS,
1299 .ldpc_max_iter = STB0899_DVBS2_LDPC_MAX_ITER,
1300
1301 .tuner_get_frequency = stb6100_get_frequency,
1302 .tuner_set_frequency = stb6100_set_frequency,
1303 .tuner_set_bandwidth = stb6100_set_bandwidth,
1304 .tuner_get_bandwidth = stb6100_get_bandwidth,
1305 .tuner_set_rfsiggain = NULL
1306};
1307
1308static struct stb6100_config tt3200_stb6100_config = {
1309 .tuner_address = 0x60,
1310 .refclock = 27000000,
1311};
1312
1313static void frontend_init(struct budget_ci *budget_ci)
1314{
1315 switch (budget_ci->budget.dev->pci->subsystem_device) {
1316 case 0x100c: // Hauppauge/TT Nova-CI budget (stv0299/ALPS BSRU6(tsa5059))
1317 budget_ci->budget.dvb_frontend =
1318 dvb_attach(stv0299_attach, &alps_bsru6_config, &budget_ci->budget.i2c_adap);
1319 if (budget_ci->budget.dvb_frontend) {
1320 budget_ci->budget.dvb_frontend->ops.tuner_ops.set_params = alps_bsru6_tuner_set_params;
1321 budget_ci->budget.dvb_frontend->tuner_priv = &budget_ci->budget.i2c_adap;
1322 break;
1323 }
1324 break;
1325
1326 case 0x100f: // Hauppauge/TT Nova-CI budget (stv0299b/Philips su1278(tsa5059))
1327 budget_ci->budget.dvb_frontend =
1328 dvb_attach(stv0299_attach, &philips_su1278_tt_config, &budget_ci->budget.i2c_adap);
1329 if (budget_ci->budget.dvb_frontend) {
1330 budget_ci->budget.dvb_frontend->ops.tuner_ops.set_params = philips_su1278_tt_tuner_set_params;
1331 break;
1332 }
1333 break;
1334
1335 case 0x1010: // TT DVB-C CI budget (stv0297/Philips tdm1316l(tda6651tt))
1336 budget_ci->tuner_pll_address = 0x61;
1337 budget_ci->budget.dvb_frontend =
1338 dvb_attach(stv0297_attach, &dvbc_philips_tdm1316l_config, &budget_ci->budget.i2c_adap);
1339 if (budget_ci->budget.dvb_frontend) {
1340 budget_ci->budget.dvb_frontend->ops.tuner_ops.set_params = dvbc_philips_tdm1316l_tuner_set_params;
1341 break;
1342 }
1343 break;
1344
1345 case 0x1011: // Hauppauge/TT Nova-T budget (tda10045/Philips tdm1316l(tda6651tt) + TDA9889)
1346 budget_ci->tuner_pll_address = 0x63;
1347 budget_ci->budget.dvb_frontend =
1348 dvb_attach(tda10045_attach, &philips_tdm1316l_config, &budget_ci->budget.i2c_adap);
1349 if (budget_ci->budget.dvb_frontend) {
1350 budget_ci->budget.dvb_frontend->ops.tuner_ops.init = philips_tdm1316l_tuner_init;
1351 budget_ci->budget.dvb_frontend->ops.tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
1352 break;
1353 }
1354 break;
1355
1356 case 0x1012: // TT DVB-T CI budget (tda10046/Philips tdm1316l(tda6651tt))
1357 budget_ci->tuner_pll_address = 0x60;
1358 budget_ci->budget.dvb_frontend =
1359 dvb_attach(tda10046_attach, &philips_tdm1316l_config_invert, &budget_ci->budget.i2c_adap);
1360 if (budget_ci->budget.dvb_frontend) {
1361 budget_ci->budget.dvb_frontend->ops.tuner_ops.init = philips_tdm1316l_tuner_init;
1362 budget_ci->budget.dvb_frontend->ops.tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
1363 break;
1364 }
1365 break;
1366
1367 case 0x1017: // TT S-1500 PCI
1368 budget_ci->budget.dvb_frontend = dvb_attach(stv0299_attach, &alps_bsbe1_config, &budget_ci->budget.i2c_adap);
1369 if (budget_ci->budget.dvb_frontend) {
1370 budget_ci->budget.dvb_frontend->ops.tuner_ops.set_params = alps_bsbe1_tuner_set_params;
1371 budget_ci->budget.dvb_frontend->tuner_priv = &budget_ci->budget.i2c_adap;
1372
1373 budget_ci->budget.dvb_frontend->ops.dishnetwork_send_legacy_command = NULL;
1374 if (dvb_attach(lnbp21_attach, budget_ci->budget.dvb_frontend, &budget_ci->budget.i2c_adap, LNBP21_LLC, 0) == NULL) {
1375 printk("%s: No LNBP21 found!\n", __func__);
1376 dvb_frontend_detach(budget_ci->budget.dvb_frontend);
1377 budget_ci->budget.dvb_frontend = NULL;
1378 }
1379 }
1380 break;
1381
1382 case 0x101a: /* TT Budget-C-1501 (philips tda10023/philips tda8274A) */
1383 budget_ci->budget.dvb_frontend = dvb_attach(tda10023_attach, &tda10023_config, &budget_ci->budget.i2c_adap, 0x48);
1384 if (budget_ci->budget.dvb_frontend) {
1385 if (dvb_attach(tda827x_attach, budget_ci->budget.dvb_frontend, 0x61, &budget_ci->budget.i2c_adap, &tda827x_config) == NULL) {
1386 printk(KERN_ERR "%s: No tda827x found!\n", __func__);
1387 dvb_frontend_detach(budget_ci->budget.dvb_frontend);
1388 budget_ci->budget.dvb_frontend = NULL;
1389 }
1390 }
1391 break;
1392
1393 case 0x101b: /* TT S-1500B (BSBE1-D01A - STV0288/STB6000/LNBP21) */
1394 budget_ci->budget.dvb_frontend = dvb_attach(stv0288_attach, &stv0288_bsbe1_d01a_config, &budget_ci->budget.i2c_adap);
1395 if (budget_ci->budget.dvb_frontend) {
1396 if (dvb_attach(stb6000_attach, budget_ci->budget.dvb_frontend, 0x63, &budget_ci->budget.i2c_adap)) {
1397 if (!dvb_attach(lnbp21_attach, budget_ci->budget.dvb_frontend, &budget_ci->budget.i2c_adap, 0, 0)) {
1398 printk(KERN_ERR "%s: No LNBP21 found!\n", __func__);
1399 dvb_frontend_detach(budget_ci->budget.dvb_frontend);
1400 budget_ci->budget.dvb_frontend = NULL;
1401 }
1402 } else {
1403 printk(KERN_ERR "%s: No STB6000 found!\n", __func__);
1404 dvb_frontend_detach(budget_ci->budget.dvb_frontend);
1405 budget_ci->budget.dvb_frontend = NULL;
1406 }
1407 }
1408 break;
1409
1410 case 0x1019: // TT S2-3200 PCI
1411 /*
1412 * NOTE! on some STB0899 versions, the internal PLL takes a longer time
1413 * to settle, aka LOCK. On the older revisions of the chip, we don't see
1414 * this, as a result on the newer chips the entire clock tree, will not
1415 * be stable after a freshly POWER 'ed up situation.
1416 * In this case, we should RESET the STB0899 (Active LOW) and wait for
1417 * PLL stabilization.
1418 *
1419 * On the TT S2 3200 and clones, the STB0899 demodulator's RESETB is
1420 * connected to the SAA7146 GPIO, GPIO2, Pin 142
1421 */
1422 /* Reset Demodulator */
1423 saa7146_setgpio(budget_ci->budget.dev, 2, SAA7146_GPIO_OUTLO);
1424 /* Wait for everything to die */
1425 msleep(50);
1426 /* Pull it up out of Reset state */
1427 saa7146_setgpio(budget_ci->budget.dev, 2, SAA7146_GPIO_OUTHI);
1428 /* Wait for PLL to stabilize */
1429 msleep(250);
1430 /*
1431 * PLL state should be stable now. Ideally, we should check
1432 * for PLL LOCK status. But well, never mind!
1433 */
1434 budget_ci->budget.dvb_frontend = dvb_attach(stb0899_attach, &tt3200_config, &budget_ci->budget.i2c_adap);
1435 if (budget_ci->budget.dvb_frontend) {
1436 if (dvb_attach(stb6100_attach, budget_ci->budget.dvb_frontend, &tt3200_stb6100_config, &budget_ci->budget.i2c_adap)) {
1437 if (!dvb_attach(lnbp21_attach, budget_ci->budget.dvb_frontend, &budget_ci->budget.i2c_adap, 0, 0)) {
1438 printk("%s: No LNBP21 found!\n", __func__);
1439 dvb_frontend_detach(budget_ci->budget.dvb_frontend);
1440 budget_ci->budget.dvb_frontend = NULL;
1441 }
1442 } else {
1443 dvb_frontend_detach(budget_ci->budget.dvb_frontend);
1444 budget_ci->budget.dvb_frontend = NULL;
1445 }
1446 }
1447 break;
1448
1449 }
1450
1451 if (budget_ci->budget.dvb_frontend == NULL) {
1452 printk("budget-ci: A frontend driver was not found for device [%04x:%04x] subsystem [%04x:%04x]\n",
1453 budget_ci->budget.dev->pci->vendor,
1454 budget_ci->budget.dev->pci->device,
1455 budget_ci->budget.dev->pci->subsystem_vendor,
1456 budget_ci->budget.dev->pci->subsystem_device);
1457 } else {
1458 if (dvb_register_frontend
1459 (&budget_ci->budget.dvb_adapter, budget_ci->budget.dvb_frontend)) {
1460 printk("budget-ci: Frontend registration failed!\n");
1461 dvb_frontend_detach(budget_ci->budget.dvb_frontend);
1462 budget_ci->budget.dvb_frontend = NULL;
1463 }
1464 }
1465}
1466
1467static int budget_ci_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
1468{
1469 struct budget_ci *budget_ci;
1470 int err;
1471
1472 budget_ci = kzalloc(sizeof(struct budget_ci), GFP_KERNEL);
1473 if (!budget_ci) {
1474 err = -ENOMEM;
1475 goto out1;
1476 }
1477
1478 dprintk(2, "budget_ci: %p\n", budget_ci);
1479
1480 dev->ext_priv = budget_ci;
1481
1482 err = ttpci_budget_init(&budget_ci->budget, dev, info, THIS_MODULE,
1483 adapter_nr);
1484 if (err)
1485 goto out2;
1486
1487 err = msp430_ir_init(budget_ci);
1488 if (err)
1489 goto out3;
1490
1491 ciintf_init(budget_ci);
1492
1493 budget_ci->budget.dvb_adapter.priv = budget_ci;
1494 frontend_init(budget_ci);
1495
1496 ttpci_budget_init_hooks(&budget_ci->budget);
1497
1498 return 0;
1499
1500out3:
1501 ttpci_budget_deinit(&budget_ci->budget);
1502out2:
1503 kfree(budget_ci);
1504out1:
1505 return err;
1506}
1507
1508static int budget_ci_detach(struct saa7146_dev *dev)
1509{
1510 struct budget_ci *budget_ci = (struct budget_ci *) dev->ext_priv;
1511 struct saa7146_dev *saa = budget_ci->budget.dev;
1512 int err;
1513
1514 if (budget_ci->budget.ci_present)
1515 ciintf_deinit(budget_ci);
1516 msp430_ir_deinit(budget_ci);
1517 if (budget_ci->budget.dvb_frontend) {
1518 dvb_unregister_frontend(budget_ci->budget.dvb_frontend);
1519 dvb_frontend_detach(budget_ci->budget.dvb_frontend);
1520 }
1521 err = ttpci_budget_deinit(&budget_ci->budget);
1522
1523 // disable frontend and CI interface
1524 saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
1525
1526 kfree(budget_ci);
1527
1528 return err;
1529}
1530
1531static struct saa7146_extension budget_extension;
1532
1533MAKE_BUDGET_INFO(ttbs2, "TT-Budget/S-1500 PCI", BUDGET_TT);
1534MAKE_BUDGET_INFO(ttbci, "TT-Budget/WinTV-NOVA-CI PCI", BUDGET_TT_HW_DISEQC);
1535MAKE_BUDGET_INFO(ttbt2, "TT-Budget/WinTV-NOVA-T PCI", BUDGET_TT);
1536MAKE_BUDGET_INFO(ttbtci, "TT-Budget-T-CI PCI", BUDGET_TT);
1537MAKE_BUDGET_INFO(ttbcci, "TT-Budget-C-CI PCI", BUDGET_TT);
1538MAKE_BUDGET_INFO(ttc1501, "TT-Budget C-1501 PCI", BUDGET_TT);
1539MAKE_BUDGET_INFO(tt3200, "TT-Budget S2-3200 PCI", BUDGET_TT);
1540MAKE_BUDGET_INFO(ttbs1500b, "TT-Budget S-1500B PCI", BUDGET_TT);
1541
1542static struct pci_device_id pci_tbl[] = {
1543 MAKE_EXTENSION_PCI(ttbci, 0x13c2, 0x100c),
1544 MAKE_EXTENSION_PCI(ttbci, 0x13c2, 0x100f),
1545 MAKE_EXTENSION_PCI(ttbcci, 0x13c2, 0x1010),
1546 MAKE_EXTENSION_PCI(ttbt2, 0x13c2, 0x1011),
1547 MAKE_EXTENSION_PCI(ttbtci, 0x13c2, 0x1012),
1548 MAKE_EXTENSION_PCI(ttbs2, 0x13c2, 0x1017),
1549 MAKE_EXTENSION_PCI(ttc1501, 0x13c2, 0x101a),
1550 MAKE_EXTENSION_PCI(tt3200, 0x13c2, 0x1019),
1551 MAKE_EXTENSION_PCI(ttbs1500b, 0x13c2, 0x101b),
1552 {
1553 .vendor = 0,
1554 }
1555};
1556
1557MODULE_DEVICE_TABLE(pci, pci_tbl);
1558
1559static struct saa7146_extension budget_extension = {
1560 .name = "budget_ci dvb",
1561 .flags = SAA7146_USE_I2C_IRQ,
1562
1563 .module = THIS_MODULE,
1564 .pci_tbl = &pci_tbl[0],
1565 .attach = budget_ci_attach,
1566 .detach = budget_ci_detach,
1567
1568 .irq_mask = MASK_03 | MASK_06 | MASK_10,
1569 .irq_func = budget_ci_irq,
1570};
1571
1572static int __init budget_ci_init(void)
1573{
1574 return saa7146_register_extension(&budget_extension);
1575}
1576
1577static void __exit budget_ci_exit(void)
1578{
1579 saa7146_unregister_extension(&budget_extension);
1580}
1581
1582module_init(budget_ci_init);
1583module_exit(budget_ci_exit);
1584
1585MODULE_LICENSE("GPL");
1586MODULE_AUTHOR("Michael Hunold, Jack Thomasson, Andrew de Quincey, others");
1587MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1588 "budget PCI DVB cards w/ CI-module produced by "
1589 "Siemens, Technotrend, Hauppauge");
diff --git a/drivers/media/dvb/ttpci/budget-core.c b/drivers/media/dvb/ttpci/budget-core.c
new file mode 100644
index 00000000000..37666d4edab
--- /dev/null
+++ b/drivers/media/dvb/ttpci/budget-core.c
@@ -0,0 +1,600 @@
1/*
2 * budget-core.c: driver for the SAA7146 based Budget DVB cards
3 *
4 * Compiled from various sources by Michael Hunold <michael@mihu.de>
5 *
6 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
7 *
8 * Copyright (C) 1999-2002 Ralph Metzler
9 * & Marcus Metzler for convergence integrated media GmbH
10 *
11 * 26feb2004 Support for FS Activy Card (Grundig tuner) by
12 * Michael Dreher <michael@5dot1.de>,
13 * Oliver Endriss <o.endriss@gmx.de>,
14 * Andreas 'randy' Weinberger
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
20 *
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
32 *
33 *
34 * the project's page is at http://www.linuxtv.org/
35 */
36
37
38#include "budget.h"
39#include "ttpci-eeprom.h"
40
41#define TS_WIDTH (2 * TS_SIZE)
42#define TS_WIDTH_ACTIVY TS_SIZE
43#define TS_WIDTH_DVBC TS_SIZE
44#define TS_HEIGHT_MASK 0xf00
45#define TS_HEIGHT_MASK_ACTIVY 0xc00
46#define TS_HEIGHT_MASK_DVBC 0xe00
47#define TS_MIN_BUFSIZE_K 188
48#define TS_MAX_BUFSIZE_K 1410
49#define TS_MAX_BUFSIZE_K_ACTIVY 564
50#define TS_MAX_BUFSIZE_K_DVBC 1316
51#define BUFFER_WARNING_WAIT (30*HZ)
52
53int budget_debug;
54static int dma_buffer_size = TS_MIN_BUFSIZE_K;
55module_param_named(debug, budget_debug, int, 0644);
56module_param_named(bufsize, dma_buffer_size, int, 0444);
57MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off).");
58MODULE_PARM_DESC(bufsize, "DMA buffer size in KB, default: 188, min: 188, max: 1410 (Activy: 564)");
59
60/****************************************************************************
61 * TT budget / WinTV Nova
62 ****************************************************************************/
63
64static int stop_ts_capture(struct budget *budget)
65{
66 dprintk(2, "budget: %p\n", budget);
67
68 saa7146_write(budget->dev, MC1, MASK_20); // DMA3 off
69 SAA7146_IER_DISABLE(budget->dev, MASK_10);
70 return 0;
71}
72
73static int start_ts_capture(struct budget *budget)
74{
75 struct saa7146_dev *dev = budget->dev;
76
77 dprintk(2, "budget: %p\n", budget);
78
79 if (!budget->feeding || !budget->fe_synced)
80 return 0;
81
82 saa7146_write(dev, MC1, MASK_20); // DMA3 off
83
84 memset(budget->grabbing, 0x00, budget->buffer_size);
85
86 saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
87
88 budget->ttbp = 0;
89
90 /*
91 * Signal path on the Activy:
92 *
93 * tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
94 *
95 * Since the tuner feeds 204 bytes packets into the SAA7146,
96 * DMA3 is configured to strip the trailing 16 FEC bytes:
97 * Pitch: 188, NumBytes3: 188, NumLines3: 1024
98 */
99
100 switch(budget->card->type) {
101 case BUDGET_FS_ACTIVY:
102 saa7146_write(dev, DD1_INIT, 0x04000000);
103 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
104 saa7146_write(dev, BRS_CTRL, 0x00000000);
105 break;
106 case BUDGET_PATCH:
107 saa7146_write(dev, DD1_INIT, 0x00000200);
108 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
109 saa7146_write(dev, BRS_CTRL, 0x60000000);
110 break;
111 case BUDGET_CIN1200C_MK3:
112 case BUDGET_KNC1C_MK3:
113 case BUDGET_KNC1CP_MK3:
114 if (budget->video_port == BUDGET_VIDEO_PORTA) {
115 saa7146_write(dev, DD1_INIT, 0x06000200);
116 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
117 saa7146_write(dev, BRS_CTRL, 0x00000000);
118 } else {
119 saa7146_write(dev, DD1_INIT, 0x00000600);
120 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
121 saa7146_write(dev, BRS_CTRL, 0x60000000);
122 }
123 break;
124 default:
125 if (budget->video_port == BUDGET_VIDEO_PORTA) {
126 saa7146_write(dev, DD1_INIT, 0x06000200);
127 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
128 saa7146_write(dev, BRS_CTRL, 0x00000000);
129 } else {
130 saa7146_write(dev, DD1_INIT, 0x02000600);
131 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
132 saa7146_write(dev, BRS_CTRL, 0x60000000);
133 }
134 }
135
136 saa7146_write(dev, MC2, (MASK_08 | MASK_24));
137 mdelay(10);
138
139 saa7146_write(dev, BASE_ODD3, 0);
140 if (budget->buffer_size > budget->buffer_height * budget->buffer_width) {
141 // using odd/even buffers
142 saa7146_write(dev, BASE_EVEN3, budget->buffer_height * budget->buffer_width);
143 } else {
144 // using a single buffer
145 saa7146_write(dev, BASE_EVEN3, 0);
146 }
147 saa7146_write(dev, PROT_ADDR3, budget->buffer_size);
148 saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
149
150 saa7146_write(dev, PITCH3, budget->buffer_width);
151 saa7146_write(dev, NUM_LINE_BYTE3,
152 (budget->buffer_height << 16) | budget->buffer_width);
153
154 saa7146_write(dev, MC2, (MASK_04 | MASK_20));
155
156 SAA7146_ISR_CLEAR(budget->dev, MASK_10); /* VPE */
157 SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
158 saa7146_write(dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
159
160 return 0;
161}
162
163static int budget_read_fe_status(struct dvb_frontend *fe, fe_status_t *status)
164{
165 struct budget *budget = (struct budget *) fe->dvb->priv;
166 int synced;
167 int ret;
168
169 if (budget->read_fe_status)
170 ret = budget->read_fe_status(fe, status);
171 else
172 ret = -EINVAL;
173
174 if (!ret) {
175 synced = (*status & FE_HAS_LOCK);
176 if (synced != budget->fe_synced) {
177 budget->fe_synced = synced;
178 spin_lock(&budget->feedlock);
179 if (synced)
180 start_ts_capture(budget);
181 else
182 stop_ts_capture(budget);
183 spin_unlock(&budget->feedlock);
184 }
185 }
186 return ret;
187}
188
189static void vpeirq(unsigned long data)
190{
191 struct budget *budget = (struct budget *) data;
192 u8 *mem = (u8 *) (budget->grabbing);
193 u32 olddma = budget->ttbp;
194 u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
195 u32 count;
196
197 /* Ensure streamed PCI data is synced to CPU */
198 pci_dma_sync_sg_for_cpu(budget->dev->pci, budget->pt.slist, budget->pt.nents, PCI_DMA_FROMDEVICE);
199
200 /* nearest lower position divisible by 188 */
201 newdma -= newdma % 188;
202
203 if (newdma >= budget->buffer_size)
204 return;
205
206 budget->ttbp = newdma;
207
208 if (budget->feeding == 0 || newdma == olddma)
209 return;
210
211 if (newdma > olddma) { /* no wraparound, dump olddma..newdma */
212 count = newdma - olddma;
213 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
214 } else { /* wraparound, dump olddma..buflen and 0..newdma */
215 count = budget->buffer_size - olddma;
216 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
217 count += newdma;
218 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
219 }
220
221 if (count > budget->buffer_warning_threshold)
222 budget->buffer_warnings++;
223
224 if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) {
225 printk("%s %s: used %d times >80%% of buffer (%u bytes now)\n",
226 budget->dev->name, __func__, budget->buffer_warnings, count);
227 budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT;
228 budget->buffer_warnings = 0;
229 }
230}
231
232
233int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
234 int uselocks, int nobusyloop)
235{
236 struct saa7146_dev *saa = budget->dev;
237 int result = 0;
238 unsigned long flags = 0;
239
240 if (count > 4 || count <= 0)
241 return 0;
242
243 if (uselocks)
244 spin_lock_irqsave(&budget->debilock, flags);
245
246 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
247 if (uselocks)
248 spin_unlock_irqrestore(&budget->debilock, flags);
249 return result;
250 }
251
252 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
253 saa7146_write(saa, DEBI_CONFIG, config);
254 saa7146_write(saa, DEBI_PAGE, 0);
255 saa7146_write(saa, MC2, (2 << 16) | 2);
256
257 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
258 if (uselocks)
259 spin_unlock_irqrestore(&budget->debilock, flags);
260 return result;
261 }
262
263 result = saa7146_read(saa, DEBI_AD);
264 result &= (0xffffffffUL >> ((4 - count) * 8));
265
266 if (uselocks)
267 spin_unlock_irqrestore(&budget->debilock, flags);
268
269 return result;
270}
271
272int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
273 int count, u32 value, int uselocks, int nobusyloop)
274{
275 struct saa7146_dev *saa = budget->dev;
276 unsigned long flags = 0;
277 int result;
278
279 if (count > 4 || count <= 0)
280 return 0;
281
282 if (uselocks)
283 spin_lock_irqsave(&budget->debilock, flags);
284
285 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
286 if (uselocks)
287 spin_unlock_irqrestore(&budget->debilock, flags);
288 return result;
289 }
290
291 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
292 saa7146_write(saa, DEBI_CONFIG, config);
293 saa7146_write(saa, DEBI_PAGE, 0);
294 saa7146_write(saa, DEBI_AD, value);
295 saa7146_write(saa, MC2, (2 << 16) | 2);
296
297 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
298 if (uselocks)
299 spin_unlock_irqrestore(&budget->debilock, flags);
300 return result;
301 }
302
303 if (uselocks)
304 spin_unlock_irqrestore(&budget->debilock, flags);
305 return 0;
306}
307
308
309/****************************************************************************
310 * DVB API SECTION
311 ****************************************************************************/
312
313static int budget_start_feed(struct dvb_demux_feed *feed)
314{
315 struct dvb_demux *demux = feed->demux;
316 struct budget *budget = (struct budget *) demux->priv;
317 int status = 0;
318
319 dprintk(2, "budget: %p\n", budget);
320
321 if (!demux->dmx.frontend)
322 return -EINVAL;
323
324 spin_lock(&budget->feedlock);
325 feed->pusi_seen = 0; /* have a clean section start */
326 if (budget->feeding++ == 0)
327 status = start_ts_capture(budget);
328 spin_unlock(&budget->feedlock);
329 return status;
330}
331
332static int budget_stop_feed(struct dvb_demux_feed *feed)
333{
334 struct dvb_demux *demux = feed->demux;
335 struct budget *budget = (struct budget *) demux->priv;
336 int status = 0;
337
338 dprintk(2, "budget: %p\n", budget);
339
340 spin_lock(&budget->feedlock);
341 if (--budget->feeding == 0)
342 status = stop_ts_capture(budget);
343 spin_unlock(&budget->feedlock);
344 return status;
345}
346
347static int budget_register(struct budget *budget)
348{
349 struct dvb_demux *dvbdemux = &budget->demux;
350 int ret;
351
352 dprintk(2, "budget: %p\n", budget);
353
354 dvbdemux->priv = (void *) budget;
355
356 dvbdemux->filternum = 256;
357 dvbdemux->feednum = 256;
358 dvbdemux->start_feed = budget_start_feed;
359 dvbdemux->stop_feed = budget_stop_feed;
360 dvbdemux->write_to_decoder = NULL;
361
362 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
363 DMX_MEMORY_BASED_FILTERING);
364
365 dvb_dmx_init(&budget->demux);
366
367 budget->dmxdev.filternum = 256;
368 budget->dmxdev.demux = &dvbdemux->dmx;
369 budget->dmxdev.capabilities = 0;
370
371 dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter);
372
373 budget->hw_frontend.source = DMX_FRONTEND_0;
374
375 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
376
377 if (ret < 0)
378 return ret;
379
380 budget->mem_frontend.source = DMX_MEMORY_FE;
381 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
382 if (ret < 0)
383 return ret;
384
385 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
386 if (ret < 0)
387 return ret;
388
389 dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
390
391 return 0;
392}
393
394static void budget_unregister(struct budget *budget)
395{
396 struct dvb_demux *dvbdemux = &budget->demux;
397
398 dprintk(2, "budget: %p\n", budget);
399
400 dvb_net_release(&budget->dvb_net);
401
402 dvbdemux->dmx.close(&dvbdemux->dmx);
403 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
404 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
405
406 dvb_dmxdev_release(&budget->dmxdev);
407 dvb_dmx_release(&budget->demux);
408}
409
410int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
411 struct saa7146_pci_extension_data *info,
412 struct module *owner, short *adapter_nums)
413{
414 int ret = 0;
415 struct budget_info *bi = info->ext_priv;
416 int max_bufsize;
417 int height_mask;
418
419 memset(budget, 0, sizeof(struct budget));
420
421 dprintk(2, "dev: %p, budget: %p\n", dev, budget);
422
423 budget->card = bi;
424 budget->dev = (struct saa7146_dev *) dev;
425
426 switch(budget->card->type) {
427 case BUDGET_FS_ACTIVY:
428 budget->buffer_width = TS_WIDTH_ACTIVY;
429 max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY;
430 height_mask = TS_HEIGHT_MASK_ACTIVY;
431 break;
432
433 case BUDGET_KNC1C:
434 case BUDGET_KNC1CP:
435 case BUDGET_CIN1200C:
436 case BUDGET_KNC1C_MK3:
437 case BUDGET_KNC1CP_MK3:
438 case BUDGET_CIN1200C_MK3:
439 budget->buffer_width = TS_WIDTH_DVBC;
440 max_bufsize = TS_MAX_BUFSIZE_K_DVBC;
441 height_mask = TS_HEIGHT_MASK_DVBC;
442 break;
443
444 default:
445 budget->buffer_width = TS_WIDTH;
446 max_bufsize = TS_MAX_BUFSIZE_K;
447 height_mask = TS_HEIGHT_MASK;
448 }
449
450 if (dma_buffer_size < TS_MIN_BUFSIZE_K)
451 dma_buffer_size = TS_MIN_BUFSIZE_K;
452 else if (dma_buffer_size > max_bufsize)
453 dma_buffer_size = max_bufsize;
454
455 budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width;
456 if (budget->buffer_height > 0xfff) {
457 budget->buffer_height /= 2;
458 budget->buffer_height &= height_mask;
459 budget->buffer_size = 2 * budget->buffer_height * budget->buffer_width;
460 } else {
461 budget->buffer_height &= height_mask;
462 budget->buffer_size = budget->buffer_height * budget->buffer_width;
463 }
464 budget->buffer_warning_threshold = budget->buffer_size * 80/100;
465 budget->buffer_warnings = 0;
466 budget->buffer_warning_time = jiffies;
467
468 dprintk(2, "%s: buffer type = %s, width = %d, height = %d\n",
469 budget->dev->name,
470 budget->buffer_size > budget->buffer_width * budget->buffer_height ? "odd/even" : "single",
471 budget->buffer_width, budget->buffer_height);
472 printk("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size);
473
474 ret = dvb_register_adapter(&budget->dvb_adapter, budget->card->name,
475 owner, &budget->dev->pci->dev, adapter_nums);
476 if (ret < 0)
477 return ret;
478
479 /* set dd1 stream a & b */
480 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
481 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
482 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
483 saa7146_write(dev, DD1_INIT, 0x02000000);
484 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
485
486 if (bi->type != BUDGET_FS_ACTIVY)
487 budget->video_port = BUDGET_VIDEO_PORTB;
488 else
489 budget->video_port = BUDGET_VIDEO_PORTA;
490 spin_lock_init(&budget->feedlock);
491 spin_lock_init(&budget->debilock);
492
493 /* the Siemens DVB needs this if you want to have the i2c chips
494 get recognized before the main driver is loaded */
495 if (bi->type != BUDGET_FS_ACTIVY)
496 saa7146_write(dev, GPIO_CTRL, 0x500000); /* GPIO 3 = 1 */
497
498 strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
499
500 saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
501 strcpy(budget->i2c_adap.name, budget->card->name);
502
503 if (i2c_add_adapter(&budget->i2c_adap) < 0) {
504 ret = -ENOMEM;
505 goto err_dvb_unregister;
506 }
507
508 ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
509
510 budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt);
511 if (NULL == budget->grabbing) {
512 ret = -ENOMEM;
513 goto err_del_i2c;
514 }
515
516 saa7146_write(dev, PCI_BT_V1, 0x001c0000);
517 /* upload all */
518 saa7146_write(dev, GPIO_CTRL, 0x000000);
519
520 tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
521
522 /* frontend power on */
523 if (bi->type != BUDGET_FS_ACTIVY)
524 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
525
526 if ((ret = budget_register(budget)) == 0)
527 return 0; /* Everything OK */
528
529 /* An error occurred, cleanup resources */
530 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
531
532err_del_i2c:
533 i2c_del_adapter(&budget->i2c_adap);
534
535err_dvb_unregister:
536 dvb_unregister_adapter(&budget->dvb_adapter);
537
538 return ret;
539}
540
541void ttpci_budget_init_hooks(struct budget *budget)
542{
543 if (budget->dvb_frontend && !budget->read_fe_status) {
544 budget->read_fe_status = budget->dvb_frontend->ops.read_status;
545 budget->dvb_frontend->ops.read_status = budget_read_fe_status;
546 }
547}
548
549int ttpci_budget_deinit(struct budget *budget)
550{
551 struct saa7146_dev *dev = budget->dev;
552
553 dprintk(2, "budget: %p\n", budget);
554
555 budget_unregister(budget);
556
557 tasklet_kill(&budget->vpe_tasklet);
558
559 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
560
561 i2c_del_adapter(&budget->i2c_adap);
562
563 dvb_unregister_adapter(&budget->dvb_adapter);
564
565 return 0;
566}
567
568void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
569{
570 struct budget *budget = (struct budget *) dev->ext_priv;
571
572 dprintk(8, "dev: %p, budget: %p\n", dev, budget);
573
574 if (*isr & MASK_10)
575 tasklet_schedule(&budget->vpe_tasklet);
576}
577
578void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
579{
580 struct budget *budget = (struct budget *) dev->ext_priv;
581
582 spin_lock(&budget->feedlock);
583 budget->video_port = video_port;
584 if (budget->feeding) {
585 stop_ts_capture(budget);
586 start_ts_capture(budget);
587 }
588 spin_unlock(&budget->feedlock);
589}
590
591EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
592EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
593EXPORT_SYMBOL_GPL(ttpci_budget_init);
594EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks);
595EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
596EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
597EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
598EXPORT_SYMBOL_GPL(budget_debug);
599
600MODULE_LICENSE("GPL");
diff --git a/drivers/media/dvb/ttpci/budget-patch.c b/drivers/media/dvb/ttpci/budget-patch.c
new file mode 100644
index 00000000000..3395d1a9051
--- /dev/null
+++ b/drivers/media/dvb/ttpci/budget-patch.c
@@ -0,0 +1,673 @@
1/*
2 * budget-patch.c: driver for Budget Patch,
3 * hardware modification of DVB-S cards enabling full TS
4 *
5 * Written by Emard <emard@softhome.net>
6 *
7 * Original idea by Roberto Deza <rdeza@unav.es>
8 *
9 * Special thanks to Holger Waechtler, Michael Hunold, Marian Durkovic
10 * and Metzlerbros
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
28 *
29 *
30 * the project's page is at http://www.linuxtv.org/
31 */
32
33#include "av7110.h"
34#include "av7110_hw.h"
35#include "budget.h"
36#include "stv0299.h"
37#include "ves1x93.h"
38#include "tda8083.h"
39
40#include "bsru6.h"
41
42DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
43
44#define budget_patch budget
45
46static struct saa7146_extension budget_extension;
47
48MAKE_BUDGET_INFO(ttbp, "TT-Budget/Patch DVB-S 1.x PCI", BUDGET_PATCH);
49//MAKE_BUDGET_INFO(satel,"TT-Budget/Patch SATELCO PCI", BUDGET_TT_HW_DISEQC);
50
51static struct pci_device_id pci_tbl[] = {
52 MAKE_EXTENSION_PCI(ttbp,0x13c2, 0x0000),
53// MAKE_EXTENSION_PCI(satel, 0x13c2, 0x1013),
54 {
55 .vendor = 0,
56 }
57};
58
59/* those lines are for budget-patch to be tried
60** on a true budget card and observe the
61** behaviour of VSYNC generated by rps1.
62** this code was shamelessly copy/pasted from budget.c
63*/
64static void gpio_Set22K (struct budget *budget, int state)
65{
66 struct saa7146_dev *dev=budget->dev;
67 dprintk(2, "budget: %p\n", budget);
68 saa7146_setgpio(dev, 3, (state ? SAA7146_GPIO_OUTHI : SAA7146_GPIO_OUTLO));
69}
70
71/* Diseqc functions only for TT Budget card */
72/* taken from the Skyvision DVB driver by
73 Ralph Metzler <rjkm@metzlerbros.de> */
74
75static void DiseqcSendBit (struct budget *budget, int data)
76{
77 struct saa7146_dev *dev=budget->dev;
78 dprintk(2, "budget: %p\n", budget);
79
80 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
81 udelay(data ? 500 : 1000);
82 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
83 udelay(data ? 1000 : 500);
84}
85
86static void DiseqcSendByte (struct budget *budget, int data)
87{
88 int i, par=1, d;
89
90 dprintk(2, "budget: %p\n", budget);
91
92 for (i=7; i>=0; i--) {
93 d = (data>>i)&1;
94 par ^= d;
95 DiseqcSendBit(budget, d);
96 }
97
98 DiseqcSendBit(budget, par);
99}
100
101static int SendDiSEqCMsg (struct budget *budget, int len, u8 *msg, unsigned long burst)
102{
103 struct saa7146_dev *dev=budget->dev;
104 int i;
105
106 dprintk(2, "budget: %p\n", budget);
107
108 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
109 mdelay(16);
110
111 for (i=0; i<len; i++)
112 DiseqcSendByte(budget, msg[i]);
113
114 mdelay(16);
115
116 if (burst!=-1) {
117 if (burst)
118 DiseqcSendByte(budget, 0xff);
119 else {
120 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
121 mdelay(12);
122 udelay(500);
123 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
124 }
125 msleep(20);
126 }
127
128 return 0;
129}
130
131/* shamelessly copy/pasted from budget.c
132*/
133static int budget_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
134{
135 struct budget* budget = (struct budget*) fe->dvb->priv;
136
137 switch (tone) {
138 case SEC_TONE_ON:
139 gpio_Set22K (budget, 1);
140 break;
141
142 case SEC_TONE_OFF:
143 gpio_Set22K (budget, 0);
144 break;
145
146 default:
147 return -EINVAL;
148 }
149
150 return 0;
151}
152
153static int budget_diseqc_send_master_cmd(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd* cmd)
154{
155 struct budget* budget = (struct budget*) fe->dvb->priv;
156
157 SendDiSEqCMsg (budget, cmd->msg_len, cmd->msg, 0);
158
159 return 0;
160}
161
162static int budget_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
163{
164 struct budget* budget = (struct budget*) fe->dvb->priv;
165
166 SendDiSEqCMsg (budget, 0, NULL, minicmd);
167
168 return 0;
169}
170
171static int budget_av7110_send_fw_cmd(struct budget_patch *budget, u16* buf, int length)
172{
173 int i;
174
175 dprintk(2, "budget: %p\n", budget);
176
177 for (i = 2; i < length; i++)
178 {
179 ttpci_budget_debiwrite(budget, DEBINOSWAP, COMMAND + 2*i, 2, (u32) buf[i], 0,0);
180 msleep(5);
181 }
182 if (length)
183 ttpci_budget_debiwrite(budget, DEBINOSWAP, COMMAND + 2, 2, (u32) buf[1], 0,0);
184 else
185 ttpci_budget_debiwrite(budget, DEBINOSWAP, COMMAND + 2, 2, 0, 0,0);
186 msleep(5);
187 ttpci_budget_debiwrite(budget, DEBINOSWAP, COMMAND, 2, (u32) buf[0], 0,0);
188 msleep(5);
189 return 0;
190}
191
192static void av7110_set22k(struct budget_patch *budget, int state)
193{
194 u16 buf[2] = {( COMTYPE_AUDIODAC << 8) | (state ? ON22K : OFF22K), 0};
195
196 dprintk(2, "budget: %p\n", budget);
197 budget_av7110_send_fw_cmd(budget, buf, 2);
198}
199
200static int av7110_send_diseqc_msg(struct budget_patch *budget, int len, u8 *msg, int burst)
201{
202 int i;
203 u16 buf[18] = { ((COMTYPE_AUDIODAC << 8) | SendDiSEqC),
204 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
205
206 dprintk(2, "budget: %p\n", budget);
207
208 if (len>10)
209 len=10;
210
211 buf[1] = len+2;
212 buf[2] = len;
213
214 if (burst != -1)
215 buf[3]=burst ? 0x01 : 0x00;
216 else
217 buf[3]=0xffff;
218
219 for (i=0; i<len; i++)
220 buf[i+4]=msg[i];
221
222 budget_av7110_send_fw_cmd(budget, buf, 18);
223 return 0;
224}
225
226static int budget_patch_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
227{
228 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
229
230 switch (tone) {
231 case SEC_TONE_ON:
232 av7110_set22k (budget, 1);
233 break;
234
235 case SEC_TONE_OFF:
236 av7110_set22k (budget, 0);
237 break;
238
239 default:
240 return -EINVAL;
241 }
242
243 return 0;
244}
245
246static int budget_patch_diseqc_send_master_cmd(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd* cmd)
247{
248 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
249
250 av7110_send_diseqc_msg (budget, cmd->msg_len, cmd->msg, 0);
251
252 return 0;
253}
254
255static int budget_patch_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
256{
257 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
258
259 av7110_send_diseqc_msg (budget, 0, NULL, minicmd);
260
261 return 0;
262}
263
264static int alps_bsrv2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
265{
266 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
267 u8 pwr = 0;
268 u8 buf[4];
269 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };
270 u32 div = (params->frequency + 479500) / 125;
271
272 if (params->frequency > 2000000) pwr = 3;
273 else if (params->frequency > 1800000) pwr = 2;
274 else if (params->frequency > 1600000) pwr = 1;
275 else if (params->frequency > 1200000) pwr = 0;
276 else if (params->frequency >= 1100000) pwr = 1;
277 else pwr = 2;
278
279 buf[0] = (div >> 8) & 0x7f;
280 buf[1] = div & 0xff;
281 buf[2] = ((div & 0x18000) >> 10) | 0x95;
282 buf[3] = (pwr << 6) | 0x30;
283
284 // NOTE: since we're using a prescaler of 2, we set the
285 // divisor frequency to 62.5kHz and divide by 125 above
286
287 if (fe->ops.i2c_gate_ctrl)
288 fe->ops.i2c_gate_ctrl(fe, 1);
289 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1)
290 return -EIO;
291 return 0;
292}
293
294static struct ves1x93_config alps_bsrv2_config = {
295 .demod_address = 0x08,
296 .xin = 90100000UL,
297 .invert_pwm = 0,
298};
299
300static int grundig_29504_451_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
301{
302 struct budget_patch* budget = (struct budget_patch*) fe->dvb->priv;
303 u32 div;
304 u8 data[4];
305 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
306
307 div = params->frequency / 125;
308 data[0] = (div >> 8) & 0x7f;
309 data[1] = div & 0xff;
310 data[2] = 0x8e;
311 data[3] = 0x00;
312
313 if (fe->ops.i2c_gate_ctrl)
314 fe->ops.i2c_gate_ctrl(fe, 1);
315 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1)
316 return -EIO;
317 return 0;
318}
319
320static struct tda8083_config grundig_29504_451_config = {
321 .demod_address = 0x68,
322};
323
324static void frontend_init(struct budget_patch* budget)
325{
326 switch(budget->dev->pci->subsystem_device) {
327 case 0x0000: // Hauppauge/TT WinTV DVB-S rev1.X
328 case 0x1013: // SATELCO Multimedia PCI
329
330 // try the ALPS BSRV2 first of all
331 budget->dvb_frontend = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &budget->i2c_adap);
332 if (budget->dvb_frontend) {
333 budget->dvb_frontend->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
334 budget->dvb_frontend->ops.diseqc_send_master_cmd = budget_patch_diseqc_send_master_cmd;
335 budget->dvb_frontend->ops.diseqc_send_burst = budget_patch_diseqc_send_burst;
336 budget->dvb_frontend->ops.set_tone = budget_patch_set_tone;
337 break;
338 }
339
340 // try the ALPS BSRU6 now
341 budget->dvb_frontend = dvb_attach(stv0299_attach, &alps_bsru6_config, &budget->i2c_adap);
342 if (budget->dvb_frontend) {
343 budget->dvb_frontend->ops.tuner_ops.set_params = alps_bsru6_tuner_set_params;
344 budget->dvb_frontend->tuner_priv = &budget->i2c_adap;
345
346 budget->dvb_frontend->ops.diseqc_send_master_cmd = budget_diseqc_send_master_cmd;
347 budget->dvb_frontend->ops.diseqc_send_burst = budget_diseqc_send_burst;
348 budget->dvb_frontend->ops.set_tone = budget_set_tone;
349 break;
350 }
351
352 // Try the grundig 29504-451
353 budget->dvb_frontend = dvb_attach(tda8083_attach, &grundig_29504_451_config, &budget->i2c_adap);
354 if (budget->dvb_frontend) {
355 budget->dvb_frontend->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
356 budget->dvb_frontend->ops.diseqc_send_master_cmd = budget_diseqc_send_master_cmd;
357 budget->dvb_frontend->ops.diseqc_send_burst = budget_diseqc_send_burst;
358 budget->dvb_frontend->ops.set_tone = budget_set_tone;
359 break;
360 }
361 break;
362 }
363
364 if (budget->dvb_frontend == NULL) {
365 printk("dvb-ttpci: A frontend driver was not found for device [%04x:%04x] subsystem [%04x:%04x]\n",
366 budget->dev->pci->vendor,
367 budget->dev->pci->device,
368 budget->dev->pci->subsystem_vendor,
369 budget->dev->pci->subsystem_device);
370 } else {
371 if (dvb_register_frontend(&budget->dvb_adapter, budget->dvb_frontend)) {
372 printk("budget-av: Frontend registration failed!\n");
373 dvb_frontend_detach(budget->dvb_frontend);
374 budget->dvb_frontend = NULL;
375 }
376 }
377}
378
379/* written by Emard */
380static int budget_patch_attach (struct saa7146_dev* dev, struct saa7146_pci_extension_data *info)
381{
382 struct budget_patch *budget;
383 int err;
384 int count = 0;
385 int detected = 0;
386
387#define PATCH_RESET 0
388#define RPS_IRQ 0
389#define HPS_SETUP 0
390#if PATCH_RESET
391 saa7146_write(dev, MC1, MASK_31);
392 msleep(40);
393#endif
394#if HPS_SETUP
395 // initialize registers. Better to have it like this
396 // than leaving something unconfigured
397 saa7146_write(dev, DD1_STREAM_B, 0);
398 // port B VSYNC at rising edge
399 saa7146_write(dev, DD1_INIT, 0x00000200); // have this in budget-core too!
400 saa7146_write(dev, BRS_CTRL, 0x00000000); // VBI
401
402 // debi config
403 // saa7146_write(dev, DEBI_CONFIG, MASK_30|MASK_28|MASK_18);
404
405 // zero all HPS registers
406 saa7146_write(dev, HPS_H_PRESCALE, 0); // r68
407 saa7146_write(dev, HPS_H_SCALE, 0); // r6c
408 saa7146_write(dev, BCS_CTRL, 0); // r70
409 saa7146_write(dev, HPS_V_SCALE, 0); // r60
410 saa7146_write(dev, HPS_V_GAIN, 0); // r64
411 saa7146_write(dev, CHROMA_KEY_RANGE, 0); // r74
412 saa7146_write(dev, CLIP_FORMAT_CTRL, 0); // r78
413 // Set HPS prescaler for port B input
414 saa7146_write(dev, HPS_CTRL, (1<<30) | (0<<29) | (1<<28) | (0<<12) );
415 saa7146_write(dev, MC2,
416 0 * (MASK_08 | MASK_24) | // BRS control
417 0 * (MASK_09 | MASK_25) | // a
418 0 * (MASK_10 | MASK_26) | // b
419 1 * (MASK_06 | MASK_22) | // HPS_CTRL1
420 1 * (MASK_05 | MASK_21) | // HPS_CTRL2
421 0 * (MASK_01 | MASK_15) // DEBI
422 );
423#endif
424 // Disable RPS1 and RPS0
425 saa7146_write(dev, MC1, ( MASK_29 | MASK_28));
426 // RPS1 timeout disable
427 saa7146_write(dev, RPS_TOV1, 0);
428
429 // code for autodetection
430 // will wait for VBI_B event (vertical blank at port B)
431 // and will reset GPIO3 after VBI_B is detected.
432 // (GPIO3 should be raised high by CPU to
433 // test if GPIO3 will generate vertical blank signal
434 // in budget patch GPIO3 is connected to VSYNC_B
435 count = 0;
436#if 0
437 WRITE_RPS1(CMD_UPLOAD |
438 MASK_10 | MASK_09 | MASK_08 | MASK_06 | MASK_05 | MASK_04 | MASK_03 | MASK_02 );
439#endif
440 WRITE_RPS1(CMD_PAUSE | EVT_VBI_B);
441 WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
442 WRITE_RPS1(GPIO3_MSK);
443 WRITE_RPS1(SAA7146_GPIO_OUTLO<<24);
444#if RPS_IRQ
445 // issue RPS1 interrupt to increment counter
446 WRITE_RPS1(CMD_INTERRUPT);
447 // at least a NOP is neede between two interrupts
448 WRITE_RPS1(CMD_NOP);
449 // interrupt again
450 WRITE_RPS1(CMD_INTERRUPT);
451#endif
452 WRITE_RPS1(CMD_STOP);
453
454#if RPS_IRQ
455 // set event counter 1 source as RPS1 interrupt (0x03) (rE4 p53)
456 // use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
457 // use 0x15 to track VPE interrupts - increase by 1 every vpeirq() is called
458 saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
459 // set event counter 1 threshold to maximum allowed value (rEC p55)
460 saa7146_write(dev, ECT1R, 0x3fff );
461#endif
462 // Fix VSYNC level
463 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
464 // Set RPS1 Address register to point to RPS code (r108 p42)
465 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
466 // Enable RPS1, (rFC p33)
467 saa7146_write(dev, MC1, (MASK_13 | MASK_29 ));
468
469
470 mdelay(50);
471 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
472 mdelay(150);
473
474
475 if( (saa7146_read(dev, GPIO_CTRL) & 0x10000000) == 0)
476 detected = 1;
477
478#if RPS_IRQ
479 printk("Event Counter 1 0x%04x\n", saa7146_read(dev, EC1R) & 0x3fff );
480#endif
481 // Disable RPS1
482 saa7146_write(dev, MC1, ( MASK_29 ));
483
484 if(detected == 0)
485 printk("budget-patch not detected or saa7146 in non-default state.\n"
486 "try enabling ressetting of 7146 with MASK_31 in MC1 register\n");
487
488 else
489 printk("BUDGET-PATCH DETECTED.\n");
490
491
492/* OLD (Original design by Roberto Deza):
493** This code will setup the SAA7146_RPS1 to generate a square
494** wave on GPIO3, changing when a field (TS_HEIGHT/2 "lines" of
495** TS_WIDTH packets) has been acquired on SAA7146_D1B video port;
496** then, this GPIO3 output which is connected to the D1B_VSYNC
497** input, will trigger the acquisition of the alternate field
498** and so on.
499** Currently, the TT_budget / WinTV_Nova cards have two ICs
500** (74HCT4040, LVC74) for the generation of this VSYNC signal,
501** which seems that can be done perfectly without this :-)).
502*/
503
504/* New design (By Emard)
505** this rps1 code will copy internal HS event to GPIO3 pin.
506** GPIO3 is in budget-patch hardware connected to port B VSYNC
507
508** HS is an internal event of 7146, accessible with RPS
509** and temporarily raised high every n lines
510** (n in defined in the RPS_THRESH1 counter threshold)
511** I think HS is raised high on the beginning of the n-th line
512** and remains high until this n-th line that triggered
513** it is completely received. When the reception of n-th line
514** ends, HS is lowered.
515
516** To transmit data over DMA, 7146 needs changing state at
517** port B VSYNC pin. Any changing of port B VSYNC will
518** cause some DMA data transfer, with more or less packets loss.
519** It depends on the phase and frequency of VSYNC and
520** the way of 7146 is instructed to trigger on port B (defined
521** in DD1_INIT register, 3rd nibble from the right valid
522** numbers are 0-7, see datasheet)
523**
524** The correct triggering can minimize packet loss,
525** dvbtraffic should give this stable bandwidths:
526** 22k transponder = 33814 kbit/s
527** 27.5k transponder = 38045 kbit/s
528** by experiment it is found that the best results
529** (stable bandwidths and almost no packet loss)
530** are obtained using DD1_INIT triggering number 2
531** (Va at rising edge of VS Fa = HS x VS-failing forced toggle)
532** and a VSYNC phase that occurs in the middle of DMA transfer
533** (about byte 188*512=96256 in the DMA window).
534**
535** Phase of HS is still not clear to me how to control,
536** It just happens to be so. It can be seen if one enables
537** RPS_IRQ and print Event Counter 1 in vpeirq(). Every
538** time RPS_INTERRUPT is called, the Event Counter 1 will
539** increment. That's how the 7146 is programmed to do event
540** counting in this budget-patch.c
541** I *think* HPS setting has something to do with the phase
542** of HS but I can't be 100% sure in that.
543
544** hardware debug note: a working budget card (including budget patch)
545** with vpeirq() interrupt setup in mode "0x90" (every 64K) will
546** generate 3 interrupts per 25-Hz DMA frame of 2*188*512 bytes
547** and that means 3*25=75 Hz of interrupt frequency, as seen by
548** watch cat /proc/interrupts
549**
550** If this frequency is 3x lower (and data received in the DMA
551** buffer don't start with 0x47, but in the middle of packets,
552** whose lengths appear to be like 188 292 188 104 etc.
553** this means VSYNC line is not connected in the hardware.
554** (check soldering pcb and pins)
555** The same behaviour of missing VSYNC can be duplicated on budget
556** cards, by setting DD1_INIT trigger mode 7 in 3rd nibble.
557*/
558
559 // Setup RPS1 "program" (p35)
560 count = 0;
561
562
563 // Wait Source Line Counter Threshold (p36)
564 WRITE_RPS1(CMD_PAUSE | EVT_HS);
565 // Set GPIO3=1 (p42)
566 WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
567 WRITE_RPS1(GPIO3_MSK);
568 WRITE_RPS1(SAA7146_GPIO_OUTHI<<24);
569#if RPS_IRQ
570 // issue RPS1 interrupt
571 WRITE_RPS1(CMD_INTERRUPT);
572#endif
573 // Wait reset Source Line Counter Threshold (p36)
574 WRITE_RPS1(CMD_PAUSE | RPS_INV | EVT_HS);
575 // Set GPIO3=0 (p42)
576 WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
577 WRITE_RPS1(GPIO3_MSK);
578 WRITE_RPS1(SAA7146_GPIO_OUTLO<<24);
579#if RPS_IRQ
580 // issue RPS1 interrupt
581 WRITE_RPS1(CMD_INTERRUPT);
582#endif
583 // Jump to begin of RPS program (p37)
584 WRITE_RPS1(CMD_JUMP);
585 WRITE_RPS1(dev->d_rps1.dma_handle);
586
587 // Fix VSYNC level
588 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
589 // Set RPS1 Address register to point to RPS code (r108 p42)
590 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
591
592 if (!(budget = kmalloc (sizeof(struct budget_patch), GFP_KERNEL)))
593 return -ENOMEM;
594
595 dprintk(2, "budget: %p\n", budget);
596
597 err = ttpci_budget_init(budget, dev, info, THIS_MODULE, adapter_nr);
598 if (err) {
599 kfree(budget);
600 return err;
601 }
602
603 // Set Source Line Counter Threshold, using BRS (rCC p43)
604 // It generates HS event every TS_HEIGHT lines
605 // this is related to TS_WIDTH set in register
606 // NUM_LINE_BYTE3 in budget-core.c. If NUM_LINE_BYTE
607 // low 16 bits are set to TS_WIDTH bytes (TS_WIDTH=2*188
608 //,then RPS_THRESH1
609 // should be set to trigger every TS_HEIGHT (512) lines.
610 //
611 saa7146_write(dev, RPS_THRESH1, budget->buffer_height | MASK_12 );
612
613 // saa7146_write(dev, RPS_THRESH0, ((TS_HEIGHT/2)<<16) |MASK_28| (TS_HEIGHT/2) |MASK_12 );
614 // Enable RPS1 (rFC p33)
615 saa7146_write(dev, MC1, (MASK_13 | MASK_29));
616
617
618 dev->ext_priv = budget;
619
620 budget->dvb_adapter.priv = budget;
621 frontend_init(budget);
622
623 ttpci_budget_init_hooks(budget);
624
625 return 0;
626}
627
628static int budget_patch_detach (struct saa7146_dev* dev)
629{
630 struct budget_patch *budget = (struct budget_patch*) dev->ext_priv;
631 int err;
632
633 if (budget->dvb_frontend) {
634 dvb_unregister_frontend(budget->dvb_frontend);
635 dvb_frontend_detach(budget->dvb_frontend);
636 }
637 err = ttpci_budget_deinit (budget);
638
639 kfree (budget);
640
641 return err;
642}
643
644static int __init budget_patch_init(void)
645{
646 return saa7146_register_extension(&budget_extension);
647}
648
649static void __exit budget_patch_exit(void)
650{
651 saa7146_unregister_extension(&budget_extension);
652}
653
654static struct saa7146_extension budget_extension = {
655 .name = "budget_patch dvb",
656 .flags = 0,
657
658 .module = THIS_MODULE,
659 .pci_tbl = pci_tbl,
660 .attach = budget_patch_attach,
661 .detach = budget_patch_detach,
662
663 .irq_mask = MASK_10,
664 .irq_func = ttpci_budget_irq10_handler,
665};
666
667module_init(budget_patch_init);
668module_exit(budget_patch_exit);
669
670MODULE_LICENSE("GPL");
671MODULE_AUTHOR("Emard, Roberto Deza, Holger Waechtler, Michael Hunold, others");
672MODULE_DESCRIPTION("Driver for full TS modified DVB-S SAA7146+AV7110 "
673 "based so-called Budget Patch cards");
diff --git a/drivers/media/dvb/ttpci/budget.c b/drivers/media/dvb/ttpci/budget.c
new file mode 100644
index 00000000000..d238fb9371a
--- /dev/null
+++ b/drivers/media/dvb/ttpci/budget.c
@@ -0,0 +1,793 @@
1/*
2 * budget.c: driver for the SAA7146 based Budget DVB cards
3 *
4 * Compiled from various sources by Michael Hunold <michael@mihu.de>
5 *
6 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
7 *
8 * Copyright (C) 1999-2002 Ralph Metzler
9 * & Marcus Metzler for convergence integrated media GmbH
10 *
11 * 26feb2004 Support for FS Activy Card (Grundig tuner) by
12 * Michael Dreher <michael@5dot1.de>,
13 * Oliver Endriss <o.endriss@gmx.de> and
14 * Andreas 'randy' Weinberger
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
20 *
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
32 *
33 *
34 * the project's page is at http://www.linuxtv.org/
35 */
36
37#include "budget.h"
38#include "stv0299.h"
39#include "ves1x93.h"
40#include "ves1820.h"
41#include "l64781.h"
42#include "tda8083.h"
43#include "s5h1420.h"
44#include "tda10086.h"
45#include "tda826x.h"
46#include "lnbp21.h"
47#include "bsru6.h"
48#include "bsbe1.h"
49#include "tdhd1.h"
50#include "stv6110x.h"
51#include "stv090x.h"
52#include "isl6423.h"
53
54static int diseqc_method;
55module_param(diseqc_method, int, 0444);
56MODULE_PARM_DESC(diseqc_method, "Select DiSEqC method for subsystem id 13c2:1003, 0: default, 1: more reliable (for newer revisions only)");
57
58DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
59
60static void Set22K (struct budget *budget, int state)
61{
62 struct saa7146_dev *dev=budget->dev;
63 dprintk(2, "budget: %p\n", budget);
64 saa7146_setgpio(dev, 3, (state ? SAA7146_GPIO_OUTHI : SAA7146_GPIO_OUTLO));
65}
66
67/* Diseqc functions only for TT Budget card */
68/* taken from the Skyvision DVB driver by
69 Ralph Metzler <rjkm@metzlerbros.de> */
70
71static void DiseqcSendBit (struct budget *budget, int data)
72{
73 struct saa7146_dev *dev=budget->dev;
74 dprintk(2, "budget: %p\n", budget);
75
76 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
77 udelay(data ? 500 : 1000);
78 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
79 udelay(data ? 1000 : 500);
80}
81
82static void DiseqcSendByte (struct budget *budget, int data)
83{
84 int i, par=1, d;
85
86 dprintk(2, "budget: %p\n", budget);
87
88 for (i=7; i>=0; i--) {
89 d = (data>>i)&1;
90 par ^= d;
91 DiseqcSendBit(budget, d);
92 }
93
94 DiseqcSendBit(budget, par);
95}
96
97static int SendDiSEqCMsg (struct budget *budget, int len, u8 *msg, unsigned long burst)
98{
99 struct saa7146_dev *dev=budget->dev;
100 int i;
101
102 dprintk(2, "budget: %p\n", budget);
103
104 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
105 mdelay(16);
106
107 for (i=0; i<len; i++)
108 DiseqcSendByte(budget, msg[i]);
109
110 mdelay(16);
111
112 if (burst!=-1) {
113 if (burst)
114 DiseqcSendByte(budget, 0xff);
115 else {
116 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
117 mdelay(12);
118 udelay(500);
119 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
120 }
121 msleep(20);
122 }
123
124 return 0;
125}
126
127/*
128 * Routines for the Fujitsu Siemens Activy budget card
129 * 22 kHz tone and DiSEqC are handled by the frontend.
130 * Voltage must be set here.
131 * GPIO 1: LNBP EN, GPIO 2: LNBP VSEL
132 */
133static int SetVoltage_Activy (struct budget *budget, fe_sec_voltage_t voltage)
134{
135 struct saa7146_dev *dev=budget->dev;
136
137 dprintk(2, "budget: %p\n", budget);
138
139 switch (voltage) {
140 case SEC_VOLTAGE_13:
141 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI);
142 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTLO);
143 break;
144 case SEC_VOLTAGE_18:
145 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI);
146 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
147 break;
148 case SEC_VOLTAGE_OFF:
149 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTLO);
150 break;
151 default:
152 return -EINVAL;
153 }
154
155 return 0;
156}
157
158static int siemens_budget_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
159{
160 struct budget* budget = (struct budget*) fe->dvb->priv;
161
162 return SetVoltage_Activy (budget, voltage);
163}
164
165static int budget_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
166{
167 struct budget* budget = (struct budget*) fe->dvb->priv;
168
169 switch (tone) {
170 case SEC_TONE_ON:
171 Set22K (budget, 1);
172 break;
173
174 case SEC_TONE_OFF:
175 Set22K (budget, 0);
176 break;
177
178 default:
179 return -EINVAL;
180 }
181
182 return 0;
183}
184
185static int budget_diseqc_send_master_cmd(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd* cmd)
186{
187 struct budget* budget = (struct budget*) fe->dvb->priv;
188
189 SendDiSEqCMsg (budget, cmd->msg_len, cmd->msg, 0);
190
191 return 0;
192}
193
194static int budget_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
195{
196 struct budget* budget = (struct budget*) fe->dvb->priv;
197
198 SendDiSEqCMsg (budget, 0, NULL, minicmd);
199
200 return 0;
201}
202
203static int alps_bsrv2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
204{
205 struct budget* budget = (struct budget*) fe->dvb->priv;
206 u8 pwr = 0;
207 u8 buf[4];
208 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };
209 u32 div = (params->frequency + 479500) / 125;
210
211 if (params->frequency > 2000000) pwr = 3;
212 else if (params->frequency > 1800000) pwr = 2;
213 else if (params->frequency > 1600000) pwr = 1;
214 else if (params->frequency > 1200000) pwr = 0;
215 else if (params->frequency >= 1100000) pwr = 1;
216 else pwr = 2;
217
218 buf[0] = (div >> 8) & 0x7f;
219 buf[1] = div & 0xff;
220 buf[2] = ((div & 0x18000) >> 10) | 0x95;
221 buf[3] = (pwr << 6) | 0x30;
222
223 // NOTE: since we're using a prescaler of 2, we set the
224 // divisor frequency to 62.5kHz and divide by 125 above
225
226 if (fe->ops.i2c_gate_ctrl)
227 fe->ops.i2c_gate_ctrl(fe, 1);
228 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
229 return 0;
230}
231
232static struct ves1x93_config alps_bsrv2_config =
233{
234 .demod_address = 0x08,
235 .xin = 90100000UL,
236 .invert_pwm = 0,
237};
238
239static int alps_tdbe2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
240{
241 struct budget* budget = (struct budget*) fe->dvb->priv;
242 u32 div;
243 u8 data[4];
244 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
245
246 div = (params->frequency + 35937500 + 31250) / 62500;
247
248 data[0] = (div >> 8) & 0x7f;
249 data[1] = div & 0xff;
250 data[2] = 0x85 | ((div >> 10) & 0x60);
251 data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
252
253 if (fe->ops.i2c_gate_ctrl)
254 fe->ops.i2c_gate_ctrl(fe, 1);
255 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
256 return 0;
257}
258
259static struct ves1820_config alps_tdbe2_config = {
260 .demod_address = 0x09,
261 .xin = 57840000UL,
262 .invert = 1,
263 .selagc = VES1820_SELAGC_SIGNAMPERR,
264};
265
266static int grundig_29504_401_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
267{
268 struct budget *budget = fe->dvb->priv;
269 u8 *tuner_addr = fe->tuner_priv;
270 u32 div;
271 u8 cfg, cpump, band_select;
272 u8 data[4];
273 struct i2c_msg msg = { .flags = 0, .buf = data, .len = sizeof(data) };
274
275 if (tuner_addr)
276 msg.addr = *tuner_addr;
277 else
278 msg.addr = 0x61;
279
280 div = (36125000 + params->frequency) / 166666;
281
282 cfg = 0x88;
283
284 if (params->frequency < 175000000) cpump = 2;
285 else if (params->frequency < 390000000) cpump = 1;
286 else if (params->frequency < 470000000) cpump = 2;
287 else if (params->frequency < 750000000) cpump = 1;
288 else cpump = 3;
289
290 if (params->frequency < 175000000) band_select = 0x0e;
291 else if (params->frequency < 470000000) band_select = 0x05;
292 else band_select = 0x03;
293
294 data[0] = (div >> 8) & 0x7f;
295 data[1] = div & 0xff;
296 data[2] = ((div >> 10) & 0x60) | cfg;
297 data[3] = (cpump << 6) | band_select;
298
299 if (fe->ops.i2c_gate_ctrl)
300 fe->ops.i2c_gate_ctrl(fe, 1);
301 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
302 return 0;
303}
304
305static struct l64781_config grundig_29504_401_config = {
306 .demod_address = 0x55,
307};
308
309static struct l64781_config grundig_29504_401_config_activy = {
310 .demod_address = 0x54,
311};
312
313static u8 tuner_address_grundig_29504_401_activy = 0x60;
314
315static int grundig_29504_451_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
316{
317 struct budget* budget = (struct budget*) fe->dvb->priv;
318 u32 div;
319 u8 data[4];
320 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
321
322 div = params->frequency / 125;
323 data[0] = (div >> 8) & 0x7f;
324 data[1] = div & 0xff;
325 data[2] = 0x8e;
326 data[3] = 0x00;
327
328 if (fe->ops.i2c_gate_ctrl)
329 fe->ops.i2c_gate_ctrl(fe, 1);
330 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
331 return 0;
332}
333
334static struct tda8083_config grundig_29504_451_config = {
335 .demod_address = 0x68,
336};
337
338static int s5h1420_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
339{
340 struct budget* budget = (struct budget*) fe->dvb->priv;
341 u32 div;
342 u8 data[4];
343 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
344
345 div = params->frequency / 1000;
346 data[0] = (div >> 8) & 0x7f;
347 data[1] = div & 0xff;
348 data[2] = 0xc2;
349
350 if (div < 1450)
351 data[3] = 0x00;
352 else if (div < 1850)
353 data[3] = 0x40;
354 else if (div < 2000)
355 data[3] = 0x80;
356 else
357 data[3] = 0xc0;
358
359 if (fe->ops.i2c_gate_ctrl)
360 fe->ops.i2c_gate_ctrl(fe, 1);
361 if (i2c_transfer (&budget->i2c_adap, &msg, 1) != 1) return -EIO;
362
363 return 0;
364}
365
366static struct s5h1420_config s5h1420_config = {
367 .demod_address = 0x53,
368 .invert = 1,
369 .cdclk_polarity = 1,
370};
371
372static struct tda10086_config tda10086_config = {
373 .demod_address = 0x0e,
374 .invert = 0,
375 .diseqc_tone = 1,
376 .xtal_freq = TDA10086_XTAL_16M,
377};
378
379static struct stv0299_config alps_bsru6_config_activy = {
380 .demod_address = 0x68,
381 .inittab = alps_bsru6_inittab,
382 .mclk = 88000000UL,
383 .invert = 1,
384 .op0_off = 1,
385 .min_delay_ms = 100,
386 .set_symbol_rate = alps_bsru6_set_symbol_rate,
387};
388
389static struct stv0299_config alps_bsbe1_config_activy = {
390 .demod_address = 0x68,
391 .inittab = alps_bsbe1_inittab,
392 .mclk = 88000000UL,
393 .invert = 1,
394 .op0_off = 1,
395 .min_delay_ms = 100,
396 .set_symbol_rate = alps_bsbe1_set_symbol_rate,
397};
398
399static int alps_tdhd1_204_request_firmware(struct dvb_frontend *fe, const struct firmware **fw, char *name)
400{
401 struct budget *budget = (struct budget *)fe->dvb->priv;
402
403 return request_firmware(fw, name, &budget->dev->pci->dev);
404}
405
406
407static int i2c_readreg(struct i2c_adapter *i2c, u8 adr, u8 reg)
408{
409 u8 val;
410 struct i2c_msg msg[] = {
411 { .addr = adr, .flags = 0, .buf = &reg, .len = 1 },
412 { .addr = adr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
413 };
414
415 return (i2c_transfer(i2c, msg, 2) != 2) ? -EIO : val;
416}
417
418static u8 read_pwm(struct budget* budget)
419{
420 u8 b = 0xff;
421 u8 pwm;
422 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
423 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
424
425 if ((i2c_transfer(&budget->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
426 pwm = 0x48;
427
428 return pwm;
429}
430
431static struct stv090x_config tt1600_stv090x_config = {
432 .device = STV0903,
433 .demod_mode = STV090x_SINGLE,
434 .clk_mode = STV090x_CLK_EXT,
435
436 .xtal = 13500000,
437 .address = 0x68,
438
439 .ts1_mode = STV090x_TSMODE_DVBCI,
440 .ts2_mode = STV090x_TSMODE_SERIAL_CONTINUOUS,
441
442 .repeater_level = STV090x_RPTLEVEL_16,
443
444 .tuner_init = NULL,
445 .tuner_sleep = NULL,
446 .tuner_set_mode = NULL,
447 .tuner_set_frequency = NULL,
448 .tuner_get_frequency = NULL,
449 .tuner_set_bandwidth = NULL,
450 .tuner_get_bandwidth = NULL,
451 .tuner_set_bbgain = NULL,
452 .tuner_get_bbgain = NULL,
453 .tuner_set_refclk = NULL,
454 .tuner_get_status = NULL,
455};
456
457static struct stv6110x_config tt1600_stv6110x_config = {
458 .addr = 0x60,
459 .refclk = 27000000,
460 .clk_div = 2,
461};
462
463static struct isl6423_config tt1600_isl6423_config = {
464 .current_max = SEC_CURRENT_515m,
465 .curlim = SEC_CURRENT_LIM_ON,
466 .mod_extern = 1,
467 .addr = 0x08,
468};
469
470static void frontend_init(struct budget *budget)
471{
472 (void)alps_bsbe1_config; /* avoid warning */
473
474 switch(budget->dev->pci->subsystem_device) {
475 case 0x1003: // Hauppauge/TT Nova budget (stv0299/ALPS BSRU6(tsa5059) OR ves1893/ALPS BSRV2(sp5659))
476 case 0x1013:
477 // try the ALPS BSRV2 first of all
478 budget->dvb_frontend = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &budget->i2c_adap);
479 if (budget->dvb_frontend) {
480 budget->dvb_frontend->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
481 budget->dvb_frontend->ops.diseqc_send_master_cmd = budget_diseqc_send_master_cmd;
482 budget->dvb_frontend->ops.diseqc_send_burst = budget_diseqc_send_burst;
483 budget->dvb_frontend->ops.set_tone = budget_set_tone;
484 break;
485 }
486
487 // try the ALPS BSRU6 now
488 budget->dvb_frontend = dvb_attach(stv0299_attach, &alps_bsru6_config, &budget->i2c_adap);
489 if (budget->dvb_frontend) {
490 budget->dvb_frontend->ops.tuner_ops.set_params = alps_bsru6_tuner_set_params;
491 budget->dvb_frontend->tuner_priv = &budget->i2c_adap;
492 if (budget->dev->pci->subsystem_device == 0x1003 && diseqc_method == 0) {
493 budget->dvb_frontend->ops.diseqc_send_master_cmd = budget_diseqc_send_master_cmd;
494 budget->dvb_frontend->ops.diseqc_send_burst = budget_diseqc_send_burst;
495 budget->dvb_frontend->ops.set_tone = budget_set_tone;
496 }
497 break;
498 }
499 break;
500
501 case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
502
503 budget->dvb_frontend = dvb_attach(ves1820_attach, &alps_tdbe2_config, &budget->i2c_adap, read_pwm(budget));
504 if (budget->dvb_frontend) {
505 budget->dvb_frontend->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
506 break;
507 }
508 break;
509
510 case 0x1005: // Hauppauge/TT Nova-T budget (L64781/Grundig 29504-401(tsa5060))
511
512 budget->dvb_frontend = dvb_attach(l64781_attach, &grundig_29504_401_config, &budget->i2c_adap);
513 if (budget->dvb_frontend) {
514 budget->dvb_frontend->ops.tuner_ops.set_params = grundig_29504_401_tuner_set_params;
515 budget->dvb_frontend->tuner_priv = NULL;
516 break;
517 }
518 break;
519
520 case 0x4f60: /* Fujitsu Siemens Activy Budget-S PCI rev AL (stv0299/tsa5059) */
521 {
522 int subtype = i2c_readreg(&budget->i2c_adap, 0x50, 0x67);
523
524 if (subtype < 0)
525 break;
526 /* fixme: find a better way to identify the card */
527 if (subtype < 0x36) {
528 /* assume ALPS BSRU6 */
529 budget->dvb_frontend = dvb_attach(stv0299_attach, &alps_bsru6_config_activy, &budget->i2c_adap);
530 if (budget->dvb_frontend) {
531 printk(KERN_INFO "budget: tuner ALPS BSRU6 detected\n");
532 budget->dvb_frontend->ops.tuner_ops.set_params = alps_bsru6_tuner_set_params;
533 budget->dvb_frontend->tuner_priv = &budget->i2c_adap;
534 budget->dvb_frontend->ops.set_voltage = siemens_budget_set_voltage;
535 budget->dvb_frontend->ops.dishnetwork_send_legacy_command = NULL;
536 break;
537 }
538 } else {
539 /* assume ALPS BSBE1 */
540 /* reset tuner */
541 saa7146_setgpio(budget->dev, 3, SAA7146_GPIO_OUTLO);
542 msleep(50);
543 saa7146_setgpio(budget->dev, 3, SAA7146_GPIO_OUTHI);
544 msleep(250);
545 budget->dvb_frontend = dvb_attach(stv0299_attach, &alps_bsbe1_config_activy, &budget->i2c_adap);
546 if (budget->dvb_frontend) {
547 printk(KERN_INFO "budget: tuner ALPS BSBE1 detected\n");
548 budget->dvb_frontend->ops.tuner_ops.set_params = alps_bsbe1_tuner_set_params;
549 budget->dvb_frontend->tuner_priv = &budget->i2c_adap;
550 budget->dvb_frontend->ops.set_voltage = siemens_budget_set_voltage;
551 budget->dvb_frontend->ops.dishnetwork_send_legacy_command = NULL;
552 break;
553 }
554 }
555 break;
556 }
557
558 case 0x4f61: // Fujitsu Siemens Activy Budget-S PCI rev GR (tda8083/Grundig 29504-451(tsa5522))
559 budget->dvb_frontend = dvb_attach(tda8083_attach, &grundig_29504_451_config, &budget->i2c_adap);
560 if (budget->dvb_frontend) {
561 budget->dvb_frontend->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
562 budget->dvb_frontend->ops.set_voltage = siemens_budget_set_voltage;
563 budget->dvb_frontend->ops.dishnetwork_send_legacy_command = NULL;
564 }
565 break;
566
567 case 0x5f60: /* Fujitsu Siemens Activy Budget-T PCI rev AL (tda10046/ALPS TDHD1-204A) */
568 budget->dvb_frontend = dvb_attach(tda10046_attach, &alps_tdhd1_204a_config, &budget->i2c_adap);
569 if (budget->dvb_frontend) {
570 budget->dvb_frontend->ops.tuner_ops.set_params = alps_tdhd1_204a_tuner_set_params;
571 budget->dvb_frontend->tuner_priv = &budget->i2c_adap;
572 }
573 break;
574
575 case 0x5f61: /* Fujitsu Siemens Activy Budget-T PCI rev GR (L64781/Grundig 29504-401(tsa5060)) */
576 budget->dvb_frontend = dvb_attach(l64781_attach, &grundig_29504_401_config_activy, &budget->i2c_adap);
577 if (budget->dvb_frontend) {
578 budget->dvb_frontend->tuner_priv = &tuner_address_grundig_29504_401_activy;
579 budget->dvb_frontend->ops.tuner_ops.set_params = grundig_29504_401_tuner_set_params;
580 }
581 break;
582
583 case 0x1016: // Hauppauge/TT Nova-S SE (samsung s5h1420/????(tda8260))
584 budget->dvb_frontend = dvb_attach(s5h1420_attach, &s5h1420_config, &budget->i2c_adap);
585 if (budget->dvb_frontend) {
586 budget->dvb_frontend->ops.tuner_ops.set_params = s5h1420_tuner_set_params;
587 if (dvb_attach(lnbp21_attach, budget->dvb_frontend, &budget->i2c_adap, 0, 0) == NULL) {
588 printk("%s: No LNBP21 found!\n", __func__);
589 goto error_out;
590 }
591 break;
592 }
593
594 case 0x1018: // TT Budget-S-1401 (philips tda10086/philips tda8262)
595 // gpio2 is connected to CLB - reset it + leave it high
596 saa7146_setgpio(budget->dev, 2, SAA7146_GPIO_OUTLO);
597 msleep(1);
598 saa7146_setgpio(budget->dev, 2, SAA7146_GPIO_OUTHI);
599 msleep(1);
600
601 budget->dvb_frontend = dvb_attach(tda10086_attach, &tda10086_config, &budget->i2c_adap);
602 if (budget->dvb_frontend) {
603 if (dvb_attach(tda826x_attach, budget->dvb_frontend, 0x60, &budget->i2c_adap, 0) == NULL)
604 printk("%s: No tda826x found!\n", __func__);
605 if (dvb_attach(lnbp21_attach, budget->dvb_frontend, &budget->i2c_adap, 0, 0) == NULL) {
606 printk("%s: No LNBP21 found!\n", __func__);
607 goto error_out;
608 }
609 break;
610 }
611
612 case 0x101c: { /* TT S2-1600 */
613 struct stv6110x_devctl *ctl;
614 saa7146_setgpio(budget->dev, 2, SAA7146_GPIO_OUTLO);
615 msleep(50);
616 saa7146_setgpio(budget->dev, 2, SAA7146_GPIO_OUTHI);
617 msleep(250);
618
619 budget->dvb_frontend = dvb_attach(stv090x_attach,
620 &tt1600_stv090x_config,
621 &budget->i2c_adap,
622 STV090x_DEMODULATOR_0);
623
624 if (budget->dvb_frontend) {
625
626 ctl = dvb_attach(stv6110x_attach,
627 budget->dvb_frontend,
628 &tt1600_stv6110x_config,
629 &budget->i2c_adap);
630
631 if (ctl) {
632 tt1600_stv090x_config.tuner_init = ctl->tuner_init;
633 tt1600_stv090x_config.tuner_sleep = ctl->tuner_sleep;
634 tt1600_stv090x_config.tuner_set_mode = ctl->tuner_set_mode;
635 tt1600_stv090x_config.tuner_set_frequency = ctl->tuner_set_frequency;
636 tt1600_stv090x_config.tuner_get_frequency = ctl->tuner_get_frequency;
637 tt1600_stv090x_config.tuner_set_bandwidth = ctl->tuner_set_bandwidth;
638 tt1600_stv090x_config.tuner_get_bandwidth = ctl->tuner_get_bandwidth;
639 tt1600_stv090x_config.tuner_set_bbgain = ctl->tuner_set_bbgain;
640 tt1600_stv090x_config.tuner_get_bbgain = ctl->tuner_get_bbgain;
641 tt1600_stv090x_config.tuner_set_refclk = ctl->tuner_set_refclk;
642 tt1600_stv090x_config.tuner_get_status = ctl->tuner_get_status;
643
644 /* call the init function once to initialize
645 tuner's clock output divider and demod's
646 master clock */
647 if (budget->dvb_frontend->ops.init)
648 budget->dvb_frontend->ops.init(budget->dvb_frontend);
649
650 if (dvb_attach(isl6423_attach,
651 budget->dvb_frontend,
652 &budget->i2c_adap,
653 &tt1600_isl6423_config) == NULL) {
654 printk(KERN_ERR "%s: No Intersil ISL6423 found!\n", __func__);
655 goto error_out;
656 }
657 } else {
658 printk(KERN_ERR "%s: No STV6110(A) Silicon Tuner found!\n", __func__);
659 goto error_out;
660 }
661 }
662 }
663 break;
664 }
665
666 if (budget->dvb_frontend == NULL) {
667 printk("budget: A frontend driver was not found for device [%04x:%04x] subsystem [%04x:%04x]\n",
668 budget->dev->pci->vendor,
669 budget->dev->pci->device,
670 budget->dev->pci->subsystem_vendor,
671 budget->dev->pci->subsystem_device);
672 } else {
673 if (dvb_register_frontend(&budget->dvb_adapter, budget->dvb_frontend))
674 goto error_out;
675 }
676 return;
677
678error_out:
679 printk("budget: Frontend registration failed!\n");
680 dvb_frontend_detach(budget->dvb_frontend);
681 budget->dvb_frontend = NULL;
682 return;
683}
684
685static int budget_attach (struct saa7146_dev* dev, struct saa7146_pci_extension_data *info)
686{
687 struct budget *budget = NULL;
688 int err;
689
690 budget = kmalloc(sizeof(struct budget), GFP_KERNEL);
691 if( NULL == budget ) {
692 return -ENOMEM;
693 }
694
695 dprintk(2, "dev:%p, info:%p, budget:%p\n", dev, info, budget);
696
697 dev->ext_priv = budget;
698
699 err = ttpci_budget_init(budget, dev, info, THIS_MODULE, adapter_nr);
700 if (err) {
701 printk("==> failed\n");
702 kfree (budget);
703 return err;
704 }
705
706 budget->dvb_adapter.priv = budget;
707 frontend_init(budget);
708
709 ttpci_budget_init_hooks(budget);
710
711 return 0;
712}
713
714static int budget_detach (struct saa7146_dev* dev)
715{
716 struct budget *budget = (struct budget*) dev->ext_priv;
717 int err;
718
719 if (budget->dvb_frontend) {
720 dvb_unregister_frontend(budget->dvb_frontend);
721 dvb_frontend_detach(budget->dvb_frontend);
722 }
723
724 err = ttpci_budget_deinit (budget);
725
726 kfree (budget);
727 dev->ext_priv = NULL;
728
729 return err;
730}
731
732static struct saa7146_extension budget_extension;
733
734MAKE_BUDGET_INFO(ttbs, "TT-Budget/WinTV-NOVA-S PCI", BUDGET_TT);
735MAKE_BUDGET_INFO(ttbc, "TT-Budget/WinTV-NOVA-C PCI", BUDGET_TT);
736MAKE_BUDGET_INFO(ttbt, "TT-Budget/WinTV-NOVA-T PCI", BUDGET_TT);
737MAKE_BUDGET_INFO(satel, "SATELCO Multimedia PCI", BUDGET_TT_HW_DISEQC);
738MAKE_BUDGET_INFO(ttbs1401, "TT-Budget-S-1401 PCI", BUDGET_TT);
739MAKE_BUDGET_INFO(tt1600, "TT-Budget S2-1600 PCI", BUDGET_TT);
740MAKE_BUDGET_INFO(fsacs0, "Fujitsu Siemens Activy Budget-S PCI (rev GR/grundig frontend)", BUDGET_FS_ACTIVY);
741MAKE_BUDGET_INFO(fsacs1, "Fujitsu Siemens Activy Budget-S PCI (rev AL/alps frontend)", BUDGET_FS_ACTIVY);
742MAKE_BUDGET_INFO(fsact, "Fujitsu Siemens Activy Budget-T PCI (rev GR/Grundig frontend)", BUDGET_FS_ACTIVY);
743MAKE_BUDGET_INFO(fsact1, "Fujitsu Siemens Activy Budget-T PCI (rev AL/ALPS TDHD1-204A)", BUDGET_FS_ACTIVY);
744
745static struct pci_device_id pci_tbl[] = {
746 MAKE_EXTENSION_PCI(ttbs, 0x13c2, 0x1003),
747 MAKE_EXTENSION_PCI(ttbc, 0x13c2, 0x1004),
748 MAKE_EXTENSION_PCI(ttbt, 0x13c2, 0x1005),
749 MAKE_EXTENSION_PCI(satel, 0x13c2, 0x1013),
750 MAKE_EXTENSION_PCI(ttbs, 0x13c2, 0x1016),
751 MAKE_EXTENSION_PCI(ttbs1401, 0x13c2, 0x1018),
752 MAKE_EXTENSION_PCI(tt1600, 0x13c2, 0x101c),
753 MAKE_EXTENSION_PCI(fsacs1,0x1131, 0x4f60),
754 MAKE_EXTENSION_PCI(fsacs0,0x1131, 0x4f61),
755 MAKE_EXTENSION_PCI(fsact1, 0x1131, 0x5f60),
756 MAKE_EXTENSION_PCI(fsact, 0x1131, 0x5f61),
757 {
758 .vendor = 0,
759 }
760};
761
762MODULE_DEVICE_TABLE(pci, pci_tbl);
763
764static struct saa7146_extension budget_extension = {
765 .name = "budget dvb",
766 .flags = SAA7146_USE_I2C_IRQ,
767
768 .module = THIS_MODULE,
769 .pci_tbl = pci_tbl,
770 .attach = budget_attach,
771 .detach = budget_detach,
772
773 .irq_mask = MASK_10,
774 .irq_func = ttpci_budget_irq10_handler,
775};
776
777static int __init budget_init(void)
778{
779 return saa7146_register_extension(&budget_extension);
780}
781
782static void __exit budget_exit(void)
783{
784 saa7146_unregister_extension(&budget_extension);
785}
786
787module_init(budget_init);
788module_exit(budget_exit);
789
790MODULE_LICENSE("GPL");
791MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
792MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
793 "budget PCI DVB cards by Siemens, Technotrend, Hauppauge");
diff --git a/drivers/media/dvb/ttpci/budget.h b/drivers/media/dvb/ttpci/budget.h
new file mode 100644
index 00000000000..3ad0c6789ba
--- /dev/null
+++ b/drivers/media/dvb/ttpci/budget.h
@@ -0,0 +1,123 @@
1
2#ifndef __BUDGET_DVB__
3#define __BUDGET_DVB__
4
5#include "dvb_frontend.h"
6#include "dvbdev.h"
7#include "demux.h"
8#include "dvb_demux.h"
9#include "dmxdev.h"
10#include "dvb_filter.h"
11#include "dvb_net.h"
12
13#include <linux/module.h>
14#include <linux/mutex.h>
15
16#include <media/saa7146.h>
17
18extern int budget_debug;
19
20#ifdef dprintk
21#undef dprintk
22#endif
23
24#define dprintk(level,args...) \
25 do { if ((budget_debug & level)) { printk("%s: %s(): ", KBUILD_MODNAME, __func__); printk(args); } } while (0)
26
27struct budget_info {
28 char *name;
29 int type;
30};
31
32/* place to store all the necessary device information */
33struct budget {
34
35 /* devices */
36 struct dvb_device dvb_dev;
37 struct dvb_net dvb_net;
38
39 struct saa7146_dev *dev;
40
41 struct i2c_adapter i2c_adap;
42 struct budget_info *card;
43
44 unsigned char *grabbing;
45 struct saa7146_pgtable pt;
46
47 struct tasklet_struct fidb_tasklet;
48 struct tasklet_struct vpe_tasklet;
49
50 struct dmxdev dmxdev;
51 struct dvb_demux demux;
52
53 struct dmx_frontend hw_frontend;
54 struct dmx_frontend mem_frontend;
55
56 int ci_present;
57 int video_port;
58
59 u32 buffer_width;
60 u32 buffer_height;
61 u32 buffer_size;
62 u32 buffer_warning_threshold;
63 u32 buffer_warnings;
64 unsigned long buffer_warning_time;
65
66 u32 ttbp;
67 int feeding;
68
69 spinlock_t feedlock;
70
71 spinlock_t debilock;
72
73 struct dvb_adapter dvb_adapter;
74 struct dvb_frontend *dvb_frontend;
75 int (*read_fe_status)(struct dvb_frontend *fe, fe_status_t *status);
76 int fe_synced;
77
78 void *priv;
79};
80
81#define MAKE_BUDGET_INFO(x_var,x_name,x_type) \
82static struct budget_info x_var ## _info = { \
83 .name=x_name, \
84 .type=x_type }; \
85static struct saa7146_pci_extension_data x_var = { \
86 .ext_priv = &x_var ## _info, \
87 .ext = &budget_extension };
88
89#define BUDGET_TT 0
90#define BUDGET_TT_HW_DISEQC 1
91#define BUDGET_PATCH 3
92#define BUDGET_FS_ACTIVY 4
93#define BUDGET_CIN1200S 5
94#define BUDGET_CIN1200C 6
95#define BUDGET_CIN1200T 7
96#define BUDGET_KNC1S 8
97#define BUDGET_KNC1C 9
98#define BUDGET_KNC1T 10
99#define BUDGET_KNC1SP 11
100#define BUDGET_KNC1CP 12
101#define BUDGET_KNC1TP 13
102#define BUDGET_TVSTAR 14
103#define BUDGET_CIN1200C_MK3 15
104#define BUDGET_KNC1C_MK3 16
105#define BUDGET_KNC1CP_MK3 17
106#define BUDGET_KNC1S2 18
107
108#define BUDGET_VIDEO_PORTA 0
109#define BUDGET_VIDEO_PORTB 1
110
111extern int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
112 struct saa7146_pci_extension_data *info,
113 struct module *owner, short *adapter_nums);
114extern void ttpci_budget_init_hooks(struct budget *budget);
115extern int ttpci_budget_deinit(struct budget *budget);
116extern void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr);
117extern void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port);
118extern int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
119 int uselocks, int nobusyloop);
120extern int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr, int count, u32 value,
121 int uselocks, int nobusyloop);
122
123#endif
diff --git a/drivers/media/dvb/ttpci/ttpci-eeprom.c b/drivers/media/dvb/ttpci/ttpci-eeprom.c
new file mode 100644
index 00000000000..7dd54b3026a
--- /dev/null
+++ b/drivers/media/dvb/ttpci/ttpci-eeprom.c
@@ -0,0 +1,147 @@
1/*
2 Retrieve encoded MAC address from 24C16 serial 2-wire EEPROM,
3 decode it and store it in the associated adapter struct for
4 use by dvb_net.c
5
6 This card appear to have the 24C16 write protect held to ground,
7 thus permitting normal read/write operation. Theoretically it
8 would be possible to write routines to burn a different (encoded)
9 MAC address into the EEPROM.
10
11 Robert Schlabbach GMX
12 Michael Glaum KVH Industries
13 Holger Waechtler Convergence
14
15 Copyright (C) 2002-2003 Ralph Metzler <rjkm@metzlerbros.de>
16 Metzler Brothers Systementwicklung GbR
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31
32*/
33
34#include <asm/errno.h>
35#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
38#include <linux/i2c.h>
39
40#include "ttpci-eeprom.h"
41
42#if 1
43#define dprintk(x...) do { printk(x); } while (0)
44#else
45#define dprintk(x...) do { } while (0)
46#endif
47
48
49static int check_mac_tt(u8 *buf)
50{
51 int i;
52 u16 tmp = 0xffff;
53
54 for (i = 0; i < 8; i++) {
55 tmp = (tmp << 8) | ((tmp >> 8) ^ buf[i]);
56 tmp ^= (tmp >> 4) & 0x0f;
57 tmp ^= (tmp << 12) ^ ((tmp & 0xff) << 5);
58 }
59 tmp ^= 0xffff;
60 return (((tmp >> 8) ^ buf[8]) | ((tmp & 0xff) ^ buf[9]));
61}
62
63static int getmac_tt(u8 * decodedMAC, u8 * encodedMAC)
64{
65 u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c,
66 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6,
67 0x1d, 0x36, 0x64, 0x78};
68 u8 data[20];
69 int i;
70
71 /* In case there is a sig check failure have the orig contents available */
72 memcpy(data, encodedMAC, 20);
73
74 for (i = 0; i < 20; i++)
75 data[i] ^= xor[i];
76 for (i = 0; i < 10; i++)
77 data[i] = ((data[2 * i + 1] << 8) | data[2 * i])
78 >> ((data[2 * i + 1] >> 6) & 3);
79
80 if (check_mac_tt(data))
81 return -ENODEV;
82
83 decodedMAC[0] = data[2]; decodedMAC[1] = data[1]; decodedMAC[2] = data[0];
84 decodedMAC[3] = data[6]; decodedMAC[4] = data[5]; decodedMAC[5] = data[4];
85 return 0;
86}
87
88static int ttpci_eeprom_read_encodedMAC(struct i2c_adapter *adapter, u8 * encodedMAC)
89{
90 int ret;
91 u8 b0[] = { 0xcc };
92
93 struct i2c_msg msg[] = {
94 { .addr = 0x50, .flags = 0, .buf = b0, .len = 1 },
95 { .addr = 0x50, .flags = I2C_M_RD, .buf = encodedMAC, .len = 20 }
96 };
97
98 /* dprintk("%s\n", __func__); */
99
100 ret = i2c_transfer(adapter, msg, 2);
101
102 if (ret != 2) /* Assume EEPROM isn't there */
103 return (-ENODEV);
104
105 return 0;
106}
107
108
109int ttpci_eeprom_parse_mac(struct i2c_adapter *adapter, u8 *proposed_mac)
110{
111 int ret, i;
112 u8 encodedMAC[20];
113 u8 decodedMAC[6];
114
115 ret = ttpci_eeprom_read_encodedMAC(adapter, encodedMAC);
116
117 if (ret != 0) { /* Will only be -ENODEV */
118 dprintk("Couldn't read from EEPROM: not there?\n");
119 memset(proposed_mac, 0, 6);
120 return ret;
121 }
122
123 ret = getmac_tt(decodedMAC, encodedMAC);
124 if( ret != 0 ) {
125 dprintk("adapter failed MAC signature check\n");
126 dprintk("encoded MAC from EEPROM was " );
127 for(i=0; i<19; i++) {
128 dprintk( "%.2x:", encodedMAC[i]);
129 }
130 dprintk("%.2x\n", encodedMAC[19]);
131 memset(proposed_mac, 0, 6);
132 return ret;
133 }
134
135 memcpy(proposed_mac, decodedMAC, 6);
136 dprintk("adapter has MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
137 decodedMAC[0], decodedMAC[1], decodedMAC[2],
138 decodedMAC[3], decodedMAC[4], decodedMAC[5]);
139 return 0;
140}
141
142EXPORT_SYMBOL(ttpci_eeprom_parse_mac);
143
144MODULE_LICENSE("GPL");
145MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
146MODULE_DESCRIPTION("Decode dvb_net MAC address from EEPROM of PCI DVB cards "
147 "made by Siemens, Technotrend, Hauppauge");
diff --git a/drivers/media/dvb/ttpci/ttpci-eeprom.h b/drivers/media/dvb/ttpci/ttpci-eeprom.h
new file mode 100644
index 00000000000..e2dc6cfe205
--- /dev/null
+++ b/drivers/media/dvb/ttpci/ttpci-eeprom.h
@@ -0,0 +1,33 @@
1/*
2 Retrieve encoded MAC address from ATMEL ttpci_eeprom serial 2-wire EEPROM,
3 decode it and store it in associated adapter net device
4
5 Robert Schlabbach GMX
6 Michael Glaum KVH Industries
7 Holger Waechtler Convergence
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23*/
24
25#ifndef __TTPCI_EEPROM_H__
26#define __TTPCI_EEPROM_H__
27
28#include <linux/types.h>
29#include <linux/i2c.h>
30
31extern int ttpci_eeprom_parse_mac(struct i2c_adapter *adapter, u8 *propsed_mac);
32
33#endif