2 * pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
4 * Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kthread.h>
22 #include <linux/freezer.h>
24 #include "pvrusb2-hdw-internal.h"
25 #include "pvrusb2-hdw.h"
26 #include "pvrusb2-io.h"
27 #include "pvrusb2-dvb.h"
29 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
31 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter
*adap
)
35 struct pvr2_buffer
*bp
;
36 struct pvr2_stream
*stream
;
38 printk(KERN_DEBUG
"dvb thread started\n");
41 stream
= adap
->channel
.stream
->stream
;
44 if (kthread_should_stop()) break;
46 /* Not sure about this... */
49 bp
= pvr2_stream_get_ready_buffer(stream
);
51 count
= pvr2_buffer_get_count(bp
);
56 pvr2_buffer_get_id(bp
)],
59 ret
= pvr2_buffer_get_status(bp
);
62 ret
= pvr2_buffer_queue(bp
);
65 /* Since we know we did something to a buffer,
66 just go back and try again. No point in
67 blocking unless we really ran out of
68 buffers to process. */
73 /* Wait until more buffers become available or we're
74 told not to wait any longer. */
75 ret
= wait_event_interruptible(
76 adap
->buffer_wait_data
,
77 (pvr2_stream_get_ready_count(stream
) > 0) ||
78 kthread_should_stop());
82 /* If we get here and ret is < 0, then an error has occurred.
83 Probably would be a good idea to communicate that to DVB core... */
85 printk(KERN_DEBUG
"dvb thread stopped\n");
90 static int pvr2_dvb_feed_thread(void *data
)
92 int stat
= pvr2_dvb_feed_func(data
);
93 /* from videobuf-dvb.c: */
94 while (!kthread_should_stop()) {
95 set_current_state(TASK_INTERRUPTIBLE
);
101 static void pvr2_dvb_notify(struct pvr2_dvb_adapter
*adap
)
103 wake_up(&adap
->buffer_wait_data
);
106 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter
*adap
)
109 struct pvr2_stream
*stream
;
112 kthread_stop(adap
->thread
);
116 if (adap
->channel
.stream
) {
117 stream
= adap
->channel
.stream
->stream
;
122 pvr2_hdw_set_streaming(adap
->channel
.hdw
, 0);
123 pvr2_stream_set_callback(stream
, NULL
, NULL
);
124 pvr2_stream_kill(stream
);
125 pvr2_stream_set_buffer_count(stream
, 0);
126 pvr2_channel_claim_stream(&adap
->channel
, NULL
);
129 if (adap
->stream_run
) {
130 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
131 if (!(adap
->buffer_storage
[idx
])) continue;
132 kfree(adap
->buffer_storage
[idx
]);
133 adap
->buffer_storage
[idx
] = NULL
;
135 adap
->stream_run
= 0;
139 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter
*adap
)
141 struct pvr2_context
*pvr
= adap
->channel
.mc_head
;
144 struct pvr2_buffer
*bp
;
145 struct pvr2_stream
*stream
= NULL
;
147 if (adap
->stream_run
) return -EIO
;
149 ret
= pvr2_channel_claim_stream(&adap
->channel
, &pvr
->video_stream
);
150 /* somebody else already has the stream */
151 if (ret
< 0) return ret
;
153 stream
= adap
->channel
.stream
->stream
;
155 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
156 adap
->buffer_storage
[idx
] = kmalloc(PVR2_DVB_BUFFER_SIZE
,
158 if (!(adap
->buffer_storage
[idx
])) return -ENOMEM
;
161 pvr2_stream_set_callback(pvr
->video_stream
.stream
,
162 (pvr2_stream_callback
) pvr2_dvb_notify
, adap
);
164 ret
= pvr2_stream_set_buffer_count(stream
, PVR2_DVB_BUFFER_COUNT
);
165 if (ret
< 0) return ret
;
167 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
168 bp
= pvr2_stream_get_buffer(stream
, idx
);
169 pvr2_buffer_set_buffer(bp
,
170 adap
->buffer_storage
[idx
],
171 PVR2_DVB_BUFFER_SIZE
);
174 ret
= pvr2_hdw_set_streaming(adap
->channel
.hdw
, 1);
175 if (ret
< 0) return ret
;
177 while ((bp
= pvr2_stream_get_idle_buffer(stream
)) != NULL
) {
178 ret
= pvr2_buffer_queue(bp
);
179 if (ret
< 0) return ret
;
182 adap
->thread
= kthread_run(pvr2_dvb_feed_thread
, adap
, "pvrusb2-dvb");
184 if (IS_ERR(adap
->thread
)) {
185 ret
= PTR_ERR(adap
->thread
);
190 adap
->stream_run
= !0;
195 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter
*adap
)
197 int ret
= pvr2_dvb_stream_do_start(adap
);
198 if (ret
< 0) pvr2_dvb_stream_end(adap
);
202 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed
*dvbdmxfeed
, int onoff
)
204 struct pvr2_dvb_adapter
*adap
= dvbdmxfeed
->demux
->priv
;
207 if (adap
== NULL
) return -ENODEV
;
209 mutex_lock(&adap
->lock
);
212 if (!adap
->feedcount
) {
213 printk(KERN_DEBUG
"start feeding\n");
214 ret
= pvr2_dvb_stream_start(adap
);
218 } else if (adap
->feedcount
> 0) {
220 if (!adap
->feedcount
) {
221 printk(KERN_DEBUG
"stop feeding\n");
222 pvr2_dvb_stream_end(adap
);
226 mutex_unlock(&adap
->lock
);
231 static int pvr2_dvb_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
233 printk(KERN_DEBUG
"start pid: 0x%04x, feedtype: %d\n",
234 dvbdmxfeed
->pid
, dvbdmxfeed
->type
);
235 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 1);
238 static int pvr2_dvb_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
240 printk(KERN_DEBUG
"stop pid: 0x%04x, feedtype: %d\n",
241 dvbdmxfeed
->pid
, dvbdmxfeed
->type
);
242 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 0);
245 static int pvr2_dvb_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
247 struct pvr2_dvb_adapter
*adap
= fe
->dvb
->priv
;
248 return pvr2_channel_limit_inputs(
250 (acquire
? (1 << PVR2_CVAL_INPUT_DTV
) : 0));
253 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter
*adap
)
257 ret
= dvb_register_adapter(&adap
->dvb_adap
, "pvrusb2-dvb",
258 THIS_MODULE
/*&hdw->usb_dev->owner*/,
259 &adap
->channel
.hdw
->usb_dev
->dev
,
262 err("dvb_register_adapter failed: error %d", ret
);
265 adap
->dvb_adap
.priv
= adap
;
267 adap
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
|
268 DMX_SECTION_FILTERING
|
269 DMX_MEMORY_BASED_FILTERING
;
270 adap
->demux
.priv
= adap
;
271 adap
->demux
.filternum
= 256;
272 adap
->demux
.feednum
= 256;
273 adap
->demux
.start_feed
= pvr2_dvb_start_feed
;
274 adap
->demux
.stop_feed
= pvr2_dvb_stop_feed
;
275 adap
->demux
.write_to_decoder
= NULL
;
277 ret
= dvb_dmx_init(&adap
->demux
);
279 err("dvb_dmx_init failed: error %d", ret
);
283 adap
->dmxdev
.filternum
= adap
->demux
.filternum
;
284 adap
->dmxdev
.demux
= &adap
->demux
.dmx
;
285 adap
->dmxdev
.capabilities
= 0;
287 ret
= dvb_dmxdev_init(&adap
->dmxdev
, &adap
->dvb_adap
);
289 err("dvb_dmxdev_init failed: error %d", ret
);
293 dvb_net_init(&adap
->dvb_adap
, &adap
->dvb_net
, &adap
->demux
.dmx
);
298 dvb_dmx_release(&adap
->demux
);
300 dvb_unregister_adapter(&adap
->dvb_adap
);
305 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter
*adap
)
307 printk(KERN_DEBUG
"unregistering DVB devices\n");
308 dvb_net_release(&adap
->dvb_net
);
309 adap
->demux
.dmx
.close(&adap
->demux
.dmx
);
310 dvb_dmxdev_release(&adap
->dmxdev
);
311 dvb_dmx_release(&adap
->demux
);
312 dvb_unregister_adapter(&adap
->dvb_adap
);
316 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter
*adap
)
318 struct pvr2_hdw
*hdw
= adap
->channel
.hdw
;
319 struct pvr2_dvb_props
*dvb_props
= hdw
->hdw_desc
->dvb_props
;
322 if (dvb_props
== NULL
) {
323 err("fe_props not defined!");
327 ret
= pvr2_channel_limit_inputs(
329 (1 << PVR2_CVAL_INPUT_DTV
));
331 err("failed to grab control of dtv input (code=%d)",
336 if (dvb_props
->frontend_attach
== NULL
) {
337 err("frontend_attach not defined!");
342 if ((dvb_props
->frontend_attach(adap
) == 0) && (adap
->fe
)) {
344 if (dvb_register_frontend(&adap
->dvb_adap
, adap
->fe
)) {
345 err("frontend registration failed!");
346 dvb_frontend_detach(adap
->fe
);
352 if (dvb_props
->tuner_attach
)
353 dvb_props
->tuner_attach(adap
);
355 if (adap
->fe
->ops
.analog_ops
.standby
)
356 adap
->fe
->ops
.analog_ops
.standby(adap
->fe
);
358 /* Ensure all frontends negotiate bus access */
359 adap
->fe
->ops
.ts_bus_ctrl
= pvr2_dvb_bus_ctrl
;
362 err("no frontend was attached!");
368 pvr2_channel_limit_inputs(&adap
->channel
, 0);
372 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter
*adap
)
374 if (adap
->fe
!= NULL
) {
375 dvb_unregister_frontend(adap
->fe
);
376 dvb_frontend_detach(adap
->fe
);
381 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter
*adap
)
383 pvr2_dvb_stream_end(adap
);
384 pvr2_dvb_frontend_exit(adap
);
385 pvr2_dvb_adapter_exit(adap
);
386 pvr2_channel_done(&adap
->channel
);
390 static void pvr2_dvb_internal_check(struct pvr2_channel
*chp
)
392 struct pvr2_dvb_adapter
*adap
;
393 adap
= container_of(chp
, struct pvr2_dvb_adapter
, channel
);
394 if (!adap
->channel
.mc_head
->disconnect_flag
) return;
395 pvr2_dvb_destroy(adap
);
398 struct pvr2_dvb_adapter
*pvr2_dvb_create(struct pvr2_context
*pvr
)
401 struct pvr2_dvb_adapter
*adap
;
402 if (!pvr
->hdw
->hdw_desc
->dvb_props
) {
403 /* Device lacks a digital interface so don't set up
404 the DVB side of the driver either. For now. */
407 adap
= kzalloc(sizeof(*adap
), GFP_KERNEL
);
408 if (!adap
) return adap
;
409 pvr2_channel_init(&adap
->channel
, pvr
);
410 adap
->channel
.check_func
= pvr2_dvb_internal_check
;
411 init_waitqueue_head(&adap
->buffer_wait_data
);
412 mutex_init(&adap
->lock
);
413 ret
= pvr2_dvb_adapter_init(adap
);
414 if (ret
< 0) goto fail1
;
415 ret
= pvr2_dvb_frontend_init(adap
);
416 if (ret
< 0) goto fail2
;
420 pvr2_dvb_adapter_exit(adap
);
422 pvr2_channel_done(&adap
->channel
);