Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee13...
[linux-2.6] / drivers / media / dvb / ttpci / av7110_v4l.c
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/dvb/
26  */
27
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/types.h>
31 #include <linux/delay.h>
32 #include <linux/fs.h>
33 #include <linux/timer.h>
34 #include <linux/poll.h>
35 #include <linux/smp_lock.h>
36
37 #include "av7110.h"
38 #include "av7110_hw.h"
39 #include "av7110_av.h"
40
41 int msp_writereg(struct av7110 *av7110, u8 dev, u16 reg, u16 val)
42 {
43         u8 msg[5] = { dev, reg >> 8, reg & 0xff, val >> 8 , val & 0xff };
44         struct i2c_msg msgs = { .flags = 0, .len = 5, .buf = msg };
45
46         switch (av7110->adac_type) {
47         case DVB_ADAC_MSP34x0:
48                 msgs.addr = 0x40;
49                 break;
50         case DVB_ADAC_MSP34x5:
51                 msgs.addr = 0x42;
52                 break;
53         default:
54                 return 0;
55         }
56
57         if (i2c_transfer(&av7110->i2c_adap, &msgs, 1) != 1) {
58                 dprintk(1, "dvb-ttpci: failed @ card %d, %u = %u\n",
59                        av7110->dvb_adapter.num, reg, val);
60                 return -EIO;
61         }
62         return 0;
63 }
64
65 static int msp_readreg(struct av7110 *av7110, u8 dev, u16 reg, u16 *val)
66 {
67         u8 msg1[3] = { dev, reg >> 8, reg & 0xff };
68         u8 msg2[2];
69         struct i2c_msg msgs[2] = {
70                 { .flags = 0       , .len = 3, .buf = msg1 },
71                 { .flags = I2C_M_RD, .len = 2, .buf = msg2 }
72         };
73
74         switch (av7110->adac_type) {
75         case DVB_ADAC_MSP34x0:
76                 msgs[0].addr = 0x40;
77                 msgs[1].addr = 0x40;
78                 break;
79         case DVB_ADAC_MSP34x5:
80                 msgs[0].addr = 0x42;
81                 msgs[1].addr = 0x42;
82                 break;
83         default:
84                 return 0;
85         }
86
87         if (i2c_transfer(&av7110->i2c_adap, &msgs[0], 2) != 2) {
88                 dprintk(1, "dvb-ttpci: failed @ card %d, %u\n",
89                        av7110->dvb_adapter.num, reg);
90                 return -EIO;
91         }
92         *val = (msg2[0] << 8) | msg2[1];
93         return 0;
94 }
95
96 static struct v4l2_input inputs[4] = {
97         {
98                 .index          = 0,
99                 .name           = "DVB",
100                 .type           = V4L2_INPUT_TYPE_CAMERA,
101                 .audioset       = 1,
102                 .tuner          = 0, /* ignored */
103                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
104                 .status         = 0,
105         }, {
106                 .index          = 1,
107                 .name           = "Television",
108                 .type           = V4L2_INPUT_TYPE_TUNER,
109                 .audioset       = 2,
110                 .tuner          = 0,
111                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
112                 .status         = 0,
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         }, {
122                 .index          = 3,
123                 .name           = "Y/C",
124                 .type           = V4L2_INPUT_TYPE_CAMERA,
125                 .audioset       = 0,
126                 .tuner          = 0,
127                 .std            = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
128                 .status         = 0,
129         }
130 };
131
132 static int ves1820_writereg(struct saa7146_dev *dev, u8 addr, u8 reg, u8 data)
133 {
134         u8 buf[] = { 0x00, reg, data };
135         struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 3 };
136
137         dprintk(4, "dev: %p\n", dev);
138
139         if (1 != saa7146_i2c_transfer(dev, &msg, 1, 1))
140                 return -1;
141         return 0;
142 }
143
144 static int stv0297_writereg(struct saa7146_dev *dev, u8 addr, u8 reg, u8 data)
145 {
146         u8 buf [] = { reg, data };
147         struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 2 };
148
149         if (1 != saa7146_i2c_transfer(dev, &msg, 1, 1))
150                 return -1;
151         return 0;
152 }
153
154
155 static int tuner_write(struct saa7146_dev *dev, u8 addr, u8 data [4])
156 {
157         struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = data, .len = 4 };
158
159         dprintk(4, "dev: %p\n", dev);
160
161         if (1 != saa7146_i2c_transfer(dev, &msg, 1, 1))
162                 return -1;
163         return 0;
164 }
165
166 static int ves1820_set_tv_freq(struct saa7146_dev *dev, u32 freq)
167 {
168         u32 div;
169         u8 config;
170         u8 buf[4];
171
172         dprintk(4, "freq: 0x%08x\n", freq);
173
174         /* magic number: 614. tuning with the frequency given by v4l2
175            is always off by 614*62.5 = 38375 kHz...*/
176         div = freq + 614;
177
178         buf[0] = (div >> 8) & 0x7f;
179         buf[1] = div & 0xff;
180         buf[2] = 0x8e;
181
182         if (freq < (u32) (16 * 168.25))
183                 config = 0xa0;
184         else if (freq < (u32) (16 * 447.25))
185                 config = 0x90;
186         else
187                 config = 0x30;
188         config &= ~0x02;
189
190         buf[3] = config;
191
192         return tuner_write(dev, 0x61, buf);
193 }
194
195 static int stv0297_set_tv_freq(struct saa7146_dev *dev, u32 freq)
196 {
197         u32 div;
198         u8 data[4];
199
200         div = (freq + 38900000 + 31250) / 62500;
201
202         data[0] = (div >> 8) & 0x7f;
203         data[1] = div & 0xff;
204         data[2] = 0xce;
205
206         if (freq < 45000000)
207                 return -EINVAL;
208         else if (freq < 137000000)
209                 data[3] = 0x01;
210         else if (freq < 403000000)
211                 data[3] = 0x02;
212         else if (freq < 860000000)
213                 data[3] = 0x04;
214         else
215                 return -EINVAL;
216
217         stv0297_writereg(dev, 0x1C, 0x87, 0x78);
218         stv0297_writereg(dev, 0x1C, 0x86, 0xc8);
219         return tuner_write(dev, 0x63, data);
220 }
221
222
223
224 static struct saa7146_standard analog_standard[];
225 static struct saa7146_standard dvb_standard[];
226 static struct saa7146_standard standard[];
227
228 static struct v4l2_audio msp3400_v4l2_audio = {
229         .index = 0,
230         .name = "Television",
231         .capability = V4L2_AUDCAP_STEREO
232 };
233
234 static int av7110_dvb_c_switch(struct saa7146_fh *fh)
235 {
236         struct saa7146_dev *dev = fh->dev;
237         struct saa7146_vv *vv = dev->vv_data;
238         struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
239         u16 adswitch;
240         int source, sync, err;
241
242         dprintk(4, "%p\n", av7110);
243
244         if ((vv->video_status & STATUS_OVERLAY) != 0) {
245                 vv->ov_suspend = vv->video_fh;
246                 err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
247                 if (err != 0) {
248                         dprintk(2, "suspending video failed\n");
249                         vv->ov_suspend = NULL;
250                 }
251         }
252
253         if (0 != av7110->current_input) {
254                 dprintk(1, "switching to analog TV:\n");
255                 adswitch = 1;
256                 source = SAA7146_HPS_SOURCE_PORT_B;
257                 sync = SAA7146_HPS_SYNC_PORT_B;
258                 memcpy(standard, analog_standard, sizeof(struct saa7146_standard) * 2);
259
260                 switch (av7110->current_input) {
261                 case 1:
262                         dprintk(1, "switching SAA7113 to Analog Tuner Input.\n");
263                         msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0000); // loudspeaker source
264                         msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0000); // headphone source
265                         msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0000); // SCART 1 source
266                         msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
267                         msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); // loudspeaker + headphone
268                         msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); // SCART 1 volume
269
270                         if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
271                                 if (ves1820_writereg(dev, 0x09, 0x0f, 0x60))
272                                         dprintk(1, "setting band in demodulator failed.\n");
273                         } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
274                                 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // TDA9819 pin9(STD)
275                                 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI); // TDA9819 pin30(VIF)
276                         }
277                         if (i2c_writereg(av7110, 0x48, 0x02, 0xd0) != 1)
278                                 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
279                         break;
280                 case 2:
281                         dprintk(1, "switching SAA7113 to Video AV CVBS Input.\n");
282                         if (i2c_writereg(av7110, 0x48, 0x02, 0xd2) != 1)
283                                 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
284                         break;
285                 case 3:
286                         dprintk(1, "switching SAA7113 to Video AV Y/C Input.\n");
287                         if (i2c_writereg(av7110, 0x48, 0x02, 0xd9) != 1)
288                                 dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
289                         break;
290                 default:
291                         dprintk(1, "switching SAA7113 to Input: AV7110: SAA7113: invalid input.\n");
292                 }
293         } else {
294                 adswitch = 0;
295                 source = SAA7146_HPS_SOURCE_PORT_A;
296                 sync = SAA7146_HPS_SYNC_PORT_A;
297                 memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
298                 dprintk(1, "switching DVB mode\n");
299                 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
300                 msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
301                 msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
302                 msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
303                 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
304                 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
305
306                 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
307                         if (ves1820_writereg(dev, 0x09, 0x0f, 0x20))
308                                 dprintk(1, "setting band in demodulator failed.\n");
309                 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
310                         saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
311                         saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
312                 }
313         }
314
315         /* hmm, this does not do anything!? */
316         if (av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, adswitch))
317                 dprintk(1, "ADSwitch error\n");
318
319         saa7146_set_hps_source_and_sync(dev, source, sync);
320
321         if (vv->ov_suspend != NULL) {
322                 saa7146_start_preview(vv->ov_suspend);
323                 vv->ov_suspend = NULL;
324         }
325
326         return 0;
327 }
328
329 static int av7110_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
330 {
331         struct saa7146_dev *dev = fh->dev;
332         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
333         dprintk(4, "saa7146_dev: %p\n", dev);
334
335         switch (cmd) {
336         case VIDIOC_G_TUNER:
337         {
338                 struct v4l2_tuner *t = arg;
339                 u16 stereo_det;
340                 s8 stereo;
341
342                 dprintk(2, "VIDIOC_G_TUNER: %d\n", t->index);
343
344                 if (!av7110->analog_tuner_flags || t->index != 0)
345                         return -EINVAL;
346
347                 memset(t, 0, sizeof(*t));
348                 strcpy(t->name, "Television");
349
350                 t->type = V4L2_TUNER_ANALOG_TV;
351                 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
352                         V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
353                 t->rangelow = 772;      /* 48.25 MHZ / 62.5 kHz = 772, see fi1216mk2-specs, page 2 */
354                 t->rangehigh = 13684;   /* 855.25 MHz / 62.5 kHz = 13684 */
355                 /* FIXME: add the real signal strength here */
356                 t->signal = 0xffff;
357                 t->afc = 0;
358
359                 // FIXME: standard / stereo detection is still broken
360                 msp_readreg(av7110, MSP_RD_DEM, 0x007e, &stereo_det);
361                 dprintk(1, "VIDIOC_G_TUNER: msp3400 TV standard detection: 0x%04x\n", stereo_det);
362                 msp_readreg(av7110, MSP_RD_DSP, 0x0018, &stereo_det);
363                 dprintk(1, "VIDIOC_G_TUNER: msp3400 stereo detection: 0x%04x\n", stereo_det);
364                 stereo = (s8)(stereo_det >> 8);
365                 if (stereo > 0x10) {
366                         /* stereo */
367                         t->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
368                         t->audmode = V4L2_TUNER_MODE_STEREO;
369                 }
370                 else if (stereo < -0x10) {
371                         /* bilingual */
372                         t->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
373                         t->audmode = V4L2_TUNER_MODE_LANG1;
374                 }
375                 else /* mono */
376                         t->rxsubchans = V4L2_TUNER_SUB_MONO;
377
378                 return 0;
379         }
380         case VIDIOC_S_TUNER:
381         {
382                 struct v4l2_tuner *t = arg;
383                 u16 fm_matrix, src;
384                 dprintk(2, "VIDIOC_S_TUNER: %d\n", t->index);
385
386                 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
387                         return -EINVAL;
388
389                 switch (t->audmode) {
390                 case V4L2_TUNER_MODE_STEREO:
391                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_STEREO\n");
392                         fm_matrix = 0x3001; // stereo
393                         src = 0x0020;
394                         break;
395                 case V4L2_TUNER_MODE_LANG1_LANG2:
396                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1_LANG2\n");
397                         fm_matrix = 0x3000; // bilingual
398                         src = 0x0020;
399                         break;
400                 case V4L2_TUNER_MODE_LANG1:
401                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1\n");
402                         fm_matrix = 0x3000; // mono
403                         src = 0x0000;
404                         break;
405                 case V4L2_TUNER_MODE_LANG2:
406                         dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG2\n");
407                         fm_matrix = 0x3000; // mono
408                         src = 0x0010;
409                         break;
410                 default: /* case V4L2_TUNER_MODE_MONO: */
411                         dprintk(2, "VIDIOC_S_TUNER: TDA9840_SET_MONO\n");
412                         fm_matrix = 0x3000; // mono
413                         src = 0x0030;
414                         break;
415                 }
416                 msp_writereg(av7110, MSP_WR_DSP, 0x000e, fm_matrix);
417                 msp_writereg(av7110, MSP_WR_DSP, 0x0008, src);
418                 msp_writereg(av7110, MSP_WR_DSP, 0x0009, src);
419                 msp_writereg(av7110, MSP_WR_DSP, 0x000a, src);
420                 return 0;
421         }
422         case VIDIOC_G_FREQUENCY:
423         {
424                 struct v4l2_frequency *f = arg;
425
426                 dprintk(2, "VIDIOC_G_FREQ: freq:0x%08x.\n", f->frequency);
427
428                 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
429                         return -EINVAL;
430
431                 memset(f, 0, sizeof(*f));
432                 f->type = V4L2_TUNER_ANALOG_TV;
433                 f->frequency =  av7110->current_freq;
434                 return 0;
435         }
436         case VIDIOC_S_FREQUENCY:
437         {
438                 struct v4l2_frequency *f = arg;
439
440                 dprintk(2, "VIDIOC_S_FREQUENCY: freq:0x%08x.\n", f->frequency);
441
442                 if (!av7110->analog_tuner_flags || av7110->current_input != 1)
443                         return -EINVAL;
444
445                 if (V4L2_TUNER_ANALOG_TV != f->type)
446                         return -EINVAL;
447
448                 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0xffe0); // fast mute
449                 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0xffe0);
450
451                 /* tune in desired frequency */
452                 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
453                         ves1820_set_tv_freq(dev, f->frequency);
454                 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
455                         stv0297_set_tv_freq(dev, f->frequency);
456                 }
457                 av7110->current_freq = f->frequency;
458
459                 msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x003f); // start stereo detection
460                 msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x0000);
461                 msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); // loudspeaker + headphone
462                 msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); // SCART 1 volume
463                 return 0;
464         }
465         case VIDIOC_ENUMINPUT:
466         {
467                 struct v4l2_input *i = arg;
468
469                 dprintk(2, "VIDIOC_ENUMINPUT: %d\n", i->index);
470
471                 if (av7110->analog_tuner_flags) {
472                         if (i->index < 0 || i->index >= 4)
473                                 return -EINVAL;
474                 } else {
475                         if (i->index != 0)
476                                 return -EINVAL;
477                 }
478
479                 memcpy(i, &inputs[i->index], sizeof(struct v4l2_input));
480
481                 return 0;
482         }
483         case VIDIOC_G_INPUT:
484         {
485                 int *input = (int *)arg;
486                 *input = av7110->current_input;
487                 dprintk(2, "VIDIOC_G_INPUT: %d\n", *input);
488                 return 0;
489         }
490         case VIDIOC_S_INPUT:
491         {
492                 int input = *(int *)arg;
493
494                 dprintk(2, "VIDIOC_S_INPUT: %d\n", input);
495
496                 if (!av7110->analog_tuner_flags)
497                         return 0;
498
499                 if (input < 0 || input >= 4)
500                         return -EINVAL;
501
502                 av7110->current_input = input;
503                 return av7110_dvb_c_switch(fh);
504         }
505         case VIDIOC_G_AUDIO:
506         {
507                 struct v4l2_audio *a = arg;
508
509                 dprintk(2, "VIDIOC_G_AUDIO: %d\n", a->index);
510                 if (a->index != 0)
511                         return -EINVAL;
512                 memcpy(a, &msp3400_v4l2_audio, sizeof(struct v4l2_audio));
513                 break;
514         }
515         case VIDIOC_S_AUDIO:
516         {
517                 struct v4l2_audio *a = arg;
518                 dprintk(2, "VIDIOC_S_AUDIO: %d\n", a->index);
519                 break;
520         }
521         case VIDIOC_G_SLICED_VBI_CAP:
522         {
523                 struct v4l2_sliced_vbi_cap *cap = arg;
524                 dprintk(2, "VIDIOC_G_SLICED_VBI_CAP\n");
525                 memset(cap, 0, sizeof *cap);
526                 if (FW_VERSION(av7110->arm_app) >= 0x2623) {
527                         cap->service_set = V4L2_SLICED_WSS_625;
528                         cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
529                 }
530                 break;
531         }
532         case VIDIOC_G_FMT:
533         {
534                 struct v4l2_format *f = arg;
535                 dprintk(2, "VIDIOC_G_FMT:\n");
536                 if (f->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ||
537                     FW_VERSION(av7110->arm_app) < 0x2623)
538                         return -EAGAIN; /* handled by core driver */
539                 memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
540                 if (av7110->wssMode) {
541                         f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
542                         f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
543                         f->fmt.sliced.io_size = sizeof (struct v4l2_sliced_vbi_data);
544                 }
545                 break;
546         }
547         case VIDIOC_S_FMT:
548         {
549                 struct v4l2_format *f = arg;
550                 dprintk(2, "VIDIOC_S_FMT\n");
551                 if (f->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ||
552                     FW_VERSION(av7110->arm_app) < 0x2623)
553                         return -EAGAIN; /* handled by core driver */
554                 if (f->fmt.sliced.service_set != V4L2_SLICED_WSS_625 &&
555                     f->fmt.sliced.service_lines[0][23] != V4L2_SLICED_WSS_625) {
556                         memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
557                         /* WSS controlled by firmware */
558                         av7110->wssMode = 0;
559                         av7110->wssData = 0;
560                         return av7110_fw_cmd(av7110, COMTYPE_ENCODER,
561                                              SetWSSConfig, 1, 0);
562                 } else {
563                         memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
564                         f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
565                         f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
566                         f->fmt.sliced.io_size = sizeof (struct v4l2_sliced_vbi_data);
567                         /* WSS controlled by userspace */
568                         av7110->wssMode = 1;
569                         av7110->wssData = 0;
570                 }
571                 break;
572         }
573         default:
574                 printk("no such ioctl\n");
575                 return -ENOIOCTLCMD;
576         }
577         return 0;
578 }
579
580 static int av7110_vbi_reset(struct inode *inode, struct file *file)
581 {
582         struct saa7146_fh *fh = file->private_data;
583         struct saa7146_dev *dev = fh->dev;
584         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
585
586         dprintk(2, "%s\n", __FUNCTION__);
587         av7110->wssMode = 0;
588         av7110->wssData = 0;
589         if (FW_VERSION(av7110->arm_app) < 0x2623)
590                 return 0;
591         else
592                 return av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 1, 0);
593 }
594
595 static ssize_t av7110_vbi_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
596 {
597         struct saa7146_fh *fh = file->private_data;
598         struct saa7146_dev *dev = fh->dev;
599         struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
600         struct v4l2_sliced_vbi_data d;
601         int rc;
602
603         dprintk(2, "%s\n", __FUNCTION__);
604         if (FW_VERSION(av7110->arm_app) < 0x2623 || !av7110->wssMode || count != sizeof d)
605                 return -EINVAL;
606         if (copy_from_user(&d, data, count))
607                 return -EFAULT;
608         if ((d.id != 0 && d.id != V4L2_SLICED_WSS_625) || d.field != 0 || d.line != 23)
609                 return -EINVAL;
610         if (d.id)
611                 av7110->wssData = ((d.data[1] << 8) & 0x3f00) | d.data[0];
612         else
613                 av7110->wssData = 0x8000;
614         rc = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 1, av7110->wssData);
615         return (rc < 0) ? rc : count;
616 }
617
618 /****************************************************************************
619  * INITIALIZATION
620  ****************************************************************************/
621
622 static struct saa7146_extension_ioctls ioctls[] = {
623         { VIDIOC_ENUMINPUT,     SAA7146_EXCLUSIVE },
624         { VIDIOC_G_INPUT,       SAA7146_EXCLUSIVE },
625         { VIDIOC_S_INPUT,       SAA7146_EXCLUSIVE },
626         { VIDIOC_G_FREQUENCY,   SAA7146_EXCLUSIVE },
627         { VIDIOC_S_FREQUENCY,   SAA7146_EXCLUSIVE },
628         { VIDIOC_G_TUNER,       SAA7146_EXCLUSIVE },
629         { VIDIOC_S_TUNER,       SAA7146_EXCLUSIVE },
630         { VIDIOC_G_AUDIO,       SAA7146_EXCLUSIVE },
631         { VIDIOC_S_AUDIO,       SAA7146_EXCLUSIVE },
632         { VIDIOC_G_SLICED_VBI_CAP, SAA7146_EXCLUSIVE },
633         { VIDIOC_G_FMT,         SAA7146_BEFORE },
634         { VIDIOC_S_FMT,         SAA7146_BEFORE },
635         { 0, 0 }
636 };
637
638 static u8 saa7113_init_regs[] = {
639         0x02, 0xd0,
640         0x03, 0x23,
641         0x04, 0x00,
642         0x05, 0x00,
643         0x06, 0xe9,
644         0x07, 0x0d,
645         0x08, 0x98,
646         0x09, 0x02,
647         0x0a, 0x80,
648         0x0b, 0x40,
649         0x0c, 0x40,
650         0x0d, 0x00,
651         0x0e, 0x01,
652         0x0f, 0x7c,
653         0x10, 0x48,
654         0x11, 0x0c,
655         0x12, 0x8b,
656         0x13, 0x1a,
657         0x14, 0x00,
658         0x15, 0x00,
659         0x16, 0x00,
660         0x17, 0x00,
661         0x18, 0x00,
662         0x19, 0x00,
663         0x1a, 0x00,
664         0x1b, 0x00,
665         0x1c, 0x00,
666         0x1d, 0x00,
667         0x1e, 0x00,
668
669         0x41, 0x77,
670         0x42, 0x77,
671         0x43, 0x77,
672         0x44, 0x77,
673         0x45, 0x77,
674         0x46, 0x77,
675         0x47, 0x77,
676         0x48, 0x77,
677         0x49, 0x77,
678         0x4a, 0x77,
679         0x4b, 0x77,
680         0x4c, 0x77,
681         0x4d, 0x77,
682         0x4e, 0x77,
683         0x4f, 0x77,
684         0x50, 0x77,
685         0x51, 0x77,
686         0x52, 0x77,
687         0x53, 0x77,
688         0x54, 0x77,
689         0x55, 0x77,
690         0x56, 0x77,
691         0x57, 0xff,
692
693         0xff
694 };
695
696
697 static struct saa7146_ext_vv av7110_vv_data_st;
698 static struct saa7146_ext_vv av7110_vv_data_c;
699
700 int av7110_init_analog_module(struct av7110 *av7110)
701 {
702         u16 version1, version2;
703
704         if (i2c_writereg(av7110, 0x80, 0x0, 0x80) == 1 &&
705             i2c_writereg(av7110, 0x80, 0x0, 0) == 1) {
706                 printk("dvb-ttpci: DVB-C analog module @ card %d detected, initializing MSP3400\n",
707                         av7110->dvb_adapter.num);
708                 av7110->adac_type = DVB_ADAC_MSP34x0;
709         } else if (i2c_writereg(av7110, 0x84, 0x0, 0x80) == 1 &&
710                    i2c_writereg(av7110, 0x84, 0x0, 0) == 1) {
711                 printk("dvb-ttpci: DVB-C analog module @ card %d detected, initializing MSP3415\n",
712                         av7110->dvb_adapter.num);
713                 av7110->adac_type = DVB_ADAC_MSP34x5;
714         } else
715                 return -ENODEV;
716
717         msleep(100); // the probing above resets the msp...
718         msp_readreg(av7110, MSP_RD_DSP, 0x001e, &version1);
719         msp_readreg(av7110, MSP_RD_DSP, 0x001f, &version2);
720         dprintk(1, "dvb-ttpci: @ card %d MSP34xx version 0x%04x 0x%04x\n",
721                 av7110->dvb_adapter.num, version1, version2);
722         msp_writereg(av7110, MSP_WR_DSP, 0x0013, 0x0c00);
723         msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
724         msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
725         msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
726         msp_writereg(av7110, MSP_WR_DSP, 0x0004, 0x7f00); // loudspeaker volume
727         msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
728         msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
729         msp_writereg(av7110, MSP_WR_DSP, 0x000d, 0x1900); // prescale SCART
730
731         if (i2c_writereg(av7110, 0x48, 0x01, 0x00)!=1) {
732                 INFO(("saa7113 not accessible.\n"));
733         } else {
734                 u8 *i = saa7113_init_regs;
735
736                 if ((av7110->dev->pci->subsystem_vendor == 0x110a) && (av7110->dev->pci->subsystem_device == 0x0000)) {
737                         /* Fujitsu/Siemens DVB-Cable */
738                         av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
739                 } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x0002)) {
740                         /* Hauppauge/TT DVB-C premium */
741                         av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
742                 } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x000A)) {
743                         /* Hauppauge/TT DVB-C premium */
744                         av7110->analog_tuner_flags |= ANALOG_TUNER_STV0297;
745                 }
746
747                 /* setup for DVB by default */
748                 if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
749                         if (ves1820_writereg(av7110->dev, 0x09, 0x0f, 0x20))
750                                 dprintk(1, "setting band in demodulator failed.\n");
751                 } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
752                         saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
753                         saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
754                 }
755
756                 /* init the saa7113 */
757                 while (*i != 0xff) {
758                         if (i2c_writereg(av7110, 0x48, i[0], i[1]) != 1) {
759                                 dprintk(1, "saa7113 initialization failed @ card %d", av7110->dvb_adapter.num);
760                                 break;
761                         }
762                         i += 2;
763                 }
764                 /* setup msp for analog sound: B/G Dual-FM */
765                 msp_writereg(av7110, MSP_WR_DEM, 0x00bb, 0x02d0); // AD_CV
766                 msp_writereg(av7110, MSP_WR_DEM, 0x0001,  3); // FIR1
767                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 18); // FIR1
768                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 27); // FIR1
769                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 48); // FIR1
770                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 66); // FIR1
771                 msp_writereg(av7110, MSP_WR_DEM, 0x0001, 72); // FIR1
772                 msp_writereg(av7110, MSP_WR_DEM, 0x0005,  4); // FIR2
773                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 64); // FIR2
774                 msp_writereg(av7110, MSP_WR_DEM, 0x0005,  0); // FIR2
775                 msp_writereg(av7110, MSP_WR_DEM, 0x0005,  3); // FIR2
776                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 18); // FIR2
777                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 27); // FIR2
778                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 48); // FIR2
779                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 66); // FIR2
780                 msp_writereg(av7110, MSP_WR_DEM, 0x0005, 72); // FIR2
781                 msp_writereg(av7110, MSP_WR_DEM, 0x0083, 0xa000); // MODE_REG
782                 msp_writereg(av7110, MSP_WR_DEM, 0x0093, 0x00aa); // DCO1_LO 5.74MHz
783                 msp_writereg(av7110, MSP_WR_DEM, 0x009b, 0x04fc); // DCO1_HI
784                 msp_writereg(av7110, MSP_WR_DEM, 0x00a3, 0x038e); // DCO2_LO 5.5MHz
785                 msp_writereg(av7110, MSP_WR_DEM, 0x00ab, 0x04c6); // DCO2_HI
786                 msp_writereg(av7110, MSP_WR_DEM, 0x0056, 0); // LOAD_REG 1/2
787         }
788
789         memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
790         /* set dd1 stream a & b */
791         saa7146_write(av7110->dev, DD1_STREAM_B, 0x00000000);
792         saa7146_write(av7110->dev, DD1_INIT, 0x03000700);
793         saa7146_write(av7110->dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
794
795         return 0;
796 }
797
798 int av7110_init_v4l(struct av7110 *av7110)
799 {
800         struct saa7146_dev* dev = av7110->dev;
801         int ret;
802
803         /* special case DVB-C: these cards have an analog tuner
804            plus need some special handling, so we have separate
805            saa7146_ext_vv data for these... */
806         if (av7110->analog_tuner_flags)
807                 ret = saa7146_vv_init(dev, &av7110_vv_data_c);
808         else
809                 ret = saa7146_vv_init(dev, &av7110_vv_data_st);
810
811         if (ret) {
812                 ERR(("cannot init capture device. skipping.\n"));
813                 return -ENODEV;
814         }
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         } else {
824                 if (av7110->analog_tuner_flags)
825                         av7110->analog_tuner_flags |= ANALOG_TUNER_VBI;
826         }
827         return 0;
828 }
829
830 int av7110_exit_v4l(struct av7110 *av7110)
831 {
832         saa7146_unregister_device(&av7110->v4l_dev, av7110->dev);
833         if (av7110->analog_tuner_flags & ANALOG_TUNER_VBI)
834                 saa7146_unregister_device(&av7110->vbi_dev, av7110->dev);
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) */
842 static 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
856 static 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
870 static 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
884 static 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 = VIDEO_MODE_PAL;
890                 av7110_set_vidmode(av7110, av7110->vidmode);
891         }
892         else if (std->id & V4L2_STD_NTSC) {
893                 av7110->vidmode = VIDEO_MODE_NTSC;
894                 av7110_set_vidmode(av7110, av7110->vidmode);
895         }
896         else
897                 return -1;
898
899         return 0;
900 }
901
902
903 static 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         .ioctls         = &ioctls[0],
914         .ioctl          = av7110_ioctl,
915
916         .vbi_fops.open  = av7110_vbi_reset,
917         .vbi_fops.release = av7110_vbi_reset,
918         .vbi_fops.write = av7110_vbi_write,
919 };
920
921 static struct saa7146_ext_vv av7110_vv_data_c = {
922         .inputs         = 1,
923         .audios         = 1,
924         .capabilities   = V4L2_CAP_TUNER | V4L2_CAP_SLICED_VBI_OUTPUT,
925         .flags          = SAA7146_USE_PORT_B_FOR_VBI,
926
927         .stds           = &standard[0],
928         .num_stds       = ARRAY_SIZE(standard),
929         .std_callback   = &std_callback,
930
931         .ioctls         = &ioctls[0],
932         .ioctl          = av7110_ioctl,
933
934         .vbi_fops.open  = av7110_vbi_reset,
935         .vbi_fops.release = av7110_vbi_reset,
936         .vbi_fops.write = av7110_vbi_write,
937 };
938