2 * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces
4 * Copyright (C) 2004 Andrew de Quincey
6 * Parts of this file were based on sources as follows:
8 * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de>
12 * Copyright (C) 1999-2002 Ralph Metzler
13 * & Marcus Metzler for convergence integrated media GmbH
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <linux/list.h>
34 #include <linux/module.h>
35 #include <linux/vmalloc.h>
36 #include <linux/delay.h>
37 #include <linux/spinlock.h>
38 #include <linux/sched.h>
39 #include <linux/kthread.h>
41 #include "dvb_ca_en50221.h"
42 #include "dvb_ringbuffer.h"
44 static int dvb_ca_en50221_debug
;
46 module_param_named(cam_debug
, dvb_ca_en50221_debug
, int, 0644);
47 MODULE_PARM_DESC(cam_debug
, "enable verbose debug messages");
49 #define dprintk if (dvb_ca_en50221_debug) printk
51 #define INIT_TIMEOUT_SECS 10
53 #define HOST_LINK_BUF_SIZE 0x200
55 #define RX_BUFFER_SIZE 65535
57 #define MAX_RX_PACKETS_PER_ITERATION 10
60 #define CTRLIF_COMMAND 1
61 #define CTRLIF_STATUS 1
62 #define CTRLIF_SIZE_LOW 2
63 #define CTRLIF_SIZE_HIGH 3
65 #define CMDREG_HC 1 /* Host control */
66 #define CMDREG_SW 2 /* Size write */
67 #define CMDREG_SR 4 /* Size read */
68 #define CMDREG_RS 8 /* Reset interface */
69 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
70 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
71 #define IRQEN (CMDREG_DAIE)
73 #define STATUSREG_RE 1 /* read error */
74 #define STATUSREG_WE 2 /* write error */
75 #define STATUSREG_FR 0x40 /* module free */
76 #define STATUSREG_DA 0x80 /* data available */
77 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
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
90 /* Information on a CA slot */
93 /* current state of the CAM */
96 /* mutex used for serializing access to one CI slot */
97 struct mutex slot_lock
;
99 /* Number of CAMCHANGES that have occurred since last processing */
100 atomic_t camchange_count
;
102 /* Type of last CAMCHANGE */
105 /* base address of CAM config */
108 /* value to write into Config Control register */
111 /* if 1, the CAM supports DA IRQs */
112 u8 da_irq_supported
:1;
114 /* size of the buffer to use when talking to the CAM */
117 /* buffer for incoming packets */
118 struct dvb_ringbuffer rx_buffer
;
120 /* timer used during various states of the slot */
121 unsigned long timeout
;
124 /* Private CA-interface information */
125 struct dvb_ca_private
{
127 /* pointer back to the public data structure */
128 struct dvb_ca_en50221
*pub
;
131 struct dvb_device
*dvbdev
;
133 /* Flags describing the interface (DVB_CA_FLAG_*) */
136 /* number of slots supported by this CA interface */
137 unsigned int slot_count
;
139 /* information on each slot */
140 struct dvb_ca_slot
*slot_info
;
142 /* wait queues for read() and write() operations */
143 wait_queue_head_t wait_queue
;
145 /* PID of the monitoring thread */
146 struct task_struct
*thread
;
148 /* Flag indicating if the CA device is open */
151 /* Flag indicating the thread should wake up now */
152 unsigned int wakeup
:1;
154 /* Delay the main thread should use */
157 /* Slot to start looking for data to read from in the next user-space read operation */
160 /* mutex serializing ioctls */
161 struct mutex ioctl_mutex
;
164 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
165 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
166 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
170 * Safely find needle in haystack.
172 * @param haystack Buffer to look in.
173 * @param hlen Number of bytes in haystack.
174 * @param needle Buffer to find.
175 * @param nlen Number of bytes in needle.
176 * @return Pointer into haystack needle was found at, or NULL if not found.
178 static char *findstr(char * haystack
, int hlen
, char * needle
, int nlen
)
185 for (i
= 0; i
<= hlen
- nlen
; i
++) {
186 if (!strncmp(haystack
+ i
, needle
, nlen
))
195 /* ******************************************************************************** */
196 /* EN50221 physical interface functions */
202 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private
*ca
, int slot
)
209 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
) {
210 return (atomic_read(&ca
->slot_info
[slot
].camchange_count
) != 0);
214 slot_status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, ca
->open
);
216 cam_present_now
= (slot_status
& DVB_CA_EN50221_POLL_CAM_PRESENT
) ? 1 : 0;
217 cam_changed
= (slot_status
& DVB_CA_EN50221_POLL_CAM_CHANGED
) ? 1 : 0;
219 int cam_present_old
= (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
);
220 cam_changed
= (cam_present_now
!= cam_present_old
);
224 if (!cam_present_now
) {
225 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
227 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
229 atomic_set(&ca
->slot_info
[slot
].camchange_count
, 1);
231 if ((ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) &&
232 (slot_status
& DVB_CA_EN50221_POLL_CAM_READY
)) {
233 // move to validate state if reset is completed
234 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
243 * Wait for flags to become set on the STATUS register on a CAM interface,
244 * checking for errors and timeout.
246 * @param ca CA instance.
247 * @param slot Slot on interface.
248 * @param waitfor Flags to wait for.
249 * @param timeout_ms Timeout in milliseconds.
251 * @return 0 on success, nonzero on error.
253 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
254 u8 waitfor
, int timeout_hz
)
256 unsigned long timeout
;
259 dprintk("%s\n", __func__
);
261 /* loop until timeout elapsed */
263 timeout
= jiffies
+ timeout_hz
;
265 /* read the status and check for error */
266 int res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
270 /* if we got the flags, it was successful! */
272 dprintk("%s succeeded timeout:%lu\n", __func__
, jiffies
- start
);
276 /* check for timeout */
277 if (time_after(jiffies
, timeout
)) {
285 dprintk("%s failed timeout:%lu\n", __func__
, jiffies
- start
);
287 /* if we get here, we've timed out */
293 * Initialise the link layer connection to a CAM.
295 * @param ca CA instance.
296 * @param slot Slot id.
298 * @return 0 on success, nonzero on failure.
300 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
306 dprintk("%s\n", __func__
);
308 /* we'll be determining these during this function */
309 ca
->slot_info
[slot
].da_irq_supported
= 0;
311 /* set the host link buffer size temporarily. it will be overwritten with the
312 * real negotiated size later. */
313 ca
->slot_info
[slot
].link_buf_size
= 2;
315 /* read the buffer size from the CAM */
316 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SR
)) != 0)
318 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
/ 10)) != 0)
320 if ((ret
= dvb_ca_en50221_read_data(ca
, slot
, buf
, 2)) != 2)
322 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
325 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
326 buf_size
= (buf
[0] << 8) | buf
[1];
327 if (buf_size
> HOST_LINK_BUF_SIZE
)
328 buf_size
= HOST_LINK_BUF_SIZE
;
329 ca
->slot_info
[slot
].link_buf_size
= buf_size
;
330 buf
[0] = buf_size
>> 8;
331 buf
[1] = buf_size
& 0xff;
332 dprintk("Chosen link buffer size of %i\n", buf_size
);
334 /* write the buffer size to the CAM */
335 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SW
)) != 0)
337 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_FR
, HZ
/ 10)) != 0)
339 if ((ret
= dvb_ca_en50221_write_data(ca
, slot
, buf
, 2)) != 2)
341 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
349 * Read a tuple from attribute memory.
351 * @param ca CA instance.
352 * @param slot Slot id.
353 * @param address Address to read from. Updated.
354 * @param tupleType Tuple id byte. Updated.
355 * @param tupleLength Tuple length. Updated.
356 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
358 * @return 0 on success, nonzero on error.
360 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private
*ca
, int slot
,
361 int *address
, int *tupleType
, int *tupleLength
, u8
* tuple
)
366 int _address
= *address
;
368 /* grab the next tuple length and type */
369 if ((_tupleType
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
)) < 0)
371 if (_tupleType
== 0xff) {
372 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType
);
374 *tupleType
= _tupleType
;
378 if ((_tupleLength
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ 2)) < 0)
382 dprintk("TUPLE type:0x%x length:%i\n", _tupleType
, _tupleLength
);
384 /* read in the whole tuple */
385 for (i
= 0; i
< _tupleLength
; i
++) {
386 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ (i
* 2));
387 dprintk(" 0x%02x: 0x%02x %c\n",
389 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
391 _address
+= (_tupleLength
* 2);
394 *tupleType
= _tupleType
;
395 *tupleLength
= _tupleLength
;
402 * Parse attribute memory of a CAM module, extracting Config register, and checking
403 * it is a DVB CAM module.
405 * @param ca CA instance.
406 * @param slot Slot id.
408 * @return 0 on success, <0 on failure.
410 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
419 int got_cftableentry
= 0;
428 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
430 if (tupleType
!= 0x1D)
437 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
439 if (tupleType
!= 0x1C)
446 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
448 if (tupleType
!= 0x15)
454 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
455 &tupleLength
, tuple
)) < 0)
457 if (tupleType
!= 0x20)
459 if (tupleLength
!= 4)
461 manfid
= (tuple
[1] << 8) | tuple
[0];
462 devid
= (tuple
[3] << 8) | tuple
[2];
467 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
468 &tupleLength
, tuple
)) < 0)
470 if (tupleType
!= 0x1A)
475 /* extract the configbase */
477 if (tupleLength
< (3 + rasz
+ 14))
479 ca
->slot_info
[slot
].config_base
= 0;
480 for (i
= 0; i
< rasz
+ 1; i
++) {
481 ca
->slot_info
[slot
].config_base
|= (tuple
[2 + i
] << (8 * i
));
484 /* check it contains the correct DVB string */
485 dvb_str
= findstr((char *)tuple
, tupleLength
, "DVB_CI_V", 8);
488 if (tupleLength
< ((dvb_str
- (char *) tuple
) + 12))
491 /* is it a version we support? */
492 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
493 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
494 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9], dvb_str
[10], dvb_str
[11]);
498 /* process the CFTABLE_ENTRY tuples, and any after those */
499 while ((!end_chain
) && (address
< 0x1000)) {
500 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
501 &tupleLength
, tuple
)) < 0)
504 case 0x1B: // CISTPL_CFTABLE_ENTRY
505 if (tupleLength
< (2 + 11 + 17))
508 /* if we've already parsed one, just use it */
509 if (got_cftableentry
)
512 /* get the config option */
513 ca
->slot_info
[slot
].config_option
= tuple
[0] & 0x3f;
515 /* OK, check it contains the correct strings */
516 if ((findstr((char *)tuple
, tupleLength
, "DVB_HOST", 8) == NULL
) ||
517 (findstr((char *)tuple
, tupleLength
, "DVB_CI_MODULE", 13) == NULL
))
520 got_cftableentry
= 1;
523 case 0x14: // CISTPL_NO_LINK
526 case 0xFF: // CISTPL_END
530 default: /* Unknown tuple type - just skip this tuple and move to the next one */
531 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType
,
537 if ((address
> 0x1000) || (!got_cftableentry
))
540 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
541 manfid
, devid
, ca
->slot_info
[slot
].config_base
, ca
->slot_info
[slot
].config_option
);
549 * Set CAM's configoption correctly.
551 * @param ca CA instance.
552 * @param slot Slot containing the CAM.
554 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
558 dprintk("%s\n", __func__
);
560 /* set the config option */
561 ca
->pub
->write_attribute_mem(ca
->pub
, slot
,
562 ca
->slot_info
[slot
].config_base
,
563 ca
->slot_info
[slot
].config_option
);
566 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, ca
->slot_info
[slot
].config_base
);
567 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
568 ca
->slot_info
[slot
].config_option
, configoption
& 0x3f);
577 * This function talks to an EN50221 CAM control interface. It reads a buffer of
578 * data from the CAM. The data can either be stored in a supplied buffer, or
579 * automatically be added to the slot's rx_buffer.
581 * @param ca CA instance.
582 * @param slot Slot to read from.
583 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
584 * the data will be added into the buffering system as a normal fragment.
585 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
587 * @return Number of bytes read, or < 0 on error
589 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
)
593 u8 buf
[HOST_LINK_BUF_SIZE
];
596 dprintk("%s\n", __func__
);
598 /* check if we have space for a link buf in the rx_buffer */
602 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
606 buf_free
= dvb_ringbuffer_free(&ca
->slot_info
[slot
].rx_buffer
);
608 if (buf_free
< (ca
->slot_info
[slot
].link_buf_size
+ DVB_RINGBUFFER_PKTHDRSIZE
)) {
614 /* check if there is data available */
615 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
617 if (!(status
& STATUSREG_DA
)) {
623 /* read the amount of data */
624 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
)) < 0)
626 bytes_read
= status
<< 8;
627 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
)) < 0)
629 bytes_read
|= status
;
631 /* check it will fit */
633 if (bytes_read
> ca
->slot_info
[slot
].link_buf_size
) {
634 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
635 ca
->dvbdev
->adapter
->num
, bytes_read
, ca
->slot_info
[slot
].link_buf_size
);
636 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
640 if (bytes_read
< 2) {
641 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
642 ca
->dvbdev
->adapter
->num
);
643 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
648 if (bytes_read
> ecount
) {
649 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
650 ca
->dvbdev
->adapter
->num
);
656 /* fill the buffer */
657 for (i
= 0; i
< bytes_read
; i
++) {
658 /* read byte and check */
659 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_DATA
)) < 0)
662 /* OK, store it in the buffer */
666 /* check for read error (RE should now be 0) */
667 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
669 if (status
& STATUSREG_RE
) {
670 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
675 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
677 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
681 dvb_ringbuffer_pkt_write(&ca
->slot_info
[slot
].rx_buffer
, buf
, bytes_read
);
683 memcpy(ebuf
, buf
, bytes_read
);
686 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
687 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
689 /* wake up readers when a last_fragment is received */
690 if ((buf
[1] & 0x80) == 0x00) {
691 wake_up_interruptible(&ca
->wait_queue
);
701 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
704 * @param ca CA instance.
705 * @param slot Slot to write to.
706 * @param ebuf The data in this buffer is treated as a complete link-level packet to
708 * @param count Size of ebuf.
710 * @return Number of bytes written, or < 0 on error.
712 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* buf
, int bytes_write
)
717 dprintk("%s\n", __func__
);
721 if (bytes_write
> ca
->slot_info
[slot
].link_buf_size
)
724 /* it is possible we are dealing with a single buffer implementation,
725 thus if there is data available for read or if there is even a read
726 already in progress, we do nothing but awake the kernel thread to
727 process the data if necessary. */
728 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
730 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
731 if (status
& STATUSREG_DA
)
732 dvb_ca_en50221_thread_wakeup(ca
);
739 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
740 IRQEN
| CMDREG_HC
)) != 0)
743 /* check if interface is still free */
744 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
746 if (!(status
& STATUSREG_FR
)) {
747 /* it wasn't free => try again later */
752 /* send the amount of data */
753 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
, bytes_write
>> 8)) != 0)
755 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
756 bytes_write
& 0xff)) != 0)
759 /* send the buffer */
760 for (i
= 0; i
< bytes_write
; i
++) {
761 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
, buf
[i
])) != 0)
765 /* check for write error (WE should now be 0) */
766 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
768 if (status
& STATUSREG_WE
) {
769 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
773 status
= bytes_write
;
775 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
776 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
779 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
784 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
788 /* ******************************************************************************** */
789 /* EN50221 higher level functions */
793 * A CAM has been removed => shut it down.
795 * @param ca CA instance.
796 * @param slot Slot to shut down.
798 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
800 dprintk("%s\n", __func__
);
802 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
803 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
805 /* need to wake up all processes to check if they're now
806 trying to write to a defunct CAM */
807 wake_up_interruptible(&ca
->wait_queue
);
809 dprintk("Slot %i shutdown\n", slot
);
814 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
818 * A CAMCHANGE IRQ has occurred.
820 * @param ca CA instance.
821 * @param slot Slot concerned.
822 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
824 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
, int change_type
)
826 struct dvb_ca_private
*ca
= pubca
->private;
828 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
830 switch (change_type
) {
831 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
832 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
839 ca
->slot_info
[slot
].camchange_type
= change_type
;
840 atomic_inc(&ca
->slot_info
[slot
].camchange_count
);
841 dvb_ca_en50221_thread_wakeup(ca
);
843 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
847 * A CAMREADY IRQ has occurred.
849 * @param ca CA instance.
850 * @param slot Slot concerned.
852 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
854 struct dvb_ca_private
*ca
= pubca
->private;
856 dprintk("CAMREADY IRQ slot:%i\n", slot
);
858 if (ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
859 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
860 dvb_ca_en50221_thread_wakeup(ca
);
866 * An FR or DA IRQ has occurred.
868 * @param ca CA instance.
869 * @param slot Slot concerned.
871 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
873 struct dvb_ca_private
*ca
= pubca
->private;
876 dprintk("FR/DA IRQ slot:%i\n", slot
);
878 switch (ca
->slot_info
[slot
].slot_state
) {
879 case DVB_CA_SLOTSTATE_LINKINIT
:
880 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
881 if (flags
& STATUSREG_DA
) {
882 dprintk("CAM supports DA IRQ\n");
883 ca
->slot_info
[slot
].da_irq_supported
= 1;
887 case DVB_CA_SLOTSTATE_RUNNING
:
889 dvb_ca_en50221_thread_wakeup(ca
);
896 /* ******************************************************************************** */
897 /* EN50221 thread functions */
900 * Wake up the DVB CA thread
902 * @param ca CA instance.
904 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
907 dprintk("%s\n", __func__
);
911 wake_up_process(ca
->thread
);
915 * Update the delay used by the thread.
917 * @param ca CA instance.
919 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
922 int curdelay
= 100000000;
925 /* Beware of too high polling frequency, because one polling
926 * call might take several hundred milliseconds until timeout!
928 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
929 switch (ca
->slot_info
[slot
].slot_state
) {
931 case DVB_CA_SLOTSTATE_NONE
:
932 delay
= HZ
* 60; /* 60s */
933 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
934 delay
= HZ
* 5; /* 5s */
936 case DVB_CA_SLOTSTATE_INVALID
:
937 delay
= HZ
* 60; /* 60s */
938 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
939 delay
= HZ
/ 10; /* 100ms */
942 case DVB_CA_SLOTSTATE_UNINITIALISED
:
943 case DVB_CA_SLOTSTATE_WAITREADY
:
944 case DVB_CA_SLOTSTATE_VALIDATE
:
945 case DVB_CA_SLOTSTATE_WAITFR
:
946 case DVB_CA_SLOTSTATE_LINKINIT
:
947 delay
= HZ
/ 10; /* 100ms */
950 case DVB_CA_SLOTSTATE_RUNNING
:
951 delay
= HZ
* 60; /* 60s */
952 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
953 delay
= HZ
/ 10; /* 100ms */
955 if ((!ca
->slot_info
[slot
].da_irq_supported
) ||
956 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
)))
957 delay
= HZ
/ 10; /* 100ms */
962 if (delay
< curdelay
)
966 ca
->delay
= curdelay
;
972 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
974 static int dvb_ca_en50221_thread(void *data
)
976 struct dvb_ca_private
*ca
= data
;
983 dprintk("%s\n", __func__
);
985 /* choose the correct initial delay */
986 dvb_ca_en50221_thread_update_delay(ca
);
989 while (!kthread_should_stop()) {
990 /* sleep for a bit */
992 set_current_state(TASK_INTERRUPTIBLE
);
993 schedule_timeout(ca
->delay
);
994 if (kthread_should_stop())
999 /* go through all the slots processing them */
1000 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1002 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1004 // check the cam status + deal with CAMCHANGEs
1005 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1006 /* clear down an old CI slot if necessary */
1007 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1008 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1010 /* if a CAM is NOW present, initialise it */
1011 if (ca
->slot_info
[slot
].camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
) {
1012 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1015 /* we've handled one CAMCHANGE */
1016 dvb_ca_en50221_thread_update_delay(ca
);
1017 atomic_dec(&ca
->slot_info
[slot
].camchange_count
);
1020 // CAM state machine
1021 switch (ca
->slot_info
[slot
].slot_state
) {
1022 case DVB_CA_SLOTSTATE_NONE
:
1023 case DVB_CA_SLOTSTATE_INVALID
:
1027 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1028 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1029 ca
->pub
->slot_reset(ca
->pub
, slot
);
1030 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1033 case DVB_CA_SLOTSTATE_WAITREADY
:
1034 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1035 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1036 ca
->dvbdev
->adapter
->num
);
1037 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1038 dvb_ca_en50221_thread_update_delay(ca
);
1041 // no other action needed; will automatically change state when ready
1044 case DVB_CA_SLOTSTATE_VALIDATE
:
1045 if (dvb_ca_en50221_parse_attributes(ca
, slot
) != 0) {
1046 /* we need this extra check for annoying interfaces like the budget-av */
1047 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1048 (ca
->pub
->poll_slot_status
)) {
1049 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1050 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1051 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1052 dvb_ca_en50221_thread_update_delay(ca
);
1057 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1058 ca
->dvbdev
->adapter
->num
);
1059 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1060 dvb_ca_en50221_thread_update_delay(ca
);
1063 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1064 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1065 ca
->dvbdev
->adapter
->num
);
1066 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1067 dvb_ca_en50221_thread_update_delay(ca
);
1070 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1071 CTRLIF_COMMAND
, CMDREG_RS
) != 0) {
1072 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1073 ca
->dvbdev
->adapter
->num
);
1074 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1075 dvb_ca_en50221_thread_update_delay(ca
);
1078 dprintk("DVB CAM validated successfully\n");
1080 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1081 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1085 case DVB_CA_SLOTSTATE_WAITFR
:
1086 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1087 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1088 ca
->dvbdev
->adapter
->num
);
1089 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1090 dvb_ca_en50221_thread_update_delay(ca
);
1094 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1095 if (flags
& STATUSREG_FR
) {
1096 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1101 case DVB_CA_SLOTSTATE_LINKINIT
:
1102 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1103 /* we need this extra check for annoying interfaces like the budget-av */
1104 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1105 (ca
->pub
->poll_slot_status
)) {
1106 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1107 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1108 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1109 dvb_ca_en50221_thread_update_delay(ca
);
1114 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca
->dvbdev
->adapter
->num
);
1115 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1116 dvb_ca_en50221_thread_update_delay(ca
);
1120 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1121 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1122 if (rxbuf
== NULL
) {
1123 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca
->dvbdev
->adapter
->num
);
1124 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1125 dvb_ca_en50221_thread_update_delay(ca
);
1128 dvb_ringbuffer_init(&ca
->slot_info
[slot
].rx_buffer
, rxbuf
, RX_BUFFER_SIZE
);
1131 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1132 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1133 dvb_ca_en50221_thread_update_delay(ca
);
1134 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca
->dvbdev
->adapter
->num
);
1137 case DVB_CA_SLOTSTATE_RUNNING
:
1141 // poll slots for data
1143 while ((status
= dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0)) > 0) {
1147 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1148 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1149 // we dont want to sleep on the next iteration so we can handle the cam change
1154 /* check if we've hit our limit this time */
1155 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1156 // dont sleep; there is likely to be more data to read
1164 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1173 /* ******************************************************************************** */
1174 /* EN50221 IO interface functions */
1177 * Real ioctl implementation.
1178 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1180 * @param inode Inode concerned.
1181 * @param file File concerned.
1182 * @param cmd IOCTL command.
1183 * @param arg Associated argument.
1185 * @return 0 on success, <0 on error.
1187 static int dvb_ca_en50221_io_do_ioctl(struct file
*file
,
1188 unsigned int cmd
, void *parg
)
1190 struct dvb_device
*dvbdev
= file
->private_data
;
1191 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1195 dprintk("%s\n", __func__
);
1197 if (mutex_lock_interruptible(&ca
->ioctl_mutex
))
1198 return -ERESTARTSYS
;
1202 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1203 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1204 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1205 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1206 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1207 dvb_ca_en50221_camchange_irq(ca
->pub
,
1209 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1211 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1213 ca
->next_read_slot
= 0;
1214 dvb_ca_en50221_thread_wakeup(ca
);
1218 struct ca_caps
*caps
= parg
;
1220 caps
->slot_num
= ca
->slot_count
;
1221 caps
->slot_type
= CA_CI_LINK
;
1222 caps
->descr_num
= 0;
1223 caps
->descr_type
= 0;
1227 case CA_GET_SLOT_INFO
: {
1228 struct ca_slot_info
*info
= parg
;
1230 if ((info
->num
> ca
->slot_count
) || (info
->num
< 0)) {
1235 info
->type
= CA_CI_LINK
;
1237 if ((ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1238 && (ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_INVALID
)) {
1239 info
->flags
= CA_CI_MODULE_PRESENT
;
1241 if (ca
->slot_info
[info
->num
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1242 info
->flags
|= CA_CI_MODULE_READY
;
1253 mutex_unlock(&ca
->ioctl_mutex
);
1259 * Wrapper for ioctl implementation.
1261 * @param inode Inode concerned.
1262 * @param file File concerned.
1263 * @param cmd IOCTL command.
1264 * @param arg Associated argument.
1266 * @return 0 on success, <0 on error.
1268 static long dvb_ca_en50221_io_ioctl(struct file
*file
,
1269 unsigned int cmd
, unsigned long arg
)
1271 return dvb_usercopy(file
, cmd
, arg
, dvb_ca_en50221_io_do_ioctl
);
1276 * Implementation of write() syscall.
1278 * @param file File structure.
1279 * @param buf Source buffer.
1280 * @param count Size of source buffer.
1281 * @param ppos Position in file (ignored).
1283 * @return Number of bytes read, or <0 on error.
1285 static ssize_t
dvb_ca_en50221_io_write(struct file
*file
,
1286 const char __user
* buf
, size_t count
, loff_t
* ppos
)
1288 struct dvb_device
*dvbdev
= file
->private_data
;
1289 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1290 u8 slot
, connection_id
;
1292 u8 fragbuf
[HOST_LINK_BUF_SIZE
];
1295 unsigned long timeout
;
1298 dprintk("%s\n", __func__
);
1300 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1304 /* extract slot & connection id */
1305 if (copy_from_user(&slot
, buf
, 1))
1307 if (copy_from_user(&connection_id
, buf
+ 1, 1))
1312 /* check if the slot is actually running */
1313 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1316 /* fragment the packets & store in the buffer */
1317 while (fragpos
< count
) {
1318 fraglen
= ca
->slot_info
[slot
].link_buf_size
- 2;
1321 if (fraglen
> HOST_LINK_BUF_SIZE
- 2)
1322 fraglen
= HOST_LINK_BUF_SIZE
- 2;
1323 if ((count
- fragpos
) < fraglen
)
1324 fraglen
= count
- fragpos
;
1326 fragbuf
[0] = connection_id
;
1327 fragbuf
[1] = ((fragpos
+ fraglen
) < count
) ? 0x80 : 0x00;
1328 status
= copy_from_user(fragbuf
+ 2, buf
+ fragpos
, fraglen
);
1334 timeout
= jiffies
+ HZ
/ 2;
1336 while (!time_after(jiffies
, timeout
)) {
1337 /* check the CAM hasn't been removed/reset in the meantime */
1338 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
) {
1343 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1344 status
= dvb_ca_en50221_write_data(ca
, slot
, fragbuf
, fraglen
+ 2);
1345 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1346 if (status
== (fraglen
+ 2)) {
1350 if (status
!= -EAGAIN
)
1370 * Condition for waking up in dvb_ca_en50221_io_read_condition
1372 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private
*ca
,
1373 int *result
, int *_slot
)
1379 int connection_id
= -1;
1383 slot
= ca
->next_read_slot
;
1384 while ((slot_count
< ca
->slot_count
) && (!found
)) {
1385 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1388 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1392 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1394 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1395 if (connection_id
== -1)
1396 connection_id
= hdr
[0];
1397 if ((hdr
[0] == connection_id
) && ((hdr
[1] & 0x80) == 0)) {
1403 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1407 slot
= (slot
+ 1) % ca
->slot_count
;
1411 ca
->next_read_slot
= slot
;
1417 * Implementation of read() syscall.
1419 * @param file File structure.
1420 * @param buf Destination buffer.
1421 * @param count Size of destination buffer.
1422 * @param ppos Position in file (ignored).
1424 * @return Number of bytes read, or <0 on error.
1426 static ssize_t
dvb_ca_en50221_io_read(struct file
*file
, char __user
* buf
,
1427 size_t count
, loff_t
* ppos
)
1429 struct dvb_device
*dvbdev
= file
->private_data
;
1430 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1435 int connection_id
= -1;
1437 int last_fragment
= 0;
1442 dprintk("%s\n", __func__
);
1444 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1448 /* wait for some data */
1449 if ((status
= dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
)) == 0) {
1451 /* if we're in nonblocking mode, exit immediately */
1452 if (file
->f_flags
& O_NONBLOCK
)
1453 return -EWOULDBLOCK
;
1455 /* wait for some data */
1456 status
= wait_event_interruptible(ca
->wait_queue
,
1457 dvb_ca_en50221_io_read_condition
1458 (ca
, &result
, &slot
));
1460 if ((status
< 0) || (result
< 0)) {
1466 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1470 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca
->dvbdev
->adapter
->num
);
1475 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1476 if (connection_id
== -1)
1477 connection_id
= hdr
[0];
1478 if (hdr
[0] == connection_id
) {
1479 if (pktlen
< count
) {
1480 if ((pktlen
+ fraglen
- 2) > count
) {
1481 fraglen
= count
- pktlen
;
1486 if ((status
= dvb_ringbuffer_pkt_read_user(&ca
->slot_info
[slot
].rx_buffer
, idx
, 2,
1487 buf
+ pktlen
, fraglen
)) < 0) {
1493 if ((hdr
[1] & 0x80) == 0)
1498 idx2
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1500 dvb_ringbuffer_pkt_dispose(&ca
->slot_info
[slot
].rx_buffer
, idx
);
1503 } while (!last_fragment
);
1506 hdr
[1] = connection_id
;
1507 status
= copy_to_user(buf
, hdr
, 2);
1520 * Implementation of file open syscall.
1522 * @param inode Inode concerned.
1523 * @param file File concerned.
1525 * @return 0 on success, <0 on failure.
1527 static int dvb_ca_en50221_io_open(struct inode
*inode
, struct file
*file
)
1529 struct dvb_device
*dvbdev
= file
->private_data
;
1530 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1534 dprintk("%s\n", __func__
);
1536 if (!try_module_get(ca
->pub
->owner
))
1539 err
= dvb_generic_open(inode
, file
);
1541 module_put(ca
->pub
->owner
);
1545 for (i
= 0; i
< ca
->slot_count
; i
++) {
1547 if (ca
->slot_info
[i
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1548 if (ca
->slot_info
[i
].rx_buffer
.data
!= NULL
) {
1549 /* it is safe to call this here without locks because
1550 * ca->open == 0. Data is not read in this case */
1551 dvb_ringbuffer_flush(&ca
->slot_info
[i
].rx_buffer
);
1557 dvb_ca_en50221_thread_update_delay(ca
);
1558 dvb_ca_en50221_thread_wakeup(ca
);
1565 * Implementation of file close syscall.
1567 * @param inode Inode concerned.
1568 * @param file File concerned.
1570 * @return 0 on success, <0 on failure.
1572 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1574 struct dvb_device
*dvbdev
= file
->private_data
;
1575 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1578 dprintk("%s\n", __func__
);
1580 /* mark the CA device as closed */
1582 dvb_ca_en50221_thread_update_delay(ca
);
1584 err
= dvb_generic_release(inode
, file
);
1586 module_put(ca
->pub
->owner
);
1593 * Implementation of poll() syscall.
1595 * @param file File concerned.
1596 * @param wait poll wait table.
1598 * @return Standard poll mask.
1600 static unsigned int dvb_ca_en50221_io_poll(struct file
*file
, poll_table
* wait
)
1602 struct dvb_device
*dvbdev
= file
->private_data
;
1603 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1604 unsigned int mask
= 0;
1608 dprintk("%s\n", __func__
);
1610 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1614 /* if there is something, return now */
1618 /* wait for something to happen */
1619 poll_wait(file
, &ca
->wait_queue
, wait
);
1621 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1627 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1630 static const struct file_operations dvb_ca_fops
= {
1631 .owner
= THIS_MODULE
,
1632 .read
= dvb_ca_en50221_io_read
,
1633 .write
= dvb_ca_en50221_io_write
,
1634 .unlocked_ioctl
= dvb_ca_en50221_io_ioctl
,
1635 .open
= dvb_ca_en50221_io_open
,
1636 .release
= dvb_ca_en50221_io_release
,
1637 .poll
= dvb_ca_en50221_io_poll
,
1638 .llseek
= noop_llseek
,
1641 static struct dvb_device dvbdev_ca
= {
1646 .fops
= &dvb_ca_fops
,
1650 /* ******************************************************************************** */
1651 /* Initialisation/shutdown functions */
1655 * Initialise a new DVB CA EN50221 interface device.
1657 * @param dvb_adapter DVB adapter to attach the new CA device to.
1658 * @param ca The dvb_ca instance.
1659 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1660 * @param slot_count Number of slots supported.
1662 * @return 0 on success, nonzero on failure
1664 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1665 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1668 struct dvb_ca_private
*ca
= NULL
;
1671 dprintk("%s\n", __func__
);
1676 /* initialise the system data */
1677 if ((ca
= kzalloc(sizeof(struct dvb_ca_private
), GFP_KERNEL
)) == NULL
) {
1683 ca
->slot_count
= slot_count
;
1684 if ((ca
->slot_info
= kcalloc(slot_count
, sizeof(struct dvb_ca_slot
), GFP_KERNEL
)) == NULL
) {
1688 init_waitqueue_head(&ca
->wait_queue
);
1691 ca
->next_read_slot
= 0;
1692 pubca
->private = ca
;
1694 /* register the DVB device */
1695 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
, DVB_DEVICE_CA
);
1699 /* now initialise each slot */
1700 for (i
= 0; i
< slot_count
; i
++) {
1701 memset(&ca
->slot_info
[i
], 0, sizeof(struct dvb_ca_slot
));
1702 ca
->slot_info
[i
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1703 atomic_set(&ca
->slot_info
[i
].camchange_count
, 0);
1704 ca
->slot_info
[i
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1705 mutex_init(&ca
->slot_info
[i
].slot_lock
);
1708 mutex_init(&ca
->ioctl_mutex
);
1710 if (signal_pending(current
)) {
1716 /* create a kthread for monitoring this CA device */
1717 ca
->thread
= kthread_run(dvb_ca_en50221_thread
, ca
, "kdvb-ca-%i:%i",
1718 ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
1719 if (IS_ERR(ca
->thread
)) {
1720 ret
= PTR_ERR(ca
->thread
);
1721 printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
1729 if (ca
->dvbdev
!= NULL
)
1730 dvb_unregister_device(ca
->dvbdev
);
1731 kfree(ca
->slot_info
);
1734 pubca
->private = NULL
;
1737 EXPORT_SYMBOL(dvb_ca_en50221_release
);
1742 * Release a DVB CA EN50221 interface device.
1744 * @param ca_dev The dvb_device_t instance for the CA device.
1745 * @param ca The associated dvb_ca instance.
1747 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1749 struct dvb_ca_private
*ca
= pubca
->private;
1752 dprintk("%s\n", __func__
);
1754 /* shutdown the thread if there was one */
1755 kthread_stop(ca
->thread
);
1757 for (i
= 0; i
< ca
->slot_count
; i
++) {
1758 dvb_ca_en50221_slot_shutdown(ca
, i
);
1759 vfree(ca
->slot_info
[i
].rx_buffer
.data
);
1761 kfree(ca
->slot_info
);
1762 dvb_unregister_device(ca
->dvbdev
);
1764 pubca
->private = NULL
;