]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/usb/em28xx/em28xx-dvb.c
[media] m88ts2022: convert to Kernel I2C driver model
[karo-tx-linux.git] / drivers / media / usb / em28xx / em28xx-dvb.c
1 /*
2  DVB device driver for em28xx
3
4  (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
5
6  (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7         - Fixes for the driver to properly work with HVR-950
8         - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
9         - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
10
11  (c) 2008 Aidan Thornton <makosoft@googlemail.com>
12
13  (c) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
14
15  Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
16         (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
17         (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
18
19  This program is free software; you can redistribute it and/or modify
20  it under the terms of the GNU General Public License as published by
21  the Free Software Foundation; either version 2 of the License.
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/usb.h>
27
28 #include "em28xx.h"
29 #include <media/v4l2-common.h>
30 #include <dvb_demux.h>
31 #include <dvb_net.h>
32 #include <dmxdev.h>
33 #include <media/tuner.h>
34 #include "tuner-simple.h"
35 #include <linux/gpio.h>
36
37 #include "lgdt330x.h"
38 #include "lgdt3305.h"
39 #include "zl10353.h"
40 #include "s5h1409.h"
41 #include "mt352.h"
42 #include "mt352_priv.h" /* FIXME */
43 #include "tda1002x.h"
44 #include "tda18271.h"
45 #include "s921.h"
46 #include "drxd.h"
47 #include "cxd2820r.h"
48 #include "tda18271c2dd.h"
49 #include "drxk.h"
50 #include "tda10071.h"
51 #include "a8293.h"
52 #include "qt1010.h"
53 #include "mb86a20s.h"
54 #include "m88ds3103.h"
55 #include "m88ts2022.h"
56
57 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
58 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
59 MODULE_LICENSE("GPL");
60
61 static unsigned int debug;
62 module_param(debug, int, 0644);
63 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
64
65 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
66
67 #define dprintk(level, fmt, arg...) do {                        \
68 if (debug >= level)                                             \
69         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
70 } while (0)
71
72 struct em28xx_dvb {
73         struct dvb_frontend        *fe[2];
74
75         /* feed count management */
76         struct mutex               lock;
77         int                        nfeeds;
78
79         /* general boilerplate stuff */
80         struct dvb_adapter         adapter;
81         struct dvb_demux           demux;
82         struct dmxdev              dmxdev;
83         struct dmx_frontend        fe_hw;
84         struct dmx_frontend        fe_mem;
85         struct dvb_net             net;
86
87         /* Due to DRX-K - probably need changes */
88         int (*gate_ctrl)(struct dvb_frontend *, int);
89         struct semaphore      pll_mutex;
90         bool                    dont_attach_fe1;
91         int                     lna_gpio;
92         struct i2c_client       *i2c_client_tuner;
93 };
94
95
96 static inline void print_err_status(struct em28xx *dev,
97                                      int packet, int status)
98 {
99         char *errmsg = "Unknown";
100
101         switch (status) {
102         case -ENOENT:
103                 errmsg = "unlinked synchronuously";
104                 break;
105         case -ECONNRESET:
106                 errmsg = "unlinked asynchronuously";
107                 break;
108         case -ENOSR:
109                 errmsg = "Buffer error (overrun)";
110                 break;
111         case -EPIPE:
112                 errmsg = "Stalled (device not responding)";
113                 break;
114         case -EOVERFLOW:
115                 errmsg = "Babble (bad cable?)";
116                 break;
117         case -EPROTO:
118                 errmsg = "Bit-stuff error (bad cable?)";
119                 break;
120         case -EILSEQ:
121                 errmsg = "CRC/Timeout (could be anything)";
122                 break;
123         case -ETIME:
124                 errmsg = "Device does not respond";
125                 break;
126         }
127         if (packet < 0) {
128                 dprintk(1, "URB status %d [%s].\n", status, errmsg);
129         } else {
130                 dprintk(1, "URB packet %d, status %d [%s].\n",
131                         packet, status, errmsg);
132         }
133 }
134
135 static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
136 {
137         int xfer_bulk, num_packets, i;
138
139         if (!dev)
140                 return 0;
141
142         if (dev->disconnected)
143                 return 0;
144
145         if (urb->status < 0)
146                 print_err_status(dev, -1, urb->status);
147
148         xfer_bulk = usb_pipebulk(urb->pipe);
149
150         if (xfer_bulk) /* bulk */
151                 num_packets = 1;
152         else /* isoc */
153                 num_packets = urb->number_of_packets;
154
155         for (i = 0; i < num_packets; i++) {
156                 if (xfer_bulk) {
157                         if (urb->status < 0) {
158                                 print_err_status(dev, i, urb->status);
159                                 if (urb->status != -EPROTO)
160                                         continue;
161                         }
162                         dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
163                                         urb->actual_length);
164                 } else {
165                         if (urb->iso_frame_desc[i].status < 0) {
166                                 print_err_status(dev, i,
167                                                  urb->iso_frame_desc[i].status);
168                                 if (urb->iso_frame_desc[i].status != -EPROTO)
169                                         continue;
170                         }
171                         dvb_dmx_swfilter(&dev->dvb->demux,
172                                          urb->transfer_buffer +
173                                          urb->iso_frame_desc[i].offset,
174                                          urb->iso_frame_desc[i].actual_length);
175                 }
176         }
177
178         return 0;
179 }
180
181 static int em28xx_start_streaming(struct em28xx_dvb *dvb)
182 {
183         int rc;
184         struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
185         struct em28xx *dev = i2c_bus->dev;
186         int dvb_max_packet_size, packet_multiplier, dvb_alt;
187
188         if (dev->dvb_xfer_bulk) {
189                 if (!dev->dvb_ep_bulk)
190                         return -ENODEV;
191                 dvb_max_packet_size = 512; /* USB 2.0 spec */
192                 packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
193                 dvb_alt = 0;
194         } else { /* isoc */
195                 if (!dev->dvb_ep_isoc)
196                         return -ENODEV;
197                 dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
198                 if (dvb_max_packet_size < 0)
199                         return dvb_max_packet_size;
200                 packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
201                 dvb_alt = dev->dvb_alt_isoc;
202         }
203
204         usb_set_interface(dev->udev, 0, dvb_alt);
205         rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
206         if (rc < 0)
207                 return rc;
208
209         dprintk(1, "Using %d buffers each with %d x %d bytes\n",
210                 EM28XX_DVB_NUM_BUFS,
211                 packet_multiplier,
212                 dvb_max_packet_size);
213
214         return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
215                                     dev->dvb_xfer_bulk,
216                                     EM28XX_DVB_NUM_BUFS,
217                                     dvb_max_packet_size,
218                                     packet_multiplier,
219                                     em28xx_dvb_urb_data_copy);
220 }
221
222 static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
223 {
224         struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
225         struct em28xx *dev = i2c_bus->dev;
226
227         em28xx_stop_urbs(dev);
228
229         return 0;
230 }
231
232 static int em28xx_start_feed(struct dvb_demux_feed *feed)
233 {
234         struct dvb_demux *demux  = feed->demux;
235         struct em28xx_dvb *dvb = demux->priv;
236         int rc, ret;
237
238         if (!demux->dmx.frontend)
239                 return -EINVAL;
240
241         mutex_lock(&dvb->lock);
242         dvb->nfeeds++;
243         rc = dvb->nfeeds;
244
245         if (dvb->nfeeds == 1) {
246                 ret = em28xx_start_streaming(dvb);
247                 if (ret < 0)
248                         rc = ret;
249         }
250
251         mutex_unlock(&dvb->lock);
252         return rc;
253 }
254
255 static int em28xx_stop_feed(struct dvb_demux_feed *feed)
256 {
257         struct dvb_demux *demux  = feed->demux;
258         struct em28xx_dvb *dvb = demux->priv;
259         int err = 0;
260
261         mutex_lock(&dvb->lock);
262         dvb->nfeeds--;
263
264         if (0 == dvb->nfeeds)
265                 err = em28xx_stop_streaming(dvb);
266
267         mutex_unlock(&dvb->lock);
268         return err;
269 }
270
271
272
273 /* ------------------------------------------------------------------ */
274 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
275 {
276         struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
277         struct em28xx *dev = i2c_bus->dev;
278
279         if (acquire)
280                 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
281         else
282                 return em28xx_set_mode(dev, EM28XX_SUSPEND);
283 }
284
285 /* ------------------------------------------------------------------ */
286
287 static struct lgdt330x_config em2880_lgdt3303_dev = {
288         .demod_address = 0x0e,
289         .demod_chip = LGDT3303,
290 };
291
292 static struct lgdt3305_config em2870_lgdt3304_dev = {
293         .i2c_addr           = 0x0e,
294         .demod_chip         = LGDT3304,
295         .spectral_inversion = 1,
296         .deny_i2c_rptr      = 1,
297         .mpeg_mode          = LGDT3305_MPEG_PARALLEL,
298         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
299         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
300         .vsb_if_khz         = 3250,
301         .qam_if_khz         = 4000,
302 };
303
304 static struct lgdt3305_config em2874_lgdt3305_dev = {
305         .i2c_addr           = 0x0e,
306         .demod_chip         = LGDT3305,
307         .spectral_inversion = 1,
308         .deny_i2c_rptr      = 0,
309         .mpeg_mode          = LGDT3305_MPEG_SERIAL,
310         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
311         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
312         .vsb_if_khz         = 3250,
313         .qam_if_khz         = 4000,
314 };
315
316 static struct s921_config sharp_isdbt = {
317         .demod_address = 0x30 >> 1
318 };
319
320 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
321         .demod_address = (0x1e >> 1),
322         .no_tuner = 1,
323         .parallel_ts = 1,
324         .if2 = 45600,
325 };
326
327 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
328         .demod_address = 0x32 >> 1,
329         .output_mode   = S5H1409_PARALLEL_OUTPUT,
330         .gpio          = S5H1409_GPIO_OFF,
331         .inversion     = S5H1409_INVERSION_OFF,
332         .status_mode   = S5H1409_DEMODLOCKING,
333         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
334 };
335
336 static struct tda18271_std_map kworld_a340_std_map = {
337         .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
338                       .if_lvl = 1, .rfagc_top = 0x37, },
339         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
340                       .if_lvl = 1, .rfagc_top = 0x37, },
341 };
342
343 static struct tda18271_config kworld_a340_config = {
344         .std_map           = &kworld_a340_std_map,
345 };
346
347 static struct tda18271_config kworld_ub435q_v2_config = {
348         .std_map        = &kworld_a340_std_map,
349         .gate           = TDA18271_GATE_DIGITAL,
350 };
351
352 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
353         .demod_address = (0x1e >> 1),
354         .no_tuner = 1,
355         .disable_i2c_gate_ctrl = 1,
356         .parallel_ts = 1,
357         .if2 = 45600,
358 };
359
360 static struct drxd_config em28xx_drxd = {
361         .demod_address = 0x70,
362         .demod_revision = 0xa2,
363         .pll_type = DRXD_PLL_NONE,
364         .clock = 12000,
365         .insert_rs_byte = 1,
366         .IF = 42800000,
367         .disable_i2c_gate_ctrl = 1,
368 };
369
370 static struct drxk_config terratec_h5_drxk = {
371         .adr = 0x29,
372         .single_master = 1,
373         .no_i2c_bridge = 1,
374         .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
375         .qam_demod_parameter_count = 2,
376         .load_firmware_sync = true,
377 };
378
379 static struct drxk_config hauppauge_930c_drxk = {
380         .adr = 0x29,
381         .single_master = 1,
382         .no_i2c_bridge = 1,
383         .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
384         .chunk_size = 56,
385         .qam_demod_parameter_count = 2,
386         .load_firmware_sync = true,
387 };
388
389 static struct drxk_config terratec_htc_stick_drxk = {
390         .adr = 0x29,
391         .single_master = 1,
392         .no_i2c_bridge = 1,
393         .microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
394         .chunk_size = 54,
395         .qam_demod_parameter_count = 2,
396         /* Required for the antenna_gpio to disable LNA. */
397         .antenna_dvbt = true,
398         /* The windows driver uses the same. This will disable LNA. */
399         .antenna_gpio = 0x6,
400         .load_firmware_sync = true,
401 };
402
403 static struct drxk_config maxmedia_ub425_tc_drxk = {
404         .adr = 0x29,
405         .single_master = 1,
406         .no_i2c_bridge = 1,
407         .microcode_name = "dvb-demod-drxk-01.fw",
408         .chunk_size = 62,
409         .load_firmware_sync = true,
410         .qam_demod_parameter_count = 2,
411 };
412
413 static struct drxk_config pctv_520e_drxk = {
414         .adr = 0x29,
415         .single_master = 1,
416         .microcode_name = "dvb-demod-drxk-pctv.fw",
417         .qam_demod_parameter_count = 2,
418         .chunk_size = 58,
419         .antenna_dvbt = true, /* disable LNA */
420         .antenna_gpio = (1 << 2), /* disable LNA */
421         .load_firmware_sync = true,
422 };
423
424 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
425 {
426         struct em28xx_dvb *dvb = fe->sec_priv;
427         int status;
428
429         if (!dvb)
430                 return -EINVAL;
431
432         if (enable) {
433                 down(&dvb->pll_mutex);
434                 status = dvb->gate_ctrl(fe, 1);
435         } else {
436                 status = dvb->gate_ctrl(fe, 0);
437                 up(&dvb->pll_mutex);
438         }
439         return status;
440 }
441
442 static void hauppauge_hvr930c_init(struct em28xx *dev)
443 {
444         int i;
445
446         struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
447                 {EM2874_R80_GPIO_P0_CTRL,       0xff,   0xff,   0x65},
448                 {EM2874_R80_GPIO_P0_CTRL,       0xfb,   0xff,   0x32},
449                 {EM2874_R80_GPIO_P0_CTRL,       0xff,   0xff,   0xb8},
450                 {       -1,                     -1,     -1,     -1},
451         };
452         struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
453                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x01},
454                 {EM2874_R80_GPIO_P0_CTRL,       0xaf,   0xff,   0x65},
455                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x76},
456                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x01},
457                 {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x0b},
458                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x40},
459
460                 {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x65},
461                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x65},
462                 {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x0b},
463                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x65},
464
465                 {       -1,                     -1,     -1,     -1},
466         };
467
468         struct {
469                 unsigned char r[4];
470                 int len;
471         } regs[] = {
472                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
473                 {{ 0x01, 0x02 }, 2},
474                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
475                 {{ 0x01, 0x00 }, 2},
476                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
477                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
478                 {{ 0x01, 0x00 }, 2},
479                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
480                 {{ 0x04, 0x00 }, 2},
481                 {{ 0x00, 0x04 }, 2},
482                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
483                 {{ 0x04, 0x14 }, 2},
484                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
485         };
486
487         em28xx_gpio_set(dev, hauppauge_hvr930c_init);
488         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
489         msleep(10);
490         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
491         msleep(10);
492
493         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
494
495         for (i = 0; i < ARRAY_SIZE(regs); i++)
496                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
497         em28xx_gpio_set(dev, hauppauge_hvr930c_end);
498
499         msleep(100);
500
501         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
502         msleep(30);
503
504         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
505         msleep(10);
506
507 }
508
509 static void terratec_h5_init(struct em28xx *dev)
510 {
511         int i;
512         struct em28xx_reg_seq terratec_h5_init[] = {
513                 {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
514                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
515                 {EM2874_R80_GPIO_P0_CTRL,       0xf2,   0xff,   50},
516                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
517                 {       -1,                     -1,     -1,     -1},
518         };
519         struct em28xx_reg_seq terratec_h5_end[] = {
520                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
521                 {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   50},
522                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
523                 {       -1,                     -1,     -1,     -1},
524         };
525         struct {
526                 unsigned char r[4];
527                 int len;
528         } regs[] = {
529                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
530                 {{ 0x01, 0x02 }, 2},
531                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
532                 {{ 0x01, 0x00 }, 2},
533                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
534                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
535                 {{ 0x01, 0x00 }, 2},
536                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
537                 {{ 0x04, 0x00 }, 2},
538                 {{ 0x00, 0x04 }, 2},
539                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
540                 {{ 0x04, 0x14 }, 2},
541                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
542         };
543
544         em28xx_gpio_set(dev, terratec_h5_init);
545         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
546         msleep(10);
547         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
548         msleep(10);
549
550         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
551
552         for (i = 0; i < ARRAY_SIZE(regs); i++)
553                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
554         em28xx_gpio_set(dev, terratec_h5_end);
555 };
556
557 static void terratec_htc_stick_init(struct em28xx *dev)
558 {
559         int i;
560
561         /*
562          * GPIO configuration:
563          * 0xff: unknown (does not affect DVB-T).
564          * 0xf6: DRX-K (demodulator).
565          * 0xe6: unknown (does not affect DVB-T).
566          * 0xb6: unknown (does not affect DVB-T).
567          */
568         struct em28xx_reg_seq terratec_htc_stick_init[] = {
569                 {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
570                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
571                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   50},
572                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
573                 {       -1,                     -1,     -1,     -1},
574         };
575         struct em28xx_reg_seq terratec_htc_stick_end[] = {
576                 {EM2874_R80_GPIO_P0_CTRL,       0xb6,   0xff,   100},
577                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   50},
578                 {       -1,                     -1,     -1,     -1},
579         };
580
581         /*
582          * Init the analog decoder (not yet supported), but
583          * it's probably still a good idea.
584          */
585         struct {
586                 unsigned char r[4];
587                 int len;
588         } regs[] = {
589                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
590                 {{ 0x01, 0x02 }, 2},
591                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
592                 {{ 0x01, 0x00 }, 2},
593                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
594         };
595
596         em28xx_gpio_set(dev, terratec_htc_stick_init);
597
598         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
599         msleep(10);
600         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
601         msleep(10);
602
603         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
604
605         for (i = 0; i < ARRAY_SIZE(regs); i++)
606                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
607
608         em28xx_gpio_set(dev, terratec_htc_stick_end);
609 };
610
611 static void terratec_htc_usb_xs_init(struct em28xx *dev)
612 {
613         int i;
614
615         struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
616                 {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
617                 {EM2874_R80_GPIO_P0_CTRL,       0xb2,   0xff,   100},
618                 {EM2874_R80_GPIO_P0_CTRL,       0xb2,   0xff,   50},
619                 {EM2874_R80_GPIO_P0_CTRL,       0xb6,   0xff,   100},
620                 {       -1,                     -1,     -1,     -1},
621         };
622         struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
623                 {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   100},
624                 {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   50},
625                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
626                 {       -1,                     -1,     -1,     -1},
627         };
628
629         /*
630          * Init the analog decoder (not yet supported), but
631          * it's probably still a good idea.
632          */
633         struct {
634                 unsigned char r[4];
635                 int len;
636         } regs[] = {
637                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
638                 {{ 0x01, 0x02 }, 2},
639                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
640                 {{ 0x01, 0x00 }, 2},
641                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
642                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
643                 {{ 0x01, 0x00 }, 2},
644                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
645                 {{ 0x04, 0x00 }, 2},
646                 {{ 0x00, 0x04 }, 2},
647                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
648                 {{ 0x04, 0x14 }, 2},
649                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
650         };
651
652         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
653
654         em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
655
656         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
657         msleep(10);
658         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
659         msleep(10);
660
661         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
662
663         for (i = 0; i < ARRAY_SIZE(regs); i++)
664                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
665
666         em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
667 };
668
669 static void pctv_520e_init(struct em28xx *dev)
670 {
671         /*
672          * Init AVF4910B analog decoder. Looks like I2C traffic to
673          * digital demodulator and tuner are routed via AVF4910B.
674          */
675         int i;
676         struct {
677                 unsigned char r[4];
678                 int len;
679         } regs[] = {
680                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
681                 {{ 0x01, 0x02 }, 2},
682                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
683                 {{ 0x01, 0x00 }, 2},
684                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
685                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
686                 {{ 0x01, 0x00 }, 2},
687                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
688         };
689
690         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1; /* 0x41 */
691
692         for (i = 0; i < ARRAY_SIZE(regs); i++)
693                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
694 };
695
696 static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
697 {
698         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
699         struct em28xx *dev = fe->dvb->priv;
700 #ifdef CONFIG_GPIOLIB
701         struct em28xx_dvb *dvb = dev->dvb;
702         int ret;
703         unsigned long flags;
704
705         if (c->lna == 1)
706                 flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
707         else
708                 flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
709
710         ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
711         if (ret)
712                 em28xx_errdev("gpio request failed %d\n", ret);
713         else
714                 gpio_free(dvb->lna_gpio);
715
716         return ret;
717 #else
718         dev_warn(&dev->udev->dev, "%s: LNA control is disabled (lna=%u)\n",
719                         KBUILD_MODNAME, c->lna);
720         return 0;
721 #endif
722 }
723
724 static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
725 {
726         /* Values extracted from a USB trace of the Terratec Windows driver */
727         static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
728         static u8 reset[]          = { RESET,      0x80 };
729         static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
730         static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
731         static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
732         static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
733         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
734         static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
735         static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
736         static u8 tuner_go[]       = { TUNER_GO, 0x01};
737
738         mt352_write(fe, clock_config,   sizeof(clock_config));
739         udelay(200);
740         mt352_write(fe, reset,          sizeof(reset));
741         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
742         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
743         mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
744         mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
745         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
746         mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
747         mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
748         mt352_write(fe, tuner_go,       sizeof(tuner_go));
749         return 0;
750 }
751
752 static struct mt352_config terratec_xs_mt352_cfg = {
753         .demod_address = (0x1e >> 1),
754         .no_tuner = 1,
755         .if2 = 45600,
756         .demod_init = em28xx_mt352_terratec_xs_init,
757 };
758
759 static struct tda10023_config em28xx_tda10023_config = {
760         .demod_address = 0x0c,
761         .invert = 1,
762 };
763
764 static struct cxd2820r_config em28xx_cxd2820r_config = {
765         .i2c_address = (0xd8 >> 1),
766         .ts_mode = CXD2820R_TS_SERIAL,
767 };
768
769 static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
770         .output_opt = TDA18271_OUTPUT_LT_OFF,
771         .gate = TDA18271_GATE_DIGITAL,
772 };
773
774 static const struct tda10071_config em28xx_tda10071_config = {
775         .demod_i2c_addr = 0x55, /* (0xaa >> 1) */
776         .tuner_i2c_addr = 0x14,
777         .i2c_wr_max = 64,
778         .ts_mode = TDA10071_TS_SERIAL,
779         .spec_inv = 0,
780         .xtal = 40444000, /* 40.444 MHz */
781         .pll_multiplier = 20,
782 };
783
784 static const struct a8293_config em28xx_a8293_config = {
785         .i2c_addr = 0x08, /* (0x10 >> 1) */
786 };
787
788 static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
789         .demod_address = (0x1e >> 1),
790         .disable_i2c_gate_ctrl = 1,
791         .no_tuner = 1,
792         .parallel_ts = 1,
793 };
794 static struct qt1010_config em28xx_qt1010_config = {
795         .i2c_address = 0x62
796 };
797
798 static const struct mb86a20s_config c3tech_duo_mb86a20s_config = {
799         .demod_address = 0x10,
800         .is_serial = true,
801 };
802
803 static struct tda18271_std_map mb86a20s_tda18271_config = {
804         .dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
805                       .if_lvl = 1, .rfagc_top = 0x37, },
806 };
807
808 static struct tda18271_config c3tech_duo_tda18271_config = {
809         .std_map = &mb86a20s_tda18271_config,
810         .gate    = TDA18271_GATE_DIGITAL,
811         .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
812 };
813
814 static const struct m88ds3103_config pctv_461e_m88ds3103_config = {
815         .i2c_addr = 0x68,
816         .clock = 27000000,
817         .i2c_wr_max = 33,
818         .clock_out = 0,
819         .ts_mode = M88DS3103_TS_PARALLEL_16,
820         .agc = 0x99,
821 };
822
823 /* ------------------------------------------------------------------ */
824
825 static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
826 {
827         struct dvb_frontend *fe;
828         struct xc2028_config cfg;
829
830         memset(&cfg, 0, sizeof(cfg));
831         cfg.i2c_adap  = &dev->i2c_adap[dev->def_i2c_bus];
832         cfg.i2c_addr  = addr;
833
834         if (!dev->dvb->fe[0]) {
835                 em28xx_errdev("/2: dvb frontend not attached. "
836                                 "Can't attach xc3028\n");
837                 return -EINVAL;
838         }
839
840         fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
841         if (!fe) {
842                 em28xx_errdev("/2: xc3028 attach failed\n");
843                 dvb_frontend_detach(dev->dvb->fe[0]);
844                 dev->dvb->fe[0] = NULL;
845                 return -EINVAL;
846         }
847
848         em28xx_info("%s/2: xc3028 attached\n", dev->name);
849
850         return 0;
851 }
852
853 /* ------------------------------------------------------------------ */
854
855 static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
856                                struct em28xx *dev, struct device *device)
857 {
858         int result;
859
860         mutex_init(&dvb->lock);
861
862         /* register adapter */
863         result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
864                                       adapter_nr);
865         if (result < 0) {
866                 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
867                        dev->name, result);
868                 goto fail_adapter;
869         }
870
871         /* Ensure all frontends negotiate bus access */
872         dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
873         if (dvb->fe[1])
874                 dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
875
876         dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
877
878         /* register frontend */
879         result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
880         if (result < 0) {
881                 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
882                        dev->name, result);
883                 goto fail_frontend0;
884         }
885
886         /* register 2nd frontend */
887         if (dvb->fe[1]) {
888                 result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
889                 if (result < 0) {
890                         printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
891                                 dev->name, result);
892                         goto fail_frontend1;
893                 }
894         }
895
896         /* register demux stuff */
897         dvb->demux.dmx.capabilities =
898                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
899                 DMX_MEMORY_BASED_FILTERING;
900         dvb->demux.priv       = dvb;
901         dvb->demux.filternum  = 256;
902         dvb->demux.feednum    = 256;
903         dvb->demux.start_feed = em28xx_start_feed;
904         dvb->demux.stop_feed  = em28xx_stop_feed;
905
906         result = dvb_dmx_init(&dvb->demux);
907         if (result < 0) {
908                 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
909                        dev->name, result);
910                 goto fail_dmx;
911         }
912
913         dvb->dmxdev.filternum    = 256;
914         dvb->dmxdev.demux        = &dvb->demux.dmx;
915         dvb->dmxdev.capabilities = 0;
916         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
917         if (result < 0) {
918                 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
919                        dev->name, result);
920                 goto fail_dmxdev;
921         }
922
923         dvb->fe_hw.source = DMX_FRONTEND_0;
924         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
925         if (result < 0) {
926                 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
927                        dev->name, result);
928                 goto fail_fe_hw;
929         }
930
931         dvb->fe_mem.source = DMX_MEMORY_FE;
932         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
933         if (result < 0) {
934                 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
935                        dev->name, result);
936                 goto fail_fe_mem;
937         }
938
939         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
940         if (result < 0) {
941                 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
942                        dev->name, result);
943                 goto fail_fe_conn;
944         }
945
946         /* register network adapter */
947         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
948         return 0;
949
950 fail_fe_conn:
951         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
952 fail_fe_mem:
953         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
954 fail_fe_hw:
955         dvb_dmxdev_release(&dvb->dmxdev);
956 fail_dmxdev:
957         dvb_dmx_release(&dvb->demux);
958 fail_dmx:
959         if (dvb->fe[1])
960                 dvb_unregister_frontend(dvb->fe[1]);
961         dvb_unregister_frontend(dvb->fe[0]);
962 fail_frontend1:
963         if (dvb->fe[1])
964                 dvb_frontend_detach(dvb->fe[1]);
965 fail_frontend0:
966         dvb_frontend_detach(dvb->fe[0]);
967         dvb_unregister_adapter(&dvb->adapter);
968 fail_adapter:
969         return result;
970 }
971
972 static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
973 {
974         dvb_net_release(&dvb->net);
975         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
976         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
977         dvb_dmxdev_release(&dvb->dmxdev);
978         dvb_dmx_release(&dvb->demux);
979         if (dvb->fe[1])
980                 dvb_unregister_frontend(dvb->fe[1]);
981         dvb_unregister_frontend(dvb->fe[0]);
982         if (dvb->fe[1] && !dvb->dont_attach_fe1)
983                 dvb_frontend_detach(dvb->fe[1]);
984         dvb_frontend_detach(dvb->fe[0]);
985         dvb_unregister_adapter(&dvb->adapter);
986 }
987
988 static int em28xx_dvb_init(struct em28xx *dev)
989 {
990         int result = 0, mfe_shared = 0;
991         struct em28xx_dvb *dvb;
992
993         if (!dev->board.has_dvb) {
994                 /* This device does not support the extension */
995                 printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
996                 return 0;
997         }
998
999         dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
1000
1001         if (dvb == NULL) {
1002                 em28xx_info("em28xx_dvb: memory allocation failed\n");
1003                 return -ENOMEM;
1004         }
1005         dev->dvb = dvb;
1006         dvb->fe[0] = dvb->fe[1] = NULL;
1007
1008         mutex_lock(&dev->lock);
1009         em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
1010         /* init frontend */
1011         switch (dev->model) {
1012         case EM2874_BOARD_LEADERSHIP_ISDBT:
1013                 dvb->fe[0] = dvb_attach(s921_attach,
1014                                 &sharp_isdbt, &dev->i2c_adap[dev->def_i2c_bus]);
1015
1016                 if (!dvb->fe[0]) {
1017                         result = -EINVAL;
1018                         goto out_free;
1019                 }
1020
1021                 break;
1022         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
1023         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
1024         case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
1025         case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
1026                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
1027                                            &em2880_lgdt3303_dev,
1028                                            &dev->i2c_adap[dev->def_i2c_bus]);
1029                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1030                         result = -EINVAL;
1031                         goto out_free;
1032                 }
1033                 break;
1034         case EM2880_BOARD_KWORLD_DVB_310U:
1035                 dvb->fe[0] = dvb_attach(zl10353_attach,
1036                                            &em28xx_zl10353_with_xc3028,
1037                                            &dev->i2c_adap[dev->def_i2c_bus]);
1038                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1039                         result = -EINVAL;
1040                         goto out_free;
1041                 }
1042                 break;
1043         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
1044         case EM2882_BOARD_TERRATEC_HYBRID_XS:
1045         case EM2880_BOARD_EMPIRE_DUAL_TV:
1046                 dvb->fe[0] = dvb_attach(zl10353_attach,
1047                                            &em28xx_zl10353_xc3028_no_i2c_gate,
1048                                            &dev->i2c_adap[dev->def_i2c_bus]);
1049                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1050                         result = -EINVAL;
1051                         goto out_free;
1052                 }
1053                 break;
1054         case EM2880_BOARD_TERRATEC_HYBRID_XS:
1055         case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
1056         case EM2881_BOARD_PINNACLE_HYBRID_PRO:
1057         case EM2882_BOARD_DIKOM_DK300:
1058         case EM2882_BOARD_KWORLD_VS_DVBT:
1059                 dvb->fe[0] = dvb_attach(zl10353_attach,
1060                                            &em28xx_zl10353_xc3028_no_i2c_gate,
1061                                            &dev->i2c_adap[dev->def_i2c_bus]);
1062                 if (dvb->fe[0] == NULL) {
1063                         /* This board could have either a zl10353 or a mt352.
1064                            If the chip id isn't for zl10353, try mt352 */
1065                         dvb->fe[0] = dvb_attach(mt352_attach,
1066                                                    &terratec_xs_mt352_cfg,
1067                                                    &dev->i2c_adap[dev->def_i2c_bus]);
1068                 }
1069
1070                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1071                         result = -EINVAL;
1072                         goto out_free;
1073                 }
1074                 break;
1075         case EM2870_BOARD_KWORLD_355U:
1076                 dvb->fe[0] = dvb_attach(zl10353_attach,
1077                                            &em28xx_zl10353_no_i2c_gate_dev,
1078                                            &dev->i2c_adap[dev->def_i2c_bus]);
1079                 if (dvb->fe[0] != NULL)
1080                         dvb_attach(qt1010_attach, dvb->fe[0],
1081                                    &dev->i2c_adap[dev->def_i2c_bus], &em28xx_qt1010_config);
1082                 break;
1083         case EM2883_BOARD_KWORLD_HYBRID_330U:
1084         case EM2882_BOARD_EVGA_INDTUBE:
1085                 dvb->fe[0] = dvb_attach(s5h1409_attach,
1086                                            &em28xx_s5h1409_with_xc3028,
1087                                            &dev->i2c_adap[dev->def_i2c_bus]);
1088                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1089                         result = -EINVAL;
1090                         goto out_free;
1091                 }
1092                 break;
1093         case EM2882_BOARD_KWORLD_ATSC_315U:
1094                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
1095                                            &em2880_lgdt3303_dev,
1096                                            &dev->i2c_adap[dev->def_i2c_bus]);
1097                 if (dvb->fe[0] != NULL) {
1098                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1099                                 &dev->i2c_adap[dev->def_i2c_bus], 0x61, TUNER_THOMSON_DTT761X)) {
1100                                 result = -EINVAL;
1101                                 goto out_free;
1102                         }
1103                 }
1104                 break;
1105         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
1106         case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
1107                 dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
1108                                            &dev->i2c_adap[dev->def_i2c_bus], &dev->udev->dev);
1109                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1110                         result = -EINVAL;
1111                         goto out_free;
1112                 }
1113                 break;
1114         case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
1115                 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
1116                 dvb->fe[0] = dvb_attach(tda10023_attach,
1117                         &em28xx_tda10023_config,
1118                         &dev->i2c_adap[dev->def_i2c_bus], 0x48);
1119                 if (dvb->fe[0]) {
1120                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1121                                 &dev->i2c_adap[dev->def_i2c_bus], 0x60, TUNER_PHILIPS_CU1216L)) {
1122                                 result = -EINVAL;
1123                                 goto out_free;
1124                         }
1125                 }
1126                 break;
1127         case EM2870_BOARD_KWORLD_A340:
1128                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1129                                            &em2870_lgdt3304_dev,
1130                                            &dev->i2c_adap[dev->def_i2c_bus]);
1131                 if (dvb->fe[0] != NULL)
1132                         dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1133                                    &dev->i2c_adap[dev->def_i2c_bus], &kworld_a340_config);
1134                 break;
1135         case EM28174_BOARD_PCTV_290E:
1136                 /* set default GPIO0 for LNA, used if GPIOLIB is undefined */
1137                 dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
1138                                 CXD2820R_GPIO_L;
1139                 dvb->fe[0] = dvb_attach(cxd2820r_attach,
1140                                         &em28xx_cxd2820r_config,
1141                                         &dev->i2c_adap[dev->def_i2c_bus],
1142                                         &dvb->lna_gpio);
1143                 if (dvb->fe[0]) {
1144                         /* FE 0 attach tuner */
1145                         if (!dvb_attach(tda18271_attach,
1146                                         dvb->fe[0],
1147                                         0x60,
1148                                         &dev->i2c_adap[dev->def_i2c_bus],
1149                                         &em28xx_cxd2820r_tda18271_config)) {
1150
1151                                 dvb_frontend_detach(dvb->fe[0]);
1152                                 result = -EINVAL;
1153                                 goto out_free;
1154                         }
1155
1156 #ifdef CONFIG_GPIOLIB
1157                         /* enable LNA for DVB-T, DVB-T2 and DVB-C */
1158                         result = gpio_request_one(dvb->lna_gpio,
1159                                         GPIOF_OUT_INIT_LOW, NULL);
1160                         if (result)
1161                                 em28xx_errdev("gpio request failed %d\n",
1162                                                 result);
1163                         else
1164                                 gpio_free(dvb->lna_gpio);
1165
1166                         result = 0; /* continue even set LNA fails */
1167 #endif
1168                         dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
1169                 }
1170
1171                 break;
1172         case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
1173         {
1174                 struct xc5000_config cfg;
1175                 hauppauge_hvr930c_init(dev);
1176
1177                 dvb->fe[0] = dvb_attach(drxk_attach,
1178                                         &hauppauge_930c_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1179                 if (!dvb->fe[0]) {
1180                         result = -EINVAL;
1181                         goto out_free;
1182                 }
1183                 /* FIXME: do we need a pll semaphore? */
1184                 dvb->fe[0]->sec_priv = dvb;
1185                 sema_init(&dvb->pll_mutex, 1);
1186                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1187                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1188
1189                 /* Attach xc5000 */
1190                 memset(&cfg, 0, sizeof(cfg));
1191                 cfg.i2c_address  = 0x61;
1192                 cfg.if_khz = 4000;
1193
1194                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1195                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1196                 if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
1197                                 &cfg)) {
1198                         result = -EINVAL;
1199                         goto out_free;
1200                 }
1201                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1202                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1203
1204                 break;
1205         }
1206         case EM2884_BOARD_TERRATEC_H5:
1207                 terratec_h5_init(dev);
1208
1209                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1210                 if (!dvb->fe[0]) {
1211                         result = -EINVAL;
1212                         goto out_free;
1213                 }
1214                 /* FIXME: do we need a pll semaphore? */
1215                 dvb->fe[0]->sec_priv = dvb;
1216                 sema_init(&dvb->pll_mutex, 1);
1217                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1218                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1219
1220                 /* Attach tda18271 to DVB-C frontend */
1221                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1222                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1223                 if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
1224                         result = -EINVAL;
1225                         goto out_free;
1226                 }
1227                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1228                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1229
1230                 break;
1231         case EM2884_BOARD_C3TECH_DIGITAL_DUO:
1232                 dvb->fe[0] = dvb_attach(mb86a20s_attach,
1233                                            &c3tech_duo_mb86a20s_config,
1234                                            &dev->i2c_adap[dev->def_i2c_bus]);
1235                 if (dvb->fe[0] != NULL)
1236                         dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1237                                    &dev->i2c_adap[dev->def_i2c_bus],
1238                                    &c3tech_duo_tda18271_config);
1239                 break;
1240         case EM28174_BOARD_PCTV_460E:
1241                 /* attach demod */
1242                 dvb->fe[0] = dvb_attach(tda10071_attach,
1243                         &em28xx_tda10071_config, &dev->i2c_adap[dev->def_i2c_bus]);
1244
1245                 /* attach SEC */
1246                 if (dvb->fe[0])
1247                         dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
1248                                 &em28xx_a8293_config);
1249                 break;
1250         case EM2874_BOARD_DELOCK_61959:
1251         case EM2874_BOARD_MAXMEDIA_UB425_TC:
1252                 /* attach demodulator */
1253                 dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1254                                 &dev->i2c_adap[dev->def_i2c_bus]);
1255
1256                 if (dvb->fe[0]) {
1257                         /* disable I2C-gate */
1258                         dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
1259
1260                         /* attach tuner */
1261                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1262                                         &dev->i2c_adap[dev->def_i2c_bus],
1263                                         &em28xx_cxd2820r_tda18271_config)) {
1264                                 dvb_frontend_detach(dvb->fe[0]);
1265                                 result = -EINVAL;
1266                                 goto out_free;
1267                         }
1268                 }
1269                 break;
1270         case EM2884_BOARD_PCTV_510E:
1271         case EM2884_BOARD_PCTV_520E:
1272                 pctv_520e_init(dev);
1273
1274                 /* attach demodulator */
1275                 dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1276                                 &dev->i2c_adap[dev->def_i2c_bus]);
1277
1278                 if (dvb->fe[0]) {
1279                         /* attach tuner */
1280                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1281                                         &dev->i2c_adap[dev->def_i2c_bus],
1282                                         &em28xx_cxd2820r_tda18271_config)) {
1283                                 dvb_frontend_detach(dvb->fe[0]);
1284                                 result = -EINVAL;
1285                                 goto out_free;
1286                         }
1287                 }
1288                 break;
1289         case EM2884_BOARD_CINERGY_HTC_STICK:
1290                 terratec_htc_stick_init(dev);
1291
1292                 /* attach demodulator */
1293                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1294                                         &dev->i2c_adap[dev->def_i2c_bus]);
1295                 if (!dvb->fe[0]) {
1296                         result = -EINVAL;
1297                         goto out_free;
1298                 }
1299
1300                 /* Attach the demodulator. */
1301                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1302                                 &dev->i2c_adap[dev->def_i2c_bus],
1303                                 &em28xx_cxd2820r_tda18271_config)) {
1304                         result = -EINVAL;
1305                         goto out_free;
1306                 }
1307                 break;
1308         case EM2884_BOARD_TERRATEC_HTC_USB_XS:
1309                 terratec_htc_usb_xs_init(dev);
1310
1311                 /* attach demodulator */
1312                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1313                                         &dev->i2c_adap[dev->def_i2c_bus]);
1314                 if (!dvb->fe[0]) {
1315                         result = -EINVAL;
1316                         goto out_free;
1317                 }
1318
1319                 /* Attach the demodulator. */
1320                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1321                                 &dev->i2c_adap[dev->def_i2c_bus],
1322                                 &em28xx_cxd2820r_tda18271_config)) {
1323                         result = -EINVAL;
1324                         goto out_free;
1325                 }
1326                 break;
1327         case EM2874_BOARD_KWORLD_UB435Q_V2:
1328                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1329                                         &em2874_lgdt3305_dev,
1330                                         &dev->i2c_adap[dev->def_i2c_bus]);
1331                 if (!dvb->fe[0]) {
1332                         result = -EINVAL;
1333                         goto out_free;
1334                 }
1335
1336                 /* Attach the demodulator. */
1337                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1338                                 &dev->i2c_adap[dev->def_i2c_bus],
1339                                 &kworld_ub435q_v2_config)) {
1340                         result = -EINVAL;
1341                         goto out_free;
1342                 }
1343                 break;
1344         case EM28178_BOARD_PCTV_461E:
1345                 {
1346                         /* demod I2C adapter */
1347                         struct i2c_adapter *i2c_adapter;
1348                         struct i2c_board_info info;
1349                         struct m88ts2022_config m88ts2022_config = {
1350                                 .clock = 27000000,
1351                         };
1352                         memset(&info, 0, sizeof(struct i2c_board_info));
1353
1354                         /* attach demod */
1355                         dvb->fe[0] = dvb_attach(m88ds3103_attach,
1356                                         &pctv_461e_m88ds3103_config,
1357                                         &dev->i2c_adap[dev->def_i2c_bus],
1358                                         &i2c_adapter);
1359                         if (dvb->fe[0] == NULL) {
1360                                 result = -ENODEV;
1361                                 goto out_free;
1362                         }
1363
1364                         /* attach tuner */
1365                         m88ts2022_config.fe = dvb->fe[0];
1366                         strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE);
1367                         info.addr = 0x60;
1368                         info.platform_data = &m88ts2022_config;
1369                         request_module("m88ts2022");
1370                         dvb->i2c_client_tuner = i2c_new_device(i2c_adapter, &info);
1371
1372                         /* delegate signal strength measurement to tuner */
1373                         dvb->fe[0]->ops.read_signal_strength =
1374                                         dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1375
1376                         /* attach SEC */
1377                         if (!dvb_attach(a8293_attach, dvb->fe[0],
1378                                         &dev->i2c_adap[dev->def_i2c_bus],
1379                                         &em28xx_a8293_config)) {
1380                                 dvb_frontend_detach(dvb->fe[0]);
1381                                 result = -ENODEV;
1382                                 goto out_free;
1383                         }
1384                 }
1385                 break;
1386         default:
1387                 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
1388                                 " isn't supported yet\n");
1389                 break;
1390         }
1391         if (NULL == dvb->fe[0]) {
1392                 em28xx_errdev("/2: frontend initialization failed\n");
1393                 result = -EINVAL;
1394                 goto out_free;
1395         }
1396         /* define general-purpose callback pointer */
1397         dvb->fe[0]->callback = em28xx_tuner_callback;
1398         if (dvb->fe[1])
1399                 dvb->fe[1]->callback = em28xx_tuner_callback;
1400
1401         /* register everything */
1402         result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
1403
1404         if (result < 0)
1405                 goto out_free;
1406
1407         /* MFE lock */
1408         dvb->adapter.mfe_shared = mfe_shared;
1409
1410         em28xx_info("Successfully loaded em28xx-dvb\n");
1411 ret:
1412         em28xx_set_mode(dev, EM28XX_SUSPEND);
1413         mutex_unlock(&dev->lock);
1414         return result;
1415
1416 out_free:
1417         kfree(dvb);
1418         dev->dvb = NULL;
1419         goto ret;
1420 }
1421
1422 static inline void prevent_sleep(struct dvb_frontend_ops *ops)
1423 {
1424         ops->set_voltage = NULL;
1425         ops->sleep = NULL;
1426         ops->tuner_ops.sleep = NULL;
1427 }
1428
1429 static int em28xx_dvb_fini(struct em28xx *dev)
1430 {
1431         if (!dev->board.has_dvb) {
1432                 /* This device does not support the extension */
1433                 return 0;
1434         }
1435
1436         if (dev->dvb) {
1437                 struct em28xx_dvb *dvb = dev->dvb;
1438
1439                 if (dev->disconnected) {
1440                         /* We cannot tell the device to sleep
1441                          * once it has been unplugged. */
1442                         if (dvb->fe[0])
1443                                 prevent_sleep(&dvb->fe[0]->ops);
1444                         if (dvb->fe[1])
1445                                 prevent_sleep(&dvb->fe[1]->ops);
1446                 }
1447
1448                 i2c_release_client(dvb->i2c_client_tuner);
1449                 em28xx_unregister_dvb(dvb);
1450                 kfree(dvb);
1451                 dev->dvb = NULL;
1452         }
1453
1454         return 0;
1455 }
1456
1457 static struct em28xx_ops dvb_ops = {
1458         .id   = EM28XX_DVB,
1459         .name = "Em28xx dvb Extension",
1460         .init = em28xx_dvb_init,
1461         .fini = em28xx_dvb_fini,
1462 };
1463
1464 static int __init em28xx_dvb_register(void)
1465 {
1466         return em28xx_register_extension(&dvb_ops);
1467 }
1468
1469 static void __exit em28xx_dvb_unregister(void)
1470 {
1471         em28xx_unregister_extension(&dvb_ops);
1472 }
1473
1474 module_init(em28xx_dvb_register);
1475 module_exit(em28xx_dvb_unregister);