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/slab.h>
24 #include <linux/usb.h>
27 #include <media/v4l2-common.h>
28 #include <media/videobuf-vmalloc.h>
31 #include "dvb_dummy_fe.h"
33 MODULE_DESCRIPTION("driver for cx231xx based DVB cards");
34 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
35 MODULE_LICENSE("GPL");
37 static unsigned int debug
;
38 module_param(debug
, int, 0644);
39 MODULE_PARM_DESC(debug
, "enable debug messages [dvb]");
41 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
43 #define dprintk(level, fmt, arg...) do { \
45 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
48 #define CX231XX_DVB_NUM_BUFS 5
49 #define CX231XX_DVB_MAX_PACKETSIZE 564
50 #define CX231XX_DVB_MAX_PACKETS 64
53 struct dvb_frontend
*frontend
;
55 /* feed count management */
59 /* general boilerplate stuff */
60 struct dvb_adapter adapter
;
61 struct dvb_demux demux
;
63 struct dmx_frontend fe_hw
;
64 struct dmx_frontend fe_mem
;
68 static inline void print_err_status(struct cx231xx
*dev
, int packet
, int status
)
70 char *errmsg
= "Unknown";
74 errmsg
= "unlinked synchronuously";
77 errmsg
= "unlinked asynchronuously";
80 errmsg
= "Buffer error (overrun)";
83 errmsg
= "Stalled (device not responding)";
86 errmsg
= "Babble (bad cable?)";
89 errmsg
= "Bit-stuff error (bad cable?)";
92 errmsg
= "CRC/Timeout (could be anything)";
95 errmsg
= "Device does not respond";
99 dprintk(1, "URB status %d [%s].\n", status
, errmsg
);
101 dprintk(1, "URB packet %d, status %d [%s].\n",
102 packet
, status
, errmsg
);
106 static inline int dvb_isoc_copy(struct cx231xx
*dev
, struct urb
*urb
)
113 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
116 if (urb
->status
< 0) {
117 print_err_status(dev
, -1, urb
->status
);
118 if (urb
->status
== -ENOENT
)
122 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
123 int status
= urb
->iso_frame_desc
[i
].status
;
126 print_err_status(dev
, i
, status
);
127 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
131 dvb_dmx_swfilter(&dev
->dvb
->demux
, urb
->transfer_buffer
+
132 urb
->iso_frame_desc
[i
].offset
,
133 urb
->iso_frame_desc
[i
].actual_length
);
139 static int start_streaming(struct cx231xx_dvb
*dvb
)
142 struct cx231xx
*dev
= dvb
->adapter
.priv
;
144 usb_set_interface(dev
->udev
, 0, 1);
145 rc
= cx231xx_set_mode(dev
, CX231XX_DIGITAL_MODE
);
149 return cx231xx_init_isoc(dev
, CX231XX_DVB_MAX_PACKETS
,
150 CX231XX_DVB_NUM_BUFS
,
151 CX231XX_DVB_MAX_PACKETSIZE
, dvb_isoc_copy
);
154 static int stop_streaming(struct cx231xx_dvb
*dvb
)
156 struct cx231xx
*dev
= dvb
->adapter
.priv
;
158 cx231xx_uninit_isoc(dev
);
160 cx231xx_set_mode(dev
, CX231XX_SUSPEND
);
165 static int start_feed(struct dvb_demux_feed
*feed
)
167 struct dvb_demux
*demux
= feed
->demux
;
168 struct cx231xx_dvb
*dvb
= demux
->priv
;
171 if (!demux
->dmx
.frontend
)
174 mutex_lock(&dvb
->lock
);
178 if (dvb
->nfeeds
== 1) {
179 ret
= start_streaming(dvb
);
184 mutex_unlock(&dvb
->lock
);
188 static int stop_feed(struct dvb_demux_feed
*feed
)
190 struct dvb_demux
*demux
= feed
->demux
;
191 struct cx231xx_dvb
*dvb
= demux
->priv
;
194 mutex_lock(&dvb
->lock
);
197 if (0 == dvb
->nfeeds
)
198 err
= stop_streaming(dvb
);
200 mutex_unlock(&dvb
->lock
);
204 /* ------------------------------------------------------------------ */
205 static int cx231xx_dvb_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
207 struct cx231xx
*dev
= fe
->dvb
->priv
;
210 return cx231xx_set_mode(dev
, CX231XX_DIGITAL_MODE
);
212 return cx231xx_set_mode(dev
, CX231XX_SUSPEND
);
215 /* ------------------------------------------------------------------ */
217 static struct xc5000_config cnxt_rde250_tunerconfig
= {
222 /* ------------------------------------------------------------------ */
224 static int attach_xc5000(u8 addr
, struct cx231xx
*dev
)
227 struct dvb_frontend
*fe
;
228 struct xc5000_config cfg
;
230 memset(&cfg
, 0, sizeof(cfg
));
231 cfg
.i2c_adap
= &dev
->i2c_bus
[1].i2c_adap
;
234 if (!dev
->dvb
->frontend
) {
235 printk(KERN_ERR
"%s/2: dvb frontend not attached. "
236 "Can't attach xc5000\n", dev
->name
);
240 fe
= dvb_attach(xc5000_attach
, dev
->dvb
->frontend
, &cfg
);
242 printk(KERN_ERR
"%s/2: xc5000 attach failed\n", dev
->name
);
243 dvb_frontend_detach(dev
->dvb
->frontend
);
244 dev
->dvb
->frontend
= NULL
;
248 printk(KERN_INFO
"%s/2: xc5000 attached\n", dev
->name
);
254 int cx231xx_set_analog_freq(struct cx231xx
*dev
, u32 freq
)
258 if ((dev
->dvb
!= NULL
) && (dev
->dvb
->frontend
!= NULL
)) {
260 struct dvb_tuner_ops
*dops
= &dev
->dvb
->frontend
->ops
.tuner_ops
;
262 if (dops
->set_analog_params
!= NULL
) {
263 struct analog_parameters params
;
265 params
.frequency
= freq
;
266 params
.std
= dev
->norm
;
267 params
.mode
= 0; /* 0- Air; 1 - cable */
268 /*params.audmode = ; */
270 /* Set the analog parameters to set the frequency */
271 cx231xx_info("Setting Frequency for XC5000\n");
272 dops
->set_analog_params(dev
->dvb
->frontend
, ¶ms
);
280 int cx231xx_reset_analog_tuner(struct cx231xx
*dev
)
284 if ((dev
->dvb
!= NULL
) && (dev
->dvb
->frontend
!= NULL
)) {
286 struct dvb_tuner_ops
*dops
= &dev
->dvb
->frontend
->ops
.tuner_ops
;
288 if (dops
->init
!= NULL
&& !dev
->xc_fw_load_done
) {
290 cx231xx_info("Reloading firmware for XC5000\n");
291 status
= dops
->init(dev
->dvb
->frontend
);
293 dev
->xc_fw_load_done
= 1;
295 ("XC5000 firmware download completed\n");
297 dev
->xc_fw_load_done
= 0;
299 ("XC5000 firmware download failed !!!\n");
308 /* ------------------------------------------------------------------ */
310 static int register_dvb(struct cx231xx_dvb
*dvb
,
311 struct module
*module
,
312 struct cx231xx
*dev
, struct device
*device
)
316 mutex_init(&dvb
->lock
);
318 /* register adapter */
319 result
= dvb_register_adapter(&dvb
->adapter
, dev
->name
, module
, device
,
323 "%s: dvb_register_adapter failed (errno = %d)\n",
328 /* Ensure all frontends negotiate bus access */
329 dvb
->frontend
->ops
.ts_bus_ctrl
= cx231xx_dvb_bus_ctrl
;
331 dvb
->adapter
.priv
= dev
;
333 /* register frontend */
334 result
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
337 "%s: dvb_register_frontend failed (errno = %d)\n",
342 /* register demux stuff */
343 dvb
->demux
.dmx
.capabilities
=
344 DMX_TS_FILTERING
| DMX_SECTION_FILTERING
|
345 DMX_MEMORY_BASED_FILTERING
;
346 dvb
->demux
.priv
= dvb
;
347 dvb
->demux
.filternum
= 256;
348 dvb
->demux
.feednum
= 256;
349 dvb
->demux
.start_feed
= start_feed
;
350 dvb
->demux
.stop_feed
= stop_feed
;
352 result
= dvb_dmx_init(&dvb
->demux
);
354 printk(KERN_WARNING
"%s: dvb_dmx_init failed (errno = %d)\n",
359 dvb
->dmxdev
.filternum
= 256;
360 dvb
->dmxdev
.demux
= &dvb
->demux
.dmx
;
361 dvb
->dmxdev
.capabilities
= 0;
362 result
= dvb_dmxdev_init(&dvb
->dmxdev
, &dvb
->adapter
);
364 printk(KERN_WARNING
"%s: dvb_dmxdev_init failed (errno = %d)\n",
369 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
370 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
373 "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
378 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
379 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
382 "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
387 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
390 "%s: connect_frontend failed (errno = %d)\n", dev
->name
,
395 /* register network adapter */
396 dvb_net_init(&dvb
->adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
400 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
402 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
404 dvb_dmxdev_release(&dvb
->dmxdev
);
406 dvb_dmx_release(&dvb
->demux
);
408 dvb_unregister_frontend(dvb
->frontend
);
410 dvb_frontend_detach(dvb
->frontend
);
411 dvb_unregister_adapter(&dvb
->adapter
);
416 static void unregister_dvb(struct cx231xx_dvb
*dvb
)
418 dvb_net_release(&dvb
->net
);
419 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
420 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
421 dvb_dmxdev_release(&dvb
->dmxdev
);
422 dvb_dmx_release(&dvb
->demux
);
423 dvb_unregister_frontend(dvb
->frontend
);
424 dvb_frontend_detach(dvb
->frontend
);
425 dvb_unregister_adapter(&dvb
->adapter
);
428 static int dvb_init(struct cx231xx
*dev
)
431 struct cx231xx_dvb
*dvb
;
433 if (!dev
->board
.has_dvb
) {
434 /* This device does not support the extension */
438 dvb
= kzalloc(sizeof(struct cx231xx_dvb
), GFP_KERNEL
);
441 printk(KERN_INFO
"cx231xx_dvb: memory allocation failed\n");
445 dev
->cx231xx_set_analog_freq
= cx231xx_set_analog_freq
;
446 dev
->cx231xx_reset_analog_tuner
= cx231xx_reset_analog_tuner
;
448 cx231xx_set_mode(dev
, CX231XX_DIGITAL_MODE
);
450 switch (dev
->model
) {
451 case CX231XX_BOARD_CNXT_RDE_250
:
453 /* dev->dvb->frontend = dvb_attach(s5h1411_attach,
454 &dvico_s5h1411_config,
455 &dev->i2c_bus[1].i2c_adap); */
456 dev
->dvb
->frontend
= dvb_attach(dvb_dummy_fe_ofdm_attach
);
458 if (dev
->dvb
->frontend
== NULL
) {
460 ": Failed to attach dummy front end\n");
465 /* define general-purpose callback pointer */
466 dvb
->frontend
->callback
= cx231xx_tuner_callback
;
468 if (!dvb_attach(xc5000_attach
, dev
->dvb
->frontend
,
469 &dev
->i2c_bus
[1].i2c_adap
,
470 &cnxt_rde250_tunerconfig
)) {
476 case CX231XX_BOARD_CNXT_RDU_250
:
478 dev
->dvb
->frontend
= dvb_attach(dvb_dummy_fe_ofdm_attach
);
480 if (dev
->dvb
->frontend
== NULL
) {
482 ": Failed to attach dummy front end\n");
487 /* define general-purpose callback pointer */
488 dvb
->frontend
->callback
= cx231xx_tuner_callback
;
490 if (!dvb_attach(xc5000_attach
, dev
->dvb
->frontend
,
491 &dev
->i2c_bus
[1].i2c_adap
,
492 &cnxt_rde250_tunerconfig
)) {
499 printk(KERN_ERR
"%s/2: The frontend of your DVB/ATSC card"
500 " isn't supported yet\n", dev
->name
);
503 if (NULL
== dvb
->frontend
) {
505 "%s/2: frontend initialization failed\n", dev
->name
);
510 /* register everything */
511 result
= register_dvb(dvb
, THIS_MODULE
, dev
, &dev
->udev
->dev
);
516 cx231xx_set_mode(dev
, CX231XX_SUSPEND
);
517 printk(KERN_INFO
"Successfully loaded cx231xx-dvb\n");
521 cx231xx_set_mode(dev
, CX231XX_SUSPEND
);
527 static int dvb_fini(struct cx231xx
*dev
)
529 if (!dev
->board
.has_dvb
) {
530 /* This device does not support the extension */
535 unregister_dvb(dev
->dvb
);
542 static struct cx231xx_ops dvb_ops
= {
544 .name
= "Cx231xx dvb Extension",
549 static int __init
cx231xx_dvb_register(void)
551 return cx231xx_register_extension(&dvb_ops
);
554 static void __exit
cx231xx_dvb_unregister(void)
556 cx231xx_unregister_extension(&dvb_ops
);
559 module_init(cx231xx_dvb_register
);
560 module_exit(cx231xx_dvb_unregister
);