GNU Linux-libre 4.4.288-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), "
30                 "if applicable for the device (default: 0=automatic/off).");
31
32 struct dib0700_adapter_state {
33         int (*set_param_save) (struct dvb_frontend *);
34         const struct firmware *frontend_firmware;
35         struct dib7000p_ops dib7000p_ops;
36         struct dib8000_ops dib8000_ops;
37 };
38
39 /* Hauppauge Nova-T 500 (aka Bristol)
40  *  has a LNA on GPIO0 which is enabled by setting 1 */
41 static struct mt2060_config bristol_mt2060_config[2] = {
42         {
43                 .i2c_address = 0x60,
44                 .clock_out   = 3,
45         }, {
46                 .i2c_address = 0x61,
47         }
48 };
49
50
51 static struct dibx000_agc_config bristol_dib3000p_mt2060_agc_config = {
52         .band_caps = BAND_VHF | BAND_UHF,
53         .setup     = (1 << 8) | (5 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (2 << 0),
54
55         .agc1_max = 42598,
56         .agc1_min = 17694,
57         .agc2_max = 45875,
58         .agc2_min = 0,
59
60         .agc1_pt1 = 0,
61         .agc1_pt2 = 59,
62
63         .agc1_slope1 = 0,
64         .agc1_slope2 = 69,
65
66         .agc2_pt1 = 0,
67         .agc2_pt2 = 59,
68
69         .agc2_slope1 = 111,
70         .agc2_slope2 = 28,
71 };
72
73 static struct dib3000mc_config bristol_dib3000mc_config[2] = {
74         {       .agc          = &bristol_dib3000p_mt2060_agc_config,
75                 .max_time     = 0x196,
76                 .ln_adc_level = 0x1cc7,
77                 .output_mpeg2_in_188_bytes = 1,
78         },
79         {       .agc          = &bristol_dib3000p_mt2060_agc_config,
80                 .max_time     = 0x196,
81                 .ln_adc_level = 0x1cc7,
82                 .output_mpeg2_in_188_bytes = 1,
83         }
84 };
85
86 static int bristol_frontend_attach(struct dvb_usb_adapter *adap)
87 {
88         struct dib0700_state *st = adap->dev->priv;
89         if (adap->id == 0) {
90                 dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 0); msleep(10);
91                 dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 1); msleep(10);
92                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0); msleep(10);
93                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1); msleep(10);
94
95                 if (force_lna_activation)
96                         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
97                 else
98                         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 0);
99
100                 if (dib3000mc_i2c_enumeration(&adap->dev->i2c_adap, 2, DEFAULT_DIB3000P_I2C_ADDRESS, bristol_dib3000mc_config) != 0) {
101                         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0); msleep(10);
102                         return -ENODEV;
103                 }
104         }
105         st->mt2060_if1[adap->id] = 1220;
106         return (adap->fe_adap[0].fe = dvb_attach(dib3000mc_attach, &adap->dev->i2c_adap,
107                 (10 + adap->id) << 1, &bristol_dib3000mc_config[adap->id])) == NULL ? -ENODEV : 0;
108 }
109
110 static int eeprom_read(struct i2c_adapter *adap,u8 adrs,u8 *pval)
111 {
112         struct i2c_msg msg[2] = {
113                 { .addr = 0x50, .flags = 0,        .buf = &adrs, .len = 1 },
114                 { .addr = 0x50, .flags = I2C_M_RD, .buf = pval,  .len = 1 },
115         };
116         if (i2c_transfer(adap, msg, 2) != 2) return -EREMOTEIO;
117         return 0;
118 }
119
120 static int bristol_tuner_attach(struct dvb_usb_adapter *adap)
121 {
122         struct i2c_adapter *prim_i2c = &adap->dev->i2c_adap;
123         struct i2c_adapter *tun_i2c = dib3000mc_get_tuner_i2c_master(adap->fe_adap[0].fe, 1);
124         s8 a;
125         int if1=1220;
126         if (adap->dev->udev->descriptor.idVendor  == cpu_to_le16(USB_VID_HAUPPAUGE) &&
127                 adap->dev->udev->descriptor.idProduct == cpu_to_le16(USB_PID_HAUPPAUGE_NOVA_T_500_2)) {
128                 if (!eeprom_read(prim_i2c,0x59 + adap->id,&a)) if1=1220+a;
129         }
130         return dvb_attach(mt2060_attach, adap->fe_adap[0].fe, tun_i2c,
131                           &bristol_mt2060_config[adap->id], if1) == NULL ?
132                           -ENODEV : 0;
133 }
134
135 /* STK7700D: Pinnacle/Terratec/Hauppauge Dual DVB-T Diversity */
136
137 /* MT226x */
138 static struct dibx000_agc_config stk7700d_7000p_mt2266_agc_config[2] = {
139         {
140                 BAND_UHF,
141
142                 /* 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,
143                 * 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 */
144                 (0 << 15) | (0 << 14) | (1 << 11) | (1 << 10) | (1 << 9) | (0 << 8)
145             | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
146
147                 1130,
148                 21,
149
150                 0,
151                 118,
152
153                 0,
154                 3530,
155                 1,
156                 0,
157
158                 65535,
159                 33770,
160                 65535,
161                 23592,
162
163                 0,
164                 62,
165                 255,
166                 64,
167                 64,
168                 132,
169                 192,
170                 80,
171                 80,
172
173                 17,
174                 27,
175                 23,
176                 51,
177
178                 1,
179         }, {
180                 BAND_VHF | BAND_LBAND,
181
182                 /* 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,
183                 * 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 */
184                 (0 << 15) | (0 << 14) | (1 << 11) | (1 << 10) | (1 << 9) | (0 << 8)
185             | (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0),
186
187                 2372,
188                 21,
189
190                 0,
191                 118,
192
193                 0,
194                 3530,
195                 1,
196                 0,
197
198                 65535,
199                 0,
200                 65535,
201                 23592,
202
203                 0,
204                 128,
205                 128,
206                 128,
207                 0,
208                 128,
209                 253,
210                 81,
211                 0,
212
213                 17,
214                 27,
215                 23,
216                 51,
217
218                 1,
219         }
220 };
221
222 static struct dibx000_bandwidth_config stk7700d_mt2266_pll_config = {
223         .internal = 60000,
224         .sampling = 30000,
225         .pll_prediv = 1,
226         .pll_ratio = 8,
227         .pll_range = 3,
228         .pll_reset = 1,
229         .pll_bypass = 0,
230         .enable_refdiv = 0,
231         .bypclk_div = 0,
232         .IO_CLK_en_core = 1,
233         .ADClkSrc = 1,
234         .modulo = 2,
235         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
236         .ifreq = 0,
237         .timf = 20452225,
238 };
239
240 static struct dib7000p_config stk7700d_dib7000p_mt2266_config[] = {
241         {       .output_mpeg2_in_188_bytes = 1,
242                 .hostbus_diversity = 1,
243                 .tuner_is_baseband = 1,
244
245                 .agc_config_count = 2,
246                 .agc = stk7700d_7000p_mt2266_agc_config,
247                 .bw  = &stk7700d_mt2266_pll_config,
248
249                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
250                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
251                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
252         },
253         {       .output_mpeg2_in_188_bytes = 1,
254                 .hostbus_diversity = 1,
255                 .tuner_is_baseband = 1,
256
257                 .agc_config_count = 2,
258                 .agc = stk7700d_7000p_mt2266_agc_config,
259                 .bw  = &stk7700d_mt2266_pll_config,
260
261                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
262                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
263                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
264         }
265 };
266
267 static struct mt2266_config stk7700d_mt2266_config[2] = {
268         {       .i2c_address = 0x60
269         },
270         {       .i2c_address = 0x60
271         }
272 };
273
274 static int stk7700P2_frontend_attach(struct dvb_usb_adapter *adap)
275 {
276         struct dib0700_adapter_state *state = adap->priv;
277
278         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
279                 return -ENODEV;
280
281         if (adap->id == 0) {
282                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
283                 msleep(10);
284                 dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
285                 dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
286                 dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
287                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
288                 msleep(10);
289                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
290                 msleep(10);
291                 if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
292                                              stk7700d_dib7000p_mt2266_config)
293                     != 0) {
294                         err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
295                         dvb_detach(state->dib7000p_ops.set_wbd_ref);
296                         return -ENODEV;
297                 }
298         }
299
300         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap,
301                            0x80 + (adap->id << 1),
302                            &stk7700d_dib7000p_mt2266_config[adap->id]);
303
304         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
305 }
306
307 static int stk7700d_frontend_attach(struct dvb_usb_adapter *adap)
308 {
309         struct dib0700_adapter_state *state = adap->priv;
310
311         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
312                 return -ENODEV;
313
314         if (adap->id == 0) {
315                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
316                 msleep(10);
317                 dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
318                 dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
319                 dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
320                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
321                 msleep(10);
322                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
323                 msleep(10);
324                 dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
325                 if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 2, 18,
326                                              stk7700d_dib7000p_mt2266_config)
327                     != 0) {
328                         err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
329                         dvb_detach(state->dib7000p_ops.set_wbd_ref);
330                         return -ENODEV;
331                 }
332         }
333
334         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap,
335                            0x80 + (adap->id << 1),
336                            &stk7700d_dib7000p_mt2266_config[adap->id]);
337
338         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
339 }
340
341 static int stk7700d_tuner_attach(struct dvb_usb_adapter *adap)
342 {
343         struct i2c_adapter *tun_i2c;
344         struct dib0700_adapter_state *state = adap->priv;
345
346         tun_i2c = state->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
347                                             DIBX000_I2C_INTERFACE_TUNER, 1);
348         return dvb_attach(mt2266_attach, adap->fe_adap[0].fe, tun_i2c,
349                 &stk7700d_mt2266_config[adap->id]) == NULL ? -ENODEV : 0;
350 }
351
352 /* STK7700-PH: Digital/Analog Hybrid Tuner, e.h. Cinergy HT USB HE */
353 static struct dibx000_agc_config xc3028_agc_config = {
354         .band_caps = BAND_VHF | BAND_UHF,
355         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=0,
356          * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
357          * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=2, P_agc_write=0 */
358         .setup = (0 << 15) | (0 << 14) | (0 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0),
359         .inv_gain = 712,
360         .time_stabiliz = 21,
361         .alpha_level = 0,
362         .thlock = 118,
363         .wbd_inv = 0,
364         .wbd_ref = 2867,
365         .wbd_sel = 0,
366         .wbd_alpha = 2,
367         .agc1_max = 0,
368         .agc1_min = 0,
369         .agc2_max = 39718,
370         .agc2_min = 9930,
371         .agc1_pt1 = 0,
372         .agc1_pt2 = 0,
373         .agc1_pt3 = 0,
374         .agc1_slope1 = 0,
375         .agc1_slope2 = 0,
376         .agc2_pt1 = 0,
377         .agc2_pt2 = 128,
378         .agc2_slope1 = 29,
379         .agc2_slope2 = 29,
380         .alpha_mant = 17,
381         .alpha_exp = 27,
382         .beta_mant = 23,
383         .beta_exp = 51,
384         .perform_agc_softsplit = 1,
385 };
386
387 /* PLL Configuration for COFDM BW_MHz = 8.00 with external clock = 30.00 */
388 static struct dibx000_bandwidth_config xc3028_bw_config = {
389         .internal = 60000,
390         .sampling = 30000,
391         .pll_prediv = 1,
392         .pll_ratio = 8,
393         .pll_range = 3,
394         .pll_reset = 1,
395         .pll_bypass = 0,
396         .enable_refdiv = 0,
397         .bypclk_div = 0,
398         .IO_CLK_en_core = 1,
399         .ADClkSrc = 1,
400         .modulo = 0,
401         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0), /* sad_cfg: refsel, sel, freq_15k */
402         .ifreq = (1 << 25) | 5816102,  /* ifreq = 5.200000 MHz */
403         .timf = 20452225,
404         .xtal_hz = 30000000,
405 };
406
407 static struct dib7000p_config stk7700ph_dib7700_xc3028_config = {
408         .output_mpeg2_in_188_bytes = 1,
409         .tuner_is_baseband = 1,
410
411         .agc_config_count = 1,
412         .agc = &xc3028_agc_config,
413         .bw  = &xc3028_bw_config,
414
415         .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
416         .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
417         .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
418 };
419
420 static int stk7700ph_xc3028_callback(void *ptr, int component,
421                                      int command, int arg)
422 {
423         struct dvb_usb_adapter *adap = ptr;
424         struct dib0700_adapter_state *state = adap->priv;
425
426         switch (command) {
427         case XC2028_TUNER_RESET:
428                 /* Send the tuner in then out of reset */
429                 state->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 0);
430                 msleep(10);
431                 state->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
432                 break;
433         case XC2028_RESET_CLK:
434         case XC2028_I2C_FLUSH:
435                 break;
436         default:
437                 err("%s: unknown command %d, arg %d\n", __func__,
438                         command, arg);
439                 return -EINVAL;
440         }
441         return 0;
442 }
443
444 static struct xc2028_ctrl stk7700ph_xc3028_ctrl = {
445         .fname = "/*(DEBLOBBED)*/",
446         .max_len = 64,
447         .demod = XC3028_FE_DIBCOM52,
448 };
449
450 static struct xc2028_config stk7700ph_xc3028_config = {
451         .i2c_addr = 0x61,
452         .ctrl = &stk7700ph_xc3028_ctrl,
453 };
454
455 static int stk7700ph_frontend_attach(struct dvb_usb_adapter *adap)
456 {
457         struct usb_device_descriptor *desc = &adap->dev->udev->descriptor;
458         struct dib0700_adapter_state *state = adap->priv;
459
460         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
461                 return -ENODEV;
462
463         if (desc->idVendor  == cpu_to_le16(USB_VID_PINNACLE) &&
464             desc->idProduct == cpu_to_le16(USB_PID_PINNACLE_EXPRESSCARD_320CX))
465                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
466         else
467                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
468         msleep(20);
469         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
470         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
471         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
472         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
473         msleep(10);
474         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
475         msleep(20);
476         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
477         msleep(10);
478
479         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
480                                      &stk7700ph_dib7700_xc3028_config) != 0) {
481                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
482                     __func__);
483                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
484                 return -ENODEV;
485         }
486
487         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x80,
488                 &stk7700ph_dib7700_xc3028_config);
489
490         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
491 }
492
493 static int stk7700ph_tuner_attach(struct dvb_usb_adapter *adap)
494 {
495         struct i2c_adapter *tun_i2c;
496         struct dib0700_adapter_state *state = adap->priv;
497
498         tun_i2c = state->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
499                 DIBX000_I2C_INTERFACE_TUNER, 1);
500
501         stk7700ph_xc3028_config.i2c_adap = tun_i2c;
502
503         /* FIXME: generalize & move to common area */
504         adap->fe_adap[0].fe->callback = stk7700ph_xc3028_callback;
505
506         return dvb_attach(xc2028_attach, adap->fe_adap[0].fe, &stk7700ph_xc3028_config)
507                 == NULL ? -ENODEV : 0;
508 }
509
510 #define DEFAULT_RC_INTERVAL 50
511
512 static u8 rc_request[] = { REQUEST_POLL_RC, 0 };
513
514 /*
515  * This function is used only when firmware is < 1.20 version. Newer
516  * firmwares use bulk mode, with functions implemented at dib0700_core,
517  * at dib0700_rc_urb_completion()
518  */
519 static int dib0700_rc_query_old_firmware(struct dvb_usb_device *d)
520 {
521         u8 key[4];
522         enum rc_type protocol;
523         u32 scancode;
524         u8 toggle;
525         int i;
526         struct dib0700_state *st = d->priv;
527
528         if (st->fw_version >= 0x10200) {
529                 /* For 1.20 firmware , We need to keep the RC polling
530                    callback so we can reuse the input device setup in
531                    dvb-usb-remote.c.  However, the actual work is being done
532                    in the bulk URB completion handler. */
533                 return 0;
534         }
535
536         i = dib0700_ctrl_rd(d, rc_request, 2, key, 4);
537         if (i <= 0) {
538                 err("RC Query Failed");
539                 return -1;
540         }
541
542         /* losing half of KEY_0 events from Philipps rc5 remotes.. */
543         if (key[0] == 0 && key[1] == 0 && key[2] == 0 && key[3] == 0)
544                 return 0;
545
546         /* info("%d: %2X %2X %2X %2X",dvb_usb_dib0700_ir_proto,(int)key[3-2],(int)key[3-3],(int)key[3-1],(int)key[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_BIT_NEC:
552                 /* NEC protocol sends repeat code as 0 0 0 FF */
553                 if ((key[3-2] == 0x00) && (key[3-3] == 0x00) &&
554                     (key[3] == 0xff)) {
555                         rc_repeat(d->rc_dev);
556                         return 0;
557                 }
558
559                 protocol = RC_TYPE_NEC;
560                 scancode = RC_SCANCODE_NEC(key[3-2], key[3-3]);
561                 toggle = 0;
562                 break;
563
564         default:
565                 /* RC-5 protocol changes toggle bit on new keypress */
566                 protocol = RC_TYPE_RC5;
567                 scancode = RC_SCANCODE_RC5(key[3-2], key[3-3]);
568                 toggle = key[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         case BAND_VHF:
1664                         state->dib8000_ops.set_gpio(fe, 3, 0, 1);
1665                         break;
1666         case BAND_UHF:
1667                         state->dib8000_ops.set_gpio(fe, 3, 0, 0);
1668                         break;
1669         }
1670
1671         ret = state->set_param_save(fe);
1672         if (ret < 0)
1673                 return ret;
1674
1675         if (fe->dtv_property_cache.bandwidth_hz != 6000000) {
1676                 deb_info("only 6MHz bandwidth is supported\n");
1677                 return -EINVAL;
1678         }
1679
1680         /** Update PLL if needed ratio **/
1681         state->dib8000_ops.update_pll(fe, &dib8090_pll_config_12mhz, fe->dtv_property_cache.bandwidth_hz / 1000, 0);
1682
1683         /** Get optimize PLL ratio to remove spurious **/
1684         pll_ratio = dib8090_compute_pll_parameters(fe);
1685         if (pll_ratio == 17)
1686                 timf = 21387946;
1687         else if (pll_ratio == 18)
1688                 timf = 20199727;
1689         else if (pll_ratio == 19)
1690                 timf = 19136583;
1691         else
1692                 timf = 18179756;
1693
1694         /** Update ratio **/
1695         state->dib8000_ops.update_pll(fe, &dib8090_pll_config_12mhz, fe->dtv_property_cache.bandwidth_hz / 1000, pll_ratio);
1696
1697         state->dib8000_ops.ctrl_timf(fe, DEMOD_TIMF_SET, timf);
1698
1699         if (band != BAND_CBAND) {
1700                 /* dib0090_get_wbd_target is returning any possible temperature compensated wbd-target */
1701                 target = (dib0090_get_wbd_target(fe) * 8 * 18 / 33 + 1) / 2;
1702                 state->dib8000_ops.set_wbd_ref(fe, target);
1703         }
1704
1705         if (band == BAND_CBAND) {
1706                 deb_info("tuning in CBAND - soft-AGC startup\n");
1707                 dib0090_set_tune_state(fe, CT_AGC_START);
1708
1709                 do {
1710                         ret = dib0090_gain_control(fe);
1711                         msleep(ret);
1712                         tune_state = dib0090_get_tune_state(fe);
1713                         if (tune_state == CT_AGC_STEP_0)
1714                                 state->dib8000_ops.set_gpio(fe, 6, 0, 1);
1715                         else if (tune_state == CT_AGC_STEP_1) {
1716                                 dib0090_get_current_gain(fe, NULL, NULL, &rf_gain_limit, &ltgain);
1717                                 if (rf_gain_limit < 2000) /* activate the external attenuator in case of very high input power */
1718                                         state->dib8000_ops.set_gpio(fe, 6, 0, 0);
1719                         }
1720                 } while (tune_state < CT_AGC_STOP);
1721
1722                 deb_info("switching to PWM AGC\n");
1723                 dib0090_pwm_gain_reset(fe);
1724                 state->dib8000_ops.pwm_agc_reset(fe);
1725                 state->dib8000_ops.set_tune_state(fe, CT_DEMOD_START);
1726         } else {
1727                 /* for everything else than CBAND we are using standard AGC */
1728                 deb_info("not tuning in CBAND - standard AGC startup\n");
1729                 dib0090_pwm_gain_reset(fe);
1730         }
1731
1732         return 0;
1733 }
1734
1735 static int dib809x_tuner_attach(struct dvb_usb_adapter *adap)
1736 {
1737         struct dib0700_adapter_state *st = adap->priv;
1738         struct i2c_adapter *tun_i2c = st->dib8000_ops.get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_TUNER, 1);
1739
1740         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &dib809x_dib0090_config) == NULL)
1741                 return -ENODEV;
1742
1743         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
1744         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib8096_set_param_override;
1745         return 0;
1746 }
1747
1748 static int stk809x_frontend_attach(struct dvb_usb_adapter *adap)
1749 {
1750         struct dib0700_adapter_state *state = adap->priv;
1751
1752         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1753                 return -ENODEV;
1754
1755         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
1756         msleep(10);
1757         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
1758         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1759         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1760
1761         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1762
1763         dib0700_ctrl_clock(adap->dev, 72, 1);
1764
1765         msleep(10);
1766         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1767         msleep(10);
1768         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1769
1770         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18, 0x80, 0);
1771
1772         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x80, &dib809x_dib8000_config[0]);
1773
1774         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
1775 }
1776
1777 static int nim8096md_tuner_attach(struct dvb_usb_adapter *adap)
1778 {
1779         struct dib0700_adapter_state *st = adap->priv;
1780         struct i2c_adapter *tun_i2c;
1781         struct dvb_frontend *fe_slave  = st->dib8000_ops.get_slave_frontend(adap->fe_adap[0].fe, 1);
1782
1783         if (fe_slave) {
1784                 tun_i2c = st->dib8000_ops.get_i2c_master(fe_slave, DIBX000_I2C_INTERFACE_TUNER, 1);
1785                 if (dvb_attach(dib0090_register, fe_slave, tun_i2c, &dib809x_dib0090_config) == NULL)
1786                         return -ENODEV;
1787                 fe_slave->dvb = adap->fe_adap[0].fe->dvb;
1788                 fe_slave->ops.tuner_ops.set_params = dib8096_set_param_override;
1789         }
1790         tun_i2c = st->dib8000_ops.get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_TUNER, 1);
1791         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &dib809x_dib0090_config) == NULL)
1792                 return -ENODEV;
1793
1794         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
1795         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib8096_set_param_override;
1796
1797         return 0;
1798 }
1799
1800 static int nim8096md_frontend_attach(struct dvb_usb_adapter *adap)
1801 {
1802         struct dvb_frontend *fe_slave;
1803         struct dib0700_adapter_state *state = adap->priv;
1804
1805         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1806                 return -ENODEV;
1807
1808         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
1809         msleep(20);
1810         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
1811         msleep(1000);
1812         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
1813         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1814         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1815
1816         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1817
1818         dib0700_ctrl_clock(adap->dev, 72, 1);
1819
1820         msleep(20);
1821         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1822         msleep(20);
1823         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1824
1825         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 2, 18, 0x80, 0);
1826
1827         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x80, &dib809x_dib8000_config[0]);
1828         if (adap->fe_adap[0].fe == NULL)
1829                 return -ENODEV;
1830
1831         /* Needed to increment refcount */
1832         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
1833                 return -ENODEV;
1834
1835         fe_slave = state->dib8000_ops.init(&adap->dev->i2c_adap, 0x82, &dib809x_dib8000_config[1]);
1836         state->dib8000_ops.set_slave_frontend(adap->fe_adap[0].fe, fe_slave);
1837
1838         return fe_slave == NULL ?  -ENODEV : 0;
1839 }
1840
1841 /* TFE8096P */
1842 static struct dibx000_agc_config dib8096p_agc_config[2] = {
1843         {
1844                 .band_caps              = BAND_UHF,
1845                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0,
1846                    P_agc_freq_pwm_div=1, P_agc_inv_pwm1=0,
1847                    P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1848                    P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5,
1849                    P_agc_write=0 */
1850                 .setup                  = (0 << 15) | (0 << 14) | (5 << 11)
1851                         | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5)
1852                         | (0 << 4) | (5 << 1) | (0 << 0),
1853
1854                 .inv_gain               = 684,
1855                 .time_stabiliz  = 10,
1856
1857                 .alpha_level    = 0,
1858                 .thlock                 = 118,
1859
1860                 .wbd_inv                = 0,
1861                 .wbd_ref                = 1200,
1862                 .wbd_sel                = 3,
1863                 .wbd_alpha              = 5,
1864
1865                 .agc1_max               = 65535,
1866                 .agc1_min               = 0,
1867
1868                 .agc2_max               = 32767,
1869                 .agc2_min               = 0,
1870
1871                 .agc1_pt1               = 0,
1872                 .agc1_pt2               = 0,
1873                 .agc1_pt3               = 105,
1874                 .agc1_slope1    = 0,
1875                 .agc1_slope2    = 156,
1876                 .agc2_pt1               = 105,
1877                 .agc2_pt2               = 255,
1878                 .agc2_slope1    = 54,
1879                 .agc2_slope2    = 0,
1880
1881                 .alpha_mant             = 28,
1882                 .alpha_exp              = 26,
1883                 .beta_mant              = 31,
1884                 .beta_exp               = 51,
1885
1886                 .perform_agc_softsplit = 0,
1887         } , {
1888                 .band_caps              = BAND_FM | BAND_VHF | BAND_CBAND,
1889                 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0,
1890                    P_agc_freq_pwm_div=1, P_agc_inv_pwm1=0,
1891                    P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1892                    P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5,
1893                    P_agc_write=0 */
1894                 .setup                  = (0 << 15) | (0 << 14) | (5 << 11)
1895                         | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5)
1896                         | (0 << 4) | (5 << 1) | (0 << 0),
1897
1898                 .inv_gain               = 732,
1899                 .time_stabiliz  = 10,
1900
1901                 .alpha_level    = 0,
1902                 .thlock                 = 118,
1903
1904                 .wbd_inv                = 0,
1905                 .wbd_ref                = 1200,
1906                 .wbd_sel                = 3,
1907                 .wbd_alpha              = 5,
1908
1909                 .agc1_max               = 65535,
1910                 .agc1_min               = 0,
1911
1912                 .agc2_max               = 32767,
1913                 .agc2_min               = 0,
1914
1915                 .agc1_pt1               = 0,
1916                 .agc1_pt2               = 0,
1917                 .agc1_pt3               = 98,
1918                 .agc1_slope1    = 0,
1919                 .agc1_slope2    = 167,
1920                 .agc2_pt1               = 98,
1921                 .agc2_pt2               = 255,
1922                 .agc2_slope1    = 52,
1923                 .agc2_slope2    = 0,
1924
1925                 .alpha_mant             = 28,
1926                 .alpha_exp              = 26,
1927                 .beta_mant              = 31,
1928                 .beta_exp               = 51,
1929
1930                 .perform_agc_softsplit = 0,
1931         }
1932 };
1933
1934 static struct dibx000_bandwidth_config dib8096p_clock_config_12_mhz = {
1935         .internal = 108000,
1936         .sampling = 13500,
1937         .pll_prediv = 1,
1938         .pll_ratio = 9,
1939         .pll_range = 1,
1940         .pll_reset = 0,
1941         .pll_bypass = 0,
1942         .enable_refdiv = 0,
1943         .bypclk_div = 0,
1944         .IO_CLK_en_core = 0,
1945         .ADClkSrc = 0,
1946         .modulo = 2,
1947         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
1948         .ifreq = (0 << 25) | 0,
1949         .timf = 20199729,
1950         .xtal_hz = 12000000,
1951 };
1952
1953 static struct dib8000_config tfe8096p_dib8000_config = {
1954         .output_mpeg2_in_188_bytes      = 1,
1955         .hostbus_diversity                      = 1,
1956         .update_lna                                     = NULL,
1957
1958         .agc_config_count                       = 2,
1959         .agc                                            = dib8096p_agc_config,
1960         .pll                                            = &dib8096p_clock_config_12_mhz,
1961
1962         .gpio_dir                                       = DIB8000_GPIO_DEFAULT_DIRECTIONS,
1963         .gpio_val                                       = DIB8000_GPIO_DEFAULT_VALUES,
1964         .gpio_pwm_pos                           = DIB8000_GPIO_DEFAULT_PWM_POS,
1965
1966         .agc_control                            = NULL,
1967         .diversity_delay                        = 48,
1968         .output_mode                            = OUTMODE_MPEG2_FIFO,
1969         .enMpegOutput                           = 1,
1970 };
1971
1972 static struct dib0090_wbd_slope dib8096p_wbd_table[] = {
1973         { 380, 81, 850, 64, 540, 4},
1974         { 860, 51, 866, 21, 375, 4},
1975         {1700, 0, 250, 0, 100, 6},
1976         {2600, 0, 250, 0, 100, 6},
1977         { 0xFFFF, 0, 0, 0, 0, 0},
1978 };
1979
1980 static struct dib0090_config tfe8096p_dib0090_config = {
1981         .io.clock_khz                   = 12000,
1982         .io.pll_bypass                  = 0,
1983         .io.pll_range                   = 0,
1984         .io.pll_prediv                  = 3,
1985         .io.pll_loopdiv                 = 6,
1986         .io.adc_clock_ratio             = 0,
1987         .io.pll_int_loop_filt   = 0,
1988
1989         .freq_offset_khz_uhf    = -143,
1990         .freq_offset_khz_vhf    = -143,
1991
1992         .get_adc_power                  = dib8090_get_adc_power,
1993
1994         .clkouttobamse                  = 1,
1995         .analog_output                  = 0,
1996
1997         .wbd_vhf_offset                 = 0,
1998         .wbd_cband_offset               = 0,
1999         .use_pwm_agc                    = 1,
2000         .clkoutdrive                    = 0,
2001
2002         .fref_clock_ratio               = 1,
2003
2004         .ls_cfg_pad_drv                 = 0,
2005         .data_tx_drv                    = 0,
2006         .low_if                                 = NULL,
2007         .in_soc                                 = 1,
2008         .force_cband_input              = 0,
2009 };
2010
2011 struct dibx090p_adc {
2012         u32 freq;                       /* RF freq MHz */
2013         u32 timf;                       /* New Timf */
2014         u32 pll_loopdiv;        /* New prediv */
2015         u32 pll_prediv;         /* New loopdiv */
2016 };
2017
2018 struct dibx090p_best_adc {
2019         u32 timf;
2020         u32 pll_loopdiv;
2021         u32 pll_prediv;
2022 };
2023
2024 static int dib8096p_get_best_sampling(struct dvb_frontend *fe, struct dibx090p_best_adc *adc)
2025 {
2026         u8 spur = 0, prediv = 0, loopdiv = 0, min_prediv = 1, max_prediv = 1;
2027         u16 xtal = 12000;
2028         u16 fcp_min = 1900;  /* PLL, Minimum Frequency of phase comparator (KHz) */
2029         u16 fcp_max = 20000; /* PLL, Maximum Frequency of phase comparator (KHz) */
2030         u32 fmem_max = 140000; /* 140MHz max SDRAM freq */
2031         u32 fdem_min = 66000;
2032         u32 fcp = 0, fs = 0, fdem = 0, fmem = 0;
2033         u32 harmonic_id = 0;
2034
2035         adc->timf = 0;
2036         adc->pll_loopdiv = loopdiv;
2037         adc->pll_prediv = prediv;
2038
2039         deb_info("bandwidth = %d", fe->dtv_property_cache.bandwidth_hz);
2040
2041         /* Find Min and Max prediv */
2042         while ((xtal / max_prediv) >= fcp_min)
2043                 max_prediv++;
2044
2045         max_prediv--;
2046         min_prediv = max_prediv;
2047         while ((xtal / min_prediv) <= fcp_max) {
2048                 min_prediv--;
2049                 if (min_prediv == 1)
2050                         break;
2051         }
2052         deb_info("MIN prediv = %d : MAX prediv = %d", min_prediv, max_prediv);
2053
2054         min_prediv = 1;
2055
2056         for (prediv = min_prediv; prediv < max_prediv; prediv++) {
2057                 fcp = xtal / prediv;
2058                 if (fcp > fcp_min && fcp < fcp_max) {
2059                         for (loopdiv = 1; loopdiv < 64; loopdiv++) {
2060                                 fmem = ((xtal/prediv) * loopdiv);
2061                                 fdem = fmem / 2;
2062                                 fs   = fdem / 4;
2063
2064                                 /* test min/max system restrictions */
2065                                 if ((fdem >= fdem_min) && (fmem <= fmem_max) && (fs >= fe->dtv_property_cache.bandwidth_hz / 1000)) {
2066                                         spur = 0;
2067                                         /* test fs harmonics positions */
2068                                         for (harmonic_id = (fe->dtv_property_cache.frequency / (1000 * fs));  harmonic_id <= ((fe->dtv_property_cache.frequency / (1000 * fs)) + 1); harmonic_id++) {
2069                                                 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)))) {
2070                                                         spur = 1;
2071                                                         break;
2072                                                 }
2073                                         }
2074
2075                                         if (!spur) {
2076                                                 adc->pll_loopdiv = loopdiv;
2077                                                 adc->pll_prediv = prediv;
2078                                                 adc->timf = (4260880253U / fdem) * (1 << 8);
2079                                                 adc->timf += ((4260880253U % fdem) << 8) / fdem;
2080
2081                                                 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);
2082                                                 break;
2083                                         }
2084                                 }
2085                         }
2086                 }
2087                 if (!spur)
2088                         break;
2089         }
2090
2091         if (adc->pll_loopdiv == 0 && adc->pll_prediv == 0)
2092                 return -EINVAL;
2093         return 0;
2094 }
2095
2096 static int dib8096p_agc_startup(struct dvb_frontend *fe)
2097 {
2098         struct dvb_usb_adapter *adap = fe->dvb->priv;
2099         struct dib0700_adapter_state *state = adap->priv;
2100         struct dibx000_bandwidth_config pll;
2101         struct dibx090p_best_adc adc;
2102         u16 target;
2103         int ret;
2104
2105         ret = state->set_param_save(fe);
2106         if (ret < 0)
2107                 return ret;
2108         memset(&pll, 0, sizeof(struct dibx000_bandwidth_config));
2109
2110         dib0090_pwm_gain_reset(fe);
2111         /* dib0090_get_wbd_target is returning any possible
2112            temperature compensated wbd-target */
2113         target = (dib0090_get_wbd_target(fe) * 8  + 1) / 2;
2114         state->dib8000_ops.set_wbd_ref(fe, target);
2115
2116         if (dib8096p_get_best_sampling(fe, &adc) == 0) {
2117                 pll.pll_ratio  = adc.pll_loopdiv;
2118                 pll.pll_prediv = adc.pll_prediv;
2119
2120                 dib0700_set_i2c_speed(adap->dev, 200);
2121                 state->dib8000_ops.update_pll(fe, &pll, fe->dtv_property_cache.bandwidth_hz / 1000, 0);
2122                 state->dib8000_ops.ctrl_timf(fe, DEMOD_TIMF_SET, adc.timf);
2123                 dib0700_set_i2c_speed(adap->dev, 1000);
2124         }
2125         return 0;
2126 }
2127
2128 static int tfe8096p_frontend_attach(struct dvb_usb_adapter *adap)
2129 {
2130         struct dib0700_state *st = adap->dev->priv;
2131         u32 fw_version;
2132         struct dib0700_adapter_state *state = adap->priv;
2133
2134         if (!dvb_attach(dib8000_attach, &state->dib8000_ops))
2135                 return -ENODEV;
2136
2137         dib0700_get_version(adap->dev, NULL, NULL, &fw_version, NULL);
2138         if (fw_version >= 0x10200)
2139                 st->fw_use_new_i2c_api = 1;
2140
2141         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
2142         msleep(20);
2143         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
2144         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
2145         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
2146
2147         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
2148
2149         dib0700_ctrl_clock(adap->dev, 72, 1);
2150
2151         msleep(20);
2152         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
2153         msleep(20);
2154         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
2155
2156         state->dib8000_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 0x10, 0x80, 1);
2157
2158         adap->fe_adap[0].fe = state->dib8000_ops.init(&adap->dev->i2c_adap,
2159                                              0x80, &tfe8096p_dib8000_config);
2160
2161         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
2162 }
2163
2164 static int tfe8096p_tuner_attach(struct dvb_usb_adapter *adap)
2165 {
2166         struct dib0700_adapter_state *st = adap->priv;
2167         struct i2c_adapter *tun_i2c = st->dib8000_ops.get_i2c_tuner(adap->fe_adap[0].fe);
2168
2169         tfe8096p_dib0090_config.reset = st->dib8000_ops.tuner_sleep;
2170         tfe8096p_dib0090_config.sleep = st->dib8000_ops.tuner_sleep;
2171         tfe8096p_dib0090_config.wbd = dib8096p_wbd_table;
2172
2173         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c,
2174                                 &tfe8096p_dib0090_config) == NULL)
2175                 return -ENODEV;
2176
2177         st->dib8000_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
2178
2179         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
2180         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib8096p_agc_startup;
2181         return 0;
2182 }
2183
2184 /* STK9090M */
2185 static int dib90x0_pid_filter(struct dvb_usb_adapter *adapter, int index, u16 pid, int onoff)
2186 {
2187         return dib9000_fw_pid_filter(adapter->fe_adap[0].fe, index, pid, onoff);
2188 }
2189
2190 static int dib90x0_pid_filter_ctrl(struct dvb_usb_adapter *adapter, int onoff)
2191 {
2192         return dib9000_fw_pid_filter_ctrl(adapter->fe_adap[0].fe, onoff);
2193 }
2194
2195 static int dib90x0_tuner_reset(struct dvb_frontend *fe, int onoff)
2196 {
2197         return dib9000_set_gpio(fe, 5, 0, !onoff);
2198 }
2199
2200 static int dib90x0_tuner_sleep(struct dvb_frontend *fe, int onoff)
2201 {
2202         return dib9000_set_gpio(fe, 0, 0, onoff);
2203 }
2204
2205 static int dib01x0_pmu_update(struct i2c_adapter *i2c, u16 *data, u8 len)
2206 {
2207         u8 wb[4] = { 0xc >> 8, 0xc & 0xff, 0, 0 };
2208         u8 rb[2];
2209         struct i2c_msg msg[2] = {
2210                 {.addr = 0x1e >> 1, .flags = 0, .buf = wb, .len = 2},
2211                 {.addr = 0x1e >> 1, .flags = I2C_M_RD, .buf = rb, .len = 2},
2212         };
2213         u8 index_data;
2214
2215         dibx000_i2c_set_speed(i2c, 250);
2216
2217         if (i2c_transfer(i2c, msg, 2) != 2)
2218                 return -EIO;
2219
2220         switch (rb[0] << 8 | rb[1]) {
2221         case 0:
2222                         deb_info("Found DiB0170 rev1: This version of DiB0170 is not supported any longer.\n");
2223                         return -EIO;
2224         case 1:
2225                         deb_info("Found DiB0170 rev2");
2226                         break;
2227         case 2:
2228                         deb_info("Found DiB0190 rev2");
2229                         break;
2230         default:
2231                         deb_info("DiB01x0 not found");
2232                         return -EIO;
2233         }
2234
2235         for (index_data = 0; index_data < len; index_data += 2) {
2236                 wb[2] = (data[index_data + 1] >> 8) & 0xff;
2237                 wb[3] = (data[index_data + 1]) & 0xff;
2238
2239                 if (data[index_data] == 0) {
2240                         wb[0] = (data[index_data] >> 8) & 0xff;
2241                         wb[1] = (data[index_data]) & 0xff;
2242                         msg[0].len = 2;
2243                         if (i2c_transfer(i2c, msg, 2) != 2)
2244                                 return -EIO;
2245                         wb[2] |= rb[0];
2246                         wb[3] |= rb[1] & ~(3 << 4);
2247                 }
2248
2249                 wb[0] = (data[index_data] >> 8)&0xff;
2250                 wb[1] = (data[index_data])&0xff;
2251                 msg[0].len = 4;
2252                 if (i2c_transfer(i2c, &msg[0], 1) != 1)
2253                         return -EIO;
2254         }
2255         return 0;
2256 }
2257
2258 static struct dib9000_config stk9090m_config = {
2259         .output_mpeg2_in_188_bytes = 1,
2260         .output_mode = OUTMODE_MPEG2_FIFO,
2261         .vcxo_timer = 279620,
2262         .timing_frequency = 20452225,
2263         .demod_clock_khz = 60000,
2264         .xtal_clock_khz = 30000,
2265         .if_drives = (0 << 15) | (1 << 13) | (0 << 12) | (3 << 10) | (0 << 9) | (1 << 7) | (0 << 6) | (0 << 4) | (1 << 3) | (1 << 1) | (0),
2266         .subband = {
2267                 2,
2268                 {
2269                         { 240, { BOARD_GPIO_COMPONENT_DEMOD, BOARD_GPIO_FUNCTION_SUBBAND_GPIO, 0x0008, 0x0000, 0x0008 } }, /* GPIO 3 to 1 for VHF */
2270                         { 890, { BOARD_GPIO_COMPONENT_DEMOD, BOARD_GPIO_FUNCTION_SUBBAND_GPIO, 0x0008, 0x0000, 0x0000 } }, /* GPIO 3 to 0 for UHF */
2271                         { 0 },
2272                 },
2273         },
2274         .gpio_function = {
2275                 { .component = BOARD_GPIO_COMPONENT_DEMOD, .function = BOARD_GPIO_FUNCTION_COMPONENT_ON, .mask = 0x10 | 0x21, .direction = 0 & ~0x21, .value = (0x10 & ~0x1) | 0x20 },
2276                 { .component = BOARD_GPIO_COMPONENT_DEMOD, .function = BOARD_GPIO_FUNCTION_COMPONENT_OFF, .mask = 0x10 | 0x21, .direction = 0 & ~0x21, .value = 0 | 0x21 },
2277         },
2278 };
2279
2280 static struct dib9000_config nim9090md_config[2] = {
2281         {
2282                 .output_mpeg2_in_188_bytes = 1,
2283                 .output_mode = OUTMODE_MPEG2_FIFO,
2284                 .vcxo_timer = 279620,
2285                 .timing_frequency = 20452225,
2286                 .demod_clock_khz = 60000,
2287                 .xtal_clock_khz = 30000,
2288                 .if_drives = (0 << 15) | (1 << 13) | (0 << 12) | (3 << 10) | (0 << 9) | (1 << 7) | (0 << 6) | (0 << 4) | (1 << 3) | (1 << 1) | (0),
2289         }, {
2290                 .output_mpeg2_in_188_bytes = 1,
2291                 .output_mode = OUTMODE_DIVERSITY,
2292                 .vcxo_timer = 279620,
2293                 .timing_frequency = 20452225,
2294                 .demod_clock_khz = 60000,
2295                 .xtal_clock_khz = 30000,
2296                 .if_drives = (0 << 15) | (1 << 13) | (0 << 12) | (3 << 10) | (0 << 9) | (1 << 7) | (0 << 6) | (0 << 4) | (1 << 3) | (1 << 1) | (0),
2297                 .subband = {
2298                         2,
2299                         {
2300                                 { 240, { BOARD_GPIO_COMPONENT_DEMOD, BOARD_GPIO_FUNCTION_SUBBAND_GPIO, 0x0006, 0x0000, 0x0006 } }, /* GPIO 1 and 2 to 1 for VHF */
2301                                 { 890, { BOARD_GPIO_COMPONENT_DEMOD, BOARD_GPIO_FUNCTION_SUBBAND_GPIO, 0x0006, 0x0000, 0x0000 } }, /* GPIO 1 and 2 to 0 for UHF */
2302                                 { 0 },
2303                         },
2304                 },
2305                 .gpio_function = {
2306                         { .component = BOARD_GPIO_COMPONENT_DEMOD, .function = BOARD_GPIO_FUNCTION_COMPONENT_ON, .mask = 0x10 | 0x21, .direction = 0 & ~0x21, .value = (0x10 & ~0x1) | 0x20 },
2307                         { .component = BOARD_GPIO_COMPONENT_DEMOD, .function = BOARD_GPIO_FUNCTION_COMPONENT_OFF, .mask = 0x10 | 0x21, .direction = 0 & ~0x21, .value = 0 | 0x21 },
2308                 },
2309         }
2310 };
2311
2312 static struct dib0090_config dib9090_dib0090_config = {
2313         .io.pll_bypass = 0,
2314         .io.pll_range = 1,
2315         .io.pll_prediv = 1,
2316         .io.pll_loopdiv = 8,
2317         .io.adc_clock_ratio = 8,
2318         .io.pll_int_loop_filt = 0,
2319         .io.clock_khz = 30000,
2320         .reset = dib90x0_tuner_reset,
2321         .sleep = dib90x0_tuner_sleep,
2322         .clkouttobamse = 0,
2323         .analog_output = 0,
2324         .use_pwm_agc = 0,
2325         .clkoutdrive = 0,
2326         .freq_offset_khz_uhf = 0,
2327         .freq_offset_khz_vhf = 0,
2328 };
2329
2330 static struct dib0090_config nim9090md_dib0090_config[2] = {
2331         {
2332                 .io.pll_bypass = 0,
2333                 .io.pll_range = 1,
2334                 .io.pll_prediv = 1,
2335                 .io.pll_loopdiv = 8,
2336                 .io.adc_clock_ratio = 8,
2337                 .io.pll_int_loop_filt = 0,
2338                 .io.clock_khz = 30000,
2339                 .reset = dib90x0_tuner_reset,
2340                 .sleep = dib90x0_tuner_sleep,
2341                 .clkouttobamse = 1,
2342                 .analog_output = 0,
2343                 .use_pwm_agc = 0,
2344                 .clkoutdrive = 0,
2345                 .freq_offset_khz_uhf = 0,
2346                 .freq_offset_khz_vhf = 0,
2347         }, {
2348                 .io.pll_bypass = 0,
2349                 .io.pll_range = 1,
2350                 .io.pll_prediv = 1,
2351                 .io.pll_loopdiv = 8,
2352                 .io.adc_clock_ratio = 8,
2353                 .io.pll_int_loop_filt = 0,
2354                 .io.clock_khz = 30000,
2355                 .reset = dib90x0_tuner_reset,
2356                 .sleep = dib90x0_tuner_sleep,
2357                 .clkouttobamse = 0,
2358                 .analog_output = 0,
2359                 .use_pwm_agc = 0,
2360                 .clkoutdrive = 0,
2361                 .freq_offset_khz_uhf = 0,
2362                 .freq_offset_khz_vhf = 0,
2363         }
2364 };
2365
2366
2367 static int stk9090m_frontend_attach(struct dvb_usb_adapter *adap)
2368 {
2369         struct dib0700_adapter_state *state = adap->priv;
2370         struct dib0700_state *st = adap->dev->priv;
2371         u32 fw_version;
2372
2373         /* Make use of the new i2c functions from FW 1.20 */
2374         dib0700_get_version(adap->dev, NULL, NULL, &fw_version, NULL);
2375         if (fw_version >= 0x10200)
2376                 st->fw_use_new_i2c_api = 1;
2377         dib0700_set_i2c_speed(adap->dev, 340);
2378
2379         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
2380         msleep(20);
2381         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
2382         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
2383         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
2384         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
2385
2386         dib0700_ctrl_clock(adap->dev, 72, 1);
2387
2388         msleep(20);
2389         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
2390         msleep(20);
2391         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
2392
2393         dib9000_i2c_enumeration(&adap->dev->i2c_adap, 1, 0x10, 0x80);
2394
2395         if (reject_firmware(&state->frontend_firmware, "/*(DEBLOBBED)*/", &adap->dev->udev->dev)) {
2396                 deb_info("%s: Upload failed. (file not found?)\n", __func__);
2397                 return -ENODEV;
2398         } else {
2399                 deb_info("%s: firmware read %Zu bytes.\n", __func__, state->frontend_firmware->size);
2400         }
2401         stk9090m_config.microcode_B_fe_size = state->frontend_firmware->size;
2402         stk9090m_config.microcode_B_fe_buffer = state->frontend_firmware->data;
2403
2404         adap->fe_adap[0].fe = dvb_attach(dib9000_attach, &adap->dev->i2c_adap, 0x80, &stk9090m_config);
2405
2406         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
2407 }
2408
2409 static int dib9090_tuner_attach(struct dvb_usb_adapter *adap)
2410 {
2411         struct dib0700_adapter_state *state = adap->priv;
2412         struct i2c_adapter *i2c = dib9000_get_tuner_interface(adap->fe_adap[0].fe);
2413         u16 data_dib190[10] = {
2414                 1, 0x1374,
2415                 2, 0x01a2,
2416                 7, 0x0020,
2417                 0, 0x00ef,
2418                 8, 0x0486,
2419         };
2420
2421         if (!IS_ENABLED(CONFIG_DVB_DIB9000))
2422                 return -ENODEV;
2423         if (dvb_attach(dib0090_fw_register, adap->fe_adap[0].fe, i2c, &dib9090_dib0090_config) == NULL)
2424                 return -ENODEV;
2425         i2c = dib9000_get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_GPIO_1_2, 0);
2426         if (!i2c)
2427                 return -ENODEV;
2428         if (dib01x0_pmu_update(i2c, data_dib190, 10) != 0)
2429                 return -ENODEV;
2430         dib0700_set_i2c_speed(adap->dev, 1500);
2431         if (dib9000_firmware_post_pll_init(adap->fe_adap[0].fe) < 0)
2432                 return -ENODEV;
2433         release_firmware(state->frontend_firmware);
2434         return 0;
2435 }
2436
2437 static int nim9090md_frontend_attach(struct dvb_usb_adapter *adap)
2438 {
2439         struct dib0700_adapter_state *state = adap->priv;
2440         struct dib0700_state *st = adap->dev->priv;
2441         struct i2c_adapter *i2c;
2442         struct dvb_frontend *fe_slave;
2443         u32 fw_version;
2444
2445         /* Make use of the new i2c functions from FW 1.20 */
2446         dib0700_get_version(adap->dev, NULL, NULL, &fw_version, NULL);
2447         if (fw_version >= 0x10200)
2448                 st->fw_use_new_i2c_api = 1;
2449         dib0700_set_i2c_speed(adap->dev, 340);
2450
2451         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
2452         msleep(20);
2453         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
2454         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
2455         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
2456         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
2457
2458         dib0700_ctrl_clock(adap->dev, 72, 1);
2459
2460         msleep(20);
2461         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
2462         msleep(20);
2463         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
2464
2465         if (reject_firmware(&state->frontend_firmware, "/*(DEBLOBBED)*/", &adap->dev->udev->dev)) {
2466                 deb_info("%s: Upload failed. (file not found?)\n", __func__);
2467                 return -EIO;
2468         } else {
2469                 deb_info("%s: firmware read %Zu bytes.\n", __func__, state->frontend_firmware->size);
2470         }
2471         nim9090md_config[0].microcode_B_fe_size = state->frontend_firmware->size;
2472         nim9090md_config[0].microcode_B_fe_buffer = state->frontend_firmware->data;
2473         nim9090md_config[1].microcode_B_fe_size = state->frontend_firmware->size;
2474         nim9090md_config[1].microcode_B_fe_buffer = state->frontend_firmware->data;
2475
2476         dib9000_i2c_enumeration(&adap->dev->i2c_adap, 1, 0x20, 0x80);
2477         adap->fe_adap[0].fe = dvb_attach(dib9000_attach, &adap->dev->i2c_adap, 0x80, &nim9090md_config[0]);
2478
2479         if (adap->fe_adap[0].fe == NULL)
2480                 return -ENODEV;
2481
2482         i2c = dib9000_get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_GPIO_3_4, 0);
2483         dib9000_i2c_enumeration(i2c, 1, 0x12, 0x82);
2484
2485         fe_slave = dvb_attach(dib9000_attach, i2c, 0x82, &nim9090md_config[1]);
2486         dib9000_set_slave_frontend(adap->fe_adap[0].fe, fe_slave);
2487
2488         return fe_slave == NULL ?  -ENODEV : 0;
2489 }
2490
2491 static int nim9090md_tuner_attach(struct dvb_usb_adapter *adap)
2492 {
2493         struct dib0700_adapter_state *state = adap->priv;
2494         struct i2c_adapter *i2c;
2495         struct dvb_frontend *fe_slave;
2496         u16 data_dib190[10] = {
2497                 1, 0x5374,
2498                 2, 0x01ae,
2499                 7, 0x0020,
2500                 0, 0x00ef,
2501                 8, 0x0406,
2502         };
2503         if (!IS_ENABLED(CONFIG_DVB_DIB9000))
2504                 return -ENODEV;
2505         i2c = dib9000_get_tuner_interface(adap->fe_adap[0].fe);
2506         if (dvb_attach(dib0090_fw_register, adap->fe_adap[0].fe, i2c, &nim9090md_dib0090_config[0]) == NULL)
2507                 return -ENODEV;
2508         i2c = dib9000_get_i2c_master(adap->fe_adap[0].fe, DIBX000_I2C_INTERFACE_GPIO_1_2, 0);
2509         if (!i2c)
2510                 return -ENODEV;
2511         if (dib01x0_pmu_update(i2c, data_dib190, 10) < 0)
2512                 return -ENODEV;
2513
2514         dib0700_set_i2c_speed(adap->dev, 1500);
2515         if (dib9000_firmware_post_pll_init(adap->fe_adap[0].fe) < 0)
2516                 return -ENODEV;
2517
2518         fe_slave = dib9000_get_slave_frontend(adap->fe_adap[0].fe, 1);
2519         if (fe_slave != NULL) {
2520                 i2c = dib9000_get_component_bus_interface(adap->fe_adap[0].fe);
2521                 dib9000_set_i2c_adapter(fe_slave, i2c);
2522
2523                 i2c = dib9000_get_tuner_interface(fe_slave);
2524                 if (dvb_attach(dib0090_fw_register, fe_slave, i2c, &nim9090md_dib0090_config[1]) == NULL)
2525                         return -ENODEV;
2526                 fe_slave->dvb = adap->fe_adap[0].fe->dvb;
2527                 dib9000_fw_set_component_bus_speed(adap->fe_adap[0].fe, 1500);
2528                 if (dib9000_firmware_post_pll_init(fe_slave) < 0)
2529                         return -ENODEV;
2530         }
2531         release_firmware(state->frontend_firmware);
2532
2533         return 0;
2534 }
2535
2536 /* NIM7090 */
2537 static int dib7090p_get_best_sampling(struct dvb_frontend *fe , struct dibx090p_best_adc *adc)
2538 {
2539         u8 spur = 0, prediv = 0, loopdiv = 0, min_prediv = 1, max_prediv = 1;
2540
2541         u16 xtal = 12000;
2542         u32 fcp_min = 1900;  /* PLL Minimum Frequency comparator KHz */
2543         u32 fcp_max = 20000; /* PLL Maximum Frequency comparator KHz */
2544         u32 fdem_max = 76000;
2545         u32 fdem_min = 69500;
2546         u32 fcp = 0, fs = 0, fdem = 0;
2547         u32 harmonic_id = 0;
2548
2549         adc->pll_loopdiv = loopdiv;
2550         adc->pll_prediv = prediv;
2551         adc->timf = 0;
2552
2553         deb_info("bandwidth = %d fdem_min =%d", fe->dtv_property_cache.bandwidth_hz, fdem_min);
2554
2555         /* Find Min and Max prediv */
2556         while ((xtal/max_prediv) >= fcp_min)
2557                 max_prediv++;
2558
2559         max_prediv--;
2560         min_prediv = max_prediv;
2561         while ((xtal/min_prediv) <= fcp_max) {
2562                 min_prediv--;
2563                 if (min_prediv == 1)
2564                         break;
2565         }
2566         deb_info("MIN prediv = %d : MAX prediv = %d", min_prediv, max_prediv);
2567
2568         min_prediv = 2;
2569
2570         for (prediv = min_prediv ; prediv < max_prediv; prediv++) {
2571                 fcp = xtal / prediv;
2572                 if (fcp > fcp_min && fcp < fcp_max) {
2573                         for (loopdiv = 1 ; loopdiv < 64 ; loopdiv++) {
2574                                 fdem = ((xtal/prediv) * loopdiv);
2575                                 fs   = fdem / 4;
2576                                 /* test min/max system restrictions */
2577
2578                                 if ((fdem >= fdem_min) && (fdem <= fdem_max) && (fs >= fe->dtv_property_cache.bandwidth_hz/1000)) {
2579                                         spur = 0;
2580                                         /* test fs harmonics positions */
2581                                         for (harmonic_id = (fe->dtv_property_cache.frequency / (1000*fs)) ;  harmonic_id <= ((fe->dtv_property_cache.frequency / (1000*fs))+1) ; harmonic_id++) {
2582                                                 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)))) {
2583                                                         spur = 1;
2584                                                         break;
2585                                                 }
2586                                         }
2587
2588                                         if (!spur) {
2589                                                 adc->pll_loopdiv = loopdiv;
2590                                                 adc->pll_prediv = prediv;
2591                                                 adc->timf = 2396745143UL/fdem*(1 << 9);
2592                                                 adc->timf += ((2396745143UL%fdem) << 9)/fdem;
2593                                                 deb_info("loopdiv=%i prediv=%i timf=%i", loopdiv, prediv, adc->timf);
2594                                                 break;
2595                                         }
2596                                 }
2597                         }
2598                 }
2599                 if (!spur)
2600                         break;
2601         }
2602
2603
2604         if (adc->pll_loopdiv == 0 && adc->pll_prediv == 0)
2605                 return -EINVAL;
2606         else
2607                 return 0;
2608 }
2609
2610 static int dib7090_agc_startup(struct dvb_frontend *fe)
2611 {
2612         struct dvb_usb_adapter *adap = fe->dvb->priv;
2613         struct dib0700_adapter_state *state = adap->priv;
2614         struct dibx000_bandwidth_config pll;
2615         u16 target;
2616         struct dibx090p_best_adc adc;
2617         int ret;
2618
2619         ret = state->set_param_save(fe);
2620         if (ret < 0)
2621                 return ret;
2622
2623         memset(&pll, 0, sizeof(struct dibx000_bandwidth_config));
2624         dib0090_pwm_gain_reset(fe);
2625         target = (dib0090_get_wbd_target(fe) * 8 + 1) / 2;
2626         state->dib7000p_ops.set_wbd_ref(fe, target);
2627
2628         if (dib7090p_get_best_sampling(fe, &adc) == 0) {
2629                 pll.pll_ratio  = adc.pll_loopdiv;
2630                 pll.pll_prediv = adc.pll_prediv;
2631
2632                 state->dib7000p_ops.update_pll(fe, &pll);
2633                 state->dib7000p_ops.ctrl_timf(fe, DEMOD_TIMF_SET, adc.timf);
2634         }
2635         return 0;
2636 }
2637
2638 static int dib7090_agc_restart(struct dvb_frontend *fe, u8 restart)
2639 {
2640         deb_info("AGC restart callback: %d", restart);
2641         if (restart == 0) /* before AGC startup */
2642                 dib0090_set_dc_servo(fe, 1);
2643         return 0;
2644 }
2645
2646 static int tfe7790p_update_lna(struct dvb_frontend *fe, u16 agc_global)
2647 {
2648         struct dvb_usb_adapter *adap = fe->dvb->priv;
2649         struct dib0700_adapter_state *state = adap->priv;
2650
2651         deb_info("update LNA: agc global=%i", agc_global);
2652
2653         if (agc_global < 25000) {
2654                 state->dib7000p_ops.set_gpio(fe, 8, 0, 0);
2655                 state->dib7000p_ops.set_agc1_min(fe, 0);
2656         } else {
2657                 state->dib7000p_ops.set_gpio(fe, 8, 0, 1);
2658                 state->dib7000p_ops.set_agc1_min(fe, 32768);
2659         }
2660
2661         return 0;
2662 }
2663
2664 static struct dib0090_wbd_slope dib7090_wbd_table[] = {
2665         { 380,   81, 850, 64, 540,  4},
2666         { 860,   51, 866, 21,  375, 4},
2667         {1700,    0, 250, 0,   100, 6},
2668         {2600,    0, 250, 0,   100, 6},
2669         { 0xFFFF, 0,   0, 0,   0,   0},
2670 };
2671
2672 static struct dibx000_agc_config dib7090_agc_config[2] = {
2673         {
2674                 .band_caps      = BAND_UHF,
2675                 /* 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,
2676                 * 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 */
2677                 .setup          = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
2678
2679                 .inv_gain       = 687,
2680                 .time_stabiliz  = 10,
2681
2682                 .alpha_level    = 0,
2683                 .thlock         = 118,
2684
2685                 .wbd_inv        = 0,
2686                 .wbd_ref        = 1200,
2687                 .wbd_sel        = 3,
2688                 .wbd_alpha      = 5,
2689
2690                 .agc1_max       = 65535,
2691                 .agc1_min       = 32768,
2692
2693                 .agc2_max       = 65535,
2694                 .agc2_min       = 0,
2695
2696                 .agc1_pt1       = 0,
2697                 .agc1_pt2       = 32,
2698                 .agc1_pt3       = 114,
2699                 .agc1_slope1    = 143,
2700                 .agc1_slope2    = 144,
2701                 .agc2_pt1       = 114,
2702                 .agc2_pt2       = 227,
2703                 .agc2_slope1    = 116,
2704                 .agc2_slope2    = 117,
2705
2706                 .alpha_mant     = 18,
2707                 .alpha_exp      = 0,
2708                 .beta_mant      = 20,
2709                 .beta_exp       = 59,
2710
2711                 .perform_agc_softsplit = 0,
2712         } , {
2713                 .band_caps      = BAND_FM | BAND_VHF | BAND_CBAND,
2714                 /* 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,
2715                 * 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 */
2716                 .setup          = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
2717
2718                 .inv_gain       = 732,
2719                 .time_stabiliz  = 10,
2720
2721                 .alpha_level    = 0,
2722                 .thlock         = 118,
2723
2724                 .wbd_inv        = 0,
2725                 .wbd_ref        = 1200,
2726                 .wbd_sel        = 3,
2727                 .wbd_alpha      = 5,
2728
2729                 .agc1_max       = 65535,
2730                 .agc1_min       = 0,
2731
2732                 .agc2_max       = 65535,
2733                 .agc2_min       = 0,
2734
2735                 .agc1_pt1       = 0,
2736                 .agc1_pt2       = 0,
2737                 .agc1_pt3       = 98,
2738                 .agc1_slope1    = 0,
2739                 .agc1_slope2    = 167,
2740                 .agc2_pt1       = 98,
2741                 .agc2_pt2       = 255,
2742                 .agc2_slope1    = 104,
2743                 .agc2_slope2    = 0,
2744
2745                 .alpha_mant     = 18,
2746                 .alpha_exp      = 0,
2747                 .beta_mant      = 20,
2748                 .beta_exp       = 59,
2749
2750                 .perform_agc_softsplit = 0,
2751         }
2752 };
2753
2754 static struct dibx000_bandwidth_config dib7090_clock_config_12_mhz = {
2755         .internal = 60000,
2756         .sampling = 15000,
2757         .pll_prediv = 1,
2758         .pll_ratio = 5,
2759         .pll_range = 0,
2760         .pll_reset = 0,
2761         .pll_bypass = 0,
2762         .enable_refdiv = 0,
2763         .bypclk_div = 0,
2764         .IO_CLK_en_core = 1,
2765         .ADClkSrc = 1,
2766         .modulo = 2,
2767         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
2768         .ifreq = (0 << 25) | 0,
2769         .timf = 20452225,
2770         .xtal_hz = 15000000,
2771 };
2772
2773 static struct dib7000p_config nim7090_dib7000p_config = {
2774         .output_mpeg2_in_188_bytes  = 1,
2775         .hostbus_diversity                      = 1,
2776         .tuner_is_baseband                      = 1,
2777         .update_lna                                     = tfe7790p_update_lna, /* GPIO used is the same as TFE7790 */
2778
2779         .agc_config_count                       = 2,
2780         .agc                                            = dib7090_agc_config,
2781
2782         .bw                                                     = &dib7090_clock_config_12_mhz,
2783
2784         .gpio_dir                                       = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
2785         .gpio_val                                       = DIB7000P_GPIO_DEFAULT_VALUES,
2786         .gpio_pwm_pos                           = DIB7000P_GPIO_DEFAULT_PWM_POS,
2787
2788         .pwm_freq_div                           = 0,
2789
2790         .agc_control                            = dib7090_agc_restart,
2791
2792         .spur_protect                           = 0,
2793         .disable_sample_and_hold        = 0,
2794         .enable_current_mirror          = 0,
2795         .diversity_delay                        = 0,
2796
2797         .output_mode                            = OUTMODE_MPEG2_FIFO,
2798         .enMpegOutput                           = 1,
2799 };
2800
2801 static int tfe7090p_pvr_update_lna(struct dvb_frontend *fe, u16 agc_global)
2802 {
2803         struct dvb_usb_adapter *adap = fe->dvb->priv;
2804         struct dib0700_adapter_state *state = adap->priv;
2805
2806         deb_info("TFE7090P-PVR update LNA: agc global=%i", agc_global);
2807         if (agc_global < 25000) {
2808                 state->dib7000p_ops.set_gpio(fe, 5, 0, 0);
2809                 state->dib7000p_ops.set_agc1_min(fe, 0);
2810         } else {
2811                 state->dib7000p_ops.set_gpio(fe, 5, 0, 1);
2812                 state->dib7000p_ops.set_agc1_min(fe, 32768);
2813         }
2814
2815         return 0;
2816 }
2817
2818 static struct dib7000p_config tfe7090pvr_dib7000p_config[2] = {
2819         {
2820                 .output_mpeg2_in_188_bytes  = 1,
2821                 .hostbus_diversity                      = 1,
2822                 .tuner_is_baseband                      = 1,
2823                 .update_lna                                     = tfe7090p_pvr_update_lna,
2824
2825                 .agc_config_count                       = 2,
2826                 .agc                                            = dib7090_agc_config,
2827
2828                 .bw                                                     = &dib7090_clock_config_12_mhz,
2829
2830                 .gpio_dir                                       = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
2831                 .gpio_val                                       = DIB7000P_GPIO_DEFAULT_VALUES,
2832                 .gpio_pwm_pos                           = DIB7000P_GPIO_DEFAULT_PWM_POS,
2833
2834                 .pwm_freq_div                           = 0,
2835
2836                 .agc_control                            = dib7090_agc_restart,
2837
2838                 .spur_protect                           = 0,
2839                 .disable_sample_and_hold        = 0,
2840                 .enable_current_mirror          = 0,
2841                 .diversity_delay                        = 0,
2842
2843                 .output_mode                            = OUTMODE_MPEG2_PAR_GATED_CLK,
2844                 .default_i2c_addr                       = 0x90,
2845                 .enMpegOutput                           = 1,
2846         }, {
2847                 .output_mpeg2_in_188_bytes  = 1,
2848                 .hostbus_diversity                      = 1,
2849                 .tuner_is_baseband                      = 1,
2850                 .update_lna                                     = tfe7090p_pvr_update_lna,
2851
2852                 .agc_config_count                       = 2,
2853                 .agc                                            = dib7090_agc_config,
2854
2855                 .bw                                                     = &dib7090_clock_config_12_mhz,
2856
2857                 .gpio_dir                                       = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
2858                 .gpio_val                                       = DIB7000P_GPIO_DEFAULT_VALUES,
2859                 .gpio_pwm_pos                           = DIB7000P_GPIO_DEFAULT_PWM_POS,
2860
2861                 .pwm_freq_div                           = 0,
2862
2863                 .agc_control                            = dib7090_agc_restart,
2864
2865                 .spur_protect                           = 0,
2866                 .disable_sample_and_hold        = 0,
2867                 .enable_current_mirror          = 0,
2868                 .diversity_delay                        = 0,
2869
2870                 .output_mode                            = OUTMODE_MPEG2_PAR_GATED_CLK,
2871                 .default_i2c_addr                       = 0x92,
2872                 .enMpegOutput                           = 0,
2873         }
2874 };
2875
2876 static struct dib0090_config nim7090_dib0090_config = {
2877         .io.clock_khz = 12000,
2878         .io.pll_bypass = 0,
2879         .io.pll_range = 0,
2880         .io.pll_prediv = 3,
2881         .io.pll_loopdiv = 6,
2882         .io.adc_clock_ratio = 0,
2883         .io.pll_int_loop_filt = 0,
2884
2885         .freq_offset_khz_uhf = 0,
2886         .freq_offset_khz_vhf = 0,
2887
2888         .clkouttobamse = 1,
2889         .analog_output = 0,
2890
2891         .wbd_vhf_offset = 0,
2892         .wbd_cband_offset = 0,
2893         .use_pwm_agc = 1,
2894         .clkoutdrive = 0,
2895
2896         .fref_clock_ratio = 0,
2897
2898         .wbd = dib7090_wbd_table,
2899
2900         .ls_cfg_pad_drv = 0,
2901         .data_tx_drv = 0,
2902         .low_if = NULL,
2903         .in_soc = 1,
2904 };
2905
2906 static struct dib7000p_config tfe7790p_dib7000p_config = {
2907         .output_mpeg2_in_188_bytes  = 1,
2908         .hostbus_diversity                      = 1,
2909         .tuner_is_baseband                      = 1,
2910         .update_lna                                     = tfe7790p_update_lna,
2911
2912         .agc_config_count                       = 2,
2913         .agc                                            = dib7090_agc_config,
2914
2915         .bw                                                     = &dib7090_clock_config_12_mhz,
2916
2917         .gpio_dir                                       = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
2918         .gpio_val                                       = DIB7000P_GPIO_DEFAULT_VALUES,
2919         .gpio_pwm_pos                           = DIB7000P_GPIO_DEFAULT_PWM_POS,
2920
2921         .pwm_freq_div                           = 0,
2922
2923         .agc_control                            = dib7090_agc_restart,
2924
2925         .spur_protect                           = 0,
2926         .disable_sample_and_hold        = 0,
2927         .enable_current_mirror          = 0,
2928         .diversity_delay                        = 0,
2929
2930         .output_mode                            = OUTMODE_MPEG2_PAR_GATED_CLK,
2931         .enMpegOutput                           = 1,
2932 };
2933
2934 static struct dib0090_config tfe7790p_dib0090_config = {
2935         .io.clock_khz = 12000,
2936         .io.pll_bypass = 0,
2937         .io.pll_range = 0,
2938         .io.pll_prediv = 3,
2939         .io.pll_loopdiv = 6,
2940         .io.adc_clock_ratio = 0,
2941         .io.pll_int_loop_filt = 0,
2942
2943         .freq_offset_khz_uhf = 0,
2944         .freq_offset_khz_vhf = 0,
2945
2946         .clkouttobamse = 1,
2947         .analog_output = 0,
2948
2949         .wbd_vhf_offset = 0,
2950         .wbd_cband_offset = 0,
2951         .use_pwm_agc = 1,
2952         .clkoutdrive = 0,
2953
2954         .fref_clock_ratio = 0,
2955
2956         .wbd = dib7090_wbd_table,
2957
2958         .ls_cfg_pad_drv = 0,
2959         .data_tx_drv = 0,
2960         .low_if = NULL,
2961         .in_soc = 1,
2962         .force_cband_input = 0,
2963         .is_dib7090e = 0,
2964         .force_crystal_mode = 1,
2965 };
2966
2967 static struct dib0090_config tfe7090pvr_dib0090_config[2] = {
2968         {
2969                 .io.clock_khz = 12000,
2970                 .io.pll_bypass = 0,
2971                 .io.pll_range = 0,
2972                 .io.pll_prediv = 3,
2973                 .io.pll_loopdiv = 6,
2974                 .io.adc_clock_ratio = 0,
2975                 .io.pll_int_loop_filt = 0,
2976
2977                 .freq_offset_khz_uhf = 50,
2978                 .freq_offset_khz_vhf = 70,
2979
2980                 .clkouttobamse = 1,
2981                 .analog_output = 0,
2982
2983                 .wbd_vhf_offset = 0,
2984                 .wbd_cband_offset = 0,
2985                 .use_pwm_agc = 1,
2986                 .clkoutdrive = 0,
2987
2988                 .fref_clock_ratio = 0,
2989
2990                 .wbd = dib7090_wbd_table,
2991
2992                 .ls_cfg_pad_drv = 0,
2993                 .data_tx_drv = 0,
2994                 .low_if = NULL,
2995                 .in_soc = 1,
2996         }, {
2997                 .io.clock_khz = 12000,
2998                 .io.pll_bypass = 0,
2999                 .io.pll_range = 0,
3000                 .io.pll_prediv = 3,
3001                 .io.pll_loopdiv = 6,
3002                 .io.adc_clock_ratio = 0,
3003                 .io.pll_int_loop_filt = 0,
3004
3005                 .freq_offset_khz_uhf = -50,
3006                 .freq_offset_khz_vhf = -70,
3007
3008                 .clkouttobamse = 1,
3009                 .analog_output = 0,
3010
3011                 .wbd_vhf_offset = 0,
3012                 .wbd_cband_offset = 0,
3013                 .use_pwm_agc = 1,
3014                 .clkoutdrive = 0,
3015
3016                 .fref_clock_ratio = 0,
3017
3018                 .wbd = dib7090_wbd_table,
3019
3020                 .ls_cfg_pad_drv = 0,
3021                 .data_tx_drv = 0,
3022                 .low_if = NULL,
3023                 .in_soc = 1,
3024         }
3025 };
3026
3027 static int nim7090_frontend_attach(struct dvb_usb_adapter *adap)
3028 {
3029         struct dib0700_adapter_state *state = adap->priv;
3030
3031         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3032                 return -ENODEV;
3033
3034         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3035         msleep(20);
3036         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
3037         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
3038         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
3039         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3040
3041         msleep(20);
3042         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3043         msleep(20);
3044         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3045
3046         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 0x10, &nim7090_dib7000p_config) != 0) {
3047                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
3048                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3049                 return -ENODEV;
3050         }
3051         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x80, &nim7090_dib7000p_config);
3052
3053         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
3054 }
3055
3056 static int nim7090_tuner_attach(struct dvb_usb_adapter *adap)
3057 {
3058         struct dib0700_adapter_state *st = adap->priv;
3059         struct i2c_adapter *tun_i2c = st->dib7000p_ops.get_i2c_tuner(adap->fe_adap[0].fe);
3060
3061         nim7090_dib0090_config.reset = st->dib7000p_ops.tuner_sleep,
3062         nim7090_dib0090_config.sleep = st->dib7000p_ops.tuner_sleep,
3063         nim7090_dib0090_config.get_adc_power = st->dib7000p_ops.get_adc_power;
3064
3065         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &nim7090_dib0090_config) == NULL)
3066                 return -ENODEV;
3067
3068         st->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3069
3070         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
3071         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7090_agc_startup;
3072         return 0;
3073 }
3074
3075 static int tfe7090pvr_frontend0_attach(struct dvb_usb_adapter *adap)
3076 {
3077         struct dib0700_state *st = adap->dev->priv;
3078         struct dib0700_adapter_state *state = adap->priv;
3079
3080         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3081                 return -ENODEV;
3082
3083         /* The TFE7090 requires the dib0700 to not be in master mode */
3084         st->disable_streaming_master_mode = 1;
3085
3086         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3087         msleep(20);
3088         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
3089         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
3090         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
3091         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3092
3093         msleep(20);
3094         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3095         msleep(20);
3096         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3097
3098         /* initialize IC 0 */
3099         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 0x20, &tfe7090pvr_dib7000p_config[0]) != 0) {
3100                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
3101                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3102                 return -ENODEV;
3103         }
3104
3105         dib0700_set_i2c_speed(adap->dev, 340);
3106         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x90, &tfe7090pvr_dib7000p_config[0]);
3107         if (adap->fe_adap[0].fe == NULL)
3108                 return -ENODEV;
3109
3110         state->dib7000p_ops.slave_reset(adap->fe_adap[0].fe);
3111
3112         return 0;
3113 }
3114
3115 static int tfe7090pvr_frontend1_attach(struct dvb_usb_adapter *adap)
3116 {
3117         struct i2c_adapter *i2c;
3118         struct dib0700_adapter_state *state = adap->priv;
3119
3120         if (adap->dev->adapter[0].fe_adap[0].fe == NULL) {
3121                 err("the master dib7090 has to be initialized first");
3122                 return -ENODEV; /* the master device has not been initialized */
3123         }
3124
3125         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3126                 return -ENODEV;
3127
3128         i2c = state->dib7000p_ops.get_i2c_master(adap->dev->adapter[0].fe_adap[0].fe, DIBX000_I2C_INTERFACE_GPIO_6_7, 1);
3129         if (state->dib7000p_ops.i2c_enumeration(i2c, 1, 0x10, &tfe7090pvr_dib7000p_config[1]) != 0) {
3130                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n", __func__);
3131                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3132                 return -ENODEV;
3133         }
3134
3135         adap->fe_adap[0].fe = state->dib7000p_ops.init(i2c, 0x92, &tfe7090pvr_dib7000p_config[1]);
3136         dib0700_set_i2c_speed(adap->dev, 200);
3137
3138         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3139 }
3140
3141 static int tfe7090pvr_tuner0_attach(struct dvb_usb_adapter *adap)
3142 {
3143         struct dib0700_adapter_state *st = adap->priv;
3144         struct i2c_adapter *tun_i2c = st->dib7000p_ops.get_i2c_tuner(adap->fe_adap[0].fe);
3145
3146         tfe7090pvr_dib0090_config[0].reset = st->dib7000p_ops.tuner_sleep;
3147         tfe7090pvr_dib0090_config[0].sleep = st->dib7000p_ops.tuner_sleep;
3148         tfe7090pvr_dib0090_config[0].get_adc_power = st->dib7000p_ops.get_adc_power;
3149
3150         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &tfe7090pvr_dib0090_config[0]) == NULL)
3151                 return -ENODEV;
3152
3153         st->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3154
3155         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
3156         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7090_agc_startup;
3157         return 0;
3158 }
3159
3160 static int tfe7090pvr_tuner1_attach(struct dvb_usb_adapter *adap)
3161 {
3162         struct dib0700_adapter_state *st = adap->priv;
3163         struct i2c_adapter *tun_i2c = st->dib7000p_ops.get_i2c_tuner(adap->fe_adap[0].fe);
3164
3165         tfe7090pvr_dib0090_config[1].reset = st->dib7000p_ops.tuner_sleep;
3166         tfe7090pvr_dib0090_config[1].sleep = st->dib7000p_ops.tuner_sleep;
3167         tfe7090pvr_dib0090_config[1].get_adc_power = st->dib7000p_ops.get_adc_power;
3168
3169         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c, &tfe7090pvr_dib0090_config[1]) == NULL)
3170                 return -ENODEV;
3171
3172         st->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3173
3174         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
3175         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7090_agc_startup;
3176         return 0;
3177 }
3178
3179 static int tfe7790p_frontend_attach(struct dvb_usb_adapter *adap)
3180 {
3181         struct dib0700_state *st = adap->dev->priv;
3182         struct dib0700_adapter_state *state = adap->priv;
3183
3184         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3185                 return -ENODEV;
3186
3187         /* The TFE7790P requires the dib0700 to not be in master mode */
3188         st->disable_streaming_master_mode = 1;
3189
3190         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3191         msleep(20);
3192         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
3193         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
3194         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
3195         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3196         msleep(20);
3197         dib0700_ctrl_clock(adap->dev, 72, 1);
3198         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3199         msleep(20);
3200         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3201
3202         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap,
3203                                 1, 0x10, &tfe7790p_dib7000p_config) != 0) {
3204                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
3205                                 __func__);
3206                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3207                 return -ENODEV;
3208         }
3209         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap,
3210                         0x80, &tfe7790p_dib7000p_config);
3211
3212         return adap->fe_adap[0].fe == NULL ?  -ENODEV : 0;
3213 }
3214
3215 static int tfe7790p_tuner_attach(struct dvb_usb_adapter *adap)
3216 {
3217         struct dib0700_adapter_state *st = adap->priv;
3218         struct i2c_adapter *tun_i2c =
3219                 st->dib7000p_ops.get_i2c_tuner(adap->fe_adap[0].fe);
3220
3221
3222         tfe7790p_dib0090_config.reset = st->dib7000p_ops.tuner_sleep;
3223         tfe7790p_dib0090_config.sleep = st->dib7000p_ops.tuner_sleep;
3224         tfe7790p_dib0090_config.get_adc_power = st->dib7000p_ops.get_adc_power;
3225
3226         if (dvb_attach(dib0090_register, adap->fe_adap[0].fe, tun_i2c,
3227                                 &tfe7790p_dib0090_config) == NULL)
3228                 return -ENODEV;
3229
3230         st->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3231
3232         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
3233         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7090_agc_startup;
3234         return 0;
3235 }
3236
3237 /* STK7070PD */
3238 static struct dib7000p_config stk7070pd_dib7000p_config[2] = {
3239         {
3240                 .output_mpeg2_in_188_bytes = 1,
3241
3242                 .agc_config_count = 1,
3243                 .agc = &dib7070_agc_config,
3244                 .bw  = &dib7070_bw_config_12_mhz,
3245                 .tuner_is_baseband = 1,
3246                 .spur_protect = 1,
3247
3248                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
3249                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
3250                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
3251
3252                 .hostbus_diversity = 1,
3253         }, {
3254                 .output_mpeg2_in_188_bytes = 1,
3255
3256                 .agc_config_count = 1,
3257                 .agc = &dib7070_agc_config,
3258                 .bw  = &dib7070_bw_config_12_mhz,
3259                 .tuner_is_baseband = 1,
3260                 .spur_protect = 1,
3261
3262                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
3263                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
3264                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
3265
3266                 .hostbus_diversity = 1,
3267         }
3268 };
3269
3270 static void stk7070pd_init(struct dvb_usb_device *dev)
3271 {
3272         dib0700_set_gpio(dev, GPIO6, GPIO_OUT, 1);
3273         msleep(10);
3274         dib0700_set_gpio(dev, GPIO9, GPIO_OUT, 1);
3275         dib0700_set_gpio(dev, GPIO4, GPIO_OUT, 1);
3276         dib0700_set_gpio(dev, GPIO7, GPIO_OUT, 1);
3277         dib0700_set_gpio(dev, GPIO10, GPIO_OUT, 0);
3278
3279         dib0700_ctrl_clock(dev, 72, 1);
3280
3281         msleep(10);
3282         dib0700_set_gpio(dev, GPIO10, GPIO_OUT, 1);
3283 }
3284
3285 static int stk7070pd_frontend_attach0(struct dvb_usb_adapter *adap)
3286 {
3287         struct dib0700_adapter_state *state = adap->priv;
3288
3289         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3290                 return -ENODEV;
3291
3292         stk7070pd_init(adap->dev);
3293
3294         msleep(10);
3295         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3296
3297         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 2, 18,
3298                                      stk7070pd_dib7000p_config) != 0) {
3299                 err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
3300                     __func__);
3301                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3302                 return -ENODEV;
3303         }
3304
3305         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x80, &stk7070pd_dib7000p_config[0]);
3306         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3307 }
3308
3309 static int stk7070pd_frontend_attach1(struct dvb_usb_adapter *adap)
3310 {
3311         struct dib0700_adapter_state *state = adap->priv;
3312
3313         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3314                 return -ENODEV;
3315
3316         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x82, &stk7070pd_dib7000p_config[1]);
3317         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3318 }
3319
3320 static int novatd_read_status_override(struct dvb_frontend *fe,
3321                                        enum fe_status *stat)
3322 {
3323         struct dvb_usb_adapter *adap = fe->dvb->priv;
3324         struct dvb_usb_device *dev = adap->dev;
3325         struct dib0700_state *state = dev->priv;
3326         int ret;
3327
3328         ret = state->read_status(fe, stat);
3329
3330         if (!ret)
3331                 dib0700_set_gpio(dev, adap->id == 0 ? GPIO1 : GPIO0, GPIO_OUT,
3332                                 !!(*stat & FE_HAS_LOCK));
3333
3334         return ret;
3335 }
3336
3337 static int novatd_sleep_override(struct dvb_frontend* fe)
3338 {
3339         struct dvb_usb_adapter *adap = fe->dvb->priv;
3340         struct dvb_usb_device *dev = adap->dev;
3341         struct dib0700_state *state = dev->priv;
3342
3343         /* turn off LED */
3344         dib0700_set_gpio(dev, adap->id == 0 ? GPIO1 : GPIO0, GPIO_OUT, 0);
3345
3346         return state->sleep(fe);
3347 }
3348
3349 /**
3350  * novatd_frontend_attach - Nova-TD specific attach
3351  *
3352  * Nova-TD has GPIO0, 1 and 2 for LEDs. So do not fiddle with them except for
3353  * information purposes.
3354  */
3355 static int novatd_frontend_attach(struct dvb_usb_adapter *adap)
3356 {
3357         struct dvb_usb_device *dev = adap->dev;
3358         struct dib0700_state *st = dev->priv;
3359         struct dib0700_adapter_state *state = adap->priv;
3360
3361         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3362                 return -ENODEV;
3363
3364         if (adap->id == 0) {
3365                 stk7070pd_init(dev);
3366
3367                 /* turn the power LED on, the other two off (just in case) */
3368                 dib0700_set_gpio(dev, GPIO0, GPIO_OUT, 0);
3369                 dib0700_set_gpio(dev, GPIO1, GPIO_OUT, 0);
3370                 dib0700_set_gpio(dev, GPIO2, GPIO_OUT, 1);
3371
3372                 if (state->dib7000p_ops.i2c_enumeration(&dev->i2c_adap, 2, 18,
3373                                              stk7070pd_dib7000p_config) != 0) {
3374                         err("%s: state->dib7000p_ops.i2c_enumeration failed.  Cannot continue\n",
3375                             __func__);
3376                         dvb_detach(state->dib7000p_ops.set_wbd_ref);
3377                         return -ENODEV;
3378                 }
3379         }
3380
3381         adap->fe_adap[0].fe = state->dib7000p_ops.init(&dev->i2c_adap,
3382                         adap->id == 0 ? 0x80 : 0x82,
3383                         &stk7070pd_dib7000p_config[adap->id]);
3384
3385         if (adap->fe_adap[0].fe == NULL)
3386                 return -ENODEV;
3387
3388         st->read_status = adap->fe_adap[0].fe->ops.read_status;
3389         adap->fe_adap[0].fe->ops.read_status = novatd_read_status_override;
3390         st->sleep = adap->fe_adap[0].fe->ops.sleep;
3391         adap->fe_adap[0].fe->ops.sleep = novatd_sleep_override;
3392
3393         return 0;
3394 }
3395
3396 /* S5H1411 */
3397 static struct s5h1411_config pinnacle_801e_config = {
3398         .output_mode   = S5H1411_PARALLEL_OUTPUT,
3399         .gpio          = S5H1411_GPIO_OFF,
3400         .mpeg_timing   = S5H1411_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK,
3401         .qam_if        = S5H1411_IF_44000,
3402         .vsb_if        = S5H1411_IF_44000,
3403         .inversion     = S5H1411_INVERSION_OFF,
3404         .status_mode   = S5H1411_DEMODLOCKING
3405 };
3406
3407 /* Pinnacle PCTV HD Pro 801e GPIOs map:
3408    GPIO0  - currently unknown
3409    GPIO1  - xc5000 tuner reset
3410    GPIO2  - CX25843 sleep
3411    GPIO3  - currently unknown
3412    GPIO4  - currently unknown
3413    GPIO6  - currently unknown
3414    GPIO7  - currently unknown
3415    GPIO9  - currently unknown
3416    GPIO10 - CX25843 reset
3417  */
3418 static int s5h1411_frontend_attach(struct dvb_usb_adapter *adap)
3419 {
3420         struct dib0700_state *st = adap->dev->priv;
3421
3422         /* Make use of the new i2c functions from FW 1.20 */
3423         st->fw_use_new_i2c_api = 1;
3424
3425         /* The s5h1411 requires the dib0700 to not be in master mode */
3426         st->disable_streaming_master_mode = 1;
3427
3428         /* All msleep values taken from Windows USB trace */
3429         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 0);
3430         dib0700_set_gpio(adap->dev, GPIO3, GPIO_OUT, 0);
3431         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3432         msleep(400);
3433         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3434         msleep(60);
3435         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3436         msleep(30);
3437         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
3438         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
3439         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
3440         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
3441         dib0700_set_gpio(adap->dev, GPIO2, GPIO_OUT, 0);
3442         msleep(30);
3443
3444         /* Put the CX25843 to sleep for now since we're in digital mode */
3445         dib0700_set_gpio(adap->dev, GPIO2, GPIO_OUT, 1);
3446
3447         /* GPIOs are initialized, do the attach */
3448         adap->fe_adap[0].fe = dvb_attach(s5h1411_attach, &pinnacle_801e_config,
3449                               &adap->dev->i2c_adap);
3450         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3451 }
3452
3453 static int dib0700_xc5000_tuner_callback(void *priv, int component,
3454                                          int command, int arg)
3455 {
3456         struct dvb_usb_adapter *adap = priv;
3457
3458         if (command == XC5000_TUNER_RESET) {
3459                 /* Reset the tuner */
3460                 dib0700_set_gpio(adap->dev, GPIO1, GPIO_OUT, 0);
3461                 msleep(10);
3462                 dib0700_set_gpio(adap->dev, GPIO1, GPIO_OUT, 1);
3463                 msleep(10);
3464         } else {
3465                 err("xc5000: unknown tuner callback command: %d\n", command);
3466                 return -EINVAL;
3467         }
3468
3469         return 0;
3470 }
3471
3472 static struct xc5000_config s5h1411_xc5000_tunerconfig = {
3473         .i2c_address      = 0x64,
3474         .if_khz           = 5380,
3475 };
3476
3477 static int xc5000_tuner_attach(struct dvb_usb_adapter *adap)
3478 {
3479         /* FIXME: generalize & move to common area */
3480         adap->fe_adap[0].fe->callback = dib0700_xc5000_tuner_callback;
3481
3482         return dvb_attach(xc5000_attach, adap->fe_adap[0].fe, &adap->dev->i2c_adap,
3483                           &s5h1411_xc5000_tunerconfig)
3484                 == NULL ? -ENODEV : 0;
3485 }
3486
3487 static int dib0700_xc4000_tuner_callback(void *priv, int component,
3488                                          int command, int arg)
3489 {
3490         struct dvb_usb_adapter *adap = priv;
3491         struct dib0700_adapter_state *state = adap->priv;
3492
3493         if (command == XC4000_TUNER_RESET) {
3494                 /* Reset the tuner */
3495                 state->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 0);
3496                 msleep(10);
3497                 state->dib7000p_ops.set_gpio(adap->fe_adap[0].fe, 8, 0, 1);
3498         } else {
3499                 err("xc4000: unknown tuner callback command: %d\n", command);
3500                 return -EINVAL;
3501         }
3502
3503         return 0;
3504 }
3505
3506 static struct dibx000_agc_config stk7700p_7000p_xc4000_agc_config = {
3507         .band_caps = BAND_UHF | BAND_VHF,
3508         .setup = 0x64,
3509         .inv_gain = 0x02c8,
3510         .time_stabiliz = 0x15,
3511         .alpha_level = 0x00,
3512         .thlock = 0x76,
3513         .wbd_inv = 0x01,
3514         .wbd_ref = 0x0b33,
3515         .wbd_sel = 0x00,
3516         .wbd_alpha = 0x02,
3517         .agc1_max = 0x00,
3518         .agc1_min = 0x00,
3519         .agc2_max = 0x9b26,
3520         .agc2_min = 0x26ca,
3521         .agc1_pt1 = 0x00,
3522         .agc1_pt2 = 0x00,
3523         .agc1_pt3 = 0x00,
3524         .agc1_slope1 = 0x00,
3525         .agc1_slope2 = 0x00,
3526         .agc2_pt1 = 0x00,
3527         .agc2_pt2 = 0x80,
3528         .agc2_slope1 = 0x1d,
3529         .agc2_slope2 = 0x1d,
3530         .alpha_mant = 0x11,
3531         .alpha_exp = 0x1b,
3532         .beta_mant = 0x17,
3533         .beta_exp = 0x33,
3534         .perform_agc_softsplit = 0x00,
3535 };
3536
3537 static struct dibx000_bandwidth_config stk7700p_xc4000_pll_config = {
3538         .internal = 60000,
3539         .sampling = 30000,
3540         .pll_prediv = 1,
3541         .pll_ratio = 8,
3542         .pll_range = 3,
3543         .pll_reset = 1,
3544         .pll_bypass = 0,
3545         .enable_refdiv = 0,
3546         .bypclk_div = 0,
3547         .IO_CLK_en_core = 1,
3548         .ADClkSrc = 1,
3549         .modulo = 0,
3550         .sad_cfg = (3 << 14) | (1 << 12) | 524, /* sad_cfg: refsel, sel, freq_15k */
3551         .ifreq = 39370534,
3552         .timf = 20452225,
3553         .xtal_hz = 30000000
3554 };
3555
3556 /* FIXME: none of these inputs are validated yet */
3557 static struct dib7000p_config pctv_340e_config = {
3558         .output_mpeg2_in_188_bytes = 1,
3559
3560         .agc_config_count = 1,
3561         .agc = &stk7700p_7000p_xc4000_agc_config,
3562         .bw  = &stk7700p_xc4000_pll_config,
3563
3564         .gpio_dir = DIB7000M_GPIO_DEFAULT_DIRECTIONS,
3565         .gpio_val = DIB7000M_GPIO_DEFAULT_VALUES,
3566         .gpio_pwm_pos = DIB7000M_GPIO_DEFAULT_PWM_POS,
3567 };
3568
3569 /* PCTV 340e GPIOs map:
3570    dib0700:
3571    GPIO2  - CX25843 sleep
3572    GPIO3  - CS5340 reset
3573    GPIO5  - IRD
3574    GPIO6  - Power Supply
3575    GPIO8  - LNA (1=off 0=on)
3576    GPIO10 - CX25843 reset
3577    dib7000:
3578    GPIO8  - xc4000 reset
3579  */
3580 static int pctv340e_frontend_attach(struct dvb_usb_adapter *adap)
3581 {
3582         struct dib0700_state *st = adap->dev->priv;
3583         struct dib0700_adapter_state *state = adap->priv;
3584
3585         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
3586                 return -ENODEV;
3587
3588         /* Power Supply on */
3589         dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 0);
3590         msleep(50);
3591         dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 1);
3592         msleep(100); /* Allow power supply to settle before probing */
3593
3594         /* cx25843 reset */
3595         dib0700_set_gpio(adap->dev, GPIO10,  GPIO_OUT, 0);
3596         msleep(1); /* cx25843 datasheet say 350us required */
3597         dib0700_set_gpio(adap->dev, GPIO10,  GPIO_OUT, 1);
3598
3599         /* LNA off for now */
3600         dib0700_set_gpio(adap->dev, GPIO8,  GPIO_OUT, 1);
3601
3602         /* Put the CX25843 to sleep for now since we're in digital mode */
3603         dib0700_set_gpio(adap->dev, GPIO2, GPIO_OUT, 1);
3604
3605         /* FIXME: not verified yet */
3606         dib0700_ctrl_clock(adap->dev, 72, 1);
3607
3608         msleep(500);
3609
3610         if (state->dib7000p_ops.dib7000pc_detection(&adap->dev->i2c_adap) == 0) {
3611                 /* Demodulator not found for some reason? */
3612                 dvb_detach(state->dib7000p_ops.set_wbd_ref);
3613                 return -ENODEV;
3614         }
3615
3616         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x12,
3617                               &pctv_340e_config);
3618         st->is_dib7000pc = 1;
3619
3620         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3621 }
3622
3623 static struct xc4000_config dib7000p_xc4000_tunerconfig = {
3624         .i2c_address      = 0x61,
3625         .default_pm       = 1,
3626         .dvb_amplitude    = 0,
3627         .set_smoothedcvbs = 0,
3628         .if_khz           = 5400
3629 };
3630
3631 static int xc4000_tuner_attach(struct dvb_usb_adapter *adap)
3632 {
3633         struct i2c_adapter *tun_i2c;
3634         struct dib0700_adapter_state *state = adap->priv;
3635
3636         /* The xc4000 is not on the main i2c bus */
3637         tun_i2c = state->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
3638                                           DIBX000_I2C_INTERFACE_TUNER, 1);
3639         if (tun_i2c == NULL) {
3640                 printk(KERN_ERR "Could not reach tuner i2c bus\n");
3641                 return 0;
3642         }
3643
3644         /* Setup the reset callback */
3645         adap->fe_adap[0].fe->callback = dib0700_xc4000_tuner_callback;
3646
3647         return dvb_attach(xc4000_attach, adap->fe_adap[0].fe, tun_i2c,
3648                           &dib7000p_xc4000_tunerconfig)
3649                 == NULL ? -ENODEV : 0;
3650 }
3651
3652 static struct lgdt3305_config hcw_lgdt3305_config = {
3653         .i2c_addr           = 0x0e,
3654         .mpeg_mode          = LGDT3305_MPEG_PARALLEL,
3655         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
3656         .tpvalid_polarity   = LGDT3305_TP_VALID_LOW,
3657         .deny_i2c_rptr      = 0,
3658         .spectral_inversion = 1,
3659         .qam_if_khz         = 6000,
3660         .vsb_if_khz         = 6000,
3661         .usref_8vsb         = 0x0500,
3662 };
3663
3664 static struct mxl5007t_config hcw_mxl5007t_config = {
3665         .xtal_freq_hz = MxL_XTAL_25_MHZ,
3666         .if_freq_hz = MxL_IF_6_MHZ,
3667         .invert_if = 1,
3668 };
3669
3670 /* TIGER-ATSC map:
3671    GPIO0  - LNA_CTR  (H: LNA power enabled, L: LNA power disabled)
3672    GPIO1  - ANT_SEL  (H: VPA, L: MCX)
3673    GPIO4  - SCL2
3674    GPIO6  - EN_TUNER
3675    GPIO7  - SDA2
3676    GPIO10 - DEM_RST
3677
3678    MXL is behind LG's i2c repeater.  LG is on SCL2/SDA2 gpios on the DIB
3679  */
3680 static int lgdt3305_frontend_attach(struct dvb_usb_adapter *adap)
3681 {
3682         struct dib0700_state *st = adap->dev->priv;
3683
3684         /* Make use of the new i2c functions from FW 1.20 */
3685         st->fw_use_new_i2c_api = 1;
3686
3687         st->disable_streaming_master_mode = 1;
3688
3689         /* fe power enable */
3690         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
3691         msleep(30);
3692         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
3693         msleep(30);
3694
3695         /* demod reset */
3696         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3697         msleep(30);
3698         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
3699         msleep(30);
3700         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
3701         msleep(30);
3702
3703         adap->fe_adap[0].fe = dvb_attach(lgdt3305_attach,
3704                               &hcw_lgdt3305_config,
3705                               &adap->dev->i2c_adap);
3706
3707         return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
3708 }
3709
3710 static int mxl5007t_tuner_attach(struct dvb_usb_adapter *adap)
3711 {
3712         return dvb_attach(mxl5007t_attach, adap->fe_adap[0].fe,
3713                           &adap->dev->i2c_adap, 0x60,
3714                           &hcw_mxl5007t_config) == NULL ? -ENODEV : 0;
3715 }
3716
3717
3718 /* DVB-USB and USB stuff follows */
3719 struct usb_device_id dib0700_usb_id_table[] = {
3720 /* 0 */ { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7700P) },
3721         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7700P_PC) },
3722         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_500) },
3723         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_500_2) },
3724         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK) },
3725 /* 5 */ { USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR) },
3726         { USB_DEVICE(USB_VID_COMPRO,    USB_PID_COMPRO_VIDEOMATE_U500) },
3727         { USB_DEVICE(USB_VID_UNIWILL,   USB_PID_UNIWILL_STK7700P) },
3728         { USB_DEVICE(USB_VID_LEADTEK,   USB_PID_WINFAST_DTV_DONGLE_STK7700P) },
3729         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK_2) },
3730 /* 10 */{ USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_2) },
3731         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV2000E) },
3732         { USB_DEVICE(USB_VID_TERRATEC,
3733                         USB_PID_TERRATEC_CINERGY_DT_XS_DIVERSITY) },
3734         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_TD_STICK) },
3735         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7700D) },
3736 /* 15 */{ USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7070P) },
3737         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV_DVB_T_FLASH) },
3738         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7070PD) },
3739         { USB_DEVICE(USB_VID_PINNACLE,
3740                         USB_PID_PINNACLE_PCTV_DUAL_DIVERSITY_DVB_T) },
3741         { USB_DEVICE(USB_VID_COMPRO,    USB_PID_COMPRO_VIDEOMATE_U500_PC) },
3742 /* 20 */{ USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_EXPRESS) },
3743         { USB_DEVICE(USB_VID_GIGABYTE,  USB_PID_GIGABYTE_U7000) },
3744         { USB_DEVICE(USB_VID_ULTIMA_ELECTRONIC, USB_PID_ARTEC_T14BR) },
3745         { USB_DEVICE(USB_VID_ASUS,      USB_PID_ASUS_U3000) },
3746         { USB_DEVICE(USB_VID_ASUS,      USB_PID_ASUS_U3100) },
3747 /* 25 */{ USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK_3) },
3748         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_MYTV_T) },
3749         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_HT_USB_XE) },
3750         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_EXPRESSCARD_320CX) },
3751         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV72E) },
3752 /* 30 */{ USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV73E) },
3753         { USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_EC372S) },
3754         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_HT_EXPRESS) },
3755         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_T_XXS) },
3756         { USB_DEVICE(USB_VID_LEADTEK,   USB_PID_WINFAST_DTV_DONGLE_STK7700P_2) },
3757 /* 35 */{ USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_TD_STICK_52009) },
3758         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_500_3) },
3759         { USB_DEVICE(USB_VID_GIGABYTE,  USB_PID_GIGABYTE_U8000) },
3760         { USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_STK7700PH) },
3761         { USB_DEVICE(USB_VID_ASUS,      USB_PID_ASUS_U3000H) },
3762 /* 40 */{ USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV801E) },
3763         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV801E_SE) },
3764         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_T_EXPRESS) },
3765         { USB_DEVICE(USB_VID_TERRATEC,
3766                         USB_PID_TERRATEC_CINERGY_DT_XS_DIVERSITY_2) },
3767         { USB_DEVICE(USB_VID_SONY,      USB_PID_SONY_PLAYTV) },
3768 /* 45 */{ USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_PD378S) },
3769         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_TIGER_ATSC) },
3770         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_TIGER_ATSC_B210) },
3771         { USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_MC770) },
3772         { USB_DEVICE(USB_VID_ELGATO,    USB_PID_ELGATO_EYETV_DTT) },
3773 /* 50 */{ USB_DEVICE(USB_VID_ELGATO,    USB_PID_ELGATO_EYETV_DTT_Dlx) },
3774         { USB_DEVICE(USB_VID_LEADTEK,   USB_PID_WINFAST_DTV_DONGLE_H) },
3775         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_T3) },
3776         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_T5) },
3777         { USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_STK7700D) },
3778 /* 55 */{ USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_STK7700D_2) },
3779         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV73A) },
3780         { USB_DEVICE(USB_VID_PCTV,      USB_PID_PINNACLE_PCTV73ESE) },
3781         { USB_DEVICE(USB_VID_PCTV,      USB_PID_PINNACLE_PCTV282E) },
3782         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7770P) },
3783 /* 60 */{ USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_T_XXS_2) },
3784         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK807XPVR) },
3785         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK807XP) },
3786         { USB_DEVICE_VER(USB_VID_PIXELVIEW, USB_PID_PIXELVIEW_SBTVD, 0x000, 0x3f00) },
3787         { USB_DEVICE(USB_VID_EVOLUTEPC, USB_PID_TVWAY_PLUS) },
3788 /* 65 */{ USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV73ESE) },
3789         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV282E) },
3790         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK8096GP) },
3791         { USB_DEVICE(USB_VID_ELGATO,    USB_PID_ELGATO_EYETV_DIVERSITY) },
3792         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_NIM9090M) },
3793 /* 70 */{ USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_NIM8096MD) },
3794         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_NIM9090MD) },
3795         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_NIM7090) },
3796         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_TFE7090PVR) },
3797         { USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_AIRSTAR_TELESTICK_2) },
3798 /* 75 */{ USB_DEVICE(USB_VID_MEDION,    USB_PID_CREATIX_CTX1921) },
3799         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV340E) },
3800         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV340E_SE) },
3801         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_TFE7790P) },
3802         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_TFE8096P) },
3803 /* 80 */{ USB_DEVICE(USB_VID_ELGATO,    USB_PID_ELGATO_EYETV_DTT_2) },
3804         { USB_DEVICE(USB_VID_PCTV,      USB_PID_PCTV_2002E) },
3805         { USB_DEVICE(USB_VID_PCTV,      USB_PID_PCTV_2002E_SE) },
3806         { 0 }           /* Terminating entry */
3807 };
3808 MODULE_DEVICE_TABLE(usb, dib0700_usb_id_table);
3809
3810 #define DIB0700_DEFAULT_DEVICE_PROPERTIES \
3811         .caps              = DVB_USB_IS_AN_I2C_ADAPTER, \
3812         .usb_ctrl          = DEVICE_SPECIFIC, \
3813         .firmware          = "/*(DEBLOBBED)*/", \
3814         .download_firmware = dib0700_download_firmware, \
3815         .no_reconnect      = 1, \
3816         .size_of_priv      = sizeof(struct dib0700_state), \
3817         .i2c_algo          = &dib0700_i2c_algo, \
3818         .identify_state    = dib0700_identify_state
3819
3820 #define DIB0700_DEFAULT_STREAMING_CONFIG(ep) \
3821         .streaming_ctrl   = dib0700_streaming_ctrl, \
3822         .stream = { \
3823                 .type = USB_BULK, \
3824                 .count = 4, \
3825                 .endpoint = ep, \
3826                 .u = { \
3827                         .bulk = { \
3828                                 .buffersize = 39480, \
3829                         } \
3830                 } \
3831         }
3832
3833 #define DIB0700_NUM_FRONTENDS(n) \
3834         .num_frontends = n, \
3835         .size_of_priv     = sizeof(struct dib0700_adapter_state)
3836
3837 struct dvb_usb_device_properties dib0700_devices[] = {
3838         {
3839                 DIB0700_DEFAULT_DEVICE_PROPERTIES,
3840
3841                 .num_adapters = 1,
3842                 .adapter = {
3843                         {
3844                         DIB0700_NUM_FRONTENDS(1),
3845                         .fe = {{
3846                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
3847                                 .pid_filter_count = 32,
3848                                 .pid_filter       = stk7700p_pid_filter,
3849                                 .pid_filter_ctrl  = stk7700p_pid_filter_ctrl,
3850                                 .frontend_attach  = stk7700p_frontend_attach,
3851                                 .tuner_attach     = stk7700p_tuner_attach,
3852
3853                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
3854                         }},
3855                         },
3856                 },
3857
3858                 .num_device_descs = 8,
3859                 .devices = {
3860                         {   "DiBcom STK7700P reference design",
3861                                 { &dib0700_usb_id_table[0], &dib0700_usb_id_table[1] },
3862                                 { NULL },
3863                         },
3864                         {   "Hauppauge Nova-T Stick",
3865                                 { &dib0700_usb_id_table[4], &dib0700_usb_id_table[9], NULL },
3866                                 { NULL },
3867                         },
3868                         {   "AVerMedia AVerTV DVB-T Volar",
3869                                 { &dib0700_usb_id_table[5], &dib0700_usb_id_table[10] },
3870                                 { NULL },
3871                         },
3872                         {   "Compro Videomate U500",
3873                                 { &dib0700_usb_id_table[6], &dib0700_usb_id_table[19] },
3874                                 { NULL },
3875                         },
3876                         {   "Uniwill STK7700P based (Hama and others)",
3877                                 { &dib0700_usb_id_table[7], NULL },
3878                                 { NULL },
3879                         },
3880                         {   "Leadtek Winfast DTV Dongle (STK7700P based)",
3881                                 { &dib0700_usb_id_table[8], &dib0700_usb_id_table[34] },
3882                                 { NULL },
3883                         },
3884                         {   "AVerMedia AVerTV DVB-T Express",
3885                                 { &dib0700_usb_id_table[20] },
3886                                 { NULL },
3887                         },
3888                         {   "Gigabyte U7000",
3889                                 { &dib0700_usb_id_table[21], NULL },
3890                                 { NULL },
3891                         }
3892                 },
3893
3894                 .rc.core = {
3895                         .rc_interval      = DEFAULT_RC_INTERVAL,
3896                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
3897                         .rc_query         = dib0700_rc_query_old_firmware,
3898                         .allowed_protos   = RC_BIT_RC5 |
3899                                             RC_BIT_RC6_MCE |
3900                                             RC_BIT_NEC,
3901                         .change_protocol  = dib0700_change_protocol,
3902                 },
3903         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
3904
3905                 .num_adapters = 2,
3906                 .adapter = {
3907                         {
3908                         DIB0700_NUM_FRONTENDS(1),
3909                         .fe = {{
3910                                 .frontend_attach  = bristol_frontend_attach,
3911                                 .tuner_attach     = bristol_tuner_attach,
3912
3913                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
3914                         }},
3915                         }, {
3916                         DIB0700_NUM_FRONTENDS(1),
3917                         .fe = {{
3918                                 .frontend_attach  = bristol_frontend_attach,
3919                                 .tuner_attach     = bristol_tuner_attach,
3920
3921                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
3922                         }},
3923                         }
3924                 },
3925
3926                 .num_device_descs = 1,
3927                 .devices = {
3928                         {   "Hauppauge Nova-T 500 Dual DVB-T",
3929                                 { &dib0700_usb_id_table[2], &dib0700_usb_id_table[3], NULL },
3930                                 { NULL },
3931                         },
3932                 },
3933
3934                 .rc.core = {
3935                         .rc_interval      = DEFAULT_RC_INTERVAL,
3936                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
3937                         .rc_query         = dib0700_rc_query_old_firmware,
3938                         .allowed_protos   = RC_BIT_RC5 |
3939                                             RC_BIT_RC6_MCE |
3940                                             RC_BIT_NEC,
3941                         .change_protocol = dib0700_change_protocol,
3942                 },
3943         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
3944
3945                 .num_adapters = 2,
3946                 .adapter = {
3947                         {
3948                         DIB0700_NUM_FRONTENDS(1),
3949                         .fe = {{
3950                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
3951                                 .pid_filter_count = 32,
3952                                 .pid_filter       = stk70x0p_pid_filter,
3953                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
3954                                 .frontend_attach  = stk7700d_frontend_attach,
3955                                 .tuner_attach     = stk7700d_tuner_attach,
3956
3957                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
3958                         }},
3959                         }, {
3960                         DIB0700_NUM_FRONTENDS(1),
3961                         .fe = {{
3962                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
3963                                 .pid_filter_count = 32,
3964                                 .pid_filter       = stk70x0p_pid_filter,
3965                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
3966                                 .frontend_attach  = stk7700d_frontend_attach,
3967                                 .tuner_attach     = stk7700d_tuner_attach,
3968
3969                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
3970                         }},
3971                         }
3972                 },
3973
3974                 .num_device_descs = 5,
3975                 .devices = {
3976                         {   "Pinnacle PCTV 2000e",
3977                                 { &dib0700_usb_id_table[11], NULL },
3978                                 { NULL },
3979                         },
3980                         {   "Terratec Cinergy DT XS Diversity",
3981                                 { &dib0700_usb_id_table[12], NULL },
3982                                 { NULL },
3983                         },
3984                         {   "Hauppauge Nova-TD Stick/Elgato Eye-TV Diversity",
3985                                 { &dib0700_usb_id_table[13], NULL },
3986                                 { NULL },
3987                         },
3988                         {   "DiBcom STK7700D reference design",
3989                                 { &dib0700_usb_id_table[14], NULL },
3990                                 { NULL },
3991                         },
3992                         {   "YUAN High-Tech DiBcom STK7700D",
3993                                 { &dib0700_usb_id_table[55], NULL },
3994                                 { NULL },
3995                         },
3996
3997                 },
3998
3999                 .rc.core = {
4000                         .rc_interval      = DEFAULT_RC_INTERVAL,
4001                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4002                         .rc_query         = dib0700_rc_query_old_firmware,
4003                         .allowed_protos   = RC_BIT_RC5 |
4004                                             RC_BIT_RC6_MCE |
4005                                             RC_BIT_NEC,
4006                         .change_protocol = dib0700_change_protocol,
4007                 },
4008         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4009
4010                 .num_adapters = 1,
4011                 .adapter = {
4012                         {
4013                         DIB0700_NUM_FRONTENDS(1),
4014                         .fe = {{
4015                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4016                                 .pid_filter_count = 32,
4017                                 .pid_filter       = stk70x0p_pid_filter,
4018                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4019                                 .frontend_attach  = stk7700P2_frontend_attach,
4020                                 .tuner_attach     = stk7700d_tuner_attach,
4021
4022                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4023                         }},
4024                         },
4025                 },
4026
4027                 .num_device_descs = 3,
4028                 .devices = {
4029                         {   "ASUS My Cinema U3000 Mini DVBT Tuner",
4030                                 { &dib0700_usb_id_table[23], NULL },
4031                                 { NULL },
4032                         },
4033                         {   "Yuan EC372S",
4034                                 { &dib0700_usb_id_table[31], NULL },
4035                                 { NULL },
4036                         },
4037                         {   "Terratec Cinergy T Express",
4038                                 { &dib0700_usb_id_table[42], NULL },
4039                                 { NULL },
4040                         }
4041                 },
4042
4043                 .rc.core = {
4044                         .rc_interval      = DEFAULT_RC_INTERVAL,
4045                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4046                         .module_name      = "dib0700",
4047                         .rc_query         = dib0700_rc_query_old_firmware,
4048                         .allowed_protos   = RC_BIT_RC5 |
4049                                             RC_BIT_RC6_MCE |
4050                                             RC_BIT_NEC,
4051                         .change_protocol = dib0700_change_protocol,
4052                 },
4053         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4054
4055                 .num_adapters = 1,
4056                 .adapter = {
4057                         {
4058                         DIB0700_NUM_FRONTENDS(1),
4059                         .fe = {{
4060                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4061                                 .pid_filter_count = 32,
4062                                 .pid_filter       = stk70x0p_pid_filter,
4063                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4064                                 .frontend_attach  = stk7070p_frontend_attach,
4065                                 .tuner_attach     = dib7070p_tuner_attach,
4066
4067                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4068                         }},
4069                         },
4070                 },
4071
4072                 .num_device_descs = 12,
4073                 .devices = {
4074                         {   "DiBcom STK7070P reference design",
4075                                 { &dib0700_usb_id_table[15], NULL },
4076                                 { NULL },
4077                         },
4078                         {   "Pinnacle PCTV DVB-T Flash Stick",
4079                                 { &dib0700_usb_id_table[16], NULL },
4080                                 { NULL },
4081                         },
4082                         {   "Artec T14BR DVB-T",
4083                                 { &dib0700_usb_id_table[22], NULL },
4084                                 { NULL },
4085                         },
4086                         {   "ASUS My Cinema U3100 Mini DVBT Tuner",
4087                                 { &dib0700_usb_id_table[24], NULL },
4088                                 { NULL },
4089                         },
4090                         {   "Hauppauge Nova-T Stick",
4091                                 { &dib0700_usb_id_table[25], NULL },
4092                                 { NULL },
4093                         },
4094                         {   "Hauppauge Nova-T MyTV.t",
4095                                 { &dib0700_usb_id_table[26], NULL },
4096                                 { NULL },
4097                         },
4098                         {   "Pinnacle PCTV 72e",
4099                                 { &dib0700_usb_id_table[29], NULL },
4100                                 { NULL },
4101                         },
4102                         {   "Pinnacle PCTV 73e",
4103                                 { &dib0700_usb_id_table[30], NULL },
4104                                 { NULL },
4105                         },
4106                         {   "Elgato EyeTV DTT",
4107                                 { &dib0700_usb_id_table[49], NULL },
4108                                 { NULL },
4109                         },
4110                         {   "Yuan PD378S",
4111                                 { &dib0700_usb_id_table[45], NULL },
4112                                 { NULL },
4113                         },
4114                         {   "Elgato EyeTV Dtt Dlx PD378S",
4115                                 { &dib0700_usb_id_table[50], NULL },
4116                                 { NULL },
4117                         },
4118                         {   "Elgato EyeTV DTT rev. 2",
4119                                 { &dib0700_usb_id_table[80], NULL },
4120                                 { NULL },
4121                         },
4122                 },
4123
4124                 .rc.core = {
4125                         .rc_interval      = DEFAULT_RC_INTERVAL,
4126                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4127                         .module_name      = "dib0700",
4128                         .rc_query         = dib0700_rc_query_old_firmware,
4129                         .allowed_protos   = RC_BIT_RC5 |
4130                                             RC_BIT_RC6_MCE |
4131                                             RC_BIT_NEC,
4132                         .change_protocol  = dib0700_change_protocol,
4133                 },
4134         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4135
4136                 .num_adapters = 1,
4137                 .adapter = {
4138                         {
4139                         DIB0700_NUM_FRONTENDS(1),
4140                         .fe = {{
4141                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4142                                 .pid_filter_count = 32,
4143                                 .pid_filter       = stk70x0p_pid_filter,
4144                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4145                                 .frontend_attach  = stk7070p_frontend_attach,
4146                                 .tuner_attach     = dib7070p_tuner_attach,
4147
4148                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4149                         }},
4150                         },
4151                 },
4152
4153                 .num_device_descs = 3,
4154                 .devices = {
4155                         {   "Pinnacle PCTV 73A",
4156                                 { &dib0700_usb_id_table[56], NULL },
4157                                 { NULL },
4158                         },
4159                         {   "Pinnacle PCTV 73e SE",
4160                                 { &dib0700_usb_id_table[57], &dib0700_usb_id_table[65], NULL },
4161                                 { NULL },
4162                         },
4163                         {   "Pinnacle PCTV 282e",
4164                                 { &dib0700_usb_id_table[58], &dib0700_usb_id_table[66], NULL },
4165                                 { NULL },
4166                         },
4167                 },
4168
4169                 .rc.core = {
4170                         .rc_interval      = DEFAULT_RC_INTERVAL,
4171                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4172                         .module_name      = "dib0700",
4173                         .rc_query         = dib0700_rc_query_old_firmware,
4174                         .allowed_protos   = RC_BIT_RC5 |
4175                                             RC_BIT_RC6_MCE |
4176                                             RC_BIT_NEC,
4177                         .change_protocol  = dib0700_change_protocol,
4178                 },
4179         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4180
4181                 .num_adapters = 2,
4182                 .adapter = {
4183                         {
4184                         DIB0700_NUM_FRONTENDS(1),
4185                         .fe = {{
4186                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4187                                 .pid_filter_count = 32,
4188                                 .pid_filter       = stk70x0p_pid_filter,
4189                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4190                                 .frontend_attach  = novatd_frontend_attach,
4191                                 .tuner_attach     = dib7070p_tuner_attach,
4192
4193                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4194                         }},
4195                         }, {
4196                         DIB0700_NUM_FRONTENDS(1),
4197                         .fe = {{
4198                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4199                                 .pid_filter_count = 32,
4200                                 .pid_filter       = stk70x0p_pid_filter,
4201                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4202                                 .frontend_attach  = novatd_frontend_attach,
4203                                 .tuner_attach     = dib7070p_tuner_attach,
4204
4205                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4206                         }},
4207                         }
4208                 },
4209
4210                 .num_device_descs = 3,
4211                 .devices = {
4212                         {   "Hauppauge Nova-TD Stick (52009)",
4213                                 { &dib0700_usb_id_table[35], NULL },
4214                                 { NULL },
4215                         },
4216                         {   "PCTV 2002e",
4217                                 { &dib0700_usb_id_table[81], NULL },
4218                                 { NULL },
4219                         },
4220                         {   "PCTV 2002e SE",
4221                                 { &dib0700_usb_id_table[82], NULL },
4222                                 { NULL },
4223                         },
4224                 },
4225
4226                 .rc.core = {
4227                         .rc_interval      = DEFAULT_RC_INTERVAL,
4228                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4229                         .module_name      = "dib0700",
4230                         .rc_query         = dib0700_rc_query_old_firmware,
4231                         .allowed_protos   = RC_BIT_RC5 |
4232                                             RC_BIT_RC6_MCE |
4233                                             RC_BIT_NEC,
4234                         .change_protocol = dib0700_change_protocol,
4235                 },
4236         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4237
4238                 .num_adapters = 2,
4239                 .adapter = {
4240                         {
4241                         DIB0700_NUM_FRONTENDS(1),
4242                         .fe = {{
4243                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4244                                 .pid_filter_count = 32,
4245                                 .pid_filter       = stk70x0p_pid_filter,
4246                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4247                                 .frontend_attach  = stk7070pd_frontend_attach0,
4248                                 .tuner_attach     = dib7070p_tuner_attach,
4249
4250                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4251                         }},
4252                         }, {
4253                         DIB0700_NUM_FRONTENDS(1),
4254                         .fe = {{
4255                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4256                                 .pid_filter_count = 32,
4257                                 .pid_filter       = stk70x0p_pid_filter,
4258                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4259                                 .frontend_attach  = stk7070pd_frontend_attach1,
4260                                 .tuner_attach     = dib7070p_tuner_attach,
4261
4262                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4263                         }},
4264                         }
4265                 },
4266
4267                 .num_device_descs = 5,
4268                 .devices = {
4269                         {   "DiBcom STK7070PD reference design",
4270                                 { &dib0700_usb_id_table[17], NULL },
4271                                 { NULL },
4272                         },
4273                         {   "Pinnacle PCTV Dual DVB-T Diversity Stick",
4274                                 { &dib0700_usb_id_table[18], NULL },
4275                                 { NULL },
4276                         },
4277                         {   "Hauppauge Nova-TD-500 (84xxx)",
4278                                 { &dib0700_usb_id_table[36], NULL },
4279                                 { NULL },
4280                         },
4281                         {  "Terratec Cinergy DT USB XS Diversity/ T5",
4282                                 { &dib0700_usb_id_table[43],
4283                                         &dib0700_usb_id_table[53], NULL},
4284                                 { NULL },
4285                         },
4286                         {  "Sony PlayTV",
4287                                 { &dib0700_usb_id_table[44], NULL },
4288                                 { NULL },
4289                         },
4290                 },
4291
4292                 .rc.core = {
4293                         .rc_interval      = DEFAULT_RC_INTERVAL,
4294                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4295                         .module_name      = "dib0700",
4296                         .rc_query         = dib0700_rc_query_old_firmware,
4297                         .allowed_protos   = RC_BIT_RC5 |
4298                                             RC_BIT_RC6_MCE |
4299                                             RC_BIT_NEC,
4300                         .change_protocol = dib0700_change_protocol,
4301                 },
4302         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4303
4304                 .num_adapters = 2,
4305                 .adapter = {
4306                         {
4307                         DIB0700_NUM_FRONTENDS(1),
4308                         .fe = {{
4309                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4310                                 .pid_filter_count = 32,
4311                                 .pid_filter       = stk70x0p_pid_filter,
4312                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4313                                 .frontend_attach  = stk7070pd_frontend_attach0,
4314                                 .tuner_attach     = dib7070p_tuner_attach,
4315
4316                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4317                         }},
4318                         }, {
4319                         DIB0700_NUM_FRONTENDS(1),
4320                         .fe = {{
4321                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4322                                 .pid_filter_count = 32,
4323                                 .pid_filter       = stk70x0p_pid_filter,
4324                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4325                                 .frontend_attach  = stk7070pd_frontend_attach1,
4326                                 .tuner_attach     = dib7070p_tuner_attach,
4327
4328                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4329                         }},
4330                         }
4331                 },
4332
4333                 .num_device_descs = 1,
4334                 .devices = {
4335                         {   "Elgato EyeTV Diversity",
4336                                 { &dib0700_usb_id_table[68], NULL },
4337                                 { NULL },
4338                         },
4339                 },
4340
4341                 .rc.core = {
4342                         .rc_interval      = DEFAULT_RC_INTERVAL,
4343                         .rc_codes         = RC_MAP_DIB0700_NEC_TABLE,
4344                         .module_name      = "dib0700",
4345                         .rc_query         = dib0700_rc_query_old_firmware,
4346                         .allowed_protos   = RC_BIT_RC5 |
4347                                             RC_BIT_RC6_MCE |
4348                                             RC_BIT_NEC,
4349                         .change_protocol  = dib0700_change_protocol,
4350                 },
4351         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4352
4353                 .num_adapters = 1,
4354                 .adapter = {
4355                         {
4356                         DIB0700_NUM_FRONTENDS(1),
4357                         .fe = {{
4358                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4359                                 .pid_filter_count = 32,
4360                                 .pid_filter       = stk70x0p_pid_filter,
4361                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4362                                 .frontend_attach  = stk7700ph_frontend_attach,
4363                                 .tuner_attach     = stk7700ph_tuner_attach,
4364
4365                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4366                         }},
4367                         },
4368                 },
4369
4370                 .num_device_descs = 9,
4371                 .devices = {
4372                         {   "Terratec Cinergy HT USB XE",
4373                                 { &dib0700_usb_id_table[27], NULL },
4374                                 { NULL },
4375                         },
4376                         {   "Pinnacle Expresscard 320cx",
4377                                 { &dib0700_usb_id_table[28], NULL },
4378                                 { NULL },
4379                         },
4380                         {   "Terratec Cinergy HT Express",
4381                                 { &dib0700_usb_id_table[32], NULL },
4382                                 { NULL },
4383                         },
4384                         {   "Gigabyte U8000-RH",
4385                                 { &dib0700_usb_id_table[37], NULL },
4386                                 { NULL },
4387                         },
4388                         {   "YUAN High-Tech STK7700PH",
4389                                 { &dib0700_usb_id_table[38], NULL },
4390                                 { NULL },
4391                         },
4392                         {   "Asus My Cinema-U3000Hybrid",
4393                                 { &dib0700_usb_id_table[39], NULL },
4394                                 { NULL },
4395                         },
4396                         {   "YUAN High-Tech MC770",
4397                                 { &dib0700_usb_id_table[48], NULL },
4398                                 { NULL },
4399                         },
4400                         {   "Leadtek WinFast DTV Dongle H",
4401                                 { &dib0700_usb_id_table[51], NULL },
4402                                 { NULL },
4403                         },
4404                         {   "YUAN High-Tech STK7700D",
4405                                 { &dib0700_usb_id_table[54], NULL },
4406                                 { NULL },
4407                         },
4408                 },
4409
4410                 .rc.core = {
4411                         .rc_interval      = DEFAULT_RC_INTERVAL,
4412                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4413                         .module_name      = "dib0700",
4414                         .rc_query         = dib0700_rc_query_old_firmware,
4415                         .allowed_protos   = RC_BIT_RC5 |
4416                                             RC_BIT_RC6_MCE |
4417                                             RC_BIT_NEC,
4418                         .change_protocol  = dib0700_change_protocol,
4419                 },
4420         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4421                 .num_adapters = 1,
4422                 .adapter = {
4423                         {
4424                         DIB0700_NUM_FRONTENDS(1),
4425                         .fe = {{
4426                                 .frontend_attach  = s5h1411_frontend_attach,
4427                                 .tuner_attach     = xc5000_tuner_attach,
4428
4429                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4430                         }},
4431                         },
4432                 },
4433
4434                 .num_device_descs = 2,
4435                 .devices = {
4436                         {   "Pinnacle PCTV HD Pro USB Stick",
4437                                 { &dib0700_usb_id_table[40], NULL },
4438                                 { NULL },
4439                         },
4440                         {   "Pinnacle PCTV HD USB Stick",
4441                                 { &dib0700_usb_id_table[41], NULL },
4442                                 { NULL },
4443                         },
4444                 },
4445
4446                 .rc.core = {
4447                         .rc_interval      = DEFAULT_RC_INTERVAL,
4448                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4449                         .module_name      = "dib0700",
4450                         .rc_query         = dib0700_rc_query_old_firmware,
4451                         .allowed_protos   = RC_BIT_RC5 |
4452                                             RC_BIT_RC6_MCE |
4453                                             RC_BIT_NEC,
4454                         .change_protocol  = dib0700_change_protocol,
4455                 },
4456         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4457                 .num_adapters = 1,
4458                 .adapter = {
4459                         {
4460                         DIB0700_NUM_FRONTENDS(1),
4461                         .fe = {{
4462                                 .frontend_attach  = lgdt3305_frontend_attach,
4463                                 .tuner_attach     = mxl5007t_tuner_attach,
4464
4465                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4466                         }},
4467                         },
4468                 },
4469
4470                 .num_device_descs = 2,
4471                 .devices = {
4472                         {   "Hauppauge ATSC MiniCard (B200)",
4473                                 { &dib0700_usb_id_table[46], NULL },
4474                                 { NULL },
4475                         },
4476                         {   "Hauppauge ATSC MiniCard (B210)",
4477                                 { &dib0700_usb_id_table[47], NULL },
4478                                 { NULL },
4479                         },
4480                 },
4481         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4482
4483                 .num_adapters = 1,
4484                 .adapter = {
4485                         {
4486                         DIB0700_NUM_FRONTENDS(1),
4487                         .fe = {{
4488                                 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4489                                 .pid_filter_count = 32,
4490                                 .pid_filter       = stk70x0p_pid_filter,
4491                                 .pid_filter_ctrl  = stk70x0p_pid_filter_ctrl,
4492                                 .frontend_attach  = stk7770p_frontend_attach,
4493                                 .tuner_attach     = dib7770p_tuner_attach,
4494
4495                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4496                         }},
4497                         },
4498                 },
4499
4500                 .num_device_descs = 4,
4501                 .devices = {
4502                         {   "DiBcom STK7770P reference design",
4503                                 { &dib0700_usb_id_table[59], NULL },
4504                                 { NULL },
4505                         },
4506                         {   "Terratec Cinergy T USB XXS (HD)/ T3",
4507                                 { &dib0700_usb_id_table[33],
4508                                         &dib0700_usb_id_table[52],
4509                                         &dib0700_usb_id_table[60], NULL},
4510                                 { NULL },
4511                         },
4512                         {   "TechniSat AirStar TeleStick 2",
4513                                 { &dib0700_usb_id_table[74], NULL },
4514                                 { NULL },
4515                         },
4516                         {   "Medion CTX1921 DVB-T USB",
4517                                 { &dib0700_usb_id_table[75], NULL },
4518                                 { NULL },
4519                         },
4520                 },
4521
4522                 .rc.core = {
4523                         .rc_interval      = DEFAULT_RC_INTERVAL,
4524                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4525                         .module_name      = "dib0700",
4526                         .rc_query         = dib0700_rc_query_old_firmware,
4527                         .allowed_protos   = RC_BIT_RC5 |
4528                                             RC_BIT_RC6_MCE |
4529                                             RC_BIT_NEC,
4530                         .change_protocol  = dib0700_change_protocol,
4531                 },
4532         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4533                 .num_adapters = 1,
4534                 .adapter = {
4535                         {
4536                         DIB0700_NUM_FRONTENDS(1),
4537                         .fe = {{
4538                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4539                                 .pid_filter_count = 32,
4540                                 .pid_filter = stk80xx_pid_filter,
4541                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4542                                 .frontend_attach  = stk807x_frontend_attach,
4543                                 .tuner_attach     = dib807x_tuner_attach,
4544
4545                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4546                         }},
4547                         },
4548                 },
4549
4550                 .num_device_descs = 3,
4551                 .devices = {
4552                         {   "DiBcom STK807xP reference design",
4553                                 { &dib0700_usb_id_table[62], NULL },
4554                                 { NULL },
4555                         },
4556                         {   "Prolink Pixelview SBTVD",
4557                                 { &dib0700_usb_id_table[63], NULL },
4558                                 { NULL },
4559                         },
4560                         {   "EvolutePC TVWay+",
4561                                 { &dib0700_usb_id_table[64], NULL },
4562                                 { NULL },
4563                         },
4564                 },
4565
4566                 .rc.core = {
4567                         .rc_interval      = DEFAULT_RC_INTERVAL,
4568                         .rc_codes         = RC_MAP_DIB0700_NEC_TABLE,
4569                         .module_name      = "dib0700",
4570                         .rc_query         = dib0700_rc_query_old_firmware,
4571                         .allowed_protos   = RC_BIT_RC5 |
4572                                             RC_BIT_RC6_MCE |
4573                                             RC_BIT_NEC,
4574                         .change_protocol  = dib0700_change_protocol,
4575                 },
4576         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4577                 .num_adapters = 2,
4578                 .adapter = {
4579                         {
4580                         DIB0700_NUM_FRONTENDS(1),
4581                         .fe = {{
4582                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4583                                 .pid_filter_count = 32,
4584                                 .pid_filter = stk80xx_pid_filter,
4585                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4586                                 .frontend_attach  = stk807xpvr_frontend_attach0,
4587                                 .tuner_attach     = dib807x_tuner_attach,
4588
4589                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4590                         }},
4591                         },
4592                         {
4593                         DIB0700_NUM_FRONTENDS(1),
4594                         .fe = {{
4595                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4596                                 .pid_filter_count = 32,
4597                                 .pid_filter = stk80xx_pid_filter,
4598                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4599                                 .frontend_attach  = stk807xpvr_frontend_attach1,
4600                                 .tuner_attach     = dib807x_tuner_attach,
4601
4602                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4603                         }},
4604                         },
4605                 },
4606
4607                 .num_device_descs = 1,
4608                 .devices = {
4609                         {   "DiBcom STK807xPVR reference design",
4610                                 { &dib0700_usb_id_table[61], NULL },
4611                                 { NULL },
4612                         },
4613                 },
4614
4615                 .rc.core = {
4616                         .rc_interval      = DEFAULT_RC_INTERVAL,
4617                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4618                         .module_name      = "dib0700",
4619                         .rc_query         = dib0700_rc_query_old_firmware,
4620                         .allowed_protos   = RC_BIT_RC5 |
4621                                             RC_BIT_RC6_MCE |
4622                                             RC_BIT_NEC,
4623                         .change_protocol  = dib0700_change_protocol,
4624                 },
4625         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4626                 .num_adapters = 1,
4627                 .adapter = {
4628                         {
4629                         DIB0700_NUM_FRONTENDS(1),
4630                         .fe = {{
4631                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4632                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4633                                 .pid_filter_count = 32,
4634                                 .pid_filter = stk80xx_pid_filter,
4635                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4636                                 .frontend_attach  = stk809x_frontend_attach,
4637                                 .tuner_attach     = dib809x_tuner_attach,
4638
4639                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4640                         }},
4641                         },
4642                 },
4643
4644                 .num_device_descs = 1,
4645                 .devices = {
4646                         {   "DiBcom STK8096GP reference design",
4647                                 { &dib0700_usb_id_table[67], NULL },
4648                                 { NULL },
4649                         },
4650                 },
4651
4652                 .rc.core = {
4653                         .rc_interval      = DEFAULT_RC_INTERVAL,
4654                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4655                         .module_name      = "dib0700",
4656                         .rc_query         = dib0700_rc_query_old_firmware,
4657                         .allowed_protos   = RC_BIT_RC5 |
4658                                             RC_BIT_RC6_MCE |
4659                                             RC_BIT_NEC,
4660                         .change_protocol  = dib0700_change_protocol,
4661                 },
4662         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4663                 .num_adapters = 1,
4664                 .adapter = {
4665                         {
4666                         DIB0700_NUM_FRONTENDS(1),
4667                         .fe = {{
4668                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4669                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4670                                 .pid_filter_count = 32,
4671                                 .pid_filter = dib90x0_pid_filter,
4672                                 .pid_filter_ctrl = dib90x0_pid_filter_ctrl,
4673                                 .frontend_attach  = stk9090m_frontend_attach,
4674                                 .tuner_attach     = dib9090_tuner_attach,
4675
4676                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4677                         }},
4678                         },
4679                 },
4680
4681                 .num_device_descs = 1,
4682                 .devices = {
4683                         {   "DiBcom STK9090M reference design",
4684                                 { &dib0700_usb_id_table[69], NULL },
4685                                 { NULL },
4686                         },
4687                 },
4688
4689                 .rc.core = {
4690                         .rc_interval      = DEFAULT_RC_INTERVAL,
4691                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4692                         .module_name      = "dib0700",
4693                         .rc_query         = dib0700_rc_query_old_firmware,
4694                         .allowed_protos   = RC_BIT_RC5 |
4695                                             RC_BIT_RC6_MCE |
4696                                             RC_BIT_NEC,
4697                         .change_protocol  = dib0700_change_protocol,
4698                 },
4699         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4700                 .num_adapters = 1,
4701                 .adapter = {
4702                         {
4703                         DIB0700_NUM_FRONTENDS(1),
4704                         .fe = {{
4705                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4706                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4707                                 .pid_filter_count = 32,
4708                                 .pid_filter = stk80xx_pid_filter,
4709                                 .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4710                                 .frontend_attach  = nim8096md_frontend_attach,
4711                                 .tuner_attach     = nim8096md_tuner_attach,
4712
4713                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4714                         }},
4715                         },
4716                 },
4717
4718                 .num_device_descs = 1,
4719                 .devices = {
4720                         {   "DiBcom NIM8096MD reference design",
4721                                 { &dib0700_usb_id_table[70], NULL },
4722                                 { NULL },
4723                         },
4724                 },
4725
4726                 .rc.core = {
4727                         .rc_interval      = DEFAULT_RC_INTERVAL,
4728                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4729                         .module_name      = "dib0700",
4730                         .rc_query         = dib0700_rc_query_old_firmware,
4731                         .allowed_protos   = RC_BIT_RC5 |
4732                                             RC_BIT_RC6_MCE |
4733                                             RC_BIT_NEC,
4734                         .change_protocol  = dib0700_change_protocol,
4735                 },
4736         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4737                 .num_adapters = 1,
4738                 .adapter = {
4739                         {
4740                         DIB0700_NUM_FRONTENDS(1),
4741                         .fe = {{
4742                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4743                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4744                                 .pid_filter_count = 32,
4745                                 .pid_filter = dib90x0_pid_filter,
4746                                 .pid_filter_ctrl = dib90x0_pid_filter_ctrl,
4747                                 .frontend_attach  = nim9090md_frontend_attach,
4748                                 .tuner_attach     = nim9090md_tuner_attach,
4749
4750                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4751                         }},
4752                         },
4753                 },
4754
4755                 .num_device_descs = 1,
4756                 .devices = {
4757                         {   "DiBcom NIM9090MD reference design",
4758                                 { &dib0700_usb_id_table[71], NULL },
4759                                 { NULL },
4760                         },
4761                 },
4762
4763                 .rc.core = {
4764                         .rc_interval      = DEFAULT_RC_INTERVAL,
4765                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4766                         .module_name      = "dib0700",
4767                         .rc_query         = dib0700_rc_query_old_firmware,
4768                         .allowed_protos   = RC_BIT_RC5 |
4769                                             RC_BIT_RC6_MCE |
4770                                             RC_BIT_NEC,
4771                         .change_protocol  = dib0700_change_protocol,
4772                 },
4773         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4774                 .num_adapters = 1,
4775                 .adapter = {
4776                         {
4777                         DIB0700_NUM_FRONTENDS(1),
4778                         .fe = {{
4779                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4780                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4781                                 .pid_filter_count = 32,
4782                                 .pid_filter = stk70x0p_pid_filter,
4783                                 .pid_filter_ctrl = stk70x0p_pid_filter_ctrl,
4784                                 .frontend_attach  = nim7090_frontend_attach,
4785                                 .tuner_attach     = nim7090_tuner_attach,
4786
4787                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4788                         }},
4789                         },
4790                 },
4791
4792                 .num_device_descs = 1,
4793                 .devices = {
4794                         {   "DiBcom NIM7090 reference design",
4795                                 { &dib0700_usb_id_table[72], NULL },
4796                                 { NULL },
4797                         },
4798                 },
4799
4800                 .rc.core = {
4801                         .rc_interval      = DEFAULT_RC_INTERVAL,
4802                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4803                         .module_name      = "dib0700",
4804                         .rc_query         = dib0700_rc_query_old_firmware,
4805                         .allowed_protos   = RC_BIT_RC5 |
4806                                             RC_BIT_RC6_MCE |
4807                                             RC_BIT_NEC,
4808                         .change_protocol  = dib0700_change_protocol,
4809                 },
4810         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4811                 .num_adapters = 2,
4812                 .adapter = {
4813                         {
4814                         DIB0700_NUM_FRONTENDS(1),
4815                         .fe = {{
4816                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4817                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4818                                 .pid_filter_count = 32,
4819                                 .pid_filter = stk70x0p_pid_filter,
4820                                 .pid_filter_ctrl = stk70x0p_pid_filter_ctrl,
4821                                 .frontend_attach  = tfe7090pvr_frontend0_attach,
4822                                 .tuner_attach     = tfe7090pvr_tuner0_attach,
4823
4824                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4825                         }},
4826                         },
4827                         {
4828                         DIB0700_NUM_FRONTENDS(1),
4829                         .fe = {{
4830                                 .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4831                                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4832                                 .pid_filter_count = 32,
4833                                 .pid_filter = stk70x0p_pid_filter,
4834                                 .pid_filter_ctrl = stk70x0p_pid_filter_ctrl,
4835                                 .frontend_attach  = tfe7090pvr_frontend1_attach,
4836                                 .tuner_attach     = tfe7090pvr_tuner1_attach,
4837
4838                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4839                         }},
4840                         },
4841                 },
4842
4843                 .num_device_descs = 1,
4844                 .devices = {
4845                         {   "DiBcom TFE7090PVR reference design",
4846                                 { &dib0700_usb_id_table[73], NULL },
4847                                 { NULL },
4848                         },
4849                 },
4850
4851                 .rc.core = {
4852                         .rc_interval      = DEFAULT_RC_INTERVAL,
4853                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4854                         .module_name      = "dib0700",
4855                         .rc_query         = dib0700_rc_query_old_firmware,
4856                         .allowed_protos   = RC_BIT_RC5 |
4857                                             RC_BIT_RC6_MCE |
4858                                             RC_BIT_NEC,
4859                         .change_protocol  = dib0700_change_protocol,
4860                 },
4861         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4862                 .num_adapters = 1,
4863                 .adapter = {
4864                         {
4865                         DIB0700_NUM_FRONTENDS(1),
4866                         .fe = {{
4867                                 .frontend_attach  = pctv340e_frontend_attach,
4868                                 .tuner_attach     = xc4000_tuner_attach,
4869
4870                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4871                         }},
4872                         },
4873                 },
4874
4875                 .num_device_descs = 2,
4876                 .devices = {
4877                         {   "Pinnacle PCTV 340e HD Pro USB Stick",
4878                                 { &dib0700_usb_id_table[76], NULL },
4879                                 { NULL },
4880                         },
4881                         {   "Pinnacle PCTV Hybrid Stick Solo",
4882                                 { &dib0700_usb_id_table[77], NULL },
4883                                 { NULL },
4884                         },
4885                 },
4886                 .rc.core = {
4887                         .rc_interval      = DEFAULT_RC_INTERVAL,
4888                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4889                         .module_name      = "dib0700",
4890                         .rc_query         = dib0700_rc_query_old_firmware,
4891                         .allowed_protos   = RC_BIT_RC5 |
4892                                             RC_BIT_RC6_MCE |
4893                                             RC_BIT_NEC,
4894                         .change_protocol  = dib0700_change_protocol,
4895                 },
4896         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4897                 .num_adapters = 1,
4898                 .adapter = {
4899                         {
4900                                 DIB0700_NUM_FRONTENDS(1),
4901                                 .fe = {{
4902                                         .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4903                                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4904                                         .pid_filter_count = 32,
4905                                         .pid_filter = stk70x0p_pid_filter,
4906                                         .pid_filter_ctrl = stk70x0p_pid_filter_ctrl,
4907                                         .frontend_attach  = tfe7790p_frontend_attach,
4908                                         .tuner_attach     = tfe7790p_tuner_attach,
4909
4910                                         DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
4911                                 } },
4912                         },
4913                 },
4914
4915                 .num_device_descs = 1,
4916                 .devices = {
4917                         {   "DiBcom TFE7790P reference design",
4918                                 { &dib0700_usb_id_table[78], NULL },
4919                                 { NULL },
4920                         },
4921                 },
4922
4923                 .rc.core = {
4924                         .rc_interval      = DEFAULT_RC_INTERVAL,
4925                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4926                         .module_name      = "dib0700",
4927                         .rc_query         = dib0700_rc_query_old_firmware,
4928                         .allowed_protos   = RC_BIT_RC5 |
4929                                             RC_BIT_RC6_MCE |
4930                                             RC_BIT_NEC,
4931                         .change_protocol  = dib0700_change_protocol,
4932                 },
4933         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
4934                 .num_adapters = 1,
4935                 .adapter = {
4936                         {
4937                                 DIB0700_NUM_FRONTENDS(1),
4938                                 .fe = {{
4939                                         .caps  = DVB_USB_ADAP_HAS_PID_FILTER |
4940                                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
4941                                         .pid_filter_count = 32,
4942                                         .pid_filter = stk80xx_pid_filter,
4943                                         .pid_filter_ctrl = stk80xx_pid_filter_ctrl,
4944                                         .frontend_attach  = tfe8096p_frontend_attach,
4945                                         .tuner_attach     = tfe8096p_tuner_attach,
4946
4947                                         DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
4948
4949                                 } },
4950                         },
4951                 },
4952
4953                 .num_device_descs = 1,
4954                 .devices = {
4955                         {   "DiBcom TFE8096P reference design",
4956                                 { &dib0700_usb_id_table[79], NULL },
4957                                 { NULL },
4958                         },
4959                 },
4960
4961                 .rc.core = {
4962                         .rc_interval      = DEFAULT_RC_INTERVAL,
4963                         .rc_codes         = RC_MAP_DIB0700_RC5_TABLE,
4964                         .module_name      = "dib0700",
4965                         .rc_query         = dib0700_rc_query_old_firmware,
4966                         .allowed_protos   = RC_BIT_RC5 |
4967                                             RC_BIT_RC6_MCE |
4968                                             RC_BIT_NEC,
4969                         .change_protocol  = dib0700_change_protocol,
4970                 },
4971         },
4972 };
4973
4974 int dib0700_device_count = ARRAY_SIZE(dib0700_devices);