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 */
80 #define DVB_CA_SLOTSTATE_NONE 0
81 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
82 #define DVB_CA_SLOTSTATE_RUNNING 2
83 #define DVB_CA_SLOTSTATE_INVALID 3
84 #define DVB_CA_SLOTSTATE_WAITREADY 4
85 #define DVB_CA_SLOTSTATE_VALIDATE 5
86 #define DVB_CA_SLOTSTATE_WAITFR 6
87 #define DVB_CA_SLOTSTATE_LINKINIT 7
89 /* Information on a CA slot */
91 /* current state of the CAM */
94 /* mutex used for serializing access to one CI slot */
95 struct mutex slot_lock;
97 /* Number of CAMCHANGES that have occurred since last processing */
98 atomic_t camchange_count;
100 /* Type of last CAMCHANGE */
103 /* base address of CAM config */
106 /* value to write into Config Control register */
109 /* if 1, the CAM supports DA IRQs */
110 u8 da_irq_supported:1;
112 /* size of the buffer to use when talking to the CAM */
115 /* buffer for incoming packets */
116 struct dvb_ringbuffer rx_buffer;
118 /* timer used during various states of the slot */
119 unsigned long timeout;
122 /* Private CA-interface information */
123 struct dvb_ca_private {
124 struct kref refcount;
126 /* pointer back to the public data structure */
127 struct dvb_ca_en50221 *pub;
130 struct dvb_device *dvbdev;
132 /* Flags describing the interface (DVB_CA_FLAG_*) */
135 /* number of slots supported by this CA interface */
136 unsigned int slot_count;
138 /* information on each slot */
139 struct dvb_ca_slot *slot_info;
141 /* wait queues for read() and write() operations */
142 wait_queue_head_t wait_queue;
144 /* PID of the monitoring thread */
145 struct task_struct *thread;
147 /* Flag indicating if the CA device is open */
150 /* Flag indicating the thread should wake up now */
151 unsigned int wakeup:1;
153 /* Delay the main thread should use */
157 * Slot to start looking for data to read from in the next user-space
162 /* mutex serializing ioctls */
163 struct mutex ioctl_mutex;
165 /* A mutex used when a device is disconnected */
166 struct mutex remove_mutex;
168 /* Whether the device is disconnected */
172 static void dvb_ca_private_free(struct dvb_ca_private *ca)
176 dvb_device_put(ca->dvbdev);
177 for (i = 0; i < ca->slot_count; i++)
178 vfree(ca->slot_info[i].rx_buffer.data);
180 kfree(ca->slot_info);
184 static void dvb_ca_private_release(struct kref *ref)
186 struct dvb_ca_private *ca;
188 ca = container_of(ref, struct dvb_ca_private, refcount);
189 dvb_ca_private_free(ca);
192 static void dvb_ca_private_get(struct dvb_ca_private *ca)
194 kref_get(&ca->refcount);
197 static void dvb_ca_private_put(struct dvb_ca_private *ca)
199 kref_put(&ca->refcount, dvb_ca_private_release);
202 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
203 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot,
204 u8 *ebuf, int ecount);
205 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot,
206 u8 *ebuf, int ecount);
209 * Safely find needle in haystack.
211 * @haystack: Buffer to look in.
212 * @hlen: Number of bytes in haystack.
213 * @needle: Buffer to find.
214 * @nlen: Number of bytes in needle.
215 * @return Pointer into haystack needle was found at, or NULL if not found.
217 static char *findstr(char *haystack, int hlen, char *needle, int nlen)
224 for (i = 0; i <= hlen - nlen; i++) {
225 if (!strncmp(haystack + i, needle, nlen))
232 /* ************************************************************************** */
233 /* EN50221 physical interface functions */
236 * dvb_ca_en50221_check_camstatus - Check CAM status.
238 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
240 struct dvb_ca_slot *sl = &ca->slot_info[slot];
246 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
247 return (atomic_read(&sl->camchange_count) != 0);
250 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
252 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
253 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
255 int cam_present_old = (sl->slot_state != DVB_CA_SLOTSTATE_NONE);
257 cam_changed = (cam_present_now != cam_present_old);
261 if (!cam_present_now)
262 sl->camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
264 sl->camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
265 atomic_set(&sl->camchange_count, 1);
267 if ((sl->slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
268 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
269 /* move to validate state if reset is completed */
270 sl->slot_state = DVB_CA_SLOTSTATE_VALIDATE;
278 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
279 * register on a CAM interface, checking for errors and timeout.
282 * @slot: Slot on interface.
283 * @waitfor: Flags to wait for.
284 * @timeout_ms: Timeout in milliseconds.
286 * @return 0 on success, nonzero on error.
288 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
289 u8 waitfor, int timeout_hz)
291 unsigned long timeout;
294 dprintk("%s\n", __func__);
296 /* loop until timeout elapsed */
298 timeout = jiffies + timeout_hz;
302 /* read the status and check for error */
303 res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
307 /* if we got the flags, it was successful! */
309 dprintk("%s succeeded timeout:%lu\n",
310 __func__, jiffies - start);
314 /* check for timeout */
315 if (time_after(jiffies, timeout))
319 usleep_range(1000, 1100);
322 dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
324 /* if we get here, we've timed out */
329 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
334 * @return 0 on success, nonzero on failure.
336 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
338 struct dvb_ca_slot *sl = &ca->slot_info[slot];
343 dprintk("%s\n", __func__);
345 /* we'll be determining these during this function */
346 sl->da_irq_supported = 0;
349 * set the host link buffer size temporarily. it will be overwritten
350 * with the real negotiated size later.
352 sl->link_buf_size = 2;
354 /* read the buffer size from the CAM */
355 ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
359 ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ);
362 ret = dvb_ca_en50221_read_data(ca, slot, buf, 2);
365 ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
370 * store it, and choose the minimum of our buffer and the CAM's buffer
373 buf_size = (buf[0] << 8) | buf[1];
374 if (buf_size > HOST_LINK_BUF_SIZE)
375 buf_size = HOST_LINK_BUF_SIZE;
376 sl->link_buf_size = buf_size;
377 buf[0] = buf_size >> 8;
378 buf[1] = buf_size & 0xff;
379 dprintk("Chosen link buffer size of %i\n", buf_size);
381 /* write the buffer size to the CAM */
382 ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
386 ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10);
389 ret = dvb_ca_en50221_write_data(ca, slot, buf, 2);
392 ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
401 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
405 * @address: Address to read from. Updated.
406 * @tupleType: Tuple id byte. Updated.
407 * @tupleLength: Tuple length. Updated.
408 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
410 * @return 0 on success, nonzero on error.
412 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
413 int *address, int *tuple_type,
414 int *tuple_length, u8 *tuple)
419 int _address = *address;
421 /* grab the next tuple length and type */
422 _tuple_type = ca->pub->read_attribute_mem(ca->pub, slot, _address);
425 if (_tuple_type == 0xff) {
426 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tuple_type);
428 *tuple_type = _tuple_type;
432 _tuple_length = ca->pub->read_attribute_mem(ca->pub, slot,
434 if (_tuple_length < 0)
435 return _tuple_length;
438 dprintk("TUPLE type:0x%x length:%i\n", _tuple_type, _tuple_length);
440 /* read in the whole tuple */
441 for (i = 0; i < _tuple_length; i++) {
442 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot,
444 dprintk(" 0x%02x: 0x%02x %c\n",
446 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
448 _address += (_tuple_length * 2);
451 *tuple_type = _tuple_type;
452 *tuple_length = _tuple_length;
458 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
459 * extracting Config register, and checking it is a DVB CAM module.
464 * @return 0 on success, <0 on failure.
466 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
468 struct dvb_ca_slot *sl;
476 int got_cftableentry = 0;
482 /* CISTPL_DEVICE_0A */
483 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
484 &tuple_length, tuple);
487 if (tuple_type != 0x1D)
490 /* CISTPL_DEVICE_0C */
491 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
492 &tuple_length, tuple);
495 if (tuple_type != 0x1C)
499 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
500 &tuple_length, tuple);
503 if (tuple_type != 0x15)
507 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
508 &tuple_length, tuple);
511 if (tuple_type != 0x20)
513 if (tuple_length != 4)
515 manfid = (tuple[1] << 8) | tuple[0];
516 devid = (tuple[3] << 8) | tuple[2];
519 status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
520 &tuple_length, tuple);
523 if (tuple_type != 0x1A)
525 if (tuple_length < 3)
528 /* extract the configbase */
530 if (tuple_length < (3 + rasz + 14))
532 sl = &ca->slot_info[slot];
534 for (i = 0; i < rasz + 1; i++)
535 sl->config_base |= (tuple[2 + i] << (8 * i));
537 /* check it contains the correct DVB string */
538 dvb_str = findstr((char *)tuple, tuple_length, "DVB_CI_V", 8);
541 if (tuple_length < ((dvb_str - (char *)tuple) + 12))
544 /* is it a version we support? */
545 if (strncmp(dvb_str + 8, "1.00", 4)) {
546 pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
547 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9],
548 dvb_str[10], dvb_str[11]);
552 /* process the CFTABLE_ENTRY tuples, and any after those */
553 while ((!end_chain) && (address < 0x1000)) {
554 status = dvb_ca_en50221_read_tuple(ca, slot, &address,
555 &tuple_type, &tuple_length,
559 switch (tuple_type) {
560 case 0x1B: /* CISTPL_CFTABLE_ENTRY */
561 if (tuple_length < (2 + 11 + 17))
564 /* if we've already parsed one, just use it */
565 if (got_cftableentry)
568 /* get the config option */
569 sl->config_option = tuple[0] & 0x3f;
571 /* OK, check it contains the correct strings */
572 if (!findstr((char *)tuple, tuple_length,
574 !findstr((char *)tuple, tuple_length,
575 "DVB_CI_MODULE", 13))
578 got_cftableentry = 1;
581 case 0x14: /* CISTPL_NO_LINK */
584 case 0xFF: /* CISTPL_END */
588 default: /* Unknown tuple type - just skip this tuple */
589 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
590 tuple_type, tuple_length);
595 if ((address > 0x1000) || (!got_cftableentry))
598 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
599 manfid, devid, sl->config_base, sl->config_option);
606 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
609 * @slot: Slot containing the CAM.
611 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
613 struct dvb_ca_slot *sl = &ca->slot_info[slot];
616 dprintk("%s\n", __func__);
618 /* set the config option */
619 ca->pub->write_attribute_mem(ca->pub, slot, sl->config_base,
623 configoption = ca->pub->read_attribute_mem(ca->pub, slot,
625 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
626 sl->config_option, configoption & 0x3f);
633 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
634 * interface. It reads a buffer of data from the CAM. The data can either
635 * be stored in a supplied buffer, or automatically be added to the slot's
639 * @slot: Slot to read from.
640 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
641 * the data will be added into the buffering system as a normal fragment.
642 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
644 * @return Number of bytes read, or < 0 on error
646 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot,
647 u8 *ebuf, int ecount)
649 struct dvb_ca_slot *sl = &ca->slot_info[slot];
652 u8 buf[HOST_LINK_BUF_SIZE];
655 dprintk("%s\n", __func__);
657 /* check if we have space for a link buf in the rx_buffer */
661 if (!sl->rx_buffer.data) {
665 buf_free = dvb_ringbuffer_free(&sl->rx_buffer);
667 if (buf_free < (sl->link_buf_size +
668 DVB_RINGBUFFER_PKTHDRSIZE)) {
674 if (ca->pub->read_data &&
675 (sl->slot_state != DVB_CA_SLOTSTATE_LINKINIT)) {
677 status = ca->pub->read_data(ca->pub, slot, buf,
680 status = ca->pub->read_data(ca->pub, slot, buf, ecount);
687 /* check if there is data available */
688 status = ca->pub->read_cam_control(ca->pub, slot,
692 if (!(status & STATUSREG_DA)) {
698 /* read the amount of data */
699 status = ca->pub->read_cam_control(ca->pub, slot,
703 bytes_read = status << 8;
704 status = ca->pub->read_cam_control(ca->pub, slot,
708 bytes_read |= status;
710 /* check it will fit */
712 if (bytes_read > sl->link_buf_size) {
713 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
714 ca->dvbdev->adapter->num, bytes_read,
716 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
720 if (bytes_read < 2) {
721 pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
722 ca->dvbdev->adapter->num);
723 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
728 if (bytes_read > ecount) {
729 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
730 ca->dvbdev->adapter->num);
736 /* fill the buffer */
737 for (i = 0; i < bytes_read; i++) {
738 /* read byte and check */
739 status = ca->pub->read_cam_control(ca->pub, slot,
744 /* OK, store it in the buffer */
748 /* check for read error (RE should now be 0) */
749 status = ca->pub->read_cam_control(ca->pub, slot,
753 if (status & STATUSREG_RE) {
754 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
761 * OK, add it to the receive buffer, or copy into external buffer if
765 if (!sl->rx_buffer.data) {
769 dvb_ringbuffer_pkt_write(&sl->rx_buffer, buf, bytes_read);
771 memcpy(ebuf, buf, bytes_read);
774 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
775 buf[0], (buf[1] & 0x80) == 0, bytes_read);
777 /* wake up readers when a last_fragment is received */
778 if ((buf[1] & 0x80) == 0x00)
779 wake_up_interruptible(&ca->wait_queue);
788 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
789 * interface. It writes a buffer of data to a CAM.
792 * @slot: Slot to write to.
793 * @ebuf: The data in this buffer is treated as a complete link-level packet to
795 * @count: Size of ebuf.
797 * @return Number of bytes written, or < 0 on error.
799 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot,
800 u8 *buf, int bytes_write)
802 struct dvb_ca_slot *sl = &ca->slot_info[slot];
806 dprintk("%s\n", __func__);
809 if (bytes_write > sl->link_buf_size)
812 if (ca->pub->write_data &&
813 (sl->slot_state != DVB_CA_SLOTSTATE_LINKINIT))
814 return ca->pub->write_data(ca->pub, slot, buf, bytes_write);
817 * it is possible we are dealing with a single buffer implementation,
818 * thus if there is data available for read or if there is even a read
819 * already in progress, we do nothing but awake the kernel thread to
820 * process the data if necessary.
822 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
825 if (status & (STATUSREG_DA | STATUSREG_RE)) {
826 if (status & STATUSREG_DA)
827 dvb_ca_en50221_thread_wakeup(ca);
834 status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
839 /* check if interface is still free */
840 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
843 if (!(status & STATUSREG_FR)) {
844 /* it wasn't free => try again later */
850 * It may need some time for the CAM to settle down, or there might
851 * be a race condition between the CAM, writing HC and our last
852 * check for DA. This happens, if the CAM asserts DA, just after
853 * checking DA before we are setting HC. In this case it might be
854 * a bug in the CAM to keep the FR bit, the lower layer/HW
855 * communication requires a longer timeout or the CAM needs more
856 * time internally. But this happens in reality!
857 * We need to read the status from the HW again and do the same
858 * we did for the previous check for DA
860 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
864 if (status & (STATUSREG_DA | STATUSREG_RE)) {
865 if (status & STATUSREG_DA)
866 dvb_ca_en50221_thread_wakeup(ca);
872 /* send the amount of data */
873 status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH,
877 status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
882 /* send the buffer */
883 for (i = 0; i < bytes_write; i++) {
884 status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA,
890 /* check for write error (WE should now be 0) */
891 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
894 if (status & STATUSREG_WE) {
895 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
899 status = bytes_write;
901 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
902 buf[0], (buf[1] & 0x80) == 0, bytes_write);
905 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
911 /* ************************************************************************** */
912 /* EN50221 higher level functions */
915 * dvb_ca_en50221_slot_shutdown - A CAM has been removed => shut it down.
918 * @slot: Slot to shut down.
920 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
922 dprintk("%s\n", __func__);
924 ca->pub->slot_shutdown(ca->pub, slot);
925 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
928 * need to wake up all processes to check if they're now trying to
929 * write to a defunct CAM
931 wake_up_interruptible(&ca->wait_queue);
933 dprintk("Slot %i shutdown\n", slot);
940 * dvb_ca_en50221_camchange_irq - A CAMCHANGE IRQ has occurred.
943 * @slot: Slot concerned.
944 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
946 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot,
949 struct dvb_ca_private *ca = pubca->private;
950 struct dvb_ca_slot *sl = &ca->slot_info[slot];
952 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
954 switch (change_type) {
955 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
956 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
963 sl->camchange_type = change_type;
964 atomic_inc(&sl->camchange_count);
965 dvb_ca_en50221_thread_wakeup(ca);
967 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
970 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
973 * @slot: Slot concerned.
975 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
977 struct dvb_ca_private *ca = pubca->private;
978 struct dvb_ca_slot *sl = &ca->slot_info[slot];
980 dprintk("CAMREADY IRQ slot:%i\n", slot);
982 if (sl->slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
983 sl->slot_state = DVB_CA_SLOTSTATE_VALIDATE;
984 dvb_ca_en50221_thread_wakeup(ca);
987 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
990 * dvb_ca_en50221_frda_irq - An FR or DA IRQ has occurred.
993 * @slot: Slot concerned.
995 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
997 struct dvb_ca_private *ca = pubca->private;
998 struct dvb_ca_slot *sl = &ca->slot_info[slot];
1001 dprintk("FR/DA IRQ slot:%i\n", slot);
1003 switch (sl->slot_state) {
1004 case DVB_CA_SLOTSTATE_LINKINIT:
1005 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
1006 if (flags & STATUSREG_DA) {
1007 dprintk("CAM supports DA IRQ\n");
1008 sl->da_irq_supported = 1;
1012 case DVB_CA_SLOTSTATE_RUNNING:
1014 dvb_ca_en50221_thread_wakeup(ca);
1018 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
1020 /* ************************************************************************** */
1021 /* EN50221 thread functions */
1024 * Wake up the DVB CA thread
1028 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
1030 dprintk("%s\n", __func__);
1034 wake_up_process(ca->thread);
1038 * Update the delay used by the thread.
1042 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
1045 int curdelay = 100000000;
1049 * Beware of too high polling frequency, because one polling
1050 * call might take several hundred milliseconds until timeout!
1052 for (slot = 0; slot < ca->slot_count; slot++) {
1053 struct dvb_ca_slot *sl = &ca->slot_info[slot];
1055 switch (sl->slot_state) {
1057 case DVB_CA_SLOTSTATE_NONE:
1058 delay = HZ * 60; /* 60s */
1059 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1060 delay = HZ * 5; /* 5s */
1062 case DVB_CA_SLOTSTATE_INVALID:
1063 delay = HZ * 60; /* 60s */
1064 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1065 delay = HZ / 10; /* 100ms */
1068 case DVB_CA_SLOTSTATE_UNINITIALISED:
1069 case DVB_CA_SLOTSTATE_WAITREADY:
1070 case DVB_CA_SLOTSTATE_VALIDATE:
1071 case DVB_CA_SLOTSTATE_WAITFR:
1072 case DVB_CA_SLOTSTATE_LINKINIT:
1073 delay = HZ / 10; /* 100ms */
1076 case DVB_CA_SLOTSTATE_RUNNING:
1077 delay = HZ * 60; /* 60s */
1078 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1079 delay = HZ / 10; /* 100ms */
1081 if ((!sl->da_irq_supported) ||
1082 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
1083 delay = HZ / 10; /* 100ms */
1088 if (delay < curdelay)
1092 ca->delay = curdelay;
1096 * Poll if the CAM is gone.
1099 * @slot: Slot to process.
1100 * @return: 0 .. no change
1101 * 1 .. CAM state changed
1104 static int dvb_ca_en50221_poll_cam_gone(struct dvb_ca_private *ca, int slot)
1110 * we need this extra check for annoying interfaces like the
1113 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1114 (ca->pub->poll_slot_status)) {
1115 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1117 DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1118 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1119 dvb_ca_en50221_thread_update_delay(ca);
1127 * Thread state machine for one CA slot to perform the data transfer.
1130 * @slot: Slot to process.
1132 static void dvb_ca_en50221_thread_state_machine(struct dvb_ca_private *ca,
1135 struct dvb_ca_slot *sl = &ca->slot_info[slot];
1140 mutex_lock(&sl->slot_lock);
1142 /* check the cam status + deal with CAMCHANGEs */
1143 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1144 /* clear down an old CI slot if necessary */
1145 if (sl->slot_state != DVB_CA_SLOTSTATE_NONE)
1146 dvb_ca_en50221_slot_shutdown(ca, slot);
1148 /* if a CAM is NOW present, initialise it */
1149 if (sl->camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED)
1150 sl->slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1152 /* we've handled one CAMCHANGE */
1153 dvb_ca_en50221_thread_update_delay(ca);
1154 atomic_dec(&sl->camchange_count);
1157 /* CAM state machine */
1158 switch (sl->slot_state) {
1159 case DVB_CA_SLOTSTATE_NONE:
1160 case DVB_CA_SLOTSTATE_INVALID:
1161 /* no action needed */
1164 case DVB_CA_SLOTSTATE_UNINITIALISED:
1165 sl->slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1166 ca->pub->slot_reset(ca->pub, slot);
1167 sl->timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1170 case DVB_CA_SLOTSTATE_WAITREADY:
1171 if (time_after(jiffies, sl->timeout)) {
1172 pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
1173 ca->dvbdev->adapter->num);
1174 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1175 dvb_ca_en50221_thread_update_delay(ca);
1179 * no other action needed; will automatically change state when
1184 case DVB_CA_SLOTSTATE_VALIDATE:
1185 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1186 if (dvb_ca_en50221_poll_cam_gone(ca, slot))
1189 pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1190 ca->dvbdev->adapter->num);
1191 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1192 dvb_ca_en50221_thread_update_delay(ca);
1195 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1196 pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1197 ca->dvbdev->adapter->num);
1198 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1199 dvb_ca_en50221_thread_update_delay(ca);
1202 if (ca->pub->write_cam_control(ca->pub, slot,
1205 pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
1206 ca->dvbdev->adapter->num);
1207 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1208 dvb_ca_en50221_thread_update_delay(ca);
1211 dprintk("DVB CAM validated successfully\n");
1213 sl->timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1214 sl->slot_state = DVB_CA_SLOTSTATE_WAITFR;
1218 case DVB_CA_SLOTSTATE_WAITFR:
1219 if (time_after(jiffies, sl->timeout)) {
1220 pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1221 ca->dvbdev->adapter->num);
1222 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1223 dvb_ca_en50221_thread_update_delay(ca);
1227 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1228 if (flags & STATUSREG_FR) {
1229 sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1234 case DVB_CA_SLOTSTATE_LINKINIT:
1235 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1236 if (dvb_ca_en50221_poll_cam_gone(ca, slot))
1239 pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
1240 ca->dvbdev->adapter->num);
1241 sl->slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1242 dvb_ca_en50221_thread_update_delay(ca);
1246 if (!sl->rx_buffer.data) {
1247 rxbuf = vmalloc(RX_BUFFER_SIZE);
1249 pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1250 ca->dvbdev->adapter->num);
1251 sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
1252 dvb_ca_en50221_thread_update_delay(ca);
1255 dvb_ringbuffer_init(&sl->rx_buffer, rxbuf,
1259 ca->pub->slot_ts_enable(ca->pub, slot);
1260 sl->slot_state = DVB_CA_SLOTSTATE_RUNNING;
1261 dvb_ca_en50221_thread_update_delay(ca);
1262 pr_err("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
1263 ca->dvbdev->adapter->num);
1266 case DVB_CA_SLOTSTATE_RUNNING:
1270 /* poll slots for data */
1272 while (dvb_ca_en50221_read_data(ca, slot, NULL, 0) > 0) {
1277 * if a CAMCHANGE occurred at some point, do not do any
1278 * more processing of this slot
1280 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1282 * we don't want to sleep on the next iteration
1283 * so we can handle the cam change
1289 /* check if we've hit our limit this time */
1290 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1292 * don't sleep; there is likely to be more data
1302 mutex_unlock(&sl->slot_lock);
1306 * Kernel thread which monitors CA slots for CAM changes, and performs data
1309 static int dvb_ca_en50221_thread(void *data)
1311 struct dvb_ca_private *ca = data;
1314 dprintk("%s\n", __func__);
1316 /* choose the correct initial delay */
1317 dvb_ca_en50221_thread_update_delay(ca);
1320 while (!kthread_should_stop()) {
1321 /* sleep for a bit */
1323 set_current_state(TASK_INTERRUPTIBLE);
1324 schedule_timeout(ca->delay);
1325 if (kthread_should_stop())
1330 /* go through all the slots processing them */
1331 for (slot = 0; slot < ca->slot_count; slot++)
1332 dvb_ca_en50221_thread_state_machine(ca, slot);
1338 /* ************************************************************************** */
1339 /* EN50221 IO interface functions */
1342 * Real ioctl implementation.
1343 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1345 * @inode: Inode concerned.
1346 * @file: File concerned.
1347 * @cmd: IOCTL command.
1348 * @arg: Associated argument.
1350 * @return 0 on success, <0 on error.
1352 static int dvb_ca_en50221_io_do_ioctl(struct file *file,
1353 unsigned int cmd, void *parg)
1355 struct dvb_device *dvbdev = file->private_data;
1356 struct dvb_ca_private *ca = dvbdev->priv;
1360 dprintk("%s\n", __func__);
1362 if (mutex_lock_interruptible(&ca->ioctl_mutex))
1363 return -ERESTARTSYS;
1367 for (slot = 0; slot < ca->slot_count; slot++) {
1368 struct dvb_ca_slot *sl = &ca->slot_info[slot];
1370 mutex_lock(&sl->slot_lock);
1371 if (sl->slot_state != DVB_CA_SLOTSTATE_NONE) {
1372 dvb_ca_en50221_slot_shutdown(ca, slot);
1373 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1374 dvb_ca_en50221_camchange_irq(ca->pub,
1376 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1378 mutex_unlock(&sl->slot_lock);
1380 ca->next_read_slot = 0;
1381 dvb_ca_en50221_thread_wakeup(ca);
1385 struct ca_caps *caps = parg;
1387 caps->slot_num = ca->slot_count;
1388 caps->slot_type = CA_CI_LINK;
1389 caps->descr_num = 0;
1390 caps->descr_type = 0;
1394 case CA_GET_SLOT_INFO: {
1395 struct ca_slot_info *info = parg;
1396 struct dvb_ca_slot *sl;
1399 if ((slot > ca->slot_count) || (slot < 0)) {
1404 info->type = CA_CI_LINK;
1406 sl = &ca->slot_info[slot];
1407 if ((sl->slot_state != DVB_CA_SLOTSTATE_NONE) &&
1408 (sl->slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1409 info->flags = CA_CI_MODULE_PRESENT;
1411 if (sl->slot_state == DVB_CA_SLOTSTATE_RUNNING)
1412 info->flags |= CA_CI_MODULE_READY;
1422 mutex_unlock(&ca->ioctl_mutex);
1427 * Wrapper for ioctl implementation.
1429 * @inode: Inode concerned.
1430 * @file: File concerned.
1431 * @cmd: IOCTL command.
1432 * @arg: Associated argument.
1434 * @return 0 on success, <0 on error.
1436 static long dvb_ca_en50221_io_ioctl(struct file *file,
1437 unsigned int cmd, unsigned long arg)
1439 return dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1443 * Implementation of write() syscall.
1445 * @file: File structure.
1446 * @buf: Source buffer.
1447 * @count: Size of source buffer.
1448 * @ppos: Position in file (ignored).
1450 * @return Number of bytes read, or <0 on error.
1452 static ssize_t dvb_ca_en50221_io_write(struct file *file,
1453 const char __user *buf, size_t count,
1456 struct dvb_device *dvbdev = file->private_data;
1457 struct dvb_ca_private *ca = dvbdev->priv;
1458 struct dvb_ca_slot *sl;
1459 u8 slot, connection_id;
1461 u8 fragbuf[HOST_LINK_BUF_SIZE];
1464 unsigned long timeout;
1467 dprintk("%s\n", __func__);
1470 * Incoming packet has a 2 byte header.
1471 * hdr[0] = slot_id, hdr[1] = connection_id
1476 /* extract slot & connection id */
1477 if (copy_from_user(&slot, buf, 1))
1479 if (copy_from_user(&connection_id, buf + 1, 1))
1483 sl = &ca->slot_info[slot];
1485 /* check if the slot is actually running */
1486 if (sl->slot_state != DVB_CA_SLOTSTATE_RUNNING)
1489 /* fragment the packets & store in the buffer */
1490 while (fragpos < count) {
1491 fraglen = sl->link_buf_size - 2;
1494 if (fraglen > HOST_LINK_BUF_SIZE - 2)
1495 fraglen = HOST_LINK_BUF_SIZE - 2;
1496 if ((count - fragpos) < fraglen)
1497 fraglen = count - fragpos;
1499 fragbuf[0] = connection_id;
1500 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1501 status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
1507 timeout = jiffies + HZ / 2;
1509 while (!time_after(jiffies, timeout)) {
1511 * check the CAM hasn't been removed/reset in the
1514 if (sl->slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1519 mutex_lock(&sl->slot_lock);
1520 status = dvb_ca_en50221_write_data(ca, slot, fragbuf,
1522 mutex_unlock(&sl->slot_lock);
1523 if (status == (fraglen + 2)) {
1527 if (status != -EAGAIN)
1530 usleep_range(1000, 1100);
1546 * Condition for waking up in dvb_ca_en50221_io_read_condition
1548 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
1549 int *result, int *_slot)
1555 int connection_id = -1;
1559 slot = ca->next_read_slot;
1560 while ((slot_count < ca->slot_count) && (!found)) {
1561 struct dvb_ca_slot *sl = &ca->slot_info[slot];
1563 if (sl->slot_state != DVB_CA_SLOTSTATE_RUNNING)
1566 if (!sl->rx_buffer.data)
1569 idx = dvb_ringbuffer_pkt_next(&sl->rx_buffer, -1, &fraglen);
1571 dvb_ringbuffer_pkt_read(&sl->rx_buffer, idx, 0, hdr, 2);
1572 if (connection_id == -1)
1573 connection_id = hdr[0];
1574 if ((hdr[0] == connection_id) &&
1575 ((hdr[1] & 0x80) == 0)) {
1581 idx = dvb_ringbuffer_pkt_next(&sl->rx_buffer, idx,
1586 slot = (slot + 1) % ca->slot_count;
1590 ca->next_read_slot = slot;
1595 * Implementation of read() syscall.
1597 * @file: File structure.
1598 * @buf: Destination buffer.
1599 * @count: Size of destination buffer.
1600 * @ppos: Position in file (ignored).
1602 * @return Number of bytes read, or <0 on error.
1604 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user *buf,
1605 size_t count, loff_t *ppos)
1607 struct dvb_device *dvbdev = file->private_data;
1608 struct dvb_ca_private *ca = dvbdev->priv;
1609 struct dvb_ca_slot *sl;
1614 int connection_id = -1;
1616 int last_fragment = 0;
1621 dprintk("%s\n", __func__);
1624 * Outgoing packet has a 2 byte header.
1625 * hdr[0] = slot_id, hdr[1] = connection_id
1630 /* wait for some data */
1631 status = dvb_ca_en50221_io_read_condition(ca, &result, &slot);
1633 /* if we're in nonblocking mode, exit immediately */
1634 if (file->f_flags & O_NONBLOCK)
1635 return -EWOULDBLOCK;
1637 /* wait for some data */
1638 status = wait_event_interruptible(ca->wait_queue,
1639 dvb_ca_en50221_io_read_condition
1640 (ca, &result, &slot));
1642 if ((status < 0) || (result < 0)) {
1648 sl = &ca->slot_info[slot];
1649 idx = dvb_ringbuffer_pkt_next(&sl->rx_buffer, -1, &fraglen);
1653 pr_err("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n",
1654 ca->dvbdev->adapter->num);
1659 dvb_ringbuffer_pkt_read(&sl->rx_buffer, idx, 0, hdr, 2);
1660 if (connection_id == -1)
1661 connection_id = hdr[0];
1662 if (hdr[0] == connection_id) {
1663 if (pktlen < count) {
1664 if ((pktlen + fraglen - 2) > count)
1665 fraglen = count - pktlen;
1670 dvb_ringbuffer_pkt_read_user(&sl->rx_buffer,
1680 if ((hdr[1] & 0x80) == 0)
1685 idx2 = dvb_ringbuffer_pkt_next(&sl->rx_buffer, idx, &fraglen);
1687 dvb_ringbuffer_pkt_dispose(&sl->rx_buffer, idx);
1690 } while (!last_fragment);
1693 hdr[1] = connection_id;
1694 status = copy_to_user(buf, hdr, 2);
1706 * Implementation of file open syscall.
1708 * @inode: Inode concerned.
1709 * @file: File concerned.
1711 * @return 0 on success, <0 on failure.
1713 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1715 struct dvb_device *dvbdev = file->private_data;
1716 struct dvb_ca_private *ca = dvbdev->priv;
1720 dprintk("%s\n", __func__);
1722 mutex_lock(&ca->remove_mutex);
1725 mutex_unlock(&ca->remove_mutex);
1729 if (!try_module_get(ca->pub->owner)) {
1730 mutex_unlock(&ca->remove_mutex);
1734 err = dvb_generic_open(inode, file);
1736 module_put(ca->pub->owner);
1737 mutex_unlock(&ca->remove_mutex);
1741 for (i = 0; i < ca->slot_count; i++) {
1742 struct dvb_ca_slot *sl = &ca->slot_info[i];
1744 if (sl->slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1745 if (!sl->rx_buffer.data) {
1747 * it is safe to call this here without locks
1748 * because ca->open == 0. Data is not read in
1751 dvb_ringbuffer_flush(&sl->rx_buffer);
1757 dvb_ca_en50221_thread_update_delay(ca);
1758 dvb_ca_en50221_thread_wakeup(ca);
1760 dvb_ca_private_get(ca);
1762 mutex_unlock(&ca->remove_mutex);
1767 * Implementation of file close syscall.
1769 * @inode: Inode concerned.
1770 * @file: File concerned.
1772 * @return 0 on success, <0 on failure.
1774 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1776 struct dvb_device *dvbdev = file->private_data;
1777 struct dvb_ca_private *ca = dvbdev->priv;
1780 dprintk("%s\n", __func__);
1782 mutex_lock(&ca->remove_mutex);
1784 /* mark the CA device as closed */
1786 dvb_ca_en50221_thread_update_delay(ca);
1788 err = dvb_generic_release(inode, file);
1790 module_put(ca->pub->owner);
1792 dvb_ca_private_put(ca);
1794 if (dvbdev->users == 1 && ca->exit == 1) {
1795 mutex_unlock(&ca->remove_mutex);
1796 wake_up(&dvbdev->wait_queue);
1798 mutex_unlock(&ca->remove_mutex);
1805 * Implementation of poll() syscall.
1807 * @file: File concerned.
1808 * @wait: poll wait table.
1810 * @return Standard poll mask.
1812 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table *wait)
1814 struct dvb_device *dvbdev = file->private_data;
1815 struct dvb_ca_private *ca = dvbdev->priv;
1816 unsigned int mask = 0;
1820 dprintk("%s\n", __func__);
1822 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1)
1825 /* if there is something, return now */
1829 /* wait for something to happen */
1830 poll_wait(file, &ca->wait_queue, wait);
1832 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1)
1838 static const struct file_operations dvb_ca_fops = {
1839 .owner = THIS_MODULE,
1840 .read = dvb_ca_en50221_io_read,
1841 .write = dvb_ca_en50221_io_write,
1842 .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
1843 .open = dvb_ca_en50221_io_open,
1844 .release = dvb_ca_en50221_io_release,
1845 .poll = dvb_ca_en50221_io_poll,
1846 .llseek = noop_llseek,
1849 static const struct dvb_device dvbdev_ca = {
1854 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1855 .name = "dvb-ca-en50221",
1857 .fops = &dvb_ca_fops,
1860 /* ************************************************************************** */
1861 /* Initialisation/shutdown functions */
1864 * Initialise a new DVB CA EN50221 interface device.
1866 * @dvb_adapter: DVB adapter to attach the new CA device to.
1867 * @ca: The dvb_ca instance.
1868 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1869 * @slot_count: Number of slots supported.
1871 * @return 0 on success, nonzero on failure
1873 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1874 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1877 struct dvb_ca_private *ca = NULL;
1880 dprintk("%s\n", __func__);
1885 /* initialise the system data */
1886 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1891 kref_init(&ca->refcount);
1894 ca->slot_count = slot_count;
1895 ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot),
1897 if (!ca->slot_info) {
1901 init_waitqueue_head(&ca->wait_queue);
1904 ca->next_read_slot = 0;
1905 pubca->private = ca;
1907 /* register the DVB device */
1908 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca,
1911 goto free_slot_info;
1913 /* now initialise each slot */
1914 for (i = 0; i < slot_count; i++) {
1915 struct dvb_ca_slot *sl = &ca->slot_info[i];
1917 memset(sl, 0, sizeof(struct dvb_ca_slot));
1918 sl->slot_state = DVB_CA_SLOTSTATE_NONE;
1919 atomic_set(&sl->camchange_count, 0);
1920 sl->camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1921 mutex_init(&sl->slot_lock);
1924 mutex_init(&ca->ioctl_mutex);
1925 mutex_init(&ca->remove_mutex);
1927 if (signal_pending(current)) {
1929 goto unregister_device;
1933 /* create a kthread for monitoring this CA device */
1934 ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
1935 ca->dvbdev->adapter->num, ca->dvbdev->id);
1936 if (IS_ERR(ca->thread)) {
1937 ret = PTR_ERR(ca->thread);
1938 pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
1940 goto unregister_device;
1945 dvb_unregister_device(ca->dvbdev);
1947 kfree(ca->slot_info);
1951 pubca->private = NULL;
1954 EXPORT_SYMBOL(dvb_ca_en50221_init);
1957 * Release a DVB CA EN50221 interface device.
1959 * @ca_dev: The dvb_device_t instance for the CA device.
1960 * @ca: The associated dvb_ca instance.
1962 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1964 struct dvb_ca_private *ca = pubca->private;
1967 dprintk("%s\n", __func__);
1969 mutex_lock(&ca->remove_mutex);
1971 mutex_unlock(&ca->remove_mutex);
1973 if (ca->dvbdev->users < 1)
1974 wait_event(ca->dvbdev->wait_queue,
1975 ca->dvbdev->users == 1);
1977 /* shutdown the thread if there was one */
1978 kthread_stop(ca->thread);
1980 for (i = 0; i < ca->slot_count; i++)
1981 dvb_ca_en50221_slot_shutdown(ca, i);
1983 dvb_remove_device(ca->dvbdev);
1984 dvb_ca_private_put(ca);
1985 pubca->private = NULL;
1987 EXPORT_SYMBOL(dvb_ca_en50221_release);