Commit a36416d0 authored by Mike Isely's avatar Mike Isely Committed by Mauro Carvalho Chehab

V4L/DVB (7688): pvrusb2: Clean up dvb streaming start/stop

Eliminate the need for a separate pvr2_dvb_fh; since in the DVB
context there can only ever be a single instance then there is no need
for a separate instance to handle streaming state.  This simplifies
the module.  Also move streaming start/stop out of the feed thread and
into the driver's main context - which makes it possible for streaming
start up failures to be detected by the DVB core.
Signed-off-by: default avatarMike Isely <isely@pobox.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@infradead.org>
parent ceb4340d
...@@ -28,156 +28,39 @@ ...@@ -28,156 +28,39 @@
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
#define BUFFER_COUNT 32
#define BUFFER_SIZE PAGE_ALIGN(0x4000)
struct pvr2_dvb_fh {
struct pvr2_channel channel;
struct pvr2_stream *stream;
struct pvr2_dvb_adapter *adap;
wait_queue_head_t wait_data;
char *buffer_storage[BUFFER_COUNT];
};
static void pvr2_dvb_notify(struct pvr2_dvb_fh *fhp)
{
wake_up(&fhp->wait_data);
}
static int pvr2_dvb_fh_init(struct pvr2_dvb_fh *fh,
struct pvr2_dvb_adapter *adap)
{
struct pvr2_context *pvr = adap->pvr;
unsigned int idx;
int ret;
struct pvr2_buffer *bp;
init_waitqueue_head(&fh->wait_data);
fh->adap = adap;
pvr2_channel_init(&fh->channel, adap->pvr);
ret = pvr2_channel_claim_stream(&fh->channel, &pvr->video_stream);
/* somebody else already has the stream */
if (ret != 0)
return ret;
fh->stream = pvr->video_stream.stream;
for (idx = 0; idx < BUFFER_COUNT; idx++) {
fh->buffer_storage[idx] = kmalloc(BUFFER_SIZE, GFP_KERNEL);
if (!(fh->buffer_storage[idx]))
break;
}
if (idx < BUFFER_COUNT) {
/* An allocation appears to have failed */
ret = -ENOMEM;
goto cleanup;
}
pvr2_stream_set_callback(pvr->video_stream.stream,
(pvr2_stream_callback) pvr2_dvb_notify, fh);
ret = pvr2_stream_set_buffer_count(fh->stream, BUFFER_COUNT);
if (ret < 0)
return ret;
for (idx = 0; idx < BUFFER_COUNT; idx++) {
bp = pvr2_stream_get_buffer(fh->stream, idx);
pvr2_buffer_set_buffer(bp,
fh->buffer_storage[idx],
BUFFER_SIZE);
}
ret = pvr2_hdw_set_streaming(fh->channel.hdw, 1);
if (ret < 0)
goto cleanup;
while ((bp = pvr2_stream_get_idle_buffer(fh->stream)) != 0) {
ret = pvr2_buffer_queue(bp);
if (ret < 0)
goto cleanup;
}
return ret;
cleanup:
if (fh->stream)
pvr2_stream_kill(fh->stream);
for (idx = 0; idx < BUFFER_COUNT; idx++) {
if (!(fh->buffer_storage[idx]))
continue;
kfree(fh->buffer_storage[idx]);
}
pvr2_channel_done(&fh->channel);
return ret;
}
static void pvr2_dvb_fh_done(struct pvr2_dvb_fh *fh)
{
unsigned int idx;
pvr2_hdw_set_streaming(fh->channel.hdw, 0);
pvr2_stream_kill(fh->stream);
// pvr2_channel_claim_stream(&fh->channel, NULL);
for (idx = 0; idx < BUFFER_COUNT; idx++) {
if (!(fh->buffer_storage[idx]))
continue;
kfree(fh->buffer_storage[idx]);
}
pvr2_channel_done(&fh->channel);
}
static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap) static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
{ {
struct pvr2_dvb_fh fh;
int ret; int ret;
unsigned int count; unsigned int count;
struct pvr2_buffer *bp; struct pvr2_buffer *bp;
struct pvr2_stream *stream;
printk(KERN_DEBUG "dvb thread started\n"); printk(KERN_DEBUG "dvb thread started\n");
set_freezable(); set_freezable();
memset(&fh, 0, sizeof(fh)); stream = adap->channel.stream->stream;
ret = pvr2_dvb_fh_init(&fh, adap);
if (ret != 0)
return ret;
for (;;) { for (;;) {
if ((0 == adap->feedcount) || (kthread_should_stop())) if (kthread_should_stop()) break;
break;
/* Not sure about this... */ /* Not sure about this... */
try_to_freeze(); try_to_freeze();
bp = pvr2_stream_get_ready_buffer(fh.stream); bp = pvr2_stream_get_ready_buffer(stream);
if (bp != NULL) { if (bp != NULL) {
count = pvr2_buffer_get_count(bp); count = pvr2_buffer_get_count(bp);
if (count) { if (count) {
dvb_dmx_swfilter( dvb_dmx_swfilter(
&adap->demux, &adap->demux,
fh.buffer_storage[ adap->buffer_storage[
pvr2_buffer_get_id(bp)], pvr2_buffer_get_id(bp)],
count); count);
} else { } else {
ret = pvr2_buffer_get_status(bp); ret = pvr2_buffer_get_status(bp);
if (ret < 0) if (ret < 0) break;
break;
} }
ret = pvr2_buffer_queue(bp); ret = pvr2_buffer_queue(bp);
if (ret < 0) if (ret < 0) break;
break;
/* Since we know we did something to a buffer, /* Since we know we did something to a buffer,
just go back and try again. No point in just go back and try again. No point in
...@@ -189,14 +72,11 @@ static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap) ...@@ -189,14 +72,11 @@ static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
/* Wait until more buffers become available. */ /* Wait until more buffers become available. */
ret = wait_event_interruptible( ret = wait_event_interruptible(
fh.wait_data, adap->buffer_wait_data,
pvr2_stream_get_ready_count(fh.stream) > 0); pvr2_stream_get_ready_count(stream) > 0);
if (ret < 0) if (ret < 0) break;
break;
} }
pvr2_dvb_fh_done(&fh);
/* If we get here and ret is < 0, then an error has occurred. /* If we get here and ret is < 0, then an error has occurred.
Probably would be a good idea to communicate that to DVB core... */ Probably would be a good idea to communicate that to DVB core... */
...@@ -216,41 +96,131 @@ static int pvr2_dvb_feed_thread(void *data) ...@@ -216,41 +96,131 @@ static int pvr2_dvb_feed_thread(void *data)
return stat; return stat;
} }
static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff) static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
{ {
struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv; wake_up(&adap->buffer_wait_data);
int newfeedcount, ret = 0; }
if (adap == NULL) static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
return -ENODEV; {
unsigned int idx;
struct pvr2_stream *stream;
mutex_lock(&adap->lock); if (adap->thread) {
newfeedcount = adap->feedcount + (onoff ? 1 : -1); kthread_stop(adap->thread);
adap->thread = NULL;
}
if (newfeedcount == 0) { if (adap->channel.stream) {
printk(KERN_DEBUG "stop feeding\n"); stream = adap->channel.stream->stream;
} else {
stream = NULL;
}
if (stream) {
pvr2_hdw_set_streaming(adap->channel.hdw, 0);
pvr2_stream_set_callback(stream, NULL, NULL);
pvr2_stream_kill(stream);
pvr2_stream_set_buffer_count(stream, 0);
pvr2_channel_claim_stream(&adap->channel, NULL);
}
ret = kthread_stop(adap->thread); if (adap->stream_run) {
adap->thread = NULL; for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
if (!(adap->buffer_storage[idx])) continue;
kfree(adap->buffer_storage[idx]);
adap->buffer_storage[idx] = 0;
}
adap->stream_run = 0;
}
}
static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
{
struct pvr2_context *pvr = adap->channel.mc_head;
unsigned int idx;
int ret;
struct pvr2_buffer *bp;
struct pvr2_stream *stream = 0;
if (adap->stream_run) return -EIO;
ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
/* somebody else already has the stream */
if (ret < 0) return ret;
stream = adap->channel.stream->stream;
for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
GFP_KERNEL);
if (!(adap->buffer_storage[idx])) return -ENOMEM;
} }
adap->feedcount = newfeedcount; pvr2_stream_set_callback(pvr->video_stream.stream,
(pvr2_stream_callback) pvr2_dvb_notify, adap);
if (adap->feedcount == onoff && adap->feedcount > 0) { ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
if (NULL != adap->thread) if (ret < 0) return ret;
goto fail;
printk(KERN_DEBUG "start feeding\n"); for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
bp = pvr2_stream_get_buffer(stream, idx);
pvr2_buffer_set_buffer(bp,
adap->buffer_storage[idx],
PVR2_DVB_BUFFER_SIZE);
}
ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
if (ret < 0) return ret;
while ((bp = pvr2_stream_get_idle_buffer(stream)) != 0) {
ret = pvr2_buffer_queue(bp);
if (ret < 0) return ret;
}
adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
adap->thread = kthread_run(pvr2_dvb_feed_thread,
adap, "pvrusb2-dvb");
if (IS_ERR(adap->thread)) { if (IS_ERR(adap->thread)) {
ret = PTR_ERR(adap->thread); ret = PTR_ERR(adap->thread);
adap->thread = NULL; adap->thread = NULL;
return ret;
}
adap->stream_run = !0;
return 0;
}
static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
{
int ret = pvr2_dvb_stream_do_start(adap);
if (ret < 0) pvr2_dvb_stream_end(adap);
return ret;
}
static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
{
struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
int ret = 0;
if (adap == NULL) return -ENODEV;
mutex_lock(&adap->lock);
do {
if (onoff) {
if (!adap->feedcount) {
printk(KERN_DEBUG "start feeding\n");
ret = pvr2_dvb_stream_start(adap);
if (ret < 0) break;
} }
//ret = newfeedcount; (adap->feedcount)++;
} else if (adap->feedcount > 0) {
(adap->feedcount)--;
if (!adap->feedcount) {
printk(KERN_DEBUG "stop feeding\n");
pvr2_dvb_stream_end(adap);
} }
fail: }
} while (0);
mutex_unlock(&adap->lock); mutex_unlock(&adap->lock);
return ret; return ret;
...@@ -287,7 +257,7 @@ static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap) ...@@ -287,7 +257,7 @@ static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb", ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
THIS_MODULE/*&hdw->usb_dev->owner*/, THIS_MODULE/*&hdw->usb_dev->owner*/,
&adap->pvr->hdw->usb_dev->dev, &adap->channel.hdw->usb_dev->dev,
adapter_nr); adapter_nr);
if (ret < 0) { if (ret < 0) {
err("dvb_register_adapter failed: error %d", ret); err("dvb_register_adapter failed: error %d", ret);
...@@ -351,7 +321,7 @@ static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap) ...@@ -351,7 +321,7 @@ static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap) static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
{ {
struct pvr2_hdw *hdw = adap->pvr->hdw; struct pvr2_hdw *hdw = adap->channel.hdw;
struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props; struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
int ret; int ret;
...@@ -419,14 +389,14 @@ static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap) ...@@ -419,14 +389,14 @@ static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
int pvr2_dvb_init(struct pvr2_context *pvr) int pvr2_dvb_init(struct pvr2_context *pvr)
{ {
int ret = 0; int ret = 0;
struct pvr2_dvb_adapter *adap;
pvr->hdw->dvb.pvr = pvr; adap = &pvr->hdw->dvb;
adap->init = !0;
pvr2_channel_init(&adap->channel, pvr);
init_waitqueue_head(&adap->buffer_wait_data);
mutex_init(&pvr->hdw->dvb.lock); mutex_init(&pvr->hdw->dvb.lock);
ret = pvr2_dvb_adapter_init(&pvr->hdw->dvb); ret = pvr2_dvb_adapter_init(&pvr->hdw->dvb);
if (ret < 0) if (ret < 0) goto fail;
goto fail;
ret = pvr2_dvb_frontend_init(&pvr->hdw->dvb); ret = pvr2_dvb_frontend_init(&pvr->hdw->dvb);
fail: fail:
return ret; return ret;
...@@ -434,10 +404,12 @@ int pvr2_dvb_init(struct pvr2_context *pvr) ...@@ -434,10 +404,12 @@ int pvr2_dvb_init(struct pvr2_context *pvr)
int pvr2_dvb_exit(struct pvr2_context *pvr) int pvr2_dvb_exit(struct pvr2_context *pvr)
{ {
pvr2_dvb_frontend_exit(&pvr->hdw->dvb); struct pvr2_dvb_adapter *adap;
pvr2_dvb_adapter_exit(&pvr->hdw->dvb); adap = &pvr->hdw->dvb;
if (!adap->init) return 0;
pvr->hdw->dvb.pvr = NULL; pvr2_dvb_stream_end(adap);
pvr2_dvb_frontend_exit(adap);
pvr2_dvb_adapter_exit(adap);
pvr2_channel_done(&adap->channel);
return 0; return 0;
} }
...@@ -7,8 +7,11 @@ ...@@ -7,8 +7,11 @@
#include "dmxdev.h" #include "dmxdev.h"
#include "pvrusb2-context.h" #include "pvrusb2-context.h"
#define PVR2_DVB_BUFFER_COUNT 32
#define PVR2_DVB_BUFFER_SIZE PAGE_ALIGN(0x4000)
struct pvr2_dvb_adapter { struct pvr2_dvb_adapter {
struct pvr2_context *pvr; struct pvr2_channel channel;
struct dvb_adapter dvb_adap; struct dvb_adapter dvb_adap;
struct dmxdev dmxdev; struct dmxdev dmxdev;
...@@ -23,6 +26,11 @@ struct pvr2_dvb_adapter { ...@@ -23,6 +26,11 @@ struct pvr2_dvb_adapter {
struct mutex lock; struct mutex lock;
unsigned int digital_up:1; unsigned int digital_up:1;
unsigned int stream_run:1;
unsigned int init:1;
wait_queue_head_t buffer_wait_data;
char *buffer_storage[PVR2_DVB_BUFFER_COUNT];
}; };
struct pvr2_dvb_props { struct pvr2_dvb_props {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment