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/moduleparam.h>
36 #include <linux/vmalloc.h>
37 #include <linux/delay.h>
38 #include <linux/rwsem.h>
40 #include "dvb_ca_en50221.h"
41 #include "dvb_ringbuffer.h"
43 static int dvb_ca_en50221_debug;
45 module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
46 MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
48 #define dprintk if (dvb_ca_en50221_debug) printk
50 #define INIT_TIMEOUT_SECS 5
52 #define HOST_LINK_BUF_SIZE 0x200
54 #define RX_BUFFER_SIZE 65535
56 #define MAX_RX_PACKETS_PER_ITERATION 10
59 #define CTRLIF_COMMAND 1
60 #define CTRLIF_STATUS 1
61 #define CTRLIF_SIZE_LOW 2
62 #define CTRLIF_SIZE_HIGH 3
64 #define CMDREG_HC 1 /* Host control */
65 #define CMDREG_SW 2 /* Size write */
66 #define CMDREG_SR 4 /* Size read */
67 #define CMDREG_RS 8 /* Reset interface */
68 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
69 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
70 #define IRQEN (CMDREG_DAIE)
72 #define STATUSREG_RE 1 /* read error */
73 #define STATUSREG_WE 2 /* write error */
74 #define STATUSREG_FR 0x40 /* module free */
75 #define STATUSREG_DA 0x80 /* data available */
76 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
79 #define DVB_CA_SLOTSTATE_NONE 0
80 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
81 #define DVB_CA_SLOTSTATE_RUNNING 2
82 #define DVB_CA_SLOTSTATE_INVALID 3
83 #define DVB_CA_SLOTSTATE_WAITREADY 4
84 #define DVB_CA_SLOTSTATE_VALIDATE 5
85 #define DVB_CA_SLOTSTATE_WAITFR 6
86 #define DVB_CA_SLOTSTATE_LINKINIT 7
89 /* Information on a CA slot */
92 /* current state of the CAM */
95 /* Number of CAMCHANGES that have occurred since last processing */
96 atomic_t camchange_count;
98 /* Type of last CAMCHANGE */
101 /* base address of CAM config */
104 /* value to write into Config Control register */
107 /* if 1, the CAM supports DA IRQs */
108 u8 da_irq_supported:1;
110 /* size of the buffer to use when talking to the CAM */
113 /* semaphore for syncing access to slot structure */
114 struct rw_semaphore sem;
116 /* buffer for incoming packets */
117 struct dvb_ringbuffer rx_buffer;
119 /* timer used during various states of the slot */
120 unsigned long timeout;
123 /* Private CA-interface information */
124 struct dvb_ca_private {
126 /* pointer back to the public data structure */
127 struct dvb_ca_en50221* pub;
130 struct dvb_device *dvbdev;
132 /* Flags describing the interface (DVB_CA_FLAG_*) */
135 /* number of slots supported by this CA interface */
136 unsigned int slot_count;
138 /* information on each slot */
139 struct dvb_ca_slot* slot_info;
141 /* wait queues for read() and write() operations */
142 wait_queue_head_t wait_queue;
144 /* PID of the monitoring thread */
147 /* Wait queue used when shutting thread down */
148 wait_queue_head_t thread_queue;
150 /* Flag indicating when thread should exit */
153 /* Flag indicating if the CA device is open */
156 /* Flag indicating the thread should wake up now */
159 /* Delay the main thread should use */
162 /* Slot to start looking for data to read from in the next user-space read operation */
166 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private* ca);
167 static int dvb_ca_en50221_read_data(struct dvb_ca_private* ca, int slot, u8* ebuf, int ecount);
168 static int dvb_ca_en50221_write_data(struct dvb_ca_private* ca, int slot, u8* ebuf, int ecount);
172 * Safely find needle in haystack.
174 * @param haystack Buffer to look in.
175 * @param hlen Number of bytes in haystack.
176 * @param needle Buffer to find.
177 * @param nlen Number of bytes in needle.
178 * @return Pointer into haystack needle was found at, or NULL if not found.
180 static u8* findstr(u8* haystack, int hlen, u8* needle, int nlen)
187 for(i=0; i<= hlen - nlen; i++) {
188 if (!strncmp(haystack + i, needle, nlen))
197 /* ******************************************************************************** */
198 /* EN50221 physical interface functions */
204 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private* ca, int slot)
211 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
212 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
216 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
218 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1: 0;
219 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1: 0;
221 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
222 cam_changed = (cam_present_now != cam_present_old);
226 if (!cam_present_now) {
227 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
229 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
231 atomic_set(&ca->slot_info[slot].camchange_count, 1);
233 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
234 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
235 // move to validate state if reset is completed
236 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
245 * Wait for flags to become set on the STATUS register on a CAM interface,
246 * checking for errors and timeout.
248 * @param ca CA instance.
249 * @param slot Slot on interface.
250 * @param waitfor Flags to wait for.
251 * @param timeout_ms Timeout in milliseconds.
253 * @return 0 on success, nonzero on error.
255 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
256 u8 waitfor, int timeout_hz)
258 unsigned long timeout;
261 dprintk ("%s\n", __FUNCTION__);
263 /* loop until timeout elapsed */
265 timeout = jiffies + timeout_hz;
267 /* read the status and check for error */
268 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
272 /* if we got the flags, it was successful! */
274 dprintk("%s succeeded timeout:%lu\n", __FUNCTION__, jiffies - start);
278 /* check for timeout */
279 if (time_after(jiffies, timeout)) {
287 dprintk("%s failed timeout:%lu\n", __FUNCTION__, jiffies - start);
289 /* if we get here, we've timed out */
295 * Initialise the link layer connection to a CAM.
297 * @param ca CA instance.
298 * @param slot Slot id.
300 * @return 0 on success, nonzero on failure.
302 static int dvb_ca_en50221_link_init(struct dvb_ca_private* ca, int slot)
308 dprintk ("%s\n", __FUNCTION__);
310 /* we'll be determining these during this function */
311 ca->slot_info[slot].da_irq_supported = 0;
313 /* set the host link buffer size temporarily. it will be overwritten with the
314 * real negotiated size later. */
315 ca->slot_info[slot].link_buf_size = 2;
317 /* read the buffer size from the CAM */
318 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
320 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
322 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
324 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
327 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
328 buf_size = (buf[0] << 8) | buf[1];
329 if (buf_size > HOST_LINK_BUF_SIZE)
330 buf_size = HOST_LINK_BUF_SIZE;
331 ca->slot_info[slot].link_buf_size = buf_size;
332 buf[0] = buf_size >> 8;
333 buf[1] = buf_size & 0xff;
334 dprintk("Chosen link buffer size of %i\n", buf_size);
336 /* write the buffer size to the CAM */
337 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
339 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
341 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
343 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
351 * Read a tuple from attribute memory.
353 * @param ca CA instance.
354 * @param slot Slot id.
355 * @param address Address to read from. Updated.
356 * @param tupleType Tuple id byte. Updated.
357 * @param tupleLength Tuple length. Updated.
358 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
360 * @return 0 on success, nonzero on error.
362 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private* ca, int slot,
363 int* address, int* tupleType, int* tupleLength, u8* tuple)
368 int _address = *address;
370 /* grab the next tuple length and type */
371 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
373 if (_tupleType == 0xff) {
374 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
376 *tupleType = _tupleType;
380 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
384 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
386 /* read in the whole tuple */
387 for(i=0; i< _tupleLength; i++) {
388 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i*2));
389 dprintk(" 0x%02x: 0x%02x %c\n",
391 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
393 _address += (_tupleLength*2);
396 *tupleType = _tupleType;
397 *tupleLength = _tupleLength;
404 * Parse attribute memory of a CAM module, extracting Config register, and checking
405 * it is a DVB CAM module.
407 * @param ca CA instance.
408 * @param slot Slot id.
410 * @return 0 on success, <0 on failure.
412 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private* ca, int slot)
421 int got_cftableentry = 0;
430 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
432 if (tupleType != 0x1D)
439 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
441 if (tupleType != 0x1C)
448 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
450 if (tupleType != 0x15)
456 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
457 &tupleLength, tuple)) < 0)
459 if (tupleType != 0x20)
461 if (tupleLength != 4)
463 manfid = (tuple[1] << 8) | tuple[0];
464 devid = (tuple[3] << 8) | tuple[2];
469 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
470 &tupleLength, tuple)) < 0)
472 if (tupleType != 0x1A)
477 /* extract the configbase */
479 if (tupleLength < (3 + rasz + 14))
481 ca->slot_info[slot].config_base = 0;
482 for(i=0; i< rasz+1; i++) {
483 ca->slot_info[slot].config_base |= (tuple[2+i] << (8*i));
486 /* check it contains the correct DVB string */
487 dvb_str = findstr(tuple, tupleLength, "DVB_CI_V", 8);
490 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
493 /* is it a version we support? */
494 if (strncmp(dvb_str + 8, "1.00", 4)) {
495 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
496 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]);
500 /* process the CFTABLE_ENTRY tuples, and any after those */
501 while((!end_chain) && (address < 0x1000)) {
502 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
503 &tupleLength, tuple)) < 0)
506 case 0x1B: // CISTPL_CFTABLE_ENTRY
507 if (tupleLength < (2 + 11 + 17))
510 /* if we've already parsed one, just use it */
511 if (got_cftableentry)
514 /* get the config option */
515 ca->slot_info[slot].config_option = tuple[0] & 0x3f;
517 /* OK, check it contains the correct strings */
518 if ((findstr(tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
519 (findstr(tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
522 got_cftableentry = 1;
525 case 0x14: // CISTPL_NO_LINK
528 case 0xFF: // CISTPL_END
532 default: /* Unknown tuple type - just skip this tuple and move to the next one */
533 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType,
539 if ((address > 0x1000) || (!got_cftableentry))
542 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
543 manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option);
551 * Set CAM's configoption correctly.
553 * @param ca CA instance.
554 * @param slot Slot containing the CAM.
556 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private* ca, int slot)
560 dprintk ("%s\n", __FUNCTION__);
562 /* set the config option */
563 ca->pub->write_attribute_mem(ca->pub, slot,
564 ca->slot_info[slot].config_base,
565 ca->slot_info[slot].config_option);
568 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
569 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
570 ca->slot_info[slot].config_option, configoption & 0x3f);
579 * This function talks to an EN50221 CAM control interface. It reads a buffer of
580 * data from the CAM. The data can either be stored in a supplied buffer, or
581 * automatically be added to the slot's rx_buffer.
583 * @param ca CA instance.
584 * @param slot Slot to read from.
585 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
586 * the data will be added into the buffering system as a normal fragment.
587 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
589 * @return Number of bytes read, or < 0 on error
591 static int dvb_ca_en50221_read_data(struct dvb_ca_private* ca, int slot, u8* ebuf, int ecount)
595 u8 buf[HOST_LINK_BUF_SIZE];
598 dprintk ("%s\n", __FUNCTION__);
600 /* check if we have space for a link buf in the rx_buffer */
604 down_read(&ca->slot_info[slot].sem);
605 if (ca->slot_info[slot].rx_buffer.data == NULL) {
606 up_read(&ca->slot_info[slot].sem);
610 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
611 up_read(&ca->slot_info[slot].sem);
613 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
619 /* check if there is data available */
620 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
622 if (!(status & STATUSREG_DA)) {
628 /* read the amount of data */
629 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
631 bytes_read = status << 8;
632 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
634 bytes_read |= status;
636 /* check it will fit */
638 if (bytes_read > ca->slot_info[slot].link_buf_size) {
639 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
640 ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size);
641 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
645 if (bytes_read < 2) {
646 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
647 ca->dvbdev->adapter->num);
648 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
653 if (bytes_read > ecount) {
654 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
655 ca->dvbdev->adapter->num);
661 /* fill the buffer */
662 for(i=0; i < bytes_read; i++) {
663 /* read byte and check */
664 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
667 /* OK, store it in the buffer */
671 /* check for read error (RE should now be 0) */
672 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
674 if (status & STATUSREG_RE) {
675 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
680 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
682 down_read(&ca->slot_info[slot].sem);
683 if (ca->slot_info[slot].rx_buffer.data == NULL) {
684 up_read(&ca->slot_info[slot].sem);
688 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
689 up_read(&ca->slot_info[slot].sem);
691 memcpy(ebuf, buf, bytes_read);
694 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
695 buf[0], (buf[1] & 0x80) == 0, bytes_read);
697 /* wake up readers when a last_fragment is received */
698 if ((buf[1] & 0x80) == 0x00) {
699 wake_up_interruptible(&ca->wait_queue);
710 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
713 * @param ca CA instance.
714 * @param slot Slot to write to.
715 * @param ebuf The data in this buffer is treated as a complete link-level packet to
717 * @param count Size of ebuf.
719 * @return Number of bytes written, or < 0 on error.
721 static int dvb_ca_en50221_write_data(struct dvb_ca_private* ca, int slot, u8* buf, int bytes_write)
726 dprintk ("%s\n", __FUNCTION__);
730 if (bytes_write > ca->slot_info[slot].link_buf_size)
733 /* check if interface is actually waiting for us to read from it, or if a read is in progress */
734 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
736 if (status & (STATUSREG_DA|STATUSREG_RE)) {
742 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
743 IRQEN | CMDREG_HC)) != 0)
746 /* check if interface is still free */
747 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
749 if (!(status & STATUSREG_FR)) {
750 /* it wasn't free => try again later */
755 /* send the amount of data */
756 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
758 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
759 bytes_write & 0xff)) != 0)
762 /* send the buffer */
763 for(i=0; i < bytes_write; i++) {
764 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
768 /* check for write error (WE should now be 0) */
769 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
771 if (status & STATUSREG_WE) {
772 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
776 status = bytes_write;
778 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
779 buf[0], (buf[1] & 0x80) == 0, bytes_write);
782 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
787 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
791 /* ******************************************************************************** */
792 /* EN50221 higher level functions */
796 * A CAM has been removed => shut it down.
798 * @param ca CA instance.
799 * @param slot Slot to shut down.
801 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private* ca, int slot)
803 dprintk ("%s\n", __FUNCTION__);
805 down_write(&ca->slot_info[slot].sem);
806 ca->pub->slot_shutdown(ca->pub, slot);
807 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
808 if (ca->slot_info[slot].rx_buffer.data)
809 vfree(ca->slot_info[slot].rx_buffer.data);
810 ca->slot_info[slot].rx_buffer.data = NULL;
811 up_write(&ca->slot_info[slot].sem);
813 /* need to wake up all processes to check if they're now
814 trying to write to a defunct CAM */
815 wake_up_interruptible(&ca->wait_queue);
817 dprintk("Slot %i shutdown\n", slot);
822 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
826 * A CAMCHANGE IRQ has occurred.
828 * @param ca CA instance.
829 * @param slot Slot concerned.
830 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
832 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221* pubca, int slot, int change_type)
834 struct dvb_ca_private* ca = (struct dvb_ca_private*) pubca->private;
836 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
838 switch(change_type) {
839 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
840 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
847 ca->slot_info[slot].camchange_type = change_type;
848 atomic_inc(&ca->slot_info[slot].camchange_count);
849 dvb_ca_en50221_thread_wakeup(ca);
851 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
855 * A CAMREADY IRQ has occurred.
857 * @param ca CA instance.
858 * @param slot Slot concerned.
860 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221* pubca, int slot)
862 struct dvb_ca_private* ca = (struct dvb_ca_private*) pubca->private;
864 dprintk("CAMREADY IRQ slot:%i\n", slot);
866 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
867 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
868 dvb_ca_en50221_thread_wakeup(ca);
874 * An FR or DA IRQ has occurred.
876 * @param ca CA instance.
877 * @param slot Slot concerned.
879 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221* pubca, int slot)
881 struct dvb_ca_private* ca = (struct dvb_ca_private*) pubca->private;
884 dprintk("FR/DA IRQ slot:%i\n", slot);
886 switch(ca->slot_info[slot].slot_state) {
887 case DVB_CA_SLOTSTATE_LINKINIT:
888 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
889 if (flags & STATUSREG_DA) {
890 dprintk("CAM supports DA IRQ\n");
891 ca->slot_info[slot].da_irq_supported = 1;
895 case DVB_CA_SLOTSTATE_RUNNING:
897 dvb_ca_en50221_read_data(ca, slot, NULL, 0);
904 /* ******************************************************************************** */
905 /* EN50221 thread functions */
908 * Wake up the DVB CA thread
910 * @param ca CA instance.
912 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private* ca)
915 dprintk ("%s\n", __FUNCTION__);
919 wake_up_interruptible(&ca->thread_queue);
923 * Used by the CA thread to determine if an early wakeup is necessary
925 * @param ca CA instance.
927 static int dvb_ca_en50221_thread_should_wakeup(struct dvb_ca_private* ca)
941 * Update the delay used by the thread.
943 * @param ca CA instance.
945 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private* ca)
948 int curdelay = 100000000;
951 for(slot=0; slot < ca->slot_count; slot++) {
952 switch(ca->slot_info[slot].slot_state) {
954 case DVB_CA_SLOTSTATE_NONE:
955 case DVB_CA_SLOTSTATE_INVALID:
957 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
962 case DVB_CA_SLOTSTATE_UNINITIALISED:
963 case DVB_CA_SLOTSTATE_WAITREADY:
964 case DVB_CA_SLOTSTATE_VALIDATE:
965 case DVB_CA_SLOTSTATE_WAITFR:
966 case DVB_CA_SLOTSTATE_LINKINIT:
970 case DVB_CA_SLOTSTATE_RUNNING:
972 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
976 if ((!ca->slot_info[slot].da_irq_supported) ||
977 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA))) {
984 if (delay < curdelay)
988 ca->delay = curdelay;
994 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
996 static int dvb_ca_en50221_thread(void* data)
998 struct dvb_ca_private *ca = (struct dvb_ca_private*) data;
1006 dprintk ("%s\n", __FUNCTION__);
1008 /* setup kernel thread */
1009 snprintf(name, sizeof(name), "kdvb-ca-%i:%i", ca->dvbdev->adapter->num, ca->dvbdev->id);
1013 sigfillset (¤t->blocked);
1016 /* choose the correct initial delay */
1017 dvb_ca_en50221_thread_update_delay(ca);
1021 /* sleep for a bit */
1023 flags = wait_event_interruptible_timeout(ca->thread_queue,
1024 dvb_ca_en50221_thread_should_wakeup(ca),
1026 if ((flags == -ERESTARTSYS) || ca->exit) {
1027 /* got signal or quitting */
1033 /* go through all the slots processing them */
1034 for(slot=0; slot < ca->slot_count; slot++) {
1036 // check the cam status + deal with CAMCHANGEs
1037 while(dvb_ca_en50221_check_camstatus(ca, slot)) {
1038 /* clear down an old CI slot if necessary */
1039 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
1040 dvb_ca_en50221_slot_shutdown(ca, slot);
1042 /* if a CAM is NOW present, initialise it */
1043 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
1044 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1047 /* we've handled one CAMCHANGE */
1048 dvb_ca_en50221_thread_update_delay(ca);
1049 atomic_dec(&ca->slot_info[slot].camchange_count);
1052 // CAM state machine
1053 switch(ca->slot_info[slot].slot_state) {
1054 case DVB_CA_SLOTSTATE_NONE:
1055 case DVB_CA_SLOTSTATE_INVALID:
1059 case DVB_CA_SLOTSTATE_UNINITIALISED:
1060 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1061 ca->pub->slot_reset(ca->pub, slot);
1062 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1065 case DVB_CA_SLOTSTATE_WAITREADY:
1066 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1067 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1068 ca->dvbdev->adapter->num);
1069 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1070 dvb_ca_en50221_thread_update_delay(ca);
1073 // no other action needed; will automatically change state when ready
1076 case DVB_CA_SLOTSTATE_VALIDATE:
1077 if (dvb_ca_en50221_parse_attributes(ca, slot)
1079 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1080 ca->dvbdev->adapter->num);
1081 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1082 dvb_ca_en50221_thread_update_delay(ca);
1085 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1086 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1087 ca->dvbdev->adapter->num);
1088 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1089 dvb_ca_en50221_thread_update_delay(ca);
1092 if (ca->pub->write_cam_control(ca->pub, slot,
1093 CTRLIF_COMMAND, CMDREG_RS) != 0) {
1094 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1095 ca->dvbdev->adapter->num);
1096 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1097 dvb_ca_en50221_thread_update_delay(ca);
1101 dprintk("DVB CAM validated successfully\n");
1103 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1104 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
1108 case DVB_CA_SLOTSTATE_WAITFR:
1109 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1110 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1111 ca->dvbdev->adapter->num);
1112 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1113 dvb_ca_en50221_thread_update_delay(ca);
1117 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1118 if (flags & STATUSREG_FR) {
1119 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1124 case DVB_CA_SLOTSTATE_LINKINIT:
1125 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1126 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
1127 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1128 dvb_ca_en50221_thread_update_delay(ca);
1132 rxbuf = vmalloc(RX_BUFFER_SIZE);
1133 if (rxbuf == NULL) {
1134 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
1135 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1136 dvb_ca_en50221_thread_update_delay(ca);
1139 down_write(&ca->slot_info[slot].sem);
1140 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
1141 up_write(&ca->slot_info[slot].sem);
1143 ca->pub->slot_ts_enable(ca->pub, slot);
1144 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
1145 dvb_ca_en50221_thread_update_delay(ca);
1146 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num);
1149 case DVB_CA_SLOTSTATE_RUNNING:
1153 // no need to poll if the CAM supports IRQs
1154 if (ca->slot_info[slot].da_irq_supported)
1159 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1163 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1164 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1165 // we dont want to sleep on the next iteration so we can handle the cam change
1170 /* check if we've hit our limit this time */
1171 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1172 // dont sleep; there is likely to be more data to read
1185 wake_up_interruptible (&ca->thread_queue);
1191 /* ******************************************************************************** */
1192 /* EN50221 IO interface functions */
1195 * Real ioctl implementation.
1196 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1198 * @param inode Inode concerned.
1199 * @param file File concerned.
1200 * @param cmd IOCTL command.
1201 * @param arg Associated argument.
1203 * @return 0 on success, <0 on error.
1205 static int dvb_ca_en50221_io_do_ioctl(struct inode *inode, struct file *file,
1206 unsigned int cmd, void *parg)
1208 struct dvb_device* dvbdev=(struct dvb_device*) file->private_data;
1209 struct dvb_ca_private* ca = (struct dvb_ca_private*) dvbdev->priv;
1213 dprintk ("%s\n", __FUNCTION__);
1217 for(slot = 0; slot < ca->slot_count; slot++) {
1218 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
1219 dvb_ca_en50221_slot_shutdown(ca, slot);
1220 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1221 dvb_ca_en50221_camchange_irq(ca->pub,
1223 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1226 ca->next_read_slot = 0;
1227 dvb_ca_en50221_thread_wakeup(ca);
1231 struct ca_caps *caps = (struct ca_caps*) parg;
1233 caps->slot_num=ca->slot_count;
1234 caps->slot_type=CA_CI_LINK;
1240 case CA_GET_SLOT_INFO: {
1241 struct ca_slot_info *info=(struct ca_slot_info *)parg;
1243 if ((info->num > ca->slot_count) || (info->num < 0))
1246 info->type = CA_CI_LINK;
1248 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
1249 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1250 info->flags = CA_CI_MODULE_PRESENT;
1252 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1253 info->flags |= CA_CI_MODULE_READY;
1268 * Wrapper for ioctl implementation.
1270 * @param inode Inode concerned.
1271 * @param file File concerned.
1272 * @param cmd IOCTL command.
1273 * @param arg Associated argument.
1275 * @return 0 on success, <0 on error.
1277 static int dvb_ca_en50221_io_ioctl(struct inode *inode, struct file *file,
1278 unsigned int cmd, unsigned long arg)
1280 return dvb_usercopy(inode, file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1285 * Implementation of write() syscall.
1287 * @param file File structure.
1288 * @param buf Source buffer.
1289 * @param count Size of source buffer.
1290 * @param ppos Position in file (ignored).
1292 * @return Number of bytes read, or <0 on error.
1294 static ssize_t dvb_ca_en50221_io_write(struct file *file,
1295 const char __user * buf, size_t count, loff_t * ppos)
1297 struct dvb_device *dvbdev=(struct dvb_device *) file->private_data;
1298 struct dvb_ca_private *ca=(struct dvb_ca_private*) dvbdev->priv;
1299 u8 slot, connection_id;
1301 char fragbuf[HOST_LINK_BUF_SIZE];
1304 unsigned long timeout;
1307 dprintk ("%s\n", __FUNCTION__);
1309 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1313 /* extract slot & connection id */
1314 if (copy_from_user(&slot, buf, 1))
1316 if (copy_from_user(&connection_id, buf + 1, 1))
1321 /* check if the slot is actually running */
1322 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1325 /* fragment the packets & store in the buffer */
1326 while(fragpos < count) {
1327 fraglen = ca->slot_info[slot].link_buf_size - 2;
1328 if ((count - fragpos) < fraglen)
1329 fraglen = count - fragpos;
1331 fragbuf[0] = connection_id;
1332 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1333 if ((status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen)) != 0)
1336 timeout = jiffies + HZ/2;
1338 while(!time_after(jiffies, timeout)) {
1339 /* check the CAM hasn't been removed/reset in the meantime */
1340 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1345 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen+2);
1346 if (status == (fraglen+2)) {
1350 if (status != -EAGAIN)
1370 * Condition for waking up in dvb_ca_en50221_io_read_condition
1372 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private* ca, int* result, int* _slot)
1378 int connection_id = -1;
1382 slot = ca->next_read_slot;
1383 while((slot_count < ca->slot_count) && (!found)) {
1384 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1387 down_read(&ca->slot_info[slot].sem);
1389 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1390 up_read(&ca->slot_info[slot].sem);
1394 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1396 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2, 0);
1397 if (connection_id == -1)
1398 connection_id = hdr[0];
1399 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
1405 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1409 up_read(&ca->slot_info[slot].sem);
1412 slot = (slot + 1) % ca->slot_count;
1416 ca->next_read_slot = slot;
1422 * Implementation of read() syscall.
1424 * @param file File structure.
1425 * @param buf Destination buffer.
1426 * @param count Size of destination buffer.
1427 * @param ppos Position in file (ignored).
1429 * @return Number of bytes read, or <0 on error.
1431 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
1432 size_t count, loff_t * ppos)
1434 struct dvb_device *dvbdev=(struct dvb_device *) file->private_data;
1435 struct dvb_ca_private *ca=(struct dvb_ca_private*) dvbdev->priv;
1440 int connection_id = -1;
1442 int last_fragment = 0;
1447 dprintk ("%s\n", __FUNCTION__);
1449 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1453 /* wait for some data */
1454 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
1456 /* if we're in nonblocking mode, exit immediately */
1457 if (file->f_flags & O_NONBLOCK)
1458 return -EWOULDBLOCK;
1460 /* wait for some data */
1461 status = wait_event_interruptible(ca->wait_queue,
1462 dvb_ca_en50221_io_read_condition
1463 (ca, &result, &slot));
1465 if ((status < 0) || (result < 0)) {
1471 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1475 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
1480 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2, 0);
1481 if (connection_id == -1)
1482 connection_id = hdr[0];
1483 if (hdr[0] == connection_id) {
1484 if (pktlen < count) {
1485 if ((pktlen + fraglen - 2) > count) {
1486 fraglen = count - pktlen;
1491 if ((status = dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 2,
1492 buf + pktlen, fraglen, 1)) < 0) {
1498 if ((hdr[1] & 0x80) == 0)
1503 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1505 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
1508 } while (!last_fragment);
1511 hdr[1] = connection_id;
1512 if ((status = copy_to_user(buf, hdr, 2)) != 0)
1517 up_read(&ca->slot_info[slot].sem);
1523 * Implementation of file open syscall.
1525 * @param inode Inode concerned.
1526 * @param file File concerned.
1528 * @return 0 on success, <0 on failure.
1530 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1532 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1533 struct dvb_ca_private *ca = (struct dvb_ca_private*) dvbdev->priv;
1537 dprintk ("%s\n", __FUNCTION__);
1539 if (!try_module_get(ca->pub->owner))
1542 err=dvb_generic_open(inode, file);
1546 for(i=0; i< ca->slot_count; i++) {
1548 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1549 down_write(&ca->slot_info[i].sem);
1550 if (ca->slot_info[i].rx_buffer.data != NULL) {
1551 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
1553 up_write(&ca->slot_info[i].sem);
1558 dvb_ca_en50221_thread_update_delay(ca);
1559 dvb_ca_en50221_thread_wakeup(ca);
1566 * Implementation of file close syscall.
1568 * @param inode Inode concerned.
1569 * @param file File concerned.
1571 * @return 0 on success, <0 on failure.
1573 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1575 struct dvb_device *dvbdev=(struct dvb_device *) file->private_data;
1576 struct dvb_ca_private *ca=(struct dvb_ca_private*) dvbdev->priv;
1579 dprintk ("%s\n", __FUNCTION__);
1581 /* mark the CA device as closed */
1583 dvb_ca_en50221_thread_update_delay(ca);
1585 err=dvb_generic_release(inode, file);
1587 module_put(ca->pub->owner);
1594 * Implementation of poll() syscall.
1596 * @param file File concerned.
1597 * @param wait poll wait table.
1599 * @return Standard poll mask.
1601 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table *wait)
1603 struct dvb_device* dvbdev = (struct dvb_device*) file->private_data;
1604 struct dvb_ca_private* ca = (struct dvb_ca_private*) dvbdev->priv;
1605 unsigned int mask=0;
1609 dprintk ("%s\n", __FUNCTION__);
1611 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1612 up_read(&ca->slot_info[slot].sem);
1616 /* if there is something, return now */
1620 /* wait for something to happen */
1621 poll_wait(file, &ca->wait_queue, wait);
1623 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1624 up_read(&ca->slot_info[slot].sem);
1630 EXPORT_SYMBOL(dvb_ca_en50221_init);
1633 static struct file_operations dvb_ca_fops = {
1634 .owner = THIS_MODULE,
1635 .read = dvb_ca_en50221_io_read,
1636 .write = dvb_ca_en50221_io_write,
1637 .ioctl = dvb_ca_en50221_io_ioctl,
1638 .open = dvb_ca_en50221_io_open,
1639 .release= dvb_ca_en50221_io_release,
1640 .poll = dvb_ca_en50221_io_poll,
1643 static struct dvb_device dvbdev_ca = {
1648 .fops = &dvb_ca_fops,
1651 /* ******************************************************************************** */
1652 /* Initialisation/shutdown functions */
1656 * Initialise a new DVB CA EN50221 interface device.
1658 * @param dvb_adapter DVB adapter to attach the new CA device to.
1659 * @param ca The dvb_ca instance.
1660 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1661 * @param slot_count Number of slots supported.
1663 * @return 0 on success, nonzero on failure
1665 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1666 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1669 struct dvb_ca_private* ca = NULL;
1672 dprintk ("%s\n", __FUNCTION__);
1677 /* initialise the system data */
1679 (struct dvb_ca_private *) kmalloc(sizeof(struct dvb_ca_private),
1680 GFP_KERNEL)) == NULL) {
1684 memset(ca, 0, sizeof(struct dvb_ca_private));
1687 ca->slot_count = slot_count;
1688 if ((ca->slot_info = kmalloc(sizeof(struct dvb_ca_slot) * slot_count, GFP_KERNEL)) == NULL) {
1692 memset(ca->slot_info, 0, sizeof(struct dvb_ca_slot) * slot_count);
1693 init_waitqueue_head(&ca->wait_queue);
1695 init_waitqueue_head(&ca->thread_queue);
1699 ca->next_read_slot = 0;
1700 pubca->private = ca;
1702 /* register the DVB device */
1703 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA);
1707 /* now initialise each slot */
1708 for(i=0; i< slot_count; i++) {
1709 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
1710 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
1711 atomic_set(&ca->slot_info[i].camchange_count, 0);
1712 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1713 init_rwsem(&ca->slot_info[i].sem);
1716 if (signal_pending(current)) {
1722 /* create a kthread for monitoring this CA device */
1723 ret = kernel_thread (dvb_ca_en50221_thread, ca, 0);
1725 printk("dvb_ca_init: failed to start kernel_thread (%d)\n", ret);
1728 ca->thread_pid = ret;
1733 if (ca->dvbdev != NULL)
1734 dvb_unregister_device(ca->dvbdev);
1735 if (ca->slot_info != NULL)
1736 kfree(ca->slot_info);
1739 pubca->private = NULL;
1742 EXPORT_SYMBOL(dvb_ca_en50221_release);
1747 * Release a DVB CA EN50221 interface device.
1749 * @param ca_dev The dvb_device_t instance for the CA device.
1750 * @param ca The associated dvb_ca instance.
1752 void dvb_ca_en50221_release(struct dvb_ca_en50221* pubca)
1754 struct dvb_ca_private* ca = (struct dvb_ca_private*) pubca->private;
1757 dprintk ("%s\n", __FUNCTION__);
1759 /* shutdown the thread if there was one */
1760 if (ca->thread_pid) {
1761 if (kill_proc(ca->thread_pid, 0, 1) == -ESRCH) {
1762 printk("dvb_ca_release adapter %d: thread PID %d already died\n",
1763 ca->dvbdev->adapter->num, ca->thread_pid);
1767 dvb_ca_en50221_thread_wakeup(ca);
1768 wait_event_interruptible(ca->thread_queue, ca->thread_pid == 0);
1772 for(i=0; i< ca->slot_count; i++) {
1773 dvb_ca_en50221_slot_shutdown(ca, i);
1775 kfree(ca->slot_info);
1776 dvb_unregister_device(ca->dvbdev);
1778 pubca->private = NULL;