blob: b55e7e0bc2450fdf01c5f940b095b961648de27f [file] [log] [blame]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001/*
2 DVB device driver for cx231xx
3
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
Sri Deevib9255172009-03-04 17:49:01 -03005 Based on em28xx driver
Sri Deevie0d3baf2009-03-03 14:37:50 -03006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Sri Deevie0d3baf2009-03-03 14:37:50 -030024#include <linux/usb.h>
25
26#include "cx231xx.h"
27#include <media/v4l2-common.h>
28#include <media/videobuf-vmalloc.h>
29
30#include "xc5000.h"
31#include "dvb_dummy_fe.h"
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030032#include "s5h1432.h"
33#include "tda18271.h"
34#include "s5h1411.h"
Michael Krufky1a50fdd2010-07-06 18:23:53 -030035#include "lgdt3305.h"
Mauro Carvalho Chehabede676c2010-09-27 23:44:27 -030036#include "mb86a20s.h"
Sri Deevie0d3baf2009-03-03 14:37:50 -030037
Sri Deevie0d3baf2009-03-03 14:37:50 -030038MODULE_DESCRIPTION("driver for cx231xx based DVB cards");
39MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
40MODULE_LICENSE("GPL");
41
42static unsigned int debug;
43module_param(debug, int, 0644);
44MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
45
46DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
47
48#define dprintk(level, fmt, arg...) do { \
49if (debug >= level) \
50 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
51} while (0)
52
53#define CX231XX_DVB_NUM_BUFS 5
54#define CX231XX_DVB_MAX_PACKETSIZE 564
55#define CX231XX_DVB_MAX_PACKETS 64
56
57struct cx231xx_dvb {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030058 struct dvb_frontend *frontend;
Sri Deevie0d3baf2009-03-03 14:37:50 -030059
60 /* feed count management */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030061 struct mutex lock;
62 int nfeeds;
Sri Deevie0d3baf2009-03-03 14:37:50 -030063
64 /* general boilerplate stuff */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030065 struct dvb_adapter adapter;
66 struct dvb_demux demux;
67 struct dmxdev dmxdev;
68 struct dmx_frontend fe_hw;
69 struct dmx_frontend fe_mem;
70 struct dvb_net net;
Sri Deevie0d3baf2009-03-03 14:37:50 -030071};
72
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030073static struct s5h1432_config dvico_s5h1432_config = {
74 .output_mode = S5H1432_SERIAL_OUTPUT,
75 .gpio = S5H1432_GPIO_ON,
76 .qam_if = S5H1432_IF_4000,
77 .vsb_if = S5H1432_IF_4000,
78 .inversion = S5H1432_INVERSION_OFF,
79 .status_mode = S5H1432_DEMODLOCKING,
80 .mpeg_timing = S5H1432_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
81};
82
83static struct tda18271_std_map cnxt_rde253s_tda18271_std_map = {
84 .dvbt_6 = { .if_freq = 4000, .agc_mode = 3, .std = 4,
85 .if_lvl = 1, .rfagc_top = 0x37, },
86 .dvbt_7 = { .if_freq = 4000, .agc_mode = 3, .std = 5,
87 .if_lvl = 1, .rfagc_top = 0x37, },
88 .dvbt_8 = { .if_freq = 4000, .agc_mode = 3, .std = 6,
89 .if_lvl = 1, .rfagc_top = 0x37, },
90};
91
Mauro Carvalho Chehabede676c2010-09-27 23:44:27 -030092static struct tda18271_std_map mb86a20s_tda18271_config = {
93 .dvbt_6 = { .if_freq = 3300, .agc_mode = 3, .std = 4,
94 .if_lvl = 7, .rfagc_top = 0x37, },
95};
96
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030097static struct tda18271_config cnxt_rde253s_tunerconfig = {
98 .std_map = &cnxt_rde253s_tda18271_std_map,
99 .gate = TDA18271_GATE_ANALOG,
100};
101
102static struct s5h1411_config tda18271_s5h1411_config = {
103 .output_mode = S5H1411_SERIAL_OUTPUT,
104 .gpio = S5H1411_GPIO_OFF,
105 .vsb_if = S5H1411_IF_3250,
106 .qam_if = S5H1411_IF_4000,
107 .inversion = S5H1411_INVERSION_ON,
108 .status_mode = S5H1411_DEMODLOCKING,
109 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
110};
111static struct s5h1411_config xc5000_s5h1411_config = {
112 .output_mode = S5H1411_SERIAL_OUTPUT,
113 .gpio = S5H1411_GPIO_OFF,
114 .vsb_if = S5H1411_IF_3250,
115 .qam_if = S5H1411_IF_3250,
116 .inversion = S5H1411_INVERSION_OFF,
117 .status_mode = S5H1411_DEMODLOCKING,
118 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
119};
Michael Krufky1a50fdd2010-07-06 18:23:53 -0300120
121static struct lgdt3305_config hcw_lgdt3305_config = {
122 .i2c_addr = 0x0e,
123 .mpeg_mode = LGDT3305_MPEG_SERIAL,
124 .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
125 .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
126 .deny_i2c_rptr = 1,
127 .spectral_inversion = 1,
128 .qam_if_khz = 4000,
129 .vsb_if_khz = 3250,
130};
131
132static struct tda18271_std_map hauppauge_tda18271_std_map = {
133 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 4,
134 .if_lvl = 1, .rfagc_top = 0x58, },
135 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 5,
136 .if_lvl = 1, .rfagc_top = 0x58, },
137};
138
139static struct tda18271_config hcw_tda18271_config = {
140 .std_map = &hauppauge_tda18271_std_map,
141 .gate = TDA18271_GATE_DIGITAL,
142};
143
Mauro Carvalho Chehabede676c2010-09-27 23:44:27 -0300144static const struct mb86a20s_config pv_mb86a20s_config = {
145 .demod_address = 0x10,
146};
147
148static struct tda18271_config pv_tda18271_config = {
149 .std_map = &mb86a20s_tda18271_config,
150 .gate = TDA18271_GATE_DIGITAL,
151 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
152};
153
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300154static inline void print_err_status(struct cx231xx *dev, int packet, int status)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300155{
156 char *errmsg = "Unknown";
157
158 switch (status) {
159 case -ENOENT:
160 errmsg = "unlinked synchronuously";
161 break;
162 case -ECONNRESET:
163 errmsg = "unlinked asynchronuously";
164 break;
165 case -ENOSR:
166 errmsg = "Buffer error (overrun)";
167 break;
168 case -EPIPE:
169 errmsg = "Stalled (device not responding)";
170 break;
171 case -EOVERFLOW:
172 errmsg = "Babble (bad cable?)";
173 break;
174 case -EPROTO:
175 errmsg = "Bit-stuff error (bad cable?)";
176 break;
177 case -EILSEQ:
178 errmsg = "CRC/Timeout (could be anything)";
179 break;
180 case -ETIME:
181 errmsg = "Device does not respond";
182 break;
183 }
184 if (packet < 0) {
185 dprintk(1, "URB status %d [%s].\n", status, errmsg);
186 } else {
187 dprintk(1, "URB packet %d, status %d [%s].\n",
188 packet, status, errmsg);
189 }
190}
191
192static inline int dvb_isoc_copy(struct cx231xx *dev, struct urb *urb)
193{
194 int i;
195
196 if (!dev)
197 return 0;
198
199 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
200 return 0;
201
202 if (urb->status < 0) {
203 print_err_status(dev, -1, urb->status);
204 if (urb->status == -ENOENT)
205 return 0;
206 }
207
208 for (i = 0; i < urb->number_of_packets; i++) {
209 int status = urb->iso_frame_desc[i].status;
210
211 if (status < 0) {
212 print_err_status(dev, i, status);
213 if (urb->iso_frame_desc[i].status != -EPROTO)
214 continue;
215 }
216
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300217 dvb_dmx_swfilter(&dev->dvb->demux,
218 urb->transfer_buffer +
219 urb->iso_frame_desc[i].offset,
220 urb->iso_frame_desc[i].actual_length);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300221 }
222
223 return 0;
224}
225
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300226static inline int dvb_bulk_copy(struct cx231xx *dev, struct urb *urb)
227{
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300228 if (!dev)
229 return 0;
230
231 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
232 return 0;
233
234 if (urb->status < 0) {
235 print_err_status(dev, -1, urb->status);
236 if (urb->status == -ENOENT)
237 return 0;
238 }
239
240 /* Feed the transport payload into the kernel demux */
241 dvb_dmx_swfilter(&dev->dvb->demux,
242 urb->transfer_buffer, urb->actual_length);
243
244 return 0;
245}
246
Sri Deevie0d3baf2009-03-03 14:37:50 -0300247static int start_streaming(struct cx231xx_dvb *dvb)
248{
249 int rc;
250 struct cx231xx *dev = dvb->adapter.priv;
251
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300252 if (dev->USE_ISO) {
253 cx231xx_info("DVB transfer mode is ISO.\n");
Mauro Carvalho Chehaba6f6fb92010-09-26 20:38:24 -0300254 mutex_lock(&dev->i2c_lock);
255 cx231xx_enable_i2c_port_3(dev, false);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300256 cx231xx_set_alt_setting(dev, INDEX_TS1, 4);
Mauro Carvalho Chehaba6f6fb92010-09-26 20:38:24 -0300257 cx231xx_enable_i2c_port_3(dev, true);
258 mutex_unlock(&dev->i2c_lock);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300259 rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
260 if (rc < 0)
261 return rc;
262 dev->mode_tv = 1;
263 return cx231xx_init_isoc(dev, CX231XX_DVB_MAX_PACKETS,
264 CX231XX_DVB_NUM_BUFS,
265 dev->ts1_mode.max_pkt_size,
266 dvb_isoc_copy);
267 } else {
268 cx231xx_info("DVB transfer mode is BULK.\n");
269 cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
270 rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
271 if (rc < 0)
272 return rc;
273 dev->mode_tv = 1;
274 return cx231xx_init_bulk(dev, CX231XX_DVB_MAX_PACKETS,
275 CX231XX_DVB_NUM_BUFS,
276 dev->ts1_mode.max_pkt_size,
277 dvb_bulk_copy);
278 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300279
Sri Deevie0d3baf2009-03-03 14:37:50 -0300280}
281
282static int stop_streaming(struct cx231xx_dvb *dvb)
283{
284 struct cx231xx *dev = dvb->adapter.priv;
285
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300286 if (dev->USE_ISO)
287 cx231xx_uninit_isoc(dev);
288 else
289 cx231xx_uninit_bulk(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300290
291 cx231xx_set_mode(dev, CX231XX_SUSPEND);
292
293 return 0;
294}
295
296static int start_feed(struct dvb_demux_feed *feed)
297{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300298 struct dvb_demux *demux = feed->demux;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300299 struct cx231xx_dvb *dvb = demux->priv;
300 int rc, ret;
301
302 if (!demux->dmx.frontend)
303 return -EINVAL;
304
305 mutex_lock(&dvb->lock);
306 dvb->nfeeds++;
307 rc = dvb->nfeeds;
308
309 if (dvb->nfeeds == 1) {
310 ret = start_streaming(dvb);
311 if (ret < 0)
312 rc = ret;
313 }
314
315 mutex_unlock(&dvb->lock);
316 return rc;
317}
318
319static int stop_feed(struct dvb_demux_feed *feed)
320{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300321 struct dvb_demux *demux = feed->demux;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300322 struct cx231xx_dvb *dvb = demux->priv;
323 int err = 0;
324
325 mutex_lock(&dvb->lock);
326 dvb->nfeeds--;
327
328 if (0 == dvb->nfeeds)
329 err = stop_streaming(dvb);
330
331 mutex_unlock(&dvb->lock);
332 return err;
333}
334
Sri Deevie0d3baf2009-03-03 14:37:50 -0300335/* ------------------------------------------------------------------ */
336static int cx231xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
337{
338 struct cx231xx *dev = fe->dvb->priv;
339
340 if (acquire)
341 return cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
342 else
343 return cx231xx_set_mode(dev, CX231XX_SUSPEND);
344}
345
346/* ------------------------------------------------------------------ */
347
Sri Deevie0d3baf2009-03-03 14:37:50 -0300348static struct xc5000_config cnxt_rde250_tunerconfig = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300349 .i2c_address = 0x61,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300350 .if_khz = 4000,
351};
352static struct xc5000_config cnxt_rdu250_tunerconfig = {
353 .i2c_address = 0x61,
354 .if_khz = 3250,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300355};
356
Sri Deevie0d3baf2009-03-03 14:37:50 -0300357/* ------------------------------------------------------------------ */
358#if 0
359static int attach_xc5000(u8 addr, struct cx231xx *dev)
360{
361
362 struct dvb_frontend *fe;
363 struct xc5000_config cfg;
364
365 memset(&cfg, 0, sizeof(cfg));
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300366 cfg.i2c_adap = &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300367 cfg.i2c_addr = addr;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300368
369 if (!dev->dvb->frontend) {
370 printk(KERN_ERR "%s/2: dvb frontend not attached. "
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300371 "Can't attach xc5000\n", dev->name);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300372 return -EINVAL;
373 }
374
375 fe = dvb_attach(xc5000_attach, dev->dvb->frontend, &cfg);
376 if (!fe) {
377 printk(KERN_ERR "%s/2: xc5000 attach failed\n", dev->name);
378 dvb_frontend_detach(dev->dvb->frontend);
379 dev->dvb->frontend = NULL;
380 return -EINVAL;
381 }
382
383 printk(KERN_INFO "%s/2: xc5000 attached\n", dev->name);
384
385 return 0;
386}
387#endif
388
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300389int cx231xx_set_analog_freq(struct cx231xx *dev, u32 freq)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300390{
391 int status = 0;
392
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300393 if ((dev->dvb != NULL) && (dev->dvb->frontend != NULL)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300394
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300395 struct dvb_tuner_ops *dops = &dev->dvb->frontend->ops.tuner_ops;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300396
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300397 if (dops->set_analog_params != NULL) {
398 struct analog_parameters params;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300399
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300400 params.frequency = freq;
401 params.std = dev->norm;
402 params.mode = 0; /* 0- Air; 1 - cable */
403 /*params.audmode = ; */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300404
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300405 /* Set the analog parameters to set the frequency */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300406 dops->set_analog_params(dev->dvb->frontend, &params);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300407 }
408
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300409 }
410
Sri Deevie0d3baf2009-03-03 14:37:50 -0300411 return status;
412}
413
414int cx231xx_reset_analog_tuner(struct cx231xx *dev)
415{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300416 int status = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300417
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300418 if ((dev->dvb != NULL) && (dev->dvb->frontend != NULL)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300419
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300420 struct dvb_tuner_ops *dops = &dev->dvb->frontend->ops.tuner_ops;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300421
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300422 if (dops->init != NULL && !dev->xc_fw_load_done) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300423
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300424 cx231xx_info("Reloading firmware for XC5000\n");
425 status = dops->init(dev->dvb->frontend);
426 if (status == 0) {
427 dev->xc_fw_load_done = 1;
428 cx231xx_info
429 ("XC5000 firmware download completed\n");
430 } else {
431 dev->xc_fw_load_done = 0;
432 cx231xx_info
433 ("XC5000 firmware download failed !!!\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300434 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300435 }
436
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300437 }
438
Sri Deevie0d3baf2009-03-03 14:37:50 -0300439 return status;
440}
441
Sri Deevie0d3baf2009-03-03 14:37:50 -0300442/* ------------------------------------------------------------------ */
443
444static int register_dvb(struct cx231xx_dvb *dvb,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300445 struct module *module,
446 struct cx231xx *dev, struct device *device)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300447{
448 int result;
449
450 mutex_init(&dvb->lock);
451
452 /* register adapter */
453 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
454 adapter_nr);
455 if (result < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300456 printk(KERN_WARNING
457 "%s: dvb_register_adapter failed (errno = %d)\n",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300458 dev->name, result);
459 goto fail_adapter;
460 }
461
462 /* Ensure all frontends negotiate bus access */
463 dvb->frontend->ops.ts_bus_ctrl = cx231xx_dvb_bus_ctrl;
464
465 dvb->adapter.priv = dev;
466
467 /* register frontend */
468 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
469 if (result < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300470 printk(KERN_WARNING
471 "%s: dvb_register_frontend failed (errno = %d)\n",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300472 dev->name, result);
473 goto fail_frontend;
474 }
475
476 /* register demux stuff */
477 dvb->demux.dmx.capabilities =
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300478 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
479 DMX_MEMORY_BASED_FILTERING;
480 dvb->demux.priv = dvb;
481 dvb->demux.filternum = 256;
482 dvb->demux.feednum = 256;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300483 dvb->demux.start_feed = start_feed;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300484 dvb->demux.stop_feed = stop_feed;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300485
486 result = dvb_dmx_init(&dvb->demux);
487 if (result < 0) {
488 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
489 dev->name, result);
490 goto fail_dmx;
491 }
492
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300493 dvb->dmxdev.filternum = 256;
494 dvb->dmxdev.demux = &dvb->demux.dmx;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300495 dvb->dmxdev.capabilities = 0;
496 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
497 if (result < 0) {
498 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
499 dev->name, result);
500 goto fail_dmxdev;
501 }
502
503 dvb->fe_hw.source = DMX_FRONTEND_0;
504 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
505 if (result < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300506 printk(KERN_WARNING
507 "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300508 dev->name, result);
509 goto fail_fe_hw;
510 }
511
512 dvb->fe_mem.source = DMX_MEMORY_FE;
513 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
514 if (result < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300515 printk(KERN_WARNING
516 "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300517 dev->name, result);
518 goto fail_fe_mem;
519 }
520
521 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
522 if (result < 0) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300523 printk(KERN_WARNING
524 "%s: connect_frontend failed (errno = %d)\n", dev->name,
525 result);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300526 goto fail_fe_conn;
527 }
528
529 /* register network adapter */
530 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
531 return 0;
532
Sri Deevib9255172009-03-04 17:49:01 -0300533fail_fe_conn:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300534 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
Sri Deevib9255172009-03-04 17:49:01 -0300535fail_fe_mem:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300536 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
Sri Deevib9255172009-03-04 17:49:01 -0300537fail_fe_hw:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300538 dvb_dmxdev_release(&dvb->dmxdev);
Sri Deevib9255172009-03-04 17:49:01 -0300539fail_dmxdev:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300540 dvb_dmx_release(&dvb->demux);
Sri Deevib9255172009-03-04 17:49:01 -0300541fail_dmx:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300542 dvb_unregister_frontend(dvb->frontend);
Sri Deevib9255172009-03-04 17:49:01 -0300543fail_frontend:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300544 dvb_frontend_detach(dvb->frontend);
545 dvb_unregister_adapter(&dvb->adapter);
Sri Deevib9255172009-03-04 17:49:01 -0300546fail_adapter:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300547 return result;
548}
549
550static void unregister_dvb(struct cx231xx_dvb *dvb)
551{
552 dvb_net_release(&dvb->net);
553 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
554 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
555 dvb_dmxdev_release(&dvb->dmxdev);
556 dvb_dmx_release(&dvb->demux);
557 dvb_unregister_frontend(dvb->frontend);
558 dvb_frontend_detach(dvb->frontend);
559 dvb_unregister_adapter(&dvb->adapter);
560}
561
Sri Deevie0d3baf2009-03-03 14:37:50 -0300562static int dvb_init(struct cx231xx *dev)
563{
564 int result = 0;
565 struct cx231xx_dvb *dvb;
566
567 if (!dev->board.has_dvb) {
568 /* This device does not support the extension */
569 return 0;
570 }
571
572 dvb = kzalloc(sizeof(struct cx231xx_dvb), GFP_KERNEL);
573
574 if (dvb == NULL) {
575 printk(KERN_INFO "cx231xx_dvb: memory allocation failed\n");
576 return -ENOMEM;
577 }
578 dev->dvb = dvb;
579 dev->cx231xx_set_analog_freq = cx231xx_set_analog_freq;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300580 dev->cx231xx_reset_analog_tuner = cx231xx_reset_analog_tuner;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300581
Devin Heitmueller761f6cf2010-07-06 19:24:19 -0300582 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300583 cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300584 cx231xx_demod_reset(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300585 /* init frontend */
586 switch (dev->model) {
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300587 case CX231XX_BOARD_CNXT_CARRAERA:
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300588 case CX231XX_BOARD_CNXT_RDE_250:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300589
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300590 dev->dvb->frontend = dvb_attach(s5h1432_attach,
591 &dvico_s5h1432_config,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300592 &dev->i2c_bus[dev->board.demod_i2c_master].i2c_adap);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300593
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300594 if (dev->dvb->frontend == NULL) {
595 printk(DRIVER_NAME
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300596 ": Failed to attach s5h1432 front end\n");
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300597 result = -EINVAL;
598 goto out_free;
599 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300600
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300601 /* define general-purpose callback pointer */
602 dvb->frontend->callback = cx231xx_tuner_callback;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300603
Márton Némethd5abcc72010-01-16 13:21:59 -0300604 if (!dvb_attach(xc5000_attach, dev->dvb->frontend,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300605 &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
Márton Némethd5abcc72010-01-16 13:21:59 -0300606 &cnxt_rde250_tunerconfig)) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300607 result = -EINVAL;
608 goto out_free;
609 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300610
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300611 break;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300612 case CX231XX_BOARD_CNXT_SHELBY:
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300613 case CX231XX_BOARD_CNXT_RDU_250:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300614
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300615 dev->dvb->frontend = dvb_attach(s5h1411_attach,
616 &xc5000_s5h1411_config,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300617 &dev->i2c_bus[dev->board.demod_i2c_master].i2c_adap);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300618
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300619 if (dev->dvb->frontend == NULL) {
620 printk(DRIVER_NAME
621 ": Failed to attach dummy front end\n");
622 result = -EINVAL;
623 goto out_free;
624 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300625
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300626 /* define general-purpose callback pointer */
627 dvb->frontend->callback = cx231xx_tuner_callback;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300628
Márton Némethd5abcc72010-01-16 13:21:59 -0300629 if (!dvb_attach(xc5000_attach, dev->dvb->frontend,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300630 &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300631 &cnxt_rdu250_tunerconfig)) {
632 result = -EINVAL;
633 goto out_free;
634 }
635 break;
636 case CX231XX_BOARD_CNXT_RDE_253S:
637
638 dev->dvb->frontend = dvb_attach(s5h1432_attach,
639 &dvico_s5h1432_config,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300640 &dev->i2c_bus[dev->board.demod_i2c_master].i2c_adap);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300641
642 if (dev->dvb->frontend == NULL) {
643 printk(DRIVER_NAME
644 ": Failed to attach s5h1432 front end\n");
645 result = -EINVAL;
646 goto out_free;
647 }
648
649 /* define general-purpose callback pointer */
650 dvb->frontend->callback = cx231xx_tuner_callback;
651
652 if (!dvb_attach(tda18271_attach, dev->dvb->frontend,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300653 0x60, &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300654 &cnxt_rde253s_tunerconfig)) {
655 result = -EINVAL;
656 goto out_free;
657 }
658 break;
659 case CX231XX_BOARD_CNXT_RDU_253S:
660
661 dev->dvb->frontend = dvb_attach(s5h1411_attach,
662 &tda18271_s5h1411_config,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300663 &dev->i2c_bus[dev->board.demod_i2c_master].i2c_adap);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300664
665 if (dev->dvb->frontend == NULL) {
666 printk(DRIVER_NAME
667 ": Failed to attach dummy front end\n");
668 result = -EINVAL;
669 goto out_free;
670 }
671
672 /* define general-purpose callback pointer */
673 dvb->frontend->callback = cx231xx_tuner_callback;
674
675 if (!dvb_attach(tda18271_attach, dev->dvb->frontend,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300676 0x60, &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300677 &cnxt_rde253s_tunerconfig)) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300678 result = -EINVAL;
679 goto out_free;
680 }
681 break;
Michael Krufky1a50fdd2010-07-06 18:23:53 -0300682 case CX231XX_BOARD_HAUPPAUGE_EXETER:
683
684 printk(KERN_INFO "%s: looking for tuner / demod on i2c bus: %d\n",
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300685 __func__, i2c_adapter_id(&dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap));
Michael Krufky1a50fdd2010-07-06 18:23:53 -0300686
687 dev->dvb->frontend = dvb_attach(lgdt3305_attach,
688 &hcw_lgdt3305_config,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300689 &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap);
Michael Krufky1a50fdd2010-07-06 18:23:53 -0300690
691 if (dev->dvb->frontend == NULL) {
692 printk(DRIVER_NAME
693 ": Failed to attach LG3305 front end\n");
694 result = -EINVAL;
695 goto out_free;
696 }
697
698 /* define general-purpose callback pointer */
699 dvb->frontend->callback = cx231xx_tuner_callback;
700
701 dvb_attach(tda18271_attach, dev->dvb->frontend,
Mauro Carvalho Chehab1a4aa922010-09-26 20:01:51 -0300702 0x60, &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
Michael Krufky1a50fdd2010-07-06 18:23:53 -0300703 &hcw_tda18271_config);
704 break;
705
Mauro Carvalho Chehabede676c2010-09-27 23:44:27 -0300706 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
707
708 printk(KERN_INFO "%s: looking for demod on i2c bus: %d\n",
709 __func__, i2c_adapter_id(&dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap));
710
711 dev->dvb->frontend = dvb_attach(mb86a20s_attach,
712 &pv_mb86a20s_config,
713 &dev->i2c_bus[dev->board.demod_i2c_master].i2c_adap);
714
715 if (dev->dvb->frontend == NULL) {
716 printk(DRIVER_NAME
717 ": Failed to attach mb86a20s demod\n");
718 result = -EINVAL;
719 goto out_free;
720 }
721
722 /* define general-purpose callback pointer */
723 dvb->frontend->callback = cx231xx_tuner_callback;
724
725 dvb_attach(tda18271_attach, dev->dvb->frontend,
726 0x60, &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
727 &pv_tda18271_config);
728 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300729
730 default:
731 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300732 " isn't supported yet\n", dev->name);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300733 break;
734 }
735 if (NULL == dvb->frontend) {
736 printk(KERN_ERR
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300737 "%s/2: frontend initialization failed\n", dev->name);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300738 result = -EINVAL;
739 goto out_free;
740 }
741
Sri Deevie0d3baf2009-03-03 14:37:50 -0300742 /* register everything */
743 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
744
745 if (result < 0)
746 goto out_free;
747
Devin Heitmueller761f6cf2010-07-06 19:24:19 -0300748
Sri Deevie0d3baf2009-03-03 14:37:50 -0300749 printk(KERN_INFO "Successfully loaded cx231xx-dvb\n");
Devin Heitmueller761f6cf2010-07-06 19:24:19 -0300750
751ret:
752 cx231xx_set_mode(dev, CX231XX_SUSPEND);
753 mutex_unlock(&dev->lock);
754 return result;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300755
Sri Deevib9255172009-03-04 17:49:01 -0300756out_free:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300757 kfree(dvb);
758 dev->dvb = NULL;
Devin Heitmueller761f6cf2010-07-06 19:24:19 -0300759 goto ret;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300760}
761
762static int dvb_fini(struct cx231xx *dev)
763{
764 if (!dev->board.has_dvb) {
765 /* This device does not support the extension */
766 return 0;
767 }
768
769 if (dev->dvb) {
770 unregister_dvb(dev->dvb);
771 dev->dvb = NULL;
772 }
773
774 return 0;
775}
776
777static struct cx231xx_ops dvb_ops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300778 .id = CX231XX_DVB,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300779 .name = "Cx231xx dvb Extension",
780 .init = dvb_init,
781 .fini = dvb_fini,
782};
783
784static int __init cx231xx_dvb_register(void)
785{
786 return cx231xx_register_extension(&dvb_ops);
787}
788
789static void __exit cx231xx_dvb_unregister(void)
790{
791 cx231xx_unregister_extension(&dvb_ops);
792}
793
794module_init(cx231xx_dvb_register);
795module_exit(cx231xx_dvb_unregister);