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