Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / media / dvb / dvb-core / dvb_ca_en50221.c
index 0eb9aa7..00347a7 100644 (file)
@@ -35,7 +35,8 @@
 #include <linux/moduleparam.h>
 #include <linux/vmalloc.h>
 #include <linux/delay.h>
-#include <linux/rwsem.h>
+#include <linux/spinlock.h>
+#include <linux/sched.h>
 
 #include "dvb_ca_en50221.h"
 #include "dvb_ringbuffer.h"
@@ -47,7 +48,7 @@ MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
 
 #define dprintk if (dvb_ca_en50221_debug) printk
 
-#define INIT_TIMEOUT_SECS 5
+#define INIT_TIMEOUT_SECS 10
 
 #define HOST_LINK_BUF_SIZE 0x200
 
@@ -110,9 +111,6 @@ struct dvb_ca_slot {
        /* size of the buffer to use when talking to the CAM */
        int link_buf_size;
 
-       /* semaphore for syncing access to slot structure */
-       struct rw_semaphore sem;
-
        /* buffer for incoming packets */
        struct dvb_ringbuffer rx_buffer;
 
@@ -500,7 +498,7 @@ static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
        /* process the CFTABLE_ENTRY tuples, and any after those */
        while ((!end_chain) && (address < 0x1000)) {
                if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
-                                                       &tupleLength, tuple)) < 0)
+                                                       &tupleLength, tuple)) < 0)
                        return status;
                switch (tupleType) {
                case 0x1B:      // CISTPL_CFTABLE_ENTRY
@@ -601,14 +599,11 @@ static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * eb
        if (ebuf == NULL) {
                int buf_free;
 
-               down_read(&ca->slot_info[slot].sem);
                if (ca->slot_info[slot].rx_buffer.data == NULL) {
-                       up_read(&ca->slot_info[slot].sem);
                        status = -EIO;
                        goto exit;
                }
                buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
-               up_read(&ca->slot_info[slot].sem);
 
                if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
                        status = -EAGAIN;
@@ -679,14 +674,11 @@ static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * eb
 
        /* OK, add it to the receive buffer, or copy into external buffer if supplied */
        if (ebuf == NULL) {
-               down_read(&ca->slot_info[slot].sem);
                if (ca->slot_info[slot].rx_buffer.data == NULL) {
-                       up_read(&ca->slot_info[slot].sem);
                        status = -EIO;
                        goto exit;
                }
                dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
-               up_read(&ca->slot_info[slot].sem);
        } else {
                memcpy(ebuf, buf, bytes_read);
        }
@@ -801,12 +793,8 @@ static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
 {
        dprintk("%s\n", __FUNCTION__);
 
-       down_write(&ca->slot_info[slot].sem);
        ca->pub->slot_shutdown(ca->pub, slot);
        ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
-       vfree(ca->slot_info[slot].rx_buffer.data);
-       ca->slot_info[slot].rx_buffer.data = NULL;
-       up_write(&ca->slot_info[slot].sem);
 
        /* need to wake up all processes to check if they're now
           trying to write to a defunct CAM */
@@ -892,7 +880,7 @@ void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
 
        case DVB_CA_SLOTSTATE_RUNNING:
                if (ca->open)
-                       dvb_ca_en50221_read_data(ca, slot, NULL, 0);
+                       dvb_ca_en50221_thread_wakeup(ca);
                break;
        }
 }
@@ -1126,16 +1114,16 @@ static int dvb_ca_en50221_thread(void *data)
                                        break;
                                }
 
-                               rxbuf = vmalloc(RX_BUFFER_SIZE);
-                               if (rxbuf == NULL) {
-                                       printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
-                                       ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
-                                       dvb_ca_en50221_thread_update_delay(ca);
-                                       break;
+                               if (ca->slot_info[slot].rx_buffer.data == NULL) {
+                                       rxbuf = vmalloc(RX_BUFFER_SIZE);
+                                       if (rxbuf == NULL) {
+                                               printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
+                                               ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
+                                               dvb_ca_en50221_thread_update_delay(ca);
+                                               break;
+                                       }
+                                       dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
                                }
-                               down_write(&ca->slot_info[slot].sem);
-                               dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
-                               up_write(&ca->slot_info[slot].sem);
 
                                ca->pub->slot_ts_enable(ca->pub, slot);
                                ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
@@ -1147,11 +1135,7 @@ static int dvb_ca_en50221_thread(void *data)
                                if (!ca->open)
                                        continue;
 
-                               // no need to poll if the CAM supports IRQs
-                               if (ca->slot_info[slot].da_irq_supported)
-                                       break;
-
-                               // poll mode
+                               // poll slots for data
                                pktcount = 0;
                                while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
                                        if (!ca->open)
@@ -1366,12 +1350,13 @@ exit:
 /**
  * Condition for waking up in dvb_ca_en50221_io_read_condition
  */
-static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca, int *result, int *_slot)
+static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
+                                           int *result, int *_slot)
 {
        int slot;
        int slot_count = 0;
        int idx;
-       int fraglen;
+       size_t fraglen;
        int connection_id = -1;
        int found = 0;
        u8 hdr[2];
@@ -1381,10 +1366,7 @@ static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca, int *resu
                if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
                        goto nextslot;
 
-               down_read(&ca->slot_info[slot].sem);
-
                if (ca->slot_info[slot].rx_buffer.data == NULL) {
-                       up_read(&ca->slot_info[slot].sem);
                        return 0;
                }
 
@@ -1402,10 +1384,7 @@ static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca, int *resu
                        idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
                }
 
-               if (!found)
-                       up_read(&ca->slot_info[slot].sem);
-
-             nextslot:
+nextslot:
                slot = (slot + 1) % ca->slot_count;
                slot_count++;
        }
@@ -1510,8 +1489,7 @@ static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
                goto exit;
        status = pktlen;
 
-      exit:
-       up_read(&ca->slot_info[slot].sem);
+exit:
        return status;
 }
 
@@ -1543,11 +1521,11 @@ static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
        for (i = 0; i < ca->slot_count; i++) {
 
                if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
-                       down_write(&ca->slot_info[i].sem);
                        if (ca->slot_info[i].rx_buffer.data != NULL) {
+                               /* it is safe to call this here without locks because
+                                * ca->open == 0. Data is not read in this case */
                                dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
                        }
-                       up_write(&ca->slot_info[i].sem);
                }
        }
 
@@ -1606,7 +1584,6 @@ static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
        dprintk("%s\n", __FUNCTION__);
 
        if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
-               up_read(&ca->slot_info[slot].sem);
                mask |= POLLIN;
        }
 
@@ -1618,7 +1595,6 @@ static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
        poll_wait(file, &ca->wait_queue, wait);
 
        if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
-               up_read(&ca->slot_info[slot].sem);
                mask |= POLLIN;
        }
 
@@ -1673,21 +1649,17 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
                return -EINVAL;
 
        /* initialise the system data */
-       if ((ca =
-            (struct dvb_ca_private *) kmalloc(sizeof(struct dvb_ca_private),
-                                              GFP_KERNEL)) == NULL) {
+       if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
                ret = -ENOMEM;
                goto error;
        }
-       memset(ca, 0, sizeof(struct dvb_ca_private));
        ca->pub = pubca;
        ca->flags = flags;
        ca->slot_count = slot_count;
-       if ((ca->slot_info = kmalloc(sizeof(struct dvb_ca_slot) * slot_count, GFP_KERNEL)) == NULL) {
+       if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
                ret = -ENOMEM;
                goto error;
        }
-       memset(ca->slot_info, 0, sizeof(struct dvb_ca_slot) * slot_count);
        init_waitqueue_head(&ca->wait_queue);
        ca->thread_pid = 0;
        init_waitqueue_head(&ca->thread_queue);
@@ -1708,7 +1680,6 @@ 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;
-               init_rwsem(&ca->slot_info[i].sem);
        }
 
        if (signal_pending(current)) {
@@ -1728,7 +1699,7 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
        ca->thread_pid = ret;
        return 0;
 
-      error:
+error:
        if (ca != NULL) {
                if (ca->dvbdev != NULL)
                        dvb_unregister_device(ca->dvbdev);
@@ -1770,6 +1741,7 @@ void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
 
        for (i = 0; i < ca->slot_count; i++) {
                dvb_ca_en50221_slot_shutdown(ca, i);
+               vfree(ca->slot_info[i].rx_buffer.data);
        }
        kfree(ca->slot_info);
        dvb_unregister_device(ca->dvbdev);