2  * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces
 
   4  * Copyright (C) 2004 Andrew de Quincey
 
   6  * Parts of this file were based on sources as follows:
 
   8  * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de>
 
  12  * Copyright (C) 1999-2002 Ralph  Metzler
 
  13  *                       & Marcus Metzler for convergence integrated media GmbH
 
  15  * This program is free software; you can redistribute it and/or
 
  16  * modify it under the terms of the GNU General Public License
 
  17  * as published by the Free Software Foundation; either version 2
 
  18  * of the License, or (at your option) any later version.
 
  20  * This program is distributed in the hope that it will be useful,
 
  21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  23  * GNU General Public License for more details.
 
  25  * You should have received a copy of the GNU General Public License
 
  26  * along with this program; if not, write to the Free Software
 
  27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
  28  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 
  31 #include <linux/errno.h>
 
  32 #include <linux/slab.h>
 
  33 #include <linux/list.h>
 
  34 #include <linux/module.h>
 
  35 #include <linux/vmalloc.h>
 
  36 #include <linux/delay.h>
 
  37 #include <linux/spinlock.h>
 
  38 #include <linux/sched.h>
 
  39 #include <linux/kthread.h>
 
  41 #include "dvb_ca_en50221.h"
 
  42 #include "dvb_ringbuffer.h"
 
  44 static int dvb_ca_en50221_debug;
 
  46 module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
 
  47 MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
 
  49 #define dprintk if (dvb_ca_en50221_debug) printk
 
  51 #define INIT_TIMEOUT_SECS 10
 
  53 #define HOST_LINK_BUF_SIZE 0x200
 
  55 #define RX_BUFFER_SIZE 65535
 
  57 #define MAX_RX_PACKETS_PER_ITERATION 10
 
  60 #define CTRLIF_COMMAND   1
 
  61 #define CTRLIF_STATUS    1
 
  62 #define CTRLIF_SIZE_LOW  2
 
  63 #define CTRLIF_SIZE_HIGH 3
 
  65 #define CMDREG_HC        1      /* Host control */
 
  66 #define CMDREG_SW        2      /* Size write */
 
  67 #define CMDREG_SR        4      /* Size read */
 
  68 #define CMDREG_RS        8      /* Reset interface */
 
  69 #define CMDREG_FRIE   0x40      /* Enable FR interrupt */
 
  70 #define CMDREG_DAIE   0x80      /* Enable DA interrupt */
 
  71 #define IRQEN (CMDREG_DAIE)
 
  73 #define STATUSREG_RE     1      /* read error */
 
  74 #define STATUSREG_WE     2      /* write error */
 
  75 #define STATUSREG_FR  0x40      /* module free */
 
  76 #define STATUSREG_DA  0x80      /* data available */
 
  77 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE)     /* general transfer error */
 
  80 #define DVB_CA_SLOTSTATE_NONE           0
 
  81 #define DVB_CA_SLOTSTATE_UNINITIALISED  1
 
  82 #define DVB_CA_SLOTSTATE_RUNNING        2
 
  83 #define DVB_CA_SLOTSTATE_INVALID        3
 
  84 #define DVB_CA_SLOTSTATE_WAITREADY      4
 
  85 #define DVB_CA_SLOTSTATE_VALIDATE       5
 
  86 #define DVB_CA_SLOTSTATE_WAITFR         6
 
  87 #define DVB_CA_SLOTSTATE_LINKINIT       7
 
  90 /* Information on a CA slot */
 
  93         /* current state of the CAM */
 
  96         /* Number of CAMCHANGES that have occurred since last processing */
 
  97         atomic_t camchange_count;
 
  99         /* Type of last CAMCHANGE */
 
 102         /* base address of CAM config */
 
 105         /* value to write into Config Control register */
 
 108         /* if 1, the CAM supports DA IRQs */
 
 109         u8 da_irq_supported:1;
 
 111         /* size of the buffer to use when talking to the CAM */
 
 114         /* buffer for incoming packets */
 
 115         struct dvb_ringbuffer rx_buffer;
 
 117         /* timer used during various states of the slot */
 
 118         unsigned long timeout;
 
 121 /* Private CA-interface information */
 
 122 struct dvb_ca_private {
 
 124         /* pointer back to the public data structure */
 
 125         struct dvb_ca_en50221 *pub;
 
 128         struct dvb_device *dvbdev;
 
 130         /* Flags describing the interface (DVB_CA_FLAG_*) */
 
 133         /* number of slots supported by this CA interface */
 
 134         unsigned int slot_count;
 
 136         /* information on each slot */
 
 137         struct dvb_ca_slot *slot_info;
 
 139         /* wait queues for read() and write() operations */
 
 140         wait_queue_head_t wait_queue;
 
 142         /* PID of the monitoring thread */
 
 143         struct task_struct *thread;
 
 145         /* Flag indicating if the CA device is open */
 
 148         /* Flag indicating the thread should wake up now */
 
 149         unsigned int wakeup:1;
 
 151         /* Delay the main thread should use */
 
 154         /* Slot to start looking for data to read from in the next user-space read operation */
 
 158 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
 
 159 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
 
 160 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
 
 164  * Safely find needle in haystack.
 
 166  * @param haystack Buffer to look in.
 
 167  * @param hlen Number of bytes in haystack.
 
 168  * @param needle Buffer to find.
 
 169  * @param nlen Number of bytes in needle.
 
 170  * @return Pointer into haystack needle was found at, or NULL if not found.
 
 172 static char *findstr(char * haystack, int hlen, char * needle, int nlen)
 
 179         for (i = 0; i <= hlen - nlen; i++) {
 
 180                 if (!strncmp(haystack + i, needle, nlen))
 
 189 /* ******************************************************************************** */
 
 190 /* EN50221 physical interface functions */
 
 196 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
 
 203         if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
 
 204                 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
 
 208         slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
 
 210         cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
 
 211         cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
 
 213                 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
 
 214                 cam_changed = (cam_present_now != cam_present_old);
 
 218                 if (!cam_present_now) {
 
 219                         ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
 
 221                         ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
 
 223                 atomic_set(&ca->slot_info[slot].camchange_count, 1);
 
 225                 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
 
 226                     (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
 
 227                         // move to validate state if reset is completed
 
 228                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
 
 237  * Wait for flags to become set on the STATUS register on a CAM interface,
 
 238  * checking for errors and timeout.
 
 240  * @param ca CA instance.
 
 241  * @param slot Slot on interface.
 
 242  * @param waitfor Flags to wait for.
 
 243  * @param timeout_ms Timeout in milliseconds.
 
 245  * @return 0 on success, nonzero on error.
 
 247 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
 
 248                                          u8 waitfor, int timeout_hz)
 
 250         unsigned long timeout;
 
 253         dprintk("%s\n", __func__);
 
 255         /* loop until timeout elapsed */
 
 257         timeout = jiffies + timeout_hz;
 
 259                 /* read the status and check for error */
 
 260                 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
 
 264                 /* if we got the flags, it was successful! */
 
 266                         dprintk("%s succeeded timeout:%lu\n", __func__, jiffies - start);
 
 270                 /* check for timeout */
 
 271                 if (time_after(jiffies, timeout)) {
 
 279         dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
 
 281         /* if we get here, we've timed out */
 
 287  * Initialise the link layer connection to a CAM.
 
 289  * @param ca CA instance.
 
 290  * @param slot Slot id.
 
 292  * @return 0 on success, nonzero on failure.
 
 294 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
 
 300         dprintk("%s\n", __func__);
 
 302         /* we'll be determining these during this function */
 
 303         ca->slot_info[slot].da_irq_supported = 0;
 
 305         /* set the host link buffer size temporarily. it will be overwritten with the
 
 306          * real negotiated size later. */
 
 307         ca->slot_info[slot].link_buf_size = 2;
 
 309         /* read the buffer size from the CAM */
 
 310         if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
 
 312         if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
 
 314         if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
 
 316         if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
 
 319         /* store it, and choose the minimum of our buffer and the CAM's buffer size */
 
 320         buf_size = (buf[0] << 8) | buf[1];
 
 321         if (buf_size > HOST_LINK_BUF_SIZE)
 
 322                 buf_size = HOST_LINK_BUF_SIZE;
 
 323         ca->slot_info[slot].link_buf_size = buf_size;
 
 324         buf[0] = buf_size >> 8;
 
 325         buf[1] = buf_size & 0xff;
 
 326         dprintk("Chosen link buffer size of %i\n", buf_size);
 
 328         /* write the buffer size to the CAM */
 
 329         if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
 
 331         if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
 
 333         if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
 
 335         if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
 
 343  * Read a tuple from attribute memory.
 
 345  * @param ca CA instance.
 
 346  * @param slot Slot id.
 
 347  * @param address Address to read from. Updated.
 
 348  * @param tupleType Tuple id byte. Updated.
 
 349  * @param tupleLength Tuple length. Updated.
 
 350  * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
 
 352  * @return 0 on success, nonzero on error.
 
 354 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
 
 355                                      int *address, int *tupleType, int *tupleLength, u8 * tuple)
 
 360         int _address = *address;
 
 362         /* grab the next tuple length and type */
 
 363         if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
 
 365         if (_tupleType == 0xff) {
 
 366                 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
 
 368                 *tupleType = _tupleType;
 
 372         if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
 
 376         dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
 
 378         /* read in the whole tuple */
 
 379         for (i = 0; i < _tupleLength; i++) {
 
 380                 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2));
 
 381                 dprintk("  0x%02x: 0x%02x %c\n",
 
 383                         ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
 
 385         _address += (_tupleLength * 2);
 
 388         *tupleType = _tupleType;
 
 389         *tupleLength = _tupleLength;
 
 396  * Parse attribute memory of a CAM module, extracting Config register, and checking
 
 397  * it is a DVB CAM module.
 
 399  * @param ca CA instance.
 
 400  * @param slot Slot id.
 
 402  * @return 0 on success, <0 on failure.
 
 404 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
 
 413         int got_cftableentry = 0;
 
 422              dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
 
 424         if (tupleType != 0x1D)
 
 431              dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
 
 433         if (tupleType != 0x1C)
 
 440              dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
 
 442         if (tupleType != 0x15)
 
 448         if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
 
 449                                                 &tupleLength, tuple)) < 0)
 
 451         if (tupleType != 0x20)
 
 453         if (tupleLength != 4)
 
 455         manfid = (tuple[1] << 8) | tuple[0];
 
 456         devid = (tuple[3] << 8) | tuple[2];
 
 461         if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
 
 462                                                 &tupleLength, tuple)) < 0)
 
 464         if (tupleType != 0x1A)
 
 469         /* extract the configbase */
 
 471         if (tupleLength < (3 + rasz + 14))
 
 473         ca->slot_info[slot].config_base = 0;
 
 474         for (i = 0; i < rasz + 1; i++) {
 
 475                 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i));
 
 478         /* check it contains the correct DVB string */
 
 479         dvb_str = findstr((char *)tuple, tupleLength, "DVB_CI_V", 8);
 
 482         if (tupleLength < ((dvb_str - (char *) tuple) + 12))
 
 485         /* is it a version we support? */
 
 486         if (strncmp(dvb_str + 8, "1.00", 4)) {
 
 487                 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
 
 488                        ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]);
 
 492         /* process the CFTABLE_ENTRY tuples, and any after those */
 
 493         while ((!end_chain) && (address < 0x1000)) {
 
 494                 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
 
 495                                                         &tupleLength, tuple)) < 0)
 
 498                 case 0x1B:      // CISTPL_CFTABLE_ENTRY
 
 499                         if (tupleLength < (2 + 11 + 17))
 
 502                         /* if we've already parsed one, just use it */
 
 503                         if (got_cftableentry)
 
 506                         /* get the config option */
 
 507                         ca->slot_info[slot].config_option = tuple[0] & 0x3f;
 
 509                         /* OK, check it contains the correct strings */
 
 510                         if ((findstr((char *)tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
 
 511                             (findstr((char *)tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
 
 514                         got_cftableentry = 1;
 
 517                 case 0x14:      // CISTPL_NO_LINK
 
 520                 case 0xFF:      // CISTPL_END
 
 524                 default:        /* Unknown tuple type - just skip this tuple and move to the next one */
 
 525                         dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType,
 
 531         if ((address > 0x1000) || (!got_cftableentry))
 
 534         dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
 
 535                 manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option);
 
 543  * Set CAM's configoption correctly.
 
 545  * @param ca CA instance.
 
 546  * @param slot Slot containing the CAM.
 
 548 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
 
 552         dprintk("%s\n", __func__);
 
 554         /* set the config option */
 
 555         ca->pub->write_attribute_mem(ca->pub, slot,
 
 556                                      ca->slot_info[slot].config_base,
 
 557                                      ca->slot_info[slot].config_option);
 
 560         configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
 
 561         dprintk("Set configoption 0x%x, read configoption 0x%x\n",
 
 562                 ca->slot_info[slot].config_option, configoption & 0x3f);
 
 571  * This function talks to an EN50221 CAM control interface. It reads a buffer of
 
 572  * data from the CAM. The data can either be stored in a supplied buffer, or
 
 573  * automatically be added to the slot's rx_buffer.
 
 575  * @param ca CA instance.
 
 576  * @param slot Slot to read from.
 
 577  * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
 
 578  * the data will be added into the buffering system as a normal fragment.
 
 579  * @param ecount Size of ebuf. Ignored if ebuf is NULL.
 
 581  * @return Number of bytes read, or < 0 on error
 
 583 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount)
 
 587         u8 buf[HOST_LINK_BUF_SIZE];
 
 590         dprintk("%s\n", __func__);
 
 592         /* check if we have space for a link buf in the rx_buffer */
 
 596                 if (ca->slot_info[slot].rx_buffer.data == NULL) {
 
 600                 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
 
 602                 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
 
 608         /* check if there is data available */
 
 609         if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
 
 611         if (!(status & STATUSREG_DA)) {
 
 617         /* read the amount of data */
 
 618         if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
 
 620         bytes_read = status << 8;
 
 621         if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
 
 623         bytes_read |= status;
 
 625         /* check it will fit */
 
 627                 if (bytes_read > ca->slot_info[slot].link_buf_size) {
 
 628                         printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
 
 629                                ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size);
 
 630                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
 
 634                 if (bytes_read < 2) {
 
 635                         printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
 
 636                                ca->dvbdev->adapter->num);
 
 637                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
 
 642                 if (bytes_read > ecount) {
 
 643                         printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
 
 644                                ca->dvbdev->adapter->num);
 
 650         /* fill the buffer */
 
 651         for (i = 0; i < bytes_read; i++) {
 
 652                 /* read byte and check */
 
 653                 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
 
 656                 /* OK, store it in the buffer */
 
 660         /* check for read error (RE should now be 0) */
 
 661         if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
 
 663         if (status & STATUSREG_RE) {
 
 664                 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
 
 669         /* OK, add it to the receive buffer, or copy into external buffer if supplied */
 
 671                 if (ca->slot_info[slot].rx_buffer.data == NULL) {
 
 675                 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
 
 677                 memcpy(ebuf, buf, bytes_read);
 
 680         dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
 
 681                 buf[0], (buf[1] & 0x80) == 0, bytes_read);
 
 683         /* wake up readers when a last_fragment is received */
 
 684         if ((buf[1] & 0x80) == 0x00) {
 
 685                 wake_up_interruptible(&ca->wait_queue);
 
 695  * This function talks to an EN50221 CAM control interface. It writes a buffer of data
 
 698  * @param ca CA instance.
 
 699  * @param slot Slot to write to.
 
 700  * @param ebuf The data in this buffer is treated as a complete link-level packet to
 
 702  * @param count Size of ebuf.
 
 704  * @return Number of bytes written, or < 0 on error.
 
 706 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write)
 
 711         dprintk("%s\n", __func__);
 
 715         if (bytes_write > ca->slot_info[slot].link_buf_size)
 
 718         /* check if interface is actually waiting for us to read from it, or if a read is in progress */
 
 719         if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
 
 721         if (status & (STATUSREG_DA | STATUSREG_RE)) {
 
 727         if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
 
 728                                                  IRQEN | CMDREG_HC)) != 0)
 
 731         /* check if interface is still free */
 
 732         if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
 
 734         if (!(status & STATUSREG_FR)) {
 
 735                 /* it wasn't free => try again later */
 
 740         /* send the amount of data */
 
 741         if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
 
 743         if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
 
 744                                                  bytes_write & 0xff)) != 0)
 
 747         /* send the buffer */
 
 748         for (i = 0; i < bytes_write; i++) {
 
 749                 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
 
 753         /* check for write error (WE should now be 0) */
 
 754         if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
 
 756         if (status & STATUSREG_WE) {
 
 757                 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
 
 761         status = bytes_write;
 
 763         dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
 
 764                 buf[0], (buf[1] & 0x80) == 0, bytes_write);
 
 767         ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
 
 772 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
 
 776 /* ******************************************************************************** */
 
 777 /* EN50221 higher level functions */
 
 781  * A CAM has been removed => shut it down.
 
 783  * @param ca CA instance.
 
 784  * @param slot Slot to shut down.
 
 786 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
 
 788         dprintk("%s\n", __func__);
 
 790         ca->pub->slot_shutdown(ca->pub, slot);
 
 791         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
 
 793         /* need to wake up all processes to check if they're now
 
 794            trying to write to a defunct CAM */
 
 795         wake_up_interruptible(&ca->wait_queue);
 
 797         dprintk("Slot %i shutdown\n", slot);
 
 802 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
 
 806  * A CAMCHANGE IRQ has occurred.
 
 808  * @param ca CA instance.
 
 809  * @param slot Slot concerned.
 
 810  * @param change_type One of the DVB_CA_CAMCHANGE_* values.
 
 812 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
 
 814         struct dvb_ca_private *ca = pubca->private;
 
 816         dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
 
 818         switch (change_type) {
 
 819         case DVB_CA_EN50221_CAMCHANGE_REMOVED:
 
 820         case DVB_CA_EN50221_CAMCHANGE_INSERTED:
 
 827         ca->slot_info[slot].camchange_type = change_type;
 
 828         atomic_inc(&ca->slot_info[slot].camchange_count);
 
 829         dvb_ca_en50221_thread_wakeup(ca);
 
 831 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
 
 835  * A CAMREADY IRQ has occurred.
 
 837  * @param ca CA instance.
 
 838  * @param slot Slot concerned.
 
 840 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
 
 842         struct dvb_ca_private *ca = pubca->private;
 
 844         dprintk("CAMREADY IRQ slot:%i\n", slot);
 
 846         if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
 
 847                 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
 
 848                 dvb_ca_en50221_thread_wakeup(ca);
 
 854  * An FR or DA IRQ has occurred.
 
 856  * @param ca CA instance.
 
 857  * @param slot Slot concerned.
 
 859 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
 
 861         struct dvb_ca_private *ca = pubca->private;
 
 864         dprintk("FR/DA IRQ slot:%i\n", slot);
 
 866         switch (ca->slot_info[slot].slot_state) {
 
 867         case DVB_CA_SLOTSTATE_LINKINIT:
 
 868                 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
 
 869                 if (flags & STATUSREG_DA) {
 
 870                         dprintk("CAM supports DA IRQ\n");
 
 871                         ca->slot_info[slot].da_irq_supported = 1;
 
 875         case DVB_CA_SLOTSTATE_RUNNING:
 
 877                         dvb_ca_en50221_thread_wakeup(ca);
 
 884 /* ******************************************************************************** */
 
 885 /* EN50221 thread functions */
 
 888  * Wake up the DVB CA thread
 
 890  * @param ca CA instance.
 
 892 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
 
 895         dprintk("%s\n", __func__);
 
 899         wake_up_process(ca->thread);
 
 903  * Update the delay used by the thread.
 
 905  * @param ca CA instance.
 
 907 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
 
 910         int curdelay = 100000000;
 
 913         /* Beware of too high polling frequency, because one polling
 
 914          * call might take several hundred milliseconds until timeout!
 
 916         for (slot = 0; slot < ca->slot_count; slot++) {
 
 917                 switch (ca->slot_info[slot].slot_state) {
 
 919                 case DVB_CA_SLOTSTATE_NONE:
 
 920                         delay = HZ * 60;  /* 60s */
 
 921                         if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
 
 922                                 delay = HZ * 5;  /* 5s */
 
 924                 case DVB_CA_SLOTSTATE_INVALID:
 
 925                         delay = HZ * 60;  /* 60s */
 
 926                         if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
 
 927                                 delay = HZ / 10;  /* 100ms */
 
 930                 case DVB_CA_SLOTSTATE_UNINITIALISED:
 
 931                 case DVB_CA_SLOTSTATE_WAITREADY:
 
 932                 case DVB_CA_SLOTSTATE_VALIDATE:
 
 933                 case DVB_CA_SLOTSTATE_WAITFR:
 
 934                 case DVB_CA_SLOTSTATE_LINKINIT:
 
 935                         delay = HZ / 10;  /* 100ms */
 
 938                 case DVB_CA_SLOTSTATE_RUNNING:
 
 939                         delay = HZ * 60;  /* 60s */
 
 940                         if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
 
 941                                 delay = HZ / 10;  /* 100ms */
 
 943                                 if ((!ca->slot_info[slot].da_irq_supported) ||
 
 944                                     (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
 
 945                                         delay = HZ / 10;  /* 100ms */
 
 950                 if (delay < curdelay)
 
 954         ca->delay = curdelay;
 
 960  * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
 
 962 static int dvb_ca_en50221_thread(void *data)
 
 964         struct dvb_ca_private *ca = data;
 
 971         dprintk("%s\n", __func__);
 
 973         /* choose the correct initial delay */
 
 974         dvb_ca_en50221_thread_update_delay(ca);
 
 977         while (!kthread_should_stop()) {
 
 978                 /* sleep for a bit */
 
 980                         set_current_state(TASK_INTERRUPTIBLE);
 
 981                         schedule_timeout(ca->delay);
 
 982                         if (kthread_should_stop())
 
 987                 /* go through all the slots processing them */
 
 988                 for (slot = 0; slot < ca->slot_count; slot++) {
 
 990                         // check the cam status + deal with CAMCHANGEs
 
 991                         while (dvb_ca_en50221_check_camstatus(ca, slot)) {
 
 992                                 /* clear down an old CI slot if necessary */
 
 993                                 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
 
 994                                         dvb_ca_en50221_slot_shutdown(ca, slot);
 
 996                                 /* if a CAM is NOW present, initialise it */
 
 997                                 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
 
 998                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
 
1001                                 /* we've handled one CAMCHANGE */
 
1002                                 dvb_ca_en50221_thread_update_delay(ca);
 
1003                                 atomic_dec(&ca->slot_info[slot].camchange_count);
 
1006                         // CAM state machine
 
1007                         switch (ca->slot_info[slot].slot_state) {
 
1008                         case DVB_CA_SLOTSTATE_NONE:
 
1009                         case DVB_CA_SLOTSTATE_INVALID:
 
1013                         case DVB_CA_SLOTSTATE_UNINITIALISED:
 
1014                                 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
 
1015                                 ca->pub->slot_reset(ca->pub, slot);
 
1016                                 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
 
1019                         case DVB_CA_SLOTSTATE_WAITREADY:
 
1020                                 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
 
1021                                         printk("dvb_ca adaptor %d: PC card did not respond :(\n",
 
1022                                                ca->dvbdev->adapter->num);
 
1023                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
 
1024                                         dvb_ca_en50221_thread_update_delay(ca);
 
1027                                 // no other action needed; will automatically change state when ready
 
1030                         case DVB_CA_SLOTSTATE_VALIDATE:
 
1031                                 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
 
1032                                         /* we need this extra check for annoying interfaces like the budget-av */
 
1033                                         if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
 
1034                                             (ca->pub->poll_slot_status)) {
 
1035                                                 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
 
1036                                                 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
 
1037                                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
 
1038                                                         dvb_ca_en50221_thread_update_delay(ca);
 
1043                                         printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
 
1044                                                ca->dvbdev->adapter->num);
 
1045                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
 
1046                                         dvb_ca_en50221_thread_update_delay(ca);
 
1049                                 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
 
1050                                         printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
 
1051                                                ca->dvbdev->adapter->num);
 
1052                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
 
1053                                         dvb_ca_en50221_thread_update_delay(ca);
 
1056                                 if (ca->pub->write_cam_control(ca->pub, slot,
 
1057                                                                CTRLIF_COMMAND, CMDREG_RS) != 0) {
 
1058                                         printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
 
1059                                                ca->dvbdev->adapter->num);
 
1060                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
 
1061                                         dvb_ca_en50221_thread_update_delay(ca);
 
1064                                 dprintk("DVB CAM validated successfully\n");
 
1066                                 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
 
1067                                 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
 
1071                         case DVB_CA_SLOTSTATE_WAITFR:
 
1072                                 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
 
1073                                         printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
 
1074                                                ca->dvbdev->adapter->num);
 
1075                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
 
1076                                         dvb_ca_en50221_thread_update_delay(ca);
 
1080                                 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
 
1081                                 if (flags & STATUSREG_FR) {
 
1082                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
 
1087                         case DVB_CA_SLOTSTATE_LINKINIT:
 
1088                                 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
 
1089                                         /* we need this extra check for annoying interfaces like the budget-av */
 
1090                                         if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
 
1091                                             (ca->pub->poll_slot_status)) {
 
1092                                                 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
 
1093                                                 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
 
1094                                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
 
1095                                                         dvb_ca_en50221_thread_update_delay(ca);
 
1100                                         printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
 
1101                                         ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
 
1102                                         dvb_ca_en50221_thread_update_delay(ca);
 
1106                                 if (ca->slot_info[slot].rx_buffer.data == NULL) {
 
1107                                         rxbuf = vmalloc(RX_BUFFER_SIZE);
 
1108                                         if (rxbuf == NULL) {
 
1109                                                 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
 
1110                                                 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
 
1111                                                 dvb_ca_en50221_thread_update_delay(ca);
 
1114                                         dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
 
1117                                 ca->pub->slot_ts_enable(ca->pub, slot);
 
1118                                 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
 
1119                                 dvb_ca_en50221_thread_update_delay(ca);
 
1120                                 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num);
 
1123                         case DVB_CA_SLOTSTATE_RUNNING:
 
1127                                 // poll slots for data
 
1129                                 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
 
1133                                         /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
 
1134                                         if (dvb_ca_en50221_check_camstatus(ca, slot)) {
 
1135                                                 // we dont want to sleep on the next iteration so we can handle the cam change
 
1140                                         /* check if we've hit our limit this time */
 
1141                                         if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
 
1142                                                 // dont sleep; there is likely to be more data to read
 
1157 /* ******************************************************************************** */
 
1158 /* EN50221 IO interface functions */
 
1161  * Real ioctl implementation.
 
1162  * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
 
1164  * @param inode Inode concerned.
 
1165  * @param file File concerned.
 
1166  * @param cmd IOCTL command.
 
1167  * @param arg Associated argument.
 
1169  * @return 0 on success, <0 on error.
 
1171 static int dvb_ca_en50221_io_do_ioctl(struct inode *inode, struct file *file,
 
1172                                       unsigned int cmd, void *parg)
 
1174         struct dvb_device *dvbdev = file->private_data;
 
1175         struct dvb_ca_private *ca = dvbdev->priv;
 
1179         dprintk("%s\n", __func__);
 
1183                 for (slot = 0; slot < ca->slot_count; slot++) {
 
1184                         if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
 
1185                                 dvb_ca_en50221_slot_shutdown(ca, slot);
 
1186                                 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
 
1187                                         dvb_ca_en50221_camchange_irq(ca->pub,
 
1189                                                                      DVB_CA_EN50221_CAMCHANGE_INSERTED);
 
1192                 ca->next_read_slot = 0;
 
1193                 dvb_ca_en50221_thread_wakeup(ca);
 
1197                 struct ca_caps *caps = parg;
 
1199                 caps->slot_num = ca->slot_count;
 
1200                 caps->slot_type = CA_CI_LINK;
 
1201                 caps->descr_num = 0;
 
1202                 caps->descr_type = 0;
 
1206         case CA_GET_SLOT_INFO: {
 
1207                 struct ca_slot_info *info = parg;
 
1209                 if ((info->num > ca->slot_count) || (info->num < 0))
 
1212                 info->type = CA_CI_LINK;
 
1214                 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
 
1215                         && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
 
1216                         info->flags = CA_CI_MODULE_PRESENT;
 
1218                 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
 
1219                         info->flags |= CA_CI_MODULE_READY;
 
1234  * Wrapper for ioctl implementation.
 
1236  * @param inode Inode concerned.
 
1237  * @param file File concerned.
 
1238  * @param cmd IOCTL command.
 
1239  * @param arg Associated argument.
 
1241  * @return 0 on success, <0 on error.
 
1243 static int dvb_ca_en50221_io_ioctl(struct inode *inode, struct file *file,
 
1244                                    unsigned int cmd, unsigned long arg)
 
1246         return dvb_usercopy(inode, file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
 
1251  * Implementation of write() syscall.
 
1253  * @param file File structure.
 
1254  * @param buf Source buffer.
 
1255  * @param count Size of source buffer.
 
1256  * @param ppos Position in file (ignored).
 
1258  * @return Number of bytes read, or <0 on error.
 
1260 static ssize_t dvb_ca_en50221_io_write(struct file *file,
 
1261                                        const char __user * buf, size_t count, loff_t * ppos)
 
1263         struct dvb_device *dvbdev = file->private_data;
 
1264         struct dvb_ca_private *ca = dvbdev->priv;
 
1265         u8 slot, connection_id;
 
1267         u8 fragbuf[HOST_LINK_BUF_SIZE];
 
1270         unsigned long timeout;
 
1273         dprintk("%s\n", __func__);
 
1275         /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
 
1279         /* extract slot & connection id */
 
1280         if (copy_from_user(&slot, buf, 1))
 
1282         if (copy_from_user(&connection_id, buf + 1, 1))
 
1287         /* check if the slot is actually running */
 
1288         if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
 
1291         /* fragment the packets & store in the buffer */
 
1292         while (fragpos < count) {
 
1293                 fraglen = ca->slot_info[slot].link_buf_size - 2;
 
1294                 if ((count - fragpos) < fraglen)
 
1295                         fraglen = count - fragpos;
 
1297                 fragbuf[0] = connection_id;
 
1298                 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
 
1299                 if ((status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen)) != 0)
 
1302                 timeout = jiffies + HZ / 2;
 
1304                 while (!time_after(jiffies, timeout)) {
 
1305                         /* check the CAM hasn't been removed/reset in the meantime */
 
1306                         if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
 
1311                         status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
 
1312                         if (status == (fraglen + 2)) {
 
1316                         if (status != -EAGAIN)
 
1336  * Condition for waking up in dvb_ca_en50221_io_read_condition
 
1338 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
 
1339                                             int *result, int *_slot)
 
1345         int connection_id = -1;
 
1349         slot = ca->next_read_slot;
 
1350         while ((slot_count < ca->slot_count) && (!found)) {
 
1351                 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
 
1354                 if (ca->slot_info[slot].rx_buffer.data == NULL) {
 
1358                 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
 
1360                         dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
 
1361                         if (connection_id == -1)
 
1362                                 connection_id = hdr[0];
 
1363                         if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
 
1369                         idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
 
1373                 slot = (slot + 1) % ca->slot_count;
 
1377         ca->next_read_slot = slot;
 
1383  * Implementation of read() syscall.
 
1385  * @param file File structure.
 
1386  * @param buf Destination buffer.
 
1387  * @param count Size of destination buffer.
 
1388  * @param ppos Position in file (ignored).
 
1390  * @return Number of bytes read, or <0 on error.
 
1392 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
 
1393                                       size_t count, loff_t * ppos)
 
1395         struct dvb_device *dvbdev = file->private_data;
 
1396         struct dvb_ca_private *ca = dvbdev->priv;
 
1401         int connection_id = -1;
 
1403         int last_fragment = 0;
 
1408         dprintk("%s\n", __func__);
 
1410         /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
 
1414         /* wait for some data */
 
1415         if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
 
1417                 /* if we're in nonblocking mode, exit immediately */
 
1418                 if (file->f_flags & O_NONBLOCK)
 
1419                         return -EWOULDBLOCK;
 
1421                 /* wait for some data */
 
1422                 status = wait_event_interruptible(ca->wait_queue,
 
1423                                                   dvb_ca_en50221_io_read_condition
 
1424                                                   (ca, &result, &slot));
 
1426         if ((status < 0) || (result < 0)) {
 
1432         idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
 
1436                         printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
 
1441                 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
 
1442                 if (connection_id == -1)
 
1443                         connection_id = hdr[0];
 
1444                 if (hdr[0] == connection_id) {
 
1445                         if (pktlen < count) {
 
1446                                 if ((pktlen + fraglen - 2) > count) {
 
1447                                         fraglen = count - pktlen;
 
1452                                 if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
 
1453                                                                       buf + pktlen, fraglen)) < 0) {
 
1459                         if ((hdr[1] & 0x80) == 0)
 
1464                 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
 
1466                         dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
 
1469         } while (!last_fragment);
 
1472         hdr[1] = connection_id;
 
1473         if ((status = copy_to_user(buf, hdr, 2)) != 0)
 
1483  * Implementation of file open syscall.
 
1485  * @param inode Inode concerned.
 
1486  * @param file File concerned.
 
1488  * @return 0 on success, <0 on failure.
 
1490 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
 
1492         struct dvb_device *dvbdev = file->private_data;
 
1493         struct dvb_ca_private *ca = dvbdev->priv;
 
1497         dprintk("%s\n", __func__);
 
1499         if (!try_module_get(ca->pub->owner))
 
1502         err = dvb_generic_open(inode, file);
 
1504                 module_put(ca->pub->owner);
 
1508         for (i = 0; i < ca->slot_count; i++) {
 
1510                 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
 
1511                         if (ca->slot_info[i].rx_buffer.data != NULL) {
 
1512                                 /* it is safe to call this here without locks because
 
1513                                  * ca->open == 0. Data is not read in this case */
 
1514                                 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
 
1520         dvb_ca_en50221_thread_update_delay(ca);
 
1521         dvb_ca_en50221_thread_wakeup(ca);
 
1528  * Implementation of file close syscall.
 
1530  * @param inode Inode concerned.
 
1531  * @param file File concerned.
 
1533  * @return 0 on success, <0 on failure.
 
1535 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
 
1537         struct dvb_device *dvbdev = file->private_data;
 
1538         struct dvb_ca_private *ca = dvbdev->priv;
 
1541         dprintk("%s\n", __func__);
 
1543         /* mark the CA device as closed */
 
1545         dvb_ca_en50221_thread_update_delay(ca);
 
1547         err = dvb_generic_release(inode, file);
 
1549         module_put(ca->pub->owner);
 
1556  * Implementation of poll() syscall.
 
1558  * @param file File concerned.
 
1559  * @param wait poll wait table.
 
1561  * @return Standard poll mask.
 
1563 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
 
1565         struct dvb_device *dvbdev = file->private_data;
 
1566         struct dvb_ca_private *ca = dvbdev->priv;
 
1567         unsigned int mask = 0;
 
1571         dprintk("%s\n", __func__);
 
1573         if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
 
1577         /* if there is something, return now */
 
1581         /* wait for something to happen */
 
1582         poll_wait(file, &ca->wait_queue, wait);
 
1584         if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
 
1590 EXPORT_SYMBOL(dvb_ca_en50221_init);
 
1593 static struct file_operations dvb_ca_fops = {
 
1594         .owner = THIS_MODULE,
 
1595         .read = dvb_ca_en50221_io_read,
 
1596         .write = dvb_ca_en50221_io_write,
 
1597         .ioctl = dvb_ca_en50221_io_ioctl,
 
1598         .open = dvb_ca_en50221_io_open,
 
1599         .release = dvb_ca_en50221_io_release,
 
1600         .poll = dvb_ca_en50221_io_poll,
 
1603 static struct dvb_device dvbdev_ca = {
 
1608         .fops = &dvb_ca_fops,
 
1612 /* ******************************************************************************** */
 
1613 /* Initialisation/shutdown functions */
 
1617  * Initialise a new DVB CA EN50221 interface device.
 
1619  * @param dvb_adapter DVB adapter to attach the new CA device to.
 
1620  * @param ca The dvb_ca instance.
 
1621  * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
 
1622  * @param slot_count Number of slots supported.
 
1624  * @return 0 on success, nonzero on failure
 
1626 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
 
1627                         struct dvb_ca_en50221 *pubca, int flags, int slot_count)
 
1630         struct dvb_ca_private *ca = NULL;
 
1633         dprintk("%s\n", __func__);
 
1638         /* initialise the system data */
 
1639         if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
 
1645         ca->slot_count = slot_count;
 
1646         if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
 
1650         init_waitqueue_head(&ca->wait_queue);
 
1653         ca->next_read_slot = 0;
 
1654         pubca->private = ca;
 
1656         /* register the DVB device */
 
1657         ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA);
 
1661         /* now initialise each slot */
 
1662         for (i = 0; i < slot_count; i++) {
 
1663                 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
 
1664                 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
 
1665                 atomic_set(&ca->slot_info[i].camchange_count, 0);
 
1666                 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
 
1669         if (signal_pending(current)) {
 
1675         /* create a kthread for monitoring this CA device */
 
1676         ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
 
1677                                  ca->dvbdev->adapter->num, ca->dvbdev->id);
 
1678         if (IS_ERR(ca->thread)) {
 
1679                 ret = PTR_ERR(ca->thread);
 
1680                 printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
 
1688                 if (ca->dvbdev != NULL)
 
1689                         dvb_unregister_device(ca->dvbdev);
 
1690                 kfree(ca->slot_info);
 
1693         pubca->private = NULL;
 
1696 EXPORT_SYMBOL(dvb_ca_en50221_release);
 
1701  * Release a DVB CA EN50221 interface device.
 
1703  * @param ca_dev The dvb_device_t instance for the CA device.
 
1704  * @param ca The associated dvb_ca instance.
 
1706 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
 
1708         struct dvb_ca_private *ca = pubca->private;
 
1711         dprintk("%s\n", __func__);
 
1713         /* shutdown the thread if there was one */
 
1714         kthread_stop(ca->thread);
 
1716         for (i = 0; i < ca->slot_count; i++) {
 
1717                 dvb_ca_en50221_slot_shutdown(ca, i);
 
1718                 vfree(ca->slot_info[i].rx_buffer.data);
 
1720         kfree(ca->slot_info);
 
1721         dvb_unregister_device(ca->dvbdev);
 
1723         pubca->private = NULL;