include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-dvb.c
index dd693a1..8c95793 100644 (file)
 
 #include <linux/kthread.h>
 #include <linux/freezer.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
 #include "dvbdev.h"
+#include "pvrusb2-debug.h"
 #include "pvrusb2-hdw-internal.h"
 #include "pvrusb2-hdw.h"
 #include "pvrusb2-io.h"
 
 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)
 {
-       struct pvr2_dvb_fh fh;
        int ret;
        unsigned int count;
        struct pvr2_buffer *bp;
+       struct pvr2_stream *stream;
 
-       printk(KERN_DEBUG "dvb thread started\n");
+       pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread started");
        set_freezable();
 
-       memset(&fh, 0, sizeof(fh));
-
-       ret = pvr2_dvb_fh_init(&fh, adap);
-       if (ret != 0)
-               return ret;
+       stream = adap->channel.stream->stream;
 
        for (;;) {
-               if ((0 == adap->feedcount) || (kthread_should_stop()))
-                       break;
+               if (kthread_should_stop()) break;
 
                /* Not sure about this... */
                try_to_freeze();
 
-               bp = pvr2_stream_get_ready_buffer(fh.stream);
+               bp = pvr2_stream_get_ready_buffer(stream);
                if (bp != NULL) {
                        count = pvr2_buffer_get_count(bp);
                        if (count) {
                                dvb_dmx_swfilter(
                                        &adap->demux,
-                                       fh.buffer_storage[
-                                               pvr2_buffer_get_id(bp)],
+                                       adap->buffer_storage[
+                                           pvr2_buffer_get_id(bp)],
                                        count);
                        } else {
                                ret = pvr2_buffer_get_status(bp);
-                               if (ret < 0)
-                                       break;
+                               if (ret < 0) break;
                        }
                        ret = pvr2_buffer_queue(bp);
-                       if (ret < 0)
-                               break;
+                       if (ret < 0) break;
 
                        /* Since we know we did something to a buffer,
                           just go back and try again.  No point in
@@ -187,20 +73,19 @@ static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
                }
 
 
-               /* Wait until more buffers become available. */
+               /* Wait until more buffers become available or we're
+                  told not to wait any longer. */
                ret = wait_event_interruptible(
-                       fh.wait_data,
-                       pvr2_stream_get_ready_count(fh.stream) > 0);
-               if (ret < 0)
-                       break;
+                   adap->buffer_wait_data,
+                   (pvr2_stream_get_ready_count(stream) > 0) ||
+                   kthread_should_stop());
+               if (ret < 0) break;
        }
 
-       pvr2_dvb_fh_done(&fh);
-
        /* 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... */
 
-       printk(KERN_DEBUG "dvb thread stopped\n");
+       pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
 
        return 0;
 }
@@ -216,41 +101,133 @@ static int pvr2_dvb_feed_thread(void *data)
        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;
-       int newfeedcount, ret = 0;
+       wake_up(&adap->buffer_wait_data);
+}
 
-       if (adap == NULL)
-               return -ENODEV;
+static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
+{
+       unsigned int idx;
+       struct pvr2_stream *stream;
 
-       mutex_lock(&adap->lock);
-       newfeedcount = adap->feedcount + (onoff ? 1 : -1);
+       if (adap->thread) {
+               kthread_stop(adap->thread);
+               adap->thread = NULL;
+       }
+
+       if (adap->channel.stream) {
+               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);
+       }
+
+       if (adap->stream_run) {
+               for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
+                       if (!(adap->buffer_storage[idx])) continue;
+                       kfree(adap->buffer_storage[idx]);
+                       adap->buffer_storage[idx] = NULL;
+               }
+               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 = NULL;
+
+       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;
+       }
+
+       pvr2_stream_set_callback(pvr->video_stream.stream,
+                                (pvr2_stream_callback) pvr2_dvb_notify, adap);
+
+       ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
+       if (ret < 0) return ret;
+
+       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)) != NULL) {
+               ret = pvr2_buffer_queue(bp);
+               if (ret < 0) return ret;
+       }
 
-       if (newfeedcount == 0) {
-               printk(KERN_DEBUG "stop feeding\n");
+       adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
 
-               ret = kthread_stop(adap->thread);
+       if (IS_ERR(adap->thread)) {
+               ret = PTR_ERR(adap->thread);
                adap->thread = NULL;
+               return ret;
        }
 
-       adap->feedcount = newfeedcount;
+       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;
+}
 
-       if (adap->feedcount == onoff && adap->feedcount > 0) {
-               if (NULL != adap->thread)
-                       goto fail;
+static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
+{
+       struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
+       int ret = 0;
 
-               printk(KERN_DEBUG "start feeding\n");
+       if (adap == NULL) return -ENODEV;
 
-               adap->thread = kthread_run(pvr2_dvb_feed_thread,
-                                          adap, "pvrusb2-dvb");
-               if (IS_ERR(adap->thread)) {
-                       ret = PTR_ERR(adap->thread);
-                       adap->thread = NULL;
+       mutex_lock(&adap->lock);
+       do {
+               if (onoff) {
+                       if (!adap->feedcount) {
+                               pvr2_trace(PVR2_TRACE_DVB_FEED,
+                                          "start feeding demux");
+                               ret = pvr2_dvb_stream_start(adap);
+                               if (ret < 0) break;
+                       }
+                       (adap->feedcount)++;
+               } else if (adap->feedcount > 0) {
+                       (adap->feedcount)--;
+                       if (!adap->feedcount) {
+                               pvr2_trace(PVR2_TRACE_DVB_FEED,
+                                          "stop feeding demux");
+                               pvr2_dvb_stream_end(adap);
+                       }
                }
-               //ret = newfeedcount;
-       }
-fail:
+       } while (0);
        mutex_unlock(&adap->lock);
 
        return ret;
@@ -258,27 +235,22 @@ fail:
 
 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
 {
-       printk(KERN_DEBUG "start pid: 0x%04x, feedtype: %d\n",
-              dvbdmxfeed->pid, dvbdmxfeed->type);
+       pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
        return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
 }
 
 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
 {
-       printk(KERN_DEBUG "stop pid: 0x%04x, feedtype: %d\n",
-              dvbdmxfeed->pid, dvbdmxfeed->type);
+       pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
        return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
 }
 
 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
 {
-       /* TO DO: This function will call into the core and request for
-        * input to be set to 'dtv' if (acquire) and if it isn't set already.
-        *
-        * If (!acquire) then we should do nothing -- don't switch inputs
-        * again unless the analog side of the driver requests the bus.
-        */
-       return 0;
+       struct pvr2_dvb_adapter *adap = fe->dvb->priv;
+       return pvr2_channel_limit_inputs(
+           &adap->channel,
+           (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
 }
 
 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
@@ -287,10 +259,11 @@ static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
 
        ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
                                   THIS_MODULE/*&hdw->usb_dev->owner*/,
-                                  &adap->pvr->hdw->usb_dev->dev,
+                                  &adap->channel.hdw->usb_dev->dev,
                                   adapter_nr);
        if (ret < 0) {
-               err("dvb_register_adapter failed: error %d", ret);
+               pvr2_trace(PVR2_TRACE_ERROR_LEGS,
+                          "dvb_register_adapter failed: error %d", ret);
                goto err;
        }
        adap->dvb_adap.priv = adap;
@@ -307,7 +280,8 @@ static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
 
        ret = dvb_dmx_init(&adap->demux);
        if (ret < 0) {
-               err("dvb_dmx_init failed: error %d", ret);
+               pvr2_trace(PVR2_TRACE_ERROR_LEGS,
+                          "dvb_dmx_init failed: error %d", ret);
                goto err_dmx;
        }
 
@@ -317,14 +291,13 @@ static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
 
        ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
        if (ret < 0) {
-               err("dvb_dmxdev_init failed: error %d", ret);
+               pvr2_trace(PVR2_TRACE_ERROR_LEGS,
+                          "dvb_dmxdev_init failed: error %d", ret);
                goto err_dmx_dev;
        }
 
        dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
 
-       adap->digital_up = 1;
-
        return 0;
 
 err_dmx_dev:
@@ -337,57 +310,52 @@ err:
 
 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
 {
-       if (adap->digital_up) {
-               printk(KERN_DEBUG "unregistering DVB devices\n");
-               dvb_net_release(&adap->dvb_net);
-               adap->demux.dmx.close(&adap->demux.dmx);
-               dvb_dmxdev_release(&adap->dmxdev);
-               dvb_dmx_release(&adap->demux);
-               dvb_unregister_adapter(&adap->dvb_adap);
-               adap->digital_up = 0;
-       }
+       pvr2_trace(PVR2_TRACE_INFO, "unregistering DVB devices");
+       dvb_net_release(&adap->dvb_net);
+       adap->demux.dmx.close(&adap->demux.dmx);
+       dvb_dmxdev_release(&adap->dmxdev);
+       dvb_dmx_release(&adap->demux);
+       dvb_unregister_adapter(&adap->dvb_adap);
        return 0;
 }
 
 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
 {
-       struct pvr2_hdw *hdw = adap->pvr->hdw;
-       struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
-       int ret;
+       struct pvr2_hdw *hdw = adap->channel.hdw;
+       const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
+       int ret = 0;
 
        if (dvb_props == NULL) {
-               err("fe_props not defined!");
+               pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
                return -EINVAL;
        }
 
-       /* FIXME: This code should be moved into the core,
-        * and should only be called if we don't already have
-        * control of the bus.
-        *
-        * We can't call "pvr2_dvb_bus_ctrl(adap->fe, 1)" from here,
-        * because adap->fe isn't defined yet.
-        */
-       ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_by_id(hdw,
-                                                         PVR2_CID_INPUT),
-                                 PVR2_CVAL_INPUT_DTV);
-       if (ret != 0)
+       ret = pvr2_channel_limit_inputs(
+           &adap->channel,
+           (1 << PVR2_CVAL_INPUT_DTV));
+       if (ret) {
+               pvr2_trace(PVR2_TRACE_ERROR_LEGS,
+                          "failed to grab control of dtv input (code=%d)",
+                   ret);
                return ret;
-
-       pvr2_hdw_commit_ctl(hdw);
-
+       }
 
        if (dvb_props->frontend_attach == NULL) {
-               err("frontend_attach not defined!");
-               return -EINVAL;
+               pvr2_trace(PVR2_TRACE_ERROR_LEGS,
+                          "frontend_attach not defined!");
+               ret = -EINVAL;
+               goto done;
        }
 
        if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
 
                if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
-                       err("frontend registration failed!");
+                       pvr2_trace(PVR2_TRACE_ERROR_LEGS,
+                                  "frontend registration failed!");
                        dvb_frontend_detach(adap->fe);
                        adap->fe = NULL;
-                       return -ENODEV;
+                       ret = -ENODEV;
+                       goto done;
                }
 
                if (dvb_props->tuner_attach)
@@ -400,11 +368,15 @@ static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
                adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
 
        } else {
-               err("no frontend was attached!");
-               return -ENODEV;
+               pvr2_trace(PVR2_TRACE_ERROR_LEGS,
+                          "no frontend was attached!");
+               ret = -ENODEV;
+               return ret;
        }
 
-       return 0;
+ done:
+       pvr2_channel_limit_inputs(&adap->channel, 0);
+       return ret;
 }
 
 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
@@ -416,28 +388,48 @@ static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
        return 0;
 }
 
-int pvr2_dvb_init(struct pvr2_context *pvr)
+static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
 {
-       int ret = 0;
-
-       pvr->hdw->dvb.pvr = pvr;
-       mutex_init(&pvr->hdw->dvb.lock);
-
-       ret = pvr2_dvb_adapter_init(&pvr->hdw->dvb);
-       if (ret < 0)
-               goto fail;
-
-       ret = pvr2_dvb_frontend_init(&pvr->hdw->dvb);
-fail:
-       return ret;
+       pvr2_dvb_stream_end(adap);
+       pvr2_dvb_frontend_exit(adap);
+       pvr2_dvb_adapter_exit(adap);
+       pvr2_channel_done(&adap->channel);
+       kfree(adap);
 }
 
-int pvr2_dvb_exit(struct pvr2_context *pvr)
+static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
 {
-       pvr2_dvb_frontend_exit(&pvr->hdw->dvb);
-       pvr2_dvb_adapter_exit(&pvr->hdw->dvb);
-
-       pvr->hdw->dvb.pvr = NULL;
+       struct pvr2_dvb_adapter *adap;
+       adap = container_of(chp, struct pvr2_dvb_adapter, channel);
+       if (!adap->channel.mc_head->disconnect_flag) return;
+       pvr2_dvb_destroy(adap);
+}
 
-       return 0;
+struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
+{
+       int ret = 0;
+       struct pvr2_dvb_adapter *adap;
+       if (!pvr->hdw->hdw_desc->dvb_props) {
+               /* Device lacks a digital interface so don't set up
+                  the DVB side of the driver either.  For now. */
+               return NULL;
+       }
+       adap = kzalloc(sizeof(*adap), GFP_KERNEL);
+       if (!adap) return adap;
+       pvr2_channel_init(&adap->channel, pvr);
+       adap->channel.check_func = pvr2_dvb_internal_check;
+       init_waitqueue_head(&adap->buffer_wait_data);
+       mutex_init(&adap->lock);
+       ret = pvr2_dvb_adapter_init(adap);
+       if (ret < 0) goto fail1;
+       ret = pvr2_dvb_frontend_init(adap);
+       if (ret < 0) goto fail2;
+       return adap;
+
+fail2:
+       pvr2_dvb_adapter_exit(adap);
+fail1:
+       pvr2_channel_done(&adap->channel);
+       return NULL;
 }
+