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