V4L/DVB (8972): initial driver for af9015 chipset
[linux-2.6] / drivers / media / dvb / 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-7 DiBcom, SA
8  */
9 #include "dib0700.h"
10
11 #include "dib3000mc.h"
12 #include "dib7000m.h"
13 #include "dib7000p.h"
14 #include "mt2060.h"
15 #include "mt2266.h"
16 #include "tuner-xc2028.h"
17 #include "dib0070.h"
18
19 static int force_lna_activation;
20 module_param(force_lna_activation, int, 0644);
21 MODULE_PARM_DESC(force_lna_activation, "force the activation of Low-Noise-Amplifyer(s) (LNA), "
22                 "if applicable for the device (default: 0=automatic/off).");
23
24 struct dib0700_adapter_state {
25         int (*set_param_save) (struct dvb_frontend *, struct dvb_frontend_parameters *);
26 };
27
28 /* Hauppauge Nova-T 500 (aka Bristol)
29  *  has a LNA on GPIO0 which is enabled by setting 1 */
30 static struct mt2060_config bristol_mt2060_config[2] = {
31         {
32                 .i2c_address = 0x60,
33                 .clock_out   = 3,
34         }, {
35                 .i2c_address = 0x61,
36         }
37 };
38
39 static struct dibx000_agc_config bristol_dib3000p_mt2060_agc_config = {
40         .band_caps = BAND_VHF | BAND_UHF,
41         .setup     = (1 << 8) | (5 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (2 << 0),
42
43         .agc1_max = 42598,
44         .agc1_min = 17694,
45         .agc2_max = 45875,
46         .agc2_min = 0,
47
48         .agc1_pt1 = 0,
49         .agc1_pt2 = 59,
50
51         .agc1_slope1 = 0,
52         .agc1_slope2 = 69,
53
54         .agc2_pt1 = 0,
55         .agc2_pt2 = 59,
56
57         .agc2_slope1 = 111,
58         .agc2_slope2 = 28,
59 };
60
61 static struct dib3000mc_config bristol_dib3000mc_config[2] = {
62         {       .agc          = &bristol_dib3000p_mt2060_agc_config,
63                 .max_time     = 0x196,
64                 .ln_adc_level = 0x1cc7,
65                 .output_mpeg2_in_188_bytes = 1,
66         },
67         {       .agc          = &bristol_dib3000p_mt2060_agc_config,
68                 .max_time     = 0x196,
69                 .ln_adc_level = 0x1cc7,
70                 .output_mpeg2_in_188_bytes = 1,
71         }
72 };
73
74 static int bristol_frontend_attach(struct dvb_usb_adapter *adap)
75 {
76         struct dib0700_state *st = adap->dev->priv;
77         if (adap->id == 0) {
78                 dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 0); msleep(10);
79                 dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 1); msleep(10);
80                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0); msleep(10);
81                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1); msleep(10);
82
83                 if (force_lna_activation)
84                         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
85                 else
86                         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 0);
87
88                 if (dib3000mc_i2c_enumeration(&adap->dev->i2c_adap, 2, DEFAULT_DIB3000P_I2C_ADDRESS, bristol_dib3000mc_config) != 0) {
89                         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0); msleep(10);
90                         return -ENODEV;
91                 }
92         }
93         st->mt2060_if1[adap->id] = 1220;
94         return (adap->fe = dvb_attach(dib3000mc_attach, &adap->dev->i2c_adap,
95                 (10 + adap->id) << 1, &bristol_dib3000mc_config[adap->id])) == NULL ? -ENODEV : 0;
96 }
97
98 static int eeprom_read(struct i2c_adapter *adap,u8 adrs,u8 *pval)
99 {
100         struct i2c_msg msg[2] = {
101                 { .addr = 0x50, .flags = 0,        .buf = &adrs, .len = 1 },
102                 { .addr = 0x50, .flags = I2C_M_RD, .buf = pval,  .len = 1 },
103         };
104         if (i2c_transfer(adap, msg, 2) != 2) return -EREMOTEIO;
105         return 0;
106 }
107
108 static int bristol_tuner_attach(struct dvb_usb_adapter *adap)
109 {
110         struct i2c_adapter *prim_i2c = &adap->dev->i2c_adap;
111         struct i2c_adapter *tun_i2c = dib3000mc_get_tuner_i2c_master(adap->fe, 1);
112         s8 a;
113         int if1=1220;
114         if (adap->dev->udev->descriptor.idVendor  == cpu_to_le16(USB_VID_HAUPPAUGE) &&
115                 adap->dev->udev->descriptor.idProduct == cpu_to_le16(USB_PID_HAUPPAUGE_NOVA_T_500_2)) {
116                 if (!eeprom_read(prim_i2c,0x59 + adap->id,&a)) if1=1220+a;
117         }
118         return dvb_attach(mt2060_attach,adap->fe, tun_i2c,&bristol_mt2060_config[adap->id],
119                 if1) == NULL ? -ENODEV : 0;
120 }
121
122 /* STK7700D: Pinnacle/Terratec/Hauppauge Dual DVB-T Diversity */
123
124 /* MT226x */
125 static struct dibx000_agc_config stk7700d_7000p_mt2266_agc_config[2] = {
126         {
127                 BAND_UHF, // band_caps
128
129                 /* 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,
130                 * 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 */
131                 (0 << 15) | (0 << 14) | (1 << 11) | (1 << 10) | (1 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0), // setup
132
133                 1130,  // inv_gain
134                 21,  // time_stabiliz
135
136                 0,  // alpha_level
137                 118,  // thlock
138
139                 0,     // wbd_inv
140                 3530,  // wbd_ref
141                 1,     // wbd_sel
142                 0,     // wbd_alpha
143
144                 65535,  // agc1_max
145                 33770,  // agc1_min
146                 65535,  // agc2_max
147                 23592,  // agc2_min
148
149                 0,    // agc1_pt1
150                 62,   // agc1_pt2
151                 255,  // agc1_pt3
152                 64,   // agc1_slope1
153                 64,   // agc1_slope2
154                 132,  // agc2_pt1
155                 192,  // agc2_pt2
156                 80,   // agc2_slope1
157                 80,   // agc2_slope2
158
159                 17,  // alpha_mant
160                 27,  // alpha_exp
161                 23,  // beta_mant
162                 51,  // beta_exp
163
164                 1,  // perform_agc_softsplit
165         }, {
166                 BAND_VHF | BAND_LBAND, // band_caps
167
168                 /* 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,
169                 * 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 */
170                 (0 << 15) | (0 << 14) | (1 << 11) | (1 << 10) | (1 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0), // setup
171
172                 2372, // inv_gain
173                 21,   // time_stabiliz
174
175                 0,    // alpha_level
176                 118,  // thlock
177
178                 0,    // wbd_inv
179                 3530, // wbd_ref
180                 1,     // wbd_sel
181                 0,    // wbd_alpha
182
183                 65535, // agc1_max
184                 0,     // agc1_min
185                 65535, // agc2_max
186                 23592, // agc2_min
187
188                 0,    // agc1_pt1
189                 128,  // agc1_pt2
190                 128,  // agc1_pt3
191                 128,  // agc1_slope1
192                 0,    // agc1_slope2
193                 128,  // agc2_pt1
194                 253,  // agc2_pt2
195                 81,   // agc2_slope1
196                 0,    // agc2_slope2
197
198                 17,  // alpha_mant
199                 27,  // alpha_exp
200                 23,  // beta_mant
201                 51,  // beta_exp
202
203                 1,  // perform_agc_softsplit
204         }
205 };
206
207 static struct dibx000_bandwidth_config stk7700d_mt2266_pll_config = {
208         60000, 30000, // internal, sampling
209         1, 8, 3, 1, 0, // pll_cfg: prediv, ratio, range, reset, bypass
210         0, 0, 1, 1, 2, // misc: refdiv, bypclk_div, IO_CLK_en_core, ADClkSrc, modulo
211         (3 << 14) | (1 << 12) | (524 << 0), // sad_cfg: refsel, sel, freq_15k
212         0, // ifreq
213         20452225, // timf
214 };
215
216 static struct dib7000p_config stk7700d_dib7000p_mt2266_config[] = {
217         {       .output_mpeg2_in_188_bytes = 1,
218                 .hostbus_diversity = 1,
219                 .tuner_is_baseband = 1,
220
221                 .agc_config_count = 2,
222                 .agc = stk7700d_7000p_mt2266_agc_config,
223                 .bw  = &stk7700d_mt2266_pll_config,
224
225                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
226                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
227                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
228         },
229         {       .output_mpeg2_in_188_bytes = 1,
230                 .hostbus_diversity = 1,
231                 .tuner_is_baseband = 1,
232
233                 .agc_config_count = 2,
234                 .agc = stk7700d_7000p_mt2266_agc_config,
235                 .bw  = &stk7700d_mt2266_pll_config,
236
237                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
238                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
239                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
240         }
241 };
242
243 static struct mt2266_config stk7700d_mt2266_config[2] = {
244         {       .i2c_address = 0x60
245         },
246         {       .i2c_address = 0x60
247         }
248 };
249
250 static int stk7700P2_frontend_attach(struct dvb_usb_adapter *adap)
251 {
252         if (adap->id == 0) {
253                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
254                 msleep(10);
255                 dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
256                 dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
257                 dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
258                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
259                 msleep(10);
260                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
261                 msleep(10);
262                 dib7000p_i2c_enumeration(&adap->dev->i2c_adap,1,18,stk7700d_dib7000p_mt2266_config);
263         }
264
265         adap->fe = dvb_attach(dib7000p_attach, &adap->dev->i2c_adap,0x80+(adap->id << 1),
266                                 &stk7700d_dib7000p_mt2266_config[adap->id]);
267
268         return adap->fe == NULL ? -ENODEV : 0;
269 }
270
271 static int stk7700d_frontend_attach(struct dvb_usb_adapter *adap)
272 {
273         if (adap->id == 0) {
274                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
275                 msleep(10);
276                 dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
277                 dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
278                 dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
279                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
280                 msleep(10);
281                 dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
282                 msleep(10);
283                 dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
284                 dib7000p_i2c_enumeration(&adap->dev->i2c_adap,2,18,stk7700d_dib7000p_mt2266_config);
285         }
286
287         adap->fe = dvb_attach(dib7000p_attach, &adap->dev->i2c_adap,0x80+(adap->id << 1),
288                                 &stk7700d_dib7000p_mt2266_config[adap->id]);
289
290         return adap->fe == NULL ? -ENODEV : 0;
291 }
292
293 static int stk7700d_tuner_attach(struct dvb_usb_adapter *adap)
294 {
295         struct i2c_adapter *tun_i2c;
296         tun_i2c = dib7000p_get_i2c_master(adap->fe, DIBX000_I2C_INTERFACE_TUNER, 1);
297         return dvb_attach(mt2266_attach, adap->fe, tun_i2c,
298                 &stk7700d_mt2266_config[adap->id]) == NULL ? -ENODEV : 0;;
299 }
300
301 /* STK7700-PH: Digital/Analog Hybrid Tuner, e.h. Cinergy HT USB HE */
302 static struct dibx000_agc_config xc3028_agc_config = {
303         BAND_VHF | BAND_UHF,       /* band_caps */
304
305         /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=0,
306          * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
307          * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=2, P_agc_write=0 */
308         (0 << 15) | (0 << 14) | (0 << 11) | (0 << 10) | (0 << 9) | (0 << 8) |
309         (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0), /* setup */
310
311         712,    /* inv_gain */
312         21,     /* time_stabiliz */
313
314         0,      /* alpha_level */
315         118,    /* thlock */
316
317         0,      /* wbd_inv */
318         2867,   /* wbd_ref */
319         0,      /* wbd_sel */
320         2,      /* wbd_alpha */
321
322         0,      /* agc1_max */
323         0,      /* agc1_min */
324         39718,  /* agc2_max */
325         9930,   /* agc2_min */
326         0,      /* agc1_pt1 */
327         0,      /* agc1_pt2 */
328         0,      /* agc1_pt3 */
329         0,      /* agc1_slope1 */
330         0,      /* agc1_slope2 */
331         0,      /* agc2_pt1 */
332         128,    /* agc2_pt2 */
333         29,     /* agc2_slope1 */
334         29,     /* agc2_slope2 */
335
336         17,     /* alpha_mant */
337         27,     /* alpha_exp */
338         23,     /* beta_mant */
339         51,     /* beta_exp */
340
341         1,      /* perform_agc_softsplit */
342 };
343
344 /* PLL Configuration for COFDM BW_MHz = 8.00 with external clock = 30.00 */
345 static struct dibx000_bandwidth_config xc3028_bw_config = {
346         60000, 30000, /* internal, sampling */
347         1, 8, 3, 1, 0, /* pll_cfg: prediv, ratio, range, reset, bypass */
348         0, 0, 1, 1, 0, /* misc: refdiv, bypclk_div, IO_CLK_en_core, ADClkSrc,
349                           modulo */
350         (3 << 14) | (1 << 12) | (524 << 0), /* sad_cfg: refsel, sel, freq_15k */
351         (1 << 25) | 5816102, /* ifreq = 5.200000 MHz */
352         20452225, /* timf */
353         30000000, /* xtal_hz */
354 };
355
356 static struct dib7000p_config stk7700ph_dib7700_xc3028_config = {
357         .output_mpeg2_in_188_bytes = 1,
358         .tuner_is_baseband = 1,
359
360         .agc_config_count = 1,
361         .agc = &xc3028_agc_config,
362         .bw  = &xc3028_bw_config,
363
364         .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
365         .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
366         .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
367 };
368
369 static int stk7700ph_xc3028_callback(void *ptr, int command, int arg)
370 {
371         struct dvb_usb_adapter *adap = ptr;
372
373         switch (command) {
374         case XC2028_TUNER_RESET:
375                 /* Send the tuner in then out of reset */
376                 dib7000p_set_gpio(adap->fe, 8, 0, 0); msleep(10);
377                 dib7000p_set_gpio(adap->fe, 8, 0, 1);
378                 break;
379         case XC2028_RESET_CLK:
380                 break;
381         default:
382                 err("%s: unknown command %d, arg %d\n", __func__,
383                         command, arg);
384                 return -EINVAL;
385         }
386         return 0;
387 }
388
389 static struct xc2028_ctrl stk7700ph_xc3028_ctrl = {
390         .fname = XC2028_DEFAULT_FIRMWARE,
391         .max_len = 64,
392         .demod = XC3028_FE_DIBCOM52,
393 };
394
395 static struct xc2028_config stk7700ph_xc3028_config = {
396         .i2c_addr = 0x61,
397         .callback = stk7700ph_xc3028_callback,
398         .ctrl = &stk7700ph_xc3028_ctrl,
399 };
400
401 static int stk7700ph_frontend_attach(struct dvb_usb_adapter *adap)
402 {
403         struct usb_device_descriptor *desc = &adap->dev->udev->descriptor;
404
405         if (desc->idVendor  == cpu_to_le16(USB_VID_PINNACLE) &&
406             desc->idProduct == cpu_to_le16(USB_PID_PINNACLE_EXPRESSCARD_320CX))
407         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
408         else
409         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
410         msleep(20);
411         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
412         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
413         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
414         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
415         msleep(10);
416         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
417         msleep(20);
418         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
419         msleep(10);
420
421         dib7000p_i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
422                 &stk7700ph_dib7700_xc3028_config);
423
424         adap->fe = dvb_attach(dib7000p_attach, &adap->dev->i2c_adap, 0x80,
425                 &stk7700ph_dib7700_xc3028_config);
426
427         return adap->fe == NULL ? -ENODEV : 0;
428 }
429
430 static int stk7700ph_tuner_attach(struct dvb_usb_adapter *adap)
431 {
432         struct i2c_adapter *tun_i2c;
433
434         tun_i2c = dib7000p_get_i2c_master(adap->fe,
435                 DIBX000_I2C_INTERFACE_TUNER, 1);
436
437         stk7700ph_xc3028_config.i2c_adap = tun_i2c;
438         stk7700ph_xc3028_config.video_dev = adap;
439
440         return dvb_attach(xc2028_attach, adap->fe, &stk7700ph_xc3028_config)
441                 == NULL ? -ENODEV : 0;
442 }
443
444 #define DEFAULT_RC_INTERVAL 150
445
446 static u8 rc_request[] = { REQUEST_POLL_RC, 0 };
447
448 /* Number of keypresses to ignore before start repeating */
449 #define RC_REPEAT_DELAY 2
450
451 static int dib0700_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
452 {
453         u8 key[4];
454         int i;
455         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
456         struct dib0700_state *st = d->priv;
457         *event = 0;
458         *state = REMOTE_NO_KEY_PRESSED;
459         i=dib0700_ctrl_rd(d,rc_request,2,key,4);
460         if (i<=0) {
461                 err("RC Query Failed");
462                 return -1;
463         }
464
465         /* losing half of KEY_0 events from Philipps rc5 remotes.. */
466         if (key[0]==0 && key[1]==0 && key[2]==0 && key[3]==0) return 0;
467
468         /* 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]);  */
469
470         dib0700_rc_setup(d); /* reset ir sensor data to prevent false events */
471
472         switch (dvb_usb_dib0700_ir_proto) {
473         case 0: {
474                 /* NEC protocol sends repeat code as 0 0 0 FF */
475                 if ((key[3-2] == 0x00) && (key[3-3] == 0x00) &&
476                     (key[3] == 0xFF)) {
477                         st->rc_counter++;
478                         if (st->rc_counter > RC_REPEAT_DELAY) {
479                                 *event = d->last_event;
480                                 *state = REMOTE_KEY_PRESSED;
481                                 st->rc_counter = RC_REPEAT_DELAY;
482                         }
483                         return 0;
484                 }
485                 for (i=0;i<d->props.rc_key_map_size; i++) {
486                         if (keymap[i].custom == key[3-2] && keymap[i].data == key[3-3]) {
487                                 st->rc_counter = 0;
488                                 *event = keymap[i].event;
489                                 *state = REMOTE_KEY_PRESSED;
490                                 d->last_event = keymap[i].event;
491                                 return 0;
492                         }
493                 }
494                 break;
495         }
496         default: {
497                 /* RC-5 protocol changes toggle bit on new keypress */
498                 for (i = 0; i < d->props.rc_key_map_size; i++) {
499                         if (keymap[i].custom == key[3-2] && keymap[i].data == key[3-3]) {
500                                 if (d->last_event == keymap[i].event &&
501                                         key[3-1] == st->rc_toggle) {
502                                         st->rc_counter++;
503                                         /* prevents unwanted double hits */
504                                         if (st->rc_counter > RC_REPEAT_DELAY) {
505                                                 *event = d->last_event;
506                                                 *state = REMOTE_KEY_PRESSED;
507                                                 st->rc_counter = RC_REPEAT_DELAY;
508                                         }
509
510                                         return 0;
511                                 }
512                                 st->rc_counter = 0;
513                                 *event = keymap[i].event;
514                                 *state = REMOTE_KEY_PRESSED;
515                                 st->rc_toggle = key[3-1];
516                                 d->last_event = keymap[i].event;
517                                 return 0;
518                         }
519                 }
520                 break;
521         }
522         }
523         err("Unknown remote controller key: %2X %2X %2X %2X", (int) key[3-2], (int) key[3-3], (int) key[3-1], (int) key[3]);
524         d->last_event = 0;
525         return 0;
526 }
527
528 static struct dvb_usb_rc_key dib0700_rc_keys[] = {
529         /* Key codes for the tiny Pinnacle remote*/
530         { 0x07, 0x00, KEY_MUTE },
531         { 0x07, 0x01, KEY_MENU }, // Pinnacle logo
532         { 0x07, 0x39, KEY_POWER },
533         { 0x07, 0x03, KEY_VOLUMEUP },
534         { 0x07, 0x09, KEY_VOLUMEDOWN },
535         { 0x07, 0x06, KEY_CHANNELUP },
536         { 0x07, 0x0c, KEY_CHANNELDOWN },
537         { 0x07, 0x0f, KEY_1 },
538         { 0x07, 0x15, KEY_2 },
539         { 0x07, 0x10, KEY_3 },
540         { 0x07, 0x18, KEY_4 },
541         { 0x07, 0x1b, KEY_5 },
542         { 0x07, 0x1e, KEY_6 },
543         { 0x07, 0x11, KEY_7 },
544         { 0x07, 0x21, KEY_8 },
545         { 0x07, 0x12, KEY_9 },
546         { 0x07, 0x27, KEY_0 },
547         { 0x07, 0x24, KEY_SCREEN }, // 'Square' key
548         { 0x07, 0x2a, KEY_TEXT },   // 'T' key
549         { 0x07, 0x2d, KEY_REWIND },
550         { 0x07, 0x30, KEY_PLAY },
551         { 0x07, 0x33, KEY_FASTFORWARD },
552         { 0x07, 0x36, KEY_RECORD },
553         { 0x07, 0x3c, KEY_STOP },
554         { 0x07, 0x3f, KEY_CANCEL }, // '?' key
555         /* Key codes for the Terratec Cinergy DT XS Diversity, similar to cinergyT2.c */
556         { 0xeb, 0x01, KEY_POWER },
557         { 0xeb, 0x02, KEY_1 },
558         { 0xeb, 0x03, KEY_2 },
559         { 0xeb, 0x04, KEY_3 },
560         { 0xeb, 0x05, KEY_4 },
561         { 0xeb, 0x06, KEY_5 },
562         { 0xeb, 0x07, KEY_6 },
563         { 0xeb, 0x08, KEY_7 },
564         { 0xeb, 0x09, KEY_8 },
565         { 0xeb, 0x0a, KEY_9 },
566         { 0xeb, 0x0b, KEY_VIDEO },
567         { 0xeb, 0x0c, KEY_0 },
568         { 0xeb, 0x0d, KEY_REFRESH },
569         { 0xeb, 0x0f, KEY_EPG },
570         { 0xeb, 0x10, KEY_UP },
571         { 0xeb, 0x11, KEY_LEFT },
572         { 0xeb, 0x12, KEY_OK },
573         { 0xeb, 0x13, KEY_RIGHT },
574         { 0xeb, 0x14, KEY_DOWN },
575         { 0xeb, 0x16, KEY_INFO },
576         { 0xeb, 0x17, KEY_RED },
577         { 0xeb, 0x18, KEY_GREEN },
578         { 0xeb, 0x19, KEY_YELLOW },
579         { 0xeb, 0x1a, KEY_BLUE },
580         { 0xeb, 0x1b, KEY_CHANNELUP },
581         { 0xeb, 0x1c, KEY_VOLUMEUP },
582         { 0xeb, 0x1d, KEY_MUTE },
583         { 0xeb, 0x1e, KEY_VOLUMEDOWN },
584         { 0xeb, 0x1f, KEY_CHANNELDOWN },
585         { 0xeb, 0x40, KEY_PAUSE },
586         { 0xeb, 0x41, KEY_HOME },
587         { 0xeb, 0x42, KEY_MENU }, /* DVD Menu */
588         { 0xeb, 0x43, KEY_SUBTITLE },
589         { 0xeb, 0x44, KEY_TEXT }, /* Teletext */
590         { 0xeb, 0x45, KEY_DELETE },
591         { 0xeb, 0x46, KEY_TV },
592         { 0xeb, 0x47, KEY_DVD },
593         { 0xeb, 0x48, KEY_STOP },
594         { 0xeb, 0x49, KEY_VIDEO },
595         { 0xeb, 0x4a, KEY_AUDIO }, /* Music */
596         { 0xeb, 0x4b, KEY_SCREEN }, /* Pic */
597         { 0xeb, 0x4c, KEY_PLAY },
598         { 0xeb, 0x4d, KEY_BACK },
599         { 0xeb, 0x4e, KEY_REWIND },
600         { 0xeb, 0x4f, KEY_FASTFORWARD },
601         { 0xeb, 0x54, KEY_PREVIOUS },
602         { 0xeb, 0x58, KEY_RECORD },
603         { 0xeb, 0x5c, KEY_NEXT },
604
605         /* Key codes for the Haupauge WinTV Nova-TD, copied from nova-t-usb2.c (Nova-T USB2) */
606         { 0x1e, 0x00, KEY_0 },
607         { 0x1e, 0x01, KEY_1 },
608         { 0x1e, 0x02, KEY_2 },
609         { 0x1e, 0x03, KEY_3 },
610         { 0x1e, 0x04, KEY_4 },
611         { 0x1e, 0x05, KEY_5 },
612         { 0x1e, 0x06, KEY_6 },
613         { 0x1e, 0x07, KEY_7 },
614         { 0x1e, 0x08, KEY_8 },
615         { 0x1e, 0x09, KEY_9 },
616         { 0x1e, 0x0a, KEY_KPASTERISK },
617         { 0x1e, 0x0b, KEY_RED },
618         { 0x1e, 0x0c, KEY_RADIO },
619         { 0x1e, 0x0d, KEY_MENU },
620         { 0x1e, 0x0e, KEY_GRAVE }, /* # */
621         { 0x1e, 0x0f, KEY_MUTE },
622         { 0x1e, 0x10, KEY_VOLUMEUP },
623         { 0x1e, 0x11, KEY_VOLUMEDOWN },
624         { 0x1e, 0x12, KEY_CHANNEL },
625         { 0x1e, 0x14, KEY_UP },
626         { 0x1e, 0x15, KEY_DOWN },
627         { 0x1e, 0x16, KEY_LEFT },
628         { 0x1e, 0x17, KEY_RIGHT },
629         { 0x1e, 0x18, KEY_VIDEO },
630         { 0x1e, 0x19, KEY_AUDIO },
631         { 0x1e, 0x1a, KEY_MEDIA },
632         { 0x1e, 0x1b, KEY_EPG },
633         { 0x1e, 0x1c, KEY_TV },
634         { 0x1e, 0x1e, KEY_NEXT },
635         { 0x1e, 0x1f, KEY_BACK },
636         { 0x1e, 0x20, KEY_CHANNELUP },
637         { 0x1e, 0x21, KEY_CHANNELDOWN },
638         { 0x1e, 0x24, KEY_LAST }, /* Skip backwards */
639         { 0x1e, 0x25, KEY_OK },
640         { 0x1e, 0x29, KEY_BLUE},
641         { 0x1e, 0x2e, KEY_GREEN },
642         { 0x1e, 0x30, KEY_PAUSE },
643         { 0x1e, 0x32, KEY_REWIND },
644         { 0x1e, 0x34, KEY_FASTFORWARD },
645         { 0x1e, 0x35, KEY_PLAY },
646         { 0x1e, 0x36, KEY_STOP },
647         { 0x1e, 0x37, KEY_RECORD },
648         { 0x1e, 0x38, KEY_YELLOW },
649         { 0x1e, 0x3b, KEY_GOTO },
650         { 0x1e, 0x3d, KEY_POWER },
651
652         /* Key codes for the Leadtek Winfast DTV Dongle */
653         { 0x00, 0x42, KEY_POWER },
654         { 0x07, 0x7c, KEY_TUNER },
655         { 0x0f, 0x4e, KEY_PRINT }, /* PREVIEW */
656         { 0x08, 0x40, KEY_SCREEN }, /* full screen toggle*/
657         { 0x0f, 0x71, KEY_DOT }, /* frequency */
658         { 0x07, 0x43, KEY_0 },
659         { 0x0c, 0x41, KEY_1 },
660         { 0x04, 0x43, KEY_2 },
661         { 0x0b, 0x7f, KEY_3 },
662         { 0x0e, 0x41, KEY_4 },
663         { 0x06, 0x43, KEY_5 },
664         { 0x09, 0x7f, KEY_6 },
665         { 0x0d, 0x7e, KEY_7 },
666         { 0x05, 0x7c, KEY_8 },
667         { 0x0a, 0x40, KEY_9 },
668         { 0x0e, 0x4e, KEY_CLEAR },
669         { 0x04, 0x7c, KEY_CHANNEL }, /* show channel number */
670         { 0x0f, 0x41, KEY_LAST }, /* recall */
671         { 0x03, 0x42, KEY_MUTE },
672         { 0x06, 0x4c, KEY_RESERVED }, /* PIP button*/
673         { 0x01, 0x72, KEY_SHUFFLE }, /* SNAPSHOT */
674         { 0x0c, 0x4e, KEY_PLAYPAUSE }, /* TIMESHIFT */
675         { 0x0b, 0x70, KEY_RECORD },
676         { 0x03, 0x7d, KEY_VOLUMEUP },
677         { 0x01, 0x7d, KEY_VOLUMEDOWN },
678         { 0x02, 0x42, KEY_CHANNELUP },
679         { 0x00, 0x7d, KEY_CHANNELDOWN },
680 };
681
682 /* STK7700P: Hauppauge Nova-T Stick, AVerMedia Volar */
683 static struct dibx000_agc_config stk7700p_7000m_mt2060_agc_config = {
684         BAND_UHF | BAND_VHF,       // band_caps
685
686         /* 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,
687          * 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 */
688         (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0), // setup
689
690         712,  // inv_gain
691         41,  // time_stabiliz
692
693         0,  // alpha_level
694         118,  // thlock
695
696         0,     // wbd_inv
697         4095,  // wbd_ref
698         0,     // wbd_sel
699         0,     // wbd_alpha
700
701         42598,  // agc1_max
702         17694,  // agc1_min
703         45875,  // agc2_max
704         2621,  // agc2_min
705         0,  // agc1_pt1
706         76,  // agc1_pt2
707         139,  // agc1_pt3
708         52,  // agc1_slope1
709         59,  // agc1_slope2
710         107,  // agc2_pt1
711         172,  // agc2_pt2
712         57,  // agc2_slope1
713         70,  // agc2_slope2
714
715         21,  // alpha_mant
716         25,  // alpha_exp
717         28,  // beta_mant
718         48,  // beta_exp
719
720         1,  // perform_agc_softsplit
721         {  0,     // split_min
722            107,   // split_max
723            51800, // global_split_min
724            24700  // global_split_max
725         },
726 };
727
728 static struct dibx000_agc_config stk7700p_7000p_mt2060_agc_config = {
729         BAND_UHF | BAND_VHF,
730
731         /* 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,
732          * 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 */
733         (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0), // setup
734
735         712, // inv_gain
736         41,  // time_stabiliz
737
738         0,   // alpha_level
739         118, // thlock
740
741         0,    // wbd_inv
742         4095, // wbd_ref
743         0,    // wbd_sel
744         0,    // wbd_alpha
745
746         42598, // agc1_max
747         16384, // agc1_min
748         42598, // agc2_max
749             0, // agc2_min
750
751           0,   // agc1_pt1
752         137,   // agc1_pt2
753         255,   // agc1_pt3
754
755           0,   // agc1_slope1
756         255,   // agc1_slope2
757
758         0,     // agc2_pt1
759         0,     // agc2_pt2
760
761          0,    // agc2_slope1
762         41,    // agc2_slope2
763
764         15, // alpha_mant
765         25, // alpha_exp
766
767         28, // beta_mant
768         48, // beta_exp
769
770         0, // perform_agc_softsplit
771 };
772
773 static struct dibx000_bandwidth_config stk7700p_pll_config = {
774         60000, 30000, // internal, sampling
775         1, 8, 3, 1, 0, // pll_cfg: prediv, ratio, range, reset, bypass
776         0, 0, 1, 1, 0, // misc: refdiv, bypclk_div, IO_CLK_en_core, ADClkSrc, modulo
777         (3 << 14) | (1 << 12) | (524 << 0), // sad_cfg: refsel, sel, freq_15k
778         60258167, // ifreq
779         20452225, // timf
780         30000000, // xtal
781 };
782
783 static struct dib7000m_config stk7700p_dib7000m_config = {
784         .dvbt_mode = 1,
785         .output_mpeg2_in_188_bytes = 1,
786         .quartz_direct = 1,
787
788         .agc_config_count = 1,
789         .agc = &stk7700p_7000m_mt2060_agc_config,
790         .bw  = &stk7700p_pll_config,
791
792         .gpio_dir = DIB7000M_GPIO_DEFAULT_DIRECTIONS,
793         .gpio_val = DIB7000M_GPIO_DEFAULT_VALUES,
794         .gpio_pwm_pos = DIB7000M_GPIO_DEFAULT_PWM_POS,
795 };
796
797 static struct dib7000p_config stk7700p_dib7000p_config = {
798         .output_mpeg2_in_188_bytes = 1,
799
800         .agc_config_count = 1,
801         .agc = &stk7700p_7000p_mt2060_agc_config,
802         .bw  = &stk7700p_pll_config,
803
804         .gpio_dir = DIB7000M_GPIO_DEFAULT_DIRECTIONS,
805         .gpio_val = DIB7000M_GPIO_DEFAULT_VALUES,
806         .gpio_pwm_pos = DIB7000M_GPIO_DEFAULT_PWM_POS,
807 };
808
809 static int stk7700p_frontend_attach(struct dvb_usb_adapter *adap)
810 {
811         struct dib0700_state *st = adap->dev->priv;
812         /* unless there is no real power management in DVB - we leave the device on GPIO6 */
813
814         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
815         dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 0); msleep(50);
816
817         dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 1); msleep(10);
818         dib0700_set_gpio(adap->dev, GPIO9,  GPIO_OUT, 1);
819
820         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0); msleep(10);
821         dib0700_ctrl_clock(adap->dev, 72, 1);
822         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1); msleep(100);
823
824         dib0700_set_gpio(adap->dev,  GPIO0, GPIO_OUT, 1);
825
826         st->mt2060_if1[0] = 1220;
827
828         if (dib7000pc_detection(&adap->dev->i2c_adap)) {
829                 adap->fe = dvb_attach(dib7000p_attach, &adap->dev->i2c_adap, 18, &stk7700p_dib7000p_config);
830                 st->is_dib7000pc = 1;
831         } else
832                 adap->fe = dvb_attach(dib7000m_attach, &adap->dev->i2c_adap, 18, &stk7700p_dib7000m_config);
833
834         return adap->fe == NULL ? -ENODEV : 0;
835 }
836
837 static struct mt2060_config stk7700p_mt2060_config = {
838         0x60
839 };
840
841 static int stk7700p_tuner_attach(struct dvb_usb_adapter *adap)
842 {
843         struct i2c_adapter *prim_i2c = &adap->dev->i2c_adap;
844         struct dib0700_state *st = adap->dev->priv;
845         struct i2c_adapter *tun_i2c;
846         s8 a;
847         int if1=1220;
848         if (adap->dev->udev->descriptor.idVendor  == cpu_to_le16(USB_VID_HAUPPAUGE) &&
849                 adap->dev->udev->descriptor.idProduct == cpu_to_le16(USB_PID_HAUPPAUGE_NOVA_T_STICK)) {
850                 if (!eeprom_read(prim_i2c,0x58,&a)) if1=1220+a;
851         }
852         if (st->is_dib7000pc)
853                 tun_i2c = dib7000p_get_i2c_master(adap->fe, DIBX000_I2C_INTERFACE_TUNER, 1);
854         else
855                 tun_i2c = dib7000m_get_i2c_master(adap->fe, DIBX000_I2C_INTERFACE_TUNER, 1);
856
857         return dvb_attach(mt2060_attach, adap->fe, tun_i2c, &stk7700p_mt2060_config,
858                 if1) == NULL ? -ENODEV : 0;
859 }
860
861 /* DIB7070 generic */
862 static struct dibx000_agc_config dib7070_agc_config = {
863         BAND_UHF | BAND_VHF | BAND_LBAND | BAND_SBAND,
864         /* 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,
865          * 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 */
866         (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) | (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0), // setup
867
868         600, // inv_gain
869         10,  // time_stabiliz
870
871         0,  // alpha_level
872         118,  // thlock
873
874         0,     // wbd_inv
875         3530,  // wbd_ref
876         1,     // wbd_sel
877         5,     // wbd_alpha
878
879         65535,  // agc1_max
880                 0,  // agc1_min
881
882         65535,  // agc2_max
883         0,      // agc2_min
884
885         0,      // agc1_pt1
886         40,     // agc1_pt2
887         183,    // agc1_pt3
888         206,    // agc1_slope1
889         255,    // agc1_slope2
890         72,     // agc2_pt1
891         152,    // agc2_pt2
892         88,     // agc2_slope1
893         90,     // agc2_slope2
894
895         17,  // alpha_mant
896         27,  // alpha_exp
897         23,  // beta_mant
898         51,  // beta_exp
899
900         0,  // perform_agc_softsplit
901 };
902
903 static int dib7070_tuner_reset(struct dvb_frontend *fe, int onoff)
904 {
905         return dib7000p_set_gpio(fe, 8, 0, !onoff);
906 }
907
908 static int dib7070_tuner_sleep(struct dvb_frontend *fe, int onoff)
909 {
910         return dib7000p_set_gpio(fe, 9, 0, onoff);
911 }
912
913 static struct dib0070_config dib7070p_dib0070_config[2] = {
914         {
915                 .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
916                 .reset = dib7070_tuner_reset,
917                 .sleep = dib7070_tuner_sleep,
918                 .clock_khz = 12000,
919                 .clock_pad_drive = 4
920         }, {
921                 .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
922                 .reset = dib7070_tuner_reset,
923                 .sleep = dib7070_tuner_sleep,
924                 .clock_khz = 12000,
925
926         }
927 };
928
929 static int dib7070_set_param_override(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
930 {
931         struct dvb_usb_adapter *adap = fe->dvb->priv;
932         struct dib0700_adapter_state *state = adap->priv;
933
934         u16 offset;
935         u8 band = BAND_OF_FREQUENCY(fep->frequency/1000);
936         switch (band) {
937                 case BAND_VHF: offset = 950; break;
938                 case BAND_UHF:
939                 default: offset = 550; break;
940         }
941         deb_info("WBD for DiB7000P: %d\n", offset + dib0070_wbd_offset(fe));
942         dib7000p_set_wbd_ref(fe, offset + dib0070_wbd_offset(fe));
943         return state->set_param_save(fe, fep);
944 }
945
946 static int dib7070p_tuner_attach(struct dvb_usb_adapter *adap)
947 {
948         struct dib0700_adapter_state *st = adap->priv;
949         struct i2c_adapter *tun_i2c = dib7000p_get_i2c_master(adap->fe, DIBX000_I2C_INTERFACE_TUNER, 1);
950
951         if (adap->id == 0) {
952                 if (dvb_attach(dib0070_attach, adap->fe, tun_i2c, &dib7070p_dib0070_config[0]) == NULL)
953                         return -ENODEV;
954         } else {
955                 if (dvb_attach(dib0070_attach, adap->fe, tun_i2c, &dib7070p_dib0070_config[1]) == NULL)
956                         return -ENODEV;
957         }
958
959         st->set_param_save = adap->fe->ops.tuner_ops.set_params;
960         adap->fe->ops.tuner_ops.set_params = dib7070_set_param_override;
961         return 0;
962 }
963
964 static struct dibx000_bandwidth_config dib7070_bw_config_12_mhz = {
965         60000, 15000, // internal, sampling
966         1, 20, 3, 1, 0, // pll_cfg: prediv, ratio, range, reset, bypass
967         0, 0, 1, 1, 2, // misc: refdiv, bypclk_div, IO_CLK_en_core, ADClkSrc, modulo
968         (3 << 14) | (1 << 12) | (524 << 0), // sad_cfg: refsel, sel, freq_15k
969         (0 << 25) | 0, // ifreq = 0.000000 MHz
970         20452225, // timf
971         12000000, // xtal_hz
972 };
973
974 static struct dib7000p_config dib7070p_dib7000p_config = {
975         .output_mpeg2_in_188_bytes = 1,
976
977         .agc_config_count = 1,
978         .agc = &dib7070_agc_config,
979         .bw  = &dib7070_bw_config_12_mhz,
980         .tuner_is_baseband = 1,
981         .spur_protect = 1,
982
983         .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
984         .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
985         .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
986
987         .hostbus_diversity = 1,
988 };
989
990 /* STK7070P */
991 static int stk7070p_frontend_attach(struct dvb_usb_adapter *adap)
992 {
993         struct usb_device_descriptor *p = &adap->dev->udev->descriptor;
994         if (p->idVendor  == cpu_to_le16(USB_VID_PINNACLE) &&
995             p->idProduct == cpu_to_le16(USB_PID_PINNACLE_PCTV72E))
996                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0);
997         else
998                 dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
999         msleep(10);
1000         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
1001         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1002         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1003         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1004
1005         dib0700_ctrl_clock(adap->dev, 72, 1);
1006
1007         msleep(10);
1008         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1009         msleep(10);
1010         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1011
1012         dib7000p_i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
1013                 &dib7070p_dib7000p_config);
1014
1015         adap->fe = dvb_attach(dib7000p_attach, &adap->dev->i2c_adap, 0x80,
1016                 &dib7070p_dib7000p_config);
1017         return adap->fe == NULL ? -ENODEV : 0;
1018 }
1019
1020 /* STK7070PD */
1021 static struct dib7000p_config stk7070pd_dib7000p_config[2] = {
1022         {
1023                 .output_mpeg2_in_188_bytes = 1,
1024
1025                 .agc_config_count = 1,
1026                 .agc = &dib7070_agc_config,
1027                 .bw  = &dib7070_bw_config_12_mhz,
1028                 .tuner_is_baseband = 1,
1029                 .spur_protect = 1,
1030
1031                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
1032                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
1033                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
1034
1035                 .hostbus_diversity = 1,
1036         }, {
1037                 .output_mpeg2_in_188_bytes = 1,
1038
1039                 .agc_config_count = 1,
1040                 .agc = &dib7070_agc_config,
1041                 .bw  = &dib7070_bw_config_12_mhz,
1042                 .tuner_is_baseband = 1,
1043                 .spur_protect = 1,
1044
1045                 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
1046                 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
1047                 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
1048
1049                 .hostbus_diversity = 1,
1050         }
1051 };
1052
1053 static int stk7070pd_frontend_attach0(struct dvb_usb_adapter *adap)
1054 {
1055         dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 1);
1056         msleep(10);
1057         dib0700_set_gpio(adap->dev, GPIO9, GPIO_OUT, 1);
1058         dib0700_set_gpio(adap->dev, GPIO4, GPIO_OUT, 1);
1059         dib0700_set_gpio(adap->dev, GPIO7, GPIO_OUT, 1);
1060         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0);
1061
1062         dib0700_ctrl_clock(adap->dev, 72, 1);
1063
1064         msleep(10);
1065         dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1);
1066         msleep(10);
1067         dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1);
1068
1069         dib7000p_i2c_enumeration(&adap->dev->i2c_adap, 2, 18, stk7070pd_dib7000p_config);
1070
1071         adap->fe = dvb_attach(dib7000p_attach, &adap->dev->i2c_adap, 0x80, &stk7070pd_dib7000p_config[0]);
1072         return adap->fe == NULL ? -ENODEV : 0;
1073 }
1074
1075 static int stk7070pd_frontend_attach1(struct dvb_usb_adapter *adap)
1076 {
1077         adap->fe = dvb_attach(dib7000p_attach, &adap->dev->i2c_adap, 0x82, &stk7070pd_dib7000p_config[1]);
1078         return adap->fe == NULL ? -ENODEV : 0;
1079 }
1080
1081 /* DVB-USB and USB stuff follows */
1082 struct usb_device_id dib0700_usb_id_table[] = {
1083 /* 0 */ { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7700P) },
1084         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7700P_PC) },
1085         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_500) },
1086         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_500_2) },
1087         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK) },
1088 /* 5 */ { USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR) },
1089         { USB_DEVICE(USB_VID_COMPRO,    USB_PID_COMPRO_VIDEOMATE_U500) },
1090         { USB_DEVICE(USB_VID_UNIWILL,   USB_PID_UNIWILL_STK7700P) },
1091         { USB_DEVICE(USB_VID_LEADTEK,   USB_PID_WINFAST_DTV_DONGLE_STK7700P) },
1092         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK_2) },
1093 /* 10 */{ USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_2) },
1094         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV2000E) },
1095         { USB_DEVICE(USB_VID_TERRATEC,
1096                         USB_PID_TERRATEC_CINERGY_DT_XS_DIVERSITY) },
1097         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_TD_STICK) },
1098         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7700D) },
1099 /* 15 */{ USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7070P) },
1100         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV_DVB_T_FLASH) },
1101         { USB_DEVICE(USB_VID_DIBCOM,    USB_PID_DIBCOM_STK7070PD) },
1102         { USB_DEVICE(USB_VID_PINNACLE,
1103                         USB_PID_PINNACLE_PCTV_DUAL_DIVERSITY_DVB_T) },
1104         { USB_DEVICE(USB_VID_COMPRO,    USB_PID_COMPRO_VIDEOMATE_U500_PC) },
1105 /* 20 */{ USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_EXPRESS) },
1106         { USB_DEVICE(USB_VID_GIGABYTE,  USB_PID_GIGABYTE_U7000) },
1107         { USB_DEVICE(USB_VID_ULTIMA_ELECTRONIC, USB_PID_ARTEC_T14BR) },
1108         { USB_DEVICE(USB_VID_ASUS,      USB_PID_ASUS_U3000) },
1109         { USB_DEVICE(USB_VID_ASUS,      USB_PID_ASUS_U3100) },
1110 /* 25 */{ USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_STICK_3) },
1111         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_MYTV_T) },
1112         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_HT_USB_XE) },
1113         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_EXPRESSCARD_320CX) },
1114         { USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV72E) },
1115 /* 30 */{ USB_DEVICE(USB_VID_PINNACLE,  USB_PID_PINNACLE_PCTV73E) },
1116         { USB_DEVICE(USB_VID_YUAN,      USB_PID_YUAN_EC372S) },
1117         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_HT_EXPRESS) },
1118         { USB_DEVICE(USB_VID_TERRATEC,  USB_PID_TERRATEC_CINERGY_T_XXS) },
1119         { USB_DEVICE(USB_VID_LEADTEK,   USB_PID_WINFAST_DTV_DONGLE_STK7700P_2) },
1120 /* 35 */{ USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_TD_STICK_52009) },
1121         { USB_DEVICE(USB_VID_HAUPPAUGE, USB_PID_HAUPPAUGE_NOVA_T_500_3) },
1122         { 0 }           /* Terminating entry */
1123 };
1124 MODULE_DEVICE_TABLE(usb, dib0700_usb_id_table);
1125
1126 #define DIB0700_DEFAULT_DEVICE_PROPERTIES \
1127         .caps              = DVB_USB_IS_AN_I2C_ADAPTER, \
1128         .usb_ctrl          = DEVICE_SPECIFIC, \
1129         .firmware          = "dvb-usb-dib0700-1.10.fw", \
1130         .download_firmware = dib0700_download_firmware, \
1131         .no_reconnect      = 1, \
1132         .size_of_priv      = sizeof(struct dib0700_state), \
1133         .i2c_algo          = &dib0700_i2c_algo, \
1134         .identify_state    = dib0700_identify_state
1135
1136 #define DIB0700_DEFAULT_STREAMING_CONFIG(ep) \
1137         .streaming_ctrl   = dib0700_streaming_ctrl, \
1138         .stream = { \
1139                 .type = USB_BULK, \
1140                 .count = 4, \
1141                 .endpoint = ep, \
1142                 .u = { \
1143                         .bulk = { \
1144                                 .buffersize = 39480, \
1145                         } \
1146                 } \
1147         }
1148
1149 struct dvb_usb_device_properties dib0700_devices[] = {
1150         {
1151                 DIB0700_DEFAULT_DEVICE_PROPERTIES,
1152
1153                 .num_adapters = 1,
1154                 .adapter = {
1155                         {
1156                                 .frontend_attach  = stk7700p_frontend_attach,
1157                                 .tuner_attach     = stk7700p_tuner_attach,
1158
1159                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
1160                         },
1161                 },
1162
1163                 .num_device_descs = 8,
1164                 .devices = {
1165                         {   "DiBcom STK7700P reference design",
1166                                 { &dib0700_usb_id_table[0], &dib0700_usb_id_table[1] },
1167                                 { NULL },
1168                         },
1169                         {   "Hauppauge Nova-T Stick",
1170                                 { &dib0700_usb_id_table[4], &dib0700_usb_id_table[9], NULL },
1171                                 { NULL },
1172                         },
1173                         {   "AVerMedia AVerTV DVB-T Volar",
1174                                 { &dib0700_usb_id_table[5], &dib0700_usb_id_table[10] },
1175                                 { NULL },
1176                         },
1177                         {   "Compro Videomate U500",
1178                                 { &dib0700_usb_id_table[6], &dib0700_usb_id_table[19] },
1179                                 { NULL },
1180                         },
1181                         {   "Uniwill STK7700P based (Hama and others)",
1182                                 { &dib0700_usb_id_table[7], NULL },
1183                                 { NULL },
1184                         },
1185                         {   "Leadtek Winfast DTV Dongle (STK7700P based)",
1186                                 { &dib0700_usb_id_table[8], &dib0700_usb_id_table[34] },
1187                                 { NULL },
1188                         },
1189                         {   "AVerMedia AVerTV DVB-T Express",
1190                                 { &dib0700_usb_id_table[20] },
1191                                 { NULL },
1192                         },
1193                         {   "Gigabyte U7000",
1194                                 { &dib0700_usb_id_table[21], NULL },
1195                                 { NULL },
1196                         }
1197                 },
1198
1199                 .rc_interval      = DEFAULT_RC_INTERVAL,
1200                 .rc_key_map       = dib0700_rc_keys,
1201                 .rc_key_map_size  = ARRAY_SIZE(dib0700_rc_keys),
1202                 .rc_query         = dib0700_rc_query
1203         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
1204
1205                 .num_adapters = 2,
1206                 .adapter = {
1207                         {
1208                                 .frontend_attach  = bristol_frontend_attach,
1209                                 .tuner_attach     = bristol_tuner_attach,
1210
1211                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
1212                         }, {
1213                                 .frontend_attach  = bristol_frontend_attach,
1214                                 .tuner_attach     = bristol_tuner_attach,
1215
1216                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
1217                         }
1218                 },
1219
1220                 .num_device_descs = 1,
1221                 .devices = {
1222                         {   "Hauppauge Nova-T 500 Dual DVB-T",
1223                                 { &dib0700_usb_id_table[2], &dib0700_usb_id_table[3], NULL },
1224                                 { NULL },
1225                         },
1226                 },
1227
1228                 .rc_interval      = DEFAULT_RC_INTERVAL,
1229                 .rc_key_map       = dib0700_rc_keys,
1230                 .rc_key_map_size  = ARRAY_SIZE(dib0700_rc_keys),
1231                 .rc_query         = dib0700_rc_query
1232         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
1233
1234                 .num_adapters = 2,
1235                 .adapter = {
1236                         {
1237                                 .frontend_attach  = stk7700d_frontend_attach,
1238                                 .tuner_attach     = stk7700d_tuner_attach,
1239
1240                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
1241                         }, {
1242                                 .frontend_attach  = stk7700d_frontend_attach,
1243                                 .tuner_attach     = stk7700d_tuner_attach,
1244
1245                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
1246                         }
1247                 },
1248
1249                 .num_device_descs = 4,
1250                 .devices = {
1251                         {   "Pinnacle PCTV 2000e",
1252                                 { &dib0700_usb_id_table[11], NULL },
1253                                 { NULL },
1254                         },
1255                         {   "Terratec Cinergy DT XS Diversity",
1256                                 { &dib0700_usb_id_table[12], NULL },
1257                                 { NULL },
1258                         },
1259                         {   "Hauppauge Nova-TD Stick/Elgato Eye-TV Diversity",
1260                                 { &dib0700_usb_id_table[13], NULL },
1261                                 { NULL },
1262                         },
1263                         {   "DiBcom STK7700D reference design",
1264                                 { &dib0700_usb_id_table[14], NULL },
1265                                 { NULL },
1266                         }
1267                 },
1268
1269                 .rc_interval      = DEFAULT_RC_INTERVAL,
1270                 .rc_key_map       = dib0700_rc_keys,
1271                 .rc_key_map_size  = ARRAY_SIZE(dib0700_rc_keys),
1272                 .rc_query         = dib0700_rc_query
1273
1274         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
1275
1276                 .num_adapters = 1,
1277                 .adapter = {
1278                         {
1279                                 .frontend_attach  = stk7700P2_frontend_attach,
1280                                 .tuner_attach     = stk7700d_tuner_attach,
1281
1282                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
1283                         },
1284                 },
1285
1286                 .num_device_descs = 2,
1287                 .devices = {
1288                         {   "ASUS My Cinema U3000 Mini DVBT Tuner",
1289                                 { &dib0700_usb_id_table[23], NULL },
1290                                 { NULL },
1291                         },
1292                         {   "Yuan EC372S",
1293                                 { &dib0700_usb_id_table[31], NULL },
1294                                 { NULL },
1295                         }
1296                 }
1297         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
1298
1299                 .num_adapters = 1,
1300                 .adapter = {
1301                         {
1302                                 .frontend_attach  = stk7070p_frontend_attach,
1303                                 .tuner_attach     = dib7070p_tuner_attach,
1304
1305                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
1306
1307                                 .size_of_priv     = sizeof(struct dib0700_adapter_state),
1308                         },
1309                 },
1310
1311                 .num_device_descs = 9,
1312                 .devices = {
1313                         {   "DiBcom STK7070P reference design",
1314                                 { &dib0700_usb_id_table[15], NULL },
1315                                 { NULL },
1316                         },
1317                         {   "Pinnacle PCTV DVB-T Flash Stick",
1318                                 { &dib0700_usb_id_table[16], NULL },
1319                                 { NULL },
1320                         },
1321                         {   "Artec T14BR DVB-T",
1322                                 { &dib0700_usb_id_table[22], NULL },
1323                                 { NULL },
1324                         },
1325                         {   "ASUS My Cinema U3100 Mini DVBT Tuner",
1326                                 { &dib0700_usb_id_table[24], NULL },
1327                                 { NULL },
1328                         },
1329                         {   "Hauppauge Nova-T Stick",
1330                                 { &dib0700_usb_id_table[25], NULL },
1331                                 { NULL },
1332                         },
1333                         {   "Hauppauge Nova-T MyTV.t",
1334                                 { &dib0700_usb_id_table[26], NULL },
1335                                 { NULL },
1336                         },
1337                         {   "Pinnacle PCTV 72e",
1338                                 { &dib0700_usb_id_table[29], NULL },
1339                                 { NULL },
1340                         },
1341                         {   "Pinnacle PCTV 73e",
1342                                 { &dib0700_usb_id_table[30], NULL },
1343                                 { NULL },
1344                         },
1345                         {   "Terratec Cinergy T USB XXS",
1346                                 { &dib0700_usb_id_table[33], NULL },
1347                                 { NULL },
1348                         },
1349                 },
1350
1351                 .rc_interval      = DEFAULT_RC_INTERVAL,
1352                 .rc_key_map       = dib0700_rc_keys,
1353                 .rc_key_map_size  = ARRAY_SIZE(dib0700_rc_keys),
1354                 .rc_query         = dib0700_rc_query
1355
1356         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
1357
1358                 .num_adapters = 2,
1359                 .adapter = {
1360                         {
1361                                 .frontend_attach  = stk7070pd_frontend_attach0,
1362                                 .tuner_attach     = dib7070p_tuner_attach,
1363
1364                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
1365
1366                                 .size_of_priv     = sizeof(struct dib0700_adapter_state),
1367                         }, {
1368                                 .frontend_attach  = stk7070pd_frontend_attach1,
1369                                 .tuner_attach     = dib7070p_tuner_attach,
1370
1371                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x03),
1372
1373                                 .size_of_priv     = sizeof(struct dib0700_adapter_state),
1374                         }
1375                 },
1376
1377                 .num_device_descs = 4,
1378                 .devices = {
1379                         {   "DiBcom STK7070PD reference design",
1380                                 { &dib0700_usb_id_table[17], NULL },
1381                                 { NULL },
1382                         },
1383                         {   "Pinnacle PCTV Dual DVB-T Diversity Stick",
1384                                 { &dib0700_usb_id_table[18], NULL },
1385                                 { NULL },
1386                         },
1387                         {   "Hauppauge Nova-TD Stick (52009)",
1388                                 { &dib0700_usb_id_table[35], NULL },
1389                                 { NULL },
1390                         },
1391                         {   "Hauppauge Nova-TD-500 (84xxx)",
1392                                 { &dib0700_usb_id_table[36], NULL },
1393                                 { NULL },
1394                         }
1395                 }
1396         }, { DIB0700_DEFAULT_DEVICE_PROPERTIES,
1397
1398                 .num_adapters = 1,
1399                 .adapter = {
1400                         {
1401                                 .frontend_attach  = stk7700ph_frontend_attach,
1402                                 .tuner_attach     = stk7700ph_tuner_attach,
1403
1404                                 DIB0700_DEFAULT_STREAMING_CONFIG(0x02),
1405
1406                                 .size_of_priv = sizeof(struct
1407                                                 dib0700_adapter_state),
1408                         },
1409                 },
1410
1411                 .num_device_descs = 3,
1412                 .devices = {
1413                         {   "Terratec Cinergy HT USB XE",
1414                                 { &dib0700_usb_id_table[27], NULL },
1415                                 { NULL },
1416                         },
1417                         {   "Pinnacle Expresscard 320cx",
1418                                 { &dib0700_usb_id_table[28], NULL },
1419                                 { NULL },
1420                         },
1421                         {   "Terratec Cinergy HT Express",
1422                                 { &dib0700_usb_id_table[32], NULL },
1423                                 { NULL },
1424                         },
1425                 },
1426                 .rc_interval      = DEFAULT_RC_INTERVAL,
1427                 .rc_key_map       = dib0700_rc_keys,
1428                 .rc_key_map_size  = ARRAY_SIZE(dib0700_rc_keys),
1429                 .rc_query         = dib0700_rc_query
1430         },
1431 };
1432
1433 int dib0700_device_count = ARRAY_SIZE(dib0700_devices);