2 DVB device driver for cx231xx
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
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.
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.
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.
22 #include <linux/kernel.h>
23 #include <linux/usb.h>
26 #include <media/v4l2-common.h>
27 #include <media/videobuf-vmalloc.h>
30 #include "dvb_dummy_fe.h"
32 MODULE_DESCRIPTION("driver for cx231xx based DVB cards");
33 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
34 MODULE_LICENSE("GPL");
36 static unsigned int debug
;
37 module_param(debug
, int, 0644);
38 MODULE_PARM_DESC(debug
, "enable debug messages [dvb]");
40 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
42 #define dprintk(level, fmt, arg...) do { \
44 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
47 #define CX231XX_DVB_NUM_BUFS 5
48 #define CX231XX_DVB_MAX_PACKETSIZE 564
49 #define CX231XX_DVB_MAX_PACKETS 64
52 struct dvb_frontend
*frontend
;
54 /* feed count management */
58 /* general boilerplate stuff */
59 struct dvb_adapter adapter
;
60 struct dvb_demux demux
;
62 struct dmx_frontend fe_hw
;
63 struct dmx_frontend fe_mem
;
67 static inline void print_err_status(struct cx231xx
*dev
, int packet
, int status
)
69 char *errmsg
= "Unknown";
73 errmsg
= "unlinked synchronuously";
76 errmsg
= "unlinked asynchronuously";
79 errmsg
= "Buffer error (overrun)";
82 errmsg
= "Stalled (device not responding)";
85 errmsg
= "Babble (bad cable?)";
88 errmsg
= "Bit-stuff error (bad cable?)";
91 errmsg
= "CRC/Timeout (could be anything)";
94 errmsg
= "Device does not respond";
98 dprintk(1, "URB status %d [%s].\n", status
, errmsg
);
100 dprintk(1, "URB packet %d, status %d [%s].\n",
101 packet
, status
, errmsg
);
105 static inline int dvb_isoc_copy(struct cx231xx
*dev
, struct urb
*urb
)
112 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
115 if (urb
->status
< 0) {
116 print_err_status(dev
, -1, urb
->status
);
117 if (urb
->status
== -ENOENT
)
121 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
122 int status
= urb
->iso_frame_desc
[i
].status
;
125 print_err_status(dev
, i
, status
);
126 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
130 dvb_dmx_swfilter(&dev
->dvb
->demux
, urb
->transfer_buffer
+
131 urb
->iso_frame_desc
[i
].offset
,
132 urb
->iso_frame_desc
[i
].actual_length
);
138 static int start_streaming(struct cx231xx_dvb
*dvb
)
141 struct cx231xx
*dev
= dvb
->adapter
.priv
;
143 usb_set_interface(dev
->udev
, 0, 1);
144 rc
= cx231xx_set_mode(dev
, CX231XX_DIGITAL_MODE
);
148 return cx231xx_init_isoc(dev
, CX231XX_DVB_MAX_PACKETS
,
149 CX231XX_DVB_NUM_BUFS
,
150 CX231XX_DVB_MAX_PACKETSIZE
, dvb_isoc_copy
);
153 static int stop_streaming(struct cx231xx_dvb
*dvb
)
155 struct cx231xx
*dev
= dvb
->adapter
.priv
;
157 cx231xx_uninit_isoc(dev
);
159 cx231xx_set_mode(dev
, CX231XX_SUSPEND
);
164 static int start_feed(struct dvb_demux_feed
*feed
)
166 struct dvb_demux
*demux
= feed
->demux
;
167 struct cx231xx_dvb
*dvb
= demux
->priv
;
170 if (!demux
->dmx
.frontend
)
173 mutex_lock(&dvb
->lock
);
177 if (dvb
->nfeeds
== 1) {
178 ret
= start_streaming(dvb
);
183 mutex_unlock(&dvb
->lock
);
187 static int stop_feed(struct dvb_demux_feed
*feed
)
189 struct dvb_demux
*demux
= feed
->demux
;
190 struct cx231xx_dvb
*dvb
= demux
->priv
;
193 mutex_lock(&dvb
->lock
);
196 if (0 == dvb
->nfeeds
)
197 err
= stop_streaming(dvb
);
199 mutex_unlock(&dvb
->lock
);
203 /* ------------------------------------------------------------------ */
204 static int cx231xx_dvb_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
206 struct cx231xx
*dev
= fe
->dvb
->priv
;
209 return cx231xx_set_mode(dev
, CX231XX_DIGITAL_MODE
);
211 return cx231xx_set_mode(dev
, CX231XX_SUSPEND
);
214 /* ------------------------------------------------------------------ */
216 static struct xc5000_config cnxt_rde250_tunerconfig
= {
221 /* ------------------------------------------------------------------ */
223 static int attach_xc5000(u8 addr
, struct cx231xx
*dev
)
226 struct dvb_frontend
*fe
;
227 struct xc5000_config cfg
;
229 memset(&cfg
, 0, sizeof(cfg
));
230 cfg
.i2c_adap
= &dev
->i2c_bus
[1].i2c_adap
;
233 if (!dev
->dvb
->frontend
) {
234 printk(KERN_ERR
"%s/2: dvb frontend not attached. "
235 "Can't attach xc5000\n", dev
->name
);
239 fe
= dvb_attach(xc5000_attach
, dev
->dvb
->frontend
, &cfg
);
241 printk(KERN_ERR
"%s/2: xc5000 attach failed\n", dev
->name
);
242 dvb_frontend_detach(dev
->dvb
->frontend
);
243 dev
->dvb
->frontend
= NULL
;
247 printk(KERN_INFO
"%s/2: xc5000 attached\n", dev
->name
);
253 int cx231xx_set_analog_freq(struct cx231xx
*dev
, u32 freq
)
257 if ((dev
->dvb
!= NULL
) && (dev
->dvb
->frontend
!= NULL
)) {
259 struct dvb_tuner_ops
*dops
= &dev
->dvb
->frontend
->ops
.tuner_ops
;
261 if (dops
->set_analog_params
!= NULL
) {
262 struct analog_parameters params
;
264 params
.frequency
= freq
;
265 params
.std
= dev
->norm
;
266 params
.mode
= 0; /* 0- Air; 1 - cable */
267 /*params.audmode = ; */
269 /* Set the analog parameters to set the frequency */
270 cx231xx_info("Setting Frequency for XC5000\n");
271 dops
->set_analog_params(dev
->dvb
->frontend
, ¶ms
);
279 int cx231xx_reset_analog_tuner(struct cx231xx
*dev
)
283 if ((dev
->dvb
!= NULL
) && (dev
->dvb
->frontend
!= NULL
)) {
285 struct dvb_tuner_ops
*dops
= &dev
->dvb
->frontend
->ops
.tuner_ops
;
287 if (dops
->init
!= NULL
&& !dev
->xc_fw_load_done
) {
289 cx231xx_info("Reloading firmware for XC5000\n");
290 status
= dops
->init(dev
->dvb
->frontend
);
292 dev
->xc_fw_load_done
= 1;
294 ("XC5000 firmware download completed\n");
296 dev
->xc_fw_load_done
= 0;
298 ("XC5000 firmware download failed !!!\n");
307 /* ------------------------------------------------------------------ */
309 static int register_dvb(struct cx231xx_dvb
*dvb
,
310 struct module
*module
,
311 struct cx231xx
*dev
, struct device
*device
)
315 mutex_init(&dvb
->lock
);
317 /* register adapter */
318 result
= dvb_register_adapter(&dvb
->adapter
, dev
->name
, module
, device
,
322 "%s: dvb_register_adapter failed (errno = %d)\n",
327 /* Ensure all frontends negotiate bus access */
328 dvb
->frontend
->ops
.ts_bus_ctrl
= cx231xx_dvb_bus_ctrl
;
330 dvb
->adapter
.priv
= dev
;
332 /* register frontend */
333 result
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
336 "%s: dvb_register_frontend failed (errno = %d)\n",
341 /* register demux stuff */
342 dvb
->demux
.dmx
.capabilities
=
343 DMX_TS_FILTERING
| DMX_SECTION_FILTERING
|
344 DMX_MEMORY_BASED_FILTERING
;
345 dvb
->demux
.priv
= dvb
;
346 dvb
->demux
.filternum
= 256;
347 dvb
->demux
.feednum
= 256;
348 dvb
->demux
.start_feed
= start_feed
;
349 dvb
->demux
.stop_feed
= stop_feed
;
351 result
= dvb_dmx_init(&dvb
->demux
);
353 printk(KERN_WARNING
"%s: dvb_dmx_init failed (errno = %d)\n",
358 dvb
->dmxdev
.filternum
= 256;
359 dvb
->dmxdev
.demux
= &dvb
->demux
.dmx
;
360 dvb
->dmxdev
.capabilities
= 0;
361 result
= dvb_dmxdev_init(&dvb
->dmxdev
, &dvb
->adapter
);
363 printk(KERN_WARNING
"%s: dvb_dmxdev_init failed (errno = %d)\n",
368 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
369 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
372 "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
377 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
378 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
381 "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
386 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
389 "%s: connect_frontend failed (errno = %d)\n", dev
->name
,
394 /* register network adapter */
395 dvb_net_init(&dvb
->adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
399 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
401 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
403 dvb_dmxdev_release(&dvb
->dmxdev
);
405 dvb_dmx_release(&dvb
->demux
);
407 dvb_unregister_frontend(dvb
->frontend
);
409 dvb_frontend_detach(dvb
->frontend
);
410 dvb_unregister_adapter(&dvb
->adapter
);
415 static void unregister_dvb(struct cx231xx_dvb
*dvb
)
417 dvb_net_release(&dvb
->net
);
418 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
419 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
420 dvb_dmxdev_release(&dvb
->dmxdev
);
421 dvb_dmx_release(&dvb
->demux
);
422 dvb_unregister_frontend(dvb
->frontend
);
423 dvb_frontend_detach(dvb
->frontend
);
424 dvb_unregister_adapter(&dvb
->adapter
);
427 static int dvb_init(struct cx231xx
*dev
)
430 struct cx231xx_dvb
*dvb
;
432 if (!dev
->board
.has_dvb
) {
433 /* This device does not support the extension */
437 dvb
= kzalloc(sizeof(struct cx231xx_dvb
), GFP_KERNEL
);
440 printk(KERN_INFO
"cx231xx_dvb: memory allocation failed\n");
444 dev
->cx231xx_set_analog_freq
= cx231xx_set_analog_freq
;
445 dev
->cx231xx_reset_analog_tuner
= cx231xx_reset_analog_tuner
;
447 cx231xx_set_mode(dev
, CX231XX_DIGITAL_MODE
);
449 switch (dev
->model
) {
450 case CX231XX_BOARD_CNXT_RDE_250
:
452 /* dev->dvb->frontend = dvb_attach(s5h1411_attach,
453 &dvico_s5h1411_config,
454 &dev->i2c_bus[1].i2c_adap); */
455 dev
->dvb
->frontend
= dvb_attach(dvb_dummy_fe_ofdm_attach
);
457 if (dev
->dvb
->frontend
== NULL
) {
459 ": Failed to attach dummy front end\n");
464 /* define general-purpose callback pointer */
465 dvb
->frontend
->callback
= cx231xx_tuner_callback
;
467 if (!dvb_attach(xc5000_attach
, dev
->dvb
->frontend
,
468 &dev
->i2c_bus
[1].i2c_adap
,
469 &cnxt_rde250_tunerconfig
)) {
475 case CX231XX_BOARD_CNXT_RDU_250
:
477 dev
->dvb
->frontend
= dvb_attach(dvb_dummy_fe_ofdm_attach
);
479 if (dev
->dvb
->frontend
== NULL
) {
481 ": Failed to attach dummy front end\n");
486 /* define general-purpose callback pointer */
487 dvb
->frontend
->callback
= cx231xx_tuner_callback
;
489 if (!dvb_attach(xc5000_attach
, dev
->dvb
->frontend
,
490 &dev
->i2c_bus
[1].i2c_adap
,
491 &cnxt_rde250_tunerconfig
)) {
498 printk(KERN_ERR
"%s/2: The frontend of your DVB/ATSC card"
499 " isn't supported yet\n", dev
->name
);
502 if (NULL
== dvb
->frontend
) {
504 "%s/2: frontend initialization failed\n", dev
->name
);
509 /* register everything */
510 result
= register_dvb(dvb
, THIS_MODULE
, dev
, &dev
->udev
->dev
);
515 cx231xx_set_mode(dev
, CX231XX_SUSPEND
);
516 printk(KERN_INFO
"Successfully loaded cx231xx-dvb\n");
520 cx231xx_set_mode(dev
, CX231XX_SUSPEND
);
526 static int dvb_fini(struct cx231xx
*dev
)
528 if (!dev
->board
.has_dvb
) {
529 /* This device does not support the extension */
534 unregister_dvb(dev
->dvb
);
541 static struct cx231xx_ops dvb_ops
= {
543 .name
= "Cx231xx dvb Extension",
548 static int __init
cx231xx_dvb_register(void)
550 return cx231xx_register_extension(&dvb_ops
);
553 static void __exit
cx231xx_dvb_unregister(void)
555 cx231xx_unregister_extension(&dvb_ops
);
558 module_init(cx231xx_dvb_register
);
559 module_exit(cx231xx_dvb_unregister
);