V4L/DVB (11398): Support for Legend Silicon LGS8913/LGS8GL5/LGS8GXX China DMB-TH...
[safe/jmp/linux-2.6] / drivers / media / dvb / dvb-core / dvb_ca_en50221.c
index 3f74795..cb22da5 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/delay.h>
 #include <linux/spinlock.h>
 #include <linux/sched.h>
+#include <linux/kthread.h>
 
 #include "dvb_ca_en50221.h"
 #include "dvb_ringbuffer.h"
@@ -92,6 +93,9 @@ struct dvb_ca_slot {
        /* current state of the CAM */
        int slot_state;
 
+       /* mutex used for serializing access to one CI slot */
+       struct mutex slot_lock;
+
        /* Number of CAMCHANGES that have occurred since last processing */
        atomic_t camchange_count;
 
@@ -139,13 +143,7 @@ struct dvb_ca_private {
        wait_queue_head_t wait_queue;
 
        /* PID of the monitoring thread */
-       pid_t thread_pid;
-
-       /* Wait queue used when shutting thread down */
-       wait_queue_head_t thread_queue;
-
-       /* Flag indicating when thread should exit */
-       unsigned int exit:1;
+       struct task_struct *thread;
 
        /* Flag indicating if the CA device is open */
        unsigned int open:1;
@@ -255,7 +253,7 @@ static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
        unsigned long timeout;
        unsigned long start;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        /* loop until timeout elapsed */
        start = jiffies;
@@ -268,7 +266,7 @@ static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
 
                /* if we got the flags, it was successful! */
                if (res & waitfor) {
-                       dprintk("%s succeeded timeout:%lu\n", __FUNCTION__, jiffies - start);
+                       dprintk("%s succeeded timeout:%lu\n", __func__, jiffies - start);
                        return 0;
                }
 
@@ -281,7 +279,7 @@ static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
                msleep(1);
        }
 
-       dprintk("%s failed timeout:%lu\n", __FUNCTION__, jiffies - start);
+       dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
 
        /* if we get here, we've timed out */
        return -ETIMEDOUT;
@@ -302,7 +300,7 @@ static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
        int buf_size;
        u8 buf[2];
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        /* we'll be determining these during this function */
        ca->slot_info[slot].da_irq_supported = 0;
@@ -554,7 +552,7 @@ static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
 {
        int configoption;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        /* set the config option */
        ca->pub->write_attribute_mem(ca->pub, slot,
@@ -592,7 +590,7 @@ static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * eb
        u8 buf[HOST_LINK_BUF_SIZE];
        int i;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        /* check if we have space for a link buf in the rx_buffer */
        if (ebuf == NULL) {
@@ -713,17 +711,23 @@ static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * b
        int status;
        int i;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
 
-       // sanity check
+       /* sanity check */
        if (bytes_write > ca->slot_info[slot].link_buf_size)
                return -EINVAL;
 
-       /* check if interface is actually waiting for us to read from it, or if a read is in progress */
+       /* it is possible we are dealing with a single buffer implementation,
+          thus if there is data available for read or if there is even a read
+          already in progress, we do nothing but awake the kernel thread to
+          process the data if necessary. */
        if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
                goto exitnowrite;
        if (status & (STATUSREG_DA | STATUSREG_RE)) {
+               if (status & STATUSREG_DA)
+                       dvb_ca_en50221_thread_wakeup(ca);
+
                status = -EAGAIN;
                goto exitnowrite;
        }
@@ -790,7 +794,7 @@ EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
  */
 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
 {
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        ca->pub->slot_shutdown(ca->pub, slot);
        ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
@@ -897,31 +901,13 @@ void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
 {
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        ca->wakeup = 1;
        mb();
-       wake_up_interruptible(&ca->thread_queue);
-}
-
-/**
- * Used by the CA thread to determine if an early wakeup is necessary
- *
- * @param ca CA instance.
- */
-static int dvb_ca_en50221_thread_should_wakeup(struct dvb_ca_private *ca)
-{
-       if (ca->wakeup) {
-               ca->wakeup = 0;
-               return 1;
-       }
-       if (ca->exit)
-               return 1;
-
-       return 0;
+       wake_up_process(ca->thread);
 }
 
-
 /**
  * Update the delay used by the thread.
  *
@@ -933,15 +919,21 @@ static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
        int curdelay = 100000000;
        int slot;
 
+       /* Beware of too high polling frequency, because one polling
+        * call might take several hundred milliseconds until timeout!
+        */
        for (slot = 0; slot < ca->slot_count; slot++) {
                switch (ca->slot_info[slot].slot_state) {
                default:
                case DVB_CA_SLOTSTATE_NONE:
+                       delay = HZ * 60;  /* 60s */
+                       if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
+                               delay = HZ * 5;  /* 5s */
+                       break;
                case DVB_CA_SLOTSTATE_INVALID:
-                       delay = HZ * 60;
-                       if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
-                               delay = HZ / 10;
-                       }
+                       delay = HZ * 60;  /* 60s */
+                       if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
+                               delay = HZ / 10;  /* 100ms */
                        break;
 
                case DVB_CA_SLOTSTATE_UNINITIALISED:
@@ -949,19 +941,17 @@ static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
                case DVB_CA_SLOTSTATE_VALIDATE:
                case DVB_CA_SLOTSTATE_WAITFR:
                case DVB_CA_SLOTSTATE_LINKINIT:
-                       delay = HZ / 10;
+                       delay = HZ / 10;  /* 100ms */
                        break;
 
                case DVB_CA_SLOTSTATE_RUNNING:
-                       delay = HZ * 60;
-                       if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
-                               delay = HZ / 10;
-                       }
+                       delay = HZ * 60;  /* 60s */
+                       if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
+                               delay = HZ / 10;  /* 100ms */
                        if (ca->open) {
                                if ((!ca->slot_info[slot].da_irq_supported) ||
-                                   (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA))) {
-                                       delay = HZ / 10;
-                               }
+                                   (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
+                                       delay = HZ / 10;  /* 100ms */
                        }
                        break;
                }
@@ -981,43 +971,33 @@ static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
 static int dvb_ca_en50221_thread(void *data)
 {
        struct dvb_ca_private *ca = data;
-       char name[15];
        int slot;
        int flags;
        int status;
        int pktcount;
        void *rxbuf;
 
-       dprintk("%s\n", __FUNCTION__);
-
-       /* setup kernel thread */
-       snprintf(name, sizeof(name), "kdvb-ca-%i:%i", ca->dvbdev->adapter->num, ca->dvbdev->id);
-
-       lock_kernel();
-       daemonize(name);
-       sigfillset(&current->blocked);
-       unlock_kernel();
+       dprintk("%s\n", __func__);
 
        /* choose the correct initial delay */
        dvb_ca_en50221_thread_update_delay(ca);
 
        /* main loop */
-       while (!ca->exit) {
+       while (!kthread_should_stop()) {
                /* sleep for a bit */
                if (!ca->wakeup) {
-                       flags = wait_event_interruptible_timeout(ca->thread_queue,
-                                                                dvb_ca_en50221_thread_should_wakeup(ca),
-                                                                ca->delay);
-                       if ((flags == -ERESTARTSYS) || ca->exit) {
-                               /* got signal or quitting */
-                               break;
-                       }
+                       set_current_state(TASK_INTERRUPTIBLE);
+                       schedule_timeout(ca->delay);
+                       if (kthread_should_stop())
+                               return 0;
                }
                ca->wakeup = 0;
 
                /* go through all the slots processing them */
                for (slot = 0; slot < ca->slot_count; slot++) {
 
+                       mutex_lock(&ca->slot_info[slot].slot_lock);
+
                        // check the cam status + deal with CAMCHANGEs
                        while (dvb_ca_en50221_check_camstatus(ca, slot)) {
                                /* clear down an old CI slot if necessary */
@@ -1063,7 +1043,7 @@ static int dvb_ca_en50221_thread(void *data)
                                        /* we need this extra check for annoying interfaces like the budget-av */
                                        if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
                                            (ca->pub->poll_slot_status)) {
-                                               int status = ca->pub->poll_slot_status(ca->pub, slot, 0);
+                                               status = ca->pub->poll_slot_status(ca->pub, slot, 0);
                                                if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
                                                        ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
                                                        dvb_ca_en50221_thread_update_delay(ca);
@@ -1120,7 +1100,7 @@ static int dvb_ca_en50221_thread(void *data)
                                        /* we need this extra check for annoying interfaces like the budget-av */
                                        if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
                                            (ca->pub->poll_slot_status)) {
-                                               int status = ca->pub->poll_slot_status(ca->pub, slot, 0);
+                                               status = ca->pub->poll_slot_status(ca->pub, slot, 0);
                                                if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
                                                        ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
                                                        dvb_ca_en50221_thread_update_delay(ca);
@@ -1153,7 +1133,7 @@ static int dvb_ca_en50221_thread(void *data)
 
                        case DVB_CA_SLOTSTATE_RUNNING:
                                if (!ca->open)
-                                       continue;
+                                       break;
 
                                // poll slots for data
                                pktcount = 0;
@@ -1177,13 +1157,11 @@ static int dvb_ca_en50221_thread(void *data)
                                }
                                break;
                        }
+
+                       mutex_unlock(&ca->slot_info[slot].slot_lock);
                }
        }
 
-       /* completed */
-       ca->thread_pid = 0;
-       mb();
-       wake_up_interruptible(&ca->thread_queue);
        return 0;
 }
 
@@ -1211,11 +1189,12 @@ static int dvb_ca_en50221_io_do_ioctl(struct inode *inode, struct file *file,
        int err = 0;
        int slot;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        switch (cmd) {
        case CA_RESET:
                for (slot = 0; slot < ca->slot_count; slot++) {
+                       mutex_lock(&ca->slot_info[slot].slot_lock);
                        if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
                                dvb_ca_en50221_slot_shutdown(ca, slot);
                                if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
@@ -1223,6 +1202,7 @@ static int dvb_ca_en50221_io_do_ioctl(struct inode *inode, struct file *file,
                                                                     slot,
                                                                     DVB_CA_EN50221_CAMCHANGE_INSERTED);
                        }
+                       mutex_unlock(&ca->slot_info[slot].slot_lock);
                }
                ca->next_read_slot = 0;
                dvb_ca_en50221_thread_wakeup(ca);
@@ -1305,7 +1285,7 @@ static ssize_t dvb_ca_en50221_io_write(struct file *file,
        unsigned long timeout;
        int written;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
        if (count < 2)
@@ -1343,7 +1323,9 @@ static ssize_t dvb_ca_en50221_io_write(struct file *file,
                                goto exit;
                        }
 
+                       mutex_lock(&ca->slot_info[slot].slot_lock);
                        status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
+                       mutex_unlock(&ca->slot_info[slot].slot_lock);
                        if (status == (fraglen + 2)) {
                                written = 1;
                                break;
@@ -1392,7 +1374,7 @@ static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
 
                idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
                while (idx != -1) {
-                       dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2, 0);
+                       dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
                        if (connection_id == -1)
                                connection_id = hdr[0];
                        if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
@@ -1440,7 +1422,7 @@ static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
        int pktlen;
        int dispose = 0;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
        if (count < 2)
@@ -1473,7 +1455,7 @@ static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
                        goto exit;
                }
 
-               dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2, 0);
+               dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
                if (connection_id == -1)
                        connection_id = hdr[0];
                if (hdr[0] == connection_id) {
@@ -1484,8 +1466,8 @@ static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
                                        fraglen -= 2;
                                }
 
-                               if ((status = dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 2,
-                                                                     (u8 *)buf + pktlen, fraglen, 1)) < 0) {
+                               if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
+                                                                     buf + pktlen, fraglen)) < 0) {
                                        goto exit;
                                }
                                pktlen += fraglen;
@@ -1529,14 +1511,16 @@ static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
        int err;
        int i;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        if (!try_module_get(ca->pub->owner))
                return -EIO;
 
        err = dvb_generic_open(inode, file);
-       if (err < 0)
+       if (err < 0) {
+               module_put(ca->pub->owner);
                return err;
+       }
 
        for (i = 0; i < ca->slot_count; i++) {
 
@@ -1571,7 +1555,7 @@ static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
        struct dvb_ca_private *ca = dvbdev->priv;
        int err;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        /* mark the CA device as closed */
        ca->open = 0;
@@ -1601,7 +1585,7 @@ static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
        int slot;
        int result = 0;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
                mask |= POLLIN;
@@ -1623,7 +1607,7 @@ static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
 EXPORT_SYMBOL(dvb_ca_en50221_init);
 
 
-static struct file_operations dvb_ca_fops = {
+static const struct file_operations dvb_ca_fops = {
        .owner = THIS_MODULE,
        .read = dvb_ca_en50221_io_read,
        .write = dvb_ca_en50221_io_write,
@@ -1663,7 +1647,7 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
        struct dvb_ca_private *ca = NULL;
        int i;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        if (slot_count < 1)
                return -EINVAL;
@@ -1681,9 +1665,6 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
                goto error;
        }
        init_waitqueue_head(&ca->wait_queue);
-       ca->thread_pid = 0;
-       init_waitqueue_head(&ca->thread_queue);
-       ca->exit = 0;
        ca->open = 0;
        ca->wakeup = 0;
        ca->next_read_slot = 0;
@@ -1700,6 +1681,7 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
                ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
                atomic_set(&ca->slot_info[i].camchange_count, 0);
                ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
+               mutex_init(&ca->slot_info[i].slot_lock);
        }
 
        if (signal_pending(current)) {
@@ -1709,14 +1691,14 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
        mb();
 
        /* create a kthread for monitoring this CA device */
-
-       ret = kernel_thread(dvb_ca_en50221_thread, ca, 0);
-
-       if (ret < 0) {
-               printk("dvb_ca_init: failed to start kernel_thread (%d)\n", ret);
+       ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
+                                ca->dvbdev->adapter->num, ca->dvbdev->id);
+       if (IS_ERR(ca->thread)) {
+               ret = PTR_ERR(ca->thread);
+               printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
+                       ret);
                goto error;
        }
-       ca->thread_pid = ret;
        return 0;
 
 error:
@@ -1744,20 +1726,10 @@ void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
        struct dvb_ca_private *ca = pubca->private;
        int i;
 
-       dprintk("%s\n", __FUNCTION__);
+       dprintk("%s\n", __func__);
 
        /* shutdown the thread if there was one */
-       if (ca->thread_pid) {
-               if (kill_proc(ca->thread_pid, 0, 1) == -ESRCH) {
-                       printk("dvb_ca_release adapter %d: thread PID %d already died\n",
-                              ca->dvbdev->adapter->num, ca->thread_pid);
-               } else {
-                       ca->exit = 1;
-                       mb();
-                       dvb_ca_en50221_thread_wakeup(ca);
-                       wait_event_interruptible(ca->thread_queue, ca->thread_pid == 0);
-               }
-       }
+       kthread_stop(ca->thread);
 
        for (i = 0; i < ca->slot_count; i++) {
                dvb_ca_en50221_slot_shutdown(ca, i);