]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/usb/em28xx/em28xx-dvb.c
Merge tag 'pstore-v4.8' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
[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 "mt2060.h"
42 #include "mt352.h"
43 #include "mt352_priv.h" /* FIXME */
44 #include "tda1002x.h"
45 #include "drx39xyj/drx39xxj.h"
46 #include "tda18271.h"
47 #include "s921.h"
48 #include "drxd.h"
49 #include "cxd2820r.h"
50 #include "tda18271c2dd.h"
51 #include "drxk.h"
52 #include "tda10071.h"
53 #include "tda18212.h"
54 #include "a8293.h"
55 #include "qt1010.h"
56 #include "mb86a20s.h"
57 #include "m88ds3103.h"
58 #include "ts2020.h"
59 #include "si2168.h"
60 #include "si2157.h"
61 #include "tc90522.h"
62 #include "qm1d1c0042.h"
63
64 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
65 MODULE_LICENSE("GPL");
66 MODULE_DESCRIPTION(DRIVER_DESC " - digital TV interface");
67 MODULE_VERSION(EM28XX_VERSION);
68
69 static unsigned int debug;
70 module_param(debug, int, 0644);
71 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
72
73 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
74
75 #define dprintk(level, fmt, arg...) do {                        \
76 if (debug >= level)                                             \
77         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
78 } while (0)
79
80 struct em28xx_dvb {
81         struct dvb_frontend        *fe[2];
82
83         /* feed count management */
84         struct mutex               lock;
85         int                        nfeeds;
86
87         /* general boilerplate stuff */
88         struct dvb_adapter         adapter;
89         struct dvb_demux           demux;
90         struct dmxdev              dmxdev;
91         struct dmx_frontend        fe_hw;
92         struct dmx_frontend        fe_mem;
93         struct dvb_net             net;
94
95         /* Due to DRX-K - probably need changes */
96         int (*gate_ctrl)(struct dvb_frontend *, int);
97         struct semaphore      pll_mutex;
98         bool                    dont_attach_fe1;
99         int                     lna_gpio;
100         struct i2c_client       *i2c_client_demod;
101         struct i2c_client       *i2c_client_tuner;
102         struct i2c_client       *i2c_client_sec;
103 };
104
105 static inline void print_err_status(struct em28xx *dev,
106                                     int packet, int status)
107 {
108         char *errmsg = "Unknown";
109
110         switch (status) {
111         case -ENOENT:
112                 errmsg = "unlinked synchronuously";
113                 break;
114         case -ECONNRESET:
115                 errmsg = "unlinked asynchronuously";
116                 break;
117         case -ENOSR:
118                 errmsg = "Buffer error (overrun)";
119                 break;
120         case -EPIPE:
121                 errmsg = "Stalled (device not responding)";
122                 break;
123         case -EOVERFLOW:
124                 errmsg = "Babble (bad cable?)";
125                 break;
126         case -EPROTO:
127                 errmsg = "Bit-stuff error (bad cable?)";
128                 break;
129         case -EILSEQ:
130                 errmsg = "CRC/Timeout (could be anything)";
131                 break;
132         case -ETIME:
133                 errmsg = "Device does not respond";
134                 break;
135         }
136         if (packet < 0) {
137                 dprintk(1, "URB status %d [%s].\n", status, errmsg);
138         } else {
139                 dprintk(1, "URB packet %d, status %d [%s].\n",
140                         packet, status, errmsg);
141         }
142 }
143
144 static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
145 {
146         int xfer_bulk, num_packets, i;
147
148         if (!dev)
149                 return 0;
150
151         if (dev->disconnected)
152                 return 0;
153
154         if (urb->status < 0)
155                 print_err_status(dev, -1, urb->status);
156
157         xfer_bulk = usb_pipebulk(urb->pipe);
158
159         if (xfer_bulk) /* bulk */
160                 num_packets = 1;
161         else /* isoc */
162                 num_packets = urb->number_of_packets;
163
164         for (i = 0; i < num_packets; i++) {
165                 if (xfer_bulk) {
166                         if (urb->status < 0) {
167                                 print_err_status(dev, i, urb->status);
168                                 if (urb->status != -EPROTO)
169                                         continue;
170                         }
171                         if (!urb->actual_length)
172                                 continue;
173                         dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
174                                          urb->actual_length);
175                 } else {
176                         if (urb->iso_frame_desc[i].status < 0) {
177                                 print_err_status(dev, i,
178                                                  urb->iso_frame_desc[i].status);
179                                 if (urb->iso_frame_desc[i].status != -EPROTO)
180                                         continue;
181                         }
182                         if (!urb->iso_frame_desc[i].actual_length)
183                                 continue;
184                         dvb_dmx_swfilter(&dev->dvb->demux,
185                                          urb->transfer_buffer +
186                                          urb->iso_frame_desc[i].offset,
187                                          urb->iso_frame_desc[i].actual_length);
188                 }
189         }
190
191         return 0;
192 }
193
194 static int em28xx_start_streaming(struct em28xx_dvb *dvb)
195 {
196         int rc;
197         struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
198         struct em28xx *dev = i2c_bus->dev;
199         int dvb_max_packet_size, packet_multiplier, dvb_alt;
200
201         if (dev->dvb_xfer_bulk) {
202                 if (!dev->dvb_ep_bulk)
203                         return -ENODEV;
204                 dvb_max_packet_size = 512; /* USB 2.0 spec */
205                 packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
206                 dvb_alt = 0;
207         } else { /* isoc */
208                 if (!dev->dvb_ep_isoc)
209                         return -ENODEV;
210                 dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
211                 if (dvb_max_packet_size < 0)
212                         return dvb_max_packet_size;
213                 packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
214                 dvb_alt = dev->dvb_alt_isoc;
215         }
216
217         usb_set_interface(dev->udev, dev->ifnum, dvb_alt);
218         rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
219         if (rc < 0)
220                 return rc;
221
222         dprintk(1, "Using %d buffers each with %d x %d bytes, alternate %d\n",
223                 EM28XX_DVB_NUM_BUFS,
224                 packet_multiplier,
225                 dvb_max_packet_size, dvb_alt);
226
227         return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
228                                     dev->dvb_xfer_bulk,
229                                     EM28XX_DVB_NUM_BUFS,
230                                     dvb_max_packet_size,
231                                     packet_multiplier,
232                                     em28xx_dvb_urb_data_copy);
233 }
234
235 static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
236 {
237         struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
238         struct em28xx *dev = i2c_bus->dev;
239
240         em28xx_stop_urbs(dev);
241
242         return 0;
243 }
244
245 static int em28xx_start_feed(struct dvb_demux_feed *feed)
246 {
247         struct dvb_demux *demux  = feed->demux;
248         struct em28xx_dvb *dvb = demux->priv;
249         int rc, ret;
250
251         if (!demux->dmx.frontend)
252                 return -EINVAL;
253
254         mutex_lock(&dvb->lock);
255         dvb->nfeeds++;
256         rc = dvb->nfeeds;
257
258         if (dvb->nfeeds == 1) {
259                 ret = em28xx_start_streaming(dvb);
260                 if (ret < 0)
261                         rc = ret;
262         }
263
264         mutex_unlock(&dvb->lock);
265         return rc;
266 }
267
268 static int em28xx_stop_feed(struct dvb_demux_feed *feed)
269 {
270         struct dvb_demux *demux  = feed->demux;
271         struct em28xx_dvb *dvb = demux->priv;
272         int err = 0;
273
274         mutex_lock(&dvb->lock);
275         dvb->nfeeds--;
276
277         if (0 == dvb->nfeeds)
278                 err = em28xx_stop_streaming(dvb);
279
280         mutex_unlock(&dvb->lock);
281         return err;
282 }
283
284
285 /* ------------------------------------------------------------------ */
286 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
287 {
288         struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
289         struct em28xx *dev = i2c_bus->dev;
290
291         if (acquire)
292                 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
293         else
294                 return em28xx_set_mode(dev, EM28XX_SUSPEND);
295 }
296
297 /* ------------------------------------------------------------------ */
298
299 static struct lgdt330x_config em2880_lgdt3303_dev = {
300         .demod_address = 0x0e,
301         .demod_chip = LGDT3303,
302 };
303
304 static struct lgdt3305_config em2870_lgdt3304_dev = {
305         .i2c_addr           = 0x0e,
306         .demod_chip         = LGDT3304,
307         .spectral_inversion = 1,
308         .deny_i2c_rptr      = 1,
309         .mpeg_mode          = LGDT3305_MPEG_PARALLEL,
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 lgdt3305_config em2874_lgdt3305_dev = {
317         .i2c_addr           = 0x0e,
318         .demod_chip         = LGDT3305,
319         .spectral_inversion = 1,
320         .deny_i2c_rptr      = 0,
321         .mpeg_mode          = LGDT3305_MPEG_SERIAL,
322         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
323         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
324         .vsb_if_khz         = 3250,
325         .qam_if_khz         = 4000,
326 };
327
328 static struct lgdt3305_config em2874_lgdt3305_nogate_dev = {
329         .i2c_addr           = 0x0e,
330         .demod_chip         = LGDT3305,
331         .spectral_inversion = 1,
332         .deny_i2c_rptr      = 1,
333         .mpeg_mode          = LGDT3305_MPEG_SERIAL,
334         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
335         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
336         .vsb_if_khz         = 3600,
337         .qam_if_khz         = 3600,
338 };
339
340 static struct s921_config sharp_isdbt = {
341         .demod_address = 0x30 >> 1
342 };
343
344 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
345         .demod_address = (0x1e >> 1),
346         .no_tuner = 1,
347         .parallel_ts = 1,
348         .if2 = 45600,
349 };
350
351 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
352         .demod_address = 0x32 >> 1,
353         .output_mode   = S5H1409_PARALLEL_OUTPUT,
354         .gpio          = S5H1409_GPIO_OFF,
355         .inversion     = S5H1409_INVERSION_OFF,
356         .status_mode   = S5H1409_DEMODLOCKING,
357         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
358 };
359
360 static struct tda18271_std_map kworld_a340_std_map = {
361         .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
362                       .if_lvl = 1, .rfagc_top = 0x37, },
363         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
364                       .if_lvl = 1, .rfagc_top = 0x37, },
365 };
366
367 static struct tda18271_config kworld_a340_config = {
368         .std_map           = &kworld_a340_std_map,
369 };
370
371 static struct tda18271_config kworld_ub435q_v2_config = {
372         .std_map        = &kworld_a340_std_map,
373         .gate           = TDA18271_GATE_DIGITAL,
374 };
375
376 static struct tda18212_config kworld_ub435q_v3_config = {
377         .if_atsc_vsb    = 3600,
378         .if_atsc_qam    = 3600,
379 };
380
381 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
382         .demod_address = (0x1e >> 1),
383         .no_tuner = 1,
384         .disable_i2c_gate_ctrl = 1,
385         .parallel_ts = 1,
386         .if2 = 45600,
387 };
388
389 static struct drxd_config em28xx_drxd = {
390         .demod_address = 0x70,
391         .demod_revision = 0xa2,
392         .pll_type = DRXD_PLL_NONE,
393         .clock = 12000,
394         .insert_rs_byte = 1,
395         .IF = 42800000,
396         .disable_i2c_gate_ctrl = 1,
397 };
398
399 static struct drxk_config terratec_h5_drxk = {
400         .adr = 0x29,
401         .single_master = 1,
402         .no_i2c_bridge = 1,
403         .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
404         .qam_demod_parameter_count = 2,
405 };
406
407 static struct drxk_config hauppauge_930c_drxk = {
408         .adr = 0x29,
409         .single_master = 1,
410         .no_i2c_bridge = 1,
411         .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
412         .chunk_size = 56,
413         .qam_demod_parameter_count = 2,
414 };
415
416 static struct drxk_config terratec_htc_stick_drxk = {
417         .adr = 0x29,
418         .single_master = 1,
419         .no_i2c_bridge = 1,
420         .microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
421         .chunk_size = 54,
422         .qam_demod_parameter_count = 2,
423         /* Required for the antenna_gpio to disable LNA. */
424         .antenna_dvbt = true,
425         /* The windows driver uses the same. This will disable LNA. */
426         .antenna_gpio = 0x6,
427 };
428
429 static struct drxk_config maxmedia_ub425_tc_drxk = {
430         .adr = 0x29,
431         .single_master = 1,
432         .no_i2c_bridge = 1,
433         .microcode_name = "dvb-demod-drxk-01.fw",
434         .chunk_size = 62,
435         .qam_demod_parameter_count = 2,
436 };
437
438 static struct drxk_config pctv_520e_drxk = {
439         .adr = 0x29,
440         .single_master = 1,
441         .microcode_name = "dvb-demod-drxk-pctv.fw",
442         .qam_demod_parameter_count = 2,
443         .chunk_size = 58,
444         .antenna_dvbt = true, /* disable LNA */
445         .antenna_gpio = (1 << 2), /* disable LNA */
446 };
447
448 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
449 {
450         struct em28xx_dvb *dvb = fe->sec_priv;
451         int status;
452
453         if (!dvb)
454                 return -EINVAL;
455
456         if (enable) {
457                 down(&dvb->pll_mutex);
458                 status = dvb->gate_ctrl(fe, 1);
459         } else {
460                 status = dvb->gate_ctrl(fe, 0);
461                 up(&dvb->pll_mutex);
462         }
463         return status;
464 }
465
466 static void hauppauge_hvr930c_init(struct em28xx *dev)
467 {
468         int i;
469
470         struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
471                 {EM2874_R80_GPIO_P0_CTRL,       0xff,   0xff,   0x65},
472                 {EM2874_R80_GPIO_P0_CTRL,       0xfb,   0xff,   0x32},
473                 {EM2874_R80_GPIO_P0_CTRL,       0xff,   0xff,   0xb8},
474                 {       -1,                     -1,     -1,     -1},
475         };
476         struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
477                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x01},
478                 {EM2874_R80_GPIO_P0_CTRL,       0xaf,   0xff,   0x65},
479                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x76},
480                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x01},
481                 {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x0b},
482                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x40},
483
484                 {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x65},
485                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x65},
486                 {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x0b},
487                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x65},
488
489                 {       -1,                     -1,     -1,     -1},
490         };
491
492         struct {
493                 unsigned char r[4];
494                 int len;
495         } regs[] = {
496                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
497                 {{ 0x01, 0x02 }, 2},
498                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
499                 {{ 0x01, 0x00 }, 2},
500                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
501                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
502                 {{ 0x01, 0x00 }, 2},
503                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
504                 {{ 0x04, 0x00 }, 2},
505                 {{ 0x00, 0x04 }, 2},
506                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
507                 {{ 0x04, 0x14 }, 2},
508                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
509         };
510
511         em28xx_gpio_set(dev, hauppauge_hvr930c_init);
512         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
513         msleep(10);
514         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
515         msleep(10);
516
517         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
518
519         for (i = 0; i < ARRAY_SIZE(regs); i++)
520                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
521         em28xx_gpio_set(dev, hauppauge_hvr930c_end);
522
523         msleep(100);
524
525         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
526         msleep(30);
527
528         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
529         msleep(10);
530
531 }
532
533 static void terratec_h5_init(struct em28xx *dev)
534 {
535         int i;
536         struct em28xx_reg_seq terratec_h5_init[] = {
537                 {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
538                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
539                 {EM2874_R80_GPIO_P0_CTRL,       0xf2,   0xff,   50},
540                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
541                 {       -1,                     -1,     -1,     -1},
542         };
543         struct em28xx_reg_seq terratec_h5_end[] = {
544                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
545                 {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   50},
546                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
547                 {       -1,                     -1,     -1,     -1},
548         };
549         struct {
550                 unsigned char r[4];
551                 int len;
552         } regs[] = {
553                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
554                 {{ 0x01, 0x02 }, 2},
555                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
556                 {{ 0x01, 0x00 }, 2},
557                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
558                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
559                 {{ 0x01, 0x00 }, 2},
560                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
561                 {{ 0x04, 0x00 }, 2},
562                 {{ 0x00, 0x04 }, 2},
563                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
564                 {{ 0x04, 0x14 }, 2},
565                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
566         };
567
568         em28xx_gpio_set(dev, terratec_h5_init);
569         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
570         msleep(10);
571         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
572         msleep(10);
573
574         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
575
576         for (i = 0; i < ARRAY_SIZE(regs); i++)
577                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
578         em28xx_gpio_set(dev, terratec_h5_end);
579 };
580
581 static void terratec_htc_stick_init(struct em28xx *dev)
582 {
583         int i;
584
585         /*
586          * GPIO configuration:
587          * 0xff: unknown (does not affect DVB-T).
588          * 0xf6: DRX-K (demodulator).
589          * 0xe6: unknown (does not affect DVB-T).
590          * 0xb6: unknown (does not affect DVB-T).
591          */
592         struct em28xx_reg_seq terratec_htc_stick_init[] = {
593                 {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
594                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
595                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   50},
596                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
597                 {       -1,                     -1,     -1,     -1},
598         };
599         struct em28xx_reg_seq terratec_htc_stick_end[] = {
600                 {EM2874_R80_GPIO_P0_CTRL,       0xb6,   0xff,   100},
601                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   50},
602                 {       -1,                     -1,     -1,     -1},
603         };
604
605         /*
606          * Init the analog decoder (not yet supported), but
607          * it's probably still a good idea.
608          */
609         struct {
610                 unsigned char r[4];
611                 int len;
612         } regs[] = {
613                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
614                 {{ 0x01, 0x02 }, 2},
615                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
616                 {{ 0x01, 0x00 }, 2},
617                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
618         };
619
620         em28xx_gpio_set(dev, terratec_htc_stick_init);
621
622         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
623         msleep(10);
624         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
625         msleep(10);
626
627         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
628
629         for (i = 0; i < ARRAY_SIZE(regs); i++)
630                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
631
632         em28xx_gpio_set(dev, terratec_htc_stick_end);
633 };
634
635 static void terratec_htc_usb_xs_init(struct em28xx *dev)
636 {
637         int i;
638
639         struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
640                 {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
641                 {EM2874_R80_GPIO_P0_CTRL,       0xb2,   0xff,   100},
642                 {EM2874_R80_GPIO_P0_CTRL,       0xb2,   0xff,   50},
643                 {EM2874_R80_GPIO_P0_CTRL,       0xb6,   0xff,   100},
644                 {       -1,                     -1,     -1,     -1},
645         };
646         struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
647                 {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   100},
648                 {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   50},
649                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
650                 {       -1,                     -1,     -1,     -1},
651         };
652
653         /*
654          * Init the analog decoder (not yet supported), but
655          * it's probably still a good idea.
656          */
657         struct {
658                 unsigned char r[4];
659                 int len;
660         } regs[] = {
661                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
662                 {{ 0x01, 0x02 }, 2},
663                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
664                 {{ 0x01, 0x00 }, 2},
665                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
666                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
667                 {{ 0x01, 0x00 }, 2},
668                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
669                 {{ 0x04, 0x00 }, 2},
670                 {{ 0x00, 0x04 }, 2},
671                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
672                 {{ 0x04, 0x14 }, 2},
673                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
674         };
675
676         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
677
678         em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
679
680         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
681         msleep(10);
682         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
683         msleep(10);
684
685         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
686
687         for (i = 0; i < ARRAY_SIZE(regs); i++)
688                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
689
690         em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
691 };
692
693 static void pctv_520e_init(struct em28xx *dev)
694 {
695         /*
696          * Init AVF4910B analog decoder. Looks like I2C traffic to
697          * digital demodulator and tuner are routed via AVF4910B.
698          */
699         int i;
700         struct {
701                 unsigned char r[4];
702                 int len;
703         } regs[] = {
704                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
705                 {{ 0x01, 0x02 }, 2},
706                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
707                 {{ 0x01, 0x00 }, 2},
708                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
709                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
710                 {{ 0x01, 0x00 }, 2},
711                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
712         };
713
714         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1; /* 0x41 */
715
716         for (i = 0; i < ARRAY_SIZE(regs); i++)
717                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
718 };
719
720 static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
721 {
722         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
723         struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
724         struct em28xx *dev = i2c_bus->dev;
725 #ifdef CONFIG_GPIOLIB
726         struct em28xx_dvb *dvb = dev->dvb;
727         int ret;
728         unsigned long flags;
729
730         if (c->lna == 1)
731                 flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
732         else
733                 flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
734
735         ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
736         if (ret)
737                 em28xx_errdev("gpio request failed %d\n", ret);
738         else
739                 gpio_free(dvb->lna_gpio);
740
741         return ret;
742 #else
743         dev_warn(&dev->udev->dev, "%s: LNA control is disabled (lna=%u)\n",
744                  KBUILD_MODNAME, c->lna);
745         return 0;
746 #endif
747 }
748
749 static int em28xx_pctv_292e_set_lna(struct dvb_frontend *fe)
750 {
751         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
752         struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
753         struct em28xx *dev = i2c_bus->dev;
754         u8 lna;
755
756         if (c->lna == 1)
757                 lna = 0x01;
758         else
759                 lna = 0x00;
760
761         return em28xx_write_reg_bits(dev, EM2874_R80_GPIO_P0_CTRL, lna, 0x01);
762 }
763
764 static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
765 {
766         /* Values extracted from a USB trace of the Terratec Windows driver */
767         static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
768         static u8 reset[]          = { RESET,      0x80 };
769         static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
770         static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
771         static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
772         static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
773         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
774         static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
775         static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
776         static u8 tuner_go[]       = { TUNER_GO, 0x01};
777
778         mt352_write(fe, clock_config,   sizeof(clock_config));
779         udelay(200);
780         mt352_write(fe, reset,          sizeof(reset));
781         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
782         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
783         mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
784         mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
785         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
786         mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
787         mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
788         mt352_write(fe, tuner_go,       sizeof(tuner_go));
789         return 0;
790 }
791
792 static void px_bcud_init(struct em28xx *dev)
793 {
794         int i;
795         struct {
796                 unsigned char r[4];
797                 int len;
798         } regs1[] = {
799                 {{ 0x0e, 0x77 }, 2},
800                 {{ 0x0f, 0x77 }, 2},
801                 {{ 0x03, 0x90 }, 2},
802         }, regs2[] = {
803                 {{ 0x07, 0x01 }, 2},
804                 {{ 0x08, 0x10 }, 2},
805                 {{ 0x13, 0x00 }, 2},
806                 {{ 0x17, 0x00 }, 2},
807                 {{ 0x03, 0x01 }, 2},
808                 {{ 0x10, 0xb1 }, 2},
809                 {{ 0x11, 0x40 }, 2},
810                 {{ 0x85, 0x7a }, 2},
811                 {{ 0x87, 0x04 }, 2},
812         };
813         static struct em28xx_reg_seq gpio[] = {
814                 {EM28XX_R06_I2C_CLK,            0x40,   0xff,   300},
815                 {EM2874_R80_GPIO_P0_CTRL,       0xfd,   0xff,   60},
816                 {EM28XX_R15_RGAIN,              0x20,   0xff,   0},
817                 {EM28XX_R16_GGAIN,              0x20,   0xff,   0},
818                 {EM28XX_R17_BGAIN,              0x20,   0xff,   0},
819                 {EM28XX_R18_ROFFSET,            0x00,   0xff,   0},
820                 {EM28XX_R19_GOFFSET,            0x00,   0xff,   0},
821                 {EM28XX_R1A_BOFFSET,            0x00,   0xff,   0},
822                 {EM28XX_R23_UOFFSET,            0x00,   0xff,   0},
823                 {EM28XX_R24_VOFFSET,            0x00,   0xff,   0},
824                 {EM28XX_R26_COMPR,              0x00,   0xff,   0},
825                 {0x13,                          0x08,   0xff,   0},
826                 {EM28XX_R12_VINENABLE,          0x27,   0xff,   0},
827                 {EM28XX_R0C_USBSUSP,            0x10,   0xff,   0},
828                 {EM28XX_R27_OUTFMT,             0x00,   0xff,   0},
829                 {EM28XX_R10_VINMODE,            0x00,   0xff,   0},
830                 {EM28XX_R11_VINCTRL,            0x11,   0xff,   0},
831                 {EM2874_R50_IR_CONFIG,          0x01,   0xff,   0},
832                 {EM2874_R5F_TS_ENABLE,          0x80,   0xff,   0},
833                 {EM28XX_R06_I2C_CLK,            0x46,   0xff,   0},
834         };
835         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x46);
836         /* sleeping ISDB-T */
837         dev->dvb->i2c_client_demod->addr = 0x14;
838         for (i = 0; i < ARRAY_SIZE(regs1); i++)
839                 i2c_master_send(dev->dvb->i2c_client_demod, regs1[i].r,
840                                 regs1[i].len);
841         /* sleeping ISDB-S */
842         dev->dvb->i2c_client_demod->addr = 0x15;
843         for (i = 0; i < ARRAY_SIZE(regs2); i++)
844                 i2c_master_send(dev->dvb->i2c_client_demod, regs2[i].r,
845                                 regs2[i].len);
846         for (i = 0; i < ARRAY_SIZE(gpio); i++) {
847                 em28xx_write_reg_bits(dev, gpio[i].reg, gpio[i].val,
848                                       gpio[i].mask);
849                 if (gpio[i].sleep > 0)
850                         msleep(gpio[i].sleep);
851         }
852 };
853
854 static struct mt352_config terratec_xs_mt352_cfg = {
855         .demod_address = (0x1e >> 1),
856         .no_tuner = 1,
857         .if2 = 45600,
858         .demod_init = em28xx_mt352_terratec_xs_init,
859 };
860
861 static struct tda10023_config em28xx_tda10023_config = {
862         .demod_address = 0x0c,
863         .invert = 1,
864 };
865
866 static struct cxd2820r_config em28xx_cxd2820r_config = {
867         .i2c_address = (0xd8 >> 1),
868         .ts_mode = CXD2820R_TS_SERIAL,
869 };
870
871 static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
872         .output_opt = TDA18271_OUTPUT_LT_OFF,
873         .gate = TDA18271_GATE_DIGITAL,
874 };
875
876 static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
877         .demod_address = (0x1e >> 1),
878         .disable_i2c_gate_ctrl = 1,
879         .no_tuner = 1,
880         .parallel_ts = 1,
881 };
882
883 static struct mt2060_config em28xx_mt2060_config = {
884         .i2c_address = 0x60,
885 };
886
887 static struct qt1010_config em28xx_qt1010_config = {
888         .i2c_address = 0x62
889 };
890
891 static const struct mb86a20s_config c3tech_duo_mb86a20s_config = {
892         .demod_address = 0x10,
893         .is_serial = true,
894 };
895
896 static struct tda18271_std_map mb86a20s_tda18271_config = {
897         .dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
898                       .if_lvl = 1, .rfagc_top = 0x37, },
899 };
900
901 static struct tda18271_config c3tech_duo_tda18271_config = {
902         .std_map = &mb86a20s_tda18271_config,
903         .gate    = TDA18271_GATE_DIGITAL,
904         .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
905 };
906
907 static const struct m88ds3103_config pctv_461e_m88ds3103_config = {
908         .i2c_addr = 0x68,
909         .clock = 27000000,
910         .i2c_wr_max = 33,
911         .clock_out = 0,
912         .ts_mode = M88DS3103_TS_PARALLEL,
913         .ts_clk = 16000,
914         .ts_clk_pol = 1,
915         .agc = 0x99,
916 };
917
918 static struct tda18271_std_map drx_j_std_map = {
919         .atsc_6   = { .if_freq = 5000, .agc_mode = 3, .std = 0, .if_lvl = 1,
920                       .rfagc_top = 0x37, },
921         .qam_6    = { .if_freq = 5380, .agc_mode = 3, .std = 3, .if_lvl = 1,
922                       .rfagc_top = 0x37, },
923 };
924
925 static struct tda18271_config pinnacle_80e_dvb_config = {
926         .std_map = &drx_j_std_map,
927         .gate    = TDA18271_GATE_DIGITAL,
928         .role    = TDA18271_MASTER,
929 };
930
931 /* ------------------------------------------------------------------ */
932
933 static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
934 {
935         struct dvb_frontend *fe;
936         struct xc2028_config cfg;
937         struct xc2028_ctrl ctl;
938
939         memset(&cfg, 0, sizeof(cfg));
940         cfg.i2c_adap  = &dev->i2c_adap[dev->def_i2c_bus];
941         cfg.i2c_addr  = addr;
942
943         memset(&ctl, 0, sizeof(ctl));
944         em28xx_setup_xc3028(dev, &ctl);
945         cfg.ctrl  = &ctl;
946
947         if (!dev->dvb->fe[0]) {
948                 em28xx_errdev("/2: dvb frontend not attached. "
949                                 "Can't attach xc3028\n");
950                 return -EINVAL;
951         }
952
953         fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
954         if (!fe) {
955                 em28xx_errdev("/2: xc3028 attach failed\n");
956                 dvb_frontend_detach(dev->dvb->fe[0]);
957                 dev->dvb->fe[0] = NULL;
958                 return -EINVAL;
959         }
960
961         em28xx_info("%s/2: xc3028 attached\n", dev->name);
962
963         return 0;
964 }
965
966 /* ------------------------------------------------------------------ */
967
968 static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
969                                struct em28xx *dev, struct device *device)
970 {
971         int result;
972         bool create_rf_connector = false;
973
974         mutex_init(&dvb->lock);
975
976         /* register adapter */
977         result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
978                                       adapter_nr);
979         if (result < 0) {
980                 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
981                        dev->name, result);
982                 goto fail_adapter;
983         }
984 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
985         dvb->adapter.mdev = dev->media_dev;
986 #endif
987
988         /* Ensure all frontends negotiate bus access */
989         dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
990         if (dvb->fe[1])
991                 dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
992
993         dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
994
995         /* register frontend */
996         result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
997         if (result < 0) {
998                 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
999                        dev->name, result);
1000                 goto fail_frontend0;
1001         }
1002
1003         /* register 2nd frontend */
1004         if (dvb->fe[1]) {
1005                 result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
1006                 if (result < 0) {
1007                         printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
1008                                dev->name, result);
1009                         goto fail_frontend1;
1010                 }
1011         }
1012
1013         /* register demux stuff */
1014         dvb->demux.dmx.capabilities =
1015                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1016                 DMX_MEMORY_BASED_FILTERING;
1017         dvb->demux.priv       = dvb;
1018         dvb->demux.filternum  = 256;
1019         dvb->demux.feednum    = 256;
1020         dvb->demux.start_feed = em28xx_start_feed;
1021         dvb->demux.stop_feed  = em28xx_stop_feed;
1022
1023         result = dvb_dmx_init(&dvb->demux);
1024         if (result < 0) {
1025                 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
1026                        dev->name, result);
1027                 goto fail_dmx;
1028         }
1029
1030         dvb->dmxdev.filternum    = 256;
1031         dvb->dmxdev.demux        = &dvb->demux.dmx;
1032         dvb->dmxdev.capabilities = 0;
1033         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
1034         if (result < 0) {
1035                 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
1036                        dev->name, result);
1037                 goto fail_dmxdev;
1038         }
1039
1040         dvb->fe_hw.source = DMX_FRONTEND_0;
1041         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1042         if (result < 0) {
1043                 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
1044                        dev->name, result);
1045                 goto fail_fe_hw;
1046         }
1047
1048         dvb->fe_mem.source = DMX_MEMORY_FE;
1049         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1050         if (result < 0) {
1051                 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
1052                        dev->name, result);
1053                 goto fail_fe_mem;
1054         }
1055
1056         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1057         if (result < 0) {
1058                 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
1059                        dev->name, result);
1060                 goto fail_fe_conn;
1061         }
1062
1063         /* register network adapter */
1064         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
1065
1066         /* If the analog part won't create RF connectors, DVB will do it */
1067         if (!dev->has_video || (dev->tuner_type == TUNER_ABSENT))
1068                 create_rf_connector = true;
1069
1070         result = dvb_create_media_graph(&dvb->adapter, create_rf_connector);
1071         if (result < 0)
1072                 goto fail_create_graph;
1073
1074         return 0;
1075
1076 fail_create_graph:
1077         dvb_net_release(&dvb->net);
1078 fail_fe_conn:
1079         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1080 fail_fe_mem:
1081         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1082 fail_fe_hw:
1083         dvb_dmxdev_release(&dvb->dmxdev);
1084 fail_dmxdev:
1085         dvb_dmx_release(&dvb->demux);
1086 fail_dmx:
1087         if (dvb->fe[1])
1088                 dvb_unregister_frontend(dvb->fe[1]);
1089         dvb_unregister_frontend(dvb->fe[0]);
1090 fail_frontend1:
1091         if (dvb->fe[1])
1092                 dvb_frontend_detach(dvb->fe[1]);
1093 fail_frontend0:
1094         dvb_frontend_detach(dvb->fe[0]);
1095         dvb_unregister_adapter(&dvb->adapter);
1096 fail_adapter:
1097         return result;
1098 }
1099
1100 static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
1101 {
1102         dvb_net_release(&dvb->net);
1103         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1104         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1105         dvb_dmxdev_release(&dvb->dmxdev);
1106         dvb_dmx_release(&dvb->demux);
1107         if (dvb->fe[1])
1108                 dvb_unregister_frontend(dvb->fe[1]);
1109         dvb_unregister_frontend(dvb->fe[0]);
1110         if (dvb->fe[1] && !dvb->dont_attach_fe1)
1111                 dvb_frontend_detach(dvb->fe[1]);
1112         dvb_frontend_detach(dvb->fe[0]);
1113         dvb_unregister_adapter(&dvb->adapter);
1114 }
1115
1116 static int em28xx_dvb_init(struct em28xx *dev)
1117 {
1118         int result = 0;
1119         struct em28xx_dvb *dvb;
1120
1121         if (dev->is_audio_only) {
1122                 /* Shouldn't initialize IR for this interface */
1123                 return 0;
1124         }
1125
1126         if (!dev->board.has_dvb) {
1127                 /* This device does not support the extension */
1128                 return 0;
1129         }
1130
1131         em28xx_info("Binding DVB extension\n");
1132
1133         dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
1134         if (dvb == NULL) {
1135                 em28xx_info("em28xx_dvb: memory allocation failed\n");
1136                 return -ENOMEM;
1137         }
1138         dev->dvb = dvb;
1139         dvb->fe[0] = dvb->fe[1] = NULL;
1140
1141         /* pre-allocate DVB usb transfer buffers */
1142         if (dev->dvb_xfer_bulk) {
1143                 result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1144                                            dev->dvb_xfer_bulk,
1145                                            EM28XX_DVB_NUM_BUFS,
1146                                            512,
1147                                            EM28XX_DVB_BULK_PACKET_MULTIPLIER);
1148         } else {
1149                 result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1150                                            dev->dvb_xfer_bulk,
1151                                            EM28XX_DVB_NUM_BUFS,
1152                                            dev->dvb_max_pkt_size_isoc,
1153                                            EM28XX_DVB_NUM_ISOC_PACKETS);
1154         }
1155         if (result) {
1156                 em28xx_errdev("em28xx_dvb: failed to pre-allocate USB transfer buffers for DVB.\n");
1157                 kfree(dvb);
1158                 dev->dvb = NULL;
1159                 return result;
1160         }
1161
1162         mutex_lock(&dev->lock);
1163         em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
1164         /* init frontend */
1165         switch (dev->model) {
1166         case EM2874_BOARD_LEADERSHIP_ISDBT:
1167                 dvb->fe[0] = dvb_attach(s921_attach,
1168                                 &sharp_isdbt, &dev->i2c_adap[dev->def_i2c_bus]);
1169
1170                 if (!dvb->fe[0]) {
1171                         result = -EINVAL;
1172                         goto out_free;
1173                 }
1174
1175                 break;
1176         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
1177         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
1178         case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
1179         case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
1180                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
1181                                            &em2880_lgdt3303_dev,
1182                                            &dev->i2c_adap[dev->def_i2c_bus]);
1183                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1184                         result = -EINVAL;
1185                         goto out_free;
1186                 }
1187                 break;
1188         case EM2880_BOARD_KWORLD_DVB_310U:
1189                 dvb->fe[0] = dvb_attach(zl10353_attach,
1190                                            &em28xx_zl10353_with_xc3028,
1191                                            &dev->i2c_adap[dev->def_i2c_bus]);
1192                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1193                         result = -EINVAL;
1194                         goto out_free;
1195                 }
1196                 break;
1197         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
1198         case EM2882_BOARD_TERRATEC_HYBRID_XS:
1199         case EM2880_BOARD_EMPIRE_DUAL_TV:
1200                 dvb->fe[0] = dvb_attach(zl10353_attach,
1201                                            &em28xx_zl10353_xc3028_no_i2c_gate,
1202                                            &dev->i2c_adap[dev->def_i2c_bus]);
1203                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1204                         result = -EINVAL;
1205                         goto out_free;
1206                 }
1207                 break;
1208         case EM2880_BOARD_TERRATEC_HYBRID_XS:
1209         case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
1210         case EM2881_BOARD_PINNACLE_HYBRID_PRO:
1211         case EM2882_BOARD_DIKOM_DK300:
1212         case EM2882_BOARD_KWORLD_VS_DVBT:
1213                 dvb->fe[0] = dvb_attach(zl10353_attach,
1214                                            &em28xx_zl10353_xc3028_no_i2c_gate,
1215                                            &dev->i2c_adap[dev->def_i2c_bus]);
1216                 if (dvb->fe[0] == NULL) {
1217                         /* This board could have either a zl10353 or a mt352.
1218                            If the chip id isn't for zl10353, try mt352 */
1219                         dvb->fe[0] = dvb_attach(mt352_attach,
1220                                                    &terratec_xs_mt352_cfg,
1221                                                    &dev->i2c_adap[dev->def_i2c_bus]);
1222                 }
1223
1224                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1225                         result = -EINVAL;
1226                         goto out_free;
1227                 }
1228                 break;
1229         case EM2870_BOARD_TERRATEC_XS_MT2060:
1230                 dvb->fe[0] = dvb_attach(zl10353_attach,
1231                                                 &em28xx_zl10353_no_i2c_gate_dev,
1232                                                 &dev->i2c_adap[dev->def_i2c_bus]);
1233                 if (dvb->fe[0] != NULL) {
1234                         dvb_attach(mt2060_attach, dvb->fe[0],
1235                                         &dev->i2c_adap[dev->def_i2c_bus],
1236                                         &em28xx_mt2060_config, 1220);
1237                 }
1238                 break;
1239         case EM2870_BOARD_KWORLD_355U:
1240                 dvb->fe[0] = dvb_attach(zl10353_attach,
1241                                            &em28xx_zl10353_no_i2c_gate_dev,
1242                                            &dev->i2c_adap[dev->def_i2c_bus]);
1243                 if (dvb->fe[0] != NULL)
1244                         dvb_attach(qt1010_attach, dvb->fe[0],
1245                                    &dev->i2c_adap[dev->def_i2c_bus], &em28xx_qt1010_config);
1246                 break;
1247         case EM2883_BOARD_KWORLD_HYBRID_330U:
1248         case EM2882_BOARD_EVGA_INDTUBE:
1249                 dvb->fe[0] = dvb_attach(s5h1409_attach,
1250                                            &em28xx_s5h1409_with_xc3028,
1251                                            &dev->i2c_adap[dev->def_i2c_bus]);
1252                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1253                         result = -EINVAL;
1254                         goto out_free;
1255                 }
1256                 break;
1257         case EM2882_BOARD_KWORLD_ATSC_315U:
1258                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
1259                                            &em2880_lgdt3303_dev,
1260                                            &dev->i2c_adap[dev->def_i2c_bus]);
1261                 if (dvb->fe[0] != NULL) {
1262                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1263                                         &dev->i2c_adap[dev->def_i2c_bus],
1264                                         0x61, TUNER_THOMSON_DTT761X)) {
1265                                 result = -EINVAL;
1266                                 goto out_free;
1267                         }
1268                 }
1269                 break;
1270         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
1271         case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
1272                 dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
1273                                            &dev->i2c_adap[dev->def_i2c_bus], &dev->udev->dev);
1274                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1275                         result = -EINVAL;
1276                         goto out_free;
1277                 }
1278                 break;
1279         case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
1280                 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
1281                 dvb->fe[0] = dvb_attach(tda10023_attach,
1282                         &em28xx_tda10023_config,
1283                         &dev->i2c_adap[dev->def_i2c_bus], 0x48);
1284                 if (dvb->fe[0]) {
1285                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1286                                         &dev->i2c_adap[dev->def_i2c_bus],
1287                                         0x60, TUNER_PHILIPS_CU1216L)) {
1288                                 result = -EINVAL;
1289                                 goto out_free;
1290                         }
1291                 }
1292                 break;
1293         case EM2870_BOARD_KWORLD_A340:
1294                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1295                                            &em2870_lgdt3304_dev,
1296                                            &dev->i2c_adap[dev->def_i2c_bus]);
1297                 if (!dvb->fe[0]) {
1298                         result = -EINVAL;
1299                         goto out_free;
1300                 }
1301                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1302                                 &dev->i2c_adap[dev->def_i2c_bus],
1303                         &kworld_a340_config)) {
1304                                 dvb_frontend_detach(dvb->fe[0]);
1305                                 result = -EINVAL;
1306                                 goto out_free;
1307                 }
1308                 break;
1309         case EM28174_BOARD_PCTV_290E:
1310                 /* set default GPIO0 for LNA, used if GPIOLIB is undefined */
1311                 dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
1312                                 CXD2820R_GPIO_L;
1313                 dvb->fe[0] = dvb_attach(cxd2820r_attach,
1314                                         &em28xx_cxd2820r_config,
1315                                         &dev->i2c_adap[dev->def_i2c_bus],
1316                                         &dvb->lna_gpio);
1317                 if (dvb->fe[0]) {
1318                         /* FE 0 attach tuner */
1319                         if (!dvb_attach(tda18271_attach,
1320                                         dvb->fe[0],
1321                                         0x60,
1322                                         &dev->i2c_adap[dev->def_i2c_bus],
1323                                         &em28xx_cxd2820r_tda18271_config)) {
1324
1325                                 dvb_frontend_detach(dvb->fe[0]);
1326                                 result = -EINVAL;
1327                                 goto out_free;
1328                         }
1329
1330 #ifdef CONFIG_GPIOLIB
1331                         /* enable LNA for DVB-T, DVB-T2 and DVB-C */
1332                         result = gpio_request_one(dvb->lna_gpio,
1333                                                   GPIOF_OUT_INIT_LOW, NULL);
1334                         if (result)
1335                                 em28xx_errdev("gpio request failed %d\n",
1336                                               result);
1337                         else
1338                                 gpio_free(dvb->lna_gpio);
1339
1340                         result = 0; /* continue even set LNA fails */
1341 #endif
1342                         dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
1343                 }
1344
1345                 break;
1346         case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
1347         {
1348                 struct xc5000_config cfg;
1349
1350                 hauppauge_hvr930c_init(dev);
1351
1352                 dvb->fe[0] = dvb_attach(drxk_attach,
1353                                         &hauppauge_930c_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1354                 if (!dvb->fe[0]) {
1355                         result = -EINVAL;
1356                         goto out_free;
1357                 }
1358                 /* FIXME: do we need a pll semaphore? */
1359                 dvb->fe[0]->sec_priv = dvb;
1360                 sema_init(&dvb->pll_mutex, 1);
1361                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1362                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1363
1364                 /* Attach xc5000 */
1365                 memset(&cfg, 0, sizeof(cfg));
1366                 cfg.i2c_address  = 0x61;
1367                 cfg.if_khz = 4000;
1368
1369                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1370                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1371                 if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
1372                                 &cfg)) {
1373                         result = -EINVAL;
1374                         goto out_free;
1375                 }
1376                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1377                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1378
1379                 break;
1380         }
1381         case EM2884_BOARD_TERRATEC_H5:
1382                 terratec_h5_init(dev);
1383
1384                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1385                 if (!dvb->fe[0]) {
1386                         result = -EINVAL;
1387                         goto out_free;
1388                 }
1389                 /* FIXME: do we need a pll semaphore? */
1390                 dvb->fe[0]->sec_priv = dvb;
1391                 sema_init(&dvb->pll_mutex, 1);
1392                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1393                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1394
1395                 /* Attach tda18271 to DVB-C frontend */
1396                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1397                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1398                 if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
1399                         result = -EINVAL;
1400                         goto out_free;
1401                 }
1402                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1403                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1404
1405                 break;
1406         case EM2884_BOARD_C3TECH_DIGITAL_DUO:
1407                 dvb->fe[0] = dvb_attach(mb86a20s_attach,
1408                                            &c3tech_duo_mb86a20s_config,
1409                                            &dev->i2c_adap[dev->def_i2c_bus]);
1410                 if (dvb->fe[0] != NULL)
1411                         dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1412                                    &dev->i2c_adap[dev->def_i2c_bus],
1413                                    &c3tech_duo_tda18271_config);
1414                 break;
1415         case EM28174_BOARD_PCTV_460E: {
1416                 struct i2c_client *client;
1417                 struct i2c_board_info board_info;
1418                 struct tda10071_platform_data tda10071_pdata = {};
1419                 struct a8293_platform_data a8293_pdata = {};
1420
1421                 /* attach demod + tuner combo */
1422                 tda10071_pdata.clk = 40444000, /* 40.444 MHz */
1423                 tda10071_pdata.i2c_wr_max = 64,
1424                 tda10071_pdata.ts_mode = TDA10071_TS_SERIAL,
1425                 tda10071_pdata.pll_multiplier = 20,
1426                 tda10071_pdata.tuner_i2c_addr = 0x14,
1427                 memset(&board_info, 0, sizeof(board_info));
1428                 strlcpy(board_info.type, "tda10071_cx24118", I2C_NAME_SIZE);
1429                 board_info.addr = 0x55;
1430                 board_info.platform_data = &tda10071_pdata;
1431                 request_module("tda10071");
1432                 client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1433                 if (client == NULL || client->dev.driver == NULL) {
1434                         result = -ENODEV;
1435                         goto out_free;
1436                 }
1437                 if (!try_module_get(client->dev.driver->owner)) {
1438                         i2c_unregister_device(client);
1439                         result = -ENODEV;
1440                         goto out_free;
1441                 }
1442                 dvb->fe[0] = tda10071_pdata.get_dvb_frontend(client);
1443                 dvb->i2c_client_demod = client;
1444
1445                 /* attach SEC */
1446                 a8293_pdata.dvb_frontend = dvb->fe[0];
1447                 memset(&board_info, 0, sizeof(board_info));
1448                 strlcpy(board_info.type, "a8293", I2C_NAME_SIZE);
1449                 board_info.addr = 0x08;
1450                 board_info.platform_data = &a8293_pdata;
1451                 request_module("a8293");
1452                 client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1453                 if (client == NULL || client->dev.driver == NULL) {
1454                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1455                         i2c_unregister_device(dvb->i2c_client_demod);
1456                         result = -ENODEV;
1457                         goto out_free;
1458                 }
1459                 if (!try_module_get(client->dev.driver->owner)) {
1460                         i2c_unregister_device(client);
1461                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1462                         i2c_unregister_device(dvb->i2c_client_demod);
1463                         result = -ENODEV;
1464                         goto out_free;
1465                 }
1466                 dvb->i2c_client_sec = client;
1467                 break;
1468         }
1469         case EM2874_BOARD_DELOCK_61959:
1470         case EM2874_BOARD_MAXMEDIA_UB425_TC:
1471                 /* attach demodulator */
1472                 dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1473                                 &dev->i2c_adap[dev->def_i2c_bus]);
1474
1475                 if (dvb->fe[0]) {
1476                         /* disable I2C-gate */
1477                         dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
1478
1479                         /* attach tuner */
1480                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1481                                         &dev->i2c_adap[dev->def_i2c_bus],
1482                                         &em28xx_cxd2820r_tda18271_config)) {
1483                                 dvb_frontend_detach(dvb->fe[0]);
1484                                 result = -EINVAL;
1485                                 goto out_free;
1486                         }
1487                 }
1488                 break;
1489         case EM2884_BOARD_PCTV_510E:
1490         case EM2884_BOARD_PCTV_520E:
1491                 pctv_520e_init(dev);
1492
1493                 /* attach demodulator */
1494                 dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1495                                 &dev->i2c_adap[dev->def_i2c_bus]);
1496
1497                 if (dvb->fe[0]) {
1498                         /* attach tuner */
1499                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1500                                         &dev->i2c_adap[dev->def_i2c_bus],
1501                                         &em28xx_cxd2820r_tda18271_config)) {
1502                                 dvb_frontend_detach(dvb->fe[0]);
1503                                 result = -EINVAL;
1504                                 goto out_free;
1505                         }
1506                 }
1507                 break;
1508         case EM2884_BOARD_ELGATO_EYETV_HYBRID_2008:
1509         case EM2884_BOARD_CINERGY_HTC_STICK:
1510                 terratec_htc_stick_init(dev);
1511
1512                 /* attach demodulator */
1513                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1514                                         &dev->i2c_adap[dev->def_i2c_bus]);
1515                 if (!dvb->fe[0]) {
1516                         result = -EINVAL;
1517                         goto out_free;
1518                 }
1519
1520                 /* Attach the demodulator. */
1521                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1522                                 &dev->i2c_adap[dev->def_i2c_bus],
1523                                 &em28xx_cxd2820r_tda18271_config)) {
1524                         result = -EINVAL;
1525                         goto out_free;
1526                 }
1527                 break;
1528         case EM2884_BOARD_TERRATEC_HTC_USB_XS:
1529                 terratec_htc_usb_xs_init(dev);
1530
1531                 /* attach demodulator */
1532                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1533                                         &dev->i2c_adap[dev->def_i2c_bus]);
1534                 if (!dvb->fe[0]) {
1535                         result = -EINVAL;
1536                         goto out_free;
1537                 }
1538
1539                 /* Attach the demodulator. */
1540                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1541                                 &dev->i2c_adap[dev->def_i2c_bus],
1542                                 &em28xx_cxd2820r_tda18271_config)) {
1543                         result = -EINVAL;
1544                         goto out_free;
1545                 }
1546                 break;
1547         case EM2874_BOARD_KWORLD_UB435Q_V2:
1548                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1549                                         &em2874_lgdt3305_dev,
1550                                         &dev->i2c_adap[dev->def_i2c_bus]);
1551                 if (!dvb->fe[0]) {
1552                         result = -EINVAL;
1553                         goto out_free;
1554                 }
1555
1556                 /* Attach the demodulator. */
1557                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1558                                 &dev->i2c_adap[dev->def_i2c_bus],
1559                                 &kworld_ub435q_v2_config)) {
1560                         result = -EINVAL;
1561                         goto out_free;
1562                 }
1563                 break;
1564         case EM2874_BOARD_KWORLD_UB435Q_V3:
1565         {
1566                 struct i2c_client *client;
1567                 struct i2c_adapter *adapter = &dev->i2c_adap[dev->def_i2c_bus];
1568                 struct i2c_board_info board_info = {
1569                         .type = "tda18212",
1570                         .addr = 0x60,
1571                         .platform_data = &kworld_ub435q_v3_config,
1572                 };
1573
1574                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1575                                         &em2874_lgdt3305_nogate_dev,
1576                                         &dev->i2c_adap[dev->def_i2c_bus]);
1577                 if (!dvb->fe[0]) {
1578                         result = -EINVAL;
1579                         goto out_free;
1580                 }
1581
1582                 /* attach tuner */
1583                 kworld_ub435q_v3_config.fe = dvb->fe[0];
1584                 request_module("tda18212");
1585                 client = i2c_new_device(adapter, &board_info);
1586                 if (client == NULL || client->dev.driver == NULL) {
1587                         dvb_frontend_detach(dvb->fe[0]);
1588                         result = -ENODEV;
1589                         goto out_free;
1590                 }
1591
1592                 if (!try_module_get(client->dev.driver->owner)) {
1593                         i2c_unregister_device(client);
1594                         dvb_frontend_detach(dvb->fe[0]);
1595                         result = -ENODEV;
1596                         goto out_free;
1597                 }
1598
1599                 dvb->i2c_client_tuner = client;
1600                 break;
1601         }
1602         case EM2874_BOARD_PCTV_HD_MINI_80E:
1603                 dvb->fe[0] = dvb_attach(drx39xxj_attach, &dev->i2c_adap[dev->def_i2c_bus]);
1604                 if (dvb->fe[0] != NULL) {
1605                         dvb->fe[0] = dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1606                                                 &dev->i2c_adap[dev->def_i2c_bus],
1607                                                 &pinnacle_80e_dvb_config);
1608                         if (!dvb->fe[0]) {
1609                                 result = -EINVAL;
1610                                 goto out_free;
1611                         }
1612                 }
1613                 break;
1614         case EM28178_BOARD_PCTV_461E: {
1615                 struct i2c_client *client;
1616                 struct i2c_adapter *i2c_adapter;
1617                 struct i2c_board_info board_info;
1618                 struct m88ds3103_platform_data m88ds3103_pdata = {};
1619                 struct ts2020_config ts2020_config = {};
1620                 struct a8293_platform_data a8293_pdata = {};
1621
1622                 /* attach demod */
1623                 m88ds3103_pdata.clk = 27000000;
1624                 m88ds3103_pdata.i2c_wr_max = 33;
1625                 m88ds3103_pdata.ts_mode = M88DS3103_TS_PARALLEL;
1626                 m88ds3103_pdata.ts_clk = 16000;
1627                 m88ds3103_pdata.ts_clk_pol = 1;
1628                 m88ds3103_pdata.agc = 0x99;
1629                 memset(&board_info, 0, sizeof(board_info));
1630                 strlcpy(board_info.type, "m88ds3103", I2C_NAME_SIZE);
1631                 board_info.addr = 0x68;
1632                 board_info.platform_data = &m88ds3103_pdata;
1633                 request_module("m88ds3103");
1634                 client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1635                 if (client == NULL || client->dev.driver == NULL) {
1636                         result = -ENODEV;
1637                         goto out_free;
1638                 }
1639                 if (!try_module_get(client->dev.driver->owner)) {
1640                         i2c_unregister_device(client);
1641                         result = -ENODEV;
1642                         goto out_free;
1643                 }
1644                 dvb->fe[0] = m88ds3103_pdata.get_dvb_frontend(client);
1645                 i2c_adapter = m88ds3103_pdata.get_i2c_adapter(client);
1646                 dvb->i2c_client_demod = client;
1647
1648                 /* attach tuner */
1649                 ts2020_config.fe = dvb->fe[0];
1650                 memset(&board_info, 0, sizeof(board_info));
1651                 strlcpy(board_info.type, "ts2022", I2C_NAME_SIZE);
1652                 board_info.addr = 0x60;
1653                 board_info.platform_data = &ts2020_config;
1654                 request_module("ts2020");
1655                 client = i2c_new_device(i2c_adapter, &board_info);
1656                 if (client == NULL || client->dev.driver == NULL) {
1657                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1658                         i2c_unregister_device(dvb->i2c_client_demod);
1659                         result = -ENODEV;
1660                         goto out_free;
1661                 }
1662                 if (!try_module_get(client->dev.driver->owner)) {
1663                         i2c_unregister_device(client);
1664                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1665                         i2c_unregister_device(dvb->i2c_client_demod);
1666                         result = -ENODEV;
1667                         goto out_free;
1668                 }
1669                 dvb->i2c_client_tuner = client;
1670                 /* delegate signal strength measurement to tuner */
1671                 dvb->fe[0]->ops.read_signal_strength =
1672                                 dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1673
1674                 /* attach SEC */
1675                 a8293_pdata.dvb_frontend = dvb->fe[0];
1676                 memset(&board_info, 0, sizeof(board_info));
1677                 strlcpy(board_info.type, "a8293", I2C_NAME_SIZE);
1678                 board_info.addr = 0x08;
1679                 board_info.platform_data = &a8293_pdata;
1680                 request_module("a8293");
1681                 client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1682                 if (client == NULL || client->dev.driver == NULL) {
1683                         module_put(dvb->i2c_client_tuner->dev.driver->owner);
1684                         i2c_unregister_device(dvb->i2c_client_tuner);
1685                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1686                         i2c_unregister_device(dvb->i2c_client_demod);
1687                         result = -ENODEV;
1688                         goto out_free;
1689                 }
1690                 if (!try_module_get(client->dev.driver->owner)) {
1691                         i2c_unregister_device(client);
1692                         module_put(dvb->i2c_client_tuner->dev.driver->owner);
1693                         i2c_unregister_device(dvb->i2c_client_tuner);
1694                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1695                         i2c_unregister_device(dvb->i2c_client_demod);
1696                         result = -ENODEV;
1697                         goto out_free;
1698                 }
1699                 dvb->i2c_client_sec = client;
1700                 break;
1701         }
1702         case EM28178_BOARD_PCTV_292E:
1703                 {
1704                         struct i2c_adapter *adapter;
1705                         struct i2c_client *client;
1706                         struct i2c_board_info info;
1707                         struct si2168_config si2168_config;
1708                         struct si2157_config si2157_config;
1709
1710                         /* attach demod */
1711                         memset(&si2168_config, 0, sizeof(si2168_config));
1712                         si2168_config.i2c_adapter = &adapter;
1713                         si2168_config.fe = &dvb->fe[0];
1714                         si2168_config.ts_mode = SI2168_TS_PARALLEL;
1715                         memset(&info, 0, sizeof(struct i2c_board_info));
1716                         strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1717                         info.addr = 0x64;
1718                         info.platform_data = &si2168_config;
1719                         request_module(info.type);
1720                         client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1721                         if (client == NULL || client->dev.driver == NULL) {
1722                                 result = -ENODEV;
1723                                 goto out_free;
1724                         }
1725
1726                         if (!try_module_get(client->dev.driver->owner)) {
1727                                 i2c_unregister_device(client);
1728                                 result = -ENODEV;
1729                                 goto out_free;
1730                         }
1731
1732                         dvb->i2c_client_demod = client;
1733
1734                         /* attach tuner */
1735                         memset(&si2157_config, 0, sizeof(si2157_config));
1736                         si2157_config.fe = dvb->fe[0];
1737                         si2157_config.if_port = 1;
1738 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1739                         si2157_config.mdev = dev->media_dev;
1740 #endif
1741                         memset(&info, 0, sizeof(struct i2c_board_info));
1742                         strlcpy(info.type, "si2157", I2C_NAME_SIZE);
1743                         info.addr = 0x60;
1744                         info.platform_data = &si2157_config;
1745                         request_module(info.type);
1746                         client = i2c_new_device(adapter, &info);
1747                         if (client == NULL || client->dev.driver == NULL) {
1748                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1749                                 i2c_unregister_device(dvb->i2c_client_demod);
1750                                 result = -ENODEV;
1751                                 goto out_free;
1752                         }
1753
1754                         if (!try_module_get(client->dev.driver->owner)) {
1755                                 i2c_unregister_device(client);
1756                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1757                                 i2c_unregister_device(dvb->i2c_client_demod);
1758                                 result = -ENODEV;
1759                                 goto out_free;
1760                         }
1761
1762                         dvb->i2c_client_tuner = client;
1763                         dvb->fe[0]->ops.set_lna = em28xx_pctv_292e_set_lna;
1764                 }
1765                 break;
1766         case EM28178_BOARD_TERRATEC_T2_STICK_HD:
1767                 {
1768                         struct i2c_adapter *adapter;
1769                         struct i2c_client *client;
1770                         struct i2c_board_info info;
1771                         struct si2168_config si2168_config;
1772                         struct si2157_config si2157_config;
1773
1774                         /* attach demod */
1775                         memset(&si2168_config, 0, sizeof(si2168_config));
1776                         si2168_config.i2c_adapter = &adapter;
1777                         si2168_config.fe = &dvb->fe[0];
1778                         si2168_config.ts_mode = SI2168_TS_PARALLEL;
1779                         memset(&info, 0, sizeof(struct i2c_board_info));
1780                         strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1781                         info.addr = 0x64;
1782                         info.platform_data = &si2168_config;
1783                         request_module(info.type);
1784                         client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1785                         if (client == NULL || client->dev.driver == NULL) {
1786                                 result = -ENODEV;
1787                                 goto out_free;
1788                         }
1789
1790                         if (!try_module_get(client->dev.driver->owner)) {
1791                                 i2c_unregister_device(client);
1792                                 result = -ENODEV;
1793                                 goto out_free;
1794                         }
1795
1796                         dvb->i2c_client_demod = client;
1797
1798                         /* attach tuner */
1799                         memset(&si2157_config, 0, sizeof(si2157_config));
1800                         si2157_config.fe = dvb->fe[0];
1801                         si2157_config.if_port = 0;
1802 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1803                         si2157_config.mdev = dev->media_dev;
1804 #endif
1805                         memset(&info, 0, sizeof(struct i2c_board_info));
1806                         strlcpy(info.type, "si2146", I2C_NAME_SIZE);
1807                         info.addr = 0x60;
1808                         info.platform_data = &si2157_config;
1809                         request_module("si2157");
1810                         client = i2c_new_device(adapter, &info);
1811                         if (client == NULL || client->dev.driver == NULL) {
1812                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1813                                 i2c_unregister_device(dvb->i2c_client_demod);
1814                                 result = -ENODEV;
1815                                 goto out_free;
1816                         }
1817
1818                         if (!try_module_get(client->dev.driver->owner)) {
1819                                 i2c_unregister_device(client);
1820                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1821                                 i2c_unregister_device(dvb->i2c_client_demod);
1822                                 result = -ENODEV;
1823                                 goto out_free;
1824                         }
1825
1826                         dvb->i2c_client_tuner = client;
1827                 }
1828                 break;
1829
1830         case EM28178_BOARD_PLEX_PX_BCUD:
1831                 {
1832                         struct i2c_client *client;
1833                         struct i2c_board_info info;
1834                         struct tc90522_config tc90522_config;
1835                         struct qm1d1c0042_config qm1d1c0042_config;
1836
1837                         /* attach demod */
1838                         memset(&tc90522_config, 0, sizeof(tc90522_config));
1839                         memset(&info, 0, sizeof(struct i2c_board_info));
1840                         strlcpy(info.type, "tc90522sat", I2C_NAME_SIZE);
1841                         info.addr = 0x15;
1842                         info.platform_data = &tc90522_config;
1843                         request_module("tc90522");
1844                         client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1845                         if (client == NULL || client->dev.driver == NULL) {
1846                                 result = -ENODEV;
1847                                 goto out_free;
1848                         }
1849                         dvb->i2c_client_demod = client;
1850                         if (!try_module_get(client->dev.driver->owner)) {
1851                                 i2c_unregister_device(client);
1852                                 result = -ENODEV;
1853                                 goto out_free;
1854                         }
1855
1856                         /* attach tuner */
1857                         memset(&qm1d1c0042_config, 0,
1858                                sizeof(qm1d1c0042_config));
1859                         qm1d1c0042_config.fe = tc90522_config.fe;
1860                         qm1d1c0042_config.lpf = 1;
1861                         memset(&info, 0, sizeof(struct i2c_board_info));
1862                         strlcpy(info.type, "qm1d1c0042", I2C_NAME_SIZE);
1863                         info.addr = 0x61;
1864                         info.platform_data = &qm1d1c0042_config;
1865                         request_module(info.type);
1866                         client = i2c_new_device(tc90522_config.tuner_i2c,
1867                                                 &info);
1868                         if (client == NULL || client->dev.driver == NULL) {
1869                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1870                                 i2c_unregister_device(dvb->i2c_client_demod);
1871                                 result = -ENODEV;
1872                                 goto out_free;
1873                         }
1874                         dvb->i2c_client_tuner = client;
1875                         if (!try_module_get(client->dev.driver->owner)) {
1876                                 i2c_unregister_device(client);
1877                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1878                                 i2c_unregister_device(dvb->i2c_client_demod);
1879                                 result = -ENODEV;
1880                                 goto out_free;
1881                         }
1882                         dvb->fe[0] = tc90522_config.fe;
1883                         px_bcud_init(dev);
1884                 }
1885                 break;
1886         case EM28174_BOARD_HAUPPAUGE_WINTV_DUALHD_DVB:
1887                 {
1888                         struct i2c_adapter *adapter;
1889                         struct i2c_client *client;
1890                         struct i2c_board_info info;
1891                         struct si2168_config si2168_config;
1892                         struct si2157_config si2157_config;
1893
1894                         /* attach demod */
1895                         memset(&si2168_config, 0, sizeof(si2168_config));
1896                         si2168_config.i2c_adapter = &adapter;
1897                         si2168_config.fe = &dvb->fe[0];
1898                         si2168_config.ts_mode = SI2168_TS_SERIAL;
1899                         memset(&info, 0, sizeof(struct i2c_board_info));
1900                         strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1901                         info.addr = 0x64;
1902                         info.platform_data = &si2168_config;
1903                         request_module(info.type);
1904                         client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1905                         if (client == NULL || client->dev.driver == NULL) {
1906                                 result = -ENODEV;
1907                                 goto out_free;
1908                         }
1909
1910                         if (!try_module_get(client->dev.driver->owner)) {
1911                                 i2c_unregister_device(client);
1912                                 result = -ENODEV;
1913                                 goto out_free;
1914                         }
1915
1916                         dvb->i2c_client_demod = client;
1917
1918                         /* attach tuner */
1919                         memset(&si2157_config, 0, sizeof(si2157_config));
1920                         si2157_config.fe = dvb->fe[0];
1921                         si2157_config.if_port = 1;
1922 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1923                         si2157_config.mdev = dev->media_dev;
1924 #endif
1925                         memset(&info, 0, sizeof(struct i2c_board_info));
1926                         strlcpy(info.type, "si2157", I2C_NAME_SIZE);
1927                         info.addr = 0x60;
1928                         info.platform_data = &si2157_config;
1929                         request_module(info.type);
1930                         client = i2c_new_device(adapter, &info);
1931                         if (client == NULL || client->dev.driver == NULL) {
1932                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1933                                 i2c_unregister_device(dvb->i2c_client_demod);
1934                                 result = -ENODEV;
1935                                 goto out_free;
1936                         }
1937
1938                         if (!try_module_get(client->dev.driver->owner)) {
1939                                 i2c_unregister_device(client);
1940                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1941                                 i2c_unregister_device(dvb->i2c_client_demod);
1942                                 result = -ENODEV;
1943                                 goto out_free;
1944                         }
1945
1946                         dvb->i2c_client_tuner = client;
1947
1948                 }
1949                 break;
1950         default:
1951                 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
1952                                 " isn't supported yet\n");
1953                 break;
1954         }
1955         if (NULL == dvb->fe[0]) {
1956                 em28xx_errdev("/2: frontend initialization failed\n");
1957                 result = -EINVAL;
1958                 goto out_free;
1959         }
1960         /* define general-purpose callback pointer */
1961         dvb->fe[0]->callback = em28xx_tuner_callback;
1962         if (dvb->fe[1])
1963                 dvb->fe[1]->callback = em28xx_tuner_callback;
1964
1965         /* register everything */
1966         result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
1967
1968         if (result < 0)
1969                 goto out_free;
1970
1971         em28xx_info("DVB extension successfully initialized\n");
1972
1973         kref_get(&dev->ref);
1974
1975 ret:
1976         em28xx_set_mode(dev, EM28XX_SUSPEND);
1977         mutex_unlock(&dev->lock);
1978         return result;
1979
1980 out_free:
1981         kfree(dvb);
1982         dev->dvb = NULL;
1983         goto ret;
1984 }
1985
1986 static inline void prevent_sleep(struct dvb_frontend_ops *ops)
1987 {
1988         ops->set_voltage = NULL;
1989         ops->sleep = NULL;
1990         ops->tuner_ops.sleep = NULL;
1991 }
1992
1993 static int em28xx_dvb_fini(struct em28xx *dev)
1994 {
1995         struct em28xx_dvb *dvb;
1996         struct i2c_client *client;
1997
1998         if (dev->is_audio_only) {
1999                 /* Shouldn't initialize IR for this interface */
2000                 return 0;
2001         }
2002
2003         if (!dev->board.has_dvb) {
2004                 /* This device does not support the extension */
2005                 return 0;
2006         }
2007
2008         if (!dev->dvb)
2009                 return 0;
2010
2011         em28xx_info("Closing DVB extension\n");
2012
2013         dvb = dev->dvb;
2014
2015         em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
2016
2017         if (dev->disconnected) {
2018                 /* We cannot tell the device to sleep
2019                  * once it has been unplugged. */
2020                 if (dvb->fe[0]) {
2021                         prevent_sleep(&dvb->fe[0]->ops);
2022                         dvb->fe[0]->exit = DVB_FE_DEVICE_REMOVED;
2023                 }
2024                 if (dvb->fe[1]) {
2025                         prevent_sleep(&dvb->fe[1]->ops);
2026                         dvb->fe[1]->exit = DVB_FE_DEVICE_REMOVED;
2027                 }
2028         }
2029
2030         /* remove I2C SEC */
2031         client = dvb->i2c_client_sec;
2032         if (client) {
2033                 module_put(client->dev.driver->owner);
2034                 i2c_unregister_device(client);
2035         }
2036
2037         /* remove I2C tuner */
2038         client = dvb->i2c_client_tuner;
2039         if (client) {
2040                 module_put(client->dev.driver->owner);
2041                 i2c_unregister_device(client);
2042         }
2043
2044         /* remove I2C demod */
2045         client = dvb->i2c_client_demod;
2046         if (client) {
2047                 module_put(client->dev.driver->owner);
2048                 i2c_unregister_device(client);
2049         }
2050
2051         em28xx_unregister_dvb(dvb);
2052         kfree(dvb);
2053         dev->dvb = NULL;
2054         kref_put(&dev->ref, em28xx_free_device);
2055
2056         return 0;
2057 }
2058
2059 static int em28xx_dvb_suspend(struct em28xx *dev)
2060 {
2061         int ret = 0;
2062
2063         if (dev->is_audio_only)
2064                 return 0;
2065
2066         if (!dev->board.has_dvb)
2067                 return 0;
2068
2069         em28xx_info("Suspending DVB extension\n");
2070         if (dev->dvb) {
2071                 struct em28xx_dvb *dvb = dev->dvb;
2072
2073                 if (dvb->fe[0]) {
2074                         ret = dvb_frontend_suspend(dvb->fe[0]);
2075                         em28xx_info("fe0 suspend %d\n", ret);
2076                 }
2077                 if (dvb->fe[1]) {
2078                         dvb_frontend_suspend(dvb->fe[1]);
2079                         em28xx_info("fe1 suspend %d\n", ret);
2080                 }
2081         }
2082
2083         return 0;
2084 }
2085
2086 static int em28xx_dvb_resume(struct em28xx *dev)
2087 {
2088         int ret = 0;
2089
2090         if (dev->is_audio_only)
2091                 return 0;
2092
2093         if (!dev->board.has_dvb)
2094                 return 0;
2095
2096         em28xx_info("Resuming DVB extension\n");
2097         if (dev->dvb) {
2098                 struct em28xx_dvb *dvb = dev->dvb;
2099
2100                 if (dvb->fe[0]) {
2101                         ret = dvb_frontend_resume(dvb->fe[0]);
2102                         em28xx_info("fe0 resume %d\n", ret);
2103                 }
2104
2105                 if (dvb->fe[1]) {
2106                         ret = dvb_frontend_resume(dvb->fe[1]);
2107                         em28xx_info("fe1 resume %d\n", ret);
2108                 }
2109         }
2110
2111         return 0;
2112 }
2113
2114 static struct em28xx_ops dvb_ops = {
2115         .id   = EM28XX_DVB,
2116         .name = "Em28xx dvb Extension",
2117         .init = em28xx_dvb_init,
2118         .fini = em28xx_dvb_fini,
2119         .suspend = em28xx_dvb_suspend,
2120         .resume = em28xx_dvb_resume,
2121 };
2122
2123 static int __init em28xx_dvb_register(void)
2124 {
2125         return em28xx_register_extension(&dvb_ops);
2126 }
2127
2128 static void __exit em28xx_dvb_unregister(void)
2129 {
2130         em28xx_unregister_extension(&dvb_ops);
2131 }
2132
2133 module_init(em28xx_dvb_register);
2134 module_exit(em28xx_dvb_unregister);