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/smp_lock.h>
40 #include <linux/kthread.h>
42 #include "dvb_ca_en50221.h"
43 #include "dvb_ringbuffer.h"
45 static int dvb_ca_en50221_debug;
47 module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
48 MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
50 #define dprintk if (dvb_ca_en50221_debug) printk
52 #define INIT_TIMEOUT_SECS 10
54 #define HOST_LINK_BUF_SIZE 0x200
56 #define RX_BUFFER_SIZE 65535
58 #define MAX_RX_PACKETS_PER_ITERATION 10
61 #define CTRLIF_COMMAND 1
62 #define CTRLIF_STATUS 1
63 #define CTRLIF_SIZE_LOW 2
64 #define CTRLIF_SIZE_HIGH 3
66 #define CMDREG_HC 1 /* Host control */
67 #define CMDREG_SW 2 /* Size write */
68 #define CMDREG_SR 4 /* Size read */
69 #define CMDREG_RS 8 /* Reset interface */
70 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
71 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
72 #define IRQEN (CMDREG_DAIE)
74 #define STATUSREG_RE 1 /* read error */
75 #define STATUSREG_WE 2 /* write error */
76 #define STATUSREG_FR 0x40 /* module free */
77 #define STATUSREG_DA 0x80 /* data available */
78 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
81 #define DVB_CA_SLOTSTATE_NONE 0
82 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
83 #define DVB_CA_SLOTSTATE_RUNNING 2
84 #define DVB_CA_SLOTSTATE_INVALID 3
85 #define DVB_CA_SLOTSTATE_WAITREADY 4
86 #define DVB_CA_SLOTSTATE_VALIDATE 5
87 #define DVB_CA_SLOTSTATE_WAITFR 6
88 #define DVB_CA_SLOTSTATE_LINKINIT 7
91 /* Information on a CA slot */
94 /* current state of the CAM */
97 /* mutex used for serializing access to one CI slot */
98 struct mutex slot_lock;
100 /* Number of CAMCHANGES that have occurred since last processing */
101 atomic_t camchange_count;
103 /* Type of last CAMCHANGE */
106 /* base address of CAM config */
109 /* value to write into Config Control register */
112 /* if 1, the CAM supports DA IRQs */
113 u8 da_irq_supported:1;
115 /* size of the buffer to use when talking to the CAM */
118 /* buffer for incoming packets */
119 struct dvb_ringbuffer rx_buffer;
121 /* timer used during various states of the slot */
122 unsigned long timeout;
125 /* Private CA-interface information */
126 struct dvb_ca_private {
128 /* pointer back to the public data structure */
129 struct dvb_ca_en50221 *pub;
132 struct dvb_device *dvbdev;
134 /* Flags describing the interface (DVB_CA_FLAG_*) */
137 /* number of slots supported by this CA interface */
138 unsigned int slot_count;
140 /* information on each slot */
141 struct dvb_ca_slot *slot_info;
143 /* wait queues for read() and write() operations */
144 wait_queue_head_t wait_queue;
146 /* PID of the monitoring thread */
147 struct task_struct *thread;
149 /* Flag indicating if the CA device is open */
152 /* Flag indicating the thread should wake up now */
153 unsigned int wakeup:1;
155 /* Delay the main thread should use */
158 /* Slot to start looking for data to read from in the next user-space read operation */
162 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
163 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
164 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
168 * Safely find needle in haystack.
170 * @param haystack Buffer to look in.
171 * @param hlen Number of bytes in haystack.
172 * @param needle Buffer to find.
173 * @param nlen Number of bytes in needle.
174 * @return Pointer into haystack needle was found at, or NULL if not found.
176 static char *findstr(char * haystack, int hlen, char * needle, int nlen)
183 for (i = 0; i <= hlen - nlen; i++) {
184 if (!strncmp(haystack + i, needle, nlen))
193 /* ******************************************************************************** */
194 /* EN50221 physical interface functions */
200 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
207 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
208 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
212 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
214 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
215 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
217 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
218 cam_changed = (cam_present_now != cam_present_old);
222 if (!cam_present_now) {
223 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
225 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
227 atomic_set(&ca->slot_info[slot].camchange_count, 1);
229 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
230 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
231 // move to validate state if reset is completed
232 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
241 * Wait for flags to become set on the STATUS register on a CAM interface,
242 * checking for errors and timeout.
244 * @param ca CA instance.
245 * @param slot Slot on interface.
246 * @param waitfor Flags to wait for.
247 * @param timeout_ms Timeout in milliseconds.
249 * @return 0 on success, nonzero on error.
251 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
252 u8 waitfor, int timeout_hz)
254 unsigned long timeout;
257 dprintk("%s\n", __func__);
259 /* loop until timeout elapsed */
261 timeout = jiffies + timeout_hz;
263 /* read the status and check for error */
264 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
268 /* if we got the flags, it was successful! */
270 dprintk("%s succeeded timeout:%lu\n", __func__, jiffies - start);
274 /* check for timeout */
275 if (time_after(jiffies, timeout)) {
283 dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
285 /* if we get here, we've timed out */
291 * Initialise the link layer connection to a CAM.
293 * @param ca CA instance.
294 * @param slot Slot id.
296 * @return 0 on success, nonzero on failure.
298 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
304 dprintk("%s\n", __func__);
306 /* we'll be determining these during this function */
307 ca->slot_info[slot].da_irq_supported = 0;
309 /* set the host link buffer size temporarily. it will be overwritten with the
310 * real negotiated size later. */
311 ca->slot_info[slot].link_buf_size = 2;
313 /* read the buffer size from the CAM */
314 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
316 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
318 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
320 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
323 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
324 buf_size = (buf[0] << 8) | buf[1];
325 if (buf_size > HOST_LINK_BUF_SIZE)
326 buf_size = HOST_LINK_BUF_SIZE;
327 ca->slot_info[slot].link_buf_size = buf_size;
328 buf[0] = buf_size >> 8;
329 buf[1] = buf_size & 0xff;
330 dprintk("Chosen link buffer size of %i\n", buf_size);
332 /* write the buffer size to the CAM */
333 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
335 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
337 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
339 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
347 * Read a tuple from attribute memory.
349 * @param ca CA instance.
350 * @param slot Slot id.
351 * @param address Address to read from. Updated.
352 * @param tupleType Tuple id byte. Updated.
353 * @param tupleLength Tuple length. Updated.
354 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
356 * @return 0 on success, nonzero on error.
358 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
359 int *address, int *tupleType, int *tupleLength, u8 * tuple)
364 int _address = *address;
366 /* grab the next tuple length and type */
367 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
369 if (_tupleType == 0xff) {
370 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
372 *tupleType = _tupleType;
376 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
380 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
382 /* read in the whole tuple */
383 for (i = 0; i < _tupleLength; i++) {
384 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2));
385 dprintk(" 0x%02x: 0x%02x %c\n",
387 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
389 _address += (_tupleLength * 2);
392 *tupleType = _tupleType;
393 *tupleLength = _tupleLength;
400 * Parse attribute memory of a CAM module, extracting Config register, and checking
401 * it is a DVB CAM module.
403 * @param ca CA instance.
404 * @param slot Slot id.
406 * @return 0 on success, <0 on failure.
408 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
417 int got_cftableentry = 0;
426 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
428 if (tupleType != 0x1D)
435 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
437 if (tupleType != 0x1C)
444 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
446 if (tupleType != 0x15)
452 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
453 &tupleLength, tuple)) < 0)
455 if (tupleType != 0x20)
457 if (tupleLength != 4)
459 manfid = (tuple[1] << 8) | tuple[0];
460 devid = (tuple[3] << 8) | tuple[2];
465 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
466 &tupleLength, tuple)) < 0)
468 if (tupleType != 0x1A)
473 /* extract the configbase */
475 if (tupleLength < (3 + rasz + 14))
477 ca->slot_info[slot].config_base = 0;
478 for (i = 0; i < rasz + 1; i++) {
479 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i));
482 /* check it contains the correct DVB string */
483 dvb_str = findstr((char *)tuple, tupleLength, "DVB_CI_V", 8);
486 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
489 /* is it a version we support? */
490 if (strncmp(dvb_str + 8, "1.00", 4)) {
491 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
492 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]);
496 /* process the CFTABLE_ENTRY tuples, and any after those */
497 while ((!end_chain) && (address < 0x1000)) {
498 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
499 &tupleLength, tuple)) < 0)
502 case 0x1B: // CISTPL_CFTABLE_ENTRY
503 if (tupleLength < (2 + 11 + 17))
506 /* if we've already parsed one, just use it */
507 if (got_cftableentry)
510 /* get the config option */
511 ca->slot_info[slot].config_option = tuple[0] & 0x3f;
513 /* OK, check it contains the correct strings */
514 if ((findstr((char *)tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
515 (findstr((char *)tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
518 got_cftableentry = 1;
521 case 0x14: // CISTPL_NO_LINK
524 case 0xFF: // CISTPL_END
528 default: /* Unknown tuple type - just skip this tuple and move to the next one */
529 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType,
535 if ((address > 0x1000) || (!got_cftableentry))
538 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
539 manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option);
547 * Set CAM's configoption correctly.
549 * @param ca CA instance.
550 * @param slot Slot containing the CAM.
552 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
556 dprintk("%s\n", __func__);
558 /* set the config option */
559 ca->pub->write_attribute_mem(ca->pub, slot,
560 ca->slot_info[slot].config_base,
561 ca->slot_info[slot].config_option);
564 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
565 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
566 ca->slot_info[slot].config_option, configoption & 0x3f);
575 * This function talks to an EN50221 CAM control interface. It reads a buffer of
576 * data from the CAM. The data can either be stored in a supplied buffer, or
577 * automatically be added to the slot's rx_buffer.
579 * @param ca CA instance.
580 * @param slot Slot to read from.
581 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
582 * the data will be added into the buffering system as a normal fragment.
583 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
585 * @return Number of bytes read, or < 0 on error
587 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount)
591 u8 buf[HOST_LINK_BUF_SIZE];
594 dprintk("%s\n", __func__);
596 /* check if we have space for a link buf in the rx_buffer */
600 if (ca->slot_info[slot].rx_buffer.data == NULL) {
604 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
606 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
612 /* check if there is data available */
613 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
615 if (!(status & STATUSREG_DA)) {
621 /* read the amount of data */
622 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
624 bytes_read = status << 8;
625 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
627 bytes_read |= status;
629 /* check it will fit */
631 if (bytes_read > ca->slot_info[slot].link_buf_size) {
632 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
633 ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size);
634 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
638 if (bytes_read < 2) {
639 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
640 ca->dvbdev->adapter->num);
641 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
646 if (bytes_read > ecount) {
647 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
648 ca->dvbdev->adapter->num);
654 /* fill the buffer */
655 for (i = 0; i < bytes_read; i++) {
656 /* read byte and check */
657 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
660 /* OK, store it in the buffer */
664 /* check for read error (RE should now be 0) */
665 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
667 if (status & STATUSREG_RE) {
668 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
673 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
675 if (ca->slot_info[slot].rx_buffer.data == NULL) {
679 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
681 memcpy(ebuf, buf, bytes_read);
684 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
685 buf[0], (buf[1] & 0x80) == 0, bytes_read);
687 /* wake up readers when a last_fragment is received */
688 if ((buf[1] & 0x80) == 0x00) {
689 wake_up_interruptible(&ca->wait_queue);
699 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
702 * @param ca CA instance.
703 * @param slot Slot to write to.
704 * @param ebuf The data in this buffer is treated as a complete link-level packet to
706 * @param count Size of ebuf.
708 * @return Number of bytes written, or < 0 on error.
710 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write)
715 dprintk("%s\n", __func__);
719 if (bytes_write > ca->slot_info[slot].link_buf_size)
722 /* it is possible we are dealing with a single buffer implementation,
723 thus if there is data available for read or if there is even a read
724 already in progress, we do nothing but awake the kernel thread to
725 process the data if necessary. */
726 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
728 if (status & (STATUSREG_DA | STATUSREG_RE)) {
729 if (status & STATUSREG_DA)
730 dvb_ca_en50221_thread_wakeup(ca);
737 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
738 IRQEN | CMDREG_HC)) != 0)
741 /* check if interface is still free */
742 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
744 if (!(status & STATUSREG_FR)) {
745 /* it wasn't free => try again later */
750 /* send the amount of data */
751 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
753 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
754 bytes_write & 0xff)) != 0)
757 /* send the buffer */
758 for (i = 0; i < bytes_write; i++) {
759 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
763 /* check for write error (WE should now be 0) */
764 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
766 if (status & STATUSREG_WE) {
767 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
771 status = bytes_write;
773 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
774 buf[0], (buf[1] & 0x80) == 0, bytes_write);
777 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
782 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
786 /* ******************************************************************************** */
787 /* EN50221 higher level functions */
791 * A CAM has been removed => shut it down.
793 * @param ca CA instance.
794 * @param slot Slot to shut down.
796 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
798 dprintk("%s\n", __func__);
800 ca->pub->slot_shutdown(ca->pub, slot);
801 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
803 /* need to wake up all processes to check if they're now
804 trying to write to a defunct CAM */
805 wake_up_interruptible(&ca->wait_queue);
807 dprintk("Slot %i shutdown\n", slot);
812 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
816 * A CAMCHANGE IRQ has occurred.
818 * @param ca CA instance.
819 * @param slot Slot concerned.
820 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
822 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
824 struct dvb_ca_private *ca = pubca->private;
826 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
828 switch (change_type) {
829 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
830 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
837 ca->slot_info[slot].camchange_type = change_type;
838 atomic_inc(&ca->slot_info[slot].camchange_count);
839 dvb_ca_en50221_thread_wakeup(ca);
841 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
845 * A CAMREADY IRQ has occurred.
847 * @param ca CA instance.
848 * @param slot Slot concerned.
850 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
852 struct dvb_ca_private *ca = pubca->private;
854 dprintk("CAMREADY IRQ slot:%i\n", slot);
856 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
857 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
858 dvb_ca_en50221_thread_wakeup(ca);
864 * An FR or DA IRQ has occurred.
866 * @param ca CA instance.
867 * @param slot Slot concerned.
869 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
871 struct dvb_ca_private *ca = pubca->private;
874 dprintk("FR/DA IRQ slot:%i\n", slot);
876 switch (ca->slot_info[slot].slot_state) {
877 case DVB_CA_SLOTSTATE_LINKINIT:
878 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
879 if (flags & STATUSREG_DA) {
880 dprintk("CAM supports DA IRQ\n");
881 ca->slot_info[slot].da_irq_supported = 1;
885 case DVB_CA_SLOTSTATE_RUNNING:
887 dvb_ca_en50221_thread_wakeup(ca);
894 /* ******************************************************************************** */
895 /* EN50221 thread functions */
898 * Wake up the DVB CA thread
900 * @param ca CA instance.
902 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
905 dprintk("%s\n", __func__);
909 wake_up_process(ca->thread);
913 * Update the delay used by the thread.
915 * @param ca CA instance.
917 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
920 int curdelay = 100000000;
923 /* Beware of too high polling frequency, because one polling
924 * call might take several hundred milliseconds until timeout!
926 for (slot = 0; slot < ca->slot_count; slot++) {
927 switch (ca->slot_info[slot].slot_state) {
929 case DVB_CA_SLOTSTATE_NONE:
930 delay = HZ * 60; /* 60s */
931 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
932 delay = HZ * 5; /* 5s */
934 case DVB_CA_SLOTSTATE_INVALID:
935 delay = HZ * 60; /* 60s */
936 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
937 delay = HZ / 10; /* 100ms */
940 case DVB_CA_SLOTSTATE_UNINITIALISED:
941 case DVB_CA_SLOTSTATE_WAITREADY:
942 case DVB_CA_SLOTSTATE_VALIDATE:
943 case DVB_CA_SLOTSTATE_WAITFR:
944 case DVB_CA_SLOTSTATE_LINKINIT:
945 delay = HZ / 10; /* 100ms */
948 case DVB_CA_SLOTSTATE_RUNNING:
949 delay = HZ * 60; /* 60s */
950 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
951 delay = HZ / 10; /* 100ms */
953 if ((!ca->slot_info[slot].da_irq_supported) ||
954 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
955 delay = HZ / 10; /* 100ms */
960 if (delay < curdelay)
964 ca->delay = curdelay;
970 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
972 static int dvb_ca_en50221_thread(void *data)
974 struct dvb_ca_private *ca = data;
981 dprintk("%s\n", __func__);
983 /* choose the correct initial delay */
984 dvb_ca_en50221_thread_update_delay(ca);
987 while (!kthread_should_stop()) {
988 /* sleep for a bit */
990 set_current_state(TASK_INTERRUPTIBLE);
991 schedule_timeout(ca->delay);
992 if (kthread_should_stop())
997 /* go through all the slots processing them */
998 for (slot = 0; slot < ca->slot_count; slot++) {
1000 mutex_lock(&ca->slot_info[slot].slot_lock);
1002 // check the cam status + deal with CAMCHANGEs
1003 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1004 /* clear down an old CI slot if necessary */
1005 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
1006 dvb_ca_en50221_slot_shutdown(ca, slot);
1008 /* if a CAM is NOW present, initialise it */
1009 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
1010 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1013 /* we've handled one CAMCHANGE */
1014 dvb_ca_en50221_thread_update_delay(ca);
1015 atomic_dec(&ca->slot_info[slot].camchange_count);
1018 // CAM state machine
1019 switch (ca->slot_info[slot].slot_state) {
1020 case DVB_CA_SLOTSTATE_NONE:
1021 case DVB_CA_SLOTSTATE_INVALID:
1025 case DVB_CA_SLOTSTATE_UNINITIALISED:
1026 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1027 ca->pub->slot_reset(ca->pub, slot);
1028 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1031 case DVB_CA_SLOTSTATE_WAITREADY:
1032 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1033 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1034 ca->dvbdev->adapter->num);
1035 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1036 dvb_ca_en50221_thread_update_delay(ca);
1039 // no other action needed; will automatically change state when ready
1042 case DVB_CA_SLOTSTATE_VALIDATE:
1043 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1044 /* we need this extra check for annoying interfaces like the budget-av */
1045 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1046 (ca->pub->poll_slot_status)) {
1047 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1048 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1049 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1050 dvb_ca_en50221_thread_update_delay(ca);
1055 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1056 ca->dvbdev->adapter->num);
1057 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1058 dvb_ca_en50221_thread_update_delay(ca);
1061 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1062 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1063 ca->dvbdev->adapter->num);
1064 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1065 dvb_ca_en50221_thread_update_delay(ca);
1068 if (ca->pub->write_cam_control(ca->pub, slot,
1069 CTRLIF_COMMAND, CMDREG_RS) != 0) {
1070 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1071 ca->dvbdev->adapter->num);
1072 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1073 dvb_ca_en50221_thread_update_delay(ca);
1076 dprintk("DVB CAM validated successfully\n");
1078 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1079 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
1083 case DVB_CA_SLOTSTATE_WAITFR:
1084 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1085 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1086 ca->dvbdev->adapter->num);
1087 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1088 dvb_ca_en50221_thread_update_delay(ca);
1092 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1093 if (flags & STATUSREG_FR) {
1094 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1099 case DVB_CA_SLOTSTATE_LINKINIT:
1100 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1101 /* we need this extra check for annoying interfaces like the budget-av */
1102 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1103 (ca->pub->poll_slot_status)) {
1104 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1105 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1106 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1107 dvb_ca_en50221_thread_update_delay(ca);
1112 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
1113 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1114 dvb_ca_en50221_thread_update_delay(ca);
1118 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1119 rxbuf = vmalloc(RX_BUFFER_SIZE);
1120 if (rxbuf == NULL) {
1121 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
1122 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1123 dvb_ca_en50221_thread_update_delay(ca);
1126 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
1129 ca->pub->slot_ts_enable(ca->pub, slot);
1130 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
1131 dvb_ca_en50221_thread_update_delay(ca);
1132 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num);
1135 case DVB_CA_SLOTSTATE_RUNNING:
1139 // poll slots for data
1141 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1145 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1146 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1147 // we dont want to sleep on the next iteration so we can handle the cam change
1152 /* check if we've hit our limit this time */
1153 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1154 // dont sleep; there is likely to be more data to read
1162 mutex_unlock(&ca->slot_info[slot].slot_lock);
1171 /* ******************************************************************************** */
1172 /* EN50221 IO interface functions */
1175 * Real ioctl implementation.
1176 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1178 * @param inode Inode concerned.
1179 * @param file File concerned.
1180 * @param cmd IOCTL command.
1181 * @param arg Associated argument.
1183 * @return 0 on success, <0 on error.
1185 static int dvb_ca_en50221_io_do_ioctl(struct file *file,
1186 unsigned int cmd, void *parg)
1188 struct dvb_device *dvbdev = file->private_data;
1189 struct dvb_ca_private *ca = dvbdev->priv;
1193 dprintk("%s\n", __func__);
1197 for (slot = 0; slot < ca->slot_count; slot++) {
1198 mutex_lock(&ca->slot_info[slot].slot_lock);
1199 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
1200 dvb_ca_en50221_slot_shutdown(ca, slot);
1201 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1202 dvb_ca_en50221_camchange_irq(ca->pub,
1204 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1206 mutex_unlock(&ca->slot_info[slot].slot_lock);
1208 ca->next_read_slot = 0;
1209 dvb_ca_en50221_thread_wakeup(ca);
1213 struct ca_caps *caps = parg;
1215 caps->slot_num = ca->slot_count;
1216 caps->slot_type = CA_CI_LINK;
1217 caps->descr_num = 0;
1218 caps->descr_type = 0;
1222 case CA_GET_SLOT_INFO: {
1223 struct ca_slot_info *info = parg;
1225 if ((info->num > ca->slot_count) || (info->num < 0))
1228 info->type = CA_CI_LINK;
1230 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
1231 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1232 info->flags = CA_CI_MODULE_PRESENT;
1234 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1235 info->flags |= CA_CI_MODULE_READY;
1250 * Wrapper for ioctl implementation.
1252 * @param inode Inode concerned.
1253 * @param file File concerned.
1254 * @param cmd IOCTL command.
1255 * @param arg Associated argument.
1257 * @return 0 on success, <0 on error.
1259 static long dvb_ca_en50221_io_ioctl(struct file *file,
1260 unsigned int cmd, unsigned long arg)
1265 ret = dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1273 * Implementation of write() syscall.
1275 * @param file File structure.
1276 * @param buf Source buffer.
1277 * @param count Size of source buffer.
1278 * @param ppos Position in file (ignored).
1280 * @return Number of bytes read, or <0 on error.
1282 static ssize_t dvb_ca_en50221_io_write(struct file *file,
1283 const char __user * buf, size_t count, loff_t * ppos)
1285 struct dvb_device *dvbdev = file->private_data;
1286 struct dvb_ca_private *ca = dvbdev->priv;
1287 u8 slot, connection_id;
1289 u8 fragbuf[HOST_LINK_BUF_SIZE];
1292 unsigned long timeout;
1295 dprintk("%s\n", __func__);
1297 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1301 /* extract slot & connection id */
1302 if (copy_from_user(&slot, buf, 1))
1304 if (copy_from_user(&connection_id, buf + 1, 1))
1309 /* check if the slot is actually running */
1310 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1313 /* fragment the packets & store in the buffer */
1314 while (fragpos < count) {
1315 fraglen = ca->slot_info[slot].link_buf_size - 2;
1316 if ((count - fragpos) < fraglen)
1317 fraglen = count - fragpos;
1319 fragbuf[0] = connection_id;
1320 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1321 if ((status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen)) != 0)
1324 timeout = jiffies + HZ / 2;
1326 while (!time_after(jiffies, timeout)) {
1327 /* check the CAM hasn't been removed/reset in the meantime */
1328 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1333 mutex_lock(&ca->slot_info[slot].slot_lock);
1334 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
1335 mutex_unlock(&ca->slot_info[slot].slot_lock);
1336 if (status == (fraglen + 2)) {
1340 if (status != -EAGAIN)
1360 * Condition for waking up in dvb_ca_en50221_io_read_condition
1362 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
1363 int *result, int *_slot)
1369 int connection_id = -1;
1373 slot = ca->next_read_slot;
1374 while ((slot_count < ca->slot_count) && (!found)) {
1375 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1378 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1382 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1384 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1385 if (connection_id == -1)
1386 connection_id = hdr[0];
1387 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
1393 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1397 slot = (slot + 1) % ca->slot_count;
1401 ca->next_read_slot = slot;
1407 * Implementation of read() syscall.
1409 * @param file File structure.
1410 * @param buf Destination buffer.
1411 * @param count Size of destination buffer.
1412 * @param ppos Position in file (ignored).
1414 * @return Number of bytes read, or <0 on error.
1416 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
1417 size_t count, loff_t * ppos)
1419 struct dvb_device *dvbdev = file->private_data;
1420 struct dvb_ca_private *ca = dvbdev->priv;
1425 int connection_id = -1;
1427 int last_fragment = 0;
1432 dprintk("%s\n", __func__);
1434 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1438 /* wait for some data */
1439 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
1441 /* if we're in nonblocking mode, exit immediately */
1442 if (file->f_flags & O_NONBLOCK)
1443 return -EWOULDBLOCK;
1445 /* wait for some data */
1446 status = wait_event_interruptible(ca->wait_queue,
1447 dvb_ca_en50221_io_read_condition
1448 (ca, &result, &slot));
1450 if ((status < 0) || (result < 0)) {
1456 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1460 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
1465 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1466 if (connection_id == -1)
1467 connection_id = hdr[0];
1468 if (hdr[0] == connection_id) {
1469 if (pktlen < count) {
1470 if ((pktlen + fraglen - 2) > count) {
1471 fraglen = count - pktlen;
1476 if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
1477 buf + pktlen, fraglen)) < 0) {
1483 if ((hdr[1] & 0x80) == 0)
1488 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1490 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
1493 } while (!last_fragment);
1496 hdr[1] = connection_id;
1497 if ((status = copy_to_user(buf, hdr, 2)) != 0)
1507 * Implementation of file open syscall.
1509 * @param inode Inode concerned.
1510 * @param file File concerned.
1512 * @return 0 on success, <0 on failure.
1514 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1516 struct dvb_device *dvbdev = file->private_data;
1517 struct dvb_ca_private *ca = dvbdev->priv;
1521 dprintk("%s\n", __func__);
1523 if (!try_module_get(ca->pub->owner))
1526 err = dvb_generic_open(inode, file);
1528 module_put(ca->pub->owner);
1532 for (i = 0; i < ca->slot_count; i++) {
1534 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1535 if (ca->slot_info[i].rx_buffer.data != NULL) {
1536 /* it is safe to call this here without locks because
1537 * ca->open == 0. Data is not read in this case */
1538 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
1544 dvb_ca_en50221_thread_update_delay(ca);
1545 dvb_ca_en50221_thread_wakeup(ca);
1552 * Implementation of file close syscall.
1554 * @param inode Inode concerned.
1555 * @param file File concerned.
1557 * @return 0 on success, <0 on failure.
1559 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1561 struct dvb_device *dvbdev = file->private_data;
1562 struct dvb_ca_private *ca = dvbdev->priv;
1565 dprintk("%s\n", __func__);
1567 /* mark the CA device as closed */
1569 dvb_ca_en50221_thread_update_delay(ca);
1571 err = dvb_generic_release(inode, file);
1573 module_put(ca->pub->owner);
1580 * Implementation of poll() syscall.
1582 * @param file File concerned.
1583 * @param wait poll wait table.
1585 * @return Standard poll mask.
1587 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
1589 struct dvb_device *dvbdev = file->private_data;
1590 struct dvb_ca_private *ca = dvbdev->priv;
1591 unsigned int mask = 0;
1595 dprintk("%s\n", __func__);
1597 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1601 /* if there is something, return now */
1605 /* wait for something to happen */
1606 poll_wait(file, &ca->wait_queue, wait);
1608 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1614 EXPORT_SYMBOL(dvb_ca_en50221_init);
1617 static const struct file_operations dvb_ca_fops = {
1618 .owner = THIS_MODULE,
1619 .read = dvb_ca_en50221_io_read,
1620 .write = dvb_ca_en50221_io_write,
1621 .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
1622 .open = dvb_ca_en50221_io_open,
1623 .release = dvb_ca_en50221_io_release,
1624 .poll = dvb_ca_en50221_io_poll,
1627 static struct dvb_device dvbdev_ca = {
1632 .fops = &dvb_ca_fops,
1636 /* ******************************************************************************** */
1637 /* Initialisation/shutdown functions */
1641 * Initialise a new DVB CA EN50221 interface device.
1643 * @param dvb_adapter DVB adapter to attach the new CA device to.
1644 * @param ca The dvb_ca instance.
1645 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1646 * @param slot_count Number of slots supported.
1648 * @return 0 on success, nonzero on failure
1650 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1651 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1654 struct dvb_ca_private *ca = NULL;
1657 dprintk("%s\n", __func__);
1662 /* initialise the system data */
1663 if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
1669 ca->slot_count = slot_count;
1670 if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
1674 init_waitqueue_head(&ca->wait_queue);
1677 ca->next_read_slot = 0;
1678 pubca->private = ca;
1680 /* register the DVB device */
1681 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA);
1685 /* now initialise each slot */
1686 for (i = 0; i < slot_count; i++) {
1687 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
1688 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
1689 atomic_set(&ca->slot_info[i].camchange_count, 0);
1690 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1691 mutex_init(&ca->slot_info[i].slot_lock);
1694 if (signal_pending(current)) {
1700 /* create a kthread for monitoring this CA device */
1701 ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
1702 ca->dvbdev->adapter->num, ca->dvbdev->id);
1703 if (IS_ERR(ca->thread)) {
1704 ret = PTR_ERR(ca->thread);
1705 printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
1713 if (ca->dvbdev != NULL)
1714 dvb_unregister_device(ca->dvbdev);
1715 kfree(ca->slot_info);
1718 pubca->private = NULL;
1721 EXPORT_SYMBOL(dvb_ca_en50221_release);
1726 * Release a DVB CA EN50221 interface device.
1728 * @param ca_dev The dvb_device_t instance for the CA device.
1729 * @param ca The associated dvb_ca instance.
1731 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1733 struct dvb_ca_private *ca = pubca->private;
1736 dprintk("%s\n", __func__);
1738 /* shutdown the thread if there was one */
1739 kthread_stop(ca->thread);
1741 for (i = 0; i < ca->slot_count; i++) {
1742 dvb_ca_en50221_slot_shutdown(ca, i);
1743 vfree(ca->slot_info[i].rx_buffer.data);
1745 kfree(ca->slot_info);
1746 dvb_unregister_device(ca->dvbdev);
1748 pubca->private = NULL;