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, u8 * ebuf, int ecount);
197 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
201 * Safely find needle in haystack.
203 * @haystack: Buffer to look in.
204 * @hlen: Number of bytes in haystack.
205 * @needle: Buffer to find.
206 * @nlen: Number of bytes in needle.
207 * @return Pointer into haystack needle was found at, or NULL if not found.
209 static char *findstr(char * haystack, int hlen, char * needle, int nlen)
216 for (i = 0; i <= hlen - nlen; i++) {
217 if (!strncmp(haystack + i, needle, nlen))
226 /* ******************************************************************************** */
227 /* EN50221 physical interface functions */
231 * dvb_ca_en50221_check_camstatus - Check CAM status.
233 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
240 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
241 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
245 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
247 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
248 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
250 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
251 cam_changed = (cam_present_now != cam_present_old);
255 if (!cam_present_now) {
256 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
258 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
260 atomic_set(&ca->slot_info[slot].camchange_count, 1);
262 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
263 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
264 // move to validate state if reset is completed
265 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
274 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
275 * register on a CAM interface, checking for errors and timeout.
278 * @slot: Slot on interface.
279 * @waitfor: Flags to wait for.
280 * @timeout_ms: Timeout in milliseconds.
282 * @return 0 on success, nonzero on error.
284 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
285 u8 waitfor, int timeout_hz)
287 unsigned long timeout;
290 dprintk("%s\n", __func__);
292 /* loop until timeout elapsed */
294 timeout = jiffies + timeout_hz;
296 /* read the status and check for error */
297 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
301 /* if we got the flags, it was successful! */
303 dprintk("%s succeeded timeout:%lu\n",
304 __func__, jiffies - start);
308 /* check for timeout */
309 if (time_after(jiffies, timeout)) {
317 dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
319 /* if we get here, we've timed out */
325 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
330 * @return 0 on success, nonzero on failure.
332 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
338 dprintk("%s\n", __func__);
340 /* we'll be determining these during this function */
341 ca->slot_info[slot].da_irq_supported = 0;
343 /* set the host link buffer size temporarily. it will be overwritten with the
344 * real negotiated size later. */
345 ca->slot_info[slot].link_buf_size = 2;
347 /* read the buffer size from the CAM */
348 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
350 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
352 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
354 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
357 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
358 buf_size = (buf[0] << 8) | buf[1];
359 if (buf_size > HOST_LINK_BUF_SIZE)
360 buf_size = HOST_LINK_BUF_SIZE;
361 ca->slot_info[slot].link_buf_size = buf_size;
362 buf[0] = buf_size >> 8;
363 buf[1] = buf_size & 0xff;
364 dprintk("Chosen link buffer size of %i\n", buf_size);
366 /* write the buffer size to the CAM */
367 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
369 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
371 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
373 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
381 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
385 * @address: Address to read from. Updated.
386 * @tupleType: Tuple id byte. Updated.
387 * @tupleLength: Tuple length. Updated.
388 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
390 * @return 0 on success, nonzero on error.
392 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
393 int *address, int *tupleType, int *tupleLength, u8 * tuple)
398 int _address = *address;
400 /* grab the next tuple length and type */
401 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
403 if (_tupleType == 0xff) {
404 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
406 *tupleType = _tupleType;
410 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
414 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
416 /* read in the whole tuple */
417 for (i = 0; i < _tupleLength; i++) {
418 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2));
419 dprintk(" 0x%02x: 0x%02x %c\n",
421 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
423 _address += (_tupleLength * 2);
426 *tupleType = _tupleType;
427 *tupleLength = _tupleLength;
434 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
435 * extracting Config register, and checking it is a DVB CAM module.
440 * @return 0 on success, <0 on failure.
442 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
451 int got_cftableentry = 0;
460 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
462 if (tupleType != 0x1D)
469 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
471 if (tupleType != 0x1C)
478 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
480 if (tupleType != 0x15)
486 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
487 &tupleLength, tuple)) < 0)
489 if (tupleType != 0x20)
491 if (tupleLength != 4)
493 manfid = (tuple[1] << 8) | tuple[0];
494 devid = (tuple[3] << 8) | tuple[2];
499 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
500 &tupleLength, tuple)) < 0)
502 if (tupleType != 0x1A)
507 /* extract the configbase */
509 if (tupleLength < (3 + rasz + 14))
511 ca->slot_info[slot].config_base = 0;
512 for (i = 0; i < rasz + 1; i++) {
513 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i));
516 /* check it contains the correct DVB string */
517 dvb_str = findstr((char *)tuple, tupleLength, "DVB_CI_V", 8);
520 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
523 /* is it a version we support? */
524 if (strncmp(dvb_str + 8, "1.00", 4)) {
525 pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
526 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9],
527 dvb_str[10], dvb_str[11]);
531 /* process the CFTABLE_ENTRY tuples, and any after those */
532 while ((!end_chain) && (address < 0x1000)) {
533 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
534 &tupleLength, tuple)) < 0)
537 case 0x1B: // CISTPL_CFTABLE_ENTRY
538 if (tupleLength < (2 + 11 + 17))
541 /* if we've already parsed one, just use it */
542 if (got_cftableentry)
545 /* get the config option */
546 ca->slot_info[slot].config_option = tuple[0] & 0x3f;
548 /* OK, check it contains the correct strings */
549 if ((findstr((char *)tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
550 (findstr((char *)tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
553 got_cftableentry = 1;
556 case 0x14: // CISTPL_NO_LINK
559 case 0xFF: // CISTPL_END
563 default: /* Unknown tuple type - just skip this tuple and move to the next one */
564 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
565 tupleType, tupleLength);
570 if ((address > 0x1000) || (!got_cftableentry))
573 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
574 manfid, devid, ca->slot_info[slot].config_base,
575 ca->slot_info[slot].config_option);
583 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
586 * @slot: Slot containing the CAM.
588 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
592 dprintk("%s\n", __func__);
594 /* set the config option */
595 ca->pub->write_attribute_mem(ca->pub, slot,
596 ca->slot_info[slot].config_base,
597 ca->slot_info[slot].config_option);
600 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
601 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
602 ca->slot_info[slot].config_option, configoption & 0x3f);
611 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
612 * interface. It reads a buffer of data from the CAM. The data can either
613 * be stored in a supplied buffer, or automatically be added to the slot's
617 * @slot: Slot to read from.
618 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
619 * the data will be added into the buffering system as a normal fragment.
620 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
622 * @return Number of bytes read, or < 0 on error
624 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount)
628 u8 buf[HOST_LINK_BUF_SIZE];
631 dprintk("%s\n", __func__);
633 /* check if we have space for a link buf in the rx_buffer */
637 if (ca->slot_info[slot].rx_buffer.data == NULL) {
641 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
643 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
649 /* check if there is data available */
650 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
652 if (!(status & STATUSREG_DA)) {
658 /* read the amount of data */
659 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
661 bytes_read = status << 8;
662 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
664 bytes_read |= status;
666 /* check it will fit */
668 if (bytes_read > ca->slot_info[slot].link_buf_size) {
669 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
670 ca->dvbdev->adapter->num, bytes_read,
671 ca->slot_info[slot].link_buf_size);
672 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
676 if (bytes_read < 2) {
677 pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
678 ca->dvbdev->adapter->num);
679 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
684 if (bytes_read > ecount) {
685 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
686 ca->dvbdev->adapter->num);
692 /* fill the buffer */
693 for (i = 0; i < bytes_read; i++) {
694 /* read byte and check */
695 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
698 /* OK, store it in the buffer */
702 /* check for read error (RE should now be 0) */
703 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
705 if (status & STATUSREG_RE) {
706 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
711 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
713 if (ca->slot_info[slot].rx_buffer.data == NULL) {
717 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
719 memcpy(ebuf, buf, bytes_read);
722 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
723 buf[0], (buf[1] & 0x80) == 0, bytes_read);
725 /* wake up readers when a last_fragment is received */
726 if ((buf[1] & 0x80) == 0x00) {
727 wake_up_interruptible(&ca->wait_queue);
737 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
738 * interface. It writes a buffer of data to a CAM.
741 * @slot: Slot to write to.
742 * @ebuf: The data in this buffer is treated as a complete link-level packet to
744 * @count: Size of ebuf.
746 * @return Number of bytes written, or < 0 on error.
748 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write)
753 dprintk("%s\n", __func__);
757 if (bytes_write > ca->slot_info[slot].link_buf_size)
760 /* it is possible we are dealing with a single buffer implementation,
761 thus if there is data available for read or if there is even a read
762 already in progress, we do nothing but awake the kernel thread to
763 process the data if necessary. */
764 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
766 if (status & (STATUSREG_DA | STATUSREG_RE)) {
767 if (status & STATUSREG_DA)
768 dvb_ca_en50221_thread_wakeup(ca);
775 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
776 IRQEN | CMDREG_HC)) != 0)
779 /* check if interface is still free */
780 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
782 if (!(status & STATUSREG_FR)) {
783 /* it wasn't free => try again later */
789 * It may need some time for the CAM to settle down, or there might
790 * be a race condition between the CAM, writing HC and our last
791 * check for DA. This happens, if the CAM asserts DA, just after
792 * checking DA before we are setting HC. In this case it might be
793 * a bug in the CAM to keep the FR bit, the lower layer/HW
794 * communication requires a longer timeout or the CAM needs more
795 * time internally. But this happens in reality!
796 * We need to read the status from the HW again and do the same
797 * we did for the previous check for DA
799 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
803 if (status & (STATUSREG_DA | STATUSREG_RE)) {
804 if (status & STATUSREG_DA)
805 dvb_ca_en50221_thread_wakeup(ca);
811 /* send the amount of data */
812 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
814 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
815 bytes_write & 0xff)) != 0)
818 /* send the buffer */
819 for (i = 0; i < bytes_write; i++) {
820 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
824 /* check for write error (WE should now be 0) */
825 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
827 if (status & STATUSREG_WE) {
828 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
832 status = bytes_write;
834 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
835 buf[0], (buf[1] & 0x80) == 0, bytes_write);
838 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
843 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
847 /* ******************************************************************************** */
848 /* EN50221 higher level functions */
852 * dvb_ca_en50221_camready_irq - A CAM has been removed => shut it down.
855 * @slot: Slot to shut down.
857 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
859 dprintk("%s\n", __func__);
861 ca->pub->slot_shutdown(ca->pub, slot);
862 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
864 /* need to wake up all processes to check if they're now
865 trying to write to a defunct CAM */
866 wake_up_interruptible(&ca->wait_queue);
868 dprintk("Slot %i shutdown\n", slot);
873 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
877 * dvb_ca_en50221_camready_irq - A CAMCHANGE IRQ has occurred.
880 * @slot: Slot concerned.
881 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
883 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
885 struct dvb_ca_private *ca = pubca->private;
887 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
889 switch (change_type) {
890 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
891 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
898 ca->slot_info[slot].camchange_type = change_type;
899 atomic_inc(&ca->slot_info[slot].camchange_count);
900 dvb_ca_en50221_thread_wakeup(ca);
902 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
906 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
909 * @slot: Slot concerned.
911 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
913 struct dvb_ca_private *ca = pubca->private;
915 dprintk("CAMREADY IRQ slot:%i\n", slot);
917 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
918 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
919 dvb_ca_en50221_thread_wakeup(ca);
925 * An FR or DA IRQ has occurred.
928 * @slot: Slot concerned.
930 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
932 struct dvb_ca_private *ca = pubca->private;
935 dprintk("FR/DA IRQ slot:%i\n", slot);
937 switch (ca->slot_info[slot].slot_state) {
938 case DVB_CA_SLOTSTATE_LINKINIT:
939 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
940 if (flags & STATUSREG_DA) {
941 dprintk("CAM supports DA IRQ\n");
942 ca->slot_info[slot].da_irq_supported = 1;
946 case DVB_CA_SLOTSTATE_RUNNING:
948 dvb_ca_en50221_thread_wakeup(ca);
955 /* ******************************************************************************** */
956 /* EN50221 thread functions */
959 * Wake up the DVB CA thread
963 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
966 dprintk("%s\n", __func__);
970 wake_up_process(ca->thread);
974 * Update the delay used by the thread.
978 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
981 int curdelay = 100000000;
984 /* Beware of too high polling frequency, because one polling
985 * call might take several hundred milliseconds until timeout!
987 for (slot = 0; slot < ca->slot_count; slot++) {
988 switch (ca->slot_info[slot].slot_state) {
990 case DVB_CA_SLOTSTATE_NONE:
991 delay = HZ * 60; /* 60s */
992 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
993 delay = HZ * 5; /* 5s */
995 case DVB_CA_SLOTSTATE_INVALID:
996 delay = HZ * 60; /* 60s */
997 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
998 delay = HZ / 10; /* 100ms */
1001 case DVB_CA_SLOTSTATE_UNINITIALISED:
1002 case DVB_CA_SLOTSTATE_WAITREADY:
1003 case DVB_CA_SLOTSTATE_VALIDATE:
1004 case DVB_CA_SLOTSTATE_WAITFR:
1005 case DVB_CA_SLOTSTATE_LINKINIT:
1006 delay = HZ / 10; /* 100ms */
1009 case DVB_CA_SLOTSTATE_RUNNING:
1010 delay = HZ * 60; /* 60s */
1011 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1012 delay = HZ / 10; /* 100ms */
1014 if ((!ca->slot_info[slot].da_irq_supported) ||
1015 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
1016 delay = HZ / 10; /* 100ms */
1021 if (delay < curdelay)
1025 ca->delay = curdelay;
1031 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
1033 static int dvb_ca_en50221_thread(void *data)
1035 struct dvb_ca_private *ca = data;
1042 dprintk("%s\n", __func__);
1044 /* choose the correct initial delay */
1045 dvb_ca_en50221_thread_update_delay(ca);
1048 while (!kthread_should_stop()) {
1049 /* sleep for a bit */
1051 set_current_state(TASK_INTERRUPTIBLE);
1052 schedule_timeout(ca->delay);
1053 if (kthread_should_stop())
1058 /* go through all the slots processing them */
1059 for (slot = 0; slot < ca->slot_count; slot++) {
1061 mutex_lock(&ca->slot_info[slot].slot_lock);
1063 // check the cam status + deal with CAMCHANGEs
1064 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1065 /* clear down an old CI slot if necessary */
1066 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
1067 dvb_ca_en50221_slot_shutdown(ca, slot);
1069 /* if a CAM is NOW present, initialise it */
1070 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
1071 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1074 /* we've handled one CAMCHANGE */
1075 dvb_ca_en50221_thread_update_delay(ca);
1076 atomic_dec(&ca->slot_info[slot].camchange_count);
1079 // CAM state machine
1080 switch (ca->slot_info[slot].slot_state) {
1081 case DVB_CA_SLOTSTATE_NONE:
1082 case DVB_CA_SLOTSTATE_INVALID:
1086 case DVB_CA_SLOTSTATE_UNINITIALISED:
1087 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1088 ca->pub->slot_reset(ca->pub, slot);
1089 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1092 case DVB_CA_SLOTSTATE_WAITREADY:
1093 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1094 pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
1095 ca->dvbdev->adapter->num);
1096 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1097 dvb_ca_en50221_thread_update_delay(ca);
1100 // no other action needed; will automatically change state when ready
1103 case DVB_CA_SLOTSTATE_VALIDATE:
1104 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1105 /* we need this extra check for annoying interfaces like the budget-av */
1106 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1107 (ca->pub->poll_slot_status)) {
1108 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1109 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1110 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1111 dvb_ca_en50221_thread_update_delay(ca);
1116 pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1117 ca->dvbdev->adapter->num);
1118 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1119 dvb_ca_en50221_thread_update_delay(ca);
1122 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1123 pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1124 ca->dvbdev->adapter->num);
1125 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1126 dvb_ca_en50221_thread_update_delay(ca);
1129 if (ca->pub->write_cam_control(ca->pub, slot,
1130 CTRLIF_COMMAND, CMDREG_RS) != 0) {
1131 pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
1132 ca->dvbdev->adapter->num);
1133 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1134 dvb_ca_en50221_thread_update_delay(ca);
1137 dprintk("DVB CAM validated successfully\n");
1139 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1140 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
1144 case DVB_CA_SLOTSTATE_WAITFR:
1145 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1146 pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1147 ca->dvbdev->adapter->num);
1148 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1149 dvb_ca_en50221_thread_update_delay(ca);
1153 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1154 if (flags & STATUSREG_FR) {
1155 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1160 case DVB_CA_SLOTSTATE_LINKINIT:
1161 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1162 /* we need this extra check for annoying interfaces like the budget-av */
1163 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1164 (ca->pub->poll_slot_status)) {
1165 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1166 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1167 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1168 dvb_ca_en50221_thread_update_delay(ca);
1173 pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
1174 ca->dvbdev->adapter->num);
1175 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1176 dvb_ca_en50221_thread_update_delay(ca);
1180 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1181 rxbuf = vmalloc(RX_BUFFER_SIZE);
1182 if (rxbuf == NULL) {
1183 pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1184 ca->dvbdev->adapter->num);
1185 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1186 dvb_ca_en50221_thread_update_delay(ca);
1189 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
1192 ca->pub->slot_ts_enable(ca->pub, slot);
1193 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
1194 dvb_ca_en50221_thread_update_delay(ca);
1195 pr_err("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
1196 ca->dvbdev->adapter->num);
1199 case DVB_CA_SLOTSTATE_RUNNING:
1203 // poll slots for data
1205 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1209 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1210 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1211 // we dont want to sleep on the next iteration so we can handle the cam change
1216 /* check if we've hit our limit this time */
1217 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1218 // dont sleep; there is likely to be more data to read
1226 mutex_unlock(&ca->slot_info[slot].slot_lock);
1235 /* ******************************************************************************** */
1236 /* EN50221 IO interface functions */
1239 * Real ioctl implementation.
1240 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1242 * @inode: Inode concerned.
1243 * @file: File concerned.
1244 * @cmd: IOCTL command.
1245 * @arg: Associated argument.
1247 * @return 0 on success, <0 on error.
1249 static int dvb_ca_en50221_io_do_ioctl(struct file *file,
1250 unsigned int cmd, void *parg)
1252 struct dvb_device *dvbdev = file->private_data;
1253 struct dvb_ca_private *ca = dvbdev->priv;
1257 dprintk("%s\n", __func__);
1259 if (mutex_lock_interruptible(&ca->ioctl_mutex))
1260 return -ERESTARTSYS;
1264 for (slot = 0; slot < ca->slot_count; slot++) {
1265 mutex_lock(&ca->slot_info[slot].slot_lock);
1266 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
1267 dvb_ca_en50221_slot_shutdown(ca, slot);
1268 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1269 dvb_ca_en50221_camchange_irq(ca->pub,
1271 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1273 mutex_unlock(&ca->slot_info[slot].slot_lock);
1275 ca->next_read_slot = 0;
1276 dvb_ca_en50221_thread_wakeup(ca);
1280 struct ca_caps *caps = parg;
1282 caps->slot_num = ca->slot_count;
1283 caps->slot_type = CA_CI_LINK;
1284 caps->descr_num = 0;
1285 caps->descr_type = 0;
1289 case CA_GET_SLOT_INFO: {
1290 struct ca_slot_info *info = parg;
1292 if ((info->num > ca->slot_count) || (info->num < 0)) {
1297 info->type = CA_CI_LINK;
1299 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
1300 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1301 info->flags = CA_CI_MODULE_PRESENT;
1303 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1304 info->flags |= CA_CI_MODULE_READY;
1315 mutex_unlock(&ca->ioctl_mutex);
1321 * Wrapper for ioctl implementation.
1323 * @inode: Inode concerned.
1324 * @file: File concerned.
1325 * @cmd: IOCTL command.
1326 * @arg: Associated argument.
1328 * @return 0 on success, <0 on error.
1330 static long dvb_ca_en50221_io_ioctl(struct file *file,
1331 unsigned int cmd, unsigned long arg)
1333 return dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1338 * Implementation of write() syscall.
1340 * @file: File structure.
1341 * @buf: Source buffer.
1342 * @count: Size of source buffer.
1343 * @ppos: Position in file (ignored).
1345 * @return Number of bytes read, or <0 on error.
1347 static ssize_t dvb_ca_en50221_io_write(struct file *file,
1348 const char __user * buf, size_t count, loff_t * ppos)
1350 struct dvb_device *dvbdev = file->private_data;
1351 struct dvb_ca_private *ca = dvbdev->priv;
1352 u8 slot, connection_id;
1354 u8 fragbuf[HOST_LINK_BUF_SIZE];
1357 unsigned long timeout;
1360 dprintk("%s\n", __func__);
1362 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1366 /* extract slot & connection id */
1367 if (copy_from_user(&slot, buf, 1))
1369 if (copy_from_user(&connection_id, buf + 1, 1))
1374 /* check if the slot is actually running */
1375 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1378 /* fragment the packets & store in the buffer */
1379 while (fragpos < count) {
1380 fraglen = ca->slot_info[slot].link_buf_size - 2;
1383 if (fraglen > HOST_LINK_BUF_SIZE - 2)
1384 fraglen = HOST_LINK_BUF_SIZE - 2;
1385 if ((count - fragpos) < fraglen)
1386 fraglen = count - fragpos;
1388 fragbuf[0] = connection_id;
1389 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1390 status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
1396 timeout = jiffies + HZ / 2;
1398 while (!time_after(jiffies, timeout)) {
1399 /* check the CAM hasn't been removed/reset in the meantime */
1400 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1405 mutex_lock(&ca->slot_info[slot].slot_lock);
1406 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
1407 mutex_unlock(&ca->slot_info[slot].slot_lock);
1408 if (status == (fraglen + 2)) {
1412 if (status != -EAGAIN)
1432 * Condition for waking up in dvb_ca_en50221_io_read_condition
1434 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
1435 int *result, int *_slot)
1441 int connection_id = -1;
1445 slot = ca->next_read_slot;
1446 while ((slot_count < ca->slot_count) && (!found)) {
1447 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1450 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1454 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1456 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1457 if (connection_id == -1)
1458 connection_id = hdr[0];
1459 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
1465 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1469 slot = (slot + 1) % ca->slot_count;
1473 ca->next_read_slot = slot;
1479 * Implementation of read() syscall.
1481 * @file: File structure.
1482 * @buf: Destination buffer.
1483 * @count: Size of destination buffer.
1484 * @ppos: Position in file (ignored).
1486 * @return Number of bytes read, or <0 on error.
1488 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
1489 size_t count, loff_t * ppos)
1491 struct dvb_device *dvbdev = file->private_data;
1492 struct dvb_ca_private *ca = dvbdev->priv;
1497 int connection_id = -1;
1499 int last_fragment = 0;
1504 dprintk("%s\n", __func__);
1506 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1510 /* wait for some data */
1511 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
1513 /* if we're in nonblocking mode, exit immediately */
1514 if (file->f_flags & O_NONBLOCK)
1515 return -EWOULDBLOCK;
1517 /* wait for some data */
1518 status = wait_event_interruptible(ca->wait_queue,
1519 dvb_ca_en50221_io_read_condition
1520 (ca, &result, &slot));
1522 if ((status < 0) || (result < 0)) {
1528 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1532 pr_err("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n",
1533 ca->dvbdev->adapter->num);
1538 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1539 if (connection_id == -1)
1540 connection_id = hdr[0];
1541 if (hdr[0] == connection_id) {
1542 if (pktlen < count) {
1543 if ((pktlen + fraglen - 2) > count) {
1544 fraglen = count - pktlen;
1549 if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
1550 buf + pktlen, fraglen)) < 0) {
1556 if ((hdr[1] & 0x80) == 0)
1561 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1563 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
1566 } while (!last_fragment);
1569 hdr[1] = connection_id;
1570 status = copy_to_user(buf, hdr, 2);
1583 * Implementation of file open syscall.
1585 * @inode: Inode concerned.
1586 * @file: File concerned.
1588 * @return 0 on success, <0 on failure.
1590 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1592 struct dvb_device *dvbdev = file->private_data;
1593 struct dvb_ca_private *ca = dvbdev->priv;
1597 dprintk("%s\n", __func__);
1599 if (!try_module_get(ca->pub->owner))
1602 err = dvb_generic_open(inode, file);
1604 module_put(ca->pub->owner);
1608 for (i = 0; i < ca->slot_count; i++) {
1610 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1611 if (ca->slot_info[i].rx_buffer.data != NULL) {
1612 /* it is safe to call this here without locks because
1613 * ca->open == 0. Data is not read in this case */
1614 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
1620 dvb_ca_en50221_thread_update_delay(ca);
1621 dvb_ca_en50221_thread_wakeup(ca);
1623 dvb_ca_private_get(ca);
1630 * Implementation of file close syscall.
1632 * @inode: Inode concerned.
1633 * @file: File concerned.
1635 * @return 0 on success, <0 on failure.
1637 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1639 struct dvb_device *dvbdev = file->private_data;
1640 struct dvb_ca_private *ca = dvbdev->priv;
1643 dprintk("%s\n", __func__);
1645 /* mark the CA device as closed */
1647 dvb_ca_en50221_thread_update_delay(ca);
1649 err = dvb_generic_release(inode, file);
1651 module_put(ca->pub->owner);
1653 dvb_ca_private_put(ca);
1660 * Implementation of poll() syscall.
1662 * @file: File concerned.
1663 * @wait: poll wait table.
1665 * @return Standard poll mask.
1667 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
1669 struct dvb_device *dvbdev = file->private_data;
1670 struct dvb_ca_private *ca = dvbdev->priv;
1671 unsigned int mask = 0;
1675 dprintk("%s\n", __func__);
1677 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1681 /* if there is something, return now */
1685 /* wait for something to happen */
1686 poll_wait(file, &ca->wait_queue, wait);
1688 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1694 EXPORT_SYMBOL(dvb_ca_en50221_init);
1697 static const struct file_operations dvb_ca_fops = {
1698 .owner = THIS_MODULE,
1699 .read = dvb_ca_en50221_io_read,
1700 .write = dvb_ca_en50221_io_write,
1701 .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
1702 .open = dvb_ca_en50221_io_open,
1703 .release = dvb_ca_en50221_io_release,
1704 .poll = dvb_ca_en50221_io_poll,
1705 .llseek = noop_llseek,
1708 static const struct dvb_device dvbdev_ca = {
1713 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1714 .name = "dvb-ca-en50221",
1716 .fops = &dvb_ca_fops,
1719 /* ******************************************************************************** */
1720 /* Initialisation/shutdown functions */
1724 * Initialise a new DVB CA EN50221 interface device.
1726 * @dvb_adapter: DVB adapter to attach the new CA device to.
1727 * @ca: The dvb_ca instance.
1728 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1729 * @slot_count: Number of slots supported.
1731 * @return 0 on success, nonzero on failure
1733 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1734 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1737 struct dvb_ca_private *ca = NULL;
1740 dprintk("%s\n", __func__);
1745 /* initialise the system data */
1746 if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
1750 kref_init(&ca->refcount);
1753 ca->slot_count = slot_count;
1754 if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
1758 init_waitqueue_head(&ca->wait_queue);
1761 ca->next_read_slot = 0;
1762 pubca->private = ca;
1764 /* register the DVB device */
1765 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA, 0);
1767 goto free_slot_info;
1769 /* now initialise each slot */
1770 for (i = 0; i < slot_count; i++) {
1771 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
1772 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
1773 atomic_set(&ca->slot_info[i].camchange_count, 0);
1774 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1775 mutex_init(&ca->slot_info[i].slot_lock);
1778 mutex_init(&ca->ioctl_mutex);
1780 if (signal_pending(current)) {
1782 goto unregister_device;
1786 /* create a kthread for monitoring this CA device */
1787 ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
1788 ca->dvbdev->adapter->num, ca->dvbdev->id);
1789 if (IS_ERR(ca->thread)) {
1790 ret = PTR_ERR(ca->thread);
1791 pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
1793 goto unregister_device;
1798 dvb_unregister_device(ca->dvbdev);
1800 kfree(ca->slot_info);
1804 pubca->private = NULL;
1807 EXPORT_SYMBOL(dvb_ca_en50221_release);
1812 * Release a DVB CA EN50221 interface device.
1814 * @ca_dev: The dvb_device_t instance for the CA device.
1815 * @ca: The associated dvb_ca instance.
1817 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1819 struct dvb_ca_private *ca = pubca->private;
1822 dprintk("%s\n", __func__);
1824 /* shutdown the thread if there was one */
1825 kthread_stop(ca->thread);
1827 for (i = 0; i < ca->slot_count; i++) {
1828 dvb_ca_en50221_slot_shutdown(ca, i);
1830 dvb_remove_device(ca->dvbdev);
1831 dvb_ca_private_put(ca);
1832 pubca->private = NULL;