GNU Linux-libre 4.14.313-gnu1
[releases.git] / drivers / media / usb / dvb-usb / dib0700_devices.c
1 /* Linux driver for devices based on the DiBcom DiB0700 USB bridge
2  *
3  *      This program is free software; you can redistribute it and/or modify it
4  *      under the terms of the GNU General Public License as published by the Free
5  *      Software Foundation, version 2.
6  *
7  *  Copyright (C) 2005-9 DiBcom, SA et al
8  */
9 #include "dib0700.h"
10
11 #include "dib3000mc.h"
12 #include "dib7000m.h"
13 #include "dib7000p.h"
14 #include "dib8000.h"
15 #include "dib9000.h"
16 #include "mt2060.h"
17 #include "mt2266.h"
18 #include "tuner-xc2028.h"
19 #include "xc5000.h"
20 #include "xc4000.h"
21 #include "s5h1411.h"
22 #include "dib0070.h"
23 #include "dib0090.h"
24 #include "lgdt3305.h"
25 #include "mxl5007t.h"
26
27 static int force_lna_activation;
28 module_param(force_lna_activation, int, 0644);
29 MODULE_PARM_DESC(force_lna_activation, "force the activation of Low-Noise-Amplifyer(s) (LNA), if applicable for the device (default: 0=automatic/off).");
30
31 struct dib0700_adapter_state {
32         int (*set_param_save) (struct dvb_frontend *);
33         const struct firmware *frontend_firmware;
34         struct dib7000p_ops dib7000p_ops;
35         struct dib8000_ops dib8000_ops;
36 };
37
38 /* Hauppauge Nova-T 500 (aka Bristol)
39  *  has a LNA on GPIO0 which is enabled by setting 1 */
40 static struct mt2060_config bristol_mt2060_config[2] = {
41         {
42                 .i2c_address = 0x60,
43                 .clock_out   = 3,
44         }, {
45                 .i2c_address = 0x61,
46         }
47 };
48
49
50 static struct dibx000_agc_config bristol_dib3000p_mt2060_agc_config = {
51         .band_caps = BAND_VHF | BAND_UHF,
52         .setup     = (1 << 8) | (5 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (2 << 0),
53
54         .agc1_max = 42598,
55         .agc1_min = 17694,
56         .agc2_max = 45875,
57         .agc2_min = 0,
58
59         .agc1_pt1 = 0,
60         .agc1_pt2 = 59,
61
62         .agc1_slope1 = 0,
63         .agc1_slope2 = 69,
64
65         .agc2_pt1 = 0,
66         .agc2_pt2 = 59,
67
68         .agc2_slope1 = 111,
69         .agc2_slope2 = 28,
70 };
71
72 static struct dib3000mc_config bristol_dib3000mc_config[2] = {
73         {       .agc          = &bristol_dib3000p_mt2060_agc_config,
74                 .max_time     = 0x196,
75                 .ln_adc_level = 0x1cc7,
76                 .output_mpeg2_in_188_bytes = 1,
77         },
78         {       .agc          = &bristol_dib3000p_mt2060_agc_config,
79                 .max_time     = 0x196,
80                 .ln_adc_level = 0x1cc7,
81                 .output_mpeg2_in_188_bytes = 1,
82         }
83 };
84
85 static int bristol_frontend_attach(struct dvb_usb_adapter *adap)
86 {
87         struct dib0700_state *st = adap->dev->priv;
88         if (adap->id == 0) {
89                 dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 0); msleep(10);
90                 dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 1); msleep(10);
91                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0); msleep(10);
92                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1); msleep(10);
93
94                 if (force_lna_activation)
95                         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
96                 else
97                         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 0);
98
99                 if (dib3000mc_i2c_enumeration(&adap->dev->i2c_adap, 2, DEFAULT_DIB3000P_I2C_ADDRESS, bristol_dib3000mc_config) != 0) {
100                         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0); msleep(10);
101                         return -ENODEV;
102                 }
103         }
104         st->mt2060_if1[adap->id] = 1220;
105         return (adap->fe_adap[0].fe = dvb_attach(dib3000mc_attach, &adap->dev->i2c_adap,
106                 (10 + adap->id) << 1, &bristol_dib3000mc_config[adap->id])) == NULL ? -ENODEV : 0;
107 }
108
109 static int eeprom_read(struct i2c_adapter *adap,u8 adrs,u8 *pval)
110 {
111         struct i2c_msg msg[2] = {
112                 { .addr = 0x50, .flags = 0,        .buf = &adrs, .len = 1 },
113                 { .addr = 0x50, .flags = I2C_M_RD, .buf = pval,  .len = 1 },
114         };
115         if (i2c_transfer(adap, msg, 2) != 2) return -EREMOTEIO;
116         return 0;
117 }
118
119 static int bristol_tuner_attach(struct dvb_usb_adapter *adap)
120 {
121         struct i2c_adapter *prim_i2c = &adap->dev->i2c_adap;
122         struct i2c_adapter *tun_i2c = dib3000mc_get_tuner_i2c_master(adap->fe_adap[0].fe, 1);
123         s8 a;
124         int if1=1220;
125         if (adap->dev->udev->descriptor.idVendor  == cpu_to_le16(USB_VID_HAUPPAUGE) &&
126                 adap->dev->udev->descriptor.idProduct == cpu_to_le16(USB_PID_HAUPPAUGE_NOVA_T_500_2)) {
127                 if (!eeprom_read(prim_i2c,0x59 + adap->id,&a)) if1=1220+a;
128         }
129         return dvb_attach(mt2060_attach, adap->fe_adap[0].fe, tun_i2c,
130                           &bristol_mt2060_config[adap->id], if1) == NULL ?
131                           -ENODEV : 0;
132 }
133
134 /* STK7700D: Pinnacle/Terratec/Hauppauge Dual DVB-T Diversity */
135
136 /* MT226x */
137 static struct dibx000_agc_config stk7700d_7000p_mt2266_agc_config[2] = {
138         {
139                 BAND_UHF,
140
141                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=1, P_agc_inv_pwm1=1, P_agc_inv_pwm2=1,
142                 * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=2, P_agc_write=0 */
143                 (0 << 15) | (0 << 14) | (1 << 11) | (1 << 10) | (1 << 9) | (0 << 8)
144             | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
145
146                 1130,
147                 21,
148
149                 0,
150                 118,
151
152                 0,
153                 3530,
154                 1,
155                 0,
156
157                 65535,
158                 33770,
159                 65535,
160                 23592,
161
162                 0,
163                 62,
164                 255,
165                 64,
166                 64,
167                 132,
168                 192,
169                 80,
170                 80,
171
172                 17,
173                 27,
174                 23,
175                 51,
176
177                 1,
178         }, {
179                 BAND_VHF | BAND_LBAND,
180
181                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=1, P_agc_inv_pwm1=1, P_agc_inv_pwm2=1,
182                 * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=2, P_agc_write=0 */
183                 (0 << 15) | (0 << 14) | (1 << 11) | (1 << 10) | (1 << 9) | (0 << 8)
184             | (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0),
185
186                 2372,
187                 21,
188
189                 0,
190                 118,
191
192                 0,
193                 3530,
194                 1,
195                 0,
196
197                 65535,
198                 0,
199                 65535,
200                 23592,
201
202                 0,
203                 128,
204                 128,
205                 128,
206                 0,
207                 128,
208                 253,
209                 81,
210                 0,
211
212                 17,
213                 27,
214                 23,
215                 51,
216
217                 1,
218         }
219 };
220
221 static struct dibx000_bandwidth_config stk7700d_mt2266_pll_config = {
222         .internal = 60000,
223         .sampling = 30000,
224         .pll_prediv = 1,
225         .pll_ratio = 8,
226         .pll_range = 3,
227         .pll_reset = 1,
228         .pll_bypass = 0,
229         .enable_refdiv = 0,
230         .bypclk_div = 0,
231         .IO_CLK_en_core = 1,
232         .ADClkSrc = 1,
233         .modulo = 2,
234         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
235         .ifreq = 0,
236         .timf = 20452225,
237 };
238
239 static struct dib7000p_config stk7700d_dib7000p_mt2266_config[] = {
240         {       .output_mpeg2_in_188_bytes = 1,
241                 .hostbus_diversity = 1,
242                 .tuner_is_baseband = 1,
243
244                 .agc_config_count = 2,
245                 .agc = stk7700d_7000p_mt2266_agc_config,
246                 .bw  = &stk7700d_mt2266_pll_config,
247
248                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
249                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
250                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
251         },
252         {       .output_mpeg2_in_188_bytes = 1,
253                 .hostbus_diversity = 1,
254                 .tuner_is_baseband = 1,
255
256                 .agc_config_count = 2,
257                 .agc = stk7700d_7000p_mt2266_agc_config,
258                 .bw  = &stk7700d_mt2266_pll_config,
259
260                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
261                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
262                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
263         }
264 };
265
266 static struct mt2266_config stk7700d_mt2266_config[2] = {
267         {       .i2c_address = 0x60
268         },
269         {       .i2c_address = 0x60
270         }
271 };
272
273 static int stk7700P2_frontend_attach(struct dvb_usb_adapter *adap)
274 {
275         struct dib0700_adapter_state *state = adap->priv;
276
277         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
278                 return -ENODEV;
279
280         if (adap->id == 0) {
281                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
282                 msleep(10);
283                 dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
284                 dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
285                 dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
286                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
287                 msleep(10);
288                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
289                 msleep(10);
290                 if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
291                                              stk7700d_dib7000p_mt2266_config)
292                     != 0) {
293                         err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
294                         dvb_detach(state->dib7000p_ops.set_wbd_ref);
295                         return -ENODEV;
296                 }
297         }
298
299         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap,
300                            0x80 + (adap->id << 1),
301                            &stk7700d_dib7000p_mt2266_config[adap->id]);
302
303         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
304 }
305
306 static int stk7700d_frontend_attach(struct dvb_usb_adapter *adap)
307 {
308         struct dib0700_adapter_state *state = adap->priv;
309
310         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
311                 return -ENODEV;
312
313         if (adap->id == 0) {
314                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
315                 msleep(10);
316                 dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
317                 dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
318                 dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
319                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
320                 msleep(10);
321                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
322                 msleep(10);
323                 dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
324                 if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 2, 18,
325                                              stk7700d_dib7000p_mt2266_config)
326                     != 0) {
327                         err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
328                         dvb_detach(state->dib7000p_ops.set_wbd_ref);
329                         return -ENODEV;
330                 }
331         }
332
333         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap,
334                            0x80 + (adap->id << 1),
335                            &stk7700d_dib7000p_mt2266_config[adap->id]);
336
337         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
338 }
339
340 static int stk7700d_tuner_attach(struct dvb_usb_adapter *adap)
341 {
342         struct i2c_adapter *tun_i2c;
343         struct dib0700_adapter_state *state = adap->priv;
344
345         tun_i2c = state->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
346                                             DIBX000_I2C_INTERFACE_TUNER, 1);
347         return dvb_attach(mt2266_attach, adap->fe_adap[0].fe, tun_i2c,
348                 &stk7700d_mt2266_config[adap->id]) == NULL ? -ENODEV : 0;
349 }
350
351 /* STK7700-PH: Digital/Analog Hybrid Tuner, e.h. Cinergy HT USB HE */
352 static struct dibx000_agc_config xc3028_agc_config = {
353         .band_caps = BAND_VHF | BAND_UHF,
354         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=0,
355          * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
356          * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=2, P_agc_write=0 */
357         .setup = (0 << 15) | (0 << 14) | (0 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0),
358         .inv_gain = 712,
359         .time_stabiliz = 21,
360         .alpha_level = 0,
361         .thlock = 118,
362         .wbd_inv = 0,
363         .wbd_ref = 2867,
364         .wbd_sel = 0,
365         .wbd_alpha = 2,
366         .agc1_max = 0,
367         .agc1_min = 0,
368         .agc2_max = 39718,
369         .agc2_min = 9930,
370         .agc1_pt1 = 0,
371         .agc1_pt2 = 0,
372         .agc1_pt3 = 0,
373         .agc1_slope1 = 0,
374         .agc1_slope2 = 0,
375         .agc2_pt1 = 0,
376         .agc2_pt2 = 128,
377         .agc2_slope1 = 29,
378         .agc2_slope2 = 29,
379         .alpha_mant = 17,
380         .alpha_exp = 27,
381         .beta_mant = 23,
382         .beta_exp = 51,
383         .perform_agc_softsplit = 1,
384 };
385
386 /* PLL Configuration for COFDM BW_MHz = 8.00 with external clock = 30.00 */
387 static struct dibx000_bandwidth_config xc3028_bw_config = {
388         .internal = 60000,
389         .sampling = 30000,
390         .pll_prediv = 1,
391         .pll_ratio = 8,
392         .pll_range = 3,
393         .pll_reset = 1,
394         .pll_bypass = 0,
395         .enable_refdiv = 0,
396         .bypclk_div = 0,
397         .IO_CLK_en_core = 1,
398         .ADClkSrc = 1,
399         .modulo = 0,
400         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0), /* sad_cfg: refsel, sel, freq_15k */
401         .ifreq = (1 << 25) | 5816102,  /* ifreq = 5.200000 MHz */
402         .timf = 20452225,
403         .xtal_hz = 30000000,
404 };
405
406 static struct dib7000p_config stk7700ph_dib7700_xc3028_config = {
407         .output_mpeg2_in_188_bytes = 1,
408         .tuner_is_baseband = 1,
409
410         .agc_config_count = 1,
411         .agc = &xc3028_agc_config,
412         .bw  = &xc3028_bw_config,
413
414         .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
415         .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
416         .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
417 };
418
419 static int stk7700ph_xc3028_callback(void *ptr, int component,
420                                      int command, int arg)
421 {
422         struct dvb_usb_adapter *adap = ptr;
423         struct dib0700_adapter_state *state = adap->priv;
424
425         switch (command) {
426         case XC2028_TUNER_RESET:
427                 /* Send the tuner in then out of reset */
428                 state->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 0);
429                 msleep(10);
430                 state->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
431                 break;
432         case XC2028_RESET_CLK:
433         case XC2028_I2C_FLUSH:
434                 break;
435         default:
436                 err("%s: unknown command %d, arg %d\n", __func__,
437                         command, arg);
438                 return -EINVAL;
439         }
440         return 0;
441 }
442
443 static struct xc2028_ctrl stk7700ph_xc3028_ctrl = {
444         .fname = "/*(DEBLOBBED)*/",
445         .max_len = 64,
446         .demod = XC3028_FE_DIBCOM52,
447 };
448
449 static struct xc2028_config stk7700ph_xc3028_config = {
450         .i2c_addr = 0x61,
451         .ctrl = &stk7700ph_xc3028_ctrl,
452 };
453
454 static int stk7700ph_frontend_attach(struct dvb_usb_adapter *adap)
455 {
456         struct usb_device_descriptor *desc = &adap->dev->udev->descriptor;
457         struct dib0700_adapter_state *state = adap->priv;
458
459         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
460                 return -ENODEV;
461
462         if (desc->idVendor  == cpu_to_le16(USB_VID_PINNACLE) &&
463             desc->idProduct == cpu_to_le16(USB_PID_PINNACLE_EXPRESSCARD_320CX))
464                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
465         else
466                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
467         msleep(20);
468         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
469         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
470         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
471         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
472         msleep(10);
473         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
474         msleep(20);
475         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
476         msleep(10);
477
478         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
479                                      &stk7700ph_dib7700_xc3028_config) != 0) {
480                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
481                     __func__);
482                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
483                 return -ENODEV;
484         }
485
486         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x80,
487                 &stk7700ph_dib7700_xc3028_config);
488
489         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
490 }
491
492 static int stk7700ph_tuner_attach(struct dvb_usb_adapter *adap)
493 {
494         struct i2c_adapter *tun_i2c;
495         struct dib0700_adapter_state *state = adap->priv;
496
497         tun_i2c = state->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
498                 DIBX000_I2C_INTERFACE_TUNER, 1);
499
500         stk7700ph_xc3028_config.i2c_adap = tun_i2c;
501
502         /* FIXME: generalize & move to common area */
503         adap->fe_adap[0].fe->callback = stk7700ph_xc3028_callback;
504
505         return dvb_attach(xc2028_attach, adap->fe_adap[0].fe, &stk7700ph_xc3028_config)
506                 == NULL ? -ENODEV : 0;
507 }
508
509 #define DEFAULT_RC_INTERVAL 50
510
511 /*
512  * This function is used only when firmware is < 1.20 version. Newer
513  * firmwares use bulk mode, with functions implemented at dib0700_core,
514  * at dib0700_rc_urb_completion()
515  */
516 static int dib0700_rc_query_old_firmware(struct dvb_usb_device *d)
517 {
518         enum rc_proto protocol;
519         u32 scancode;
520         u8 toggle;
521         int i;
522         struct dib0700_state *st = d->priv;
523
524         if (st->fw_version >= 0x10200) {
525                 /* For 1.20 firmware , We need to keep the RC polling
526                    callback so we can reuse the input device setup in
527                    dvb-usb-remote.c.  However, the actual work is being done
528                    in the bulk URB completion handler. */
529                 return 0;
530         }
531
532         st->buf[0] = REQUEST_POLL_RC;
533         st->buf[1] = 0;
534
535         i = dib0700_ctrl_rd(d, st->buf, 2, st->buf, 4);
536         if (i <= 0) {
537                 err("RC Query Failed");
538                 return -EIO;
539         }
540
541         /* losing half of KEY_0 events from Philipps rc5 remotes.. */
542         if (st->buf[0] == 0 && st->buf[1] == 0
543             && st->buf[2] == 0 && st->buf[3] == 0)
544                 return 0;
545
546         /* info("%d: %2X %2X %2X %2X",dvb_usb_dib0700_ir_proto,(int)st->buf[3 - 2],(int)st->buf[3 - 3],(int)st->buf[3 - 1],(int)st->buf[3]);  */
547
548         dib0700_rc_setup(d, NULL); /* reset ir sensor data to prevent false events */
549
550         switch (d->props.rc.core.protocol) {
551         case RC_PROTO_BIT_NEC:
552                 /* NEC protocol sends repeat code as 0 0 0 FF */
553                 if ((st->buf[3 - 2] == 0x00) && (st->buf[3 - 3] == 0x00) &&
554                     (st->buf[3] == 0xff)) {
555                         rc_repeat(d->rc_dev);
556                         return 0;
557                 }
558
559                 protocol = RC_PROTO_NEC;
560                 scancode = RC_SCANCODE_NEC(st->buf[3 - 2], st->buf[3 - 3]);
561                 toggle = 0;
562                 break;
563
564         default:
565                 /* RC-5 protocol changes toggle bit on new keypress */
566                 protocol = RC_PROTO_RC5;
567                 scancode = RC_SCANCODE_RC5(st->buf[3 - 2], st->buf[3 - 3]);
568                 toggle = st->buf[3 - 1];
569                 break;
570         }
571
572         rc_keydown(d->rc_dev, protocol, scancode, toggle);
573         return 0;
574 }
575
576 /* STK7700P: Hauppauge Nova-T Stick, AVerMedia Volar */
577 static struct dibx000_agc_config stk7700p_7000m_mt2060_agc_config = {
578         BAND_UHF | BAND_VHF,
579
580         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=5, P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
581          * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=2, P_agc_write=0 */
582         (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8)
583         | (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0),
584
585         712,
586         41,
587
588         0,
589         118,
590
591         0,
592         4095,
593         0,
594         0,
595
596         42598,
597         17694,
598         45875,
599         2621,
600         0,
601         76,
602         139,
603         52,
604         59,
605         107,
606         172,
607         57,
608         70,
609
610         21,
611         25,
612         28,
613         48,
614
615         1,
616         {  0,
617            107,
618            51800,
619            24700
620         },
621 };
622
623 static struct dibx000_agc_config stk7700p_7000p_mt2060_agc_config = {
624         .band_caps = BAND_UHF | BAND_VHF,
625         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=5, P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
626          * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=2, P_agc_write=0 */
627         .setup = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0),
628         .inv_gain = 712,
629         .time_stabiliz = 41,
630         .alpha_level = 0,
631         .thlock = 118,
632         .wbd_inv = 0,
633         .wbd_ref = 4095,
634         .wbd_sel = 0,
635         .wbd_alpha = 0,
636         .agc1_max = 42598,
637         .agc1_min = 16384,
638         .agc2_max = 42598,
639         .agc2_min = 0,
640         .agc1_pt1 = 0,
641         .agc1_pt2 = 137,
642         .agc1_pt3 = 255,
643         .agc1_slope1 = 0,
644         .agc1_slope2 = 255,
645         .agc2_pt1 = 0,
646         .agc2_pt2 = 0,
647         .agc2_slope1 = 0,
648         .agc2_slope2 = 41,
649         .alpha_mant = 15,
650         .alpha_exp = 25,
651         .beta_mant = 28,
652         .beta_exp = 48,
653         .perform_agc_softsplit = 0,
654 };
655
656 static struct dibx000_bandwidth_config stk7700p_pll_config = {
657         .internal = 60000,
658         .sampling = 30000,
659         .pll_prediv = 1,
660         .pll_ratio = 8,
661         .pll_range = 3,
662         .pll_reset = 1,
663         .pll_bypass = 0,
664         .enable_refdiv = 0,
665         .bypclk_div = 0,
666         .IO_CLK_en_core = 1,
667         .ADClkSrc = 1,
668         .modulo = 0,
669         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
670         .ifreq = 60258167,
671         .timf = 20452225,
672         .xtal_hz = 30000000,
673 };
674
675 static struct dib7000m_config stk7700p_dib7000m_config = {
676         .dvbt_mode = 1,
677         .output_mpeg2_in_188_bytes = 1,
678         .quartz_direct = 1,
679
680         .agc_config_count = 1,
681         .agc = &stk7700p_7000m_mt2060_agc_config,
682         .bw  = &stk7700p_pll_config,
683
684         .gpio_dir = DIB7000M_GPIO_DEFAULT_DIRECTIONS,
685         .gpio_val = DIB7000M_GPIO_DEFAULT_VALUES,
686         .gpio_pwm_pos = DIB7000M_GPIO_DEFAULT_PWM_POS,
687 };
688
689 static struct dib7000p_config stk7700p_dib7000p_config = {
690         .output_mpeg2_in_188_bytes = 1,
691
692         .agc_config_count = 1,
693         .agc = &stk7700p_7000p_mt2060_agc_config,
694         .bw  = &stk7700p_pll_config,
695
696         .gpio_dir = DIB7000M_GPIO_DEFAULT_DIRECTIONS,
697         .gpio_val = DIB7000M_GPIO_DEFAULT_VALUES,
698         .gpio_pwm_pos = DIB7000M_GPIO_DEFAULT_PWM_POS,
699 };
700
701 static int stk7700p_frontend_attach(struct dvb_usb_adapter *adap)
702 {
703         struct dib0700_state *st = adap->dev->priv;
704         struct dib0700_adapter_state *state = adap->priv;
705
706         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
707                 return -ENODEV;
708
709         /* unless there is no real power management in DVB - we leave the device on GPIO6 */
710
711         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
712         dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 0); msleep(50);
713
714         dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 1); msleep(10);
715         dib0700_set_gpio(adap->dev, GPIO9,  GPIO_OUT, 1);
716
717         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0); msleep(10);
718         dib0700_ctrl_clock(adap->dev, 72, 1);
719         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1); msleep(100);
720
721         dib0700_set_gpio(adap->dev,  GPIO0, GPIO_OUT, 1);
722
723         st->mt2060_if1[0] = 1220;
724
725         if (state->dib7000p_ops.dib7000pc_detection(&adap->dev->i2c_adap)) {
726                 adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 18, &stk7700p_dib7000p_config);
727                 st->is_dib7000pc = 1;
728         } else {
729                 memset(&state->dib7000p_ops, 0, sizeof(state->dib7000p_ops));
730                 adap->fe_adap[0].fe = dvb_attach(dib7000m_attach, &adap->dev->i2c_adap, 18, &stk7700p_dib7000m_config);
731         }
732
733         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
734 }
735
736 static struct mt2060_config stk7700p_mt2060_config = {
737         0x60
738 };
739
740 static int stk7700p_tuner_attach(struct dvb_usb_adapter *adap)
741 {
742         struct i2c_adapter *prim_i2c = &adap->dev->i2c_adap;
743         struct dib0700_state *st = adap->dev->priv;
744         struct i2c_adapter *tun_i2c;
745         struct dib0700_adapter_state *state = adap->priv;
746         s8 a;
747         int if1=1220;
748
749         if (adap->dev->udev->descriptor.idVendor  == cpu_to_le16(USB_VID_HAUPPAUGE) &&
750                 adap->dev->udev->descriptor.idProduct == cpu_to_le16(USB_PID_HAUPPAUGE_NOVA_T_STICK)) {
751                 if (!eeprom_read(prim_i2c,0x58,&a)) if1=1220+a;
752         }
753         if (st->is_dib7000pc)
754                 tun_i2c = state->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_TUNER, 1);
755         else
756                 tun_i2c = dib7000m_get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_TUNER, 1);
757
758         return dvb_attach(mt2060_attach, adap->fe_adap[0].fe, tun_i2c, &stk7700p_mt2060_config,
759                 if1) == NULL ? -ENODEV : 0;
760 }
761
762 /* DIB7070 generic */
763 static struct dibx000_agc_config dib7070_agc_config = {
764         .band_caps = BAND_UHF | BAND_VHF | BAND_LBAND | BAND_SBAND,
765         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=5, P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
766          * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0 */
767         .setup = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
768         .inv_gain = 600,
769         .time_stabiliz = 10,
770         .alpha_level = 0,
771         .thlock = 118,
772         .wbd_inv = 0,
773         .wbd_ref = 3530,
774         .wbd_sel = 1,
775         .wbd_alpha = 5,
776         .agc1_max = 65535,
777         .agc1_min = 0,
778         .agc2_max = 65535,
779         .agc2_min = 0,
780         .agc1_pt1 = 0,
781         .agc1_pt2 = 40,
782         .agc1_pt3 = 183,
783         .agc1_slope1 = 206,
784         .agc1_slope2 = 255,
785         .agc2_pt1 = 72,
786         .agc2_pt2 = 152,
787         .agc2_slope1 = 88,
788         .agc2_slope2 = 90,
789         .alpha_mant = 17,
790         .alpha_exp = 27,
791         .beta_mant = 23,
792         .beta_exp = 51,
793         .perform_agc_softsplit = 0,
794 };
795
796 static int dib7070_tuner_reset(struct dvb_frontend *fe, int onoff)
797 {
798         struct dvb_usb_adapter *adap = fe->dvb->priv;
799         struct dib0700_adapter_state *state = adap->priv;
800
801         deb_info("reset: %d", onoff);
802         return state->dib7000p_ops.set_gpio(fe, 8, 0, !onoff);
803 }
804
805 static int dib7070_tuner_sleep(struct dvb_frontend *fe, int onoff)
806 {
807         struct dvb_usb_adapter *adap = fe->dvb->priv;
808         struct dib0700_adapter_state *state = adap->priv;
809
810         deb_info("sleep: %d", onoff);
811         return state->dib7000p_ops.set_gpio(fe, 9, 0, onoff);
812 }
813
814 static struct dib0070_config dib7070p_dib0070_config[2] = {
815         {
816                 .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
817                 .reset = dib7070_tuner_reset,
818                 .sleep = dib7070_tuner_sleep,
819                 .clock_khz = 12000,
820                 .clock_pad_drive = 4,
821                 .charge_pump = 2,
822         }, {
823                 .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
824                 .reset = dib7070_tuner_reset,
825                 .sleep = dib7070_tuner_sleep,
826                 .clock_khz = 12000,
827                 .charge_pump = 2,
828         }
829 };
830
831 static struct dib0070_config dib7770p_dib0070_config = {
832          .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
833          .reset = dib7070_tuner_reset,
834          .sleep = dib7070_tuner_sleep,
835          .clock_khz = 12000,
836          .clock_pad_drive = 0,
837          .flip_chip = 1,
838          .charge_pump = 2,
839 };
840
841 static int dib7070_set_param_override(struct dvb_frontend *fe)
842 {
843         struct dtv_frontend_properties *p = &fe->dtv_property_cache;
844         struct dvb_usb_adapter *adap = fe->dvb->priv;
845         struct dib0700_adapter_state *state = adap->priv;
846
847         u16 offset;
848         u8 band = BAND_OF_FREQUENCY(p->frequency/1000);
849         switch (band) {
850                 case BAND_VHF: offset = 950; break;
851                 case BAND_UHF:
852                 default: offset = 550; break;
853         }
854         deb_info("WBD for DiB7000P: %d\n", offset + dib0070_wbd_offset(fe));
855         state->dib7000p_ops.set_wbd_ref(fe, offset + dib0070_wbd_offset(fe));
856         return state->set_param_save(fe);
857 }
858
859 static int dib7770_set_param_override(struct dvb_frontend *fe)
860 {
861         struct dtv_frontend_properties *p = &fe->dtv_property_cache;
862         struct dvb_usb_adapter *adap = fe->dvb->priv;
863         struct dib0700_adapter_state *state = adap->priv;
864
865         u16 offset;
866         u8 band = BAND_OF_FREQUENCY(p->frequency/1000);
867         switch (band) {
868         case BAND_VHF:
869                 state->dib7000p_ops.set_gpio(fe, 0, 0, 1);
870                 offset = 850;
871                 break;
872         case BAND_UHF:
873         default:
874                 state->dib7000p_ops.set_gpio(fe, 0, 0, 0);
875                 offset = 250;
876                 break;
877         }
878         deb_info("WBD for DiB7000P: %d\n", offset + dib0070_wbd_offset(fe));
879         state->dib7000p_ops.set_wbd_ref(fe, offset + dib0070_wbd_offset(fe));
880         return state->set_param_save(fe);
881 }
882
883 static int dib7770p_tuner_attach(struct dvb_usb_adapter *adap)
884 {
885         struct dib0700_adapter_state *st = adap->priv;
886         struct i2c_adapter *tun_i2c = st->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
887                          DIBX000_I2C_INTERFACE_TUNER, 1);
888
889         if (dvb_attach(dib0070_attach, adap->fe_adap[0].fe, tun_i2c,
890                        &dib7770p_dib0070_config) == NULL)
891                 return -ENODEV;
892
893         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
894         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7770_set_param_override;
895         return 0;
896 }
897
898 static int dib7070p_tuner_attach(struct dvb_usb_adapter *adap)
899 {
900         struct dib0700_adapter_state *st = adap->priv;
901         struct i2c_adapter *tun_i2c = st->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_TUNER, 1);
902
903         if (adap->id == 0) {
904                 if (dvb_attach(dib0070_attach, adap->fe_adap[0].fe, tun_i2c, &dib7070p_dib0070_config[0]) == NULL)
905                         return -ENODEV;
906         } else {
907                 if (dvb_attach(dib0070_attach, adap->fe_adap[0].fe, tun_i2c, &dib7070p_dib0070_config[1]) == NULL)
908                         return -ENODEV;
909         }
910
911         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
912         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7070_set_param_override;
913         return 0;
914 }
915
916 static int stk7700p_pid_filter(struct dvb_usb_adapter *adapter, int index,
917                 u16 pid, int onoff)
918 {
919         struct dib0700_adapter_state *state = adapter->priv;
920         struct dib0700_state *st = adapter->dev->priv;
921
922         if (st->is_dib7000pc)
923                 return state->dib7000p_ops.pid_filter(adapter->fe_adap[0].fe, index, pid, onoff);
924         return dib7000m_pid_filter(adapter->fe_adap[0].fe, index, pid, onoff);
925 }
926
927 static int stk7700p_pid_filter_ctrl(struct dvb_usb_adapter *adapter, int onoff)
928 {
929         struct dib0700_state *st = adapter->dev->priv;
930         struct dib0700_adapter_state *state = adapter->priv;
931         if (st->is_dib7000pc)
932                 return state->dib7000p_ops.pid_filter_ctrl(adapter->fe_adap[0].fe, onoff);
933         return dib7000m_pid_filter_ctrl(adapter->fe_adap[0].fe, onoff);
934 }
935
936 static int stk70x0p_pid_filter(struct dvb_usb_adapter *adapter, int index, u16 pid, int onoff)
937 {
938         struct dib0700_adapter_state *state = adapter->priv;
939         return state->dib7000p_ops.pid_filter(adapter->fe_adap[0].fe, index, pid, onoff);
940 }
941
942 static int stk70x0p_pid_filter_ctrl(struct dvb_usb_adapter *adapter, int onoff)
943 {
944         struct dib0700_adapter_state *state = adapter->priv;
945         return state->dib7000p_ops.pid_filter_ctrl(adapter->fe_adap[0].fe, onoff);
946 }
947
948 static struct dibx000_bandwidth_config dib7070_bw_config_12_mhz = {
949         .internal = 60000,
950         .sampling = 15000,
951         .pll_prediv = 1,
952         .pll_ratio = 20,
953         .pll_range = 3,
954         .pll_reset = 1,
955         .pll_bypass = 0,
956         .enable_refdiv = 0,
957         .bypclk_div = 0,
958         .IO_CLK_en_core = 1,
959         .ADClkSrc = 1,
960         .modulo = 2,
961         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
962         .ifreq = (0 << 25) | 0,
963         .timf = 20452225,
964         .xtal_hz = 12000000,
965 };
966
967 static struct dib7000p_config dib7070p_dib7000p_config = {
968         .output_mpeg2_in_188_bytes = 1,
969
970         .agc_config_count = 1,
971         .agc = &dib7070_agc_config,
972         .bw  = &dib7070_bw_config_12_mhz,
973         .tuner_is_baseband = 1,
974         .spur_protect = 1,
975
976         .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
977         .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
978         .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
979
980         .hostbus_diversity = 1,
981 };
982
983 /* STK7070P */
984 static int stk7070p_frontend_attach(struct dvb_usb_adapter *adap)
985 {
986         struct usb_device_descriptor *p = &adap->dev->udev->descriptor;
987         struct dib0700_adapter_state *state = adap->priv;
988
989         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
990                 return -ENODEV;
991
992         if (p->idVendor  == cpu_to_le16(USB_VID_PINNACLE) &&
993             p->idProduct == cpu_to_le16(USB_PID_PINNACLE_PCTV72E))
994                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
995         else
996                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
997         msleep(10);
998         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
999         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1000         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1001         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1002
1003         dib0700_ctrl_clock(adap->dev, 72, 1);
1004
1005         msleep(10);
1006         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1007         msleep(10);
1008         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1009
1010         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
1011                                      &dib7070p_dib7000p_config) != 0) {
1012                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
1013                     __func__);
1014                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
1015                 return -ENODEV;
1016         }
1017
1018         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x80,
1019                 &dib7070p_dib7000p_config);
1020         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
1021 }
1022
1023 /* STK7770P */
1024 static struct dib7000p_config dib7770p_dib7000p_config = {
1025         .output_mpeg2_in_188_bytes = 1,
1026
1027         .agc_config_count = 1,
1028         .agc = &dib7070_agc_config,
1029         .bw  = &dib7070_bw_config_12_mhz,
1030         .tuner_is_baseband = 1,
1031         .spur_protect = 1,
1032
1033         .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
1034         .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
1035         .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
1036
1037         .hostbus_diversity = 1,
1038         .enable_current_mirror = 1,
1039         .disable_sample_and_hold = 0,
1040 };
1041
1042 static int stk7770p_frontend_attach(struct dvb_usb_adapter *adap)
1043 {
1044         struct usb_device_descriptor *p = &adap->dev->udev->descriptor;
1045         struct dib0700_adapter_state *state = adap->priv;
1046
1047         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
1048                 return -ENODEV;
1049
1050         if (p->idVendor  == cpu_to_le16(USB_VID_PINNACLE) &&
1051             p->idProduct == cpu_to_le16(USB_PID_PINNACLE_PCTV72E))
1052                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
1053         else
1054                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
1055         msleep(10);
1056         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
1057         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1058         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1059         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1060
1061         dib0700_ctrl_clock(adap->dev, 72, 1);
1062
1063         msleep(10);
1064         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1065         msleep(10);
1066         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1067
1068         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
1069                                      &dib7770p_dib7000p_config) != 0) {
1070                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
1071                     __func__);
1072                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
1073                 return -ENODEV;
1074         }
1075
1076         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x80,
1077                 &dib7770p_dib7000p_config);
1078         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
1079 }
1080
1081 /* DIB807x generic */
1082 static struct dibx000_agc_config dib807x_agc_config[2] = {
1083         {
1084                 BAND_VHF,
1085                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0,
1086                  * P_agc_freq_pwm_div=1, P_agc_inv_pwm1=0,
1087                  * P_agc_inv_pwm2=0,P_agc_inh_dc_rv_est=0,
1088                  * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5,
1089                  * P_agc_write=0 */
1090                 (0 << 15) | (0 << 14) | (7 << 11) | (0 << 10) | (0 << 9) |
1091                         (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) |
1092                         (0 << 0), /* setup*/
1093
1094                 600, /* inv_gain*/
1095                 10,  /* time_stabiliz*/
1096
1097                 0,  /* alpha_level*/
1098                 118,  /* thlock*/
1099
1100                 0,     /* wbd_inv*/
1101                 3530,  /* wbd_ref*/
1102                 1,     /* wbd_sel*/
1103                 5,     /* wbd_alpha*/
1104
1105                 65535,  /* agc1_max*/
1106                 0,  /* agc1_min*/
1107
1108                 65535,  /* agc2_max*/
1109                 0,      /* agc2_min*/
1110
1111                 0,      /* agc1_pt1*/
1112                 40,     /* agc1_pt2*/
1113                 183,    /* agc1_pt3*/
1114                 206,    /* agc1_slope1*/
1115                 255,    /* agc1_slope2*/
1116                 72,     /* agc2_pt1*/
1117                 152,    /* agc2_pt2*/
1118                 88,     /* agc2_slope1*/
1119                 90,     /* agc2_slope2*/
1120
1121                 17,  /* alpha_mant*/
1122                 27,  /* alpha_exp*/
1123                 23,  /* beta_mant*/
1124                 51,  /* beta_exp*/
1125
1126                 0,  /* perform_agc_softsplit*/
1127         }, {
1128                 BAND_UHF,
1129                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0,
1130                  * P_agc_freq_pwm_div=1, P_agc_inv_pwm1=0,
1131                  * P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1132                  * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5,
1133                  * P_agc_write=0 */
1134                 (0 << 15) | (0 << 14) | (1 << 11) | (0 << 10) | (0 << 9) |
1135                         (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) |
1136                         (0 << 0), /* setup */
1137
1138                 600, /* inv_gain*/
1139                 10,  /* time_stabiliz*/
1140
1141                 0,  /* alpha_level*/
1142                 118,  /* thlock*/
1143
1144                 0,     /* wbd_inv*/
1145                 3530,  /* wbd_ref*/
1146                 1,     /* wbd_sel*/
1147                 5,     /* wbd_alpha*/
1148
1149                 65535,  /* agc1_max*/
1150                 0,  /* agc1_min*/
1151
1152                 65535,  /* agc2_max*/
1153                 0,      /* agc2_min*/
1154
1155                 0,      /* agc1_pt1*/
1156                 40,     /* agc1_pt2*/
1157                 183,    /* agc1_pt3*/
1158                 206,    /* agc1_slope1*/
1159                 255,    /* agc1_slope2*/
1160                 72,     /* agc2_pt1*/
1161                 152,    /* agc2_pt2*/
1162                 88,     /* agc2_slope1*/
1163                 90,     /* agc2_slope2*/
1164
1165                 17,  /* alpha_mant*/
1166                 27,  /* alpha_exp*/
1167                 23,  /* beta_mant*/
1168                 51,  /* beta_exp*/
1169
1170                 0,  /* perform_agc_softsplit*/
1171         }
1172 };
1173
1174 static struct dibx000_bandwidth_config dib807x_bw_config_12_mhz = {
1175         .internal = 60000,
1176         .sampling = 15000,
1177         .pll_prediv = 1,
1178         .pll_ratio = 20,
1179         .pll_range = 3,
1180         .pll_reset = 1,
1181         .pll_bypass = 0,
1182         .enable_refdiv = 0,
1183         .bypclk_div = 0,
1184         .IO_CLK_en_core = 1,
1185         .ADClkSrc = 1,
1186         .modulo = 2,
1187         .sad_cfg = (3 << 14) | (1 << 12) | (599 << 0),  /* sad_cfg: refsel, sel, freq_15k*/
1188         .ifreq = (0 << 25) | 0,                         /* ifreq = 0.000000 MHz*/
1189         .timf = 18179755,
1190         .xtal_hz = 12000000,
1191 };
1192
1193 static struct dib8000_config dib807x_dib8000_config[2] = {
1194         {
1195                 .output_mpeg2_in_188_bytes = 1,
1196
1197                 .agc_config_count = 2,
1198                 .agc = dib807x_agc_config,
1199                 .pll = &dib807x_bw_config_12_mhz,
1200                 .tuner_is_baseband = 1,
1201
1202                 .gpio_dir = DIB8000_GPIO_DEFAULT_DIRECTIONS,
1203                 .gpio_val = DIB8000_GPIO_DEFAULT_VALUES,
1204                 .gpio_pwm_pos = DIB8000_GPIO_DEFAULT_PWM_POS,
1205
1206                 .hostbus_diversity = 1,
1207                 .div_cfg = 1,
1208                 .agc_control = &dib0070_ctrl_agc_filter,
1209                 .output_mode = OUTMODE_MPEG2_FIFO,
1210                 .drives = 0x2d98,
1211         }, {
1212                 .output_mpeg2_in_188_bytes = 1,
1213
1214                 .agc_config_count = 2,
1215                 .agc = dib807x_agc_config,
1216                 .pll = &dib807x_bw_config_12_mhz,
1217                 .tuner_is_baseband = 1,
1218
1219                 .gpio_dir = DIB8000_GPIO_DEFAULT_DIRECTIONS,
1220                 .gpio_val = DIB8000_GPIO_DEFAULT_VALUES,
1221                 .gpio_pwm_pos = DIB8000_GPIO_DEFAULT_PWM_POS,
1222
1223                 .hostbus_diversity = 1,
1224                 .agc_control = &dib0070_ctrl_agc_filter,
1225                 .output_mode = OUTMODE_MPEG2_FIFO,
1226                 .drives = 0x2d98,
1227         }
1228 };
1229
1230 static int dib80xx_tuner_reset(struct dvb_frontend *fe, int onoff)
1231 {
1232         struct dvb_usb_adapter *adap = fe->dvb->priv;
1233         struct dib0700_adapter_state *state = adap->priv;
1234
1235         return state->dib8000_ops.set_gpio(fe, 5, 0, !onoff);
1236 }
1237
1238 static int dib80xx_tuner_sleep(struct dvb_frontend *fe, int onoff)
1239 {
1240         struct dvb_usb_adapter *adap = fe->dvb->priv;
1241         struct dib0700_adapter_state *state = adap->priv;
1242
1243         return state->dib8000_ops.set_gpio(fe, 0, 0, onoff);
1244 }
1245
1246 static const struct dib0070_wbd_gain_cfg dib8070_wbd_gain_cfg[] = {
1247     { 240,      7},
1248     { 0xffff,   6},
1249 };
1250
1251 static struct dib0070_config dib807x_dib0070_config[2] = {
1252         {
1253                 .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
1254                 .reset = dib80xx_tuner_reset,
1255                 .sleep = dib80xx_tuner_sleep,
1256                 .clock_khz = 12000,
1257                 .clock_pad_drive = 4,
1258                 .vga_filter = 1,
1259                 .force_crystal_mode = 1,
1260                 .enable_third_order_filter = 1,
1261                 .charge_pump = 0,
1262                 .wbd_gain = dib8070_wbd_gain_cfg,
1263                 .osc_buffer_state = 0,
1264                 .freq_offset_khz_uhf = -100,
1265                 .freq_offset_khz_vhf = -100,
1266         }, {
1267                 .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
1268                 .reset = dib80xx_tuner_reset,
1269                 .sleep = dib80xx_tuner_sleep,
1270                 .clock_khz = 12000,
1271                 .clock_pad_drive = 2,
1272                 .vga_filter = 1,
1273                 .force_crystal_mode = 1,
1274                 .enable_third_order_filter = 1,
1275                 .charge_pump = 0,
1276                 .wbd_gain = dib8070_wbd_gain_cfg,
1277                 .osc_buffer_state = 0,
1278                 .freq_offset_khz_uhf = -25,
1279                 .freq_offset_khz_vhf = -25,
1280         }
1281 };
1282
1283 static int dib807x_set_param_override(struct dvb_frontend *fe)
1284 {
1285         struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1286         struct dvb_usb_adapter *adap = fe->dvb->priv;
1287         struct dib0700_adapter_state *state = adap->priv;
1288
1289         u16 offset = dib0070_wbd_offset(fe);
1290         u8 band = BAND_OF_FREQUENCY(p->frequency/1000);
1291         switch (band) {
1292         case BAND_VHF:
1293                 offset += 750;
1294                 break;
1295         case BAND_UHF:  /* fall-thru wanted */
1296         default:
1297                 offset += 250; break;
1298         }
1299         deb_info("WBD for DiB8000: %d\n", offset);
1300         state->dib8000_ops.set_wbd_ref(fe, offset);
1301
1302         return state->set_param_save(fe);
1303 }
1304
1305 static int dib807x_tuner_attach(struct dvb_usb_adapter *adap)
1306 {
1307         struct dib0700_adapter_state *st = adap->priv;
1308         struct i2c_adapter *tun_i2c = st->dib8000_ops.get_i2c_master(adap->fe_adap[0].fe,
1309                         DIBX000_I2C_INTERFACE_TUNER, 1);
1310
1311         if (adap->id == 0) {
1312                 if (dvb_attach(dib0070_attach, adap->fe_adap[0].fe, tun_i2c,
1313                                 &dib807x_dib0070_config[0]) == NULL)
1314                         return -ENODEV;
1315         } else {
1316                 if (dvb_attach(dib0070_attach, adap->fe_adap[0].fe, tun_i2c,
1317                                 &dib807x_dib0070_config[1]) == NULL)
1318                         return -ENODEV;
1319         }
1320
1321         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
1322         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib807x_set_param_override;
1323         return 0;
1324 }
1325
1326 static int stk80xx_pid_filter(struct dvb_usb_adapter *adapter, int index,
1327         u16 pid, int onoff)
1328 {
1329         struct dib0700_adapter_state *state = adapter->priv;
1330
1331         return state->dib8000_ops.pid_filter(adapter->fe_adap[0].fe, index, pid, onoff);
1332 }
1333
1334 static int stk80xx_pid_filter_ctrl(struct dvb_usb_adapter *adapter,
1335                 int onoff)
1336 {
1337         struct dib0700_adapter_state *state = adapter->priv;
1338
1339         return state->dib8000_ops.pid_filter_ctrl(adapter->fe_adap[0].fe, onoff);
1340 }
1341
1342 /* STK807x */
1343 static int stk807x_frontend_attach(struct dvb_usb_adapter *adap)
1344 {
1345         struct dib0700_adapter_state *state = adap->priv;
1346
1347         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1348                 return -ENODEV;
1349
1350         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
1351         msleep(10);
1352         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
1353         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1354         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1355
1356         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1357
1358         dib0700_ctrl_clock(adap->dev, 72, 1);
1359
1360         msleep(10);
1361         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1362         msleep(10);
1363         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1364
1365         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
1366                                 0x80, 0);
1367
1368         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x80,
1369                               &dib807x_dib8000_config[0]);
1370
1371         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
1372 }
1373
1374 /* STK807xPVR */
1375 static int stk807xpvr_frontend_attach0(struct dvb_usb_adapter *adap)
1376 {
1377         struct dib0700_adapter_state *state = adap->priv;
1378
1379         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1380                 return -ENODEV;
1381
1382         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
1383         msleep(30);
1384         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
1385         msleep(500);
1386         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
1387         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1388         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1389
1390         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1391
1392         dib0700_ctrl_clock(adap->dev, 72, 1);
1393
1394         msleep(10);
1395         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1396         msleep(10);
1397         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1398
1399         /* initialize IC 0 */
1400         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 0x22, 0x80, 0);
1401
1402         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x80,
1403                               &dib807x_dib8000_config[0]);
1404
1405         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
1406 }
1407
1408 static int stk807xpvr_frontend_attach1(struct dvb_usb_adapter *adap)
1409 {
1410         struct dib0700_adapter_state *state = adap->priv;
1411
1412         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1413                 return -ENODEV;
1414
1415         /* initialize IC 1 */
1416         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 0x12, 0x82, 0);
1417
1418         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x82,
1419                               &dib807x_dib8000_config[1]);
1420
1421         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
1422 }
1423
1424 /* STK8096GP */
1425 static struct dibx000_agc_config dib8090_agc_config[2] = {
1426         {
1427         .band_caps = BAND_UHF | BAND_VHF | BAND_LBAND | BAND_SBAND,
1428         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=1,
1429          * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1430          * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0 */
1431         .setup = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8)
1432         | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
1433
1434         .inv_gain = 787,
1435         .time_stabiliz = 10,
1436
1437         .alpha_level = 0,
1438         .thlock = 118,
1439
1440         .wbd_inv = 0,
1441         .wbd_ref = 3530,
1442         .wbd_sel = 1,
1443         .wbd_alpha = 5,
1444
1445         .agc1_max = 65535,
1446         .agc1_min = 0,
1447
1448         .agc2_max = 65535,
1449         .agc2_min = 0,
1450
1451         .agc1_pt1 = 0,
1452         .agc1_pt2 = 32,
1453         .agc1_pt3 = 114,
1454         .agc1_slope1 = 143,
1455         .agc1_slope2 = 144,
1456         .agc2_pt1 = 114,
1457         .agc2_pt2 = 227,
1458         .agc2_slope1 = 116,
1459         .agc2_slope2 = 117,
1460
1461         .alpha_mant = 28,
1462         .alpha_exp = 26,
1463         .beta_mant = 31,
1464         .beta_exp = 51,
1465
1466         .perform_agc_softsplit = 0,
1467         },
1468         {
1469         .band_caps = BAND_CBAND,
1470         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=1,
1471          * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1472          * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0 */
1473         .setup = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8)
1474         | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
1475
1476         .inv_gain = 787,
1477         .time_stabiliz = 10,
1478
1479         .alpha_level = 0,
1480         .thlock = 118,
1481
1482         .wbd_inv = 0,
1483         .wbd_ref = 3530,
1484         .wbd_sel = 1,
1485         .wbd_alpha = 5,
1486
1487         .agc1_max = 0,
1488         .agc1_min = 0,
1489
1490         .agc2_max = 65535,
1491         .agc2_min = 0,
1492
1493         .agc1_pt1 = 0,
1494         .agc1_pt2 = 32,
1495         .agc1_pt3 = 114,
1496         .agc1_slope1 = 143,
1497         .agc1_slope2 = 144,
1498         .agc2_pt1 = 114,
1499         .agc2_pt2 = 227,
1500         .agc2_slope1 = 116,
1501         .agc2_slope2 = 117,
1502
1503         .alpha_mant = 28,
1504         .alpha_exp = 26,
1505         .beta_mant = 31,
1506         .beta_exp = 51,
1507
1508         .perform_agc_softsplit = 0,
1509         }
1510 };
1511
1512 static struct dibx000_bandwidth_config dib8090_pll_config_12mhz = {
1513         .internal = 54000,
1514         .sampling = 13500,
1515
1516         .pll_prediv = 1,
1517         .pll_ratio = 18,
1518         .pll_range = 3,
1519         .pll_reset = 1,
1520         .pll_bypass = 0,
1521
1522         .enable_refdiv = 0,
1523         .bypclk_div = 0,
1524         .IO_CLK_en_core = 1,
1525         .ADClkSrc = 1,
1526         .modulo = 2,
1527
1528         .sad_cfg = (3 << 14) | (1 << 12) | (599 << 0),
1529
1530         .ifreq = (0 << 25) | 0,
1531         .timf = 20199727,
1532
1533         .xtal_hz = 12000000,
1534 };
1535
1536 static int dib8090_get_adc_power(struct dvb_frontend *fe)
1537 {
1538         struct dvb_usb_adapter *adap = fe->dvb->priv;
1539         struct dib0700_adapter_state *state = adap->priv;
1540
1541         return state->dib8000_ops.get_adc_power(fe, 1);
1542 }
1543
1544 static void dib8090_agc_control(struct dvb_frontend *fe, u8 restart)
1545 {
1546         deb_info("AGC control callback: %i\n", restart);
1547         dib0090_dcc_freq(fe, restart);
1548
1549         if (restart == 0) /* before AGC startup */
1550                 dib0090_set_dc_servo(fe, 1);
1551 }
1552
1553 static struct dib8000_config dib809x_dib8000_config[2] = {
1554         {
1555         .output_mpeg2_in_188_bytes = 1,
1556
1557         .agc_config_count = 2,
1558         .agc = dib8090_agc_config,
1559         .agc_control = dib8090_agc_control,
1560         .pll = &dib8090_pll_config_12mhz,
1561         .tuner_is_baseband = 1,
1562
1563         .gpio_dir = DIB8000_GPIO_DEFAULT_DIRECTIONS,
1564         .gpio_val = DIB8000_GPIO_DEFAULT_VALUES,
1565         .gpio_pwm_pos = DIB8000_GPIO_DEFAULT_PWM_POS,
1566
1567         .hostbus_diversity = 1,
1568         .div_cfg = 0x31,
1569         .output_mode = OUTMODE_MPEG2_FIFO,
1570         .drives = 0x2d98,
1571         .diversity_delay = 48,
1572         .refclksel = 3,
1573         }, {
1574         .output_mpeg2_in_188_bytes = 1,
1575
1576         .agc_config_count = 2,
1577         .agc = dib8090_agc_config,
1578         .agc_control = dib8090_agc_control,
1579         .pll = &dib8090_pll_config_12mhz,
1580         .tuner_is_baseband = 1,
1581
1582         .gpio_dir = DIB8000_GPIO_DEFAULT_DIRECTIONS,
1583         .gpio_val = DIB8000_GPIO_DEFAULT_VALUES,
1584         .gpio_pwm_pos = DIB8000_GPIO_DEFAULT_PWM_POS,
1585
1586         .hostbus_diversity = 1,
1587         .div_cfg = 0x31,
1588         .output_mode = OUTMODE_DIVERSITY,
1589         .drives = 0x2d08,
1590         .diversity_delay = 1,
1591         .refclksel = 3,
1592         }
1593 };
1594
1595 static struct dib0090_wbd_slope dib8090_wbd_table[] = {
1596         /* max freq ; cold slope ; cold offset ; warm slope ; warm offset ; wbd gain */
1597         { 120,     0, 500,  0,   500, 4 }, /* CBAND */
1598         { 170,     0, 450,  0,   450, 4 }, /* CBAND */
1599         { 380,    48, 373, 28,   259, 6 }, /* VHF */
1600         { 860,    34, 700, 36,   616, 6 }, /* high UHF */
1601         { 0xFFFF, 34, 700, 36,   616, 6 }, /* default */
1602 };
1603
1604 static struct dib0090_config dib809x_dib0090_config = {
1605         .io.pll_bypass = 1,
1606         .io.pll_range = 1,
1607         .io.pll_prediv = 1,
1608         .io.pll_loopdiv = 20,
1609         .io.adc_clock_ratio = 8,
1610         .io.pll_int_loop_filt = 0,
1611         .io.clock_khz = 12000,
1612         .reset = dib80xx_tuner_reset,
1613         .sleep = dib80xx_tuner_sleep,
1614         .clkouttobamse = 1,
1615         .analog_output = 1,
1616         .i2c_address = DEFAULT_DIB0090_I2C_ADDRESS,
1617         .use_pwm_agc = 1,
1618         .clkoutdrive = 1,
1619         .get_adc_power = dib8090_get_adc_power,
1620         .freq_offset_khz_uhf = -63,
1621         .freq_offset_khz_vhf = -143,
1622         .wbd = dib8090_wbd_table,
1623         .fref_clock_ratio = 6,
1624 };
1625
1626 static u8 dib8090_compute_pll_parameters(struct dvb_frontend *fe)
1627 {
1628         u8 optimal_pll_ratio = 20;
1629         u32 freq_adc, ratio, rest, max = 0;
1630         u8 pll_ratio;
1631
1632         for (pll_ratio = 17; pll_ratio <= 20; pll_ratio++) {
1633                 freq_adc = 12 * pll_ratio * (1 << 8) / 16;
1634                 ratio = ((fe->dtv_property_cache.frequency / 1000) * (1 << 8) / 1000) / freq_adc;
1635                 rest = ((fe->dtv_property_cache.frequency / 1000) * (1 << 8) / 1000) - ratio * freq_adc;
1636
1637                 if (rest > freq_adc / 2)
1638                         rest = freq_adc - rest;
1639                 deb_info("PLL ratio=%i rest=%i\n", pll_ratio, rest);
1640                 if ((rest > max) && (rest > 717)) {
1641                         optimal_pll_ratio = pll_ratio;
1642                         max = rest;
1643                 }
1644         }
1645         deb_info("optimal PLL ratio=%i\n", optimal_pll_ratio);
1646
1647         return optimal_pll_ratio;
1648 }
1649
1650 static int dib8096_set_param_override(struct dvb_frontend *fe)
1651 {
1652         struct dvb_usb_adapter *adap = fe->dvb->priv;
1653         struct dib0700_adapter_state *state = adap->priv;
1654         u8 pll_ratio, band = BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000);
1655         u16 target, ltgain, rf_gain_limit;
1656         u32 timf;
1657         int ret = 0;
1658         enum frontend_tune_state tune_state = CT_SHUTDOWN;
1659
1660         switch (band) {
1661         default:
1662                         deb_info("Warning : Rf frequency  (%iHz) is not in the supported range, using VHF switch ", fe->dtv_property_cache.frequency);
1663                         /* fall through */
1664         case BAND_VHF:
1665                         state->dib8000_ops.set_gpio(fe, 3, 0, 1);
1666                         break;
1667         case BAND_UHF:
1668                         state->dib8000_ops.set_gpio(fe, 3, 0, 0);
1669                         break;
1670         }
1671
1672         ret = state->set_param_save(fe);
1673         if (ret < 0)
1674                 return ret;
1675
1676         if (fe->dtv_property_cache.bandwidth_hz != 6000000) {
1677                 deb_info("only 6MHz bandwidth is supported\n");
1678                 return -EINVAL;
1679         }
1680
1681         /** Update PLL if needed ratio **/
1682         state->dib8000_ops.update_pll(fe, &dib8090_pll_config_12mhz, fe->dtv_property_cache.bandwidth_hz / 1000, 0);
1683
1684         /** Get optimize PLL ratio to remove spurious **/
1685         pll_ratio = dib8090_compute_pll_parameters(fe);
1686         if (pll_ratio == 17)
1687                 timf = 21387946;
1688         else if (pll_ratio == 18)
1689                 timf = 20199727;
1690         else if (pll_ratio == 19)
1691                 timf = 19136583;
1692         else
1693                 timf = 18179756;
1694
1695         /** Update ratio **/
1696         state->dib8000_ops.update_pll(fe, &dib8090_pll_config_12mhz, fe->dtv_property_cache.bandwidth_hz / 1000, pll_ratio);
1697
1698         state->dib8000_ops.ctrl_timf(fe, DEMOD_TIMF_SET, timf);
1699
1700         if (band != BAND_CBAND) {
1701                 /* dib0090_get_wbd_target is returning any possible temperature compensated wbd-target */
1702                 target = (dib0090_get_wbd_target(fe) * 8 * 18 / 33 + 1) / 2;
1703                 state->dib8000_ops.set_wbd_ref(fe, target);
1704         }
1705
1706         if (band == BAND_CBAND) {
1707                 deb_info("tuning in CBAND - soft-AGC startup\n");
1708                 dib0090_set_tune_state(fe, CT_AGC_START);
1709
1710                 do {
1711                         ret = dib0090_gain_control(fe);
1712                         msleep(ret);
1713                         tune_state = dib0090_get_tune_state(fe);
1714                         if (tune_state == CT_AGC_STEP_0)
1715                                 state->dib8000_ops.set_gpio(fe, 6, 0, 1);
1716                         else if (tune_state == CT_AGC_STEP_1) {
1717                                 dib0090_get_current_gain(fe, NULL, NULL, &rf_gain_limit, &ltgain);
1718                                 if (rf_gain_limit < 2000) /* activate the external attenuator in case of very high input power */
1719                                         state->dib8000_ops.set_gpio(fe, 6, 0, 0);
1720                         }
1721                 } while (tune_state < CT_AGC_STOP);
1722
1723                 deb_info("switching to PWM AGC\n");
1724                 dib0090_pwm_gain_reset(fe);
1725                 state->dib8000_ops.pwm_agc_reset(fe);
1726                 state->dib8000_ops.set_tune_state(fe, CT_DEMOD_START);
1727         } else {
1728                 /* for everything else than CBAND we are using standard AGC */
1729                 deb_info("not tuning in CBAND - standard AGC startup\n");
1730                 dib0090_pwm_gain_reset(fe);
1731         }
1732
1733         return 0;
1734 }
1735
1736 static int dib809x_tuner_attach(struct dvb_usb_adapter *adap)
1737 {
1738         struct dib0700_adapter_state *st = adap->priv;
1739         struct i2c_adapter *tun_i2c = st->dib8000_ops.get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_TUNER, 1);
1740
1741         if (adap->id == 0) {
1742                 if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &dib809x_dib0090_config) == NULL)
1743                         return -ENODEV;
1744         } else {
1745                 if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &dib809x_dib0090_config) == NULL)
1746                         return -ENODEV;
1747         }
1748
1749         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
1750         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib8096_set_param_override;
1751         return 0;
1752 }
1753
1754 static int stk809x_frontend_attach(struct dvb_usb_adapter *adap)
1755 {
1756         struct dib0700_adapter_state *state = adap->priv;
1757
1758         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1759                 return -ENODEV;
1760
1761         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
1762         msleep(10);
1763         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
1764         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1765         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1766
1767         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1768
1769         dib0700_ctrl_clock(adap->dev, 72, 1);
1770
1771         msleep(10);
1772         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1773         msleep(10);
1774         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1775
1776         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18, 0x80, 0);
1777
1778         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x80, &dib809x_dib8000_config[0]);
1779
1780         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
1781 }
1782
1783 static int stk809x_frontend1_attach(struct dvb_usb_adapter *adap)
1784 {
1785         struct dib0700_adapter_state *state = adap->priv;
1786
1787         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1788                 return -ENODEV;
1789
1790         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 0x10, 0x82, 0);
1791
1792         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x82, &dib809x_dib8000_config[1]);
1793
1794         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
1795 }
1796
1797 static int nim8096md_tuner_attach(struct dvb_usb_adapter *adap)
1798 {
1799         struct dib0700_adapter_state *st = adap->priv;
1800         struct i2c_adapter *tun_i2c;
1801         struct dvb_frontend *fe_slave  = st->dib8000_ops.get_slave_frontend(adap->fe_adap[0].fe, 1);
1802
1803         if (fe_slave) {
1804                 tun_i2c = st->dib8000_ops.get_i2c_master(fe_slave, DIBX000_I2C_INTERFACE_TUNER, 1);
1805                 if (dvb_attach(dib0090_register, fe_slave, tun_i2c, &dib809x_dib0090_config) == NULL)
1806                         return -ENODEV;
1807                 fe_slave->dvb = adap->fe_adap[0].fe->dvb;
1808                 fe_slave->ops.tuner_ops.set_params = dib8096_set_param_override;
1809         }
1810         tun_i2c = st->dib8000_ops.get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_TUNER, 1);
1811         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &dib809x_dib0090_config) == NULL)
1812                 return -ENODEV;
1813
1814         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
1815         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib8096_set_param_override;
1816
1817         return 0;
1818 }
1819
1820 static int nim8096md_frontend_attach(struct dvb_usb_adapter *adap)
1821 {
1822         struct dvb_frontend *fe_slave;
1823         struct dib0700_adapter_state *state = adap->priv;
1824
1825         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1826                 return -ENODEV;
1827
1828         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
1829         msleep(20);
1830         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
1831         msleep(1000);
1832         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
1833         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1834         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1835
1836         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1837
1838         dib0700_ctrl_clock(adap->dev, 72, 1);
1839
1840         msleep(20);
1841         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1842         msleep(20);
1843         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1844
1845         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 2, 18, 0x80, 0);
1846
1847         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x80, &dib809x_dib8000_config[0]);
1848         if (adap->fe_adap[0].fe == NULL)
1849                 return -ENODEV;
1850
1851         /* Needed to increment refcount */
1852         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1853                 return -ENODEV;
1854
1855         fe_slave = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x82, &dib809x_dib8000_config[1]);
1856         state->dib8000_ops.set_slave_frontend(adap->fe_adap[0].fe, fe_slave);
1857
1858         return fe_slave == NULL ?  -ENODEV : 0;
1859 }
1860
1861 /* TFE8096P */
1862 static struct dibx000_agc_config dib8096p_agc_config[2] = {
1863         {
1864                 .band_caps              = BAND_UHF,
1865                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0,
1866                    P_agc_freq_pwm_div=1, P_agc_inv_pwm1=0,
1867                    P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1868                    P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5,
1869                    P_agc_write=0 */
1870                 .setup                  = (0 << 15) | (0 << 14) | (5 << 11)
1871                         | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5)
1872                         | (0 << 4) | (5 << 1) | (0 << 0),
1873
1874                 .inv_gain               = 684,
1875                 .time_stabiliz  = 10,
1876
1877                 .alpha_level    = 0,
1878                 .thlock                 = 118,
1879
1880                 .wbd_inv                = 0,
1881                 .wbd_ref                = 1200,
1882                 .wbd_sel                = 3,
1883                 .wbd_alpha              = 5,
1884
1885                 .agc1_max               = 65535,
1886                 .agc1_min               = 0,
1887
1888                 .agc2_max               = 32767,
1889                 .agc2_min               = 0,
1890
1891                 .agc1_pt1               = 0,
1892                 .agc1_pt2               = 0,
1893                 .agc1_pt3               = 105,
1894                 .agc1_slope1    = 0,
1895                 .agc1_slope2    = 156,
1896                 .agc2_pt1               = 105,
1897                 .agc2_pt2               = 255,
1898                 .agc2_slope1    = 54,
1899                 .agc2_slope2    = 0,
1900
1901                 .alpha_mant             = 28,
1902                 .alpha_exp              = 26,
1903                 .beta_mant              = 31,
1904                 .beta_exp               = 51,
1905
1906                 .perform_agc_softsplit = 0,
1907         } , {
1908                 .band_caps              = BAND_FM | BAND_VHF | BAND_CBAND,
1909                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0,
1910                    P_agc_freq_pwm_div=1, P_agc_inv_pwm1=0,
1911                    P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1912                    P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5,
1913                    P_agc_write=0 */
1914                 .setup                  = (0 << 15) | (0 << 14) | (5 << 11)
1915                         | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5)
1916                         | (0 << 4) | (5 << 1) | (0 << 0),
1917
1918                 .inv_gain               = 732,
1919                 .time_stabiliz  = 10,
1920
1921                 .alpha_level    = 0,
1922                 .thlock                 = 118,
1923
1924                 .wbd_inv                = 0,
1925                 .wbd_ref                = 1200,
1926                 .wbd_sel                = 3,
1927                 .wbd_alpha              = 5,
1928
1929                 .agc1_max               = 65535,
1930                 .agc1_min               = 0,
1931
1932                 .agc2_max               = 32767,
1933                 .agc2_min               = 0,
1934
1935                 .agc1_pt1               = 0,
1936                 .agc1_pt2               = 0,
1937                 .agc1_pt3               = 98,
1938                 .agc1_slope1    = 0,
1939                 .agc1_slope2    = 167,
1940                 .agc2_pt1               = 98,
1941                 .agc2_pt2               = 255,
1942                 .agc2_slope1    = 52,
1943                 .agc2_slope2    = 0,
1944
1945                 .alpha_mant             = 28,
1946                 .alpha_exp              = 26,
1947                 .beta_mant              = 31,
1948                 .beta_exp               = 51,
1949
1950                 .perform_agc_softsplit = 0,
1951         }
1952 };
1953
1954 static struct dibx000_bandwidth_config dib8096p_clock_config_12_mhz = {
1955         .internal = 108000,
1956         .sampling = 13500,
1957         .pll_prediv = 1,
1958         .pll_ratio = 9,
1959         .pll_range = 1,
1960         .pll_reset = 0,
1961         .pll_bypass = 0,
1962         .enable_refdiv = 0,
1963         .bypclk_div = 0,
1964         .IO_CLK_en_core = 0,
1965         .ADClkSrc = 0,
1966         .modulo = 2,
1967         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
1968         .ifreq = (0 << 25) | 0,
1969         .timf = 20199729,
1970         .xtal_hz = 12000000,
1971 };
1972
1973 static struct dib8000_config tfe8096p_dib8000_config = {
1974         .output_mpeg2_in_188_bytes      = 1,
1975         .hostbus_diversity                      = 1,
1976         .update_lna                                     = NULL,
1977
1978         .agc_config_count                       = 2,
1979         .agc                                            = dib8096p_agc_config,
1980         .pll                                            = &dib8096p_clock_config_12_mhz,
1981
1982         .gpio_dir                                       = DIB8000_GPIO_DEFAULT_DIRECTIONS,
1983         .gpio_val                                       = DIB8000_GPIO_DEFAULT_VALUES,
1984         .gpio_pwm_pos                           = DIB8000_GPIO_DEFAULT_PWM_POS,
1985
1986         .agc_control                            = NULL,
1987         .diversity_delay                        = 48,
1988         .output_mode                            = OUTMODE_MPEG2_FIFO,
1989         .enMpegOutput                           = 1,
1990 };
1991
1992 static struct dib0090_wbd_slope dib8096p_wbd_table[] = {
1993         { 380, 81, 850, 64, 540, 4},
1994         { 860, 51, 866, 21, 375, 4},
1995         {1700, 0, 250, 0, 100, 6},
1996         {2600, 0, 250, 0, 100, 6},
1997         { 0xFFFF, 0, 0, 0, 0, 0},
1998 };
1999
2000 static struct dib0090_config tfe8096p_dib0090_config = {
2001         .io.clock_khz                   = 12000,
2002         .io.pll_bypass                  = 0,
2003         .io.pll_range                   = 0,
2004         .io.pll_prediv                  = 3,
2005         .io.pll_loopdiv                 = 6,
2006         .io.adc_clock_ratio             = 0,
2007         .io.pll_int_loop_filt   = 0,
2008
2009         .freq_offset_khz_uhf    = -143,
2010         .freq_offset_khz_vhf    = -143,
2011
2012         .get_adc_power                  = dib8090_get_adc_power,
2013
2014         .clkouttobamse                  = 1,
2015         .analog_output                  = 0,
2016
2017         .wbd_vhf_offset                 = 0,
2018         .wbd_cband_offset               = 0,
2019         .use_pwm_agc                    = 1,
2020         .clkoutdrive                    = 0,
2021
2022         .fref_clock_ratio               = 1,
2023
2024         .ls_cfg_pad_drv                 = 0,
2025         .data_tx_drv                    = 0,
2026         .low_if                                 = NULL,
2027         .in_soc                                 = 1,
2028         .force_cband_input              = 0,
2029 };
2030
2031 struct dibx090p_adc {
2032         u32 freq;                       /* RF freq MHz */
2033         u32 timf;                       /* New Timf */
2034         u32 pll_loopdiv;        /* New prediv */
2035         u32 pll_prediv;         /* New loopdiv */
2036 };
2037
2038 struct dibx090p_best_adc {
2039         u32 timf;
2040         u32 pll_loopdiv;
2041         u32 pll_prediv;
2042 };
2043
2044 static int dib8096p_get_best_sampling(struct dvb_frontend *fe, struct dibx090p_best_adc *adc)
2045 {
2046         u8 spur = 0, prediv = 0, loopdiv = 0, min_prediv = 1, max_prediv = 1;
2047         u16 xtal = 12000;
2048         u16 fcp_min = 1900;  /* PLL, Minimum Frequency of phase comparator (KHz) */
2049         u16 fcp_max = 20000; /* PLL, Maximum Frequency of phase comparator (KHz) */
2050         u32 fmem_max = 140000; /* 140MHz max SDRAM freq */
2051         u32 fdem_min = 66000;
2052         u32 fcp = 0, fs = 0, fdem = 0, fmem = 0;
2053         u32 harmonic_id = 0;
2054
2055         adc->timf = 0;
2056         adc->pll_loopdiv = loopdiv;
2057         adc->pll_prediv = prediv;
2058
2059         deb_info("bandwidth = %d", fe->dtv_property_cache.bandwidth_hz);
2060
2061         /* Find Min and Max prediv */
2062         while ((xtal / max_prediv) >= fcp_min)
2063                 max_prediv++;
2064
2065         max_prediv--;
2066         min_prediv = max_prediv;
2067         while ((xtal / min_prediv) <= fcp_max) {
2068                 min_prediv--;
2069                 if (min_prediv == 1)
2070                         break;
2071         }
2072         deb_info("MIN prediv = %d : MAX prediv = %d", min_prediv, max_prediv);
2073
2074         min_prediv = 1;
2075
2076         for (prediv = min_prediv; prediv < max_prediv; prediv++) {
2077                 fcp = xtal / prediv;
2078                 if (fcp > fcp_min && fcp < fcp_max) {
2079                         for (loopdiv = 1; loopdiv < 64; loopdiv++) {
2080                                 fmem = ((xtal/prediv) * loopdiv);
2081                                 fdem = fmem / 2;
2082                                 fs   = fdem / 4;
2083
2084                                 /* test min/max system restrictions */
2085                                 if ((fdem >= fdem_min) && (fmem <= fmem_max) && (fs >= fe->dtv_property_cache.bandwidth_hz / 1000)) {
2086                                         spur = 0;
2087                                         /* test fs harmonics positions */
2088                                         for (harmonic_id = (fe->dtv_property_cache.frequency / (1000 * fs));  harmonic_id <= ((fe->dtv_property_cache.frequency / (1000 * fs)) + 1); harmonic_id++) {
2089                                                 if (((fs * harmonic_id) >= (fe->dtv_property_cache.frequency / 1000 - (fe->dtv_property_cache.bandwidth_hz / 2000))) &&  ((fs * harmonic_id) <= (fe->dtv_property_cache.frequency / 1000 + (fe->dtv_property_cache.bandwidth_hz / 2000)))) {
2090                                                         spur = 1;
2091                                                         break;
2092                                                 }
2093                                         }
2094
2095                                         if (!spur) {
2096                                                 adc->pll_loopdiv = loopdiv;
2097                                                 adc->pll_prediv = prediv;
2098                                                 adc->timf = (4260880253U / fdem) * (1 << 8);
2099                                                 adc->timf += ((4260880253U % fdem) << 8) / fdem;
2100
2101                                                 deb_info("RF %6d; BW %6d; Xtal %6d; Fmem %6d; Fdem %6d; Fs %6d; Prediv %2d; Loopdiv %2d; Timf %8d;", fe->dtv_property_cache.frequency, fe->dtv_property_cache.bandwidth_hz, xtal, fmem, fdem, fs, prediv, loopdiv, adc->timf);
2102                                                 break;
2103                                         }
2104                                 }
2105                         }
2106                 }
2107                 if (!spur)
2108                         break;
2109         }
2110
2111         if (adc->pll_loopdiv == 0 && adc->pll_prediv == 0)
2112                 return -EINVAL;
2113         return 0;
2114 }
2115
2116 static int dib8096p_agc_startup(struct dvb_frontend *fe)
2117 {
2118         struct dvb_usb_adapter *adap = fe->dvb->priv;
2119         struct dib0700_adapter_state *state = adap->priv;
2120         struct dibx000_bandwidth_config pll;
2121         struct dibx090p_best_adc adc;
2122         u16 target;
2123         int ret;
2124
2125         ret = state->set_param_save(fe);
2126         if (ret < 0)
2127                 return ret;
2128         memset(&pll, 0, sizeof(struct dibx000_bandwidth_config));
2129
2130         dib0090_pwm_gain_reset(fe);
2131         /* dib0090_get_wbd_target is returning any possible
2132            temperature compensated wbd-target */
2133         target = (dib0090_get_wbd_target(fe) * 8  + 1) / 2;
2134         state->dib8000_ops.set_wbd_ref(fe, target);
2135
2136         if (dib8096p_get_best_sampling(fe, &adc) == 0) {
2137                 pll.pll_ratio  = adc.pll_loopdiv;
2138                 pll.pll_prediv = adc.pll_prediv;
2139
2140                 dib0700_set_i2c_speed(adap->dev, 200);
2141                 state->dib8000_ops.update_pll(fe, &pll, fe->dtv_property_cache.bandwidth_hz / 1000, 0);
2142                 state->dib8000_ops.ctrl_timf(fe, DEMOD_TIMF_SET, adc.timf);
2143                 dib0700_set_i2c_speed(adap->dev, 1000);
2144         }
2145         return 0;
2146 }
2147
2148 static int tfe8096p_frontend_attach(struct dvb_usb_adapter *adap)
2149 {
2150         struct dib0700_state *st = adap->dev->priv;
2151         u32 fw_version;
2152         struct dib0700_adapter_state *state = adap->priv;
2153
2154         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
2155                 return -ENODEV;
2156
2157         dib0700_get_version(adap->dev, NULL, NULL, &fw_version, NULL);
2158         if (fw_version >= 0x10200)
2159                 st->fw_use_new_i2c_api = 1;
2160
2161         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
2162         msleep(20);
2163         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
2164         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
2165         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
2166
2167         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
2168
2169         dib0700_ctrl_clock(adap->dev, 72, 1);
2170
2171         msleep(20);
2172         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
2173         msleep(20);
2174         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
2175
2176         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 0x10, 0x80, 1);
2177
2178         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap,
2179                                              0x80, &tfe8096p_dib8000_config);
2180
2181         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
2182 }
2183
2184 static int tfe8096p_tuner_attach(struct dvb_usb_adapter *adap)
2185 {
2186         struct dib0700_adapter_state *st = adap->priv;
2187         struct i2c_adapter *tun_i2c = st->dib8000_ops.get_i2c_tuner(adap->fe_adap[0].fe);
2188
2189         tfe8096p_dib0090_config.reset = st->dib8000_ops.tuner_sleep;
2190         tfe8096p_dib0090_config.sleep = st->dib8000_ops.tuner_sleep;
2191         tfe8096p_dib0090_config.wbd = dib8096p_wbd_table;
2192
2193         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c,
2194                                 &tfe8096p_dib0090_config) == NULL)
2195                 return -ENODEV;
2196
2197         st->dib8000_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
2198
2199         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
2200         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib8096p_agc_startup;
2201         return 0;
2202 }
2203
2204 /* STK9090M */
2205 static int dib90x0_pid_filter(struct dvb_usb_adapter *adapter, int index, u16 pid, int onoff)
2206 {
2207         return dib9000_fw_pid_filter(adapter->fe_adap[0].fe, index, pid, onoff);
2208 }
2209
2210 static int dib90x0_pid_filter_ctrl(struct dvb_usb_adapter *adapter, int onoff)
2211 {
2212         return dib9000_fw_pid_filter_ctrl(adapter->fe_adap[0].fe, onoff);
2213 }
2214
2215 static int dib90x0_tuner_reset(struct dvb_frontend *fe, int onoff)
2216 {
2217         return dib9000_set_gpio(fe, 5, 0, !onoff);
2218 }
2219
2220 static int dib90x0_tuner_sleep(struct dvb_frontend *fe, int onoff)
2221 {
2222         return dib9000_set_gpio(fe, 0, 0, onoff);
2223 }
2224
2225 static int dib01x0_pmu_update(struct i2c_adapter *i2c, u16 *data, u8 len)
2226 {
2227         u8 wb[4] = { 0xc >> 8, 0xc & 0xff, 0, 0 };
2228         u8 rb[2];
2229         struct i2c_msg msg[2] = {
2230                 {.addr = 0x1e >> 1, .flags = 0, .buf = wb, .len = 2},
2231                 {.addr = 0x1e >> 1, .flags = I2C_M_RD, .buf = rb, .len = 2},
2232         };
2233         u8 index_data;
2234
2235         dibx000_i2c_set_speed(i2c, 250);
2236
2237         if (i2c_transfer(i2c, msg, 2) != 2)
2238                 return -EIO;
2239
2240         switch (rb[0] << 8 | rb[1]) {
2241         case 0:
2242                         deb_info("Found DiB0170 rev1: This version of DiB0170 is not supported any longer.\n");
2243                         return -EIO;
2244         case 1:
2245                         deb_info("Found DiB0170 rev2");
2246                         break;
2247         case 2:
2248                         deb_info("Found DiB0190 rev2");
2249                         break;
2250         default:
2251                         deb_info("DiB01x0 not found");
2252                         return -EIO;
2253         }
2254
2255         for (index_data = 0; index_data < len; index_data += 2) {
2256                 wb[2] = (data[index_data + 1] >> 8) & 0xff;
2257                 wb[3] = (data[index_data + 1]) & 0xff;
2258
2259                 if (data[index_data] == 0) {
2260                         wb[0] = (data[index_data] >> 8) & 0xff;
2261                         wb[1] = (data[index_data]) & 0xff;
2262                         msg[0].len = 2;
2263                         if (i2c_transfer(i2c, msg, 2) != 2)
2264                                 return -EIO;
2265                         wb[2] |= rb[0];
2266                         wb[3] |= rb[1] & ~(3 << 4);
2267                 }
2268
2269                 wb[0] = (data[index_data] >> 8)&0xff;
2270                 wb[1] = (data[index_data])&0xff;
2271                 msg[0].len = 4;
2272                 if (i2c_transfer(i2c, &msg[0], 1) != 1)
2273                         return -EIO;
2274         }
2275         return 0;
2276 }
2277
2278 static struct dib9000_config stk9090m_config = {
2279         .output_mpeg2_in_188_bytes = 1,
2280         .output_mode = OUTMODE_MPEG2_FIFO,
2281         .vcxo_timer = 279620,
2282         .timing_frequency = 20452225,
2283         .demod_clock_khz = 60000,
2284         .xtal_clock_khz = 30000,
2285         .if_drives = (0 << 15) | (1 << 13) | (0 << 12) | (3 << 10) | (0 << 9) | (1 << 7) | (0 << 6) | (0 << 4) | (1 << 3) | (1 << 1) | (0),
2286         .subband = {
2287                 2,
2288                 {
2289                         { 240, { BOARD_GPIO_COMPONENT_DEMOD, BOARD_GPIO_FUNCTION_SUBBAND_GPIO, 0x0008, 0x0000, 0x0008 } }, /* GPIO 3 to 1 for VHF */
2290                         { 890, { BOARD_GPIO_COMPONENT_DEMOD, BOARD_GPIO_FUNCTION_SUBBAND_GPIO, 0x0008, 0x0000, 0x0000 } }, /* GPIO 3 to 0 for UHF */
2291                         { 0 },
2292                 },
2293         },
2294         .gpio_function = {
2295                 { .component = BOARD_GPIO_COMPONENT_DEMOD, .function = BOARD_GPIO_FUNCTION_COMPONENT_ON, .mask = 0x10 | 0x21, .direction = 0 & ~0x21, .value = (0x10 & ~0x1) | 0x20 },
2296                 { .component = BOARD_GPIO_COMPONENT_DEMOD, .function = BOARD_GPIO_FUNCTION_COMPONENT_OFF, .mask = 0x10 | 0x21, .direction = 0 & ~0x21, .value = 0 | 0x21 },
2297         },
2298 };
2299
2300 static struct dib9000_config nim9090md_config[2] = {
2301         {
2302                 .output_mpeg2_in_188_bytes = 1,
2303                 .output_mode = OUTMODE_MPEG2_FIFO,
2304                 .vcxo_timer = 279620,
2305                 .timing_frequency = 20452225,
2306                 .demod_clock_khz = 60000,
2307                 .xtal_clock_khz = 30000,
2308                 .if_drives = (0 << 15) | (1 << 13) | (0 << 12) | (3 << 10) | (0 << 9) | (1 << 7) | (0 << 6) | (0 << 4) | (1 << 3) | (1 << 1) | (0),
2309         }, {
2310                 .output_mpeg2_in_188_bytes = 1,
2311                 .output_mode = OUTMODE_DIVERSITY,
2312                 .vcxo_timer = 279620,
2313                 .timing_frequency = 20452225,
2314                 .demod_clock_khz = 60000,
2315                 .xtal_clock_khz = 30000,
2316                 .if_drives = (0 << 15) | (1 << 13) | (0 << 12) | (3 << 10) | (0 << 9) | (1 << 7) | (0 << 6) | (0 << 4) | (1 << 3) | (1 << 1) | (0),
2317                 .subband = {
2318                         2,
2319                         {
2320                                 { 240, { BOARD_GPIO_COMPONENT_DEMOD, BOARD_GPIO_FUNCTION_SUBBAND_GPIO, 0x0006, 0x0000, 0x0006 } }, /* GPIO 1 and 2 to 1 for VHF */
2321                                 { 890, { BOARD_GPIO_COMPONENT_DEMOD, BOARD_GPIO_FUNCTION_SUBBAND_GPIO, 0x0006, 0x0000, 0x0000 } }, /* GPIO 1 and 2 to 0 for UHF */
2322                                 { 0 },
2323                         },
2324                 },
2325                 .gpio_function = {
2326                         { .component = BOARD_GPIO_COMPONENT_DEMOD, .function = BOARD_GPIO_FUNCTION_COMPONENT_ON, .mask = 0x10 | 0x21, .direction = 0 & ~0x21, .value = (0x10 & ~0x1) | 0x20 },
2327                         { .component = BOARD_GPIO_COMPONENT_DEMOD, .function = BOARD_GPIO_FUNCTION_COMPONENT_OFF, .mask = 0x10 | 0x21, .direction = 0 & ~0x21, .value = 0 | 0x21 },
2328                 },
2329         }
2330 };
2331
2332 static struct dib0090_config dib9090_dib0090_config = {
2333         .io.pll_bypass = 0,
2334         .io.pll_range = 1,
2335         .io.pll_prediv = 1,
2336         .io.pll_loopdiv = 8,
2337         .io.adc_clock_ratio = 8,
2338         .io.pll_int_loop_filt = 0,
2339         .io.clock_khz = 30000,
2340         .reset = dib90x0_tuner_reset,
2341         .sleep = dib90x0_tuner_sleep,
2342         .clkouttobamse = 0,
2343         .analog_output = 0,
2344         .use_pwm_agc = 0,
2345         .clkoutdrive = 0,
2346         .freq_offset_khz_uhf = 0,
2347         .freq_offset_khz_vhf = 0,
2348 };
2349
2350 static struct dib0090_config nim9090md_dib0090_config[2] = {
2351         {
2352                 .io.pll_bypass = 0,
2353                 .io.pll_range = 1,
2354                 .io.pll_prediv = 1,
2355                 .io.pll_loopdiv = 8,
2356                 .io.adc_clock_ratio = 8,
2357                 .io.pll_int_loop_filt = 0,
2358                 .io.clock_khz = 30000,
2359                 .reset = dib90x0_tuner_reset,
2360                 .sleep = dib90x0_tuner_sleep,
2361                 .clkouttobamse = 1,
2362                 .analog_output = 0,
2363                 .use_pwm_agc = 0,
2364                 .clkoutdrive = 0,
2365                 .freq_offset_khz_uhf = 0,
2366                 .freq_offset_khz_vhf = 0,
2367         }, {
2368                 .io.pll_bypass = 0,
2369                 .io.pll_range = 1,
2370                 .io.pll_prediv = 1,
2371                 .io.pll_loopdiv = 8,
2372                 .io.adc_clock_ratio = 8,
2373                 .io.pll_int_loop_filt = 0,
2374                 .io.clock_khz = 30000,
2375                 .reset = dib90x0_tuner_reset,
2376                 .sleep = dib90x0_tuner_sleep,
2377                 .clkouttobamse = 0,
2378                 .analog_output = 0,
2379                 .use_pwm_agc = 0,
2380                 .clkoutdrive = 0,
2381                 .freq_offset_khz_uhf = 0,
2382                 .freq_offset_khz_vhf = 0,
2383         }
2384 };
2385
2386
2387 static int stk9090m_frontend_attach(struct dvb_usb_adapter *adap)
2388 {
2389         struct dib0700_adapter_state *state = adap->priv;
2390         struct dib0700_state *st = adap->dev->priv;
2391         u32 fw_version;
2392
2393         /* Make use of the new i2c functions from FW 1.20 */
2394         dib0700_get_version(adap->dev, NULL, NULL, &fw_version, NULL);
2395         if (fw_version >= 0x10200)
2396                 st->fw_use_new_i2c_api = 1;
2397         dib0700_set_i2c_speed(adap->dev, 340);
2398
2399         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
2400         msleep(20);
2401         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
2402         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
2403         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
2404         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
2405
2406         dib0700_ctrl_clock(adap->dev, 72, 1);
2407
2408         msleep(20);
2409         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
2410         msleep(20);
2411         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
2412
2413         dib9000_i2c_enumeration(&adap->dev->i2c_adap, 1, 0x10, 0x80);
2414
2415         if (reject_firmware(&state->frontend_firmware, "/*(DEBLOBBED)*/", &adap->dev->udev->dev)) {
2416                 deb_info("%s: Upload failed. (file not found?)\n", __func__);
2417                 return -ENODEV;
2418         } else {
2419                 deb_info("%s: firmware read %zu bytes.\n", __func__, state->frontend_firmware->size);
2420         }
2421         stk9090m_config.microcode_B_fe_size = state->frontend_firmware->size;
2422         stk9090m_config.microcode_B_fe_buffer = state->frontend_firmware->data;
2423
2424         adap->fe_adap[0].fe = dvb_attach(dib9000_attach, &adap->dev->i2c_adap, 0x80, &stk9090m_config);
2425
2426         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
2427 }
2428
2429 static int dib9090_tuner_attach(struct dvb_usb_adapter *adap)
2430 {
2431         struct dib0700_adapter_state *state = adap->priv;
2432         struct i2c_adapter *i2c = dib9000_get_tuner_interface(adap->fe_adap[0].fe);
2433         u16 data_dib190[10] = {
2434                 1, 0x1374,
2435                 2, 0x01a2,
2436                 7, 0x0020,
2437                 0, 0x00ef,
2438                 8, 0x0486,
2439         };
2440
2441         if (!IS_ENABLED(CONFIG_DVB_DIB9000))
2442                 return -ENODEV;
2443         if (dvb_attach(dib0090_fw_register, adap->fe_adap[0].fe, i2c, &dib9090_dib0090_config) == NULL)
2444                 return -ENODEV;
2445         i2c = dib9000_get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_GPIO_1_2, 0);
2446         if (!i2c)
2447                 return -ENODEV;
2448         if (dib01x0_pmu_update(i2c, data_dib190, 10) != 0)
2449                 return -ENODEV;
2450         dib0700_set_i2c_speed(adap->dev, 1500);
2451         if (dib9000_firmware_post_pll_init(adap->fe_adap[0].fe) < 0)
2452                 return -ENODEV;
2453         release_firmware(state->frontend_firmware);
2454         return 0;
2455 }
2456
2457 static int nim9090md_frontend_attach(struct dvb_usb_adapter *adap)
2458 {
2459         struct dib0700_adapter_state *state = adap->priv;
2460         struct dib0700_state *st = adap->dev->priv;
2461         struct i2c_adapter *i2c;
2462         struct dvb_frontend *fe_slave;
2463         u32 fw_version;
2464
2465         /* Make use of the new i2c functions from FW 1.20 */
2466         dib0700_get_version(adap->dev, NULL, NULL, &fw_version, NULL);
2467         if (fw_version >= 0x10200)
2468                 st->fw_use_new_i2c_api = 1;
2469         dib0700_set_i2c_speed(adap->dev, 340);
2470
2471         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
2472         msleep(20);
2473         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
2474         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
2475         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
2476         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
2477
2478         dib0700_ctrl_clock(adap->dev, 72, 1);
2479
2480         msleep(20);
2481         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
2482         msleep(20);
2483         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
2484
2485         if (reject_firmware(&state->frontend_firmware, "/*(DEBLOBBED)*/", &adap->dev->udev->dev)) {
2486                 deb_info("%s: Upload failed. (file not found?)\n", __func__);
2487                 return -EIO;
2488         } else {
2489                 deb_info("%s: firmware read %zu bytes.\n", __func__, state->frontend_firmware->size);
2490         }
2491         nim9090md_config[0].microcode_B_fe_size = state->frontend_firmware->size;
2492         nim9090md_config[0].microcode_B_fe_buffer = state->frontend_firmware->data;
2493         nim9090md_config[1].microcode_B_fe_size = state->frontend_firmware->size;
2494         nim9090md_config[1].microcode_B_fe_buffer = state->frontend_firmware->data;
2495
2496         dib9000_i2c_enumeration(&adap->dev->i2c_adap, 1, 0x20, 0x80);
2497         adap->fe_adap[0].fe = dvb_attach(dib9000_attach, &adap->dev->i2c_adap, 0x80, &nim9090md_config[0]);
2498
2499         if (adap->fe_adap[0].fe == NULL)
2500                 return -ENODEV;
2501
2502         i2c = dib9000_get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_GPIO_3_4, 0);
2503         dib9000_i2c_enumeration(i2c, 1, 0x12, 0x82);
2504
2505         fe_slave = dvb_attach(dib9000_attach, i2c, 0x82, &nim9090md_config[1]);
2506         dib9000_set_slave_frontend(adap->fe_adap[0].fe, fe_slave);
2507
2508         return fe_slave == NULL ?  -ENODEV : 0;
2509 }
2510
2511 static int nim9090md_tuner_attach(struct dvb_usb_adapter *adap)
2512 {
2513         struct dib0700_adapter_state *state = adap->priv;
2514         struct i2c_adapter *i2c;
2515         struct dvb_frontend *fe_slave;
2516         u16 data_dib190[10] = {
2517                 1, 0x5374,
2518                 2, 0x01ae,
2519                 7, 0x0020,
2520                 0, 0x00ef,
2521                 8, 0x0406,
2522         };
2523         if (!IS_ENABLED(CONFIG_DVB_DIB9000))
2524                 return -ENODEV;
2525         i2c = dib9000_get_tuner_interface(adap->fe_adap[0].fe);
2526         if (dvb_attach(dib0090_fw_register, adap->fe_adap[0].fe, i2c, &nim9090md_dib0090_config[0]) == NULL)
2527                 return -ENODEV;
2528         i2c = dib9000_get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_GPIO_1_2, 0);
2529         if (!i2c)
2530                 return -ENODEV;
2531         if (dib01x0_pmu_update(i2c, data_dib190, 10) < 0)
2532                 return -ENODEV;
2533
2534         dib0700_set_i2c_speed(adap->dev, 1500);
2535         if (dib9000_firmware_post_pll_init(adap->fe_adap[0].fe) < 0)
2536                 return -ENODEV;
2537
2538         fe_slave = dib9000_get_slave_frontend(adap->fe_adap[0].fe, 1);
2539         if (fe_slave != NULL) {
2540                 i2c = dib9000_get_component_bus_interface(adap->fe_adap[0].fe);
2541                 dib9000_set_i2c_adapter(fe_slave, i2c);
2542
2543                 i2c = dib9000_get_tuner_interface(fe_slave);
2544                 if (dvb_attach(dib0090_fw_register, fe_slave, i2c, &nim9090md_dib0090_config[1]) == NULL)
2545                         return -ENODEV;
2546                 fe_slave->dvb = adap->fe_adap[0].fe->dvb;
2547                 dib9000_fw_set_component_bus_speed(adap->fe_adap[0].fe, 1500);
2548                 if (dib9000_firmware_post_pll_init(fe_slave) < 0)
2549                         return -ENODEV;
2550         }
2551         release_firmware(state->frontend_firmware);
2552
2553         return 0;
2554 }
2555
2556 /* NIM7090 */
2557 static int dib7090p_get_best_sampling(struct dvb_frontend *fe , struct dibx090p_best_adc *adc)
2558 {
2559         u8 spur = 0, prediv = 0, loopdiv = 0, min_prediv = 1, max_prediv = 1;
2560
2561         u16 xtal = 12000;
2562         u32 fcp_min = 1900;  /* PLL Minimum Frequency comparator KHz */
2563         u32 fcp_max = 20000; /* PLL Maximum Frequency comparator KHz */
2564         u32 fdem_max = 76000;
2565         u32 fdem_min = 69500;
2566         u32 fcp = 0, fs = 0, fdem = 0;
2567         u32 harmonic_id = 0;
2568
2569         adc->pll_loopdiv = loopdiv;
2570         adc->pll_prediv = prediv;
2571         adc->timf = 0;
2572
2573         deb_info("bandwidth = %d fdem_min =%d", fe->dtv_property_cache.bandwidth_hz, fdem_min);
2574
2575         /* Find Min and Max prediv */
2576         while ((xtal/max_prediv) >= fcp_min)
2577                 max_prediv++;
2578
2579         max_prediv--;
2580         min_prediv = max_prediv;
2581         while ((xtal/min_prediv) <= fcp_max) {
2582                 min_prediv--;
2583                 if (min_prediv == 1)
2584                         break;
2585         }
2586         deb_info("MIN prediv = %d : MAX prediv = %d", min_prediv, max_prediv);
2587
2588         min_prediv = 2;
2589
2590         for (prediv = min_prediv ; prediv < max_prediv; prediv++) {
2591                 fcp = xtal / prediv;
2592                 if (fcp > fcp_min && fcp < fcp_max) {
2593                         for (loopdiv = 1 ; loopdiv < 64 ; loopdiv++) {
2594                                 fdem = ((xtal/prediv) * loopdiv);
2595                                 fs   = fdem / 4;
2596                                 /* test min/max system restrictions */
2597
2598                                 if ((fdem >= fdem_min) && (fdem <= fdem_max) && (fs >= fe->dtv_property_cache.bandwidth_hz/1000)) {
2599                                         spur = 0;
2600                                         /* test fs harmonics positions */
2601                                         for (harmonic_id = (fe->dtv_property_cache.frequency / (1000*fs)) ;  harmonic_id <= ((fe->dtv_property_cache.frequency / (1000*fs))+1) ; harmonic_id++) {
2602                                                 if (((fs*harmonic_id) >= ((fe->dtv_property_cache.frequency/1000) - (fe->dtv_property_cache.bandwidth_hz/2000))) &&  ((fs*harmonic_id) <= ((fe->dtv_property_cache.frequency/1000) + (fe->dtv_property_cache.bandwidth_hz/2000)))) {
2603                                                         spur = 1;
2604                                                         break;
2605                                                 }
2606                                         }
2607
2608                                         if (!spur) {
2609                                                 adc->pll_loopdiv = loopdiv;
2610                                                 adc->pll_prediv = prediv;
2611                                                 adc->timf = 2396745143UL/fdem*(1 << 9);
2612                                                 adc->timf += ((2396745143UL%fdem) << 9)/fdem;
2613                                                 deb_info("loopdiv=%i prediv=%i timf=%i", loopdiv, prediv, adc->timf);
2614                                                 break;
2615                                         }
2616                                 }
2617                         }
2618                 }
2619                 if (!spur)
2620                         break;
2621         }
2622
2623
2624         if (adc->pll_loopdiv == 0 && adc->pll_prediv == 0)
2625                 return -EINVAL;
2626         else
2627                 return 0;
2628 }
2629
2630 static int dib7090_agc_startup(struct dvb_frontend *fe)
2631 {
2632         struct dvb_usb_adapter *adap = fe->dvb->priv;
2633         struct dib0700_adapter_state *state = adap->priv;
2634         struct dibx000_bandwidth_config pll;
2635         u16 target;
2636         struct dibx090p_best_adc adc;
2637         int ret;
2638
2639         ret = state->set_param_save(fe);
2640         if (ret < 0)
2641                 return ret;
2642
2643         memset(&pll, 0, sizeof(struct dibx000_bandwidth_config));
2644         dib0090_pwm_gain_reset(fe);
2645         target = (dib0090_get_wbd_target(fe) * 8 + 1) / 2;
2646         state->dib7000p_ops.set_wbd_ref(fe, target);
2647
2648         if (dib7090p_get_best_sampling(fe, &adc) == 0) {
2649                 pll.pll_ratio  = adc.pll_loopdiv;
2650                 pll.pll_prediv = adc.pll_prediv;
2651
2652                 state->dib7000p_ops.update_pll(fe, &pll);
2653                 state->dib7000p_ops.ctrl_timf(fe, DEMOD_TIMF_SET, adc.timf);
2654         }
2655         return 0;
2656 }
2657
2658 static int dib7090_agc_restart(struct dvb_frontend *fe, u8 restart)
2659 {
2660         deb_info("AGC restart callback: %d", restart);
2661         if (restart == 0) /* before AGC startup */
2662                 dib0090_set_dc_servo(fe, 1);
2663         return 0;
2664 }
2665
2666 static int tfe7790p_update_lna(struct dvb_frontend *fe, u16 agc_global)
2667 {
2668         struct dvb_usb_adapter *adap = fe->dvb->priv;
2669         struct dib0700_adapter_state *state = adap->priv;
2670
2671         deb_info("update LNA: agc global=%i", agc_global);
2672
2673         if (agc_global < 25000) {
2674                 state->dib7000p_ops.set_gpio(fe, 8, 0, 0);
2675                 state->dib7000p_ops.set_agc1_min(fe, 0);
2676         } else {
2677                 state->dib7000p_ops.set_gpio(fe, 8, 0, 1);
2678                 state->dib7000p_ops.set_agc1_min(fe, 32768);
2679         }
2680
2681         return 0;
2682 }
2683
2684 static struct dib0090_wbd_slope dib7090_wbd_table[] = {
2685         { 380,   81, 850, 64, 540,  4},
2686         { 860,   51, 866, 21,  375, 4},
2687         {1700,    0, 250, 0,   100, 6},
2688         {2600,    0, 250, 0,   100, 6},
2689         { 0xFFFF, 0,   0, 0,   0,   0},
2690 };
2691
2692 static struct dibx000_agc_config dib7090_agc_config[2] = {
2693         {
2694                 .band_caps      = BAND_UHF,
2695                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=1, P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
2696                 * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0 */
2697                 .setup          = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
2698
2699                 .inv_gain       = 687,
2700                 .time_stabiliz  = 10,
2701
2702                 .alpha_level    = 0,
2703                 .thlock         = 118,
2704
2705                 .wbd_inv        = 0,
2706                 .wbd_ref        = 1200,
2707                 .wbd_sel        = 3,
2708                 .wbd_alpha      = 5,
2709
2710                 .agc1_max       = 65535,
2711                 .agc1_min       = 32768,
2712
2713                 .agc2_max       = 65535,
2714                 .agc2_min       = 0,
2715
2716                 .agc1_pt1       = 0,
2717                 .agc1_pt2       = 32,
2718                 .agc1_pt3       = 114,
2719                 .agc1_slope1    = 143,
2720                 .agc1_slope2    = 144,
2721                 .agc2_pt1       = 114,
2722                 .agc2_pt2       = 227,
2723                 .agc2_slope1    = 116,
2724                 .agc2_slope2    = 117,
2725
2726                 .alpha_mant     = 18,
2727                 .alpha_exp      = 0,
2728                 .beta_mant      = 20,
2729                 .beta_exp       = 59,
2730
2731                 .perform_agc_softsplit = 0,
2732         } , {
2733                 .band_caps      = BAND_FM | BAND_VHF | BAND_CBAND,
2734                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=1, P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
2735                 * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0 */
2736                 .setup          = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
2737
2738                 .inv_gain       = 732,
2739                 .time_stabiliz  = 10,
2740
2741                 .alpha_level    = 0,
2742                 .thlock         = 118,
2743
2744                 .wbd_inv        = 0,
2745                 .wbd_ref        = 1200,
2746                 .wbd_sel        = 3,
2747                 .wbd_alpha      = 5,
2748
2749                 .agc1_max       = 65535,
2750                 .agc1_min       = 0,
2751
2752                 .agc2_max       = 65535,
2753                 .agc2_min       = 0,
2754
2755                 .agc1_pt1       = 0,
2756                 .agc1_pt2       = 0,
2757                 .agc1_pt3       = 98,
2758                 .agc1_slope1    = 0,
2759                 .agc1_slope2    = 167,
2760                 .agc2_pt1       = 98,
2761                 .agc2_pt2       = 255,
2762                 .agc2_slope1    = 104,
2763                 .agc2_slope2    = 0,
2764
2765                 .alpha_mant     = 18,
2766                 .alpha_exp      = 0,
2767                 .beta_mant      = 20,
2768                 .beta_exp       = 59,
2769
2770                 .perform_agc_softsplit = 0,
2771         }
2772 };
2773
2774 static struct dibx000_bandwidth_config dib7090_clock_config_12_mhz = {
2775         .internal = 60000,
2776         .sampling = 15000,
2777         .pll_prediv = 1,
2778         .pll_ratio = 5,
2779         .pll_range = 0,
2780         .pll_reset = 0,
2781         .pll_bypass = 0,
2782         .enable_refdiv = 0,
2783         .bypclk_div = 0,
2784         .IO_CLK_en_core = 1,
2785         .ADClkSrc = 1,
2786         .modulo = 2,
2787         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
2788         .ifreq = (0 << 25) | 0,
2789         .timf = 20452225,
2790         .xtal_hz = 15000000,
2791 };
2792
2793 static struct dib7000p_config nim7090_dib7000p_config = {
2794         .output_mpeg2_in_188_bytes  = 1,
2795         .hostbus_diversity                      = 1,
2796         .tuner_is_baseband                      = 1,
2797         .update_lna                                     = tfe7790p_update_lna, /* GPIO used is the same as TFE7790 */
2798
2799         .agc_config_count                       = 2,
2800         .agc                                            = dib7090_agc_config,
2801
2802         .bw                                                     = &dib7090_clock_config_12_mhz,
2803
2804         .gpio_dir                                       = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
2805         .gpio_val                                       = DIB7000P_GPIO_DEFAULT_VALUES,
2806         .gpio_pwm_pos                           = DIB7000P_GPIO_DEFAULT_PWM_POS,
2807
2808         .pwm_freq_div                           = 0,
2809
2810         .agc_control                            = dib7090_agc_restart,
2811
2812         .spur_protect                           = 0,
2813         .disable_sample_and_hold        = 0,
2814         .enable_current_mirror          = 0,
2815         .diversity_delay                        = 0,
2816
2817         .output_mode                            = OUTMODE_MPEG2_FIFO,
2818         .enMpegOutput                           = 1,
2819 };
2820
2821 static int tfe7090p_pvr_update_lna(struct dvb_frontend *fe, u16 agc_global)
2822 {
2823         struct dvb_usb_adapter *adap = fe->dvb->priv;
2824         struct dib0700_adapter_state *state = adap->priv;
2825
2826         deb_info("TFE7090P-PVR update LNA: agc global=%i", agc_global);
2827         if (agc_global < 25000) {
2828                 state->dib7000p_ops.set_gpio(fe, 5, 0, 0);
2829                 state->dib7000p_ops.set_agc1_min(fe, 0);
2830         } else {
2831                 state->dib7000p_ops.set_gpio(fe, 5, 0, 1);
2832                 state->dib7000p_ops.set_agc1_min(fe, 32768);
2833         }
2834
2835         return 0;
2836 }
2837
2838 static struct dib7000p_config tfe7090pvr_dib7000p_config[2] = {
2839         {
2840                 .output_mpeg2_in_188_bytes  = 1,
2841                 .hostbus_diversity                      = 1,
2842                 .tuner_is_baseband                      = 1,
2843                 .update_lna                                     = tfe7090p_pvr_update_lna,
2844
2845                 .agc_config_count                       = 2,
2846                 .agc                                            = dib7090_agc_config,
2847
2848                 .bw                                                     = &dib7090_clock_config_12_mhz,
2849
2850                 .gpio_dir                                       = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
2851                 .gpio_val                                       = DIB7000P_GPIO_DEFAULT_VALUES,
2852                 .gpio_pwm_pos                           = DIB7000P_GPIO_DEFAULT_PWM_POS,
2853
2854                 .pwm_freq_div                           = 0,
2855
2856                 .agc_control                            = dib7090_agc_restart,
2857
2858                 .spur_protect                           = 0,
2859                 .disable_sample_and_hold        = 0,
2860                 .enable_current_mirror          = 0,
2861                 .diversity_delay                        = 0,
2862
2863                 .output_mode                            = OUTMODE_MPEG2_PAR_GATED_CLK,
2864                 .default_i2c_addr                       = 0x90,
2865                 .enMpegOutput                           = 1,
2866         }, {
2867                 .output_mpeg2_in_188_bytes  = 1,
2868                 .hostbus_diversity                      = 1,
2869                 .tuner_is_baseband                      = 1,
2870                 .update_lna                                     = tfe7090p_pvr_update_lna,
2871
2872                 .agc_config_count                       = 2,
2873                 .agc                                            = dib7090_agc_config,
2874
2875                 .bw                                                     = &dib7090_clock_config_12_mhz,
2876
2877                 .gpio_dir                                       = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
2878                 .gpio_val                                       = DIB7000P_GPIO_DEFAULT_VALUES,
2879                 .gpio_pwm_pos                           = DIB7000P_GPIO_DEFAULT_PWM_POS,
2880
2881                 .pwm_freq_div                           = 0,
2882
2883                 .agc_control                            = dib7090_agc_restart,
2884
2885                 .spur_protect                           = 0,
2886                 .disable_sample_and_hold        = 0,
2887                 .enable_current_mirror          = 0,
2888                 .diversity_delay                        = 0,
2889
2890                 .output_mode                            = OUTMODE_MPEG2_PAR_GATED_CLK,
2891                 .default_i2c_addr                       = 0x92,
2892                 .enMpegOutput                           = 0,
2893         }
2894 };
2895
2896 static struct dib0090_config nim7090_dib0090_config = {
2897         .io.clock_khz = 12000,
2898         .io.pll_bypass = 0,
2899         .io.pll_range = 0,
2900         .io.pll_prediv = 3,
2901         .io.pll_loopdiv = 6,
2902         .io.adc_clock_ratio = 0,
2903         .io.pll_int_loop_filt = 0,
2904
2905         .freq_offset_khz_uhf = 0,
2906         .freq_offset_khz_vhf = 0,
2907
2908         .clkouttobamse = 1,
2909         .analog_output = 0,
2910
2911         .wbd_vhf_offset = 0,
2912         .wbd_cband_offset = 0,
2913         .use_pwm_agc = 1,
2914         .clkoutdrive = 0,
2915
2916         .fref_clock_ratio = 0,
2917
2918         .wbd = dib7090_wbd_table,
2919
2920         .ls_cfg_pad_drv = 0,
2921         .data_tx_drv = 0,
2922         .low_if = NULL,
2923         .in_soc = 1,
2924 };
2925
2926 static struct dib7000p_config tfe7790p_dib7000p_config = {
2927         .output_mpeg2_in_188_bytes  = 1,
2928         .hostbus_diversity                      = 1,
2929         .tuner_is_baseband                      = 1,
2930         .update_lna                                     = tfe7790p_update_lna,
2931
2932         .agc_config_count                       = 2,
2933         .agc                                            = dib7090_agc_config,
2934
2935         .bw                                                     = &dib7090_clock_config_12_mhz,
2936
2937         .gpio_dir                                       = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
2938         .gpio_val                                       = DIB7000P_GPIO_DEFAULT_VALUES,
2939         .gpio_pwm_pos                           = DIB7000P_GPIO_DEFAULT_PWM_POS,
2940
2941         .pwm_freq_div                           = 0,
2942
2943         .agc_control                            = dib7090_agc_restart,
2944
2945         .spur_protect                           = 0,
2946         .disable_sample_and_hold        = 0,
2947         .enable_current_mirror          = 0,
2948         .diversity_delay                        = 0,
2949
2950         .output_mode                            = OUTMODE_MPEG2_PAR_GATED_CLK,
2951         .enMpegOutput                           = 1,
2952 };
2953
2954 static struct dib0090_config tfe7790p_dib0090_config = {
2955         .io.clock_khz = 12000,
2956         .io.pll_bypass = 0,
2957         .io.pll_range = 0,
2958         .io.pll_prediv = 3,
2959         .io.pll_loopdiv = 6,
2960         .io.adc_clock_ratio = 0,
2961         .io.pll_int_loop_filt = 0,
2962
2963         .freq_offset_khz_uhf = 0,
2964         .freq_offset_khz_vhf = 0,
2965
2966         .clkouttobamse = 1,
2967         .analog_output = 0,
2968
2969         .wbd_vhf_offset = 0,
2970         .wbd_cband_offset = 0,
2971         .use_pwm_agc = 1,
2972         .clkoutdrive = 0,
2973
2974         .fref_clock_ratio = 0,
2975
2976         .wbd = dib7090_wbd_table,
2977
2978         .ls_cfg_pad_drv = 0,
2979         .data_tx_drv = 0,
2980         .low_if = NULL,
2981         .in_soc = 1,
2982         .force_cband_input = 0,
2983         .is_dib7090e = 0,
2984         .force_crystal_mode = 1,
2985 };
2986
2987 static struct dib0090_config tfe7090pvr_dib0090_config[2] = {
2988         {
2989                 .io.clock_khz = 12000,
2990                 .io.pll_bypass = 0,
2991                 .io.pll_range = 0,
2992                 .io.pll_prediv = 3,
2993                 .io.pll_loopdiv = 6,
2994                 .io.adc_clock_ratio = 0,
2995                 .io.pll_int_loop_filt = 0,
2996
2997                 .freq_offset_khz_uhf = 50,
2998                 .freq_offset_khz_vhf = 70,
2999
3000                 .clkouttobamse = 1,
3001                 .analog_output = 0,
3002
3003                 .wbd_vhf_offset = 0,
3004                 .wbd_cband_offset = 0,
3005                 .use_pwm_agc = 1,
3006                 .clkoutdrive = 0,
3007
3008                 .fref_clock_ratio = 0,
3009
3010                 .wbd = dib7090_wbd_table,
3011
3012                 .ls_cfg_pad_drv = 0,
3013                 .data_tx_drv = 0,
3014                 .low_if = NULL,
3015                 .in_soc = 1,
3016         }, {
3017                 .io.clock_khz = 12000,
3018                 .io.pll_bypass = 0,
3019                 .io.pll_range = 0,
3020                 .io.pll_prediv = 3,
3021                 .io.pll_loopdiv = 6,
3022                 .io.adc_clock_ratio = 0,
3023                 .io.pll_int_loop_filt = 0,
3024
3025                 .freq_offset_khz_uhf = -50,
3026                 .freq_offset_khz_vhf = -70,
3027
3028                 .clkouttobamse = 1,
3029                 .analog_output = 0,
3030
3031                 .wbd_vhf_offset = 0,
3032                 .wbd_cband_offset = 0,
3033                 .use_pwm_agc = 1,
3034                 .clkoutdrive = 0,
3035
3036                 .fref_clock_ratio = 0,
3037
3038                 .wbd = dib7090_wbd_table,
3039
3040                 .ls_cfg_pad_drv = 0,
3041                 .data_tx_drv = 0,
3042                 .low_if = NULL,
3043                 .in_soc = 1,
3044         }
3045 };
3046
3047 static int nim7090_frontend_attach(struct dvb_usb_adapter *adap)
3048 {
3049         struct dib0700_adapter_state *state = adap->priv;
3050
3051         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3052                 return -ENODEV;
3053
3054         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3055         msleep(20);
3056         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
3057         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
3058         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
3059         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3060
3061         msleep(20);
3062         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3063         msleep(20);
3064         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3065
3066         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 0x10, &nim7090_dib7000p_config) != 0) {
3067                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
3068                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3069                 return -ENODEV;
3070         }
3071         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x80, &nim7090_dib7000p_config);
3072
3073         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
3074 }
3075
3076 static int nim7090_tuner_attach(struct dvb_usb_adapter *adap)
3077 {
3078         struct dib0700_adapter_state *st = adap->priv;
3079         struct i2c_adapter *tun_i2c = st->dib7000p_ops.get_i2c_tuner(adap->fe_adap[0].fe);
3080
3081         nim7090_dib0090_config.reset = st->dib7000p_ops.tuner_sleep,
3082         nim7090_dib0090_config.sleep = st->dib7000p_ops.tuner_sleep,
3083         nim7090_dib0090_config.get_adc_power = st->dib7000p_ops.get_adc_power;
3084
3085         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &nim7090_dib0090_config) == NULL)
3086                 return -ENODEV;
3087
3088         st->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3089
3090         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
3091         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7090_agc_startup;
3092         return 0;
3093 }
3094
3095 static int tfe7090pvr_frontend0_attach(struct dvb_usb_adapter *adap)
3096 {
3097         struct dib0700_state *st = adap->dev->priv;
3098         struct dib0700_adapter_state *state = adap->priv;
3099
3100         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3101                 return -ENODEV;
3102
3103         /* The TFE7090 requires the dib0700 to not be in master mode */
3104         st->disable_streaming_master_mode = 1;
3105
3106         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3107         msleep(20);
3108         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
3109         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
3110         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
3111         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3112
3113         msleep(20);
3114         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3115         msleep(20);
3116         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3117
3118         /* initialize IC 0 */
3119         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 0x20, &tfe7090pvr_dib7000p_config[0]) != 0) {
3120                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
3121                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3122                 return -ENODEV;
3123         }
3124
3125         dib0700_set_i2c_speed(adap->dev, 340);
3126         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x90, &tfe7090pvr_dib7000p_config[0]);
3127         if (adap->fe_adap[0].fe == NULL)
3128                 return -ENODEV;
3129
3130         state->dib7000p_ops.slave_reset(adap->fe_adap[0].fe);
3131
3132         return 0;
3133 }
3134
3135 static int tfe7090pvr_frontend1_attach(struct dvb_usb_adapter *adap)
3136 {
3137         struct i2c_adapter *i2c;
3138         struct dib0700_adapter_state *state = adap->priv;
3139
3140         if (adap->dev->adapter[0].fe_adap[0].fe == NULL) {
3141                 err("the master dib7090 has to be initialized first");
3142                 return -ENODEV; /* the master device has not been initialized */
3143         }
3144
3145         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3146                 return -ENODEV;
3147
3148         i2c = state->dib7000p_ops.get_i2c_master(adap->dev->adapter[0].fe_adap[0].fe, DIBX000_I2C_INTERFACE_GPIO_6_7, 1);
3149         if (state->dib7000p_ops.i2c_enumeration(i2c, 1, 0x10, &tfe7090pvr_dib7000p_config[1]) != 0) {
3150                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
3151                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3152                 return -ENODEV;
3153         }
3154
3155         adap->fe_adap[0].fe = state->dib7000p_ops.init(i2c, 0x92, &tfe7090pvr_dib7000p_config[1]);
3156         dib0700_set_i2c_speed(adap->dev, 200);
3157
3158         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3159 }
3160
3161 static int tfe7090pvr_tuner0_attach(struct dvb_usb_adapter *adap)
3162 {
3163         struct dib0700_adapter_state *st = adap->priv;
3164         struct i2c_adapter *tun_i2c = st->dib7000p_ops.get_i2c_tuner(adap->fe_adap[0].fe);
3165
3166         tfe7090pvr_dib0090_config[0].reset = st->dib7000p_ops.tuner_sleep;
3167         tfe7090pvr_dib0090_config[0].sleep = st->dib7000p_ops.tuner_sleep;
3168         tfe7090pvr_dib0090_config[0].get_adc_power = st->dib7000p_ops.get_adc_power;
3169
3170         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &tfe7090pvr_dib0090_config[0]) == NULL)
3171                 return -ENODEV;
3172
3173         st->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3174
3175         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
3176         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7090_agc_startup;
3177         return 0;
3178 }
3179
3180 static int tfe7090pvr_tuner1_attach(struct dvb_usb_adapter *adap)
3181 {
3182         struct dib0700_adapter_state *st = adap->priv;
3183         struct i2c_adapter *tun_i2c = st->dib7000p_ops.get_i2c_tuner(adap->fe_adap[0].fe);
3184
3185         tfe7090pvr_dib0090_config[1].reset = st->dib7000p_ops.tuner_sleep;
3186         tfe7090pvr_dib0090_config[1].sleep = st->dib7000p_ops.tuner_sleep;
3187         tfe7090pvr_dib0090_config[1].get_adc_power = st->dib7000p_ops.get_adc_power;
3188
3189         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &tfe7090pvr_dib0090_config[1]) == NULL)
3190                 return -ENODEV;
3191
3192         st->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3193
3194         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
3195         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7090_agc_startup;
3196         return 0;
3197 }
3198
3199 static int tfe7790p_frontend_attach(struct dvb_usb_adapter *adap)
3200 {
3201         struct dib0700_state *st = adap->dev->priv;
3202         struct dib0700_adapter_state *state = adap->priv;
3203
3204         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3205                 return -ENODEV;
3206
3207         /* The TFE7790P requires the dib0700 to not be in master mode */
3208         st->disable_streaming_master_mode = 1;
3209
3210         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3211         msleep(20);
3212         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
3213         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
3214         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
3215         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3216         msleep(20);
3217         dib0700_ctrl_clock(adap->dev, 72, 1);
3218         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3219         msleep(20);
3220         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3221
3222         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap,
3223                                 1, 0x10, &tfe7790p_dib7000p_config) != 0) {
3224                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
3225                                 __func__);
3226                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3227                 return -ENODEV;
3228         }
3229         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap,
3230                         0x80, &tfe7790p_dib7000p_config);
3231
3232         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
3233 }
3234
3235 static int tfe7790p_tuner_attach(struct dvb_usb_adapter *adap)
3236 {
3237         struct dib0700_adapter_state *st = adap->priv;
3238         struct i2c_adapter *tun_i2c =
3239                 st->dib7000p_ops.get_i2c_tuner(adap->fe_adap[0].fe);
3240
3241
3242         tfe7790p_dib0090_config.reset = st->dib7000p_ops.tuner_sleep;
3243         tfe7790p_dib0090_config.sleep = st->dib7000p_ops.tuner_sleep;
3244         tfe7790p_dib0090_config.get_adc_power = st->dib7000p_ops.get_adc_power;
3245
3246         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c,
3247                                 &tfe7790p_dib0090_config) == NULL)
3248                 return -ENODEV;
3249
3250         st->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3251
3252         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
3253         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7090_agc_startup;
3254         return 0;
3255 }
3256
3257 /* STK7070PD */
3258 static struct dib7000p_config stk7070pd_dib7000p_config[2] = {
3259         {
3260                 .output_mpeg2_in_188_bytes = 1,
3261
3262                 .agc_config_count = 1,
3263                 .agc = &dib7070_agc_config,
3264                 .bw  = &dib7070_bw_config_12_mhz,
3265                 .tuner_is_baseband = 1,
3266                 .spur_protect = 1,
3267
3268                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
3269                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
3270                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
3271
3272                 .hostbus_diversity = 1,
3273         }, {
3274                 .output_mpeg2_in_188_bytes = 1,
3275
3276                 .agc_config_count = 1,
3277                 .agc = &dib7070_agc_config,
3278                 .bw  = &dib7070_bw_config_12_mhz,
3279                 .tuner_is_baseband = 1,
3280                 .spur_protect = 1,
3281
3282                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
3283                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
3284                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
3285
3286                 .hostbus_diversity = 1,
3287         }
3288 };
3289
3290 static void stk7070pd_init(struct dvb_usb_device *dev)
3291 {
3292         dib0700_set_gpio(dev, GPIO6, GPIO_OUT, 1);
3293         msleep(10);
3294         dib0700_set_gpio(dev, GPIO9, GPIO_OUT, 1);
3295         dib0700_set_gpio(dev, GPIO4, GPIO_OUT, 1);
3296         dib0700_set_gpio(dev, GPIO7, GPIO_OUT, 1);
3297         dib0700_set_gpio(dev, GPIO10, GPIO_OUT, 0);
3298
3299         dib0700_ctrl_clock(dev, 72, 1);
3300
3301         msleep(10);
3302         dib0700_set_gpio(dev, GPIO10, GPIO_OUT, 1);
3303 }
3304
3305 static int stk7070pd_frontend_attach0(struct dvb_usb_adapter *adap)
3306 {
3307         struct dib0700_adapter_state *state = adap->priv;
3308
3309         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3310                 return -ENODEV;
3311
3312         stk7070pd_init(adap->dev);
3313
3314         msleep(10);
3315         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3316
3317         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 2, 18,
3318                                      stk7070pd_dib7000p_config) != 0) {
3319                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
3320                     __func__);
3321                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3322                 return -ENODEV;
3323         }
3324
3325         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x80, &stk7070pd_dib7000p_config[0]);
3326         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3327 }
3328
3329 static int stk7070pd_frontend_attach1(struct dvb_usb_adapter *adap)
3330 {
3331         struct dib0700_adapter_state *state = adap->priv;
3332
3333         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3334                 return -ENODEV;
3335
3336         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x82, &stk7070pd_dib7000p_config[1]);
3337         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3338 }
3339
3340 static int novatd_read_status_override(struct dvb_frontend *fe,
3341                                        enum fe_status *stat)
3342 {
3343         struct dvb_usb_adapter *adap = fe->dvb->priv;
3344         struct dvb_usb_device *dev = adap->dev;
3345         struct dib0700_state *state = dev->priv;
3346         int ret;
3347
3348         ret = state->read_status(fe, stat);
3349
3350         if (!ret)
3351                 dib0700_set_gpio(dev, adap->id == 0 ? GPIO1 : GPIO0, GPIO_OUT,
3352                                 !!(*stat & FE_HAS_LOCK));
3353
3354         return ret;
3355 }
3356
3357 static int novatd_sleep_override(struct dvb_frontend* fe)
3358 {
3359         struct dvb_usb_adapter *adap = fe->dvb->priv;
3360         struct dvb_usb_device *dev = adap->dev;
3361         struct dib0700_state *state = dev->priv;
3362
3363         /* turn off LED */
3364         dib0700_set_gpio(dev, adap->id == 0 ? GPIO1 : GPIO0, GPIO_OUT, 0);
3365
3366         return state->sleep(fe);
3367 }
3368
3369 /**
3370  * novatd_frontend_attach - Nova-TD specific attach
3371  *
3372  * Nova-TD has GPIO0, 1 and 2 for LEDs. So do not fiddle with them except for
3373  * information purposes.
3374  */
3375 static int novatd_frontend_attach(struct dvb_usb_adapter *adap)
3376 {
3377         struct dvb_usb_device *dev = adap->dev;
3378         struct dib0700_state *st = dev->priv;
3379         struct dib0700_adapter_state *state = adap->priv;
3380
3381         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3382                 return -ENODEV;
3383
3384         if (adap->id == 0) {
3385                 stk7070pd_init(dev);
3386
3387                 /* turn the power LED on, the other two off (just in case) */
3388                 dib0700_set_gpio(dev, GPIO0, GPIO_OUT, 0);
3389                 dib0700_set_gpio(dev, GPIO1, GPIO_OUT, 0);
3390                 dib0700_set_gpio(dev, GPIO2, GPIO_OUT, 1);
3391
3392                 if (state->dib7000p_ops.i2c_enumeration(&dev->i2c_adap, 2, 18,
3393                                              stk7070pd_dib7000p_config) != 0) {
3394                         err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
3395                             __func__);
3396                         dvb_detach(state->dib7000p_ops.set_wbd_ref);
3397                         return -ENODEV;
3398                 }
3399         }
3400
3401         adap->fe_adap[0].fe = state->dib7000p_ops.init(&dev->i2c_adap,
3402                         adap->id == 0 ? 0x80 : 0x82,
3403                         &stk7070pd_dib7000p_config[adap->id]);
3404
3405         if (adap->fe_adap[0].fe == NULL)
3406                 return -ENODEV;
3407
3408         st->read_status = adap->fe_adap[0].fe->ops.read_status;
3409         adap->fe_adap[0].fe->ops.read_status = novatd_read_status_override;
3410         st->sleep = adap->fe_adap[0].fe->ops.sleep;
3411         adap->fe_adap[0].fe->ops.sleep = novatd_sleep_override;
3412
3413         return 0;
3414 }
3415
3416 /* S5H1411 */
3417 static struct s5h1411_config pinnacle_801e_config = {
3418         .output_mode   = S5H1411_PARALLEL_OUTPUT,
3419         .gpio          = S5H1411_GPIO_OFF,
3420         .mpeg_timing   = S5H1411_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK,
3421         .qam_if        = S5H1411_IF_44000,
3422         .vsb_if        = S5H1411_IF_44000,
3423         .inversion     = S5H1411_INVERSION_OFF,
3424         .status_mode   = S5H1411_DEMODLOCKING
3425 };
3426
3427 /* Pinnacle PCTV HD Pro 801e GPIOs map:
3428    GPIO0  - currently unknown
3429    GPIO1  - xc5000 tuner reset
3430    GPIO2  - CX25843 sleep
3431    GPIO3  - currently unknown
3432    GPIO4  - currently unknown
3433    GPIO6  - currently unknown
3434    GPIO7  - currently unknown
3435    GPIO9  - currently unknown
3436    GPIO10 - CX25843 reset
3437  */
3438 static int s5h1411_frontend_attach(struct dvb_usb_adapter *adap)
3439 {
3440         struct dib0700_state *st = adap->dev->priv;
3441
3442         /* Make use of the new i2c functions from FW 1.20 */
3443         st->fw_use_new_i2c_api = 1;
3444
3445         /* The s5h1411 requires the dib0700 to not be in master mode */
3446         st->disable_streaming_master_mode = 1;
3447
3448         /* All msleep values taken from Windows USB trace */
3449         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 0);
3450         dib0700_set_gpio(adap->dev, GPIO3, GPIO_OUT, 0);
3451         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3452         msleep(400);
3453         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3454         msleep(60);
3455         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3456         msleep(30);
3457         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3458         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
3459         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
3460         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
3461         dib0700_set_gpio(adap->dev, GPIO2, GPIO_OUT, 0);
3462         msleep(30);
3463
3464         /* Put the CX25843 to sleep for now since we're in digital mode */
3465         dib0700_set_gpio(adap->dev, GPIO2, GPIO_OUT, 1);
3466
3467         /* GPIOs are initialized, do the attach */
3468         adap->fe_adap[0].fe = dvb_attach(s5h1411_attach, &pinnacle_801e_config,
3469                               &adap->dev->i2c_adap);
3470         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3471 }
3472
3473 static int dib0700_xc5000_tuner_callback(void *priv, int component,
3474                                          int command, int arg)
3475 {
3476         struct dvb_usb_adapter *adap = priv;
3477
3478         if (command == XC5000_TUNER_RESET) {
3479                 /* Reset the tuner */
3480                 dib0700_set_gpio(adap->dev, GPIO1, GPIO_OUT, 0);
3481                 msleep(10);
3482                 dib0700_set_gpio(adap->dev, GPIO1, GPIO_OUT, 1);
3483                 msleep(10);
3484         } else {
3485                 err("xc5000: unknown tuner callback command: %d\n", command);
3486                 return -EINVAL;
3487         }
3488
3489         return 0;
3490 }
3491
3492 static struct xc5000_config s5h1411_xc5000_tunerconfig = {
3493         .i2c_address      = 0x64,
3494         .if_khz           = 5380,
3495 };
3496
3497 static int xc5000_tuner_attach(struct dvb_usb_adapter *adap)
3498 {
3499         /* FIXME: generalize & move to common area */
3500         adap->fe_adap[0].fe->callback = dib0700_xc5000_tuner_callback;
3501
3502         return dvb_attach(xc5000_attach, adap->fe_adap[0].fe, &adap->dev->i2c_adap,
3503                           &s5h1411_xc5000_tunerconfig)
3504                 == NULL ? -ENODEV : 0;
3505 }
3506
3507 static int dib0700_xc4000_tuner_callback(void *priv, int component,
3508                                          int command, int arg)
3509 {
3510         struct dvb_usb_adapter *adap = priv;
3511         struct dib0700_adapter_state *state = adap->priv;
3512
3513         if (command == XC4000_TUNER_RESET) {
3514                 /* Reset the tuner */
3515                 state->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 0);
3516                 msleep(10);
3517                 state->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3518         } else {
3519                 err("xc4000: unknown tuner callback command: %d\n", command);
3520                 return -EINVAL;
3521         }
3522
3523         return 0;
3524 }
3525
3526 static struct dibx000_agc_config stk7700p_7000p_xc4000_agc_config = {
3527         .band_caps = BAND_UHF | BAND_VHF,
3528         .setup = 0x64,
3529         .inv_gain = 0x02c8,
3530         .time_stabiliz = 0x15,
3531         .alpha_level = 0x00,
3532         .thlock = 0x76,
3533         .wbd_inv = 0x01,
3534         .wbd_ref = 0x0b33,
3535         .wbd_sel = 0x00,
3536         .wbd_alpha = 0x02,
3537         .agc1_max = 0x00,
3538         .agc1_min = 0x00,
3539         .agc2_max = 0x9b26,
3540         .agc2_min = 0x26ca,
3541         .agc1_pt1 = 0x00,
3542         .agc1_pt2 = 0x00,
3543         .agc1_pt3 = 0x00,
3544         .agc1_slope1 = 0x00,
3545         .agc1_slope2 = 0x00,
3546         .agc2_pt1 = 0x00,
3547         .agc2_pt2 = 0x80,
3548         .agc2_slope1 = 0x1d,
3549         .agc2_slope2 = 0x1d,
3550         .alpha_mant = 0x11,
3551         .alpha_exp = 0x1b,
3552         .beta_mant = 0x17,
3553         .beta_exp = 0x33,
3554         .perform_agc_softsplit = 0x00,
3555 };
3556
3557 static struct dibx000_bandwidth_config stk7700p_xc4000_pll_config = {
3558         .internal = 60000,
3559         .sampling = 30000,
3560         .pll_prediv = 1,
3561         .pll_ratio = 8,
3562         .pll_range = 3,
3563         .pll_reset = 1,
3564         .pll_bypass = 0,
3565         .enable_refdiv = 0,
3566         .bypclk_div = 0,
3567         .IO_CLK_en_core = 1,
3568         .ADClkSrc = 1,
3569         .modulo = 0,
3570         .sad_cfg = (3 << 14) | (1 << 12) | 524, /* sad_cfg: refsel, sel, freq_15k */
3571         .ifreq = 39370534,
3572         .timf = 20452225,
3573         .xtal_hz = 30000000
3574 };
3575
3576 /* FIXME: none of these inputs are validated yet */
3577 static struct dib7000p_config pctv_340e_config = {
3578         .output_mpeg2_in_188_bytes = 1,
3579
3580         .agc_config_count = 1,
3581         .agc = &stk7700p_7000p_xc4000_agc_config,
3582         .bw  = &stk7700p_xc4000_pll_config,
3583
3584         .gpio_dir = DIB7000M_GPIO_DEFAULT_DIRECTIONS,
3585         .gpio_val = DIB7000M_GPIO_DEFAULT_VALUES,
3586         .gpio_pwm_pos = DIB7000M_GPIO_DEFAULT_PWM_POS,
3587 };
3588
3589 /* PCTV 340e GPIOs map:
3590    dib0700:
3591    GPIO2  - CX25843 sleep
3592    GPIO3  - CS5340 reset
3593    GPIO5  - IRD
3594    GPIO6  - Power Supply
3595    GPIO8  - LNA (1=off 0=on)
3596    GPIO10 - CX25843 reset
3597    dib7000:
3598    GPIO8  - xc4000 reset
3599  */
3600 static int pctv340e_frontend_attach(struct dvb_usb_adapter *adap)
3601 {
3602         struct dib0700_state *st = adap->dev->priv;
3603         struct dib0700_adapter_state *state = adap->priv;
3604
3605         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3606                 return -ENODEV;
3607
3608         /* Power Supply on */
3609         dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 0);
3610         msleep(50);
3611         dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 1);
3612         msleep(100); /* Allow power supply to settle before probing */
3613
3614         /* cx25843 reset */
3615         dib0700_set_gpio(adap->dev, GPIO10,  GPIO_OUT, 0);
3616         msleep(1); /* cx25843 datasheet say 350us required */
3617         dib0700_set_gpio(adap->dev, GPIO10,  GPIO_OUT, 1);
3618
3619         /* LNA off for now */
3620         dib0700_set_gpio(adap->dev, GPIO8,  GPIO_OUT, 1);
3621
3622         /* Put the CX25843 to sleep for now since we're in digital mode */
3623         dib0700_set_gpio(adap->dev, GPIO2, GPIO_OUT, 1);
3624
3625         /* FIXME: not verified yet */
3626         dib0700_ctrl_clock(adap->dev, 72, 1);
3627
3628         msleep(500);
3629
3630         if (state->dib7000p_ops.dib7000pc_detection(&adap->dev->i2c_adap) == 0) {
3631                 /* Demodulator not found for some reason? */
3632                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3633                 return -ENODEV;
3634         }
3635
3636         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x12,
3637                               &pctv_340e_config);
3638         st->is_dib7000pc = 1;
3639
3640         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3641 }
3642
3643 static struct xc4000_config dib7000p_xc4000_tunerconfig = {
3644         .i2c_address      = 0x61,
3645         .default_pm       = 1,
3646         .dvb_amplitude    = 0,
3647         .set_smoothedcvbs = 0,
3648         .if_khz           = 5400
3649 };
3650
3651 static int xc4000_tuner_attach(struct dvb_usb_adapter *adap)
3652 {
3653         struct i2c_adapter *tun_i2c;
3654         struct dib0700_adapter_state *state = adap->priv;
3655
3656         /* The xc4000 is not on the main i2c bus */
3657         tun_i2c = state->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
3658                                           DIBX000_I2C_INTERFACE_TUNER, 1);
3659         if (tun_i2c == NULL) {
3660                 printk(KERN_ERR "Could not reach tuner i2c bus\n");
3661                 return 0;
3662         }
3663
3664         /* Setup the reset callback */
3665         adap->fe_adap[0].fe->callback = dib0700_xc4000_tuner_callback;
3666
3667         return dvb_attach(xc4000_attach, adap->fe_adap[0].fe, tun_i2c,
3668                           &dib7000p_xc4000_tunerconfig)
3669                 == NULL ? -ENODEV : 0;
3670 }
3671
3672 static struct lgdt3305_config hcw_lgdt3305_config = {
3673         .i2c_addr           = 0x0e,
3674         .mpeg_mode          = LGDT3305_MPEG_PARALLEL,
3675         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
3676         .tpvalid_polarity   = LGDT3305_TP_VALID_LOW,
3677         .deny_i2c_rptr      = 0,
3678         .spectral_inversion = 1,
3679         .qam_if_khz         = 6000,
3680         .vsb_if_khz         = 6000,
3681         .usref_8vsb         = 0x0500,
3682 };
3683
3684 static struct mxl5007t_config hcw_mxl5007t_config = {
3685         .xtal_freq_hz = MxL_XTAL_25_MHZ,
3686         .if_freq_hz = MxL_IF_6_MHZ,
3687         .invert_if = 1,
3688 };
3689
3690 /* TIGER-ATSC map:
3691    GPIO0  - LNA_CTR  (H: LNA power enabled, L: LNA power disabled)
3692    GPIO1  - ANT_SEL  (H: VPA, L: MCX)
3693    GPIO4  - SCL2
3694    GPIO6  - EN_TUNER
3695    GPIO7  - SDA2
3696    GPIO10 - DEM_RST
3697
3698    MXL is behind LG's i2c repeater.  LG is on SCL2/SDA2 gpios on the DIB
3699  */
3700 static int lgdt3305_frontend_attach(struct dvb_usb_adapter *adap)
3701 {
3702         struct dib0700_state *st = adap->dev->priv;
3703
3704         /* Make use of the new i2c functions from FW 1.20 */
3705         st->fw_use_new_i2c_api = 1;
3706
3707         st->disable_streaming_master_mode = 1;
3708
3709         /* fe power enable */
3710         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
3711         msleep(30);
3712         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3713         msleep(30);
3714
3715         /* demod reset */
3716         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3717         msleep(30);
3718         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3719         msleep(30);
3720         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3721         msleep(30);
3722
3723         adap->fe_adap[0].fe = dvb_attach(lgdt3305_attach,
3724                               &hcw_lgdt3305_config,
3725                               &adap->dev->i2c_adap);
3726
3727         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3728 }
3729
3730 static int mxl5007t_tuner_attach(struct dvb_usb_adapter *adap)
3731 {
3732         return dvb_attach(mxl5007t_attach, adap->fe_adap[0].fe,
3733                           &adap->dev->i2c_adap, 0x60,
3734                           &hcw_mxl5007t_config) == NULL ? -ENODEV : 0;
3735 }
3736
3737
3738 /* DVB-USB and USB stuff follows */
3739 struct usb_device_id dib0700_usb_id_table[] = {
3740 /* 0 */ { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7700P) },
3741         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7700P_PC) },
3742         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_500) },
3743         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_500_2) },
3744         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK) },
3745 /* 5 */ { USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR) },
3746         { USB_DEVICE(USB_VID_COMPRO,    USB_PID_COMPRO_VIDEOMATE_U500) },
3747         { USB_DEVICE(USB_VID_UNIWILL,   USB_PID_UNIWILL_STK7700P) },
3748         { USB_DEVICE(USB_VID_LEADTEK,   USB_PID_WINFAST_DTV_DONGLE_STK7700P) },
3749         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK_2) },
3750 /* 10 */{ USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_2) },
3751         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV2000E) },
3752         { USB_DEVICE(USB_VID_TERRATEC,
3753                         USB_PID_TERRATEC_CINERGY_DT_XS_DIVERSITY) },
3754         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_TD_STICK) },
3755         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7700D) },
3756 /* 15 */{ USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7070P) },
3757         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV_DVB_T_FLASH) },
3758         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7070PD) },
3759         { USB_DEVICE(USB_VID_PINNACLE,
3760                         USB_PID_PINNACLE_PCTV_DUAL_DIVERSITY_DVB_T) },
3761         { USB_DEVICE(USB_VID_COMPRO,    USB_PID_COMPRO_VIDEOMATE_U500_PC) },
3762 /* 20 */{ USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_EXPRESS) },
3763         { USB_DEVICE(USB_VID_GIGABYTE,  USB_PID_GIGABYTE_U7000) },
3764         { USB_DEVICE(USB_VID_ULTIMA_ELECTRONIC, USB_PID_ARTEC_T14BR) },
3765         { USB_DEVICE(USB_VID_ASUS,      USB_PID_ASUS_U3000) },
3766         { USB_DEVICE(USB_VID_ASUS,      USB_PID_ASUS_U3100) },
3767 /* 25 */{ USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK_3) },
3768         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_MYTV_T) },
3769         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_HT_USB_XE) },
3770         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_EXPRESSCARD_320CX) },
3771         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV72E) },
3772 /* 30 */{ USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV73E) },
3773         { USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_EC372S) },
3774         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_HT_EXPRESS) },
3775         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_T_XXS) },
3776         { USB_DEVICE(USB_VID_LEADTEK,   USB_PID_WINFAST_DTV_DONGLE_STK7700P_2) },
3777 /* 35 */{ USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_TD_STICK_52009) },
3778         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_500_3) },
3779         { USB_DEVICE(USB_VID_GIGABYTE,  USB_PID_GIGABYTE_U8000) },
3780         { USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_STK7700PH) },
3781         { USB_DEVICE(USB_VID_ASUS,      USB_PID_ASUS_U3000H) },
3782 /* 40 */{ USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV801E) },
3783         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV801E_SE) },
3784         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_T_EXPRESS) },
3785         { USB_DEVICE(USB_VID_TERRATEC,
3786                         USB_PID_TERRATEC_CINERGY_DT_XS_DIVERSITY_2) },
3787         { USB_DEVICE(USB_VID_SONY,      USB_PID_SONY_PLAYTV) },
3788 /* 45 */{ USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_PD378S) },
3789         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_TIGER_ATSC) },
3790         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_TIGER_ATSC_B210) },
3791         { USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_MC770) },
3792         { USB_DEVICE(USB_VID_ELGATO,    USB_PID_ELGATO_EYETV_DTT) },
3793 /* 50 */{ USB_DEVICE(USB_VID_ELGATO,    USB_PID_ELGATO_EYETV_DTT_Dlx) },
3794         { USB_DEVICE(USB_VID_LEADTEK,   USB_PID_WINFAST_DTV_DONGLE_H) },
3795         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_T3) },
3796         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_T5) },
3797         { USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_STK7700D) },
3798 /* 55 */{ USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_STK7700D_2) },
3799         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV73A) },
3800         { USB_DEVICE(USB_VID_PCTV,      USB_PID_PINNACLE_PCTV73ESE) },
3801         { USB_DEVICE(USB_VID_PCTV,      USB_PID_PINNACLE_PCTV282E) },
3802         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7770P) },
3803 /* 60 */{ USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_T_XXS_2) },
3804         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK807XPVR) },
3805         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK807XP) },
3806         { USB_DEVICE_VER(USB_VID_PIXELVIEW, USB_PID_PIXELVIEW_SBTVD, 0x000, 0x3f00) },
3807         { USB_DEVICE(USB_VID_EVOLUTEPC, USB_PID_TVWAY_PLUS) },
3808 /* 65 */{ USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV73ESE) },
3809         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV282E) },
3810         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK8096GP) },
3811         { USB_DEVICE(USB_VID_ELGATO,    USB_PID_ELGATO_EYETV_DIVERSITY) },
3812         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_NIM9090M) },
3813 /* 70 */{ USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_NIM8096MD) },
3814         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_NIM9090MD) },
3815         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_NIM7090) },
3816         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_TFE7090PVR) },
3817         { USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_AIRSTAR_TELESTICK_2) },
3818 /* 75 */{ USB_DEVICE(USB_VID_MEDION,    USB_PID_CREATIX_CTX1921) },
3819         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV340E) },
3820         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV340E_SE) },
3821         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_TFE7790P) },
3822         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_TFE8096P) },
3823 /* 80 */{ USB_DEVICE(USB_VID_ELGATO,    USB_PID_ELGATO_EYETV_DTT_2) },
3824         { USB_DEVICE(USB_VID_PCTV,      USB_PID_PCTV_2002E) },
3825         { USB_DEVICE(USB_VID_PCTV,      USB_PID_PCTV_2002E_SE) },
3826         { USB_DEVICE(USB_VID_PCTV,      USB_PID_DIBCOM_STK8096PVR) },
3827         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK8096PVR) },
3828         { USB_DEVICE(USB_VID_HAMA,      USB_PID_HAMA_DVBT_HYBRID) },
3829         { 0 }           /* Terminating entry */
3830 };
3831 MODULE_DEVICE_TABLE(usb, dib0700_usb_id_table);
3832
3833 #define DIB0700_DEFAULT_DEVICE_PROPERTIES \
3834         .caps              = DVB_USB_IS_AN_I2C_ADAPTER, \
3835         .usb_ctrl          = DEVICE_SPECIFIC, \
3836         .firmware          = "/*(DEBLOBBED)*/", \
3837         .download_firmware = dib0700_download_firmware, \
3838         .no_reconnect      = 1, \
3839         .size_of_priv      = sizeof(struct dib0700_state), \
3840         .i2c_algo          = &dib0700_i2c_algo, \
3841         .identify_state    = dib0700_identify_state
3842
3843 #define DIB0700_DEFAULT_STREAMING_CONFIG(ep) \
3844         .streaming_ctrl   = dib0700_streaming_ctrl, \
3845         .stream = { \
3846                 .type = USB_BULK, \
3847                 .count = 4, \
3848                 .endpoint = ep, \
3849                 .u = { \
3850                         .bulk = { \
3851                                 .buffersize = 39480, \
3852                         } \
3853                 } \
3854         }
3855
3856 #define DIB0700_NUM_FRONTENDS(n) \
3857         .num_frontends = n, \
3858         .size_of_priv     = sizeof(struct dib0700_adapter_state)
3859
3860 struct dvb_usb_device_properties dib0700_devices[] = {
3861         {
3862                 DIB0700_DEFAULT_DEVICE_PROPERTIES,
3863
3864                 .num_adapters = 1,
3865                 .adapter = {
3866                         {
3867                         DIB0700_NUM_FRONTENDS(1),
3868                         .fe = {{
3869                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
3870                                 .pid_filter_count = 32,
3871                                 .pid_filter       = stk7700p_pid_filter,
3872                                 .pid_filter_ctrl  = stk7700p_pid_filter_ctrl,
3873                                 .frontend_attach  = stk7700p_frontend_attach,
3874                                 .tuner_attach     = stk7700p_tuner_attach,
3875
3876                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
3877                         }},
3878                         },
3879                 },
3880
3881                 .num_device_descs = 8,
3882                 .devices = {
3883                         {   "DiBcom STK7700P reference design",
3884                                 { &dib0700_usb_id_table[0], &dib0700_usb_id_table[1] },
3885                                 { NULL },
3886                         },
3887                         {   "Hauppauge Nova-T Stick",
3888                                 { &dib0700_usb_id_table[4], &dib0700_usb_id_table[9], NULL },
3889                                 { NULL },
3890                         },
3891                         {   "AVerMedia AVerTV DVB-T Volar",
3892                                 { &dib0700_usb_id_table[5], &dib0700_usb_id_table[10] },
3893                                 { NULL },
3894                         },
3895                         {   "Compro Videomate U500",
3896                                 { &dib0700_usb_id_table[6], &dib0700_usb_id_table[19] },
3897                                 { NULL },
3898                         },
3899                         {   "Uniwill STK7700P based (Hama and others)",
3900                                 { &dib0700_usb_id_table[7], NULL },
3901                                 { NULL },
3902                         },
3903                         {   "Leadtek Winfast DTV Dongle (STK7700P based)",
3904                                 { &dib0700_usb_id_table[8], &dib0700_usb_id_table[34] },
3905                                 { NULL },
3906                         },
3907                         {   "AVerMedia AVerTV DVB-T Express",
3908                                 { &dib0700_usb_id_table[20] },
3909                                 { NULL },
3910                         },
3911                         {   "Gigabyte U7000",
3912                                 { &dib0700_usb_id_table[21], NULL },
3913                                 { NULL },
3914                         }
3915                 },
3916
3917                 .rc.core = {
3918                         .rc_interval      = DEFAULT_RC_INTERVAL,
3919                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
3920                         .rc_query         = dib0700_rc_query_old_firmware,
3921                         .allowed_protos   = RC_PROTO_BIT_RC5 |
3922                                             RC_PROTO_BIT_RC6_MCE |
3923                                             RC_PROTO_BIT_NEC,
3924                         .change_protocol  = dib0700_change_protocol,
3925                 },
3926         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
3927
3928                 .num_adapters = 2,
3929                 .adapter = {
3930                         {
3931                         DIB0700_NUM_FRONTENDS(1),
3932                         .fe = {{
3933                                 .frontend_attach  = bristol_frontend_attach,
3934                                 .tuner_attach     = bristol_tuner_attach,
3935
3936                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
3937                         }},
3938                         }, {
3939                         DIB0700_NUM_FRONTENDS(1),
3940                         .fe = {{
3941                                 .frontend_attach  = bristol_frontend_attach,
3942                                 .tuner_attach     = bristol_tuner_attach,
3943
3944                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
3945                         }},
3946                         }
3947                 },
3948
3949                 .num_device_descs = 1,
3950                 .devices = {
3951                         {   "Hauppauge Nova-T 500 Dual DVB-T",
3952                                 { &dib0700_usb_id_table[2], &dib0700_usb_id_table[3], NULL },
3953                                 { NULL },
3954                         },
3955                 },
3956
3957                 .rc.core = {
3958                         .rc_interval      = DEFAULT_RC_INTERVAL,
3959                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
3960                         .rc_query         = dib0700_rc_query_old_firmware,
3961                         .allowed_protos   = RC_PROTO_BIT_RC5 |
3962                                             RC_PROTO_BIT_RC6_MCE |
3963                                             RC_PROTO_BIT_NEC,
3964                         .change_protocol = dib0700_change_protocol,
3965                 },
3966         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
3967
3968                 .num_adapters = 2,
3969                 .adapter = {
3970                         {
3971                         DIB0700_NUM_FRONTENDS(1),
3972                         .fe = {{
3973                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
3974                                 .pid_filter_count = 32,
3975                                 .pid_filter       = stk70x0p_pid_filter,
3976                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
3977                                 .frontend_attach  = stk7700d_frontend_attach,
3978                                 .tuner_attach     = stk7700d_tuner_attach,
3979
3980                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
3981                         }},
3982                         }, {
3983                         DIB0700_NUM_FRONTENDS(1),
3984                         .fe = {{
3985                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
3986                                 .pid_filter_count = 32,
3987                                 .pid_filter       = stk70x0p_pid_filter,
3988                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
3989                                 .frontend_attach  = stk7700d_frontend_attach,
3990                                 .tuner_attach     = stk7700d_tuner_attach,
3991
3992                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
3993                         }},
3994                         }
3995                 },
3996
3997                 .num_device_descs = 5,
3998                 .devices = {
3999                         {   "Pinnacle PCTV 2000e",
4000                                 { &dib0700_usb_id_table[11], NULL },
4001                                 { NULL },
4002                         },
4003                         {   "Terratec Cinergy DT XS Diversity",
4004                                 { &dib0700_usb_id_table[12], NULL },
4005                                 { NULL },
4006                         },
4007                         {   "Hauppauge Nova-TD Stick/Elgato Eye-TV Diversity",
4008                                 { &dib0700_usb_id_table[13], NULL },
4009                                 { NULL },
4010                         },
4011                         {   "DiBcom STK7700D reference design",
4012                                 { &dib0700_usb_id_table[14], NULL },
4013                                 { NULL },
4014                         },
4015                         {   "YUAN High-Tech DiBcom STK7700D",
4016                                 { &dib0700_usb_id_table[55], NULL },
4017                                 { NULL },
4018                         },
4019
4020                 },
4021
4022                 .rc.core = {
4023                         .rc_interval      = DEFAULT_RC_INTERVAL,
4024                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4025                         .rc_query         = dib0700_rc_query_old_firmware,
4026                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4027                                             RC_PROTO_BIT_RC6_MCE |
4028                                             RC_PROTO_BIT_NEC,
4029                         .change_protocol = dib0700_change_protocol,
4030                 },
4031         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4032
4033                 .num_adapters = 1,
4034                 .adapter = {
4035                         {
4036                         DIB0700_NUM_FRONTENDS(1),
4037                         .fe = {{
4038                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4039                                 .pid_filter_count = 32,
4040                                 .pid_filter       = stk70x0p_pid_filter,
4041                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4042                                 .frontend_attach  = stk7700P2_frontend_attach,
4043                                 .tuner_attach     = stk7700d_tuner_attach,
4044
4045                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4046                         }},
4047                         },
4048                 },
4049
4050                 .num_device_descs = 3,
4051                 .devices = {
4052                         {   "ASUS My Cinema U3000 Mini DVBT Tuner",
4053                                 { &dib0700_usb_id_table[23], NULL },
4054                                 { NULL },
4055                         },
4056                         {   "Yuan EC372S",
4057                                 { &dib0700_usb_id_table[31], NULL },
4058                                 { NULL },
4059                         },
4060                         {   "Terratec Cinergy T Express",
4061                                 { &dib0700_usb_id_table[42], NULL },
4062                                 { NULL },
4063                         }
4064                 },
4065
4066                 .rc.core = {
4067                         .rc_interval      = DEFAULT_RC_INTERVAL,
4068                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4069                         .module_name      = "dib0700",
4070                         .rc_query         = dib0700_rc_query_old_firmware,
4071                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4072                                             RC_PROTO_BIT_RC6_MCE |
4073                                             RC_PROTO_BIT_NEC,
4074                         .change_protocol = dib0700_change_protocol,
4075                 },
4076         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4077
4078                 .num_adapters = 1,
4079                 .adapter = {
4080                         {
4081                         DIB0700_NUM_FRONTENDS(1),
4082                         .fe = {{
4083                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4084                                 .pid_filter_count = 32,
4085                                 .pid_filter       = stk70x0p_pid_filter,
4086                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4087                                 .frontend_attach  = stk7070p_frontend_attach,
4088                                 .tuner_attach     = dib7070p_tuner_attach,
4089
4090                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4091                         }},
4092                         },
4093                 },
4094
4095                 .num_device_descs = 12,
4096                 .devices = {
4097                         {   "DiBcom STK7070P reference design",
4098                                 { &dib0700_usb_id_table[15], NULL },
4099                                 { NULL },
4100                         },
4101                         {   "Pinnacle PCTV DVB-T Flash Stick",
4102                                 { &dib0700_usb_id_table[16], NULL },
4103                                 { NULL },
4104                         },
4105                         {   "Artec T14BR DVB-T",
4106                                 { &dib0700_usb_id_table[22], NULL },
4107                                 { NULL },
4108                         },
4109                         {   "ASUS My Cinema U3100 Mini DVBT Tuner",
4110                                 { &dib0700_usb_id_table[24], NULL },
4111                                 { NULL },
4112                         },
4113                         {   "Hauppauge Nova-T Stick",
4114                                 { &dib0700_usb_id_table[25], NULL },
4115                                 { NULL },
4116                         },
4117                         {   "Hauppauge Nova-T MyTV.t",
4118                                 { &dib0700_usb_id_table[26], NULL },
4119                                 { NULL },
4120                         },
4121                         {   "Pinnacle PCTV 72e",
4122                                 { &dib0700_usb_id_table[29], NULL },
4123                                 { NULL },
4124                         },
4125                         {   "Pinnacle PCTV 73e",
4126                                 { &dib0700_usb_id_table[30], NULL },
4127                                 { NULL },
4128                         },
4129                         {   "Elgato EyeTV DTT",
4130                                 { &dib0700_usb_id_table[49], NULL },
4131                                 { NULL },
4132                         },
4133                         {   "Yuan PD378S",
4134                                 { &dib0700_usb_id_table[45], NULL },
4135                                 { NULL },
4136                         },
4137                         {   "Elgato EyeTV Dtt Dlx PD378S",
4138                                 { &dib0700_usb_id_table[50], NULL },
4139                                 { NULL },
4140                         },
4141                         {   "Elgato EyeTV DTT rev. 2",
4142                                 { &dib0700_usb_id_table[80], NULL },
4143                                 { NULL },
4144                         },
4145                 },
4146
4147                 .rc.core = {
4148                         .rc_interval      = DEFAULT_RC_INTERVAL,
4149                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4150                         .module_name      = "dib0700",
4151                         .rc_query         = dib0700_rc_query_old_firmware,
4152                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4153                                             RC_PROTO_BIT_RC6_MCE |
4154                                             RC_PROTO_BIT_NEC,
4155                         .change_protocol  = dib0700_change_protocol,
4156                 },
4157         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4158
4159                 .num_adapters = 1,
4160                 .adapter = {
4161                         {
4162                         DIB0700_NUM_FRONTENDS(1),
4163                         .fe = {{
4164                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4165                                 .pid_filter_count = 32,
4166                                 .pid_filter       = stk70x0p_pid_filter,
4167                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4168                                 .frontend_attach  = stk7070p_frontend_attach,
4169                                 .tuner_attach     = dib7070p_tuner_attach,
4170
4171                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4172                         }},
4173                         },
4174                 },
4175
4176                 .num_device_descs = 3,
4177                 .devices = {
4178                         {   "Pinnacle PCTV 73A",
4179                                 { &dib0700_usb_id_table[56], NULL },
4180                                 { NULL },
4181                         },
4182                         {   "Pinnacle PCTV 73e SE",
4183                                 { &dib0700_usb_id_table[57], &dib0700_usb_id_table[65], NULL },
4184                                 { NULL },
4185                         },
4186                         {   "Pinnacle PCTV 282e",
4187                                 { &dib0700_usb_id_table[58], &dib0700_usb_id_table[66], NULL },
4188                                 { NULL },
4189                         },
4190                 },
4191
4192                 .rc.core = {
4193                         .rc_interval      = DEFAULT_RC_INTERVAL,
4194                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4195                         .module_name      = "dib0700",
4196                         .rc_query         = dib0700_rc_query_old_firmware,
4197                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4198                                             RC_PROTO_BIT_RC6_MCE |
4199                                             RC_PROTO_BIT_NEC,
4200                         .change_protocol  = dib0700_change_protocol,
4201                 },
4202         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4203
4204                 .num_adapters = 2,
4205                 .adapter = {
4206                         {
4207                         DIB0700_NUM_FRONTENDS(1),
4208                         .fe = {{
4209                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4210                                 .pid_filter_count = 32,
4211                                 .pid_filter       = stk70x0p_pid_filter,
4212                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4213                                 .frontend_attach  = novatd_frontend_attach,
4214                                 .tuner_attach     = dib7070p_tuner_attach,
4215
4216                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4217                         }},
4218                         }, {
4219                         DIB0700_NUM_FRONTENDS(1),
4220                         .fe = {{
4221                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4222                                 .pid_filter_count = 32,
4223                                 .pid_filter       = stk70x0p_pid_filter,
4224                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4225                                 .frontend_attach  = novatd_frontend_attach,
4226                                 .tuner_attach     = dib7070p_tuner_attach,
4227
4228                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4229                         }},
4230                         }
4231                 },
4232
4233                 .num_device_descs = 3,
4234                 .devices = {
4235                         {   "Hauppauge Nova-TD Stick (52009)",
4236                                 { &dib0700_usb_id_table[35], NULL },
4237                                 { NULL },
4238                         },
4239                         {   "PCTV 2002e",
4240                                 { &dib0700_usb_id_table[81], NULL },
4241                                 { NULL },
4242                         },
4243                         {   "PCTV 2002e SE",
4244                                 { &dib0700_usb_id_table[82], NULL },
4245                                 { NULL },
4246                         },
4247                 },
4248
4249                 .rc.core = {
4250                         .rc_interval      = DEFAULT_RC_INTERVAL,
4251                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4252                         .module_name      = "dib0700",
4253                         .rc_query         = dib0700_rc_query_old_firmware,
4254                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4255                                             RC_PROTO_BIT_RC6_MCE |
4256                                             RC_PROTO_BIT_NEC,
4257                         .change_protocol = dib0700_change_protocol,
4258                 },
4259         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4260
4261                 .num_adapters = 2,
4262                 .adapter = {
4263                         {
4264                         DIB0700_NUM_FRONTENDS(1),
4265                         .fe = {{
4266                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4267                                 .pid_filter_count = 32,
4268                                 .pid_filter       = stk70x0p_pid_filter,
4269                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4270                                 .frontend_attach  = stk7070pd_frontend_attach0,
4271                                 .tuner_attach     = dib7070p_tuner_attach,
4272
4273                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4274                         }},
4275                         }, {
4276                         DIB0700_NUM_FRONTENDS(1),
4277                         .fe = {{
4278                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4279                                 .pid_filter_count = 32,
4280                                 .pid_filter       = stk70x0p_pid_filter,
4281                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4282                                 .frontend_attach  = stk7070pd_frontend_attach1,
4283                                 .tuner_attach     = dib7070p_tuner_attach,
4284
4285                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4286                         }},
4287                         }
4288                 },
4289
4290                 .num_device_descs = 5,
4291                 .devices = {
4292                         {   "DiBcom STK7070PD reference design",
4293                                 { &dib0700_usb_id_table[17], NULL },
4294                                 { NULL },
4295                         },
4296                         {   "Pinnacle PCTV Dual DVB-T Diversity Stick",
4297                                 { &dib0700_usb_id_table[18], NULL },
4298                                 { NULL },
4299                         },
4300                         {   "Hauppauge Nova-TD-500 (84xxx)",
4301                                 { &dib0700_usb_id_table[36], NULL },
4302                                 { NULL },
4303                         },
4304                         {  "Terratec Cinergy DT USB XS Diversity/ T5",
4305                                 { &dib0700_usb_id_table[43],
4306                                         &dib0700_usb_id_table[53], NULL},
4307                                 { NULL },
4308                         },
4309                         {  "Sony PlayTV",
4310                                 { &dib0700_usb_id_table[44], NULL },
4311                                 { NULL },
4312                         },
4313                 },
4314
4315                 .rc.core = {
4316                         .rc_interval      = DEFAULT_RC_INTERVAL,
4317                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4318                         .module_name      = "dib0700",
4319                         .rc_query         = dib0700_rc_query_old_firmware,
4320                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4321                                             RC_PROTO_BIT_RC6_MCE |
4322                                             RC_PROTO_BIT_NEC,
4323                         .change_protocol = dib0700_change_protocol,
4324                 },
4325         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4326
4327                 .num_adapters = 2,
4328                 .adapter = {
4329                         {
4330                         DIB0700_NUM_FRONTENDS(1),
4331                         .fe = {{
4332                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4333                                 .pid_filter_count = 32,
4334                                 .pid_filter       = stk70x0p_pid_filter,
4335                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4336                                 .frontend_attach  = stk7070pd_frontend_attach0,
4337                                 .tuner_attach     = dib7070p_tuner_attach,
4338
4339                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4340                         }},
4341                         }, {
4342                         DIB0700_NUM_FRONTENDS(1),
4343                         .fe = {{
4344                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4345                                 .pid_filter_count = 32,
4346                                 .pid_filter       = stk70x0p_pid_filter,
4347                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4348                                 .frontend_attach  = stk7070pd_frontend_attach1,
4349                                 .tuner_attach     = dib7070p_tuner_attach,
4350
4351                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4352                         }},
4353                         }
4354                 },
4355
4356                 .num_device_descs = 1,
4357                 .devices = {
4358                         {   "Elgato EyeTV Diversity",
4359                                 { &dib0700_usb_id_table[68], NULL },
4360                                 { NULL },
4361                         },
4362                 },
4363
4364                 .rc.core = {
4365                         .rc_interval      = DEFAULT_RC_INTERVAL,
4366                         .rc_codes         = RC_MAP_DIB0700_NEC_TABLE,
4367                         .module_name      = "dib0700",
4368                         .rc_query         = dib0700_rc_query_old_firmware,
4369                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4370                                             RC_PROTO_BIT_RC6_MCE |
4371                                             RC_PROTO_BIT_NEC,
4372                         .change_protocol  = dib0700_change_protocol,
4373                 },
4374         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4375
4376                 .num_adapters = 1,
4377                 .adapter = {
4378                         {
4379                         DIB0700_NUM_FRONTENDS(1),
4380                         .fe = {{
4381                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4382                                 .pid_filter_count = 32,
4383                                 .pid_filter       = stk70x0p_pid_filter,
4384                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4385                                 .frontend_attach  = stk7700ph_frontend_attach,
4386                                 .tuner_attach     = stk7700ph_tuner_attach,
4387
4388                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4389                         }},
4390                         },
4391                 },
4392
4393                 .num_device_descs = 10,
4394                 .devices = {
4395                         {   "Terratec Cinergy HT USB XE",
4396                                 { &dib0700_usb_id_table[27], NULL },
4397                                 { NULL },
4398                         },
4399                         {   "Pinnacle Expresscard 320cx",
4400                                 { &dib0700_usb_id_table[28], NULL },
4401                                 { NULL },
4402                         },
4403                         {   "Terratec Cinergy HT Express",
4404                                 { &dib0700_usb_id_table[32], NULL },
4405                                 { NULL },
4406                         },
4407                         {   "Gigabyte U8000-RH",
4408                                 { &dib0700_usb_id_table[37], NULL },
4409                                 { NULL },
4410                         },
4411                         {   "YUAN High-Tech STK7700PH",
4412                                 { &dib0700_usb_id_table[38], NULL },
4413                                 { NULL },
4414                         },
4415                         {   "Asus My Cinema-U3000Hybrid",
4416                                 { &dib0700_usb_id_table[39], NULL },
4417                                 { NULL },
4418                         },
4419                         {   "YUAN High-Tech MC770",
4420                                 { &dib0700_usb_id_table[48], NULL },
4421                                 { NULL },
4422                         },
4423                         {   "Leadtek WinFast DTV Dongle H",
4424                                 { &dib0700_usb_id_table[51], NULL },
4425                                 { NULL },
4426                         },
4427                         {   "YUAN High-Tech STK7700D",
4428                                 { &dib0700_usb_id_table[54], NULL },
4429                                 { NULL },
4430                         },
4431                         {   "Hama DVB=T Hybrid USB Stick",
4432                                 { &dib0700_usb_id_table[85], NULL },
4433                                 { NULL },
4434                         },
4435                 },
4436
4437                 .rc.core = {
4438                         .rc_interval      = DEFAULT_RC_INTERVAL,
4439                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4440                         .module_name      = "dib0700",
4441                         .rc_query         = dib0700_rc_query_old_firmware,
4442                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4443                                             RC_PROTO_BIT_RC6_MCE |
4444                                             RC_PROTO_BIT_NEC,
4445                         .change_protocol  = dib0700_change_protocol,
4446                 },
4447         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4448                 .num_adapters = 1,
4449                 .adapter = {
4450                         {
4451                         DIB0700_NUM_FRONTENDS(1),
4452                         .fe = {{
4453                                 .frontend_attach  = s5h1411_frontend_attach,
4454                                 .tuner_attach     = xc5000_tuner_attach,
4455
4456                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4457                         }},
4458                         },
4459                 },
4460
4461                 .num_device_descs = 2,
4462                 .devices = {
4463                         {   "Pinnacle PCTV HD Pro USB Stick",
4464                                 { &dib0700_usb_id_table[40], NULL },
4465                                 { NULL },
4466                         },
4467                         {   "Pinnacle PCTV HD USB Stick",
4468                                 { &dib0700_usb_id_table[41], NULL },
4469                                 { NULL },
4470                         },
4471                 },
4472
4473                 .rc.core = {
4474                         .rc_interval      = DEFAULT_RC_INTERVAL,
4475                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4476                         .module_name      = "dib0700",
4477                         .rc_query         = dib0700_rc_query_old_firmware,
4478                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4479                                             RC_PROTO_BIT_RC6_MCE |
4480                                             RC_PROTO_BIT_NEC,
4481                         .change_protocol  = dib0700_change_protocol,
4482                 },
4483         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4484                 .num_adapters = 1,
4485                 .adapter = {
4486                         {
4487                         DIB0700_NUM_FRONTENDS(1),
4488                         .fe = {{
4489                                 .frontend_attach  = lgdt3305_frontend_attach,
4490                                 .tuner_attach     = mxl5007t_tuner_attach,
4491
4492                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4493                         }},
4494                         },
4495                 },
4496
4497                 .num_device_descs = 2,
4498                 .devices = {
4499                         {   "Hauppauge ATSC MiniCard (B200)",
4500                                 { &dib0700_usb_id_table[46], NULL },
4501                                 { NULL },
4502                         },
4503                         {   "Hauppauge ATSC MiniCard (B210)",
4504                                 { &dib0700_usb_id_table[47], NULL },
4505                                 { NULL },
4506                         },
4507                 },
4508         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4509
4510                 .num_adapters = 1,
4511                 .adapter = {
4512                         {
4513                         DIB0700_NUM_FRONTENDS(1),
4514                         .fe = {{
4515                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4516                                 .pid_filter_count = 32,
4517                                 .pid_filter       = stk70x0p_pid_filter,
4518                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4519                                 .frontend_attach  = stk7770p_frontend_attach,
4520                                 .tuner_attach     = dib7770p_tuner_attach,
4521
4522                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4523                         }},
4524                         },
4525                 },
4526
4527                 .num_device_descs = 4,
4528                 .devices = {
4529                         {   "DiBcom STK7770P reference design",
4530                                 { &dib0700_usb_id_table[59], NULL },
4531                                 { NULL },
4532                         },
4533                         {   "Terratec Cinergy T USB XXS (HD)/ T3",
4534                                 { &dib0700_usb_id_table[33],
4535                                         &dib0700_usb_id_table[52],
4536                                         &dib0700_usb_id_table[60], NULL},
4537                                 { NULL },
4538                         },
4539                         {   "TechniSat AirStar TeleStick 2",
4540                                 { &dib0700_usb_id_table[74], NULL },
4541                                 { NULL },
4542                         },
4543                         {   "Medion CTX1921 DVB-T USB",
4544                                 { &dib0700_usb_id_table[75], NULL },
4545                                 { NULL },
4546                         },
4547                 },
4548
4549                 .rc.core = {
4550                         .rc_interval      = DEFAULT_RC_INTERVAL,
4551                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4552                         .module_name      = "dib0700",
4553                         .rc_query         = dib0700_rc_query_old_firmware,
4554                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4555                                             RC_PROTO_BIT_RC6_MCE |
4556                                             RC_PROTO_BIT_NEC,
4557                         .change_protocol  = dib0700_change_protocol,
4558                 },
4559         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4560                 .num_adapters = 1,
4561                 .adapter = {
4562                         {
4563                         DIB0700_NUM_FRONTENDS(1),
4564                         .fe = {{
4565                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4566                                 .pid_filter_count = 32,
4567                                 .pid_filter = stk80xx_pid_filter,
4568                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4569                                 .frontend_attach  = stk807x_frontend_attach,
4570                                 .tuner_attach     = dib807x_tuner_attach,
4571
4572                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4573                         }},
4574                         },
4575                 },
4576
4577                 .num_device_descs = 3,
4578                 .devices = {
4579                         {   "DiBcom STK807xP reference design",
4580                                 { &dib0700_usb_id_table[62], NULL },
4581                                 { NULL },
4582                         },
4583                         {   "Prolink Pixelview SBTVD",
4584                                 { &dib0700_usb_id_table[63], NULL },
4585                                 { NULL },
4586                         },
4587                         {   "EvolutePC TVWay+",
4588                                 { &dib0700_usb_id_table[64], NULL },
4589                                 { NULL },
4590                         },
4591                 },
4592
4593                 .rc.core = {
4594                         .rc_interval      = DEFAULT_RC_INTERVAL,
4595                         .rc_codes         = RC_MAP_DIB0700_NEC_TABLE,
4596                         .module_name      = "dib0700",
4597                         .rc_query         = dib0700_rc_query_old_firmware,
4598                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4599                                             RC_PROTO_BIT_RC6_MCE |
4600                                             RC_PROTO_BIT_NEC,
4601                         .change_protocol  = dib0700_change_protocol,
4602                 },
4603         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4604                 .num_adapters = 2,
4605                 .adapter = {
4606                         {
4607                         DIB0700_NUM_FRONTENDS(1),
4608                         .fe = {{
4609                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4610                                 .pid_filter_count = 32,
4611                                 .pid_filter = stk80xx_pid_filter,
4612                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4613                                 .frontend_attach  = stk807xpvr_frontend_attach0,
4614                                 .tuner_attach     = dib807x_tuner_attach,
4615
4616                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4617                         }},
4618                         },
4619                         {
4620                         DIB0700_NUM_FRONTENDS(1),
4621                         .fe = {{
4622                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4623                                 .pid_filter_count = 32,
4624                                 .pid_filter = stk80xx_pid_filter,
4625                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4626                                 .frontend_attach  = stk807xpvr_frontend_attach1,
4627                                 .tuner_attach     = dib807x_tuner_attach,
4628
4629                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4630                         }},
4631                         },
4632                 },
4633
4634                 .num_device_descs = 1,
4635                 .devices = {
4636                         {   "DiBcom STK807xPVR reference design",
4637                                 { &dib0700_usb_id_table[61], NULL },
4638                                 { NULL },
4639                         },
4640                 },
4641
4642                 .rc.core = {
4643                         .rc_interval      = DEFAULT_RC_INTERVAL,
4644                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4645                         .module_name      = "dib0700",
4646                         .rc_query         = dib0700_rc_query_old_firmware,
4647                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4648                                             RC_PROTO_BIT_RC6_MCE |
4649                                             RC_PROTO_BIT_NEC,
4650                         .change_protocol  = dib0700_change_protocol,
4651                 },
4652         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4653                 .num_adapters = 1,
4654                 .adapter = {
4655                         {
4656                         DIB0700_NUM_FRONTENDS(1),
4657                         .fe = {{
4658                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4659                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4660                                 .pid_filter_count = 32,
4661                                 .pid_filter = stk80xx_pid_filter,
4662                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4663                                 .frontend_attach  = stk809x_frontend_attach,
4664                                 .tuner_attach     = dib809x_tuner_attach,
4665
4666                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4667                         }},
4668                         },
4669                 },
4670
4671                 .num_device_descs = 1,
4672                 .devices = {
4673                         {   "DiBcom STK8096GP reference design",
4674                                 { &dib0700_usb_id_table[67], NULL },
4675                                 { NULL },
4676                         },
4677                 },
4678
4679                 .rc.core = {
4680                         .rc_interval      = DEFAULT_RC_INTERVAL,
4681                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4682                         .module_name      = "dib0700",
4683                         .rc_query         = dib0700_rc_query_old_firmware,
4684                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4685                                             RC_PROTO_BIT_RC6_MCE |
4686                                             RC_PROTO_BIT_NEC,
4687                         .change_protocol  = dib0700_change_protocol,
4688                 },
4689         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4690                 .num_adapters = 1,
4691                 .adapter = {
4692                         {
4693                         DIB0700_NUM_FRONTENDS(1),
4694                         .fe = {{
4695                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4696                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4697                                 .pid_filter_count = 32,
4698                                 .pid_filter = dib90x0_pid_filter,
4699                                 .pid_filter_ctrl = dib90x0_pid_filter_ctrl,
4700                                 .frontend_attach  = stk9090m_frontend_attach,
4701                                 .tuner_attach     = dib9090_tuner_attach,
4702
4703                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4704                         }},
4705                         },
4706                 },
4707
4708                 .num_device_descs = 1,
4709                 .devices = {
4710                         {   "DiBcom STK9090M reference design",
4711                                 { &dib0700_usb_id_table[69], NULL },
4712                                 { NULL },
4713                         },
4714                 },
4715
4716                 .rc.core = {
4717                         .rc_interval      = DEFAULT_RC_INTERVAL,
4718                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4719                         .module_name      = "dib0700",
4720                         .rc_query         = dib0700_rc_query_old_firmware,
4721                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4722                                             RC_PROTO_BIT_RC6_MCE |
4723                                             RC_PROTO_BIT_NEC,
4724                         .change_protocol  = dib0700_change_protocol,
4725                 },
4726         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4727                 .num_adapters = 1,
4728                 .adapter = {
4729                         {
4730                         DIB0700_NUM_FRONTENDS(1),
4731                         .fe = {{
4732                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4733                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4734                                 .pid_filter_count = 32,
4735                                 .pid_filter = stk80xx_pid_filter,
4736                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4737                                 .frontend_attach  = nim8096md_frontend_attach,
4738                                 .tuner_attach     = nim8096md_tuner_attach,
4739
4740                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4741                         }},
4742                         },
4743                 },
4744
4745                 .num_device_descs = 1,
4746                 .devices = {
4747                         {   "DiBcom NIM8096MD reference design",
4748                                 { &dib0700_usb_id_table[70], NULL },
4749                                 { NULL },
4750                         },
4751                 },
4752
4753                 .rc.core = {
4754                         .rc_interval      = DEFAULT_RC_INTERVAL,
4755                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4756                         .module_name      = "dib0700",
4757                         .rc_query         = dib0700_rc_query_old_firmware,
4758                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4759                                             RC_PROTO_BIT_RC6_MCE |
4760                                             RC_PROTO_BIT_NEC,
4761                         .change_protocol  = dib0700_change_protocol,
4762                 },
4763         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4764                 .num_adapters = 1,
4765                 .adapter = {
4766                         {
4767                         DIB0700_NUM_FRONTENDS(1),
4768                         .fe = {{
4769                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4770                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4771                                 .pid_filter_count = 32,
4772                                 .pid_filter = dib90x0_pid_filter,
4773                                 .pid_filter_ctrl = dib90x0_pid_filter_ctrl,
4774                                 .frontend_attach  = nim9090md_frontend_attach,
4775                                 .tuner_attach     = nim9090md_tuner_attach,
4776
4777                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4778                         }},
4779                         },
4780                 },
4781
4782                 .num_device_descs = 1,
4783                 .devices = {
4784                         {   "DiBcom NIM9090MD reference design",
4785                                 { &dib0700_usb_id_table[71], NULL },
4786                                 { NULL },
4787                         },
4788                 },
4789
4790                 .rc.core = {
4791                         .rc_interval      = DEFAULT_RC_INTERVAL,
4792                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4793                         .module_name      = "dib0700",
4794                         .rc_query         = dib0700_rc_query_old_firmware,
4795                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4796                                             RC_PROTO_BIT_RC6_MCE |
4797                                             RC_PROTO_BIT_NEC,
4798                         .change_protocol  = dib0700_change_protocol,
4799                 },
4800         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4801                 .num_adapters = 1,
4802                 .adapter = {
4803                         {
4804                         DIB0700_NUM_FRONTENDS(1),
4805                         .fe = {{
4806                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4807                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4808                                 .pid_filter_count = 32,
4809                                 .pid_filter = stk70x0p_pid_filter,
4810                                 .pid_filter_ctrl = stk70x0p_pid_filter_ctrl,
4811                                 .frontend_attach  = nim7090_frontend_attach,
4812                                 .tuner_attach     = nim7090_tuner_attach,
4813
4814                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4815                         }},
4816                         },
4817                 },
4818
4819                 .num_device_descs = 1,
4820                 .devices = {
4821                         {   "DiBcom NIM7090 reference design",
4822                                 { &dib0700_usb_id_table[72], NULL },
4823                                 { NULL },
4824                         },
4825                 },
4826
4827                 .rc.core = {
4828                         .rc_interval      = DEFAULT_RC_INTERVAL,
4829                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4830                         .module_name      = "dib0700",
4831                         .rc_query         = dib0700_rc_query_old_firmware,
4832                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4833                                             RC_PROTO_BIT_RC6_MCE |
4834                                             RC_PROTO_BIT_NEC,
4835                         .change_protocol  = dib0700_change_protocol,
4836                 },
4837         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4838                 .num_adapters = 2,
4839                 .adapter = {
4840                         {
4841                         DIB0700_NUM_FRONTENDS(1),
4842                         .fe = {{
4843                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4844                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4845                                 .pid_filter_count = 32,
4846                                 .pid_filter = stk70x0p_pid_filter,
4847                                 .pid_filter_ctrl = stk70x0p_pid_filter_ctrl,
4848                                 .frontend_attach  = tfe7090pvr_frontend0_attach,
4849                                 .tuner_attach     = tfe7090pvr_tuner0_attach,
4850
4851                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4852                         }},
4853                         },
4854                         {
4855                         DIB0700_NUM_FRONTENDS(1),
4856                         .fe = {{
4857                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4858                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4859                                 .pid_filter_count = 32,
4860                                 .pid_filter = stk70x0p_pid_filter,
4861                                 .pid_filter_ctrl = stk70x0p_pid_filter_ctrl,
4862                                 .frontend_attach  = tfe7090pvr_frontend1_attach,
4863                                 .tuner_attach     = tfe7090pvr_tuner1_attach,
4864
4865                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4866                         }},
4867                         },
4868                 },
4869
4870                 .num_device_descs = 1,
4871                 .devices = {
4872                         {   "DiBcom TFE7090PVR reference design",
4873                                 { &dib0700_usb_id_table[73], NULL },
4874                                 { NULL },
4875                         },
4876                 },
4877
4878                 .rc.core = {
4879                         .rc_interval      = DEFAULT_RC_INTERVAL,
4880                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4881                         .module_name      = "dib0700",
4882                         .rc_query         = dib0700_rc_query_old_firmware,
4883                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4884                                             RC_PROTO_BIT_RC6_MCE |
4885                                             RC_PROTO_BIT_NEC,
4886                         .change_protocol  = dib0700_change_protocol,
4887                 },
4888         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4889                 .num_adapters = 1,
4890                 .adapter = {
4891                         {
4892                         DIB0700_NUM_FRONTENDS(1),
4893                         .fe = {{
4894                                 .frontend_attach  = pctv340e_frontend_attach,
4895                                 .tuner_attach     = xc4000_tuner_attach,
4896
4897                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4898                         }},
4899                         },
4900                 },
4901
4902                 .num_device_descs = 2,
4903                 .devices = {
4904                         {   "Pinnacle PCTV 340e HD Pro USB Stick",
4905                                 { &dib0700_usb_id_table[76], NULL },
4906                                 { NULL },
4907                         },
4908                         {   "Pinnacle PCTV Hybrid Stick Solo",
4909                                 { &dib0700_usb_id_table[77], NULL },
4910                                 { NULL },
4911                         },
4912                 },
4913                 .rc.core = {
4914                         .rc_interval      = DEFAULT_RC_INTERVAL,
4915                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4916                         .module_name      = "dib0700",
4917                         .rc_query         = dib0700_rc_query_old_firmware,
4918                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4919                                             RC_PROTO_BIT_RC6_MCE |
4920                                             RC_PROTO_BIT_NEC,
4921                         .change_protocol  = dib0700_change_protocol,
4922                 },
4923         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4924                 .num_adapters = 1,
4925                 .adapter = {
4926                         {
4927                                 DIB0700_NUM_FRONTENDS(1),
4928                                 .fe = {{
4929                                         .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4930                                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4931                                         .pid_filter_count = 32,
4932                                         .pid_filter = stk70x0p_pid_filter,
4933                                         .pid_filter_ctrl = stk70x0p_pid_filter_ctrl,
4934                                         .frontend_attach  = tfe7790p_frontend_attach,
4935                                         .tuner_attach     = tfe7790p_tuner_attach,
4936
4937                                         DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4938                                 } },
4939                         },
4940                 },
4941
4942                 .num_device_descs = 1,
4943                 .devices = {
4944                         {   "DiBcom TFE7790P reference design",
4945                                 { &dib0700_usb_id_table[78], NULL },
4946                                 { NULL },
4947                         },
4948                 },
4949
4950                 .rc.core = {
4951                         .rc_interval      = DEFAULT_RC_INTERVAL,
4952                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4953                         .module_name      = "dib0700",
4954                         .rc_query         = dib0700_rc_query_old_firmware,
4955                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4956                                             RC_PROTO_BIT_RC6_MCE |
4957                                             RC_PROTO_BIT_NEC,
4958                         .change_protocol  = dib0700_change_protocol,
4959                 },
4960         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4961                 .num_adapters = 1,
4962                 .adapter = {
4963                         {
4964                                 DIB0700_NUM_FRONTENDS(1),
4965                                 .fe = {{
4966                                         .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4967                                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4968                                         .pid_filter_count = 32,
4969                                         .pid_filter = stk80xx_pid_filter,
4970                                         .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4971                                         .frontend_attach  = tfe8096p_frontend_attach,
4972                                         .tuner_attach     = tfe8096p_tuner_attach,
4973
4974                                         DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4975
4976                                 } },
4977                         },
4978                 },
4979
4980                 .num_device_descs = 1,
4981                 .devices = {
4982                         {   "DiBcom TFE8096P reference design",
4983                                 { &dib0700_usb_id_table[79], NULL },
4984                                 { NULL },
4985                         },
4986                 },
4987
4988                 .rc.core = {
4989                         .rc_interval      = DEFAULT_RC_INTERVAL,
4990                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4991                         .module_name      = "dib0700",
4992                         .rc_query         = dib0700_rc_query_old_firmware,
4993                         .allowed_protos   = RC_PROTO_BIT_RC5 |
4994                                             RC_PROTO_BIT_RC6_MCE |
4995                                             RC_PROTO_BIT_NEC,
4996                         .change_protocol  = dib0700_change_protocol,
4997                 },
4998         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4999                 .num_adapters = 2,
5000                 .adapter = {
5001                         {
5002                                 .num_frontends = 1,
5003                                 .fe = {{
5004                                         .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
5005                                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
5006                                         .pid_filter_count = 32,
5007                                         .pid_filter = stk80xx_pid_filter,
5008                                         .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
5009                                         .frontend_attach  = stk809x_frontend_attach,
5010                                         .tuner_attach     = dib809x_tuner_attach,
5011
5012                                         DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
5013                                 } },
5014                                 .size_of_priv =
5015                                         sizeof(struct dib0700_adapter_state),
5016                         }, {
5017                                 .num_frontends = 1,
5018                                 .fe = { {
5019                                         .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
5020                                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
5021                                         .pid_filter_count = 32,
5022                                         .pid_filter = stk80xx_pid_filter,
5023                                         .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
5024                                         .frontend_attach  = stk809x_frontend1_attach,
5025                                         .tuner_attach     = dib809x_tuner_attach,
5026
5027                                         DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
5028                                 } },
5029                                 .size_of_priv =
5030                                         sizeof(struct dib0700_adapter_state),
5031                         },
5032                 },
5033                 .num_device_descs = 1,
5034                 .devices = {
5035                         {   "DiBcom STK8096-PVR reference design",
5036                                 { &dib0700_usb_id_table[83],
5037                                         &dib0700_usb_id_table[84], NULL},
5038                                 { NULL },
5039                         },
5040                 },
5041
5042                 .rc.core = {
5043                         .rc_interval      = DEFAULT_RC_INTERVAL,
5044                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
5045                         .module_name  = "dib0700",
5046                         .rc_query         = dib0700_rc_query_old_firmware,
5047                         .allowed_protos   = RC_PROTO_BIT_RC5 |
5048                                 RC_PROTO_BIT_RC6_MCE |
5049                                 RC_PROTO_BIT_NEC,
5050                         .change_protocol  = dib0700_change_protocol,
5051                 },
5052         },
5053 };
5054
5055 int dib0700_device_count = ARRAY_SIZE(dib0700_devices);