blob: ab8a740bc68cf0c2ce5abf19a335133ccc9e1162 [file] [log] [blame]
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03001/*
2 DVB device driver for em28xx
3
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -03004 (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03005
Devin Heitmuellerbdfbf952008-04-17 21:38:27 -03006 (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7 - Fixes for the driver to properly work with HVR-950
Devin Heitmueller4fd305b2008-06-04 13:43:46 -03008 - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
Devin Heitmuellere14b3652008-07-26 11:04:33 -03009 - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
Devin Heitmuellerbdfbf952008-04-17 21:38:27 -030010
Aidan Thornton3421b7782008-04-17 21:40:36 -030011 (c) 2008 Aidan Thornton <makosoft@googlemail.com>
12
13 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030014 (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
15 (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License.
20 */
21
22#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030024#include <linux/usb.h>
25
26#include "em28xx.h"
27#include <media/v4l2-common.h>
28#include <media/videobuf-vmalloc.h>
Franklin Mengd7de5d8f2009-06-06 17:05:02 -030029#include <media/tuner.h>
30#include "tuner-simple.h"
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030031
32#include "lgdt330x.h"
Jarod Wilson7e48b302010-03-07 17:55:43 -030033#include "lgdt3305.h"
Aidan Thornton7e6388a2008-04-17 21:40:03 -030034#include "zl10353.h"
Robert Krakora6e7b9ea2009-01-18 21:59:34 -030035#include "s5h1409.h"
Devin Heitmueller4fb202a2009-07-12 17:51:12 -030036#include "mt352.h"
37#include "mt352_priv.h" /* FIXME */
Antti Palosaari285eb1a2009-09-15 14:42:13 -030038#include "tda1002x.h"
Jarod Wilson7e48b302010-03-07 17:55:43 -030039#include "tda18271.h"
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -030040#include "s921.h"
Devin Heitmueller75e2b862011-03-13 02:01:02 -030041#include "drxd.h"
Antti Palosaarid6a5f922011-04-07 16:34:44 -030042#include "cxd2820r.h"
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -030043#include "tda18271c2dd.h"
44#include "drxk.h"
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030045
46MODULE_DESCRIPTION("driver for em28xx based DVB cards");
47MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
48MODULE_LICENSE("GPL");
49
50static unsigned int debug;
51module_param(debug, int, 0644);
52MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
53
54DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
55
56#define dprintk(level, fmt, arg...) do { \
57if (debug >= level) \
Aidan Thornton3421b7782008-04-17 21:40:36 -030058 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030059} while (0)
60
Aidan Thornton3421b7782008-04-17 21:40:36 -030061#define EM28XX_DVB_NUM_BUFS 5
Aidan Thornton3421b7782008-04-17 21:40:36 -030062#define EM28XX_DVB_MAX_PACKETS 64
63
64struct em28xx_dvb {
Antti Palosaarif71095b2011-04-07 15:51:52 -030065 struct dvb_frontend *fe[2];
Aidan Thornton3421b7782008-04-17 21:40:36 -030066
67 /* feed count management */
68 struct mutex lock;
69 int nfeeds;
70
71 /* general boilerplate stuff */
72 struct dvb_adapter adapter;
73 struct dvb_demux demux;
74 struct dmxdev dmxdev;
75 struct dmx_frontend fe_hw;
76 struct dmx_frontend fe_mem;
77 struct dvb_net net;
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -030078
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -030079 /* Due to DRX-K - probably need changes */
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -030080 int (*gate_ctrl)(struct dvb_frontend *, int);
81 struct semaphore pll_mutex;
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -030082 bool dont_attach_fe1;
Aidan Thornton3421b7782008-04-17 21:40:36 -030083};
84
85
86static inline void print_err_status(struct em28xx *dev,
87 int packet, int status)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030088{
Aidan Thornton3421b7782008-04-17 21:40:36 -030089 char *errmsg = "Unknown";
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030090
Aidan Thornton3421b7782008-04-17 21:40:36 -030091 switch (status) {
92 case -ENOENT:
93 errmsg = "unlinked synchronuously";
94 break;
95 case -ECONNRESET:
96 errmsg = "unlinked asynchronuously";
97 break;
98 case -ENOSR:
99 errmsg = "Buffer error (overrun)";
100 break;
101 case -EPIPE:
102 errmsg = "Stalled (device not responding)";
103 break;
104 case -EOVERFLOW:
105 errmsg = "Babble (bad cable?)";
106 break;
107 case -EPROTO:
108 errmsg = "Bit-stuff error (bad cable?)";
109 break;
110 case -EILSEQ:
111 errmsg = "CRC/Timeout (could be anything)";
112 break;
113 case -ETIME:
114 errmsg = "Device does not respond";
115 break;
116 }
117 if (packet < 0) {
118 dprintk(1, "URB status %d [%s].\n", status, errmsg);
119 } else {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300120 dprintk(1, "URB packet %d, status %d [%s].\n",
121 packet, status, errmsg);
Aidan Thornton3421b7782008-04-17 21:40:36 -0300122 }
123}
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300124
Aidan Thornton3421b7782008-04-17 21:40:36 -0300125static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
126{
127 int i;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300128
Aidan Thornton3421b7782008-04-17 21:40:36 -0300129 if (!dev)
130 return 0;
131
132 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
133 return 0;
134
135 if (urb->status < 0) {
136 print_err_status(dev, -1, urb->status);
137 if (urb->status == -ENOENT)
138 return 0;
139 }
140
141 for (i = 0; i < urb->number_of_packets; i++) {
142 int status = urb->iso_frame_desc[i].status;
143
144 if (status < 0) {
145 print_err_status(dev, i, status);
146 if (urb->iso_frame_desc[i].status != -EPROTO)
147 continue;
148 }
149
150 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
151 urb->iso_frame_desc[i].offset,
152 urb->iso_frame_desc[i].actual_length);
153 }
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300154
155 return 0;
156}
157
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300158static int start_streaming(struct em28xx_dvb *dvb)
159{
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300160 int rc;
Aidan Thornton3421b7782008-04-17 21:40:36 -0300161 struct em28xx *dev = dvb->adapter.priv;
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300162 int max_dvb_packet_size;
Aidan Thornton3421b7782008-04-17 21:40:36 -0300163
164 usb_set_interface(dev->udev, 0, 1);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300165 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
166 if (rc < 0)
167 return rc;
Aidan Thornton3421b7782008-04-17 21:40:36 -0300168
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300169 max_dvb_packet_size = em28xx_isoc_dvb_max_packetsize(dev);
170
Aidan Thornton3421b7782008-04-17 21:40:36 -0300171 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300172 EM28XX_DVB_NUM_BUFS, max_dvb_packet_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300173 dvb_isoc_copy);
Aidan Thornton3421b7782008-04-17 21:40:36 -0300174}
175
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300176static int stop_streaming(struct em28xx_dvb *dvb)
177{
Aidan Thornton3421b7782008-04-17 21:40:36 -0300178 struct em28xx *dev = dvb->adapter.priv;
179
180 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300181
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -0300182 em28xx_set_mode(dev, EM28XX_SUSPEND);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300183
Aidan Thornton3421b7782008-04-17 21:40:36 -0300184 return 0;
185}
186
187static int start_feed(struct dvb_demux_feed *feed)
188{
189 struct dvb_demux *demux = feed->demux;
190 struct em28xx_dvb *dvb = demux->priv;
191 int rc, ret;
192
193 if (!demux->dmx.frontend)
194 return -EINVAL;
195
196 mutex_lock(&dvb->lock);
197 dvb->nfeeds++;
198 rc = dvb->nfeeds;
199
200 if (dvb->nfeeds == 1) {
201 ret = start_streaming(dvb);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300202 if (ret < 0)
203 rc = ret;
Aidan Thornton3421b7782008-04-17 21:40:36 -0300204 }
205
206 mutex_unlock(&dvb->lock);
207 return rc;
208}
209
210static int stop_feed(struct dvb_demux_feed *feed)
211{
212 struct dvb_demux *demux = feed->demux;
213 struct em28xx_dvb *dvb = demux->priv;
214 int err = 0;
215
216 mutex_lock(&dvb->lock);
217 dvb->nfeeds--;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300218
219 if (0 == dvb->nfeeds)
Aidan Thornton3421b7782008-04-17 21:40:36 -0300220 err = stop_streaming(dvb);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300221
Aidan Thornton3421b7782008-04-17 21:40:36 -0300222 mutex_unlock(&dvb->lock);
223 return err;
224}
225
226
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300227
228/* ------------------------------------------------------------------ */
229static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
230{
231 struct em28xx *dev = fe->dvb->priv;
232
233 if (acquire)
234 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
235 else
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -0300236 return em28xx_set_mode(dev, EM28XX_SUSPEND);
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300237}
238
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300239/* ------------------------------------------------------------------ */
240
Mauro Carvalho Chehab227ad4a2008-04-17 21:37:40 -0300241static struct lgdt330x_config em2880_lgdt3303_dev = {
242 .demod_address = 0x0e,
243 .demod_chip = LGDT3303,
244};
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300245
Jarod Wilson7e48b302010-03-07 17:55:43 -0300246static struct lgdt3305_config em2870_lgdt3304_dev = {
247 .i2c_addr = 0x0e,
248 .demod_chip = LGDT3304,
249 .spectral_inversion = 1,
250 .deny_i2c_rptr = 1,
251 .mpeg_mode = LGDT3305_MPEG_PARALLEL,
252 .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
253 .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
254 .vsb_if_khz = 3250,
255 .qam_if_khz = 4000,
256};
257
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300258static struct s921_config sharp_isdbt = {
259 .demod_address = 0x30 >> 1
260};
261
Aidan Thornton7e6388a2008-04-17 21:40:03 -0300262static struct zl10353_config em28xx_zl10353_with_xc3028 = {
263 .demod_address = (0x1e >> 1),
264 .no_tuner = 1,
265 .parallel_ts = 1,
266 .if2 = 45600,
267};
268
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300269static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
270 .demod_address = 0x32 >> 1,
271 .output_mode = S5H1409_PARALLEL_OUTPUT,
272 .gpio = S5H1409_GPIO_OFF,
273 .inversion = S5H1409_INVERSION_OFF,
274 .status_mode = S5H1409_DEMODLOCKING,
275 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
276};
277
Jarod Wilson7e48b302010-03-07 17:55:43 -0300278static struct tda18271_std_map kworld_a340_std_map = {
279 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
280 .if_lvl = 1, .rfagc_top = 0x37, },
281 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
282 .if_lvl = 1, .rfagc_top = 0x37, },
283};
284
285static struct tda18271_config kworld_a340_config = {
286 .std_map = &kworld_a340_std_map,
287};
288
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300289static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300290 .demod_address = (0x1e >> 1),
291 .no_tuner = 1,
292 .disable_i2c_gate_ctrl = 1,
293 .parallel_ts = 1,
294 .if2 = 45600,
295};
296
Devin Heitmueller75e2b862011-03-13 02:01:02 -0300297static struct drxd_config em28xx_drxd = {
298 .index = 0, .demod_address = 0x70, .demod_revision = 0xa2,
299 .demoda_address = 0x00, .pll_address = 0x00,
300 .pll_type = DRXD_PLL_NONE, .clock = 12000, .insert_rs_byte = 1,
301 .pll_set = NULL, .osc_deviation = NULL, .IF = 42800000,
Devin Heitmueller6b142b32011-03-13 02:02:01 -0300302 .disable_i2c_gate_ctrl = 1,
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300303};
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300304
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300305struct drxk_config terratec_h5_drxk = {
306 .adr = 0x29,
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -0300307 .single_master = 1,
Mauro Carvalho Chehabf1fe1b72011-07-09 21:59:33 -0300308 .no_i2c_bridge = 1,
Mauro Carvalho Chehab8b9456a2011-07-11 14:33:51 -0300309 .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300310};
311
312static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
313{
314 struct em28xx_dvb *dvb = fe->sec_priv;
315 int status;
316
317 if (!dvb)
318 return -EINVAL;
319
320 if (enable) {
321 down(&dvb->pll_mutex);
322 status = dvb->gate_ctrl(fe, 1);
323 } else {
324 status = dvb->gate_ctrl(fe, 0);
325 up(&dvb->pll_mutex);
326 }
327 return status;
328}
329
330static void terratec_h5_init(struct em28xx *dev)
331{
332 int i;
333 struct em28xx_reg_seq terratec_h5_init[] = {
334 {EM28XX_R08_GPIO, 0xff, 0xff, 10},
335 {EM2874_R80_GPIO, 0xf6, 0xff, 100},
336 {EM2874_R80_GPIO, 0xf2, 0xff, 50},
337 {EM2874_R80_GPIO, 0xf6, 0xff, 100},
338 { -1, -1, -1, -1},
339 };
340 struct em28xx_reg_seq terratec_h5_end[] = {
341 {EM2874_R80_GPIO, 0xe6, 0xff, 100},
342 {EM2874_R80_GPIO, 0xa6, 0xff, 50},
343 {EM2874_R80_GPIO, 0xe6, 0xff, 100},
344 { -1, -1, -1, -1},
345 };
346 struct {
347 unsigned char r[4];
348 int len;
349 } regs[] = {
350 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
351 {{ 0x01, 0x02 }, 2},
352 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
353 {{ 0x01, 0x00 }, 2},
354 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
355 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
356 {{ 0x01, 0x00 }, 2},
357 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
358 {{ 0x04, 0x00 }, 2},
359 {{ 0x00, 0x04 }, 2},
360 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
361 {{ 0x04, 0x14 }, 2},
362 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
363 };
364
365 em28xx_gpio_set(dev, terratec_h5_init);
366 em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
367 msleep(10);
368 em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
369 msleep(10);
370
371 dev->i2c_client.addr = 0x82 >> 1;
372
373 for (i = 0; i < ARRAY_SIZE(regs); i++)
374 i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
375 em28xx_gpio_set(dev, terratec_h5_end);
376};
377
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300378static int mt352_terratec_xs_init(struct dvb_frontend *fe)
379{
380 /* Values extracted from a USB trace of the Terratec Windows driver */
381 static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
382 static u8 reset[] = { RESET, 0x80 };
383 static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
384 static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
385 static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
386 static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
387 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
388 static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
389 static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
Devin Heitmuellerff697862009-07-12 18:44:19 -0300390 static u8 tuner_go[] = { TUNER_GO, 0x01};
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300391
392 mt352_write(fe, clock_config, sizeof(clock_config));
393 udelay(200);
394 mt352_write(fe, reset, sizeof(reset));
395 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
396 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
397 mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
398 mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
399 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
400 mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
401 mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
402 mt352_write(fe, tuner_go, sizeof(tuner_go));
403 return 0;
404}
405
406static struct mt352_config terratec_xs_mt352_cfg = {
407 .demod_address = (0x1e >> 1),
408 .no_tuner = 1,
409 .if2 = 45600,
410 .demod_init = mt352_terratec_xs_init,
411};
412
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300413static struct tda10023_config em28xx_tda10023_config = {
414 .demod_address = 0x0c,
415 .invert = 1,
416};
417
Antti Palosaarid6a5f922011-04-07 16:34:44 -0300418static struct cxd2820r_config em28xx_cxd2820r_config = {
419 .i2c_address = (0xd8 >> 1),
420 .ts_mode = CXD2820R_TS_SERIAL,
421 .if_dvbt_6 = 3300,
422 .if_dvbt_7 = 3500,
423 .if_dvbt_8 = 4000,
424 .if_dvbt2_6 = 3300,
425 .if_dvbt2_7 = 3500,
426 .if_dvbt2_8 = 4000,
427 .if_dvbc = 5000,
428
429 /* enable LNA for DVB-T2 and DVB-C */
430 .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
431 .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
432};
433
434static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
435 .output_opt = TDA18271_OUTPUT_LT_OFF,
436};
437
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300438/* ------------------------------------------------------------------ */
439
440static int attach_xc3028(u8 addr, struct em28xx *dev)
441{
442 struct dvb_frontend *fe;
Mauro Carvalho Chehab3ca9c092008-04-17 21:37:53 -0300443 struct xc2028_config cfg;
444
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300445 memset(&cfg, 0, sizeof(cfg));
Mauro Carvalho Chehab3ca9c092008-04-17 21:37:53 -0300446 cfg.i2c_adap = &dev->i2c_adap;
447 cfg.i2c_addr = addr;
Mauro Carvalho Chehab3ca9c092008-04-17 21:37:53 -0300448
Antti Palosaarif71095b2011-04-07 15:51:52 -0300449 if (!dev->dvb->fe[0]) {
Filipe Rosset480be182009-11-04 15:32:37 -0300450 em28xx_errdev("/2: dvb frontend not attached. "
451 "Can't attach xc3028\n");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300452 return -EINVAL;
453 }
454
Antti Palosaarif71095b2011-04-07 15:51:52 -0300455 fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300456 if (!fe) {
Filipe Rosset480be182009-11-04 15:32:37 -0300457 em28xx_errdev("/2: xc3028 attach failed\n");
Antti Palosaarif71095b2011-04-07 15:51:52 -0300458 dvb_frontend_detach(dev->dvb->fe[0]);
459 dev->dvb->fe[0] = NULL;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300460 return -EINVAL;
461 }
462
Filipe Rosset480be182009-11-04 15:32:37 -0300463 em28xx_info("%s/2: xc3028 attached\n", dev->name);
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300464
465 return 0;
466}
467
Aidan Thornton3421b7782008-04-17 21:40:36 -0300468/* ------------------------------------------------------------------ */
469
Hans Verkuild45b9b82008-09-04 03:33:43 -0300470static int register_dvb(struct em28xx_dvb *dvb,
Aidan Thornton3421b7782008-04-17 21:40:36 -0300471 struct module *module,
472 struct em28xx *dev,
473 struct device *device)
474{
475 int result;
476
477 mutex_init(&dvb->lock);
478
479 /* register adapter */
480 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
481 adapter_nr);
482 if (result < 0) {
483 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
484 dev->name, result);
485 goto fail_adapter;
486 }
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300487
488 /* Ensure all frontends negotiate bus access */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300489 dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
490 if (dvb->fe[1])
491 dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300492
Aidan Thornton3421b7782008-04-17 21:40:36 -0300493 dvb->adapter.priv = dev;
494
495 /* register frontend */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300496 result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
Aidan Thornton3421b7782008-04-17 21:40:36 -0300497 if (result < 0) {
498 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
499 dev->name, result);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300500 goto fail_frontend0;
501 }
502
503 /* register 2nd frontend */
504 if (dvb->fe[1]) {
505 result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
506 if (result < 0) {
507 printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
508 dev->name, result);
509 goto fail_frontend1;
510 }
Aidan Thornton3421b7782008-04-17 21:40:36 -0300511 }
512
513 /* register demux stuff */
514 dvb->demux.dmx.capabilities =
515 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
516 DMX_MEMORY_BASED_FILTERING;
517 dvb->demux.priv = dvb;
518 dvb->demux.filternum = 256;
519 dvb->demux.feednum = 256;
520 dvb->demux.start_feed = start_feed;
521 dvb->demux.stop_feed = stop_feed;
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300522
Aidan Thornton3421b7782008-04-17 21:40:36 -0300523 result = dvb_dmx_init(&dvb->demux);
524 if (result < 0) {
525 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
526 dev->name, result);
527 goto fail_dmx;
528 }
529
530 dvb->dmxdev.filternum = 256;
531 dvb->dmxdev.demux = &dvb->demux.dmx;
532 dvb->dmxdev.capabilities = 0;
533 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
534 if (result < 0) {
535 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
536 dev->name, result);
537 goto fail_dmxdev;
538 }
539
540 dvb->fe_hw.source = DMX_FRONTEND_0;
541 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
542 if (result < 0) {
543 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
544 dev->name, result);
545 goto fail_fe_hw;
546 }
547
548 dvb->fe_mem.source = DMX_MEMORY_FE;
549 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
550 if (result < 0) {
551 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
552 dev->name, result);
553 goto fail_fe_mem;
554 }
555
556 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
557 if (result < 0) {
558 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
559 dev->name, result);
560 goto fail_fe_conn;
561 }
562
563 /* register network adapter */
564 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
565 return 0;
566
567fail_fe_conn:
568 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
569fail_fe_mem:
570 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
571fail_fe_hw:
572 dvb_dmxdev_release(&dvb->dmxdev);
573fail_dmxdev:
574 dvb_dmx_release(&dvb->demux);
575fail_dmx:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300576 if (dvb->fe[1])
577 dvb_unregister_frontend(dvb->fe[1]);
578 dvb_unregister_frontend(dvb->fe[0]);
579fail_frontend1:
580 if (dvb->fe[1])
581 dvb_frontend_detach(dvb->fe[1]);
582fail_frontend0:
583 dvb_frontend_detach(dvb->fe[0]);
Aidan Thornton3421b7782008-04-17 21:40:36 -0300584 dvb_unregister_adapter(&dvb->adapter);
585fail_adapter:
586 return result;
587}
588
589static void unregister_dvb(struct em28xx_dvb *dvb)
590{
591 dvb_net_release(&dvb->net);
592 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
593 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
594 dvb_dmxdev_release(&dvb->dmxdev);
595 dvb_dmx_release(&dvb->demux);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300596 if (dvb->fe[1])
597 dvb_unregister_frontend(dvb->fe[1]);
598 dvb_unregister_frontend(dvb->fe[0]);
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300599 if (dvb->fe[1] && !dvb->dont_attach_fe1)
Antti Palosaarif71095b2011-04-07 15:51:52 -0300600 dvb_frontend_detach(dvb->fe[1]);
601 dvb_frontend_detach(dvb->fe[0]);
Aidan Thornton3421b7782008-04-17 21:40:36 -0300602 dvb_unregister_adapter(&dvb->adapter);
603}
604
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300605static int dvb_init(struct em28xx *dev)
606{
Aidan Thornton3421b7782008-04-17 21:40:36 -0300607 int result = 0;
608 struct em28xx_dvb *dvb;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300609
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300610 if (!dev->board.has_dvb) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300611 /* This device does not support the extension */
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300612 printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300613 return 0;
614 }
615
Aidan Thornton3421b7782008-04-17 21:40:36 -0300616 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300617
618 if (dvb == NULL) {
Filipe Rosset480be182009-11-04 15:32:37 -0300619 em28xx_info("em28xx_dvb: memory allocation failed\n");
Aidan Thornton3421b7782008-04-17 21:40:36 -0300620 return -ENOMEM;
621 }
622 dev->dvb = dvb;
Antti Palosaarif71095b2011-04-07 15:51:52 -0300623 dvb->fe[0] = dvb->fe[1] = NULL;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300624
Mauro Carvalho Chehab50133182010-04-07 17:07:58 -0300625 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300626 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300627 /* init frontend */
628 switch (dev->model) {
Mauro Carvalho Chehabebaefdb2011-06-01 10:16:25 -0300629 case EM2874_BOARD_LEADERSHIP_ISDBT:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300630 dvb->fe[0] = dvb_attach(s921_attach,
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300631 &sharp_isdbt, &dev->i2c_adap);
632
Antti Palosaarif71095b2011-04-07 15:51:52 -0300633 if (!dvb->fe[0]) {
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300634 result = -EINVAL;
635 goto out_free;
636 }
637
638 break;
Douglas Schilling Landgraff89bc322008-12-01 21:01:04 -0300639 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
Mauro Carvalho Chehab10ac6602008-07-27 14:58:58 -0300640 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
Devin Heitmueller4fd305b2008-06-04 13:43:46 -0300641 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
Devin Heitmuellere14b3652008-07-26 11:04:33 -0300642 case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300643 dvb->fe[0] = dvb_attach(lgdt330x_attach,
Aidan Thornton3421b7782008-04-17 21:40:36 -0300644 &em2880_lgdt3303_dev,
645 &dev->i2c_adap);
646 if (attach_xc3028(0x61, dev) < 0) {
647 result = -EINVAL;
648 goto out_free;
649 }
Mauro Carvalho Chehab227ad4a2008-04-17 21:37:40 -0300650 break;
Thierry MERLE46510b52008-10-11 16:56:13 -0300651 case EM2880_BOARD_KWORLD_DVB_310U:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300652 dvb->fe[0] = dvb_attach(zl10353_attach,
Aidan Thornton3421b7782008-04-17 21:40:36 -0300653 &em28xx_zl10353_with_xc3028,
654 &dev->i2c_adap);
655 if (attach_xc3028(0x61, dev) < 0) {
656 result = -EINVAL;
657 goto out_free;
658 }
Aidan Thornton7e6388a2008-04-17 21:40:03 -0300659 break;
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300660 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
Uroš Vamplec994d02009-09-30 22:53:37 -0300661 case EM2882_BOARD_TERRATEC_HYBRID_XS:
Devin Heitmueller01a5fd62009-08-07 09:25:06 -0300662 case EM2880_BOARD_EMPIRE_DUAL_TV:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300663 dvb->fe[0] = dvb_attach(zl10353_attach,
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300664 &em28xx_zl10353_xc3028_no_i2c_gate,
665 &dev->i2c_adap);
666 if (attach_xc3028(0x61, dev) < 0) {
667 result = -EINVAL;
668 goto out_free;
669 }
670 break;
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300671 case EM2880_BOARD_TERRATEC_HYBRID_XS:
Catimimi65638012010-02-18 18:06:32 -0300672 case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
Devin Heitmuellerd5b3ba92009-07-08 21:51:35 -0300673 case EM2881_BOARD_PINNACLE_HYBRID_PRO:
Andrea.Amorosi76@gmail.com7ca7ef62010-02-09 17:53:38 -0300674 case EM2882_BOARD_DIKOM_DK300:
Antonio Larrosa811fab62010-03-04 22:19:48 -0300675 case EM2882_BOARD_KWORLD_VS_DVBT:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300676 dvb->fe[0] = dvb_attach(zl10353_attach,
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300677 &em28xx_zl10353_xc3028_no_i2c_gate,
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300678 &dev->i2c_adap);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300679 if (dvb->fe[0] == NULL) {
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300680 /* This board could have either a zl10353 or a mt352.
681 If the chip id isn't for zl10353, try mt352 */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300682 dvb->fe[0] = dvb_attach(mt352_attach,
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300683 &terratec_xs_mt352_cfg,
684 &dev->i2c_adap);
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300685 }
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300686
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300687 if (attach_xc3028(0x61, dev) < 0) {
688 result = -EINVAL;
689 goto out_free;
690 }
691 break;
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300692 case EM2883_BOARD_KWORLD_HYBRID_330U:
Devin Heitmueller19859222009-06-19 00:33:54 -0300693 case EM2882_BOARD_EVGA_INDTUBE:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300694 dvb->fe[0] = dvb_attach(s5h1409_attach,
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300695 &em28xx_s5h1409_with_xc3028,
696 &dev->i2c_adap);
697 if (attach_xc3028(0x61, dev) < 0) {
698 result = -EINVAL;
699 goto out_free;
700 }
701 break;
Franklin Mengd7de5d8f2009-06-06 17:05:02 -0300702 case EM2882_BOARD_KWORLD_ATSC_315U:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300703 dvb->fe[0] = dvb_attach(lgdt330x_attach,
Franklin Mengd7de5d8f2009-06-06 17:05:02 -0300704 &em2880_lgdt3303_dev,
705 &dev->i2c_adap);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300706 if (dvb->fe[0] != NULL) {
707 if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
Franklin Mengd7de5d8f2009-06-06 17:05:02 -0300708 &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
709 result = -EINVAL;
710 goto out_free;
711 }
712 }
713 break;
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300714 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
Devin Heitmuellerad9b4bb2011-03-13 02:09:59 -0300715 case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300716 dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
Devin Heitmueller75e2b862011-03-13 02:01:02 -0300717 &dev->i2c_adap, &dev->udev->dev);
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300718 if (attach_xc3028(0x61, dev) < 0) {
719 result = -EINVAL;
720 goto out_free;
721 }
722 break;
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300723 case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
724 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300725 dvb->fe[0] = dvb_attach(tda10023_attach,
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300726 &em28xx_tda10023_config,
727 &dev->i2c_adap, 0x48);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300728 if (dvb->fe[0]) {
729 if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300730 &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
731 result = -EINVAL;
732 goto out_free;
733 }
734 }
735 break;
Jarod Wilson7e48b302010-03-07 17:55:43 -0300736 case EM2870_BOARD_KWORLD_A340:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300737 dvb->fe[0] = dvb_attach(lgdt3305_attach,
Jarod Wilson7e48b302010-03-07 17:55:43 -0300738 &em2870_lgdt3304_dev,
739 &dev->i2c_adap);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300740 if (dvb->fe[0] != NULL)
741 dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
Jarod Wilson7e48b302010-03-07 17:55:43 -0300742 &dev->i2c_adap, &kworld_a340_config);
743 break;
Antti Palosaarid6a5f922011-04-07 16:34:44 -0300744 case EM28174_BOARD_PCTV_290E:
745 /* MFE
746 * FE 0 = DVB-T/T2 + FE 1 = DVB-C, both sharing same tuner. */
747 /* FE 0 */
748 dvb->fe[0] = dvb_attach(cxd2820r_attach,
749 &em28xx_cxd2820r_config, &dev->i2c_adap, NULL);
750 if (dvb->fe[0]) {
751 struct i2c_adapter *i2c_tuner;
752 i2c_tuner = cxd2820r_get_tuner_i2c_adapter(dvb->fe[0]);
753 /* FE 0 attach tuner */
754 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
755 i2c_tuner, &em28xx_cxd2820r_tda18271_config)) {
756 dvb_frontend_detach(dvb->fe[0]);
757 result = -EINVAL;
758 goto out_free;
759 }
760 /* FE 1. This dvb_attach() cannot fail. */
761 dvb->fe[1] = dvb_attach(cxd2820r_attach, NULL, NULL,
762 dvb->fe[0]);
763 dvb->fe[1]->id = 1;
764 /* FE 1 attach tuner */
765 if (!dvb_attach(tda18271_attach, dvb->fe[1], 0x60,
766 i2c_tuner, &em28xx_cxd2820r_tda18271_config)) {
767 dvb_frontend_detach(dvb->fe[1]);
768 /* leave FE 0 still active */
769 }
770 }
771 break;
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300772 case EM2884_BOARD_TERRATEC_H5:
773 terratec_h5_init(dev);
774
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300775 dvb->dont_attach_fe1 = 1;
776
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300777 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap, &dvb->fe[1]);
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300778 if (!dvb->fe[0]) {
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300779 result = -EINVAL;
780 goto out_free;
781 }
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300782
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300783 /* FIXME: do we need a pll semaphore? */
784 dvb->fe[0]->sec_priv = dvb;
785 sema_init(&dvb->pll_mutex, 1);
786 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
787 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300788 dvb->fe[1]->id = 1;
789
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300790 /* Attach tda18271 to DVB-C frontend */
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300791 if (dvb->fe[0]->ops.i2c_gate_ctrl)
792 dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
793 if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
794 result = -EINVAL;
795 goto out_free;
796 }
797 if (dvb->fe[0]->ops.i2c_gate_ctrl)
798 dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300799
800 /* Hack - needed by drxk/tda18271c2dd */
801 dvb->fe[1]->tuner_priv = dvb->fe[0]->tuner_priv;
802 memcpy(&dvb->fe[1]->ops.tuner_ops,
803 &dvb->fe[0]->ops.tuner_ops,
804 sizeof(dvb->fe[0]->ops.tuner_ops));
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300805
806 break;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300807 default:
Filipe Rosset480be182009-11-04 15:32:37 -0300808 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
809 " isn't supported yet\n");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300810 break;
811 }
Antti Palosaarif71095b2011-04-07 15:51:52 -0300812 if (NULL == dvb->fe[0]) {
Filipe Rosset480be182009-11-04 15:32:37 -0300813 em28xx_errdev("/2: frontend initialization failed\n");
Aidan Thornton3421b7782008-04-17 21:40:36 -0300814 result = -EINVAL;
815 goto out_free;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300816 }
Michael Krufkyd7cba042008-09-12 13:31:45 -0300817 /* define general-purpose callback pointer */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300818 dvb->fe[0]->callback = em28xx_tuner_callback;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300819
820 /* register everything */
Aidan Thornton3421b7782008-04-17 21:40:36 -0300821 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
822
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300823 if (result < 0)
Aidan Thornton3421b7782008-04-17 21:40:36 -0300824 goto out_free;
Aidan Thornton3421b7782008-04-17 21:40:36 -0300825
Filipe Rosset480be182009-11-04 15:32:37 -0300826 em28xx_info("Successfully loaded em28xx-dvb\n");
Mauro Carvalho Chehab50133182010-04-07 17:07:58 -0300827ret:
828 em28xx_set_mode(dev, EM28XX_SUSPEND);
829 mutex_unlock(&dev->lock);
830 return result;
Aidan Thornton3421b7782008-04-17 21:40:36 -0300831
832out_free:
833 kfree(dvb);
834 dev->dvb = NULL;
Mauro Carvalho Chehab50133182010-04-07 17:07:58 -0300835 goto ret;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300836}
837
838static int dvb_fini(struct em28xx *dev)
839{
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300840 if (!dev->board.has_dvb) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300841 /* This device does not support the extension */
842 return 0;
843 }
844
Aidan Thornton3421b7782008-04-17 21:40:36 -0300845 if (dev->dvb) {
846 unregister_dvb(dev->dvb);
Francesco Lavra19f48cb2009-12-31 08:47:11 -0300847 kfree(dev->dvb);
Aidan Thornton3421b7782008-04-17 21:40:36 -0300848 dev->dvb = NULL;
849 }
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300850
851 return 0;
852}
853
854static struct em28xx_ops dvb_ops = {
855 .id = EM28XX_DVB,
856 .name = "Em28xx dvb Extension",
857 .init = dvb_init,
858 .fini = dvb_fini,
859};
860
861static int __init em28xx_dvb_register(void)
862{
863 return em28xx_register_extension(&dvb_ops);
864}
865
866static void __exit em28xx_dvb_unregister(void)
867{
868 em28xx_unregister_extension(&dvb_ops);
869}
870
871module_init(em28xx_dvb_register);
872module_exit(em28xx_dvb_unregister);