1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces
5 * Copyright (C) 2004 Andrew de Quincey
7 * Parts of this file were based on sources as follows:
13 * Copyright (C) 1999-2002 Ralph Metzler
14 * & Marcus Metzler for convergence integrated media GmbH
17 #define pr_fmt(fmt) "dvb_ca_en50221: " fmt
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/nospec.h>
24 #include <linux/vmalloc.h>
25 #include <linux/delay.h>
26 #include <linux/spinlock.h>
27 #include <linux/sched/signal.h>
28 #include <linux/kthread.h>
30 #include <media/dvb_ca_en50221.h>
31 #include <media/dvb_ringbuffer.h>
33 static int dvb_ca_en50221_debug;
35 module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
36 MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
38 #define dprintk(fmt, arg...) do { \
39 if (dvb_ca_en50221_debug) \
40 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg);\
43 #define INIT_TIMEOUT_SECS 10
45 #define HOST_LINK_BUF_SIZE 0x200
47 #define RX_BUFFER_SIZE 65535
49 #define MAX_RX_PACKETS_PER_ITERATION 10
52 #define CTRLIF_COMMAND 1
53 #define CTRLIF_STATUS 1
54 #define CTRLIF_SIZE_LOW 2
55 #define CTRLIF_SIZE_HIGH 3
57 #define CMDREG_HC 1 /* Host control */
58 #define CMDREG_SW 2 /* Size write */
59 #define CMDREG_SR 4 /* Size read */
60 #define CMDREG_RS 8 /* Reset interface */
61 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
62 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
63 #define IRQEN (CMDREG_DAIE)
65 #define STATUSREG_RE 1 /* read error */
66 #define STATUSREG_WE 2 /* write error */
67 #define STATUSREG_FR 0x40 /* module free */
68 #define STATUSREG_DA 0x80 /* data available */
70 #define DVB_CA_SLOTSTATE_NONE 0
71 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
72 #define DVB_CA_SLOTSTATE_RUNNING 2
73 #define DVB_CA_SLOTSTATE_INVALID 3
74 #define DVB_CA_SLOTSTATE_WAITREADY 4
75 #define DVB_CA_SLOTSTATE_VALIDATE 5
76 #define DVB_CA_SLOTSTATE_WAITFR 6
77 #define DVB_CA_SLOTSTATE_LINKINIT 7
79 /* Information on a CA slot */
81 /* current state of the CAM */
84 /* mutex used for serializing access to one CI slot */
85 struct mutex slot_lock;
87 /* Number of CAMCHANGES that have occurred since last processing */
88 atomic_t camchange_count;
90 /* Type of last CAMCHANGE */
93 /* base address of CAM config */
96 /* value to write into Config Control register */
99 /* if 1, the CAM supports DA IRQs */
100 u8 da_irq_supported:1;
102 /* size of the buffer to use when talking to the CAM */
105 /* buffer for incoming packets */
106 struct dvb_ringbuffer rx_buffer;
108 /* timer used during various states of the slot */
109 unsigned long timeout;
112 /* Private CA-interface information */
113 struct dvb_ca_private {
114 struct kref refcount;
116 /* pointer back to the public data structure */
117 struct dvb_ca_en50221 *pub;
120 struct dvb_device *dvbdev;
122 /* Flags describing the interface (DVB_CA_FLAG_*) */
125 /* number of slots supported by this CA interface */
126 unsigned int slot_count;
128 /* information on each slot */
129 struct dvb_ca_slot *slot_info;
131 /* wait queues for read() and write() operations */
132 wait_queue_head_t wait_queue;
134 /* PID of the monitoring thread */
135 struct task_struct *thread;
137 /* Flag indicating if the CA device is open */
140 /* Flag indicating the thread should wake up now */
141 unsigned int wakeup:1;
143 /* Delay the main thread should use */
147 * Slot to start looking for data to read from in the next user-space
152 /* mutex serializing ioctls */
153 struct mutex ioctl_mutex;
156 static void dvb_ca_private_free(struct dvb_ca_private *ca)
160 dvb_free_device(ca->dvbdev);
161 for (i = 0; i < ca->slot_count; i++)
162 vfree(ca->slot_info[i].rx_buffer.data);
164 kfree(ca->slot_info);
168 static void dvb_ca_private_release(struct kref *ref)
170 struct dvb_ca_private *ca;
172 ca = container_of(ref, struct dvb_ca_private, refcount);
173 dvb_ca_private_free(ca);
176 static void dvb_ca_private_get(struct dvb_ca_private *ca)
178 kref_get(&ca->refcount);
181 static void dvb_ca_private_put(struct dvb_ca_private *ca)
183 kref_put(&ca->refcount, dvb_ca_private_release);
186 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
187 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot,
188 u8 *ebuf, int ecount);
189 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot,
190 u8 *ebuf, int ecount);
193 * Safely find needle in haystack.
195 * @haystack: Buffer to look in.
196 * @hlen: Number of bytes in haystack.
197 * @needle: Buffer to find.
198 * @nlen: Number of bytes in needle.
199 * return: Pointer into haystack needle was found at, or NULL if not found.
201 static char *findstr(char *haystack, int hlen, char *needle, int nlen)
208 for (i = 0; i <= hlen - nlen; i++) {
209 if (!strncmp(haystack + i, needle, nlen))
216 /* ************************************************************************** */
217 /* EN50221 physical interface functions */
220 * dvb_ca_en50221_check_camstatus - Check CAM status.
222 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
224 struct dvb_ca_slot *sl = &ca->slot_info[slot];
230 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
231 return (atomic_read(&sl->camchange_count) != 0);
234 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
236 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
237 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
239 int cam_present_old = (sl->slot_state != DVB_CA_SLOTSTATE_NONE);
241 cam_changed = (cam_present_now != cam_present_old);
245 if (!cam_present_now)
246 sl->camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
248 sl->camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
249 atomic_set(&sl->camchange_count, 1);
251 if ((sl->slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
252 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
253 /* move to validate state if reset is completed */
254 sl->slot_state = DVB_CA_SLOTSTATE_VALIDATE;
262 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
263 * register on a CAM interface, checking for errors and timeout.
266 * @slot: Slot on interface.
267 * @waitfor: Flags to wait for.
268 * @timeout_hz: Timeout in milliseconds.
270 * return: 0 on success, nonzero on error.
272 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
273 u8 waitfor, int timeout_hz)
275 unsigned long timeout;
278 dprintk("%s\n", __func__);
280 /* loop until timeout elapsed */
282 timeout = jiffies + timeout_hz;
286 /* read the status and check for error */
287 res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
291 /* if we got the flags, it was successful! */
293 dprintk("%s succeeded timeout:%lu\n",
294 __func__, jiffies - start);
298 /* check for timeout */
299 if (time_after(jiffies, timeout))
303 usleep_range(1000, 1100);
306 dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
308 /* if we get here, we've timed out */
313 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
318 * return: 0 on success, nonzero on failure.
320 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
322 struct dvb_ca_slot *sl = &ca->slot_info[slot];
327 dprintk("%s\n", __func__);
329 /* we'll be determining these during this function */
330 sl->da_irq_supported = 0;
333 * set the host link buffer size temporarily. it will be overwritten
334 * with the real negotiated size later.
336 sl->link_buf_size = 2;
338 /* read the buffer size from the CAM */
339 ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
343 ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ);
346 ret = dvb_ca_en50221_read_data(ca, slot, buf, 2);
349 ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
354 * store it, and choose the minimum of our buffer and the CAM's buffer
357 buf_size = (buf[0] << 8) | buf[1];
358 if (buf_size > HOST_LINK_BUF_SIZE)
359 buf_size = HOST_LINK_BUF_SIZE;
360 sl->link_buf_size = buf_size;
361 buf[0] = buf_size >> 8;
362 buf[1] = buf_size & 0xff;
363 dprintk("Chosen link buffer size of %i\n", buf_size);
365 /* write the buffer size to the CAM */
366 ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
370 ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10);
373 ret = dvb_ca_en50221_write_data(ca, slot, buf, 2);
376 ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
385 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
389 * @address: Address to read from. Updated.
390 * @tuple_type: Tuple id byte. Updated.
391 * @tuple_length: Tuple length. Updated.
392 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
394 * return: 0 on success, nonzero on error.
396 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
397 int *address, int *tuple_type,
398 int *tuple_length, u8 *tuple)
403 int _address = *address;
405 /* grab the next tuple length and type */
406 _tuple_type = ca->pub->read_attribute_mem(ca->pub, slot, _address);
409 if (_tuple_type == 0xff) {
410 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tuple_type);
412 *tuple_type = _tuple_type;
416 _tuple_length = ca->pub->read_attribute_mem(ca->pub, slot,
418 if (_tuple_length < 0)
419 return _tuple_length;
422 dprintk("TUPLE type:0x%x length:%i\n", _tuple_type, _tuple_length);
424 /* read in the whole tuple */
425 for (i = 0; i < _tuple_length; i++) {
426 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot,
428 dprintk(" 0x%02x: 0x%02x %c\n",
430 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
432 _address += (_tuple_length * 2);
435 *tuple_type = _tuple_type;
436 *tuple_length = _tuple_length;
442 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
443 * extracting Config register, and checking it is a DVB CAM module.
448 * return: 0 on success, <0 on failure.
450 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
452 struct dvb_ca_slot *sl;
460 int got_cftableentry = 0;
466 /* CISTPL_DEVICE_0A */
467 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
468 &tuple_length, tuple);
471 if (tuple_type != 0x1D)
474 /* CISTPL_DEVICE_0C */
475 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
476 &tuple_length, tuple);
479 if (tuple_type != 0x1C)
483 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
484 &tuple_length, tuple);
487 if (tuple_type != 0x15)
491 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
492 &tuple_length, tuple);
495 if (tuple_type != 0x20)
497 if (tuple_length != 4)
499 manfid = (tuple[1] << 8) | tuple[0];
500 devid = (tuple[3] << 8) | tuple[2];
503 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
504 &tuple_length, tuple);
507 if (tuple_type != 0x1A)
509 if (tuple_length < 3)
512 /* extract the configbase */
514 if (tuple_length < (3 + rasz + 14))
516 sl = &ca->slot_info[slot];
518 for (i = 0; i < rasz + 1; i++)
519 sl->config_base |= (tuple[2 + i] << (8 * i));
521 /* check it contains the correct DVB string */
522 dvb_str = findstr((char *)tuple, tuple_length, "DVB_CI_V", 8);
525 if (tuple_length < ((dvb_str - (char *)tuple) + 12))
528 /* is it a version we support? */
529 if (strncmp(dvb_str + 8, "1.00", 4)) {
530 pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
531 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9],
532 dvb_str[10], dvb_str[11]);
536 /* process the CFTABLE_ENTRY tuples, and any after those */
537 while ((!end_chain) && (address < 0x1000)) {
538 status = dvb_ca_en50221_read_tuple(ca, slot, &address,
539 &tuple_type, &tuple_length,
543 switch (tuple_type) {
544 case 0x1B: /* CISTPL_CFTABLE_ENTRY */
545 if (tuple_length < (2 + 11 + 17))
548 /* if we've already parsed one, just use it */
549 if (got_cftableentry)
552 /* get the config option */
553 sl->config_option = tuple[0] & 0x3f;
555 /* OK, check it contains the correct strings */
556 if (!findstr((char *)tuple, tuple_length,
558 !findstr((char *)tuple, tuple_length,
559 "DVB_CI_MODULE", 13))
562 got_cftableentry = 1;
565 case 0x14: /* CISTPL_NO_LINK */
568 case 0xFF: /* CISTPL_END */
572 default: /* Unknown tuple type - just skip this tuple */
573 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
574 tuple_type, tuple_length);
579 if ((address > 0x1000) || (!got_cftableentry))
582 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
583 manfid, devid, sl->config_base, sl->config_option);
590 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
593 * @slot: Slot containing the CAM.
595 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
597 struct dvb_ca_slot *sl = &ca->slot_info[slot];
600 dprintk("%s\n", __func__);
602 /* set the config option */
603 ca->pub->write_attribute_mem(ca->pub, slot, sl->config_base,
607 configoption = ca->pub->read_attribute_mem(ca->pub, slot,
609 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
610 sl->config_option, configoption & 0x3f);
617 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
618 * interface. It reads a buffer of data from the CAM. The data can either
619 * be stored in a supplied buffer, or automatically be added to the slot's
623 * @slot: Slot to read from.
624 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
625 * the data will be added into the buffering system as a normal
627 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
629 * return: Number of bytes read, or < 0 on error
631 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot,
632 u8 *ebuf, int ecount)
634 struct dvb_ca_slot *sl = &ca->slot_info[slot];
637 u8 buf[HOST_LINK_BUF_SIZE];
640 dprintk("%s\n", __func__);
642 /* check if we have space for a link buf in the rx_buffer */
646 if (!sl->rx_buffer.data) {
650 buf_free = dvb_ringbuffer_free(&sl->rx_buffer);
652 if (buf_free < (sl->link_buf_size +
653 DVB_RINGBUFFER_PKTHDRSIZE)) {
659 if (ca->pub->read_data &&
660 (sl->slot_state != DVB_CA_SLOTSTATE_LINKINIT)) {
662 status = ca->pub->read_data(ca->pub, slot, buf,
665 status = ca->pub->read_data(ca->pub, slot, buf, ecount);
672 /* check if there is data available */
673 status = ca->pub->read_cam_control(ca->pub, slot,
677 if (!(status & STATUSREG_DA)) {
683 /* read the amount of data */
684 status = ca->pub->read_cam_control(ca->pub, slot,
688 bytes_read = status << 8;
689 status = ca->pub->read_cam_control(ca->pub, slot,
693 bytes_read |= status;
695 /* check it will fit */
697 if (bytes_read > sl->link_buf_size) {
698 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
699 ca->dvbdev->adapter->num, bytes_read,
701 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
705 if (bytes_read < 2) {
706 pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
707 ca->dvbdev->adapter->num);
708 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
713 if (bytes_read > ecount) {
714 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
715 ca->dvbdev->adapter->num);
721 /* fill the buffer */
722 for (i = 0; i < bytes_read; i++) {
723 /* read byte and check */
724 status = ca->pub->read_cam_control(ca->pub, slot,
729 /* OK, store it in the buffer */
733 /* check for read error (RE should now be 0) */
734 status = ca->pub->read_cam_control(ca->pub, slot,
738 if (status & STATUSREG_RE) {
739 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
746 * OK, add it to the receive buffer, or copy into external buffer if
750 if (!sl->rx_buffer.data) {
754 dvb_ringbuffer_pkt_write(&sl->rx_buffer, buf, bytes_read);
756 memcpy(ebuf, buf, bytes_read);
759 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
760 buf[0], (buf[1] & 0x80) == 0, bytes_read);
762 /* wake up readers when a last_fragment is received */
763 if ((buf[1] & 0x80) == 0x00)
764 wake_up_interruptible(&ca->wait_queue);
773 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
774 * interface. It writes a buffer of data to a CAM.
777 * @slot: Slot to write to.
778 * @buf: The data in this buffer is treated as a complete link-level packet to
780 * @bytes_write: Size of ebuf.
782 * return: Number of bytes written, or < 0 on error.
784 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot,
785 u8 *buf, int bytes_write)
787 struct dvb_ca_slot *sl = &ca->slot_info[slot];
791 dprintk("%s\n", __func__);
794 if (bytes_write > sl->link_buf_size)
797 if (ca->pub->write_data &&
798 (sl->slot_state != DVB_CA_SLOTSTATE_LINKINIT))
799 return ca->pub->write_data(ca->pub, slot, buf, bytes_write);
802 * it is possible we are dealing with a single buffer implementation,
803 * thus if there is data available for read or if there is even a read
804 * already in progress, we do nothing but awake the kernel thread to
805 * process the data if necessary.
807 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
810 if (status & (STATUSREG_DA | STATUSREG_RE)) {
811 if (status & STATUSREG_DA)
812 dvb_ca_en50221_thread_wakeup(ca);
819 status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
824 /* check if interface is still free */
825 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
828 if (!(status & STATUSREG_FR)) {
829 /* it wasn't free => try again later */
835 * It may need some time for the CAM to settle down, or there might
836 * be a race condition between the CAM, writing HC and our last
837 * check for DA. This happens, if the CAM asserts DA, just after
838 * checking DA before we are setting HC. In this case it might be
839 * a bug in the CAM to keep the FR bit, the lower layer/HW
840 * communication requires a longer timeout or the CAM needs more
841 * time internally. But this happens in reality!
842 * We need to read the status from the HW again and do the same
843 * we did for the previous check for DA
845 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
849 if (status & (STATUSREG_DA | STATUSREG_RE)) {
850 if (status & STATUSREG_DA)
851 dvb_ca_en50221_thread_wakeup(ca);
857 /* send the amount of data */
858 status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH,
862 status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
867 /* send the buffer */
868 for (i = 0; i < bytes_write; i++) {
869 status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA,
875 /* check for write error (WE should now be 0) */
876 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
879 if (status & STATUSREG_WE) {
880 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
884 status = bytes_write;
886 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
887 buf[0], (buf[1] & 0x80) == 0, bytes_write);
890 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
896 /* ************************************************************************** */
897 /* EN50221 higher level functions */
900 * dvb_ca_en50221_slot_shutdown - A CAM has been removed => shut it down.
903 * @slot: Slot to shut down.
905 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
907 dprintk("%s\n", __func__);
909 ca->pub->slot_shutdown(ca->pub, slot);
910 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
913 * need to wake up all processes to check if they're now trying to
914 * write to a defunct CAM
916 wake_up_interruptible(&ca->wait_queue);
918 dprintk("Slot %i shutdown\n", slot);
925 * dvb_ca_en50221_camchange_irq - A CAMCHANGE IRQ has occurred.
927 * @pubca: CA instance.
928 * @slot: Slot concerned.
929 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
931 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot,
934 struct dvb_ca_private *ca = pubca->private;
935 struct dvb_ca_slot *sl = &ca->slot_info[slot];
937 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
939 switch (change_type) {
940 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
941 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
948 sl->camchange_type = change_type;
949 atomic_inc(&sl->camchange_count);
950 dvb_ca_en50221_thread_wakeup(ca);
952 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
955 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
957 * @pubca: CA instance.
958 * @slot: Slot concerned.
960 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
962 struct dvb_ca_private *ca = pubca->private;
963 struct dvb_ca_slot *sl = &ca->slot_info[slot];
965 dprintk("CAMREADY IRQ slot:%i\n", slot);
967 if (sl->slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
968 sl->slot_state = DVB_CA_SLOTSTATE_VALIDATE;
969 dvb_ca_en50221_thread_wakeup(ca);
972 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
975 * dvb_ca_en50221_frda_irq - An FR or DA IRQ has occurred.
977 * @pubca: CA instance.
978 * @slot: Slot concerned.
980 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
982 struct dvb_ca_private *ca = pubca->private;
983 struct dvb_ca_slot *sl = &ca->slot_info[slot];
986 dprintk("FR/DA IRQ slot:%i\n", slot);
988 switch (sl->slot_state) {
989 case DVB_CA_SLOTSTATE_LINKINIT:
990 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
991 if (flags & STATUSREG_DA) {
992 dprintk("CAM supports DA IRQ\n");
993 sl->da_irq_supported = 1;
997 case DVB_CA_SLOTSTATE_RUNNING:
999 dvb_ca_en50221_thread_wakeup(ca);
1003 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
1005 /* ************************************************************************** */
1006 /* EN50221 thread functions */
1009 * Wake up the DVB CA thread
1013 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
1015 dprintk("%s\n", __func__);
1019 wake_up_process(ca->thread);
1023 * Update the delay used by the thread.
1027 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
1030 int curdelay = 100000000;
1034 * Beware of too high polling frequency, because one polling
1035 * call might take several hundred milliseconds until timeout!
1037 for (slot = 0; slot < ca->slot_count; slot++) {
1038 struct dvb_ca_slot *sl = &ca->slot_info[slot];
1040 switch (sl->slot_state) {
1042 case DVB_CA_SLOTSTATE_NONE:
1043 delay = HZ * 60; /* 60s */
1044 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1045 delay = HZ * 5; /* 5s */
1047 case DVB_CA_SLOTSTATE_INVALID:
1048 delay = HZ * 60; /* 60s */
1049 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1050 delay = HZ / 10; /* 100ms */
1053 case DVB_CA_SLOTSTATE_UNINITIALISED:
1054 case DVB_CA_SLOTSTATE_WAITREADY:
1055 case DVB_CA_SLOTSTATE_VALIDATE:
1056 case DVB_CA_SLOTSTATE_WAITFR:
1057 case DVB_CA_SLOTSTATE_LINKINIT:
1058 delay = HZ / 10; /* 100ms */
1061 case DVB_CA_SLOTSTATE_RUNNING:
1062 delay = HZ * 60; /* 60s */
1063 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1064 delay = HZ / 10; /* 100ms */
1066 if ((!sl->da_irq_supported) ||
1067 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
1068 delay = HZ / 10; /* 100ms */
1073 if (delay < curdelay)
1077 ca->delay = curdelay;
1081 * Poll if the CAM is gone.
1084 * @slot: Slot to process.
1085 * return:: 0 .. no change
1086 * 1 .. CAM state changed
1089 static int dvb_ca_en50221_poll_cam_gone(struct dvb_ca_private *ca, int slot)
1095 * we need this extra check for annoying interfaces like the
1098 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1099 (ca->pub->poll_slot_status)) {
1100 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1102 DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1103 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1104 dvb_ca_en50221_thread_update_delay(ca);
1112 * Thread state machine for one CA slot to perform the data transfer.
1115 * @slot: Slot to process.
1117 static void dvb_ca_en50221_thread_state_machine(struct dvb_ca_private *ca,
1120 struct dvb_ca_slot *sl = &ca->slot_info[slot];
1125 mutex_lock(&sl->slot_lock);
1127 /* check the cam status + deal with CAMCHANGEs */
1128 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1129 /* clear down an old CI slot if necessary */
1130 if (sl->slot_state != DVB_CA_SLOTSTATE_NONE)
1131 dvb_ca_en50221_slot_shutdown(ca, slot);
1133 /* if a CAM is NOW present, initialise it */
1134 if (sl->camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED)
1135 sl->slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1137 /* we've handled one CAMCHANGE */
1138 dvb_ca_en50221_thread_update_delay(ca);
1139 atomic_dec(&sl->camchange_count);
1142 /* CAM state machine */
1143 switch (sl->slot_state) {
1144 case DVB_CA_SLOTSTATE_NONE:
1145 case DVB_CA_SLOTSTATE_INVALID:
1146 /* no action needed */
1149 case DVB_CA_SLOTSTATE_UNINITIALISED:
1150 sl->slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1151 ca->pub->slot_reset(ca->pub, slot);
1152 sl->timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1155 case DVB_CA_SLOTSTATE_WAITREADY:
1156 if (time_after(jiffies, sl->timeout)) {
1157 pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
1158 ca->dvbdev->adapter->num);
1159 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1160 dvb_ca_en50221_thread_update_delay(ca);
1164 * no other action needed; will automatically change state when
1169 case DVB_CA_SLOTSTATE_VALIDATE:
1170 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1171 if (dvb_ca_en50221_poll_cam_gone(ca, slot))
1174 pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1175 ca->dvbdev->adapter->num);
1176 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1177 dvb_ca_en50221_thread_update_delay(ca);
1180 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1181 pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1182 ca->dvbdev->adapter->num);
1183 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1184 dvb_ca_en50221_thread_update_delay(ca);
1187 if (ca->pub->write_cam_control(ca->pub, slot,
1190 pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
1191 ca->dvbdev->adapter->num);
1192 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1193 dvb_ca_en50221_thread_update_delay(ca);
1196 dprintk("DVB CAM validated successfully\n");
1198 sl->timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1199 sl->slot_state = DVB_CA_SLOTSTATE_WAITFR;
1203 case DVB_CA_SLOTSTATE_WAITFR:
1204 if (time_after(jiffies, sl->timeout)) {
1205 pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1206 ca->dvbdev->adapter->num);
1207 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1208 dvb_ca_en50221_thread_update_delay(ca);
1212 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1213 if (flags & STATUSREG_FR) {
1214 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1219 case DVB_CA_SLOTSTATE_LINKINIT:
1220 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1221 if (dvb_ca_en50221_poll_cam_gone(ca, slot))
1224 pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
1225 ca->dvbdev->adapter->num);
1226 sl->slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1227 dvb_ca_en50221_thread_update_delay(ca);
1231 if (!sl->rx_buffer.data) {
1232 rxbuf = vmalloc(RX_BUFFER_SIZE);
1234 pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1235 ca->dvbdev->adapter->num);
1236 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1237 dvb_ca_en50221_thread_update_delay(ca);
1240 dvb_ringbuffer_init(&sl->rx_buffer, rxbuf,
1244 ca->pub->slot_ts_enable(ca->pub, slot);
1245 sl->slot_state = DVB_CA_SLOTSTATE_RUNNING;
1246 dvb_ca_en50221_thread_update_delay(ca);
1247 pr_info("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
1248 ca->dvbdev->adapter->num);
1251 case DVB_CA_SLOTSTATE_RUNNING:
1255 /* poll slots for data */
1257 while (dvb_ca_en50221_read_data(ca, slot, NULL, 0) > 0) {
1262 * if a CAMCHANGE occurred at some point, do not do any
1263 * more processing of this slot
1265 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1267 * we don't want to sleep on the next iteration
1268 * so we can handle the cam change
1274 /* check if we've hit our limit this time */
1275 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1277 * don't sleep; there is likely to be more data
1287 mutex_unlock(&sl->slot_lock);
1291 * Kernel thread which monitors CA slots for CAM changes, and performs data
1294 static int dvb_ca_en50221_thread(void *data)
1296 struct dvb_ca_private *ca = data;
1299 dprintk("%s\n", __func__);
1301 /* choose the correct initial delay */
1302 dvb_ca_en50221_thread_update_delay(ca);
1305 while (!kthread_should_stop()) {
1306 /* sleep for a bit */
1308 set_current_state(TASK_INTERRUPTIBLE);
1309 schedule_timeout(ca->delay);
1310 if (kthread_should_stop())
1315 /* go through all the slots processing them */
1316 for (slot = 0; slot < ca->slot_count; slot++)
1317 dvb_ca_en50221_thread_state_machine(ca, slot);
1323 /* ************************************************************************** */
1324 /* EN50221 IO interface functions */
1327 * Real ioctl implementation.
1328 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1330 * @file: File concerned.
1331 * @cmd: IOCTL command.
1332 * @parg: Associated argument.
1334 * return: 0 on success, <0 on error.
1336 static int dvb_ca_en50221_io_do_ioctl(struct file *file,
1337 unsigned int cmd, void *parg)
1339 struct dvb_device *dvbdev = file->private_data;
1340 struct dvb_ca_private *ca = dvbdev->priv;
1344 dprintk("%s\n", __func__);
1346 if (mutex_lock_interruptible(&ca->ioctl_mutex))
1347 return -ERESTARTSYS;
1351 for (slot = 0; slot < ca->slot_count; slot++) {
1352 struct dvb_ca_slot *sl = &ca->slot_info[slot];
1354 mutex_lock(&sl->slot_lock);
1355 if (sl->slot_state != DVB_CA_SLOTSTATE_NONE) {
1356 dvb_ca_en50221_slot_shutdown(ca, slot);
1357 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1358 dvb_ca_en50221_camchange_irq(ca->pub,
1360 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1362 mutex_unlock(&sl->slot_lock);
1364 ca->next_read_slot = 0;
1365 dvb_ca_en50221_thread_wakeup(ca);
1369 struct ca_caps *caps = parg;
1371 caps->slot_num = ca->slot_count;
1372 caps->slot_type = CA_CI_LINK;
1373 caps->descr_num = 0;
1374 caps->descr_type = 0;
1378 case CA_GET_SLOT_INFO: {
1379 struct ca_slot_info *info = parg;
1380 struct dvb_ca_slot *sl;
1383 if ((slot >= ca->slot_count) || (slot < 0)) {
1388 info->type = CA_CI_LINK;
1390 sl = &ca->slot_info[slot];
1391 if ((sl->slot_state != DVB_CA_SLOTSTATE_NONE) &&
1392 (sl->slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1393 info->flags = CA_CI_MODULE_PRESENT;
1395 if (sl->slot_state == DVB_CA_SLOTSTATE_RUNNING)
1396 info->flags |= CA_CI_MODULE_READY;
1406 mutex_unlock(&ca->ioctl_mutex);
1411 * Wrapper for ioctl implementation.
1413 * @file: File concerned.
1414 * @cmd: IOCTL command.
1415 * @arg: Associated argument.
1417 * return: 0 on success, <0 on error.
1419 static long dvb_ca_en50221_io_ioctl(struct file *file,
1420 unsigned int cmd, unsigned long arg)
1422 return dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1426 * Implementation of write() syscall.
1428 * @file: File structure.
1429 * @buf: Source buffer.
1430 * @count: Size of source buffer.
1431 * @ppos: Position in file (ignored).
1433 * return: Number of bytes read, or <0 on error.
1435 static ssize_t dvb_ca_en50221_io_write(struct file *file,
1436 const char __user *buf, size_t count,
1439 struct dvb_device *dvbdev = file->private_data;
1440 struct dvb_ca_private *ca = dvbdev->priv;
1441 struct dvb_ca_slot *sl;
1442 u8 slot, connection_id;
1444 u8 fragbuf[HOST_LINK_BUF_SIZE];
1447 unsigned long timeout;
1450 dprintk("%s\n", __func__);
1453 * Incoming packet has a 2 byte header.
1454 * hdr[0] = slot_id, hdr[1] = connection_id
1459 /* extract slot & connection id */
1460 if (copy_from_user(&slot, buf, 1))
1462 if (copy_from_user(&connection_id, buf + 1, 1))
1467 if (slot >= ca->slot_count)
1469 slot = array_index_nospec(slot, ca->slot_count);
1470 sl = &ca->slot_info[slot];
1472 /* check if the slot is actually running */
1473 if (sl->slot_state != DVB_CA_SLOTSTATE_RUNNING)
1476 /* fragment the packets & store in the buffer */
1477 while (fragpos < count) {
1478 fraglen = sl->link_buf_size - 2;
1481 if (fraglen > HOST_LINK_BUF_SIZE - 2)
1482 fraglen = HOST_LINK_BUF_SIZE - 2;
1483 if ((count - fragpos) < fraglen)
1484 fraglen = count - fragpos;
1486 fragbuf[0] = connection_id;
1487 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1488 status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
1494 timeout = jiffies + HZ / 2;
1496 while (!time_after(jiffies, timeout)) {
1498 * check the CAM hasn't been removed/reset in the
1501 if (sl->slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1506 mutex_lock(&sl->slot_lock);
1507 status = dvb_ca_en50221_write_data(ca, slot, fragbuf,
1509 mutex_unlock(&sl->slot_lock);
1510 if (status == (fraglen + 2)) {
1514 if (status != -EAGAIN)
1517 usleep_range(1000, 1100);
1533 * Condition for waking up in dvb_ca_en50221_io_read_condition
1535 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
1536 int *result, int *_slot)
1542 int connection_id = -1;
1546 slot = ca->next_read_slot;
1547 while ((slot_count < ca->slot_count) && (!found)) {
1548 struct dvb_ca_slot *sl = &ca->slot_info[slot];
1550 if (sl->slot_state != DVB_CA_SLOTSTATE_RUNNING)
1553 if (!sl->rx_buffer.data)
1556 idx = dvb_ringbuffer_pkt_next(&sl->rx_buffer, -1, &fraglen);
1558 dvb_ringbuffer_pkt_read(&sl->rx_buffer, idx, 0, hdr, 2);
1559 if (connection_id == -1)
1560 connection_id = hdr[0];
1561 if ((hdr[0] == connection_id) &&
1562 ((hdr[1] & 0x80) == 0)) {
1568 idx = dvb_ringbuffer_pkt_next(&sl->rx_buffer, idx,
1573 slot = (slot + 1) % ca->slot_count;
1577 ca->next_read_slot = slot;
1582 * Implementation of read() syscall.
1584 * @file: File structure.
1585 * @buf: Destination buffer.
1586 * @count: Size of destination buffer.
1587 * @ppos: Position in file (ignored).
1589 * return: Number of bytes read, or <0 on error.
1591 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user *buf,
1592 size_t count, loff_t *ppos)
1594 struct dvb_device *dvbdev = file->private_data;
1595 struct dvb_ca_private *ca = dvbdev->priv;
1596 struct dvb_ca_slot *sl;
1601 int connection_id = -1;
1603 int last_fragment = 0;
1608 dprintk("%s\n", __func__);
1611 * Outgoing packet has a 2 byte header.
1612 * hdr[0] = slot_id, hdr[1] = connection_id
1617 /* wait for some data */
1618 status = dvb_ca_en50221_io_read_condition(ca, &result, &slot);
1620 /* if we're in nonblocking mode, exit immediately */
1621 if (file->f_flags & O_NONBLOCK)
1622 return -EWOULDBLOCK;
1624 /* wait for some data */
1625 status = wait_event_interruptible(ca->wait_queue,
1626 dvb_ca_en50221_io_read_condition
1627 (ca, &result, &slot));
1629 if ((status < 0) || (result < 0)) {
1635 sl = &ca->slot_info[slot];
1636 idx = dvb_ringbuffer_pkt_next(&sl->rx_buffer, -1, &fraglen);
1640 pr_err("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n",
1641 ca->dvbdev->adapter->num);
1646 dvb_ringbuffer_pkt_read(&sl->rx_buffer, idx, 0, hdr, 2);
1647 if (connection_id == -1)
1648 connection_id = hdr[0];
1649 if (hdr[0] == connection_id) {
1650 if (pktlen < count) {
1651 if ((pktlen + fraglen - 2) > count)
1652 fraglen = count - pktlen;
1657 dvb_ringbuffer_pkt_read_user(&sl->rx_buffer,
1667 if ((hdr[1] & 0x80) == 0)
1672 idx2 = dvb_ringbuffer_pkt_next(&sl->rx_buffer, idx, &fraglen);
1674 dvb_ringbuffer_pkt_dispose(&sl->rx_buffer, idx);
1677 } while (!last_fragment);
1680 hdr[1] = connection_id;
1681 status = copy_to_user(buf, hdr, 2);
1693 * Implementation of file open syscall.
1695 * @inode: Inode concerned.
1696 * @file: File concerned.
1698 * return: 0 on success, <0 on failure.
1700 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1702 struct dvb_device *dvbdev = file->private_data;
1703 struct dvb_ca_private *ca = dvbdev->priv;
1707 dprintk("%s\n", __func__);
1709 if (!try_module_get(ca->pub->owner))
1712 err = dvb_generic_open(inode, file);
1714 module_put(ca->pub->owner);
1718 for (i = 0; i < ca->slot_count; i++) {
1719 struct dvb_ca_slot *sl = &ca->slot_info[i];
1721 if (sl->slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1722 if (!sl->rx_buffer.data) {
1724 * it is safe to call this here without locks
1725 * because ca->open == 0. Data is not read in
1728 dvb_ringbuffer_flush(&sl->rx_buffer);
1734 dvb_ca_en50221_thread_update_delay(ca);
1735 dvb_ca_en50221_thread_wakeup(ca);
1737 dvb_ca_private_get(ca);
1743 * Implementation of file close syscall.
1745 * @inode: Inode concerned.
1746 * @file: File concerned.
1748 * return: 0 on success, <0 on failure.
1750 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1752 struct dvb_device *dvbdev = file->private_data;
1753 struct dvb_ca_private *ca = dvbdev->priv;
1756 dprintk("%s\n", __func__);
1758 /* mark the CA device as closed */
1760 dvb_ca_en50221_thread_update_delay(ca);
1762 err = dvb_generic_release(inode, file);
1764 module_put(ca->pub->owner);
1766 dvb_ca_private_put(ca);
1772 * Implementation of poll() syscall.
1774 * @file: File concerned.
1775 * @wait: poll wait table.
1777 * return: Standard poll mask.
1779 static __poll_t dvb_ca_en50221_io_poll(struct file *file, poll_table *wait)
1781 struct dvb_device *dvbdev = file->private_data;
1782 struct dvb_ca_private *ca = dvbdev->priv;
1787 dprintk("%s\n", __func__);
1789 poll_wait(file, &ca->wait_queue, wait);
1791 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1)
1794 /* if there is something, return now */
1798 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1)
1804 static const struct file_operations dvb_ca_fops = {
1805 .owner = THIS_MODULE,
1806 .read = dvb_ca_en50221_io_read,
1807 .write = dvb_ca_en50221_io_write,
1808 .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
1809 .open = dvb_ca_en50221_io_open,
1810 .release = dvb_ca_en50221_io_release,
1811 .poll = dvb_ca_en50221_io_poll,
1812 .llseek = noop_llseek,
1815 static const struct dvb_device dvbdev_ca = {
1820 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1821 .name = "dvb-ca-en50221",
1823 .fops = &dvb_ca_fops,
1826 /* ************************************************************************** */
1827 /* Initialisation/shutdown functions */
1830 * Initialise a new DVB CA EN50221 interface device.
1832 * @dvb_adapter: DVB adapter to attach the new CA device to.
1833 * @pubca: The dvb_ca instance.
1834 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1835 * @slot_count: Number of slots supported.
1837 * return: 0 on success, nonzero on failure
1839 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1840 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1843 struct dvb_ca_private *ca = NULL;
1846 dprintk("%s\n", __func__);
1851 /* initialise the system data */
1852 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1857 kref_init(&ca->refcount);
1860 ca->slot_count = slot_count;
1861 ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot),
1863 if (!ca->slot_info) {
1867 init_waitqueue_head(&ca->wait_queue);
1870 ca->next_read_slot = 0;
1871 pubca->private = ca;
1873 /* register the DVB device */
1874 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca,
1877 goto free_slot_info;
1879 /* now initialise each slot */
1880 for (i = 0; i < slot_count; i++) {
1881 struct dvb_ca_slot *sl = &ca->slot_info[i];
1883 memset(sl, 0, sizeof(struct dvb_ca_slot));
1884 sl->slot_state = DVB_CA_SLOTSTATE_NONE;
1885 atomic_set(&sl->camchange_count, 0);
1886 sl->camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1887 mutex_init(&sl->slot_lock);
1890 mutex_init(&ca->ioctl_mutex);
1892 if (signal_pending(current)) {
1894 goto unregister_device;
1898 /* create a kthread for monitoring this CA device */
1899 ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
1900 ca->dvbdev->adapter->num, ca->dvbdev->id);
1901 if (IS_ERR(ca->thread)) {
1902 ret = PTR_ERR(ca->thread);
1903 pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
1905 goto unregister_device;
1910 dvb_unregister_device(ca->dvbdev);
1912 kfree(ca->slot_info);
1916 pubca->private = NULL;
1919 EXPORT_SYMBOL(dvb_ca_en50221_init);
1922 * Release a DVB CA EN50221 interface device.
1924 * @pubca: The associated dvb_ca instance.
1926 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1928 struct dvb_ca_private *ca = pubca->private;
1931 dprintk("%s\n", __func__);
1933 /* shutdown the thread if there was one */
1934 kthread_stop(ca->thread);
1936 for (i = 0; i < ca->slot_count; i++)
1937 dvb_ca_en50221_slot_shutdown(ca, i);
1939 dvb_remove_device(ca->dvbdev);
1940 dvb_ca_private_put(ca);
1941 pubca->private = NULL;
1943 EXPORT_SYMBOL(dvb_ca_en50221_release);