GNU Linux-libre 4.9.309-gnu1
[releases.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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 struct tda18271_std_map drx_j_std_map = {
908         .atsc_6   = { .if_freq = 5000, .agc_mode = 3, .std = 0, .if_lvl = 1,
909                       .rfagc_top = 0x37, },
910         .qam_6    = { .if_freq = 5380, .agc_mode = 3, .std = 3, .if_lvl = 1,
911                       .rfagc_top = 0x37, },
912 };
913
914 static struct tda18271_config pinnacle_80e_dvb_config = {
915         .std_map = &drx_j_std_map,
916         .gate    = TDA18271_GATE_DIGITAL,
917         .role    = TDA18271_MASTER,
918 };
919
920 /* ------------------------------------------------------------------ */
921
922 static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
923 {
924         struct dvb_frontend *fe;
925         struct xc2028_config cfg;
926         struct xc2028_ctrl ctl;
927
928         memset(&cfg, 0, sizeof(cfg));
929         cfg.i2c_adap  = &dev->i2c_adap[dev->def_i2c_bus];
930         cfg.i2c_addr  = addr;
931
932         memset(&ctl, 0, sizeof(ctl));
933         em28xx_setup_xc3028(dev, &ctl);
934         cfg.ctrl  = &ctl;
935
936         if (!dev->dvb->fe[0]) {
937                 em28xx_errdev("/2: dvb frontend not attached. "
938                                 "Can't attach xc3028\n");
939                 return -EINVAL;
940         }
941
942         fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
943         if (!fe) {
944                 em28xx_errdev("/2: xc3028 attach failed\n");
945                 dvb_frontend_detach(dev->dvb->fe[0]);
946                 dev->dvb->fe[0] = NULL;
947                 return -EINVAL;
948         }
949
950         em28xx_info("%s/2: xc3028 attached\n", dev->name);
951
952         return 0;
953 }
954
955 /* ------------------------------------------------------------------ */
956
957 static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
958                                struct em28xx *dev, struct device *device)
959 {
960         int result;
961         bool create_rf_connector = false;
962
963         mutex_init(&dvb->lock);
964
965         /* register adapter */
966         result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
967                                       adapter_nr);
968         if (result < 0) {
969                 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
970                        dev->name, result);
971                 goto fail_adapter;
972         }
973 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
974         dvb->adapter.mdev = dev->media_dev;
975 #endif
976
977         /* Ensure all frontends negotiate bus access */
978         dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
979         if (dvb->fe[1])
980                 dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
981
982         dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
983
984         /* register frontend */
985         result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
986         if (result < 0) {
987                 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
988                        dev->name, result);
989                 goto fail_frontend0;
990         }
991
992         /* register 2nd frontend */
993         if (dvb->fe[1]) {
994                 result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
995                 if (result < 0) {
996                         printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
997                                dev->name, result);
998                         goto fail_frontend1;
999                 }
1000         }
1001
1002         /* register demux stuff */
1003         dvb->demux.dmx.capabilities =
1004                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1005                 DMX_MEMORY_BASED_FILTERING;
1006         dvb->demux.priv       = dvb;
1007         dvb->demux.filternum  = 256;
1008         dvb->demux.feednum    = 256;
1009         dvb->demux.start_feed = em28xx_start_feed;
1010         dvb->demux.stop_feed  = em28xx_stop_feed;
1011
1012         result = dvb_dmx_init(&dvb->demux);
1013         if (result < 0) {
1014                 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
1015                        dev->name, result);
1016                 goto fail_dmx;
1017         }
1018
1019         dvb->dmxdev.filternum    = 256;
1020         dvb->dmxdev.demux        = &dvb->demux.dmx;
1021         dvb->dmxdev.capabilities = 0;
1022         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
1023         if (result < 0) {
1024                 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
1025                        dev->name, result);
1026                 goto fail_dmxdev;
1027         }
1028
1029         dvb->fe_hw.source = DMX_FRONTEND_0;
1030         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1031         if (result < 0) {
1032                 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
1033                        dev->name, result);
1034                 goto fail_fe_hw;
1035         }
1036
1037         dvb->fe_mem.source = DMX_MEMORY_FE;
1038         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1039         if (result < 0) {
1040                 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
1041                        dev->name, result);
1042                 goto fail_fe_mem;
1043         }
1044
1045         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1046         if (result < 0) {
1047                 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
1048                        dev->name, result);
1049                 goto fail_fe_conn;
1050         }
1051
1052         /* register network adapter */
1053         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
1054
1055         /* If the analog part won't create RF connectors, DVB will do it */
1056         if (!dev->has_video || (dev->tuner_type == TUNER_ABSENT))
1057                 create_rf_connector = true;
1058
1059         result = dvb_create_media_graph(&dvb->adapter, create_rf_connector);
1060         if (result < 0)
1061                 goto fail_create_graph;
1062
1063         return 0;
1064
1065 fail_create_graph:
1066         dvb_net_release(&dvb->net);
1067 fail_fe_conn:
1068         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1069 fail_fe_mem:
1070         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1071 fail_fe_hw:
1072         dvb_dmxdev_release(&dvb->dmxdev);
1073 fail_dmxdev:
1074         dvb_dmx_release(&dvb->demux);
1075 fail_dmx:
1076         if (dvb->fe[1])
1077                 dvb_unregister_frontend(dvb->fe[1]);
1078         dvb_unregister_frontend(dvb->fe[0]);
1079 fail_frontend1:
1080         if (dvb->fe[1])
1081                 dvb_frontend_detach(dvb->fe[1]);
1082 fail_frontend0:
1083         dvb_frontend_detach(dvb->fe[0]);
1084         dvb_unregister_adapter(&dvb->adapter);
1085 fail_adapter:
1086         return result;
1087 }
1088
1089 static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
1090 {
1091         dvb_net_release(&dvb->net);
1092         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1093         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1094         dvb_dmxdev_release(&dvb->dmxdev);
1095         dvb_dmx_release(&dvb->demux);
1096         if (dvb->fe[1])
1097                 dvb_unregister_frontend(dvb->fe[1]);
1098         dvb_unregister_frontend(dvb->fe[0]);
1099         if (dvb->fe[1] && !dvb->dont_attach_fe1)
1100                 dvb_frontend_detach(dvb->fe[1]);
1101         dvb_frontend_detach(dvb->fe[0]);
1102         dvb_unregister_adapter(&dvb->adapter);
1103 }
1104
1105 static int em28xx_dvb_init(struct em28xx *dev)
1106 {
1107         int result = 0;
1108         struct em28xx_dvb *dvb;
1109
1110         if (dev->is_audio_only) {
1111                 /* Shouldn't initialize IR for this interface */
1112                 return 0;
1113         }
1114
1115         if (!dev->board.has_dvb) {
1116                 /* This device does not support the extension */
1117                 return 0;
1118         }
1119
1120         em28xx_info("Binding DVB extension\n");
1121
1122         dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
1123         if (dvb == NULL) {
1124                 em28xx_info("em28xx_dvb: memory allocation failed\n");
1125                 return -ENOMEM;
1126         }
1127         dev->dvb = dvb;
1128         dvb->fe[0] = dvb->fe[1] = NULL;
1129
1130         /* pre-allocate DVB usb transfer buffers */
1131         if (dev->dvb_xfer_bulk) {
1132                 result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1133                                            dev->dvb_xfer_bulk,
1134                                            EM28XX_DVB_NUM_BUFS,
1135                                            512,
1136                                            EM28XX_DVB_BULK_PACKET_MULTIPLIER);
1137         } else {
1138                 result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1139                                            dev->dvb_xfer_bulk,
1140                                            EM28XX_DVB_NUM_BUFS,
1141                                            dev->dvb_max_pkt_size_isoc,
1142                                            EM28XX_DVB_NUM_ISOC_PACKETS);
1143         }
1144         if (result) {
1145                 em28xx_errdev("em28xx_dvb: failed to pre-allocate USB transfer buffers for DVB.\n");
1146                 kfree(dvb);
1147                 dev->dvb = NULL;
1148                 return result;
1149         }
1150
1151         mutex_lock(&dev->lock);
1152         em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
1153         /* init frontend */
1154         switch (dev->model) {
1155         case EM2874_BOARD_LEADERSHIP_ISDBT:
1156                 dvb->fe[0] = dvb_attach(s921_attach,
1157                                 &sharp_isdbt, &dev->i2c_adap[dev->def_i2c_bus]);
1158
1159                 if (!dvb->fe[0]) {
1160                         result = -EINVAL;
1161                         goto out_free;
1162                 }
1163
1164                 break;
1165         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
1166         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
1167         case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
1168         case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
1169                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
1170                                            &em2880_lgdt3303_dev,
1171                                            &dev->i2c_adap[dev->def_i2c_bus]);
1172                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1173                         result = -EINVAL;
1174                         goto out_free;
1175                 }
1176                 break;
1177         case EM2880_BOARD_KWORLD_DVB_310U:
1178                 dvb->fe[0] = dvb_attach(zl10353_attach,
1179                                            &em28xx_zl10353_with_xc3028,
1180                                            &dev->i2c_adap[dev->def_i2c_bus]);
1181                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1182                         result = -EINVAL;
1183                         goto out_free;
1184                 }
1185                 break;
1186         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
1187         case EM2882_BOARD_TERRATEC_HYBRID_XS:
1188         case EM2880_BOARD_EMPIRE_DUAL_TV:
1189                 dvb->fe[0] = dvb_attach(zl10353_attach,
1190                                            &em28xx_zl10353_xc3028_no_i2c_gate,
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_TERRATEC_HYBRID_XS:
1198         case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
1199         case EM2881_BOARD_PINNACLE_HYBRID_PRO:
1200         case EM2882_BOARD_DIKOM_DK300:
1201         case EM2882_BOARD_KWORLD_VS_DVBT:
1202                 dvb->fe[0] = dvb_attach(zl10353_attach,
1203                                            &em28xx_zl10353_xc3028_no_i2c_gate,
1204                                            &dev->i2c_adap[dev->def_i2c_bus]);
1205                 if (dvb->fe[0] == NULL) {
1206                         /* This board could have either a zl10353 or a mt352.
1207                            If the chip id isn't for zl10353, try mt352 */
1208                         dvb->fe[0] = dvb_attach(mt352_attach,
1209                                                    &terratec_xs_mt352_cfg,
1210                                                    &dev->i2c_adap[dev->def_i2c_bus]);
1211                 }
1212
1213                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1214                         result = -EINVAL;
1215                         goto out_free;
1216                 }
1217                 break;
1218         case EM2870_BOARD_TERRATEC_XS_MT2060:
1219                 dvb->fe[0] = dvb_attach(zl10353_attach,
1220                                                 &em28xx_zl10353_no_i2c_gate_dev,
1221                                                 &dev->i2c_adap[dev->def_i2c_bus]);
1222                 if (dvb->fe[0] != NULL) {
1223                         dvb_attach(mt2060_attach, dvb->fe[0],
1224                                         &dev->i2c_adap[dev->def_i2c_bus],
1225                                         &em28xx_mt2060_config, 1220);
1226                 }
1227                 break;
1228         case EM2870_BOARD_KWORLD_355U:
1229                 dvb->fe[0] = dvb_attach(zl10353_attach,
1230                                            &em28xx_zl10353_no_i2c_gate_dev,
1231                                            &dev->i2c_adap[dev->def_i2c_bus]);
1232                 if (dvb->fe[0] != NULL)
1233                         dvb_attach(qt1010_attach, dvb->fe[0],
1234                                    &dev->i2c_adap[dev->def_i2c_bus], &em28xx_qt1010_config);
1235                 break;
1236         case EM2883_BOARD_KWORLD_HYBRID_330U:
1237         case EM2882_BOARD_EVGA_INDTUBE:
1238                 dvb->fe[0] = dvb_attach(s5h1409_attach,
1239                                            &em28xx_s5h1409_with_xc3028,
1240                                            &dev->i2c_adap[dev->def_i2c_bus]);
1241                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1242                         result = -EINVAL;
1243                         goto out_free;
1244                 }
1245                 break;
1246         case EM2882_BOARD_KWORLD_ATSC_315U:
1247                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
1248                                            &em2880_lgdt3303_dev,
1249                                            &dev->i2c_adap[dev->def_i2c_bus]);
1250                 if (dvb->fe[0] != NULL) {
1251                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1252                                         &dev->i2c_adap[dev->def_i2c_bus],
1253                                         0x61, TUNER_THOMSON_DTT761X)) {
1254                                 result = -EINVAL;
1255                                 goto out_free;
1256                         }
1257                 }
1258                 break;
1259         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
1260         case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
1261                 dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
1262                                            &dev->i2c_adap[dev->def_i2c_bus], &dev->udev->dev);
1263                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1264                         result = -EINVAL;
1265                         goto out_free;
1266                 }
1267                 break;
1268         case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
1269                 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
1270                 dvb->fe[0] = dvb_attach(tda10023_attach,
1271                         &em28xx_tda10023_config,
1272                         &dev->i2c_adap[dev->def_i2c_bus], 0x48);
1273                 if (dvb->fe[0]) {
1274                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1275                                         &dev->i2c_adap[dev->def_i2c_bus],
1276                                         0x60, TUNER_PHILIPS_CU1216L)) {
1277                                 result = -EINVAL;
1278                                 goto out_free;
1279                         }
1280                 }
1281                 break;
1282         case EM2870_BOARD_KWORLD_A340:
1283                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1284                                            &em2870_lgdt3304_dev,
1285                                            &dev->i2c_adap[dev->def_i2c_bus]);
1286                 if (!dvb->fe[0]) {
1287                         result = -EINVAL;
1288                         goto out_free;
1289                 }
1290                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1291                                 &dev->i2c_adap[dev->def_i2c_bus],
1292                         &kworld_a340_config)) {
1293                                 dvb_frontend_detach(dvb->fe[0]);
1294                                 result = -EINVAL;
1295                                 goto out_free;
1296                 }
1297                 break;
1298         case EM28174_BOARD_PCTV_290E:
1299                 /* set default GPIO0 for LNA, used if GPIOLIB is undefined */
1300                 dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
1301                                 CXD2820R_GPIO_L;
1302                 dvb->fe[0] = dvb_attach(cxd2820r_attach,
1303                                         &em28xx_cxd2820r_config,
1304                                         &dev->i2c_adap[dev->def_i2c_bus],
1305                                         &dvb->lna_gpio);
1306                 if (dvb->fe[0]) {
1307                         /* FE 0 attach tuner */
1308                         if (!dvb_attach(tda18271_attach,
1309                                         dvb->fe[0],
1310                                         0x60,
1311                                         &dev->i2c_adap[dev->def_i2c_bus],
1312                                         &em28xx_cxd2820r_tda18271_config)) {
1313
1314                                 dvb_frontend_detach(dvb->fe[0]);
1315                                 result = -EINVAL;
1316                                 goto out_free;
1317                         }
1318
1319 #ifdef CONFIG_GPIOLIB
1320                         /* enable LNA for DVB-T, DVB-T2 and DVB-C */
1321                         result = gpio_request_one(dvb->lna_gpio,
1322                                                   GPIOF_OUT_INIT_LOW, NULL);
1323                         if (result)
1324                                 em28xx_errdev("gpio request failed %d\n",
1325                                               result);
1326                         else
1327                                 gpio_free(dvb->lna_gpio);
1328
1329                         result = 0; /* continue even set LNA fails */
1330 #endif
1331                         dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
1332                 }
1333
1334                 break;
1335         case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
1336         {
1337                 struct xc5000_config cfg;
1338
1339                 hauppauge_hvr930c_init(dev);
1340
1341                 dvb->fe[0] = dvb_attach(drxk_attach,
1342                                         &hauppauge_930c_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1343                 if (!dvb->fe[0]) {
1344                         result = -EINVAL;
1345                         goto out_free;
1346                 }
1347                 /* FIXME: do we need a pll semaphore? */
1348                 dvb->fe[0]->sec_priv = dvb;
1349                 sema_init(&dvb->pll_mutex, 1);
1350                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1351                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1352
1353                 /* Attach xc5000 */
1354                 memset(&cfg, 0, sizeof(cfg));
1355                 cfg.i2c_address  = 0x61;
1356                 cfg.if_khz = 4000;
1357
1358                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1359                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1360                 if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
1361                                 &cfg)) {
1362                         result = -EINVAL;
1363                         goto out_free;
1364                 }
1365                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1366                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1367
1368                 break;
1369         }
1370         case EM2884_BOARD_TERRATEC_H5:
1371                 terratec_h5_init(dev);
1372
1373                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1374                 if (!dvb->fe[0]) {
1375                         result = -EINVAL;
1376                         goto out_free;
1377                 }
1378                 /* FIXME: do we need a pll semaphore? */
1379                 dvb->fe[0]->sec_priv = dvb;
1380                 sema_init(&dvb->pll_mutex, 1);
1381                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1382                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1383
1384                 /* Attach tda18271 to DVB-C frontend */
1385                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1386                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1387                 if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
1388                         result = -EINVAL;
1389                         goto out_free;
1390                 }
1391                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1392                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1393
1394                 break;
1395         case EM2884_BOARD_C3TECH_DIGITAL_DUO:
1396                 dvb->fe[0] = dvb_attach(mb86a20s_attach,
1397                                            &c3tech_duo_mb86a20s_config,
1398                                            &dev->i2c_adap[dev->def_i2c_bus]);
1399                 if (dvb->fe[0] != NULL)
1400                         dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1401                                    &dev->i2c_adap[dev->def_i2c_bus],
1402                                    &c3tech_duo_tda18271_config);
1403                 break;
1404         case EM28174_BOARD_PCTV_460E: {
1405                 struct i2c_client *client;
1406                 struct i2c_board_info board_info;
1407                 struct tda10071_platform_data tda10071_pdata = {};
1408                 struct a8293_platform_data a8293_pdata = {};
1409
1410                 /* attach demod + tuner combo */
1411                 tda10071_pdata.clk = 40444000, /* 40.444 MHz */
1412                 tda10071_pdata.i2c_wr_max = 64,
1413                 tda10071_pdata.ts_mode = TDA10071_TS_SERIAL,
1414                 tda10071_pdata.pll_multiplier = 20,
1415                 tda10071_pdata.tuner_i2c_addr = 0x14,
1416                 memset(&board_info, 0, sizeof(board_info));
1417                 strlcpy(board_info.type, "tda10071_cx24118", I2C_NAME_SIZE);
1418                 board_info.addr = 0x55;
1419                 board_info.platform_data = &tda10071_pdata;
1420                 request_module("tda10071");
1421                 client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1422                 if (client == NULL || client->dev.driver == NULL) {
1423                         result = -ENODEV;
1424                         goto out_free;
1425                 }
1426                 if (!try_module_get(client->dev.driver->owner)) {
1427                         i2c_unregister_device(client);
1428                         result = -ENODEV;
1429                         goto out_free;
1430                 }
1431                 dvb->fe[0] = tda10071_pdata.get_dvb_frontend(client);
1432                 dvb->i2c_client_demod = client;
1433
1434                 /* attach SEC */
1435                 a8293_pdata.dvb_frontend = dvb->fe[0];
1436                 memset(&board_info, 0, sizeof(board_info));
1437                 strlcpy(board_info.type, "a8293", I2C_NAME_SIZE);
1438                 board_info.addr = 0x08;
1439                 board_info.platform_data = &a8293_pdata;
1440                 request_module("a8293");
1441                 client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1442                 if (client == NULL || client->dev.driver == NULL) {
1443                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1444                         i2c_unregister_device(dvb->i2c_client_demod);
1445                         result = -ENODEV;
1446                         goto out_free;
1447                 }
1448                 if (!try_module_get(client->dev.driver->owner)) {
1449                         i2c_unregister_device(client);
1450                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1451                         i2c_unregister_device(dvb->i2c_client_demod);
1452                         result = -ENODEV;
1453                         goto out_free;
1454                 }
1455                 dvb->i2c_client_sec = client;
1456                 break;
1457         }
1458         case EM2874_BOARD_DELOCK_61959:
1459         case EM2874_BOARD_MAXMEDIA_UB425_TC:
1460                 /* attach demodulator */
1461                 dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1462                                 &dev->i2c_adap[dev->def_i2c_bus]);
1463
1464                 if (dvb->fe[0]) {
1465                         /* disable I2C-gate */
1466                         dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
1467
1468                         /* attach tuner */
1469                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1470                                         &dev->i2c_adap[dev->def_i2c_bus],
1471                                         &em28xx_cxd2820r_tda18271_config)) {
1472                                 dvb_frontend_detach(dvb->fe[0]);
1473                                 result = -EINVAL;
1474                                 goto out_free;
1475                         }
1476                 }
1477                 break;
1478         case EM2884_BOARD_PCTV_510E:
1479         case EM2884_BOARD_PCTV_520E:
1480                 pctv_520e_init(dev);
1481
1482                 /* attach demodulator */
1483                 dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1484                                 &dev->i2c_adap[dev->def_i2c_bus]);
1485
1486                 if (dvb->fe[0]) {
1487                         /* attach tuner */
1488                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1489                                         &dev->i2c_adap[dev->def_i2c_bus],
1490                                         &em28xx_cxd2820r_tda18271_config)) {
1491                                 dvb_frontend_detach(dvb->fe[0]);
1492                                 result = -EINVAL;
1493                                 goto out_free;
1494                         }
1495                 }
1496                 break;
1497         case EM2884_BOARD_ELGATO_EYETV_HYBRID_2008:
1498         case EM2884_BOARD_CINERGY_HTC_STICK:
1499                 terratec_htc_stick_init(dev);
1500
1501                 /* attach demodulator */
1502                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1503                                         &dev->i2c_adap[dev->def_i2c_bus]);
1504                 if (!dvb->fe[0]) {
1505                         result = -EINVAL;
1506                         goto out_free;
1507                 }
1508
1509                 /* Attach the demodulator. */
1510                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1511                                 &dev->i2c_adap[dev->def_i2c_bus],
1512                                 &em28xx_cxd2820r_tda18271_config)) {
1513                         result = -EINVAL;
1514                         goto out_free;
1515                 }
1516                 break;
1517         case EM2884_BOARD_TERRATEC_HTC_USB_XS:
1518                 terratec_htc_usb_xs_init(dev);
1519
1520                 /* attach demodulator */
1521                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1522                                         &dev->i2c_adap[dev->def_i2c_bus]);
1523                 if (!dvb->fe[0]) {
1524                         result = -EINVAL;
1525                         goto out_free;
1526                 }
1527
1528                 /* Attach the demodulator. */
1529                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1530                                 &dev->i2c_adap[dev->def_i2c_bus],
1531                                 &em28xx_cxd2820r_tda18271_config)) {
1532                         result = -EINVAL;
1533                         goto out_free;
1534                 }
1535                 break;
1536         case EM2874_BOARD_KWORLD_UB435Q_V2:
1537                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1538                                         &em2874_lgdt3305_dev,
1539                                         &dev->i2c_adap[dev->def_i2c_bus]);
1540                 if (!dvb->fe[0]) {
1541                         result = -EINVAL;
1542                         goto out_free;
1543                 }
1544
1545                 /* Attach the demodulator. */
1546                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1547                                 &dev->i2c_adap[dev->def_i2c_bus],
1548                                 &kworld_ub435q_v2_config)) {
1549                         result = -EINVAL;
1550                         goto out_free;
1551                 }
1552                 break;
1553         case EM2874_BOARD_KWORLD_UB435Q_V3:
1554         {
1555                 struct i2c_client *client;
1556                 struct i2c_adapter *adapter = &dev->i2c_adap[dev->def_i2c_bus];
1557                 struct i2c_board_info board_info = {
1558                         .type = "tda18212",
1559                         .addr = 0x60,
1560                         .platform_data = &kworld_ub435q_v3_config,
1561                 };
1562
1563                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1564                                         &em2874_lgdt3305_nogate_dev,
1565                                         &dev->i2c_adap[dev->def_i2c_bus]);
1566                 if (!dvb->fe[0]) {
1567                         result = -EINVAL;
1568                         goto out_free;
1569                 }
1570
1571                 /* attach tuner */
1572                 kworld_ub435q_v3_config.fe = dvb->fe[0];
1573                 request_module("tda18212");
1574                 client = i2c_new_device(adapter, &board_info);
1575                 if (client == NULL || client->dev.driver == NULL) {
1576                         dvb_frontend_detach(dvb->fe[0]);
1577                         result = -ENODEV;
1578                         goto out_free;
1579                 }
1580
1581                 if (!try_module_get(client->dev.driver->owner)) {
1582                         i2c_unregister_device(client);
1583                         dvb_frontend_detach(dvb->fe[0]);
1584                         result = -ENODEV;
1585                         goto out_free;
1586                 }
1587
1588                 dvb->i2c_client_tuner = client;
1589                 break;
1590         }
1591         case EM2874_BOARD_PCTV_HD_MINI_80E:
1592                 dvb->fe[0] = dvb_attach(drx39xxj_attach, &dev->i2c_adap[dev->def_i2c_bus]);
1593                 if (dvb->fe[0] != NULL) {
1594                         dvb->fe[0] = dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1595                                                 &dev->i2c_adap[dev->def_i2c_bus],
1596                                                 &pinnacle_80e_dvb_config);
1597                         if (!dvb->fe[0]) {
1598                                 result = -EINVAL;
1599                                 goto out_free;
1600                         }
1601                 }
1602                 break;
1603         case EM28178_BOARD_PCTV_461E: {
1604                 struct i2c_client *client;
1605                 struct i2c_adapter *i2c_adapter;
1606                 struct i2c_board_info board_info;
1607                 struct m88ds3103_platform_data m88ds3103_pdata = {};
1608                 struct ts2020_config ts2020_config = {};
1609                 struct a8293_platform_data a8293_pdata = {};
1610
1611                 /* attach demod */
1612                 m88ds3103_pdata.clk = 27000000;
1613                 m88ds3103_pdata.i2c_wr_max = 33;
1614                 m88ds3103_pdata.ts_mode = M88DS3103_TS_PARALLEL;
1615                 m88ds3103_pdata.ts_clk = 16000;
1616                 m88ds3103_pdata.ts_clk_pol = 1;
1617                 m88ds3103_pdata.agc = 0x99;
1618                 memset(&board_info, 0, sizeof(board_info));
1619                 strlcpy(board_info.type, "m88ds3103", I2C_NAME_SIZE);
1620                 board_info.addr = 0x68;
1621                 board_info.platform_data = &m88ds3103_pdata;
1622                 request_module("m88ds3103");
1623                 client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1624                 if (client == NULL || client->dev.driver == NULL) {
1625                         result = -ENODEV;
1626                         goto out_free;
1627                 }
1628                 if (!try_module_get(client->dev.driver->owner)) {
1629                         i2c_unregister_device(client);
1630                         result = -ENODEV;
1631                         goto out_free;
1632                 }
1633                 dvb->fe[0] = m88ds3103_pdata.get_dvb_frontend(client);
1634                 i2c_adapter = m88ds3103_pdata.get_i2c_adapter(client);
1635                 dvb->i2c_client_demod = client;
1636
1637                 /* attach tuner */
1638                 ts2020_config.fe = dvb->fe[0];
1639                 memset(&board_info, 0, sizeof(board_info));
1640                 strlcpy(board_info.type, "ts2022", I2C_NAME_SIZE);
1641                 board_info.addr = 0x60;
1642                 board_info.platform_data = &ts2020_config;
1643                 request_module("ts2020");
1644                 client = i2c_new_device(i2c_adapter, &board_info);
1645                 if (client == NULL || client->dev.driver == NULL) {
1646                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1647                         i2c_unregister_device(dvb->i2c_client_demod);
1648                         result = -ENODEV;
1649                         goto out_free;
1650                 }
1651                 if (!try_module_get(client->dev.driver->owner)) {
1652                         i2c_unregister_device(client);
1653                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1654                         i2c_unregister_device(dvb->i2c_client_demod);
1655                         result = -ENODEV;
1656                         goto out_free;
1657                 }
1658                 dvb->i2c_client_tuner = client;
1659                 /* delegate signal strength measurement to tuner */
1660                 dvb->fe[0]->ops.read_signal_strength =
1661                                 dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1662
1663                 /* attach SEC */
1664                 a8293_pdata.dvb_frontend = dvb->fe[0];
1665                 memset(&board_info, 0, sizeof(board_info));
1666                 strlcpy(board_info.type, "a8293", I2C_NAME_SIZE);
1667                 board_info.addr = 0x08;
1668                 board_info.platform_data = &a8293_pdata;
1669                 request_module("a8293");
1670                 client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1671                 if (client == NULL || client->dev.driver == NULL) {
1672                         module_put(dvb->i2c_client_tuner->dev.driver->owner);
1673                         i2c_unregister_device(dvb->i2c_client_tuner);
1674                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1675                         i2c_unregister_device(dvb->i2c_client_demod);
1676                         result = -ENODEV;
1677                         goto out_free;
1678                 }
1679                 if (!try_module_get(client->dev.driver->owner)) {
1680                         i2c_unregister_device(client);
1681                         module_put(dvb->i2c_client_tuner->dev.driver->owner);
1682                         i2c_unregister_device(dvb->i2c_client_tuner);
1683                         module_put(dvb->i2c_client_demod->dev.driver->owner);
1684                         i2c_unregister_device(dvb->i2c_client_demod);
1685                         result = -ENODEV;
1686                         goto out_free;
1687                 }
1688                 dvb->i2c_client_sec = client;
1689                 break;
1690         }
1691         case EM28178_BOARD_PCTV_292E:
1692                 {
1693                         struct i2c_adapter *adapter;
1694                         struct i2c_client *client;
1695                         struct i2c_board_info info;
1696                         struct si2168_config si2168_config;
1697                         struct si2157_config si2157_config;
1698
1699                         /* attach demod */
1700                         memset(&si2168_config, 0, sizeof(si2168_config));
1701                         si2168_config.i2c_adapter = &adapter;
1702                         si2168_config.fe = &dvb->fe[0];
1703                         si2168_config.ts_mode = SI2168_TS_PARALLEL;
1704                         memset(&info, 0, sizeof(struct i2c_board_info));
1705                         strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1706                         info.addr = 0x64;
1707                         info.platform_data = &si2168_config;
1708                         request_module(info.type);
1709                         client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1710                         if (client == NULL || client->dev.driver == NULL) {
1711                                 result = -ENODEV;
1712                                 goto out_free;
1713                         }
1714
1715                         if (!try_module_get(client->dev.driver->owner)) {
1716                                 i2c_unregister_device(client);
1717                                 result = -ENODEV;
1718                                 goto out_free;
1719                         }
1720
1721                         dvb->i2c_client_demod = client;
1722
1723                         /* attach tuner */
1724                         memset(&si2157_config, 0, sizeof(si2157_config));
1725                         si2157_config.fe = dvb->fe[0];
1726                         si2157_config.if_port = 1;
1727 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1728                         si2157_config.mdev = dev->media_dev;
1729 #endif
1730                         memset(&info, 0, sizeof(struct i2c_board_info));
1731                         strlcpy(info.type, "si2157", I2C_NAME_SIZE);
1732                         info.addr = 0x60;
1733                         info.platform_data = &si2157_config;
1734                         request_module(info.type);
1735                         client = i2c_new_device(adapter, &info);
1736                         if (client == NULL || client->dev.driver == NULL) {
1737                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1738                                 i2c_unregister_device(dvb->i2c_client_demod);
1739                                 result = -ENODEV;
1740                                 goto out_free;
1741                         }
1742
1743                         if (!try_module_get(client->dev.driver->owner)) {
1744                                 i2c_unregister_device(client);
1745                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1746                                 i2c_unregister_device(dvb->i2c_client_demod);
1747                                 result = -ENODEV;
1748                                 goto out_free;
1749                         }
1750
1751                         dvb->i2c_client_tuner = client;
1752                         dvb->fe[0]->ops.set_lna = em28xx_pctv_292e_set_lna;
1753                 }
1754                 break;
1755         case EM28178_BOARD_TERRATEC_T2_STICK_HD:
1756                 {
1757                         struct i2c_adapter *adapter;
1758                         struct i2c_client *client;
1759                         struct i2c_board_info info;
1760                         struct si2168_config si2168_config;
1761                         struct si2157_config si2157_config;
1762
1763                         /* attach demod */
1764                         memset(&si2168_config, 0, sizeof(si2168_config));
1765                         si2168_config.i2c_adapter = &adapter;
1766                         si2168_config.fe = &dvb->fe[0];
1767                         si2168_config.ts_mode = SI2168_TS_PARALLEL;
1768                         memset(&info, 0, sizeof(struct i2c_board_info));
1769                         strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1770                         info.addr = 0x64;
1771                         info.platform_data = &si2168_config;
1772                         request_module(info.type);
1773                         client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1774                         if (client == NULL || client->dev.driver == NULL) {
1775                                 result = -ENODEV;
1776                                 goto out_free;
1777                         }
1778
1779                         if (!try_module_get(client->dev.driver->owner)) {
1780                                 i2c_unregister_device(client);
1781                                 result = -ENODEV;
1782                                 goto out_free;
1783                         }
1784
1785                         dvb->i2c_client_demod = client;
1786
1787                         /* attach tuner */
1788                         memset(&si2157_config, 0, sizeof(si2157_config));
1789                         si2157_config.fe = dvb->fe[0];
1790                         si2157_config.if_port = 0;
1791 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1792                         si2157_config.mdev = dev->media_dev;
1793 #endif
1794                         memset(&info, 0, sizeof(struct i2c_board_info));
1795                         strlcpy(info.type, "si2146", I2C_NAME_SIZE);
1796                         info.addr = 0x60;
1797                         info.platform_data = &si2157_config;
1798                         request_module("si2157");
1799                         client = i2c_new_device(adapter, &info);
1800                         if (client == NULL || client->dev.driver == NULL) {
1801                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1802                                 i2c_unregister_device(dvb->i2c_client_demod);
1803                                 result = -ENODEV;
1804                                 goto out_free;
1805                         }
1806
1807                         if (!try_module_get(client->dev.driver->owner)) {
1808                                 i2c_unregister_device(client);
1809                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1810                                 i2c_unregister_device(dvb->i2c_client_demod);
1811                                 result = -ENODEV;
1812                                 goto out_free;
1813                         }
1814
1815                         dvb->i2c_client_tuner = client;
1816                 }
1817                 break;
1818
1819         case EM28178_BOARD_PLEX_PX_BCUD:
1820                 {
1821                         struct i2c_client *client;
1822                         struct i2c_board_info info;
1823                         struct tc90522_config tc90522_config;
1824                         struct qm1d1c0042_config qm1d1c0042_config;
1825
1826                         /* attach demod */
1827                         memset(&tc90522_config, 0, sizeof(tc90522_config));
1828                         memset(&info, 0, sizeof(struct i2c_board_info));
1829                         strlcpy(info.type, "tc90522sat", I2C_NAME_SIZE);
1830                         info.addr = 0x15;
1831                         info.platform_data = &tc90522_config;
1832                         request_module("tc90522");
1833                         client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1834                         if (client == NULL || client->dev.driver == NULL) {
1835                                 result = -ENODEV;
1836                                 goto out_free;
1837                         }
1838                         dvb->i2c_client_demod = client;
1839                         if (!try_module_get(client->dev.driver->owner)) {
1840                                 i2c_unregister_device(client);
1841                                 result = -ENODEV;
1842                                 goto out_free;
1843                         }
1844
1845                         /* attach tuner */
1846                         memset(&qm1d1c0042_config, 0,
1847                                sizeof(qm1d1c0042_config));
1848                         qm1d1c0042_config.fe = tc90522_config.fe;
1849                         qm1d1c0042_config.lpf = 1;
1850                         memset(&info, 0, sizeof(struct i2c_board_info));
1851                         strlcpy(info.type, "qm1d1c0042", I2C_NAME_SIZE);
1852                         info.addr = 0x61;
1853                         info.platform_data = &qm1d1c0042_config;
1854                         request_module(info.type);
1855                         client = i2c_new_device(tc90522_config.tuner_i2c,
1856                                                 &info);
1857                         if (client == NULL || client->dev.driver == NULL) {
1858                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1859                                 i2c_unregister_device(dvb->i2c_client_demod);
1860                                 result = -ENODEV;
1861                                 goto out_free;
1862                         }
1863                         dvb->i2c_client_tuner = client;
1864                         if (!try_module_get(client->dev.driver->owner)) {
1865                                 i2c_unregister_device(client);
1866                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1867                                 i2c_unregister_device(dvb->i2c_client_demod);
1868                                 result = -ENODEV;
1869                                 goto out_free;
1870                         }
1871                         dvb->fe[0] = tc90522_config.fe;
1872                         px_bcud_init(dev);
1873                 }
1874                 break;
1875         case EM28174_BOARD_HAUPPAUGE_WINTV_DUALHD_DVB:
1876                 {
1877                         struct i2c_adapter *adapter;
1878                         struct i2c_client *client;
1879                         struct i2c_board_info info;
1880                         struct si2168_config si2168_config;
1881                         struct si2157_config si2157_config;
1882
1883                         /* attach demod */
1884                         memset(&si2168_config, 0, sizeof(si2168_config));
1885                         si2168_config.i2c_adapter = &adapter;
1886                         si2168_config.fe = &dvb->fe[0];
1887                         si2168_config.ts_mode = SI2168_TS_SERIAL;
1888                         memset(&info, 0, sizeof(struct i2c_board_info));
1889                         strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1890                         info.addr = 0x64;
1891                         info.platform_data = &si2168_config;
1892                         request_module(info.type);
1893                         client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1894                         if (client == NULL || client->dev.driver == NULL) {
1895                                 result = -ENODEV;
1896                                 goto out_free;
1897                         }
1898
1899                         if (!try_module_get(client->dev.driver->owner)) {
1900                                 i2c_unregister_device(client);
1901                                 result = -ENODEV;
1902                                 goto out_free;
1903                         }
1904
1905                         dvb->i2c_client_demod = client;
1906
1907                         /* attach tuner */
1908                         memset(&si2157_config, 0, sizeof(si2157_config));
1909                         si2157_config.fe = dvb->fe[0];
1910                         si2157_config.if_port = 1;
1911 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1912                         si2157_config.mdev = dev->media_dev;
1913 #endif
1914                         memset(&info, 0, sizeof(struct i2c_board_info));
1915                         strlcpy(info.type, "si2157", I2C_NAME_SIZE);
1916                         info.addr = 0x60;
1917                         info.platform_data = &si2157_config;
1918                         request_module(info.type);
1919                         client = i2c_new_device(adapter, &info);
1920                         if (client == NULL || client->dev.driver == NULL) {
1921                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1922                                 i2c_unregister_device(dvb->i2c_client_demod);
1923                                 result = -ENODEV;
1924                                 goto out_free;
1925                         }
1926
1927                         if (!try_module_get(client->dev.driver->owner)) {
1928                                 i2c_unregister_device(client);
1929                                 module_put(dvb->i2c_client_demod->dev.driver->owner);
1930                                 i2c_unregister_device(dvb->i2c_client_demod);
1931                                 result = -ENODEV;
1932                                 goto out_free;
1933                         }
1934
1935                         dvb->i2c_client_tuner = client;
1936
1937                 }
1938                 break;
1939         default:
1940                 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
1941                                 " isn't supported yet\n");
1942                 break;
1943         }
1944         if (NULL == dvb->fe[0]) {
1945                 em28xx_errdev("/2: frontend initialization failed\n");
1946                 result = -EINVAL;
1947                 goto out_free;
1948         }
1949         /* define general-purpose callback pointer */
1950         dvb->fe[0]->callback = em28xx_tuner_callback;
1951         if (dvb->fe[1])
1952                 dvb->fe[1]->callback = em28xx_tuner_callback;
1953
1954         /* register everything */
1955         result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
1956
1957         if (result < 0)
1958                 goto out_free;
1959
1960         em28xx_info("DVB extension successfully initialized\n");
1961
1962         kref_get(&dev->ref);
1963
1964 ret:
1965         em28xx_set_mode(dev, EM28XX_SUSPEND);
1966         mutex_unlock(&dev->lock);
1967         return result;
1968
1969 out_free:
1970         em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
1971         kfree(dvb);
1972         dev->dvb = NULL;
1973         goto ret;
1974 }
1975
1976 static inline void prevent_sleep(struct dvb_frontend_ops *ops)
1977 {
1978         ops->set_voltage = NULL;
1979         ops->sleep = NULL;
1980         ops->tuner_ops.sleep = NULL;
1981 }
1982
1983 static int em28xx_dvb_fini(struct em28xx *dev)
1984 {
1985         struct em28xx_dvb *dvb;
1986         struct i2c_client *client;
1987
1988         if (dev->is_audio_only) {
1989                 /* Shouldn't initialize IR for this interface */
1990                 return 0;
1991         }
1992
1993         if (!dev->board.has_dvb) {
1994                 /* This device does not support the extension */
1995                 return 0;
1996         }
1997
1998         if (!dev->dvb)
1999                 return 0;
2000
2001         em28xx_info("Closing DVB extension\n");
2002
2003         dvb = dev->dvb;
2004
2005         em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
2006
2007         if (dev->disconnected) {
2008                 /* We cannot tell the device to sleep
2009                  * once it has been unplugged. */
2010                 if (dvb->fe[0]) {
2011                         prevent_sleep(&dvb->fe[0]->ops);
2012                         dvb->fe[0]->exit = DVB_FE_DEVICE_REMOVED;
2013                 }
2014                 if (dvb->fe[1]) {
2015                         prevent_sleep(&dvb->fe[1]->ops);
2016                         dvb->fe[1]->exit = DVB_FE_DEVICE_REMOVED;
2017                 }
2018         }
2019
2020         em28xx_unregister_dvb(dvb);
2021
2022         /* remove I2C SEC */
2023         client = dvb->i2c_client_sec;
2024         if (client) {
2025                 module_put(client->dev.driver->owner);
2026                 i2c_unregister_device(client);
2027         }
2028
2029         /* remove I2C tuner */
2030         client = dvb->i2c_client_tuner;
2031         if (client) {
2032                 module_put(client->dev.driver->owner);
2033                 i2c_unregister_device(client);
2034         }
2035
2036         /* remove I2C demod */
2037         client = dvb->i2c_client_demod;
2038         if (client) {
2039                 module_put(client->dev.driver->owner);
2040                 i2c_unregister_device(client);
2041         }
2042
2043         kfree(dvb);
2044         dev->dvb = NULL;
2045         kref_put(&dev->ref, em28xx_free_device);
2046
2047         return 0;
2048 }
2049
2050 static int em28xx_dvb_suspend(struct em28xx *dev)
2051 {
2052         int ret = 0;
2053
2054         if (dev->is_audio_only)
2055                 return 0;
2056
2057         if (!dev->board.has_dvb)
2058                 return 0;
2059
2060         em28xx_info("Suspending DVB extension\n");
2061         if (dev->dvb) {
2062                 struct em28xx_dvb *dvb = dev->dvb;
2063
2064                 if (dvb->fe[0]) {
2065                         ret = dvb_frontend_suspend(dvb->fe[0]);
2066                         em28xx_info("fe0 suspend %d\n", ret);
2067                 }
2068                 if (dvb->fe[1]) {
2069                         dvb_frontend_suspend(dvb->fe[1]);
2070                         em28xx_info("fe1 suspend %d\n", ret);
2071                 }
2072         }
2073
2074         return 0;
2075 }
2076
2077 static int em28xx_dvb_resume(struct em28xx *dev)
2078 {
2079         int ret = 0;
2080
2081         if (dev->is_audio_only)
2082                 return 0;
2083
2084         if (!dev->board.has_dvb)
2085                 return 0;
2086
2087         em28xx_info("Resuming DVB extension\n");
2088         if (dev->dvb) {
2089                 struct em28xx_dvb *dvb = dev->dvb;
2090
2091                 if (dvb->fe[0]) {
2092                         ret = dvb_frontend_resume(dvb->fe[0]);
2093                         em28xx_info("fe0 resume %d\n", ret);
2094                 }
2095
2096                 if (dvb->fe[1]) {
2097                         ret = dvb_frontend_resume(dvb->fe[1]);
2098                         em28xx_info("fe1 resume %d\n", ret);
2099                 }
2100         }
2101
2102         return 0;
2103 }
2104
2105 static struct em28xx_ops dvb_ops = {
2106         .id   = EM28XX_DVB,
2107         .name = "Em28xx dvb Extension",
2108         .init = em28xx_dvb_init,
2109         .fini = em28xx_dvb_fini,
2110         .suspend = em28xx_dvb_suspend,
2111         .resume = em28xx_dvb_resume,
2112 };
2113
2114 static int __init em28xx_dvb_register(void)
2115 {
2116         return em28xx_register_extension(&dvb_ops);
2117 }
2118
2119 static void __exit em28xx_dvb_unregister(void)
2120 {
2121         em28xx_unregister_extension(&dvb_ops);
2122 }
2123
2124 module_init(em28xx_dvb_register);
2125 module_exit(em28xx_dvb_unregister);