V4L/DVB (11855): cx23885: Ensure we specify I/F's for all bandwidths
[safe/jmp/linux-2.6] / drivers / media / video / cx23885 / cx23885-dvb.c
1 /*
2  *  Driver for the Conexant CX23885 PCIe bridge
3  *
4  *  Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/device.h>
25 #include <linux/fs.h>
26 #include <linux/kthread.h>
27 #include <linux/file.h>
28 #include <linux/suspend.h>
29
30 #include "cx23885.h"
31 #include <media/v4l2-common.h>
32
33 #include "dvb_ca_en50221.h"
34 #include "s5h1409.h"
35 #include "s5h1411.h"
36 #include "mt2131.h"
37 #include "tda8290.h"
38 #include "tda18271.h"
39 #include "lgdt330x.h"
40 #include "xc5000.h"
41 #include "tda10048.h"
42 #include "tuner-xc2028.h"
43 #include "tuner-simple.h"
44 #include "dib7000p.h"
45 #include "dibx000_common.h"
46 #include "zl10353.h"
47 #include "stv0900.h"
48 #include "stv6110.h"
49 #include "lnbh24.h"
50 #include "cx24116.h"
51 #include "cimax2.h"
52 #include "netup-eeprom.h"
53 #include "netup-init.h"
54 #include "lgdt3305.h"
55
56 static unsigned int debug;
57
58 #define dprintk(level, fmt, arg...)\
59         do { if (debug >= level)\
60                 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
61         } while (0)
62
63 /* ------------------------------------------------------------------ */
64
65 static unsigned int alt_tuner;
66 module_param(alt_tuner, int, 0644);
67 MODULE_PARM_DESC(alt_tuner, "Enable alternate tuner configuration");
68
69 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
70
71 /* ------------------------------------------------------------------ */
72
73 static int dvb_buf_setup(struct videobuf_queue *q,
74                          unsigned int *count, unsigned int *size)
75 {
76         struct cx23885_tsport *port = q->priv_data;
77
78         port->ts_packet_size  = 188 * 4;
79         port->ts_packet_count = 32;
80
81         *size  = port->ts_packet_size * port->ts_packet_count;
82         *count = 32;
83         return 0;
84 }
85
86 static int dvb_buf_prepare(struct videobuf_queue *q,
87                            struct videobuf_buffer *vb, enum v4l2_field field)
88 {
89         struct cx23885_tsport *port = q->priv_data;
90         return cx23885_buf_prepare(q, port, (struct cx23885_buffer *)vb, field);
91 }
92
93 static void dvb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
94 {
95         struct cx23885_tsport *port = q->priv_data;
96         cx23885_buf_queue(port, (struct cx23885_buffer *)vb);
97 }
98
99 static void dvb_buf_release(struct videobuf_queue *q,
100                             struct videobuf_buffer *vb)
101 {
102         cx23885_free_buffer(q, (struct cx23885_buffer *)vb);
103 }
104
105 static struct videobuf_queue_ops dvb_qops = {
106         .buf_setup    = dvb_buf_setup,
107         .buf_prepare  = dvb_buf_prepare,
108         .buf_queue    = dvb_buf_queue,
109         .buf_release  = dvb_buf_release,
110 };
111
112 static struct s5h1409_config hauppauge_generic_config = {
113         .demod_address = 0x32 >> 1,
114         .output_mode   = S5H1409_SERIAL_OUTPUT,
115         .gpio          = S5H1409_GPIO_ON,
116         .qam_if        = 44000,
117         .inversion     = S5H1409_INVERSION_OFF,
118         .status_mode   = S5H1409_DEMODLOCKING,
119         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
120 };
121
122 static struct tda10048_config hauppauge_hvr1200_config = {
123         .demod_address    = 0x10 >> 1,
124         .output_mode      = TDA10048_SERIAL_OUTPUT,
125         .fwbulkwritelen   = TDA10048_BULKWRITE_200,
126         .inversion        = TDA10048_INVERSION_ON,
127         .dtv6_if_freq_khz = TDA10048_IF_3300,
128         .dtv7_if_freq_khz = TDA10048_IF_3800,
129         .dtv8_if_freq_khz = TDA10048_IF_4300,
130         .clk_freq_khz     = TDA10048_CLK_16000,
131 };
132
133 static struct tda10048_config hauppauge_hvr1210_config = {
134         .demod_address    = 0x10 >> 1,
135         .output_mode      = TDA10048_SERIAL_OUTPUT,
136         .fwbulkwritelen   = TDA10048_BULKWRITE_200,
137         .inversion        = TDA10048_INVERSION_ON,
138         .if_freq_khz      = TDA10048_IF_4000,
139         .clk_freq_khz     = TDA10048_CLK_16000,
140 };
141
142 static struct s5h1409_config hauppauge_ezqam_config = {
143         .demod_address = 0x32 >> 1,
144         .output_mode   = S5H1409_SERIAL_OUTPUT,
145         .gpio          = S5H1409_GPIO_OFF,
146         .qam_if        = 4000,
147         .inversion     = S5H1409_INVERSION_ON,
148         .status_mode   = S5H1409_DEMODLOCKING,
149         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
150 };
151
152 static struct s5h1409_config hauppauge_hvr1800lp_config = {
153         .demod_address = 0x32 >> 1,
154         .output_mode   = S5H1409_SERIAL_OUTPUT,
155         .gpio          = S5H1409_GPIO_OFF,
156         .qam_if        = 44000,
157         .inversion     = S5H1409_INVERSION_OFF,
158         .status_mode   = S5H1409_DEMODLOCKING,
159         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
160 };
161
162 static struct s5h1409_config hauppauge_hvr1500_config = {
163         .demod_address = 0x32 >> 1,
164         .output_mode   = S5H1409_SERIAL_OUTPUT,
165         .gpio          = S5H1409_GPIO_OFF,
166         .inversion     = S5H1409_INVERSION_OFF,
167         .status_mode   = S5H1409_DEMODLOCKING,
168         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
169 };
170
171 static struct mt2131_config hauppauge_generic_tunerconfig = {
172         0x61
173 };
174
175 static struct lgdt330x_config fusionhdtv_5_express = {
176         .demod_address = 0x0e,
177         .demod_chip = LGDT3303,
178         .serial_mpeg = 0x40,
179 };
180
181 static struct s5h1409_config hauppauge_hvr1500q_config = {
182         .demod_address = 0x32 >> 1,
183         .output_mode   = S5H1409_SERIAL_OUTPUT,
184         .gpio          = S5H1409_GPIO_ON,
185         .qam_if        = 44000,
186         .inversion     = S5H1409_INVERSION_OFF,
187         .status_mode   = S5H1409_DEMODLOCKING,
188         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
189 };
190
191 static struct s5h1409_config dvico_s5h1409_config = {
192         .demod_address = 0x32 >> 1,
193         .output_mode   = S5H1409_SERIAL_OUTPUT,
194         .gpio          = S5H1409_GPIO_ON,
195         .qam_if        = 44000,
196         .inversion     = S5H1409_INVERSION_OFF,
197         .status_mode   = S5H1409_DEMODLOCKING,
198         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
199 };
200
201 static struct s5h1411_config dvico_s5h1411_config = {
202         .output_mode   = S5H1411_SERIAL_OUTPUT,
203         .gpio          = S5H1411_GPIO_ON,
204         .qam_if        = S5H1411_IF_44000,
205         .vsb_if        = S5H1411_IF_44000,
206         .inversion     = S5H1411_INVERSION_OFF,
207         .status_mode   = S5H1411_DEMODLOCKING,
208         .mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
209 };
210
211 static struct s5h1411_config hcw_s5h1411_config = {
212         .output_mode   = S5H1411_SERIAL_OUTPUT,
213         .gpio          = S5H1411_GPIO_OFF,
214         .vsb_if        = S5H1411_IF_44000,
215         .qam_if        = S5H1411_IF_4000,
216         .inversion     = S5H1411_INVERSION_ON,
217         .status_mode   = S5H1411_DEMODLOCKING,
218         .mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
219 };
220
221 static struct xc5000_config hauppauge_hvr1500q_tunerconfig = {
222         .i2c_address      = 0x61,
223         .if_khz           = 5380,
224 };
225
226 static struct xc5000_config dvico_xc5000_tunerconfig = {
227         .i2c_address      = 0x64,
228         .if_khz           = 5380,
229 };
230
231 static struct tda829x_config tda829x_no_probe = {
232         .probe_tuner = TDA829X_DONT_PROBE,
233 };
234
235 static struct tda18271_std_map hauppauge_tda18271_std_map = {
236         .atsc_6   = { .if_freq = 5380, .agc_mode = 3, .std = 3,
237                       .if_lvl = 6, .rfagc_top = 0x37 },
238         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 0,
239                       .if_lvl = 6, .rfagc_top = 0x37 },
240 };
241
242 static struct tda18271_config hauppauge_tda18271_config = {
243         .std_map = &hauppauge_tda18271_std_map,
244         .gate    = TDA18271_GATE_ANALOG,
245 };
246
247 static struct tda18271_config hauppauge_hvr1200_tuner_config = {
248         .gate    = TDA18271_GATE_ANALOG,
249 };
250
251 static struct tda18271_config hauppauge_hvr1210_tuner_config = {
252         .gate    = TDA18271_GATE_DIGITAL,
253 };
254
255 static struct tda18271_std_map hauppauge_hvr127x_std_map = {
256         .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
257                       .if_lvl = 1, .rfagc_top = 0x58 },
258         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
259                       .if_lvl = 1, .rfagc_top = 0x58 },
260 };
261
262 static struct tda18271_config hauppauge_hvr127x_config = {
263         .std_map = &hauppauge_hvr127x_std_map,
264 };
265
266 static struct lgdt3305_config hauppauge_lgdt3305_config = {
267         .i2c_addr           = 0x0e,
268         .mpeg_mode          = LGDT3305_MPEG_SERIAL,
269         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
270         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
271         .deny_i2c_rptr      = 1,
272         .spectral_inversion = 1,
273         .qam_if_khz         = 4000,
274         .vsb_if_khz         = 3250,
275 };
276
277 static struct dibx000_agc_config xc3028_agc_config = {
278         BAND_VHF | BAND_UHF,    /* band_caps */
279
280         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=0,
281          * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
282          * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0,
283          * P_agc_nb_est=2, P_agc_write=0
284          */
285         (0 << 15) | (0 << 14) | (0 << 11) | (0 << 10) | (0 << 9) | (0 << 8) |
286                 (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0), /* setup */
287
288         712,    /* inv_gain */
289         21,     /* time_stabiliz */
290
291         0,      /* alpha_level */
292         118,    /* thlock */
293
294         0,      /* wbd_inv */
295         2867,   /* wbd_ref */
296         0,      /* wbd_sel */
297         2,      /* wbd_alpha */
298
299         0,      /* agc1_max */
300         0,      /* agc1_min */
301         39718,  /* agc2_max */
302         9930,   /* agc2_min */
303         0,      /* agc1_pt1 */
304         0,      /* agc1_pt2 */
305         0,      /* agc1_pt3 */
306         0,      /* agc1_slope1 */
307         0,      /* agc1_slope2 */
308         0,      /* agc2_pt1 */
309         128,    /* agc2_pt2 */
310         29,     /* agc2_slope1 */
311         29,     /* agc2_slope2 */
312
313         17,     /* alpha_mant */
314         27,     /* alpha_exp */
315         23,     /* beta_mant */
316         51,     /* beta_exp */
317
318         1,      /* perform_agc_softsplit */
319 };
320
321 /* PLL Configuration for COFDM BW_MHz = 8.000000
322  * With external clock = 30.000000 */
323 static struct dibx000_bandwidth_config xc3028_bw_config = {
324         60000,  /* internal */
325         30000,  /* sampling */
326         1,      /* pll_cfg: prediv */
327         8,      /* pll_cfg: ratio */
328         3,      /* pll_cfg: range */
329         1,      /* pll_cfg: reset */
330         0,      /* pll_cfg: bypass */
331         0,      /* misc: refdiv */
332         0,      /* misc: bypclk_div */
333         1,      /* misc: IO_CLK_en_core */
334         1,      /* misc: ADClkSrc */
335         0,      /* misc: modulo */
336         (3 << 14) | (1 << 12) | (524 << 0), /* sad_cfg: refsel, sel, freq_15k */
337         (1 << 25) | 5816102, /* ifreq = 5.200000 MHz */
338         20452225, /* timf */
339         30000000  /* xtal_hz */
340 };
341
342 static struct dib7000p_config hauppauge_hvr1400_dib7000_config = {
343         .output_mpeg2_in_188_bytes = 1,
344         .hostbus_diversity = 1,
345         .tuner_is_baseband = 0,
346         .update_lna  = NULL,
347
348         .agc_config_count = 1,
349         .agc = &xc3028_agc_config,
350         .bw  = &xc3028_bw_config,
351
352         .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
353         .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
354         .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
355
356         .pwm_freq_div = 0,
357         .agc_control  = NULL,
358         .spur_protect = 0,
359
360         .output_mode = OUTMODE_MPEG2_SERIAL,
361 };
362
363 static struct zl10353_config dvico_fusionhdtv_xc3028 = {
364         .demod_address = 0x0f,
365         .if2           = 45600,
366         .no_tuner      = 1,
367         .disable_i2c_gate_ctrl = 1,
368 };
369
370 static struct stv0900_config netup_stv0900_config = {
371         .demod_address = 0x68,
372         .xtal = 27000000,
373         .clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
374         .diseqc_mode = 2,/* 2/3 PWM */
375         .path1_mode = 2,/*Serial continues clock */
376         .path2_mode = 2,/*Serial continues clock */
377         .tun1_maddress = 0,/* 0x60 */
378         .tun2_maddress = 3,/* 0x63 */
379         .tun1_adc = 1,/* 1 Vpp */
380         .tun2_adc = 1,/* 1 Vpp */
381 };
382
383 static struct stv6110_config netup_stv6110_tunerconfig_a = {
384         .i2c_address = 0x60,
385         .mclk = 27000000,
386         .iq_wiring = 0,
387 };
388
389 static struct stv6110_config netup_stv6110_tunerconfig_b = {
390         .i2c_address = 0x63,
391         .mclk = 27000000,
392         .iq_wiring = 1,
393 };
394
395 static int tbs_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
396 {
397         struct cx23885_tsport *port = fe->dvb->priv;
398         struct cx23885_dev *dev = port->dev;
399
400         if (voltage == SEC_VOLTAGE_18)
401                 cx_write(MC417_RWD, 0x00001e00);/* GPIO-13 high */
402         else if (voltage == SEC_VOLTAGE_13)
403                 cx_write(MC417_RWD, 0x00001a00);/* GPIO-13 low */
404         else
405                 cx_write(MC417_RWD, 0x00001800);/* GPIO-12 low */
406         return 0;
407 }
408
409 static struct cx24116_config tbs_cx24116_config = {
410         .demod_address = 0x05,
411 };
412
413 static struct cx24116_config tevii_cx24116_config = {
414         .demod_address = 0x55,
415 };
416
417 static struct cx24116_config dvbworld_cx24116_config = {
418         .demod_address = 0x05,
419 };
420
421 static int dvb_register(struct cx23885_tsport *port)
422 {
423         struct cx23885_dev *dev = port->dev;
424         struct cx23885_i2c *i2c_bus = NULL;
425         struct videobuf_dvb_frontend *fe0;
426         int ret;
427
428         /* Get the first frontend */
429         fe0 = videobuf_dvb_get_frontend(&port->frontends, 1);
430         if (!fe0)
431                 return -EINVAL;
432
433         /* init struct videobuf_dvb */
434         fe0->dvb.name = dev->name;
435
436         /* init frontend */
437         switch (dev->board) {
438         case CX23885_BOARD_HAUPPAUGE_HVR1250:
439                 i2c_bus = &dev->i2c_bus[0];
440                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
441                                                 &hauppauge_generic_config,
442                                                 &i2c_bus->i2c_adap);
443                 if (fe0->dvb.frontend != NULL) {
444                         dvb_attach(mt2131_attach, fe0->dvb.frontend,
445                                    &i2c_bus->i2c_adap,
446                                    &hauppauge_generic_tunerconfig, 0);
447                 }
448                 break;
449         case CX23885_BOARD_HAUPPAUGE_HVR1270:
450         case CX23885_BOARD_HAUPPAUGE_HVR1275:
451                 i2c_bus = &dev->i2c_bus[0];
452                 fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
453                                                &hauppauge_lgdt3305_config,
454                                                &i2c_bus->i2c_adap);
455                 if (fe0->dvb.frontend != NULL) {
456                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
457                                    0x60, &dev->i2c_bus[1].i2c_adap,
458                                    &hauppauge_hvr127x_config);
459                 }
460                 break;
461         case CX23885_BOARD_HAUPPAUGE_HVR1255:
462                 i2c_bus = &dev->i2c_bus[0];
463                 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
464                                                &hcw_s5h1411_config,
465                                                &i2c_bus->i2c_adap);
466                 if (fe0->dvb.frontend != NULL) {
467                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
468                                    0x60, &dev->i2c_bus[1].i2c_adap,
469                                    &hauppauge_tda18271_config);
470                 }
471                 break;
472         case CX23885_BOARD_HAUPPAUGE_HVR1800:
473                 i2c_bus = &dev->i2c_bus[0];
474                 switch (alt_tuner) {
475                 case 1:
476                         fe0->dvb.frontend =
477                                 dvb_attach(s5h1409_attach,
478                                            &hauppauge_ezqam_config,
479                                            &i2c_bus->i2c_adap);
480                         if (fe0->dvb.frontend != NULL) {
481                                 dvb_attach(tda829x_attach, fe0->dvb.frontend,
482                                            &dev->i2c_bus[1].i2c_adap, 0x42,
483                                            &tda829x_no_probe);
484                                 dvb_attach(tda18271_attach, fe0->dvb.frontend,
485                                            0x60, &dev->i2c_bus[1].i2c_adap,
486                                            &hauppauge_tda18271_config);
487                         }
488                         break;
489                 case 0:
490                 default:
491                         fe0->dvb.frontend =
492                                 dvb_attach(s5h1409_attach,
493                                            &hauppauge_generic_config,
494                                            &i2c_bus->i2c_adap);
495                         if (fe0->dvb.frontend != NULL)
496                                 dvb_attach(mt2131_attach, fe0->dvb.frontend,
497                                            &i2c_bus->i2c_adap,
498                                            &hauppauge_generic_tunerconfig, 0);
499                         break;
500                 }
501                 break;
502         case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
503                 i2c_bus = &dev->i2c_bus[0];
504                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
505                                                 &hauppauge_hvr1800lp_config,
506                                                 &i2c_bus->i2c_adap);
507                 if (fe0->dvb.frontend != NULL) {
508                         dvb_attach(mt2131_attach, fe0->dvb.frontend,
509                                    &i2c_bus->i2c_adap,
510                                    &hauppauge_generic_tunerconfig, 0);
511                 }
512                 break;
513         case CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP:
514                 i2c_bus = &dev->i2c_bus[0];
515                 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
516                                                 &fusionhdtv_5_express,
517                                                 &i2c_bus->i2c_adap);
518                 if (fe0->dvb.frontend != NULL) {
519                         dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
520                                    &i2c_bus->i2c_adap, 0x61,
521                                    TUNER_LG_TDVS_H06XF);
522                 }
523                 break;
524         case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
525                 i2c_bus = &dev->i2c_bus[1];
526                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
527                                                 &hauppauge_hvr1500q_config,
528                                                 &dev->i2c_bus[0].i2c_adap);
529                 if (fe0->dvb.frontend != NULL)
530                         dvb_attach(xc5000_attach, fe0->dvb.frontend,
531                                    &i2c_bus->i2c_adap,
532                                    &hauppauge_hvr1500q_tunerconfig);
533                 break;
534         case CX23885_BOARD_HAUPPAUGE_HVR1500:
535                 i2c_bus = &dev->i2c_bus[1];
536                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
537                                                 &hauppauge_hvr1500_config,
538                                                 &dev->i2c_bus[0].i2c_adap);
539                 if (fe0->dvb.frontend != NULL) {
540                         struct dvb_frontend *fe;
541                         struct xc2028_config cfg = {
542                                 .i2c_adap  = &i2c_bus->i2c_adap,
543                                 .i2c_addr  = 0x61,
544                         };
545                         static struct xc2028_ctrl ctl = {
546                                 .fname       = XC2028_DEFAULT_FIRMWARE,
547                                 .max_len     = 64,
548                                 .demod       = XC3028_FE_OREN538,
549                         };
550
551                         fe = dvb_attach(xc2028_attach,
552                                         fe0->dvb.frontend, &cfg);
553                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
554                                 fe->ops.tuner_ops.set_config(fe, &ctl);
555                 }
556                 break;
557         case CX23885_BOARD_HAUPPAUGE_HVR1200:
558         case CX23885_BOARD_HAUPPAUGE_HVR1700:
559                 i2c_bus = &dev->i2c_bus[0];
560                 fe0->dvb.frontend = dvb_attach(tda10048_attach,
561                         &hauppauge_hvr1200_config,
562                         &i2c_bus->i2c_adap);
563                 if (fe0->dvb.frontend != NULL) {
564                         dvb_attach(tda829x_attach, fe0->dvb.frontend,
565                                 &dev->i2c_bus[1].i2c_adap, 0x42,
566                                 &tda829x_no_probe);
567                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
568                                 0x60, &dev->i2c_bus[1].i2c_adap,
569                                 &hauppauge_hvr1200_tuner_config);
570                 }
571                 break;
572         case CX23885_BOARD_HAUPPAUGE_HVR1210:
573                 i2c_bus = &dev->i2c_bus[0];
574                 fe0->dvb.frontend = dvb_attach(tda10048_attach,
575                         &hauppauge_hvr1210_config,
576                         &i2c_bus->i2c_adap);
577                 if (fe0->dvb.frontend != NULL) {
578                         dvb_attach(tda18271_attach, fe0->dvb.frontend,
579                                 0x60, &dev->i2c_bus[1].i2c_adap,
580                                 &hauppauge_hvr1210_tuner_config);
581                 }
582                 break;
583         case CX23885_BOARD_HAUPPAUGE_HVR1400:
584                 i2c_bus = &dev->i2c_bus[0];
585                 fe0->dvb.frontend = dvb_attach(dib7000p_attach,
586                         &i2c_bus->i2c_adap,
587                         0x12, &hauppauge_hvr1400_dib7000_config);
588                 if (fe0->dvb.frontend != NULL) {
589                         struct dvb_frontend *fe;
590                         struct xc2028_config cfg = {
591                                 .i2c_adap  = &dev->i2c_bus[1].i2c_adap,
592                                 .i2c_addr  = 0x64,
593                         };
594                         static struct xc2028_ctrl ctl = {
595                                 .fname   = XC3028L_DEFAULT_FIRMWARE,
596                                 .max_len = 64,
597                                 .demod   = 5000,
598                                 /* This is true for all demods with
599                                         v36 firmware? */
600                                 .type    = XC2028_D2633,
601                         };
602
603                         fe = dvb_attach(xc2028_attach,
604                                         fe0->dvb.frontend, &cfg);
605                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
606                                 fe->ops.tuner_ops.set_config(fe, &ctl);
607                 }
608                 break;
609         case CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP:
610                 i2c_bus = &dev->i2c_bus[port->nr - 1];
611
612                 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
613                                                 &dvico_s5h1409_config,
614                                                 &i2c_bus->i2c_adap);
615                 if (fe0->dvb.frontend == NULL)
616                         fe0->dvb.frontend = dvb_attach(s5h1411_attach,
617                                                         &dvico_s5h1411_config,
618                                                         &i2c_bus->i2c_adap);
619                 if (fe0->dvb.frontend != NULL)
620                         dvb_attach(xc5000_attach, fe0->dvb.frontend,
621                                    &i2c_bus->i2c_adap,
622                                    &dvico_xc5000_tunerconfig);
623                 break;
624         case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP: {
625                 i2c_bus = &dev->i2c_bus[port->nr - 1];
626
627                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
628                                                &dvico_fusionhdtv_xc3028,
629                                                &i2c_bus->i2c_adap);
630                 if (fe0->dvb.frontend != NULL) {
631                         struct dvb_frontend      *fe;
632                         struct xc2028_config      cfg = {
633                                 .i2c_adap  = &i2c_bus->i2c_adap,
634                                 .i2c_addr  = 0x61,
635                         };
636                         static struct xc2028_ctrl ctl = {
637                                 .fname       = XC2028_DEFAULT_FIRMWARE,
638                                 .max_len     = 64,
639                                 .demod       = XC3028_FE_ZARLINK456,
640                         };
641
642                         fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
643                                         &cfg);
644                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
645                                 fe->ops.tuner_ops.set_config(fe, &ctl);
646                 }
647                 break;
648         }
649         case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H:
650         case CX23885_BOARD_COMPRO_VIDEOMATE_E650F:
651                 i2c_bus = &dev->i2c_bus[0];
652
653                 fe0->dvb.frontend = dvb_attach(zl10353_attach,
654                         &dvico_fusionhdtv_xc3028,
655                         &i2c_bus->i2c_adap);
656                 if (fe0->dvb.frontend != NULL) {
657                         struct dvb_frontend      *fe;
658                         struct xc2028_config      cfg = {
659                                 .i2c_adap  = &dev->i2c_bus[1].i2c_adap,
660                                 .i2c_addr  = 0x61,
661                         };
662                         static struct xc2028_ctrl ctl = {
663                                 .fname       = XC2028_DEFAULT_FIRMWARE,
664                                 .max_len     = 64,
665                                 .demod       = XC3028_FE_ZARLINK456,
666                         };
667
668                         fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
669                                 &cfg);
670                         if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
671                                 fe->ops.tuner_ops.set_config(fe, &ctl);
672                 }
673                 break;
674         case CX23885_BOARD_TBS_6920:
675                 i2c_bus = &dev->i2c_bus[0];
676
677                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
678                         &tbs_cx24116_config,
679                         &i2c_bus->i2c_adap);
680                 if (fe0->dvb.frontend != NULL)
681                         fe0->dvb.frontend->ops.set_voltage = tbs_set_voltage;
682
683                 break;
684         case CX23885_BOARD_TEVII_S470:
685                 i2c_bus = &dev->i2c_bus[1];
686
687                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
688                         &tevii_cx24116_config,
689                         &i2c_bus->i2c_adap);
690                 if (fe0->dvb.frontend != NULL)
691                         fe0->dvb.frontend->ops.set_voltage = tbs_set_voltage;
692
693                 break;
694         case CX23885_BOARD_DVBWORLD_2005:
695                 i2c_bus = &dev->i2c_bus[1];
696
697                 fe0->dvb.frontend = dvb_attach(cx24116_attach,
698                         &dvbworld_cx24116_config,
699                         &i2c_bus->i2c_adap);
700                 break;
701         case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
702                 i2c_bus = &dev->i2c_bus[0];
703                 switch (port->nr) {
704                 /* port B */
705                 case 1:
706                         fe0->dvb.frontend = dvb_attach(stv0900_attach,
707                                                         &netup_stv0900_config,
708                                                         &i2c_bus->i2c_adap, 0);
709                         if (fe0->dvb.frontend != NULL) {
710                                 if (dvb_attach(stv6110_attach,
711                                                 fe0->dvb.frontend,
712                                                 &netup_stv6110_tunerconfig_a,
713                                                 &i2c_bus->i2c_adap)) {
714                                         if (!dvb_attach(lnbh24_attach,
715                                                         fe0->dvb.frontend,
716                                                         &i2c_bus->i2c_adap,
717                                                         LNBH24_PCL, 0, 0x09))
718                                                 printk(KERN_ERR
719                                                         "No LNBH24 found!\n");
720
721                                 }
722                         }
723                         break;
724                 /* port C */
725                 case 2:
726                         fe0->dvb.frontend = dvb_attach(stv0900_attach,
727                                                         &netup_stv0900_config,
728                                                         &i2c_bus->i2c_adap, 1);
729                         if (fe0->dvb.frontend != NULL) {
730                                 if (dvb_attach(stv6110_attach,
731                                                 fe0->dvb.frontend,
732                                                 &netup_stv6110_tunerconfig_b,
733                                                 &i2c_bus->i2c_adap)) {
734                                         if (!dvb_attach(lnbh24_attach,
735                                                         fe0->dvb.frontend,
736                                                         &i2c_bus->i2c_adap,
737                                                         LNBH24_PCL, 0, 0x0a))
738                                                 printk(KERN_ERR
739                                                         "No LNBH24 found!\n");
740
741                                 }
742                         }
743                         break;
744                 }
745                 break;
746         default:
747                 printk(KERN_INFO "%s: The frontend of your DVB/ATSC card "
748                         " isn't supported yet\n",
749                        dev->name);
750                 break;
751         }
752         if (NULL == fe0->dvb.frontend) {
753                 printk(KERN_ERR "%s: frontend initialization failed\n",
754                         dev->name);
755                 return -1;
756         }
757         /* define general-purpose callback pointer */
758         fe0->dvb.frontend->callback = cx23885_tuner_callback;
759
760         /* Put the analog decoder in standby to keep it quiet */
761         call_all(dev, tuner, s_standby);
762
763         if (fe0->dvb.frontend->ops.analog_ops.standby)
764                 fe0->dvb.frontend->ops.analog_ops.standby(fe0->dvb.frontend);
765
766         /* register everything */
767         ret = videobuf_dvb_register_bus(&port->frontends, THIS_MODULE, port,
768                 &dev->pci->dev, adapter_nr, 0);
769
770         /* init CI & MAC */
771         switch (dev->board) {
772         case CX23885_BOARD_NETUP_DUAL_DVBS2_CI: {
773                 static struct netup_card_info cinfo;
774
775                 netup_get_card_info(&dev->i2c_bus[0].i2c_adap, &cinfo);
776                 memcpy(port->frontends.adapter.proposed_mac,
777                                 cinfo.port[port->nr - 1].mac, 6);
778                 printk(KERN_INFO "NetUP Dual DVB-S2 CI card port%d MAC="
779                         "%02X:%02X:%02X:%02X:%02X:%02X\n",
780                         port->nr,
781                         port->frontends.adapter.proposed_mac[0],
782                         port->frontends.adapter.proposed_mac[1],
783                         port->frontends.adapter.proposed_mac[2],
784                         port->frontends.adapter.proposed_mac[3],
785                         port->frontends.adapter.proposed_mac[4],
786                         port->frontends.adapter.proposed_mac[5]);
787
788                 netup_ci_init(port);
789                 break;
790                 }
791         }
792
793         return ret;
794 }
795
796 int cx23885_dvb_register(struct cx23885_tsport *port)
797 {
798
799         struct videobuf_dvb_frontend *fe0;
800         struct cx23885_dev *dev = port->dev;
801         int err, i;
802
803         /* Here we need to allocate the correct number of frontends,
804          * as reflected in the cards struct. The reality is that currrently
805          * no cx23885 boards support this - yet. But, if we don't modify this
806          * code then the second frontend would never be allocated (later)
807          * and fail with error before the attach in dvb_register().
808          * Without these changes we risk an OOPS later. The changes here
809          * are for safety, and should provide a good foundation for the
810          * future addition of any multi-frontend cx23885 based boards.
811          */
812         printk(KERN_INFO "%s() allocating %d frontend(s)\n", __func__,
813                 port->num_frontends);
814
815         for (i = 1; i <= port->num_frontends; i++) {
816                 if (videobuf_dvb_alloc_frontend(
817                         &port->frontends, i) == NULL) {
818                         printk(KERN_ERR "%s() failed to alloc\n", __func__);
819                         return -ENOMEM;
820                 }
821
822                 fe0 = videobuf_dvb_get_frontend(&port->frontends, i);
823                 if (!fe0)
824                         err = -EINVAL;
825
826                 dprintk(1, "%s\n", __func__);
827                 dprintk(1, " ->probed by Card=%d Name=%s, PCI %02x:%02x\n",
828                         dev->board,
829                         dev->name,
830                         dev->pci_bus,
831                         dev->pci_slot);
832
833                 err = -ENODEV;
834
835                 /* dvb stuff */
836                 /* We have to init the queue for each frontend on a port. */
837                 printk(KERN_INFO "%s: cx23885 based dvb card\n", dev->name);
838                 videobuf_queue_sg_init(&fe0->dvb.dvbq, &dvb_qops,
839                             &dev->pci->dev, &port->slock,
840                             V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_TOP,
841                             sizeof(struct cx23885_buffer), port);
842         }
843         err = dvb_register(port);
844         if (err != 0)
845                 printk(KERN_ERR "%s() dvb_register failed err = %d\n",
846                         __func__, err);
847
848         return err;
849 }
850
851 int cx23885_dvb_unregister(struct cx23885_tsport *port)
852 {
853         struct videobuf_dvb_frontend *fe0;
854
855         /* FIXME: in an error condition where the we have
856          * an expected number of frontends (attach problem)
857          * then this might not clean up correctly, if 1
858          * is invalid.
859          * This comment only applies to future boards IF they
860          * implement MFE support.
861          */
862         fe0 = videobuf_dvb_get_frontend(&port->frontends, 1);
863         if (fe0->dvb.frontend)
864                 videobuf_dvb_unregister_bus(&port->frontends);
865
866         switch (port->dev->board) {
867         case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
868                 netup_ci_exit(port);
869                 break;
870         }
871
872         return 0;
873 }
874