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.
24 * To obtain the license, point your browser to
25 * http://www.gnu.org/copyleft/gpl.html
28 #define pr_fmt(fmt) "dvb_ca_en50221: " fmt
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
34 #include <linux/vmalloc.h>
35 #include <linux/delay.h>
36 #include <linux/spinlock.h>
37 #include <linux/sched/signal.h>
38 #include <linux/kthread.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(fmt, arg...) do { \
49 if (dvb_ca_en50221_debug) \
50 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg);\
53 #define INIT_TIMEOUT_SECS 10
55 #define HOST_LINK_BUF_SIZE 0x200
57 #define RX_BUFFER_SIZE 65535
59 #define MAX_RX_PACKETS_PER_ITERATION 10
62 #define CTRLIF_COMMAND 1
63 #define CTRLIF_STATUS 1
64 #define CTRLIF_SIZE_LOW 2
65 #define CTRLIF_SIZE_HIGH 3
67 #define CMDREG_HC 1 /* Host control */
68 #define CMDREG_SW 2 /* Size write */
69 #define CMDREG_SR 4 /* Size read */
70 #define CMDREG_RS 8 /* Reset interface */
71 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
72 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
73 #define IRQEN (CMDREG_DAIE)
75 #define STATUSREG_RE 1 /* read error */
76 #define STATUSREG_WE 2 /* write error */
77 #define STATUSREG_FR 0x40 /* module free */
78 #define STATUSREG_DA 0x80 /* data available */
79 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
82 #define DVB_CA_SLOTSTATE_NONE 0
83 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
84 #define DVB_CA_SLOTSTATE_RUNNING 2
85 #define DVB_CA_SLOTSTATE_INVALID 3
86 #define DVB_CA_SLOTSTATE_WAITREADY 4
87 #define DVB_CA_SLOTSTATE_VALIDATE 5
88 #define DVB_CA_SLOTSTATE_WAITFR 6
89 #define DVB_CA_SLOTSTATE_LINKINIT 7
92 /* Information on a CA slot */
95 /* current state of the CAM */
98 /* mutex used for serializing access to one CI slot */
99 struct mutex slot_lock;
101 /* Number of CAMCHANGES that have occurred since last processing */
102 atomic_t camchange_count;
104 /* Type of last CAMCHANGE */
107 /* base address of CAM config */
110 /* value to write into Config Control register */
113 /* if 1, the CAM supports DA IRQs */
114 u8 da_irq_supported:1;
116 /* size of the buffer to use when talking to the CAM */
119 /* buffer for incoming packets */
120 struct dvb_ringbuffer rx_buffer;
122 /* timer used during various states of the slot */
123 unsigned long timeout;
126 /* Private CA-interface information */
127 struct dvb_ca_private {
128 struct kref refcount;
130 /* pointer back to the public data structure */
131 struct dvb_ca_en50221 *pub;
134 struct dvb_device *dvbdev;
136 /* Flags describing the interface (DVB_CA_FLAG_*) */
139 /* number of slots supported by this CA interface */
140 unsigned int slot_count;
142 /* information on each slot */
143 struct dvb_ca_slot *slot_info;
145 /* wait queues for read() and write() operations */
146 wait_queue_head_t wait_queue;
148 /* PID of the monitoring thread */
149 struct task_struct *thread;
151 /* Flag indicating if the CA device is open */
154 /* Flag indicating the thread should wake up now */
155 unsigned int wakeup:1;
157 /* Delay the main thread should use */
160 /* Slot to start looking for data to read from in the next user-space read operation */
163 /* mutex serializing ioctls */
164 struct mutex ioctl_mutex;
167 static void dvb_ca_private_free(struct dvb_ca_private *ca)
171 dvb_free_device(ca->dvbdev);
172 for (i = 0; i < ca->slot_count; i++)
173 vfree(ca->slot_info[i].rx_buffer.data);
175 kfree(ca->slot_info);
179 static void dvb_ca_private_release(struct kref *ref)
181 struct dvb_ca_private *ca = container_of(ref, struct dvb_ca_private, refcount);
182 dvb_ca_private_free(ca);
185 static void dvb_ca_private_get(struct dvb_ca_private *ca)
187 kref_get(&ca->refcount);
190 static void dvb_ca_private_put(struct dvb_ca_private *ca)
192 kref_put(&ca->refcount, dvb_ca_private_release);
195 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
196 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot,
197 u8 *ebuf, int ecount);
198 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot,
199 u8 *ebuf, int ecount);
203 * Safely find needle in haystack.
205 * @haystack: Buffer to look in.
206 * @hlen: Number of bytes in haystack.
207 * @needle: Buffer to find.
208 * @nlen: Number of bytes in needle.
209 * @return Pointer into haystack needle was found at, or NULL if not found.
211 static char *findstr(char *haystack, int hlen, char *needle, int nlen)
218 for (i = 0; i <= hlen - nlen; i++) {
219 if (!strncmp(haystack + i, needle, nlen))
228 /* ******************************************************************************** */
229 /* EN50221 physical interface functions */
233 * dvb_ca_en50221_check_camstatus - Check CAM status.
235 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
242 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
243 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
247 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
249 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
250 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
252 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
253 cam_changed = (cam_present_now != cam_present_old);
257 if (!cam_present_now) {
258 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
260 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
262 atomic_set(&ca->slot_info[slot].camchange_count, 1);
264 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
265 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
266 // move to validate state if reset is completed
267 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
276 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
277 * register on a CAM interface, checking for errors and timeout.
280 * @slot: Slot on interface.
281 * @waitfor: Flags to wait for.
282 * @timeout_ms: Timeout in milliseconds.
284 * @return 0 on success, nonzero on error.
286 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
287 u8 waitfor, int timeout_hz)
289 unsigned long timeout;
292 dprintk("%s\n", __func__);
294 /* loop until timeout elapsed */
296 timeout = jiffies + timeout_hz;
298 /* read the status and check for error */
299 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
303 /* if we got the flags, it was successful! */
305 dprintk("%s succeeded timeout:%lu\n",
306 __func__, jiffies - start);
310 /* check for timeout */
311 if (time_after(jiffies, timeout)) {
319 dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
321 /* if we get here, we've timed out */
327 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
332 * @return 0 on success, nonzero on failure.
334 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
340 dprintk("%s\n", __func__);
342 /* we'll be determining these during this function */
343 ca->slot_info[slot].da_irq_supported = 0;
345 /* set the host link buffer size temporarily. it will be overwritten with the
346 * real negotiated size later. */
347 ca->slot_info[slot].link_buf_size = 2;
349 /* read the buffer size from the CAM */
350 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
352 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
354 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
356 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
359 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
360 buf_size = (buf[0] << 8) | buf[1];
361 if (buf_size > HOST_LINK_BUF_SIZE)
362 buf_size = HOST_LINK_BUF_SIZE;
363 ca->slot_info[slot].link_buf_size = buf_size;
364 buf[0] = buf_size >> 8;
365 buf[1] = buf_size & 0xff;
366 dprintk("Chosen link buffer size of %i\n", buf_size);
368 /* write the buffer size to the CAM */
369 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
371 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
373 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
375 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
383 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
387 * @address: Address to read from. Updated.
388 * @tupleType: Tuple id byte. Updated.
389 * @tupleLength: Tuple length. Updated.
390 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
392 * @return 0 on success, nonzero on error.
394 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
395 int *address, int *tupleType,
396 int *tupleLength, u8 *tuple)
401 int _address = *address;
403 /* grab the next tuple length and type */
404 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
406 if (_tupleType == 0xff) {
407 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
409 *tupleType = _tupleType;
413 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
417 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
419 /* read in the whole tuple */
420 for (i = 0; i < _tupleLength; i++) {
421 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2));
422 dprintk(" 0x%02x: 0x%02x %c\n",
424 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
426 _address += (_tupleLength * 2);
429 *tupleType = _tupleType;
430 *tupleLength = _tupleLength;
437 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
438 * extracting Config register, and checking it is a DVB CAM module.
443 * @return 0 on success, <0 on failure.
445 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
454 int got_cftableentry = 0;
463 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
465 if (tupleType != 0x1D)
472 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
474 if (tupleType != 0x1C)
481 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
483 if (tupleType != 0x15)
489 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
490 &tupleLength, tuple)) < 0)
492 if (tupleType != 0x20)
494 if (tupleLength != 4)
496 manfid = (tuple[1] << 8) | tuple[0];
497 devid = (tuple[3] << 8) | tuple[2];
502 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
503 &tupleLength, tuple)) < 0)
505 if (tupleType != 0x1A)
510 /* extract the configbase */
512 if (tupleLength < (3 + rasz + 14))
514 ca->slot_info[slot].config_base = 0;
515 for (i = 0; i < rasz + 1; i++) {
516 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i));
519 /* check it contains the correct DVB string */
520 dvb_str = findstr((char *)tuple, tupleLength, "DVB_CI_V", 8);
523 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
526 /* is it a version we support? */
527 if (strncmp(dvb_str + 8, "1.00", 4)) {
528 pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
529 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9],
530 dvb_str[10], dvb_str[11]);
534 /* process the CFTABLE_ENTRY tuples, and any after those */
535 while ((!end_chain) && (address < 0x1000)) {
536 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
537 &tupleLength, tuple)) < 0)
540 case 0x1B: // CISTPL_CFTABLE_ENTRY
541 if (tupleLength < (2 + 11 + 17))
544 /* if we've already parsed one, just use it */
545 if (got_cftableentry)
548 /* get the config option */
549 ca->slot_info[slot].config_option = tuple[0] & 0x3f;
551 /* OK, check it contains the correct strings */
552 if ((findstr((char *)tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
553 (findstr((char *)tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
556 got_cftableentry = 1;
559 case 0x14: // CISTPL_NO_LINK
562 case 0xFF: // CISTPL_END
566 default: /* Unknown tuple type - just skip this tuple and move to the next one */
567 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
568 tupleType, tupleLength);
573 if ((address > 0x1000) || (!got_cftableentry))
576 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
577 manfid, devid, ca->slot_info[slot].config_base,
578 ca->slot_info[slot].config_option);
586 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
589 * @slot: Slot containing the CAM.
591 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
595 dprintk("%s\n", __func__);
597 /* set the config option */
598 ca->pub->write_attribute_mem(ca->pub, slot,
599 ca->slot_info[slot].config_base,
600 ca->slot_info[slot].config_option);
603 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
604 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
605 ca->slot_info[slot].config_option, configoption & 0x3f);
614 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
615 * interface. It reads a buffer of data from the CAM. The data can either
616 * be stored in a supplied buffer, or automatically be added to the slot's
620 * @slot: Slot to read from.
621 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
622 * the data will be added into the buffering system as a normal fragment.
623 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
625 * @return Number of bytes read, or < 0 on error
627 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot,
628 u8 *ebuf, int ecount)
632 u8 buf[HOST_LINK_BUF_SIZE];
635 dprintk("%s\n", __func__);
637 /* check if we have space for a link buf in the rx_buffer */
641 if (ca->slot_info[slot].rx_buffer.data == NULL) {
645 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
647 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
653 /* check if there is data available */
654 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
656 if (!(status & STATUSREG_DA)) {
662 /* read the amount of data */
663 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
665 bytes_read = status << 8;
666 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
668 bytes_read |= status;
670 /* check it will fit */
672 if (bytes_read > ca->slot_info[slot].link_buf_size) {
673 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
674 ca->dvbdev->adapter->num, bytes_read,
675 ca->slot_info[slot].link_buf_size);
676 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
680 if (bytes_read < 2) {
681 pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
682 ca->dvbdev->adapter->num);
683 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
688 if (bytes_read > ecount) {
689 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
690 ca->dvbdev->adapter->num);
696 /* fill the buffer */
697 for (i = 0; i < bytes_read; i++) {
698 /* read byte and check */
699 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
702 /* OK, store it in the buffer */
706 /* check for read error (RE should now be 0) */
707 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
709 if (status & STATUSREG_RE) {
710 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
715 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
717 if (ca->slot_info[slot].rx_buffer.data == NULL) {
721 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
723 memcpy(ebuf, buf, bytes_read);
726 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
727 buf[0], (buf[1] & 0x80) == 0, bytes_read);
729 /* wake up readers when a last_fragment is received */
730 if ((buf[1] & 0x80) == 0x00) {
731 wake_up_interruptible(&ca->wait_queue);
741 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
742 * interface. It writes a buffer of data to a CAM.
745 * @slot: Slot to write to.
746 * @ebuf: The data in this buffer is treated as a complete link-level packet to
748 * @count: Size of ebuf.
750 * @return Number of bytes written, or < 0 on error.
752 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot,
753 u8 *buf, int bytes_write)
758 dprintk("%s\n", __func__);
762 if (bytes_write > ca->slot_info[slot].link_buf_size)
765 /* it is possible we are dealing with a single buffer implementation,
766 thus if there is data available for read or if there is even a read
767 already in progress, we do nothing but awake the kernel thread to
768 process the data if necessary. */
769 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
771 if (status & (STATUSREG_DA | STATUSREG_RE)) {
772 if (status & STATUSREG_DA)
773 dvb_ca_en50221_thread_wakeup(ca);
780 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
781 IRQEN | CMDREG_HC)) != 0)
784 /* check if interface is still free */
785 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
787 if (!(status & STATUSREG_FR)) {
788 /* it wasn't free => try again later */
794 * It may need some time for the CAM to settle down, or there might
795 * be a race condition between the CAM, writing HC and our last
796 * check for DA. This happens, if the CAM asserts DA, just after
797 * checking DA before we are setting HC. In this case it might be
798 * a bug in the CAM to keep the FR bit, the lower layer/HW
799 * communication requires a longer timeout or the CAM needs more
800 * time internally. But this happens in reality!
801 * We need to read the status from the HW again and do the same
802 * we did for the previous check for DA
804 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
808 if (status & (STATUSREG_DA | STATUSREG_RE)) {
809 if (status & STATUSREG_DA)
810 dvb_ca_en50221_thread_wakeup(ca);
816 /* send the amount of data */
817 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
819 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
820 bytes_write & 0xff)) != 0)
823 /* send the buffer */
824 for (i = 0; i < bytes_write; i++) {
825 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
829 /* check for write error (WE should now be 0) */
830 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
832 if (status & STATUSREG_WE) {
833 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
837 status = bytes_write;
839 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
840 buf[0], (buf[1] & 0x80) == 0, bytes_write);
843 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
851 /* ******************************************************************************** */
852 /* EN50221 higher level functions */
856 * dvb_ca_en50221_slot_shutdown - A CAM has been removed => shut it down.
859 * @slot: Slot to shut down.
861 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
863 dprintk("%s\n", __func__);
865 ca->pub->slot_shutdown(ca->pub, slot);
866 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
868 /* need to wake up all processes to check if they're now
869 trying to write to a defunct CAM */
870 wake_up_interruptible(&ca->wait_queue);
872 dprintk("Slot %i shutdown\n", slot);
880 * dvb_ca_en50221_camchange_irq - A CAMCHANGE IRQ has occurred.
883 * @slot: Slot concerned.
884 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
886 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
888 struct dvb_ca_private *ca = pubca->private;
890 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
892 switch (change_type) {
893 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
894 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
901 ca->slot_info[slot].camchange_type = change_type;
902 atomic_inc(&ca->slot_info[slot].camchange_count);
903 dvb_ca_en50221_thread_wakeup(ca);
905 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
909 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
912 * @slot: Slot concerned.
914 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
916 struct dvb_ca_private *ca = pubca->private;
918 dprintk("CAMREADY IRQ slot:%i\n", slot);
920 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
921 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
922 dvb_ca_en50221_thread_wakeup(ca);
925 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
929 * dvb_ca_en50221_frda_irq - An FR or DA IRQ has occurred.
932 * @slot: Slot concerned.
934 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
936 struct dvb_ca_private *ca = pubca->private;
939 dprintk("FR/DA IRQ slot:%i\n", slot);
941 switch (ca->slot_info[slot].slot_state) {
942 case DVB_CA_SLOTSTATE_LINKINIT:
943 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
944 if (flags & STATUSREG_DA) {
945 dprintk("CAM supports DA IRQ\n");
946 ca->slot_info[slot].da_irq_supported = 1;
950 case DVB_CA_SLOTSTATE_RUNNING:
952 dvb_ca_en50221_thread_wakeup(ca);
956 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
959 /* ******************************************************************************** */
960 /* EN50221 thread functions */
963 * Wake up the DVB CA thread
967 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
970 dprintk("%s\n", __func__);
974 wake_up_process(ca->thread);
978 * Update the delay used by the thread.
982 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
985 int curdelay = 100000000;
988 /* Beware of too high polling frequency, because one polling
989 * call might take several hundred milliseconds until timeout!
991 for (slot = 0; slot < ca->slot_count; slot++) {
992 switch (ca->slot_info[slot].slot_state) {
994 case DVB_CA_SLOTSTATE_NONE:
995 delay = HZ * 60; /* 60s */
996 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
997 delay = HZ * 5; /* 5s */
999 case DVB_CA_SLOTSTATE_INVALID:
1000 delay = HZ * 60; /* 60s */
1001 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1002 delay = HZ / 10; /* 100ms */
1005 case DVB_CA_SLOTSTATE_UNINITIALISED:
1006 case DVB_CA_SLOTSTATE_WAITREADY:
1007 case DVB_CA_SLOTSTATE_VALIDATE:
1008 case DVB_CA_SLOTSTATE_WAITFR:
1009 case DVB_CA_SLOTSTATE_LINKINIT:
1010 delay = HZ / 10; /* 100ms */
1013 case DVB_CA_SLOTSTATE_RUNNING:
1014 delay = HZ * 60; /* 60s */
1015 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1016 delay = HZ / 10; /* 100ms */
1018 if ((!ca->slot_info[slot].da_irq_supported) ||
1019 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
1020 delay = HZ / 10; /* 100ms */
1025 if (delay < curdelay)
1029 ca->delay = curdelay;
1035 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
1037 static int dvb_ca_en50221_thread(void *data)
1039 struct dvb_ca_private *ca = data;
1046 dprintk("%s\n", __func__);
1048 /* choose the correct initial delay */
1049 dvb_ca_en50221_thread_update_delay(ca);
1052 while (!kthread_should_stop()) {
1053 /* sleep for a bit */
1055 set_current_state(TASK_INTERRUPTIBLE);
1056 schedule_timeout(ca->delay);
1057 if (kthread_should_stop())
1062 /* go through all the slots processing them */
1063 for (slot = 0; slot < ca->slot_count; slot++) {
1065 mutex_lock(&ca->slot_info[slot].slot_lock);
1067 // check the cam status + deal with CAMCHANGEs
1068 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1069 /* clear down an old CI slot if necessary */
1070 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
1071 dvb_ca_en50221_slot_shutdown(ca, slot);
1073 /* if a CAM is NOW present, initialise it */
1074 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
1075 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1078 /* we've handled one CAMCHANGE */
1079 dvb_ca_en50221_thread_update_delay(ca);
1080 atomic_dec(&ca->slot_info[slot].camchange_count);
1083 // CAM state machine
1084 switch (ca->slot_info[slot].slot_state) {
1085 case DVB_CA_SLOTSTATE_NONE:
1086 case DVB_CA_SLOTSTATE_INVALID:
1090 case DVB_CA_SLOTSTATE_UNINITIALISED:
1091 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1092 ca->pub->slot_reset(ca->pub, slot);
1093 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1096 case DVB_CA_SLOTSTATE_WAITREADY:
1097 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1098 pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
1099 ca->dvbdev->adapter->num);
1100 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1101 dvb_ca_en50221_thread_update_delay(ca);
1104 // no other action needed; will automatically change state when ready
1107 case DVB_CA_SLOTSTATE_VALIDATE:
1108 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1109 /* we need this extra check for annoying interfaces like the budget-av */
1110 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1111 (ca->pub->poll_slot_status)) {
1112 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1113 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1114 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1115 dvb_ca_en50221_thread_update_delay(ca);
1120 pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1121 ca->dvbdev->adapter->num);
1122 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1123 dvb_ca_en50221_thread_update_delay(ca);
1126 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1127 pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1128 ca->dvbdev->adapter->num);
1129 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1130 dvb_ca_en50221_thread_update_delay(ca);
1133 if (ca->pub->write_cam_control(ca->pub, slot,
1134 CTRLIF_COMMAND, CMDREG_RS) != 0) {
1135 pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
1136 ca->dvbdev->adapter->num);
1137 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1138 dvb_ca_en50221_thread_update_delay(ca);
1141 dprintk("DVB CAM validated successfully\n");
1143 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1144 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
1148 case DVB_CA_SLOTSTATE_WAITFR:
1149 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1150 pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1151 ca->dvbdev->adapter->num);
1152 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1153 dvb_ca_en50221_thread_update_delay(ca);
1157 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1158 if (flags & STATUSREG_FR) {
1159 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1164 case DVB_CA_SLOTSTATE_LINKINIT:
1165 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1166 /* we need this extra check for annoying interfaces like the budget-av */
1167 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1168 (ca->pub->poll_slot_status)) {
1169 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1170 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1171 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1172 dvb_ca_en50221_thread_update_delay(ca);
1177 pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
1178 ca->dvbdev->adapter->num);
1179 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1180 dvb_ca_en50221_thread_update_delay(ca);
1184 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1185 rxbuf = vmalloc(RX_BUFFER_SIZE);
1186 if (rxbuf == NULL) {
1187 pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1188 ca->dvbdev->adapter->num);
1189 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1190 dvb_ca_en50221_thread_update_delay(ca);
1193 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
1196 ca->pub->slot_ts_enable(ca->pub, slot);
1197 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
1198 dvb_ca_en50221_thread_update_delay(ca);
1199 pr_err("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
1200 ca->dvbdev->adapter->num);
1203 case DVB_CA_SLOTSTATE_RUNNING:
1207 // poll slots for data
1209 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1213 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1214 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1215 // we dont want to sleep on the next iteration so we can handle the cam change
1220 /* check if we've hit our limit this time */
1221 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1222 // dont sleep; there is likely to be more data to read
1230 mutex_unlock(&ca->slot_info[slot].slot_lock);
1239 /* ******************************************************************************** */
1240 /* EN50221 IO interface functions */
1243 * Real ioctl implementation.
1244 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1246 * @inode: Inode concerned.
1247 * @file: File concerned.
1248 * @cmd: IOCTL command.
1249 * @arg: Associated argument.
1251 * @return 0 on success, <0 on error.
1253 static int dvb_ca_en50221_io_do_ioctl(struct file *file,
1254 unsigned int cmd, void *parg)
1256 struct dvb_device *dvbdev = file->private_data;
1257 struct dvb_ca_private *ca = dvbdev->priv;
1261 dprintk("%s\n", __func__);
1263 if (mutex_lock_interruptible(&ca->ioctl_mutex))
1264 return -ERESTARTSYS;
1268 for (slot = 0; slot < ca->slot_count; slot++) {
1269 mutex_lock(&ca->slot_info[slot].slot_lock);
1270 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
1271 dvb_ca_en50221_slot_shutdown(ca, slot);
1272 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1273 dvb_ca_en50221_camchange_irq(ca->pub,
1275 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1277 mutex_unlock(&ca->slot_info[slot].slot_lock);
1279 ca->next_read_slot = 0;
1280 dvb_ca_en50221_thread_wakeup(ca);
1284 struct ca_caps *caps = parg;
1286 caps->slot_num = ca->slot_count;
1287 caps->slot_type = CA_CI_LINK;
1288 caps->descr_num = 0;
1289 caps->descr_type = 0;
1293 case CA_GET_SLOT_INFO: {
1294 struct ca_slot_info *info = parg;
1296 if ((info->num > ca->slot_count) || (info->num < 0)) {
1301 info->type = CA_CI_LINK;
1303 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
1304 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1305 info->flags = CA_CI_MODULE_PRESENT;
1307 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1308 info->flags |= CA_CI_MODULE_READY;
1319 mutex_unlock(&ca->ioctl_mutex);
1325 * Wrapper for ioctl implementation.
1327 * @inode: Inode concerned.
1328 * @file: File concerned.
1329 * @cmd: IOCTL command.
1330 * @arg: Associated argument.
1332 * @return 0 on success, <0 on error.
1334 static long dvb_ca_en50221_io_ioctl(struct file *file,
1335 unsigned int cmd, unsigned long arg)
1337 return dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1342 * Implementation of write() syscall.
1344 * @file: File structure.
1345 * @buf: Source buffer.
1346 * @count: Size of source buffer.
1347 * @ppos: Position in file (ignored).
1349 * @return Number of bytes read, or <0 on error.
1351 static ssize_t dvb_ca_en50221_io_write(struct file *file,
1352 const char __user *buf, size_t count,
1355 struct dvb_device *dvbdev = file->private_data;
1356 struct dvb_ca_private *ca = dvbdev->priv;
1357 u8 slot, connection_id;
1359 u8 fragbuf[HOST_LINK_BUF_SIZE];
1362 unsigned long timeout;
1365 dprintk("%s\n", __func__);
1367 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1371 /* extract slot & connection id */
1372 if (copy_from_user(&slot, buf, 1))
1374 if (copy_from_user(&connection_id, buf + 1, 1))
1379 /* check if the slot is actually running */
1380 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1383 /* fragment the packets & store in the buffer */
1384 while (fragpos < count) {
1385 fraglen = ca->slot_info[slot].link_buf_size - 2;
1388 if (fraglen > HOST_LINK_BUF_SIZE - 2)
1389 fraglen = HOST_LINK_BUF_SIZE - 2;
1390 if ((count - fragpos) < fraglen)
1391 fraglen = count - fragpos;
1393 fragbuf[0] = connection_id;
1394 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1395 status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
1401 timeout = jiffies + HZ / 2;
1403 while (!time_after(jiffies, timeout)) {
1404 /* check the CAM hasn't been removed/reset in the meantime */
1405 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1410 mutex_lock(&ca->slot_info[slot].slot_lock);
1411 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
1412 mutex_unlock(&ca->slot_info[slot].slot_lock);
1413 if (status == (fraglen + 2)) {
1417 if (status != -EAGAIN)
1437 * Condition for waking up in dvb_ca_en50221_io_read_condition
1439 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
1440 int *result, int *_slot)
1446 int connection_id = -1;
1450 slot = ca->next_read_slot;
1451 while ((slot_count < ca->slot_count) && (!found)) {
1452 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1455 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1459 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1461 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1462 if (connection_id == -1)
1463 connection_id = hdr[0];
1464 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
1470 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1474 slot = (slot + 1) % ca->slot_count;
1478 ca->next_read_slot = slot;
1484 * Implementation of read() syscall.
1486 * @file: File structure.
1487 * @buf: Destination buffer.
1488 * @count: Size of destination buffer.
1489 * @ppos: Position in file (ignored).
1491 * @return Number of bytes read, or <0 on error.
1493 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user *buf,
1494 size_t count, loff_t *ppos)
1496 struct dvb_device *dvbdev = file->private_data;
1497 struct dvb_ca_private *ca = dvbdev->priv;
1502 int connection_id = -1;
1504 int last_fragment = 0;
1509 dprintk("%s\n", __func__);
1511 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1515 /* wait for some data */
1516 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
1518 /* if we're in nonblocking mode, exit immediately */
1519 if (file->f_flags & O_NONBLOCK)
1520 return -EWOULDBLOCK;
1522 /* wait for some data */
1523 status = wait_event_interruptible(ca->wait_queue,
1524 dvb_ca_en50221_io_read_condition
1525 (ca, &result, &slot));
1527 if ((status < 0) || (result < 0)) {
1533 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1537 pr_err("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n",
1538 ca->dvbdev->adapter->num);
1543 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1544 if (connection_id == -1)
1545 connection_id = hdr[0];
1546 if (hdr[0] == connection_id) {
1547 if (pktlen < count) {
1548 if ((pktlen + fraglen - 2) > count) {
1549 fraglen = count - pktlen;
1554 if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
1555 buf + pktlen, fraglen)) < 0) {
1561 if ((hdr[1] & 0x80) == 0)
1566 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1568 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
1571 } while (!last_fragment);
1574 hdr[1] = connection_id;
1575 status = copy_to_user(buf, hdr, 2);
1588 * Implementation of file open syscall.
1590 * @inode: Inode concerned.
1591 * @file: File concerned.
1593 * @return 0 on success, <0 on failure.
1595 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1597 struct dvb_device *dvbdev = file->private_data;
1598 struct dvb_ca_private *ca = dvbdev->priv;
1602 dprintk("%s\n", __func__);
1604 if (!try_module_get(ca->pub->owner))
1607 err = dvb_generic_open(inode, file);
1609 module_put(ca->pub->owner);
1613 for (i = 0; i < ca->slot_count; i++) {
1615 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1616 if (ca->slot_info[i].rx_buffer.data != NULL) {
1617 /* it is safe to call this here without locks because
1618 * ca->open == 0. Data is not read in this case */
1619 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
1625 dvb_ca_en50221_thread_update_delay(ca);
1626 dvb_ca_en50221_thread_wakeup(ca);
1628 dvb_ca_private_get(ca);
1635 * Implementation of file close syscall.
1637 * @inode: Inode concerned.
1638 * @file: File concerned.
1640 * @return 0 on success, <0 on failure.
1642 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1644 struct dvb_device *dvbdev = file->private_data;
1645 struct dvb_ca_private *ca = dvbdev->priv;
1648 dprintk("%s\n", __func__);
1650 /* mark the CA device as closed */
1652 dvb_ca_en50221_thread_update_delay(ca);
1654 err = dvb_generic_release(inode, file);
1656 module_put(ca->pub->owner);
1658 dvb_ca_private_put(ca);
1665 * Implementation of poll() syscall.
1667 * @file: File concerned.
1668 * @wait: poll wait table.
1670 * @return Standard poll mask.
1672 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table *wait)
1674 struct dvb_device *dvbdev = file->private_data;
1675 struct dvb_ca_private *ca = dvbdev->priv;
1676 unsigned int mask = 0;
1680 dprintk("%s\n", __func__);
1682 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1686 /* if there is something, return now */
1690 /* wait for something to happen */
1691 poll_wait(file, &ca->wait_queue, wait);
1693 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1699 EXPORT_SYMBOL(dvb_ca_en50221_init);
1702 static const struct file_operations dvb_ca_fops = {
1703 .owner = THIS_MODULE,
1704 .read = dvb_ca_en50221_io_read,
1705 .write = dvb_ca_en50221_io_write,
1706 .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
1707 .open = dvb_ca_en50221_io_open,
1708 .release = dvb_ca_en50221_io_release,
1709 .poll = dvb_ca_en50221_io_poll,
1710 .llseek = noop_llseek,
1713 static const struct dvb_device dvbdev_ca = {
1718 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1719 .name = "dvb-ca-en50221",
1721 .fops = &dvb_ca_fops,
1724 /* ******************************************************************************** */
1725 /* Initialisation/shutdown functions */
1729 * Initialise a new DVB CA EN50221 interface device.
1731 * @dvb_adapter: DVB adapter to attach the new CA device to.
1732 * @ca: The dvb_ca instance.
1733 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1734 * @slot_count: Number of slots supported.
1736 * @return 0 on success, nonzero on failure
1738 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1739 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1742 struct dvb_ca_private *ca = NULL;
1745 dprintk("%s\n", __func__);
1750 /* initialise the system data */
1751 if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
1755 kref_init(&ca->refcount);
1758 ca->slot_count = slot_count;
1759 if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
1763 init_waitqueue_head(&ca->wait_queue);
1766 ca->next_read_slot = 0;
1767 pubca->private = ca;
1769 /* register the DVB device */
1770 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA, 0);
1772 goto free_slot_info;
1774 /* now initialise each slot */
1775 for (i = 0; i < slot_count; i++) {
1776 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
1777 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
1778 atomic_set(&ca->slot_info[i].camchange_count, 0);
1779 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1780 mutex_init(&ca->slot_info[i].slot_lock);
1783 mutex_init(&ca->ioctl_mutex);
1785 if (signal_pending(current)) {
1787 goto unregister_device;
1791 /* create a kthread for monitoring this CA device */
1792 ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
1793 ca->dvbdev->adapter->num, ca->dvbdev->id);
1794 if (IS_ERR(ca->thread)) {
1795 ret = PTR_ERR(ca->thread);
1796 pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
1798 goto unregister_device;
1803 dvb_unregister_device(ca->dvbdev);
1805 kfree(ca->slot_info);
1809 pubca->private = NULL;
1812 EXPORT_SYMBOL(dvb_ca_en50221_release);
1817 * Release a DVB CA EN50221 interface device.
1819 * @ca_dev: The dvb_device_t instance for the CA device.
1820 * @ca: The associated dvb_ca instance.
1822 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1824 struct dvb_ca_private *ca = pubca->private;
1827 dprintk("%s\n", __func__);
1829 /* shutdown the thread if there was one */
1830 kthread_stop(ca->thread);
1832 for (i = 0; i < ca->slot_count; i++) {
1833 dvb_ca_en50221_slot_shutdown(ca, i);
1835 dvb_remove_device(ca->dvbdev);
1836 dvb_ca_private_put(ca);
1837 pubca->private = NULL;