blob: c7c04bf712aaaee227c1fcbfe508dfb29cebe71a [file] [log] [blame]
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03001/*
2 DVB device driver for em28xx
3
4 (c) 2008 Mauro Carvalho Chehab <mchehab@infradead.org>
5
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 Thornton3421b772008-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 Mengd7de5d82009-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"
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030041
42MODULE_DESCRIPTION("driver for em28xx based DVB cards");
43MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
44MODULE_LICENSE("GPL");
45
46static unsigned int debug;
47module_param(debug, int, 0644);
48MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
49
50DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
51
52#define dprintk(level, fmt, arg...) do { \
53if (debug >= level) \
Aidan Thornton3421b772008-04-17 21:40:36 -030054 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030055} while (0)
56
Aidan Thornton3421b772008-04-17 21:40:36 -030057#define EM28XX_DVB_NUM_BUFS 5
Aidan Thornton3421b772008-04-17 21:40:36 -030058#define EM28XX_DVB_MAX_PACKETS 64
59
60struct em28xx_dvb {
61 struct dvb_frontend *frontend;
62
63 /* feed count management */
64 struct mutex lock;
65 int nfeeds;
66
67 /* general boilerplate stuff */
68 struct dvb_adapter adapter;
69 struct dvb_demux demux;
70 struct dmxdev dmxdev;
71 struct dmx_frontend fe_hw;
72 struct dmx_frontend fe_mem;
73 struct dvb_net net;
74};
75
76
77static inline void print_err_status(struct em28xx *dev,
78 int packet, int status)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030079{
Aidan Thornton3421b772008-04-17 21:40:36 -030080 char *errmsg = "Unknown";
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030081
Aidan Thornton3421b772008-04-17 21:40:36 -030082 switch (status) {
83 case -ENOENT:
84 errmsg = "unlinked synchronuously";
85 break;
86 case -ECONNRESET:
87 errmsg = "unlinked asynchronuously";
88 break;
89 case -ENOSR:
90 errmsg = "Buffer error (overrun)";
91 break;
92 case -EPIPE:
93 errmsg = "Stalled (device not responding)";
94 break;
95 case -EOVERFLOW:
96 errmsg = "Babble (bad cable?)";
97 break;
98 case -EPROTO:
99 errmsg = "Bit-stuff error (bad cable?)";
100 break;
101 case -EILSEQ:
102 errmsg = "CRC/Timeout (could be anything)";
103 break;
104 case -ETIME:
105 errmsg = "Device does not respond";
106 break;
107 }
108 if (packet < 0) {
109 dprintk(1, "URB status %d [%s].\n", status, errmsg);
110 } else {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300111 dprintk(1, "URB packet %d, status %d [%s].\n",
112 packet, status, errmsg);
Aidan Thornton3421b772008-04-17 21:40:36 -0300113 }
114}
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300115
Aidan Thornton3421b772008-04-17 21:40:36 -0300116static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
117{
118 int i;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300119
Aidan Thornton3421b772008-04-17 21:40:36 -0300120 if (!dev)
121 return 0;
122
123 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
124 return 0;
125
126 if (urb->status < 0) {
127 print_err_status(dev, -1, urb->status);
128 if (urb->status == -ENOENT)
129 return 0;
130 }
131
132 for (i = 0; i < urb->number_of_packets; i++) {
133 int status = urb->iso_frame_desc[i].status;
134
135 if (status < 0) {
136 print_err_status(dev, i, status);
137 if (urb->iso_frame_desc[i].status != -EPROTO)
138 continue;
139 }
140
141 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
142 urb->iso_frame_desc[i].offset,
143 urb->iso_frame_desc[i].actual_length);
144 }
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300145
146 return 0;
147}
148
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300149static int start_streaming(struct em28xx_dvb *dvb)
150{
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300151 int rc;
Aidan Thornton3421b772008-04-17 21:40:36 -0300152 struct em28xx *dev = dvb->adapter.priv;
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300153 int max_dvb_packet_size;
Aidan Thornton3421b772008-04-17 21:40:36 -0300154
155 usb_set_interface(dev->udev, 0, 1);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300156 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
157 if (rc < 0)
158 return rc;
Aidan Thornton3421b772008-04-17 21:40:36 -0300159
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300160 max_dvb_packet_size = em28xx_isoc_dvb_max_packetsize(dev);
161
Aidan Thornton3421b772008-04-17 21:40:36 -0300162 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300163 EM28XX_DVB_NUM_BUFS, max_dvb_packet_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300164 dvb_isoc_copy);
Aidan Thornton3421b772008-04-17 21:40:36 -0300165}
166
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300167static int stop_streaming(struct em28xx_dvb *dvb)
168{
Aidan Thornton3421b772008-04-17 21:40:36 -0300169 struct em28xx *dev = dvb->adapter.priv;
170
171 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300172
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -0300173 em28xx_set_mode(dev, EM28XX_SUSPEND);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300174
Aidan Thornton3421b772008-04-17 21:40:36 -0300175 return 0;
176}
177
178static int start_feed(struct dvb_demux_feed *feed)
179{
180 struct dvb_demux *demux = feed->demux;
181 struct em28xx_dvb *dvb = demux->priv;
182 int rc, ret;
183
184 if (!demux->dmx.frontend)
185 return -EINVAL;
186
187 mutex_lock(&dvb->lock);
188 dvb->nfeeds++;
189 rc = dvb->nfeeds;
190
191 if (dvb->nfeeds == 1) {
192 ret = start_streaming(dvb);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300193 if (ret < 0)
194 rc = ret;
Aidan Thornton3421b772008-04-17 21:40:36 -0300195 }
196
197 mutex_unlock(&dvb->lock);
198 return rc;
199}
200
201static int stop_feed(struct dvb_demux_feed *feed)
202{
203 struct dvb_demux *demux = feed->demux;
204 struct em28xx_dvb *dvb = demux->priv;
205 int err = 0;
206
207 mutex_lock(&dvb->lock);
208 dvb->nfeeds--;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300209
210 if (0 == dvb->nfeeds)
Aidan Thornton3421b772008-04-17 21:40:36 -0300211 err = stop_streaming(dvb);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300212
Aidan Thornton3421b772008-04-17 21:40:36 -0300213 mutex_unlock(&dvb->lock);
214 return err;
215}
216
217
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300218
219/* ------------------------------------------------------------------ */
220static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
221{
222 struct em28xx *dev = fe->dvb->priv;
223
224 if (acquire)
225 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
226 else
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -0300227 return em28xx_set_mode(dev, EM28XX_SUSPEND);
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300228}
229
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300230/* ------------------------------------------------------------------ */
231
Mauro Carvalho Chehab227ad4a2008-04-17 21:37:40 -0300232static struct lgdt330x_config em2880_lgdt3303_dev = {
233 .demod_address = 0x0e,
234 .demod_chip = LGDT3303,
235};
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300236
Jarod Wilson7e48b302010-03-07 17:55:43 -0300237static struct lgdt3305_config em2870_lgdt3304_dev = {
238 .i2c_addr = 0x0e,
239 .demod_chip = LGDT3304,
240 .spectral_inversion = 1,
241 .deny_i2c_rptr = 1,
242 .mpeg_mode = LGDT3305_MPEG_PARALLEL,
243 .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
244 .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
245 .vsb_if_khz = 3250,
246 .qam_if_khz = 4000,
247};
248
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300249static struct s921_config sharp_isdbt = {
250 .demod_address = 0x30 >> 1
251};
252
Aidan Thornton7e6388a2008-04-17 21:40:03 -0300253static struct zl10353_config em28xx_zl10353_with_xc3028 = {
254 .demod_address = (0x1e >> 1),
255 .no_tuner = 1,
256 .parallel_ts = 1,
257 .if2 = 45600,
258};
259
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300260static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
261 .demod_address = 0x32 >> 1,
262 .output_mode = S5H1409_PARALLEL_OUTPUT,
263 .gpio = S5H1409_GPIO_OFF,
264 .inversion = S5H1409_INVERSION_OFF,
265 .status_mode = S5H1409_DEMODLOCKING,
266 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
267};
268
Jarod Wilson7e48b302010-03-07 17:55:43 -0300269static struct tda18271_std_map kworld_a340_std_map = {
270 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
271 .if_lvl = 1, .rfagc_top = 0x37, },
272 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
273 .if_lvl = 1, .rfagc_top = 0x37, },
274};
275
276static struct tda18271_config kworld_a340_config = {
277 .std_map = &kworld_a340_std_map,
278};
279
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300280static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300281 .demod_address = (0x1e >> 1),
282 .no_tuner = 1,
283 .disable_i2c_gate_ctrl = 1,
284 .parallel_ts = 1,
285 .if2 = 45600,
286};
287
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300288#ifdef EM28XX_DRX397XD_SUPPORT
289/* [TODO] djh - not sure yet what the device config needs to contain */
290static struct drx397xD_config em28xx_drx397xD_with_xc3028 = {
291 .demod_address = (0xe0 >> 1),
292};
293#endif
294
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300295static int mt352_terratec_xs_init(struct dvb_frontend *fe)
296{
297 /* Values extracted from a USB trace of the Terratec Windows driver */
298 static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
299 static u8 reset[] = { RESET, 0x80 };
300 static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
301 static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
302 static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
303 static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
304 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
305 static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
306 static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
Devin Heitmuellerff697862009-07-12 18:44:19 -0300307 static u8 tuner_go[] = { TUNER_GO, 0x01};
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300308
309 mt352_write(fe, clock_config, sizeof(clock_config));
310 udelay(200);
311 mt352_write(fe, reset, sizeof(reset));
312 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
313 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
314 mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
315 mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
316 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
317 mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
318 mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
319 mt352_write(fe, tuner_go, sizeof(tuner_go));
320 return 0;
321}
322
323static struct mt352_config terratec_xs_mt352_cfg = {
324 .demod_address = (0x1e >> 1),
325 .no_tuner = 1,
326 .if2 = 45600,
327 .demod_init = mt352_terratec_xs_init,
328};
329
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300330static struct tda10023_config em28xx_tda10023_config = {
331 .demod_address = 0x0c,
332 .invert = 1,
333};
334
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300335/* ------------------------------------------------------------------ */
336
337static int attach_xc3028(u8 addr, struct em28xx *dev)
338{
339 struct dvb_frontend *fe;
Mauro Carvalho Chehab3ca9c092008-04-17 21:37:53 -0300340 struct xc2028_config cfg;
341
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300342 memset(&cfg, 0, sizeof(cfg));
Mauro Carvalho Chehab3ca9c092008-04-17 21:37:53 -0300343 cfg.i2c_adap = &dev->i2c_adap;
344 cfg.i2c_addr = addr;
Mauro Carvalho Chehab3ca9c092008-04-17 21:37:53 -0300345
Aidan Thornton3421b772008-04-17 21:40:36 -0300346 if (!dev->dvb->frontend) {
Filipe Rosset480be182009-11-04 15:32:37 -0300347 em28xx_errdev("/2: dvb frontend not attached. "
348 "Can't attach xc3028\n");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300349 return -EINVAL;
350 }
351
Aidan Thornton3421b772008-04-17 21:40:36 -0300352 fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300353 if (!fe) {
Filipe Rosset480be182009-11-04 15:32:37 -0300354 em28xx_errdev("/2: xc3028 attach failed\n");
Aidan Thornton3421b772008-04-17 21:40:36 -0300355 dvb_frontend_detach(dev->dvb->frontend);
Aidan Thornton3421b772008-04-17 21:40:36 -0300356 dev->dvb->frontend = NULL;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300357 return -EINVAL;
358 }
359
Filipe Rosset480be182009-11-04 15:32:37 -0300360 em28xx_info("%s/2: xc3028 attached\n", dev->name);
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300361
362 return 0;
363}
364
Aidan Thornton3421b772008-04-17 21:40:36 -0300365/* ------------------------------------------------------------------ */
366
Hans Verkuild45b9b82008-09-04 03:33:43 -0300367static int register_dvb(struct em28xx_dvb *dvb,
Aidan Thornton3421b772008-04-17 21:40:36 -0300368 struct module *module,
369 struct em28xx *dev,
370 struct device *device)
371{
372 int result;
373
374 mutex_init(&dvb->lock);
375
376 /* register adapter */
377 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
378 adapter_nr);
379 if (result < 0) {
380 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
381 dev->name, result);
382 goto fail_adapter;
383 }
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300384
385 /* Ensure all frontends negotiate bus access */
386 dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
387
Aidan Thornton3421b772008-04-17 21:40:36 -0300388 dvb->adapter.priv = dev;
389
390 /* register frontend */
391 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
392 if (result < 0) {
393 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
394 dev->name, result);
395 goto fail_frontend;
396 }
397
398 /* register demux stuff */
399 dvb->demux.dmx.capabilities =
400 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
401 DMX_MEMORY_BASED_FILTERING;
402 dvb->demux.priv = dvb;
403 dvb->demux.filternum = 256;
404 dvb->demux.feednum = 256;
405 dvb->demux.start_feed = start_feed;
406 dvb->demux.stop_feed = stop_feed;
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300407
Aidan Thornton3421b772008-04-17 21:40:36 -0300408 result = dvb_dmx_init(&dvb->demux);
409 if (result < 0) {
410 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
411 dev->name, result);
412 goto fail_dmx;
413 }
414
415 dvb->dmxdev.filternum = 256;
416 dvb->dmxdev.demux = &dvb->demux.dmx;
417 dvb->dmxdev.capabilities = 0;
418 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
419 if (result < 0) {
420 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
421 dev->name, result);
422 goto fail_dmxdev;
423 }
424
425 dvb->fe_hw.source = DMX_FRONTEND_0;
426 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
427 if (result < 0) {
428 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
429 dev->name, result);
430 goto fail_fe_hw;
431 }
432
433 dvb->fe_mem.source = DMX_MEMORY_FE;
434 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
435 if (result < 0) {
436 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
437 dev->name, result);
438 goto fail_fe_mem;
439 }
440
441 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
442 if (result < 0) {
443 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
444 dev->name, result);
445 goto fail_fe_conn;
446 }
447
448 /* register network adapter */
449 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
450 return 0;
451
452fail_fe_conn:
453 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
454fail_fe_mem:
455 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
456fail_fe_hw:
457 dvb_dmxdev_release(&dvb->dmxdev);
458fail_dmxdev:
459 dvb_dmx_release(&dvb->demux);
460fail_dmx:
461 dvb_unregister_frontend(dvb->frontend);
462fail_frontend:
463 dvb_frontend_detach(dvb->frontend);
464 dvb_unregister_adapter(&dvb->adapter);
465fail_adapter:
466 return result;
467}
468
469static void unregister_dvb(struct em28xx_dvb *dvb)
470{
471 dvb_net_release(&dvb->net);
472 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
473 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
474 dvb_dmxdev_release(&dvb->dmxdev);
475 dvb_dmx_release(&dvb->demux);
476 dvb_unregister_frontend(dvb->frontend);
477 dvb_frontend_detach(dvb->frontend);
478 dvb_unregister_adapter(&dvb->adapter);
479}
480
481
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300482static int dvb_init(struct em28xx *dev)
483{
Aidan Thornton3421b772008-04-17 21:40:36 -0300484 int result = 0;
485 struct em28xx_dvb *dvb;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300486
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300487 if (!dev->board.has_dvb) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300488 /* This device does not support the extension */
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300489 printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300490 return 0;
491 }
492
Aidan Thornton3421b772008-04-17 21:40:36 -0300493 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300494
495 if (dvb == NULL) {
Filipe Rosset480be182009-11-04 15:32:37 -0300496 em28xx_info("em28xx_dvb: memory allocation failed\n");
Aidan Thornton3421b772008-04-17 21:40:36 -0300497 return -ENOMEM;
498 }
499 dev->dvb = dvb;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300500
Mauro Carvalho Chehab50133182010-04-07 17:07:58 -0300501 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300502 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300503 /* init frontend */
504 switch (dev->model) {
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300505 case EM2874_LEADERSHIP_ISDBT:
506 dvb->frontend = dvb_attach(s921_attach,
507 &sharp_isdbt, &dev->i2c_adap);
508
509 if (!dvb->frontend) {
510 result = -EINVAL;
511 goto out_free;
512 }
513
514 break;
Douglas Schilling Landgraff89bc322008-12-01 21:01:04 -0300515 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
Mauro Carvalho Chehab10ac6602008-07-27 14:58:58 -0300516 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
Devin Heitmueller4fd305b2008-06-04 13:43:46 -0300517 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
Devin Heitmuellere14b3652008-07-26 11:04:33 -0300518 case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
Aidan Thornton3421b772008-04-17 21:40:36 -0300519 dvb->frontend = dvb_attach(lgdt330x_attach,
520 &em2880_lgdt3303_dev,
521 &dev->i2c_adap);
522 if (attach_xc3028(0x61, dev) < 0) {
523 result = -EINVAL;
524 goto out_free;
525 }
Mauro Carvalho Chehab227ad4a2008-04-17 21:37:40 -0300526 break;
Thierry MERLE46510b52008-10-11 16:56:13 -0300527 case EM2880_BOARD_KWORLD_DVB_310U:
Aidan Thornton3421b772008-04-17 21:40:36 -0300528 dvb->frontend = dvb_attach(zl10353_attach,
529 &em28xx_zl10353_with_xc3028,
530 &dev->i2c_adap);
531 if (attach_xc3028(0x61, dev) < 0) {
532 result = -EINVAL;
533 goto out_free;
534 }
Aidan Thornton7e6388a2008-04-17 21:40:03 -0300535 break;
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300536 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
Uroš Vamplec994d02009-09-30 22:53:37 -0300537 case EM2882_BOARD_TERRATEC_HYBRID_XS:
Devin Heitmueller01a5fd62009-08-07 09:25:06 -0300538 case EM2880_BOARD_EMPIRE_DUAL_TV:
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300539 dvb->frontend = dvb_attach(zl10353_attach,
540 &em28xx_zl10353_xc3028_no_i2c_gate,
541 &dev->i2c_adap);
542 if (attach_xc3028(0x61, dev) < 0) {
543 result = -EINVAL;
544 goto out_free;
545 }
546 break;
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300547 case EM2880_BOARD_TERRATEC_HYBRID_XS:
Catimimi65638012010-02-18 18:06:32 -0300548 case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
Devin Heitmuellerd5b3ba92009-07-08 21:51:35 -0300549 case EM2881_BOARD_PINNACLE_HYBRID_PRO:
Andrea.Amorosi76@gmail.com7ca7ef62010-02-09 17:53:38 -0300550 case EM2882_BOARD_DIKOM_DK300:
Antonio Larrosa811fab62010-03-04 22:19:48 -0300551 case EM2882_BOARD_KWORLD_VS_DVBT:
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300552 dvb->frontend = dvb_attach(zl10353_attach,
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300553 &em28xx_zl10353_xc3028_no_i2c_gate,
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300554 &dev->i2c_adap);
555 if (dvb->frontend == NULL) {
556 /* This board could have either a zl10353 or a mt352.
557 If the chip id isn't for zl10353, try mt352 */
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300558 dvb->frontend = dvb_attach(mt352_attach,
559 &terratec_xs_mt352_cfg,
560 &dev->i2c_adap);
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300561 }
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300562
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300563 if (attach_xc3028(0x61, dev) < 0) {
564 result = -EINVAL;
565 goto out_free;
566 }
567 break;
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300568 case EM2883_BOARD_KWORLD_HYBRID_330U:
Devin Heitmueller19859222009-06-19 00:33:54 -0300569 case EM2882_BOARD_EVGA_INDTUBE:
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300570 dvb->frontend = dvb_attach(s5h1409_attach,
571 &em28xx_s5h1409_with_xc3028,
572 &dev->i2c_adap);
573 if (attach_xc3028(0x61, dev) < 0) {
574 result = -EINVAL;
575 goto out_free;
576 }
577 break;
Franklin Mengd7de5d82009-06-06 17:05:02 -0300578 case EM2882_BOARD_KWORLD_ATSC_315U:
579 dvb->frontend = dvb_attach(lgdt330x_attach,
580 &em2880_lgdt3303_dev,
581 &dev->i2c_adap);
582 if (dvb->frontend != NULL) {
583 if (!dvb_attach(simple_tuner_attach, dvb->frontend,
584 &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
585 result = -EINVAL;
586 goto out_free;
587 }
588 }
589 break;
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300590 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
591#ifdef EM28XX_DRX397XD_SUPPORT
592 /* We don't have the config structure properly populated, so
593 this is commented out for now */
594 dvb->frontend = dvb_attach(drx397xD_attach,
595 &em28xx_drx397xD_with_xc3028,
596 &dev->i2c_adap);
597 if (attach_xc3028(0x61, dev) < 0) {
598 result = -EINVAL;
599 goto out_free;
600 }
601 break;
602#endif
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300603 case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
604 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
605 dvb->frontend = dvb_attach(tda10023_attach,
606 &em28xx_tda10023_config,
607 &dev->i2c_adap, 0x48);
608 if (dvb->frontend) {
609 if (!dvb_attach(simple_tuner_attach, dvb->frontend,
610 &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
611 result = -EINVAL;
612 goto out_free;
613 }
614 }
615 break;
Jarod Wilson7e48b302010-03-07 17:55:43 -0300616 case EM2870_BOARD_KWORLD_A340:
617 dvb->frontend = dvb_attach(lgdt3305_attach,
618 &em2870_lgdt3304_dev,
619 &dev->i2c_adap);
620 if (dvb->frontend != NULL)
621 dvb_attach(tda18271_attach, dvb->frontend, 0x60,
622 &dev->i2c_adap, &kworld_a340_config);
623 break;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300624 default:
Filipe Rosset480be182009-11-04 15:32:37 -0300625 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
626 " isn't supported yet\n");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300627 break;
628 }
Aidan Thornton3421b772008-04-17 21:40:36 -0300629 if (NULL == dvb->frontend) {
Filipe Rosset480be182009-11-04 15:32:37 -0300630 em28xx_errdev("/2: frontend initialization failed\n");
Aidan Thornton3421b772008-04-17 21:40:36 -0300631 result = -EINVAL;
632 goto out_free;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300633 }
Michael Krufkyd7cba042008-09-12 13:31:45 -0300634 /* define general-purpose callback pointer */
635 dvb->frontend->callback = em28xx_tuner_callback;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300636
637 /* register everything */
Aidan Thornton3421b772008-04-17 21:40:36 -0300638 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
639
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300640 if (result < 0)
Aidan Thornton3421b772008-04-17 21:40:36 -0300641 goto out_free;
Aidan Thornton3421b772008-04-17 21:40:36 -0300642
Filipe Rosset480be182009-11-04 15:32:37 -0300643 em28xx_info("Successfully loaded em28xx-dvb\n");
Mauro Carvalho Chehab50133182010-04-07 17:07:58 -0300644ret:
645 em28xx_set_mode(dev, EM28XX_SUSPEND);
646 mutex_unlock(&dev->lock);
647 return result;
Aidan Thornton3421b772008-04-17 21:40:36 -0300648
649out_free:
650 kfree(dvb);
651 dev->dvb = NULL;
Mauro Carvalho Chehab50133182010-04-07 17:07:58 -0300652 goto ret;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300653}
654
655static int dvb_fini(struct em28xx *dev)
656{
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300657 if (!dev->board.has_dvb) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300658 /* This device does not support the extension */
659 return 0;
660 }
661
Aidan Thornton3421b772008-04-17 21:40:36 -0300662 if (dev->dvb) {
663 unregister_dvb(dev->dvb);
Francesco Lavra19f48cb2009-12-31 08:47:11 -0300664 kfree(dev->dvb);
Aidan Thornton3421b772008-04-17 21:40:36 -0300665 dev->dvb = NULL;
666 }
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300667
668 return 0;
669}
670
671static struct em28xx_ops dvb_ops = {
672 .id = EM28XX_DVB,
673 .name = "Em28xx dvb Extension",
674 .init = dvb_init,
675 .fini = dvb_fini,
676};
677
678static int __init em28xx_dvb_register(void)
679{
680 return em28xx_register_extension(&dvb_ops);
681}
682
683static void __exit em28xx_dvb_unregister(void)
684{
685 em28xx_unregister_extension(&dvb_ops);
686}
687
688module_init(em28xx_dvb_register);
689module_exit(em28xx_dvb_unregister);